org.el 909 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515175161751717518175191752017521175221752317524175251752617527175281752917530175311753217533175341753517536175371753817539175401754117542175431754417545175461754717548175491755017551175521755317554175551755617557175581755917560175611756217563175641756517566175671756817569175701757117572175731757417575175761757717578175791758017581175821758317584175851758617587175881758917590175911759217593175941759517596175971759817599176001760117602176031760417605176061760717608176091761017611176121761317614176151761617617176181761917620176211762217623176241762517626176271762817629176301763117632176331763417635176361763717638176391764017641176421764317644176451764617647176481764917650176511765217653176541765517656176571765817659176601766117662176631766417665176661766717668176691767017671176721767317674176751767617677176781767917680176811768217683176841768517686176871768817689176901769117692176931769417695176961769717698176991770017701177021770317704177051770617707177081770917710177111771217713177141771517716177171771817719177201772117722177231772417725177261772717728177291773017731177321773317734177351773617737177381773917740177411774217743177441774517746177471774817749177501775117752177531775417755177561775717758177591776017761177621776317764177651776617767177681776917770177711777217773177741777517776177771777817779177801778117782177831778417785177861778717788177891779017791177921779317794177951779617797177981779917800178011780217803178041780517806178071780817809178101781117812178131781417815178161781717818178191782017821178221782317824178251782617827178281782917830178311783217833178341783517836178371783817839178401784117842178431784417845178461784717848178491785017851178521785317854178551785617857178581785917860178611786217863178641786517866178671786817869178701787117872178731787417875178761787717878178791788017881178821788317884178851788617887178881788917890178911789217893178941789517896178971789817899179001790117902179031790417905179061790717908179091791017911179121791317914179151791617917179181791917920179211792217923179241792517926179271792817929179301793117932179331793417935179361793717938179391794017941179421794317944179451794617947179481794917950179511795217953179541795517956179571795817959179601796117962179631796417965179661796717968179691797017971179721797317974179751797617977179781797917980179811798217983179841798517986179871798817989179901799117992179931799417995179961799717998179991800018001180021800318004180051800618007180081800918010180111801218013180141801518016180171801818019180201802118022180231802418025180261802718028180291803018031180321803318034180351803618037180381803918040180411804218043180441804518046180471804818049180501805118052180531805418055180561805718058180591806018061180621806318064180651806618067180681806918070180711807218073180741807518076180771807818079180801808118082180831808418085180861808718088180891809018091180921809318094180951809618097180981809918100181011810218103181041810518106181071810818109181101811118112181131811418115181161811718118181191812018121181221812318124181251812618127181281812918130181311813218133181341813518136181371813818139181401814118142181431814418145181461814718148181491815018151181521815318154181551815618157181581815918160181611816218163181641816518166181671816818169181701817118172181731817418175181761817718178181791818018181181821818318184181851818618187181881818918190181911819218193181941819518196181971819818199182001820118202182031820418205182061820718208182091821018211182121821318214182151821618217182181821918220182211822218223182241822518226182271822818229182301823118232182331823418235182361823718238182391824018241182421824318244182451824618247182481824918250182511825218253182541825518256182571825818259182601826118262182631826418265182661826718268182691827018271182721827318274182751827618277182781827918280182811828218283182841828518286182871828818289182901829118292182931829418295182961829718298182991830018301183021830318304183051830618307183081830918310183111831218313183141831518316183171831818319183201832118322183231832418325183261832718328183291833018331183321833318334183351833618337183381833918340183411834218343183441834518346183471834818349183501835118352183531835418355183561835718358183591836018361183621836318364183651836618367183681836918370183711837218373183741837518376183771837818379183801838118382183831838418385183861838718388183891839018391183921839318394183951839618397183981839918400184011840218403184041840518406184071840818409184101841118412184131841418415184161841718418184191842018421184221842318424184251842618427184281842918430184311843218433184341843518436184371843818439184401844118442184431844418445184461844718448184491845018451184521845318454184551845618457184581845918460184611846218463184641846518466184671846818469184701847118472184731847418475184761847718478184791848018481184821848318484184851848618487184881848918490184911849218493184941849518496184971849818499185001850118502185031850418505185061850718508185091851018511185121851318514185151851618517185181851918520185211852218523185241852518526185271852818529185301853118532185331853418535185361853718538185391854018541185421854318544185451854618547185481854918550185511855218553185541855518556185571855818559185601856118562185631856418565185661856718568185691857018571185721857318574185751857618577185781857918580185811858218583185841858518586185871858818589185901859118592185931859418595185961859718598185991860018601186021860318604186051860618607186081860918610186111861218613186141861518616186171861818619186201862118622186231862418625186261862718628186291863018631186321863318634186351863618637186381863918640186411864218643186441864518646186471864818649186501865118652186531865418655186561865718658186591866018661186621866318664186651866618667186681866918670186711867218673186741867518676186771867818679186801868118682186831868418685186861868718688186891869018691186921869318694186951869618697186981869918700187011870218703187041870518706187071870818709187101871118712187131871418715187161871718718187191872018721187221872318724187251872618727187281872918730187311873218733187341873518736187371873818739187401874118742187431874418745187461874718748187491875018751187521875318754187551875618757187581875918760187611876218763187641876518766187671876818769187701877118772187731877418775187761877718778187791878018781187821878318784187851878618787187881878918790187911879218793187941879518796187971879818799188001880118802188031880418805188061880718808188091881018811188121881318814188151881618817188181881918820188211882218823188241882518826188271882818829188301883118832188331883418835188361883718838188391884018841188421884318844188451884618847188481884918850188511885218853188541885518856188571885818859188601886118862188631886418865188661886718868188691887018871188721887318874188751887618877188781887918880188811888218883188841888518886188871888818889188901889118892188931889418895188961889718898188991890018901189021890318904189051890618907189081890918910189111891218913189141891518916189171891818919189201892118922189231892418925189261892718928189291893018931189321893318934189351893618937189381893918940189411894218943189441894518946189471894818949189501895118952189531895418955189561895718958189591896018961189621896318964189651896618967189681896918970189711897218973189741897518976189771897818979189801898118982189831898418985189861898718988189891899018991189921899318994189951899618997189981899919000190011900219003190041900519006190071900819009190101901119012190131901419015190161901719018190191902019021190221902319024190251902619027190281902919030190311903219033190341903519036190371903819039190401904119042190431904419045190461904719048190491905019051190521905319054190551905619057190581905919060190611906219063190641906519066190671906819069190701907119072190731907419075190761907719078190791908019081190821908319084190851908619087190881908919090190911909219093190941909519096190971909819099191001910119102191031910419105191061910719108191091911019111191121911319114191151911619117191181911919120191211912219123191241912519126191271912819129191301913119132191331913419135191361913719138191391914019141191421914319144191451914619147191481914919150191511915219153191541915519156191571915819159191601916119162191631916419165191661916719168191691917019171191721917319174191751917619177191781917919180191811918219183191841918519186191871918819189191901919119192191931919419195191961919719198191991920019201192021920319204192051920619207192081920919210192111921219213192141921519216192171921819219192201922119222192231922419225192261922719228192291923019231192321923319234192351923619237192381923919240192411924219243192441924519246192471924819249192501925119252192531925419255192561925719258192591926019261192621926319264192651926619267192681926919270192711927219273192741927519276192771927819279192801928119282192831928419285192861928719288192891929019291192921929319294192951929619297192981929919300193011930219303193041930519306193071930819309193101931119312193131931419315193161931719318193191932019321193221932319324193251932619327193281932919330193311933219333193341933519336193371933819339193401934119342193431934419345193461934719348193491935019351193521935319354193551935619357193581935919360193611936219363193641936519366193671936819369193701937119372193731937419375193761937719378193791938019381193821938319384193851938619387193881938919390193911939219393193941939519396193971939819399194001940119402194031940419405194061940719408194091941019411194121941319414194151941619417194181941919420194211942219423194241942519426194271942819429194301943119432194331943419435194361943719438194391944019441194421944319444194451944619447194481944919450194511945219453194541945519456194571945819459194601946119462194631946419465194661946719468194691947019471194721947319474194751947619477194781947919480194811948219483194841948519486194871948819489194901949119492194931949419495194961949719498194991950019501195021950319504195051950619507195081950919510195111951219513195141951519516195171951819519195201952119522195231952419525195261952719528195291953019531195321953319534195351953619537195381953919540195411954219543195441954519546195471954819549195501955119552195531955419555195561955719558195591956019561195621956319564195651956619567195681956919570195711957219573195741957519576195771957819579195801958119582195831958419585195861958719588195891959019591195921959319594195951959619597195981959919600196011960219603196041960519606196071960819609196101961119612196131961419615196161961719618196191962019621196221962319624196251962619627196281962919630196311963219633196341963519636196371963819639196401964119642196431964419645196461964719648196491965019651196521965319654196551965619657196581965919660196611966219663196641966519666196671966819669196701967119672196731967419675196761967719678196791968019681196821968319684196851968619687196881968919690196911969219693196941969519696196971969819699197001970119702197031970419705197061970719708197091971019711197121971319714197151971619717197181971919720197211972219723197241972519726197271972819729197301973119732197331973419735197361973719738197391974019741197421974319744197451974619747197481974919750197511975219753197541975519756197571975819759197601976119762197631976419765197661976719768197691977019771197721977319774197751977619777197781977919780197811978219783197841978519786197871978819789197901979119792197931979419795197961979719798197991980019801198021980319804198051980619807198081980919810198111981219813198141981519816198171981819819198201982119822198231982419825198261982719828198291983019831198321983319834198351983619837198381983919840198411984219843198441984519846198471984819849198501985119852198531985419855198561985719858198591986019861198621986319864198651986619867198681986919870198711987219873198741987519876198771987819879198801988119882198831988419885198861988719888198891989019891198921989319894198951989619897198981989919900199011990219903199041990519906199071990819909199101991119912199131991419915199161991719918199191992019921199221992319924199251992619927199281992919930199311993219933199341993519936199371993819939199401994119942199431994419945199461994719948199491995019951199521995319954199551995619957199581995919960199611996219963199641996519966199671996819969199701997119972199731997419975199761997719978199791998019981199821998319984199851998619987199881998919990199911999219993199941999519996199971999819999200002000120002200032000420005200062000720008200092001020011200122001320014200152001620017200182001920020200212002220023200242002520026200272002820029200302003120032200332003420035200362003720038200392004020041200422004320044200452004620047200482004920050200512005220053200542005520056200572005820059200602006120062200632006420065200662006720068200692007020071200722007320074200752007620077200782007920080200812008220083200842008520086200872008820089200902009120092200932009420095200962009720098200992010020101201022010320104201052010620107201082010920110201112011220113201142011520116201172011820119201202012120122201232012420125201262012720128201292013020131201322013320134201352013620137201382013920140201412014220143201442014520146201472014820149201502015120152201532015420155201562015720158201592016020161201622016320164201652016620167201682016920170201712017220173201742017520176201772017820179201802018120182201832018420185201862018720188201892019020191201922019320194201952019620197201982019920200202012020220203202042020520206202072020820209202102021120212202132021420215202162021720218202192022020221202222022320224202252022620227202282022920230202312023220233202342023520236202372023820239202402024120242202432024420245202462024720248202492025020251202522025320254202552025620257202582025920260202612026220263202642026520266202672026820269202702027120272202732027420275202762027720278202792028020281202822028320284202852028620287202882028920290202912029220293202942029520296202972029820299203002030120302203032030420305203062030720308203092031020311203122031320314203152031620317203182031920320203212032220323203242032520326203272032820329203302033120332203332033420335203362033720338203392034020341203422034320344203452034620347203482034920350203512035220353203542035520356203572035820359203602036120362203632036420365203662036720368203692037020371203722037320374203752037620377203782037920380203812038220383203842038520386203872038820389203902039120392203932039420395203962039720398203992040020401204022040320404204052040620407204082040920410204112041220413204142041520416204172041820419204202042120422204232042420425204262042720428204292043020431204322043320434204352043620437204382043920440204412044220443204442044520446204472044820449204502045120452204532045420455204562045720458204592046020461204622046320464204652046620467204682046920470204712047220473204742047520476204772047820479204802048120482204832048420485204862048720488204892049020491204922049320494204952049620497204982049920500205012050220503205042050520506205072050820509205102051120512205132051420515205162051720518205192052020521205222052320524205252052620527205282052920530205312053220533205342053520536205372053820539205402054120542205432054420545205462054720548205492055020551205522055320554205552055620557205582055920560205612056220563205642056520566205672056820569205702057120572205732057420575205762057720578205792058020581205822058320584205852058620587205882058920590205912059220593205942059520596205972059820599206002060120602206032060420605206062060720608206092061020611206122061320614206152061620617206182061920620206212062220623206242062520626206272062820629206302063120632206332063420635206362063720638206392064020641206422064320644206452064620647206482064920650206512065220653206542065520656206572065820659206602066120662206632066420665206662066720668206692067020671206722067320674206752067620677206782067920680206812068220683206842068520686206872068820689206902069120692206932069420695206962069720698206992070020701207022070320704207052070620707207082070920710207112071220713207142071520716207172071820719207202072120722207232072420725207262072720728207292073020731207322073320734207352073620737207382073920740207412074220743207442074520746207472074820749207502075120752207532075420755207562075720758207592076020761207622076320764207652076620767207682076920770207712077220773207742077520776207772077820779207802078120782207832078420785207862078720788207892079020791207922079320794207952079620797207982079920800208012080220803208042080520806208072080820809208102081120812208132081420815208162081720818208192082020821208222082320824208252082620827208282082920830208312083220833208342083520836208372083820839208402084120842208432084420845208462084720848208492085020851208522085320854208552085620857208582085920860208612086220863208642086520866208672086820869208702087120872208732087420875208762087720878208792088020881208822088320884208852088620887208882088920890208912089220893208942089520896208972089820899209002090120902209032090420905209062090720908209092091020911209122091320914209152091620917209182091920920209212092220923209242092520926209272092820929209302093120932209332093420935209362093720938209392094020941209422094320944209452094620947209482094920950209512095220953209542095520956209572095820959209602096120962209632096420965209662096720968209692097020971209722097320974209752097620977209782097920980209812098220983209842098520986209872098820989209902099120992209932099420995209962099720998209992100021001210022100321004210052100621007210082100921010210112101221013210142101521016210172101821019210202102121022210232102421025210262102721028210292103021031210322103321034210352103621037210382103921040210412104221043210442104521046210472104821049210502105121052210532105421055210562105721058210592106021061210622106321064210652106621067210682106921070210712107221073210742107521076210772107821079210802108121082210832108421085210862108721088210892109021091210922109321094210952109621097210982109921100211012110221103211042110521106211072110821109211102111121112211132111421115211162111721118211192112021121211222112321124211252112621127211282112921130211312113221133211342113521136211372113821139211402114121142211432114421145211462114721148211492115021151211522115321154211552115621157211582115921160211612116221163211642116521166211672116821169211702117121172211732117421175211762117721178211792118021181211822118321184211852118621187211882118921190211912119221193211942119521196211972119821199212002120121202212032120421205212062120721208212092121021211212122121321214212152121621217212182121921220212212122221223212242122521226212272122821229212302123121232212332123421235212362123721238212392124021241212422124321244212452124621247212482124921250212512125221253212542125521256212572125821259212602126121262212632126421265212662126721268212692127021271212722127321274212752127621277212782127921280212812128221283212842128521286212872128821289212902129121292212932129421295212962129721298212992130021301213022130321304213052130621307213082130921310213112131221313213142131521316213172131821319213202132121322213232132421325213262132721328213292133021331213322133321334213352133621337213382133921340213412134221343213442134521346213472134821349213502135121352213532135421355213562135721358213592136021361213622136321364213652136621367213682136921370213712137221373213742137521376213772137821379213802138121382213832138421385213862138721388213892139021391213922139321394213952139621397213982139921400214012140221403214042140521406214072140821409214102141121412214132141421415214162141721418214192142021421214222142321424214252142621427214282142921430214312143221433214342143521436214372143821439214402144121442214432144421445214462144721448214492145021451214522145321454214552145621457214582145921460214612146221463214642146521466214672146821469214702147121472214732147421475214762147721478214792148021481214822148321484214852148621487214882148921490214912149221493214942149521496214972149821499215002150121502215032150421505215062150721508215092151021511215122151321514215152151621517215182151921520215212152221523215242152521526215272152821529215302153121532215332153421535215362153721538215392154021541215422154321544215452154621547215482154921550215512155221553215542155521556215572155821559215602156121562215632156421565215662156721568215692157021571215722157321574215752157621577215782157921580215812158221583215842158521586215872158821589215902159121592215932159421595215962159721598215992160021601216022160321604216052160621607216082160921610216112161221613216142161521616216172161821619216202162121622216232162421625216262162721628216292163021631216322163321634216352163621637216382163921640216412164221643216442164521646216472164821649216502165121652216532165421655216562165721658216592166021661216622166321664216652166621667216682166921670216712167221673216742167521676216772167821679216802168121682216832168421685216862168721688216892169021691216922169321694216952169621697216982169921700217012170221703217042170521706217072170821709217102171121712217132171421715217162171721718217192172021721217222172321724217252172621727217282172921730217312173221733217342173521736217372173821739217402174121742217432174421745217462174721748217492175021751217522175321754217552175621757217582175921760217612176221763217642176521766217672176821769217702177121772217732177421775217762177721778217792178021781217822178321784217852178621787217882178921790217912179221793217942179521796217972179821799218002180121802218032180421805218062180721808218092181021811218122181321814218152181621817218182181921820218212182221823218242182521826218272182821829218302183121832218332183421835218362183721838218392184021841218422184321844218452184621847218482184921850218512185221853218542185521856218572185821859218602186121862218632186421865218662186721868218692187021871218722187321874218752187621877218782187921880218812188221883218842188521886218872188821889218902189121892218932189421895218962189721898218992190021901219022190321904219052190621907219082190921910219112191221913219142191521916219172191821919219202192121922219232192421925219262192721928219292193021931219322193321934219352193621937219382193921940219412194221943219442194521946219472194821949219502195121952219532195421955219562195721958219592196021961219622196321964219652196621967219682196921970219712197221973219742197521976219772197821979219802198121982219832198421985219862198721988219892199021991219922199321994219952199621997219982199922000220012200222003220042200522006220072200822009220102201122012220132201422015220162201722018220192202022021220222202322024220252202622027220282202922030220312203222033220342203522036220372203822039220402204122042220432204422045220462204722048220492205022051220522205322054220552205622057220582205922060220612206222063220642206522066220672206822069220702207122072220732207422075220762207722078220792208022081220822208322084220852208622087220882208922090220912209222093220942209522096220972209822099221002210122102221032210422105221062210722108221092211022111221122211322114221152211622117221182211922120221212212222123221242212522126221272212822129221302213122132221332213422135221362213722138221392214022141221422214322144221452214622147221482214922150221512215222153221542215522156221572215822159221602216122162221632216422165221662216722168221692217022171221722217322174221752217622177221782217922180221812218222183221842218522186221872218822189221902219122192221932219422195221962219722198221992220022201222022220322204222052220622207222082220922210222112221222213222142221522216222172221822219222202222122222222232222422225222262222722228222292223022231222322223322234222352223622237222382223922240222412224222243222442224522246222472224822249222502225122252222532225422255222562225722258222592226022261222622226322264222652226622267222682226922270222712227222273222742227522276222772227822279222802228122282222832228422285222862228722288222892229022291222922229322294222952229622297222982229922300223012230222303223042230522306223072230822309223102231122312223132231422315223162231722318223192232022321223222232322324223252232622327223282232922330223312233222333223342233522336223372233822339223402234122342223432234422345223462234722348223492235022351223522235322354223552235622357223582235922360223612236222363223642236522366223672236822369223702237122372223732237422375223762237722378223792238022381223822238322384223852238622387223882238922390223912239222393223942239522396223972239822399224002240122402224032240422405224062240722408224092241022411224122241322414224152241622417224182241922420224212242222423224242242522426224272242822429224302243122432224332243422435224362243722438224392244022441224422244322444224452244622447224482244922450224512245222453224542245522456224572245822459224602246122462224632246422465224662246722468224692247022471224722247322474224752247622477224782247922480224812248222483224842248522486224872248822489224902249122492224932249422495224962249722498224992250022501225022250322504225052250622507225082250922510225112251222513225142251522516225172251822519225202252122522225232252422525225262252722528225292253022531225322253322534225352253622537225382253922540225412254222543225442254522546225472254822549225502255122552225532255422555225562255722558225592256022561225622256322564225652256622567225682256922570225712257222573225742257522576225772257822579225802258122582225832258422585225862258722588225892259022591225922259322594225952259622597225982259922600226012260222603226042260522606226072260822609226102261122612226132261422615226162261722618226192262022621226222262322624226252262622627226282262922630226312263222633226342263522636226372263822639226402264122642226432264422645226462264722648226492265022651226522265322654226552265622657226582265922660226612266222663226642266522666226672266822669226702267122672226732267422675226762267722678226792268022681226822268322684226852268622687226882268922690226912269222693226942269522696226972269822699227002270122702227032270422705227062270722708227092271022711227122271322714227152271622717227182271922720227212272222723227242272522726227272272822729227302273122732227332273422735227362273722738227392274022741227422274322744227452274622747227482274922750227512275222753227542275522756227572275822759227602276122762227632276422765227662276722768227692277022771227722277322774227752277622777227782277922780227812278222783227842278522786227872278822789227902279122792227932279422795227962279722798227992280022801228022280322804228052280622807228082280922810228112281222813228142281522816228172281822819228202282122822228232282422825228262282722828228292283022831228322283322834228352283622837228382283922840228412284222843228442284522846228472284822849228502285122852228532285422855228562285722858228592286022861228622286322864228652286622867228682286922870228712287222873228742287522876228772287822879228802288122882228832288422885228862288722888228892289022891228922289322894228952289622897228982289922900229012290222903229042290522906229072290822909229102291122912229132291422915229162291722918229192292022921229222292322924229252292622927229282292922930229312293222933229342293522936229372293822939229402294122942229432294422945229462294722948229492295022951229522295322954229552295622957229582295922960229612296222963229642296522966229672296822969229702297122972229732297422975229762297722978229792298022981229822298322984229852298622987229882298922990229912299222993229942299522996229972299822999230002300123002230032300423005230062300723008230092301023011230122301323014230152301623017230182301923020230212302223023230242302523026230272302823029230302303123032230332303423035230362303723038230392304023041230422304323044230452304623047230482304923050230512305223053230542305523056230572305823059230602306123062230632306423065230662306723068230692307023071230722307323074230752307623077230782307923080230812308223083230842308523086230872308823089230902309123092230932309423095230962309723098230992310023101231022310323104231052310623107231082310923110231112311223113231142311523116231172311823119231202312123122231232312423125231262312723128231292313023131231322313323134231352313623137231382313923140231412314223143231442314523146231472314823149231502315123152231532315423155231562315723158231592316023161231622316323164231652316623167231682316923170231712317223173231742317523176231772317823179231802318123182231832318423185231862318723188231892319023191231922319323194231952319623197231982319923200232012320223203232042320523206232072320823209232102321123212232132321423215232162321723218232192322023221232222322323224232252322623227232282322923230232312323223233232342323523236232372323823239232402324123242232432324423245232462324723248232492325023251232522325323254232552325623257232582325923260232612326223263232642326523266232672326823269232702327123272232732327423275232762327723278232792328023281232822328323284232852328623287232882328923290232912329223293232942329523296232972329823299233002330123302233032330423305233062330723308233092331023311233122331323314233152331623317233182331923320233212332223323233242332523326233272332823329233302333123332233332333423335233362333723338233392334023341233422334323344233452334623347233482334923350233512335223353233542335523356233572335823359233602336123362233632336423365233662336723368233692337023371233722337323374233752337623377233782337923380233812338223383233842338523386233872338823389233902339123392233932339423395233962339723398233992340023401234022340323404234052340623407234082340923410234112341223413234142341523416234172341823419234202342123422234232342423425234262342723428234292343023431234322343323434234352343623437234382343923440234412344223443234442344523446234472344823449234502345123452234532345423455234562345723458234592346023461234622346323464234652346623467234682346923470234712347223473234742347523476234772347823479234802348123482234832348423485234862348723488234892349023491234922349323494234952349623497234982349923500235012350223503235042350523506235072350823509235102351123512235132351423515235162351723518235192352023521235222352323524235252352623527235282352923530235312353223533235342353523536235372353823539235402354123542235432354423545235462354723548235492355023551235522355323554235552355623557235582355923560235612356223563235642356523566235672356823569235702357123572235732357423575235762357723578235792358023581235822358323584235852358623587235882358923590235912359223593235942359523596235972359823599236002360123602236032360423605236062360723608236092361023611236122361323614236152361623617236182361923620236212362223623236242362523626236272362823629236302363123632236332363423635236362363723638236392364023641236422364323644236452364623647236482364923650236512365223653236542365523656236572365823659236602366123662236632366423665236662366723668236692367023671236722367323674236752367623677236782367923680236812368223683236842368523686236872368823689236902369123692236932369423695236962369723698236992370023701237022370323704237052370623707237082370923710237112371223713237142371523716237172371823719237202372123722237232372423725237262372723728237292373023731237322373323734237352373623737237382373923740237412374223743237442374523746237472374823749237502375123752237532375423755237562375723758237592376023761237622376323764237652376623767237682376923770237712377223773237742377523776237772377823779237802378123782237832378423785237862378723788237892379023791237922379323794237952379623797237982379923800238012380223803238042380523806238072380823809238102381123812238132381423815238162381723818238192382023821238222382323824238252382623827238282382923830238312383223833238342383523836238372383823839238402384123842238432384423845238462384723848238492385023851238522385323854238552385623857238582385923860238612386223863238642386523866238672386823869238702387123872238732387423875238762387723878238792388023881238822388323884238852388623887238882388923890238912389223893238942389523896238972389823899239002390123902239032390423905239062390723908239092391023911239122391323914239152391623917239182391923920239212392223923239242392523926239272392823929239302393123932239332393423935239362393723938239392394023941239422394323944239452394623947239482394923950239512395223953239542395523956239572395823959239602396123962239632396423965239662396723968239692397023971239722397323974239752397623977239782397923980239812398223983239842398523986239872398823989239902399123992239932399423995239962399723998239992400024001
  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 s 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-absolute-from-iso "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. Match group 3 will be set to the value if it exists."
  5485. (concat "^\\(?4:[ \t]*\\)\\(?1::\\(?2:"
  5486. (if literal property (regexp-quote property))
  5487. "\\):\\)[ \t]+\\(?3:[^ \t\r\n]"
  5488. (if allow-null "*")
  5489. ".*?\\)\\(?5:[ \t]*\\)$"))
  5490. (defconst org-property-re
  5491. (org-re-property ".*?" 'literal t)
  5492. "Regular expression matching a property line.
  5493. There are four matching groups:
  5494. 1: :PROPKEY: including the leading and trailing colon,
  5495. 2: PROPKEY without the leading and trailing colon,
  5496. 3: PROPVAL without leading or trailing spaces,
  5497. 4: the indentation of the current line,
  5498. 5: trailing whitespace.")
  5499. (defvar org-font-lock-hook nil
  5500. "Functions to be called for special font lock stuff.")
  5501. (defvar org-font-lock-set-keywords-hook nil
  5502. "Functions that can manipulate `org-font-lock-extra-keywords'.
  5503. This is called after `org-font-lock-extra-keywords' is defined, but before
  5504. it is installed to be used by font lock. This can be useful if something
  5505. needs to be inserted at a specific position in the font-lock sequence.")
  5506. (defun org-font-lock-hook (limit)
  5507. "Run `org-font-lock-hook' within LIMIT."
  5508. (run-hook-with-args 'org-font-lock-hook limit))
  5509. (defun org-set-font-lock-defaults ()
  5510. "Set font lock defaults for the current buffer."
  5511. (let* ((em org-fontify-emphasized-text)
  5512. (lk org-activate-links)
  5513. (org-font-lock-extra-keywords
  5514. (list
  5515. ;; Call the hook
  5516. '(org-font-lock-hook)
  5517. ;; Headlines
  5518. `(,(if org-fontify-whole-heading-line
  5519. "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
  5520. "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
  5521. (1 (org-get-level-face 1))
  5522. (2 (org-get-level-face 2))
  5523. (3 (org-get-level-face 3)))
  5524. ;; Table lines
  5525. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  5526. (1 'org-table t))
  5527. ;; Table internals
  5528. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  5529. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  5530. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  5531. '("| *\\(<[lrc]?[0-9]*>\\)" (1 'org-formula t))
  5532. ;; Drawers
  5533. (list org-drawer-regexp '(0 'org-special-keyword t))
  5534. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  5535. ;; Properties
  5536. (list org-property-re
  5537. '(1 'org-special-keyword t)
  5538. '(3 'org-property-value t))
  5539. ;; Links
  5540. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  5541. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  5542. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  5543. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  5544. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  5545. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  5546. (if (memq 'footnote lk) '(org-activate-footnote-links))
  5547. ;; Targets.
  5548. (list org-any-target-regexp '(0 'org-target t))
  5549. ;; Diary sexps.
  5550. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  5551. ;; Macro
  5552. '("{{{.+}}}" (0 'org-macro t))
  5553. '(org-hide-wide-columns (0 nil append))
  5554. ;; TODO keyword
  5555. (list (format org-heading-keyword-regexp-format
  5556. org-todo-regexp)
  5557. '(2 (org-get-todo-face 2) t))
  5558. ;; DONE
  5559. (if org-fontify-done-headline
  5560. (list (format org-heading-keyword-regexp-format
  5561. (concat
  5562. "\\(?:"
  5563. (mapconcat 'regexp-quote org-done-keywords "\\|")
  5564. "\\)"))
  5565. '(2 'org-headline-done t))
  5566. nil)
  5567. ;; Priorities
  5568. '(org-font-lock-add-priority-faces)
  5569. ;; Tags
  5570. '(org-font-lock-add-tag-faces)
  5571. ;; Tags groups
  5572. (if (and org-group-tags org-tag-groups-alist)
  5573. (list (concat org-outline-regexp-bol ".+\\(:"
  5574. (regexp-opt (mapcar 'car org-tag-groups-alist))
  5575. ":\\).*$")
  5576. '(1 'org-tag-group prepend)))
  5577. ;; Special keywords
  5578. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  5579. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  5580. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  5581. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  5582. ;; Emphasis
  5583. (if em
  5584. (if (featurep 'xemacs)
  5585. '(org-do-emphasis-faces (0 nil append))
  5586. '(org-do-emphasis-faces)))
  5587. ;; Checkboxes
  5588. '("^[ \t]*\\(?:[-+*]\\|[0-9]+[.)]\\)[ \t]+\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?\\(\\[[- X]\\]\\)"
  5589. 1 'org-checkbox prepend)
  5590. (if (cdr (assq 'checkbox org-list-automatic-rules))
  5591. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  5592. (0 (org-get-checkbox-statistics-face) t)))
  5593. ;; Description list items
  5594. '("^[ \t]*[-+*][ \t]+\\(.*?[ \t]+::\\)\\([ \t]+\\|$\\)"
  5595. 1 'org-list-dt prepend)
  5596. ;; ARCHIVEd headings
  5597. (list (concat
  5598. org-outline-regexp-bol
  5599. "\\(.*:" org-archive-tag ":.*\\)")
  5600. '(1 'org-archived prepend))
  5601. ;; Specials
  5602. '(org-do-latex-and-related)
  5603. '(org-fontify-entities)
  5604. '(org-raise-scripts)
  5605. ;; Code
  5606. '(org-activate-code (1 'org-code t))
  5607. ;; COMMENT
  5608. (list (format org-heading-keyword-regexp-format
  5609. (concat "\\("
  5610. org-comment-string "\\|" org-quote-string
  5611. "\\)"))
  5612. '(2 'org-special-keyword t))
  5613. ;; Blocks and meta lines
  5614. '(org-fontify-meta-lines-and-blocks))))
  5615. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  5616. (run-hooks 'org-font-lock-set-keywords-hook)
  5617. ;; Now set the full font-lock-keywords
  5618. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  5619. (org-set-local 'font-lock-defaults
  5620. '(org-font-lock-keywords t nil nil backward-paragraph))
  5621. (kill-local-variable 'font-lock-keywords) nil))
  5622. (defun org-toggle-pretty-entities ()
  5623. "Toggle the composition display of entities as UTF8 characters."
  5624. (interactive)
  5625. (org-set-local 'org-pretty-entities (not org-pretty-entities))
  5626. (org-restart-font-lock)
  5627. (if org-pretty-entities
  5628. (message "Entities are now displayed as UTF8 characters")
  5629. (save-restriction
  5630. (widen)
  5631. (org-decompose-region (point-min) (point-max))
  5632. (message "Entities are now displayed as plain text"))))
  5633. (defvar org-custom-properties-overlays nil
  5634. "List of overlays used for custom properties.")
  5635. (make-variable-buffer-local 'org-custom-properties-overlays)
  5636. (defun org-toggle-custom-properties-visibility ()
  5637. "Display or hide properties in `org-custom-properties'."
  5638. (interactive)
  5639. (if org-custom-properties-overlays
  5640. (progn (mapc 'delete-overlay org-custom-properties-overlays)
  5641. (setq org-custom-properties-overlays nil))
  5642. (unless (not org-custom-properties)
  5643. (save-excursion
  5644. (save-restriction
  5645. (widen)
  5646. (goto-char (point-min))
  5647. (while (re-search-forward org-property-re nil t)
  5648. (mapc (lambda(p)
  5649. (when (equal p (substring (match-string 1) 1 -1))
  5650. (let ((o (make-overlay (match-beginning 0) (1+ (match-end 0)))))
  5651. (overlay-put o 'invisible t)
  5652. (overlay-put o 'org-custom-property t)
  5653. (push o org-custom-properties-overlays))))
  5654. org-custom-properties)))))))
  5655. (defun org-fontify-entities (limit)
  5656. "Find an entity to fontify."
  5657. (let (ee)
  5658. (when org-pretty-entities
  5659. (catch 'match
  5660. (while (re-search-forward
  5661. "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]\n]\\)"
  5662. limit t)
  5663. (if (and (not (org-in-indented-comment-line))
  5664. (setq ee (org-entity-get (match-string 1)))
  5665. (= (length (nth 6 ee)) 1))
  5666. (let*
  5667. ((end (if (equal (match-string 2) "{}")
  5668. (match-end 2)
  5669. (match-end 1))))
  5670. (add-text-properties
  5671. (match-beginning 0) end
  5672. (list 'font-lock-fontified t))
  5673. (compose-region (match-beginning 0) end
  5674. (nth 6 ee) nil)
  5675. (backward-char 1)
  5676. (throw 'match t))))
  5677. nil))))
  5678. (defun org-fontify-like-in-org-mode (s &optional odd-levels)
  5679. "Fontify string S like in Org-mode."
  5680. (with-temp-buffer
  5681. (insert s)
  5682. (let ((org-odd-levels-only odd-levels))
  5683. (org-mode)
  5684. (font-lock-fontify-buffer)
  5685. (buffer-string))))
  5686. (defvar org-m nil)
  5687. (defvar org-l nil)
  5688. (defvar org-f nil)
  5689. (defun org-get-level-face (n)
  5690. "Get the right face for match N in font-lock matching of headlines."
  5691. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  5692. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  5693. (if org-cycle-level-faces
  5694. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  5695. (setq org-f (nth (1- (min org-l org-n-level-faces)) org-level-faces)))
  5696. (cond
  5697. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  5698. ((eq n 2) org-f)
  5699. (t (if org-level-color-stars-only nil org-f))))
  5700. (defun org-get-todo-face (kwd)
  5701. "Get the right face for a TODO keyword KWD.
  5702. If KWD is a number, get the corresponding match group."
  5703. (if (numberp kwd) (setq kwd (match-string kwd)))
  5704. (or (org-face-from-face-or-color
  5705. 'todo 'org-todo (cdr (assoc kwd org-todo-keyword-faces)))
  5706. (and (member kwd org-done-keywords) 'org-done)
  5707. 'org-todo))
  5708. (defun org-face-from-face-or-color (context inherit face-or-color)
  5709. "Create a face list that inherits INHERIT, but sets the foreground color.
  5710. When FACE-OR-COLOR is not a string, just return it."
  5711. (if (stringp face-or-color)
  5712. (list :inherit inherit
  5713. (cdr (assoc context org-faces-easy-properties))
  5714. face-or-color)
  5715. face-or-color))
  5716. (defun org-font-lock-add-tag-faces (limit)
  5717. "Add the special tag faces."
  5718. (when (and org-tag-faces org-tags-special-faces-re)
  5719. (while (re-search-forward org-tags-special-faces-re limit t)
  5720. (add-text-properties (match-beginning 1) (match-end 1)
  5721. (list 'face (org-get-tag-face 1)
  5722. 'font-lock-fontified t))
  5723. (backward-char 1))))
  5724. (defun org-font-lock-add-priority-faces (limit)
  5725. "Add the special priority faces."
  5726. (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
  5727. (when (save-match-data (org-at-heading-p))
  5728. (add-text-properties
  5729. (match-beginning 0) (match-end 0)
  5730. (list 'face (or (org-face-from-face-or-color
  5731. 'priority 'org-priority
  5732. (cdr (assoc (char-after (match-beginning 1))
  5733. org-priority-faces)))
  5734. 'org-priority)
  5735. 'font-lock-fontified t)))))
  5736. (defun org-get-tag-face (kwd)
  5737. "Get the right face for a TODO keyword KWD.
  5738. If KWD is a number, get the corresponding match group."
  5739. (if (numberp kwd) (setq kwd (match-string kwd)))
  5740. (or (org-face-from-face-or-color
  5741. 'tag 'org-tag (cdr (assoc kwd org-tag-faces)))
  5742. 'org-tag))
  5743. (defun org-unfontify-region (beg end &optional maybe_loudly)
  5744. "Remove fontification and activation overlays from links."
  5745. (font-lock-default-unfontify-region beg end)
  5746. (let* ((buffer-undo-list t)
  5747. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  5748. (inhibit-modification-hooks t)
  5749. deactivate-mark buffer-file-name buffer-file-truename)
  5750. (org-decompose-region beg end)
  5751. (remove-text-properties beg end
  5752. '(mouse-face t keymap t org-linked-text t
  5753. invisible t intangible t
  5754. org-no-flyspell t org-emphasis t))
  5755. (org-remove-font-lock-display-properties beg end)))
  5756. (defconst org-script-display '(((raise -0.3) (height 0.7))
  5757. ((raise 0.3) (height 0.7))
  5758. ((raise -0.5))
  5759. ((raise 0.5)))
  5760. "Display properties for showing superscripts and subscripts.")
  5761. (defun org-remove-font-lock-display-properties (beg end)
  5762. "Remove specific display properties that have been added by font lock.
  5763. The will remove the raise properties that are used to show superscripts
  5764. and subscripts."
  5765. (let (next prop)
  5766. (while (< beg end)
  5767. (setq next (next-single-property-change beg 'display nil end)
  5768. prop (get-text-property beg 'display))
  5769. (if (member prop org-script-display)
  5770. (put-text-property beg next 'display nil))
  5771. (setq beg next))))
  5772. (defun org-raise-scripts (limit)
  5773. "Add raise properties to sub/superscripts."
  5774. (when (and org-pretty-entities org-pretty-entities-include-sub-superscripts)
  5775. (if (re-search-forward
  5776. (if (eq org-use-sub-superscripts t)
  5777. org-match-substring-regexp
  5778. org-match-substring-with-braces-regexp)
  5779. limit t)
  5780. (let* ((pos (point)) table-p comment-p
  5781. (mpos (match-beginning 3))
  5782. (emph-p (get-text-property mpos 'org-emphasis))
  5783. (link-p (get-text-property mpos 'mouse-face))
  5784. (keyw-p (eq 'org-special-keyword (get-text-property mpos 'face))))
  5785. (goto-char (point-at-bol))
  5786. (setq table-p (org-looking-at-p org-table-dataline-regexp)
  5787. comment-p (org-looking-at-p "^[ \t]*#[ +]"))
  5788. (goto-char pos)
  5789. ;; Handle a_b^c
  5790. (if (member (char-after) '(?_ ?^)) (goto-char (1- pos)))
  5791. (if (or comment-p emph-p link-p keyw-p)
  5792. t
  5793. (put-text-property (match-beginning 3) (match-end 0)
  5794. 'display
  5795. (if (equal (char-after (match-beginning 2)) ?^)
  5796. (nth (if table-p 3 1) org-script-display)
  5797. (nth (if table-p 2 0) org-script-display)))
  5798. (add-text-properties (match-beginning 2) (match-end 2)
  5799. (list 'invisible t
  5800. 'org-dwidth t 'org-dwidth-n 1))
  5801. (if (and (eq (char-after (match-beginning 3)) ?{)
  5802. (eq (char-before (match-end 3)) ?}))
  5803. (progn
  5804. (add-text-properties
  5805. (match-beginning 3) (1+ (match-beginning 3))
  5806. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))
  5807. (add-text-properties
  5808. (1- (match-end 3)) (match-end 3)
  5809. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))))
  5810. t)))))
  5811. ;;;; Visibility cycling, including org-goto and indirect buffer
  5812. ;;; Cycling
  5813. (defvar org-cycle-global-status nil)
  5814. (make-variable-buffer-local 'org-cycle-global-status)
  5815. (put 'org-cycle-global-status 'org-state t)
  5816. (defvar org-cycle-subtree-status nil)
  5817. (make-variable-buffer-local 'org-cycle-subtree-status)
  5818. (put 'org-cycle-subtree-status 'org-state t)
  5819. (defvar org-inlinetask-min-level)
  5820. (defun org-unlogged-message (&rest args)
  5821. "Display a message, but avoid logging it in the *Messages* buffer."
  5822. (let ((message-log-max nil))
  5823. (apply 'message args)))
  5824. ;;;###autoload
  5825. (defun org-cycle (&optional arg)
  5826. "TAB-action and visibility cycling for Org-mode.
  5827. This is the command invoked in Org-mode by the TAB key. Its main purpose
  5828. is outline visibility cycling, but it also invokes other actions
  5829. in special contexts.
  5830. - When this function is called with a prefix argument, rotate the entire
  5831. buffer through 3 states (global cycling)
  5832. 1. OVERVIEW: Show only top-level headlines.
  5833. 2. CONTENTS: Show all headlines of all levels, but no body text.
  5834. 3. SHOW ALL: Show everything.
  5835. When called with two `C-u C-u' prefixes, switch to the startup visibility,
  5836. determined by the variable `org-startup-folded', and by any VISIBILITY
  5837. properties in the buffer.
  5838. When called with three `C-u C-u C-u' prefixed, show the entire buffer,
  5839. including any drawers.
  5840. - When inside a table, re-align the table and move to the next field.
  5841. - When point is at the beginning of a headline, rotate the subtree started
  5842. by this line through 3 different states (local cycling)
  5843. 1. FOLDED: Only the main headline is shown.
  5844. 2. CHILDREN: The main headline and the direct children are shown.
  5845. From this state, you can move to one of the children
  5846. and zoom in further.
  5847. 3. SUBTREE: Show the entire subtree, including body text.
  5848. If there is no subtree, switch directly from CHILDREN to FOLDED.
  5849. - When point is at the beginning of an empty headline and the variable
  5850. `org-cycle-level-after-item/entry-creation' is set, cycle the level
  5851. of the headline by demoting and promoting it to likely levels. This
  5852. speeds up creation document structure by pressing TAB once or several
  5853. times right after creating a new headline.
  5854. - When there is a numeric prefix, go up to a heading with level ARG, do
  5855. a `show-subtree' and return to the previous cursor position. If ARG
  5856. is negative, go up that many levels.
  5857. - When point is not at the beginning of a headline, execute the global
  5858. binding for TAB, which is re-indenting the line. See the option
  5859. `org-cycle-emulate-tab' for details.
  5860. - Special case: if point is at the beginning of the buffer and there is
  5861. no headline in line 1, this function will act as if called with prefix arg
  5862. (C-u TAB, same as S-TAB) also when called without prefix arg.
  5863. But only if also the variable `org-cycle-global-at-bob' is t."
  5864. (interactive "P")
  5865. (org-load-modules-maybe)
  5866. (unless (or (run-hook-with-args-until-success 'org-tab-first-hook)
  5867. (and org-cycle-level-after-item/entry-creation
  5868. (or (org-cycle-level)
  5869. (org-cycle-item-indentation))))
  5870. (let* ((limit-level
  5871. (or org-cycle-max-level
  5872. (and (boundp 'org-inlinetask-min-level)
  5873. org-inlinetask-min-level
  5874. (1- org-inlinetask-min-level))))
  5875. (nstars (and limit-level
  5876. (if org-odd-levels-only
  5877. (and limit-level (1- (* limit-level 2)))
  5878. limit-level)))
  5879. (org-outline-regexp
  5880. (if (not (derived-mode-p 'org-mode))
  5881. outline-regexp
  5882. (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ "))))
  5883. (bob-special (and org-cycle-global-at-bob (not arg) (bobp)
  5884. (not (looking-at org-outline-regexp))))
  5885. (org-cycle-hook
  5886. (if bob-special
  5887. (delq 'org-optimize-window-after-visibility-change
  5888. (copy-sequence org-cycle-hook))
  5889. org-cycle-hook))
  5890. (pos (point)))
  5891. (if (or bob-special (equal arg '(4)))
  5892. ;; special case: use global cycling
  5893. (setq arg t))
  5894. (cond
  5895. ((equal arg '(16))
  5896. (setq last-command 'dummy)
  5897. (org-set-startup-visibility)
  5898. (org-unlogged-message "Startup visibility, plus VISIBILITY properties"))
  5899. ((equal arg '(64))
  5900. (show-all)
  5901. (org-unlogged-message "Entire buffer visible, including drawers"))
  5902. ;; Try cdlatex TAB completion
  5903. ((org-try-cdlatex-tab))
  5904. ;; Table: enter it or move to the next field.
  5905. ((org-at-table-p 'any)
  5906. (if (org-at-table.el-p)
  5907. (message "Use C-c ' to edit table.el tables")
  5908. (if arg (org-table-edit-field t)
  5909. (org-table-justify-field-maybe)
  5910. (call-interactively 'org-table-next-field))))
  5911. ((run-hook-with-args-until-success
  5912. 'org-tab-after-check-for-table-hook))
  5913. ;; Global cycling: delegate to `org-cycle-internal-global'.
  5914. ((eq arg t) (org-cycle-internal-global))
  5915. ;; Drawers: delegate to `org-flag-drawer'.
  5916. ((and org-drawers org-drawer-regexp
  5917. (save-excursion
  5918. (beginning-of-line 1)
  5919. (looking-at org-drawer-regexp)))
  5920. (org-flag-drawer ; toggle block visibility
  5921. (not (get-char-property (match-end 0) 'invisible))))
  5922. ;; Show-subtree, ARG levels up from here.
  5923. ((integerp arg)
  5924. (save-excursion
  5925. (org-back-to-heading)
  5926. (outline-up-heading (if (< arg 0) (- arg)
  5927. (- (funcall outline-level) arg)))
  5928. (org-show-subtree)))
  5929. ;; Inline task: delegate to `org-inlinetask-toggle-visibility'.
  5930. ((and (featurep 'org-inlinetask)
  5931. (org-inlinetask-at-task-p)
  5932. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5933. (org-inlinetask-toggle-visibility))
  5934. ;; At an item/headline: delegate to `org-cycle-internal-local'.
  5935. ((and (or (and org-cycle-include-plain-lists (org-at-item-p))
  5936. (save-excursion (beginning-of-line 1)
  5937. (looking-at org-outline-regexp)))
  5938. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5939. (org-cycle-internal-local))
  5940. ;; From there: TAB emulation and template completion.
  5941. (buffer-read-only (org-back-to-heading))
  5942. ((run-hook-with-args-until-success
  5943. 'org-tab-after-check-for-cycling-hook))
  5944. ((org-try-structure-completion))
  5945. ((run-hook-with-args-until-success
  5946. 'org-tab-before-tab-emulation-hook))
  5947. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  5948. (or (not (bolp))
  5949. (not (looking-at org-outline-regexp))))
  5950. (call-interactively (global-key-binding "\t")))
  5951. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  5952. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  5953. (or (and (eq org-cycle-emulate-tab 'white)
  5954. (= (match-end 0) (point-at-eol)))
  5955. (and (eq org-cycle-emulate-tab 'whitestart)
  5956. (>= (match-end 0) pos))))
  5957. t
  5958. (eq org-cycle-emulate-tab t))
  5959. (call-interactively (global-key-binding "\t")))
  5960. (t (save-excursion
  5961. (org-back-to-heading)
  5962. (org-cycle)))))))
  5963. (defun org-cycle-internal-global ()
  5964. "Do the global cycling action."
  5965. ;; Hack to avoid display of messages for .org attachments in Gnus
  5966. (let ((ga (string-match "\\*fontification" (buffer-name))))
  5967. (cond
  5968. ((and (eq last-command this-command)
  5969. (eq org-cycle-global-status 'overview))
  5970. ;; We just created the overview - now do table of contents
  5971. ;; This can be slow in very large buffers, so indicate action
  5972. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  5973. (unless ga (org-unlogged-message "CONTENTS..."))
  5974. (org-content)
  5975. (unless ga (org-unlogged-message "CONTENTS...done"))
  5976. (setq org-cycle-global-status 'contents)
  5977. (run-hook-with-args 'org-cycle-hook 'contents))
  5978. ((and (eq last-command this-command)
  5979. (eq org-cycle-global-status 'contents))
  5980. ;; We just showed the table of contents - now show everything
  5981. (run-hook-with-args 'org-pre-cycle-hook 'all)
  5982. (show-all)
  5983. (unless ga (org-unlogged-message "SHOW ALL"))
  5984. (setq org-cycle-global-status 'all)
  5985. (run-hook-with-args 'org-cycle-hook 'all))
  5986. (t
  5987. ;; Default action: go to overview
  5988. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  5989. (org-overview)
  5990. (unless ga (org-unlogged-message "OVERVIEW"))
  5991. (setq org-cycle-global-status 'overview)
  5992. (run-hook-with-args 'org-cycle-hook 'overview)))))
  5993. (defvar org-called-with-limited-levels);Dyn-bound in ̀org-with-limited-levels'.
  5994. (defun org-cycle-internal-local ()
  5995. "Do the local cycling action."
  5996. (let ((goal-column 0) eoh eol eos has-children children-skipped struct)
  5997. ;; First, determine end of headline (EOH), end of subtree or item
  5998. ;; (EOS), and if item or heading has children (HAS-CHILDREN).
  5999. (save-excursion
  6000. (if (org-at-item-p)
  6001. (progn
  6002. (beginning-of-line)
  6003. (setq struct (org-list-struct))
  6004. (setq eoh (point-at-eol))
  6005. (setq eos (org-list-get-item-end-before-blank (point) struct))
  6006. (setq has-children (org-list-has-child-p (point) struct)))
  6007. (org-back-to-heading)
  6008. (setq eoh (save-excursion (outline-end-of-heading) (point)))
  6009. (setq eos (save-excursion (org-end-of-subtree t t)
  6010. (when (bolp) (backward-char)) (point)))
  6011. (setq has-children
  6012. (or (save-excursion
  6013. (let ((level (funcall outline-level)))
  6014. (outline-next-heading)
  6015. (and (org-at-heading-p t)
  6016. (> (funcall outline-level) level))))
  6017. (save-excursion
  6018. (org-list-search-forward (org-item-beginning-re) eos t)))))
  6019. ;; Determine end invisible part of buffer (EOL)
  6020. (beginning-of-line 2)
  6021. ;; XEmacs doesn't have `next-single-char-property-change'
  6022. (if (featurep 'xemacs)
  6023. (while (and (not (eobp)) ;; this is like `next-line'
  6024. (get-char-property (1- (point)) 'invisible))
  6025. (beginning-of-line 2))
  6026. (while (and (not (eobp)) ;; this is like `next-line'
  6027. (get-char-property (1- (point)) 'invisible))
  6028. (goto-char (next-single-char-property-change (point) 'invisible))
  6029. (and (eolp) (beginning-of-line 2))))
  6030. (setq eol (point)))
  6031. ;; Find out what to do next and set `this-command'
  6032. (cond
  6033. ((= eos eoh)
  6034. ;; Nothing is hidden behind this heading
  6035. (unless (org-before-first-heading-p)
  6036. (run-hook-with-args 'org-pre-cycle-hook 'empty))
  6037. (org-unlogged-message "EMPTY ENTRY")
  6038. (setq org-cycle-subtree-status nil)
  6039. (save-excursion
  6040. (goto-char eos)
  6041. (outline-next-heading)
  6042. (if (outline-invisible-p) (org-flag-heading nil))))
  6043. ((and (or (>= eol eos)
  6044. (not (string-match "\\S-" (buffer-substring eol eos))))
  6045. (or has-children
  6046. (not (setq children-skipped
  6047. org-cycle-skip-children-state-if-no-children))))
  6048. ;; Entire subtree is hidden in one line: children view
  6049. (unless (org-before-first-heading-p)
  6050. (run-hook-with-args 'org-pre-cycle-hook 'children))
  6051. (if (org-at-item-p)
  6052. (org-list-set-item-visibility (point-at-bol) struct 'children)
  6053. (org-show-entry)
  6054. (org-with-limited-levels (show-children))
  6055. ;; FIXME: This slows down the func way too much.
  6056. ;; How keep drawers hidden in subtree anyway?
  6057. ;; (when (memq 'org-cycle-hide-drawers org-cycle-hook)
  6058. ;; (org-cycle-hide-drawers 'subtree))
  6059. ;; Fold every list in subtree to top-level items.
  6060. (when (eq org-cycle-include-plain-lists 'integrate)
  6061. (save-excursion
  6062. (org-back-to-heading)
  6063. (while (org-list-search-forward (org-item-beginning-re) eos t)
  6064. (beginning-of-line 1)
  6065. (let* ((struct (org-list-struct))
  6066. (prevs (org-list-prevs-alist struct))
  6067. (end (org-list-get-bottom-point struct)))
  6068. (mapc (lambda (e) (org-list-set-item-visibility e struct 'folded))
  6069. (org-list-get-all-items (point) struct prevs))
  6070. (goto-char (if (< end eos) end eos)))))))
  6071. (org-unlogged-message "CHILDREN")
  6072. (save-excursion
  6073. (goto-char eos)
  6074. (outline-next-heading)
  6075. (if (outline-invisible-p) (org-flag-heading nil)))
  6076. (setq org-cycle-subtree-status 'children)
  6077. (unless (org-before-first-heading-p)
  6078. (run-hook-with-args 'org-cycle-hook 'children)))
  6079. ((or children-skipped
  6080. (and (eq last-command this-command)
  6081. (eq org-cycle-subtree-status 'children)))
  6082. ;; We just showed the children, or no children are there,
  6083. ;; now show everything.
  6084. (unless (org-before-first-heading-p)
  6085. (run-hook-with-args 'org-pre-cycle-hook 'subtree))
  6086. (outline-flag-region eoh eos nil)
  6087. (org-unlogged-message
  6088. (if children-skipped "SUBTREE (NO CHILDREN)" "SUBTREE"))
  6089. (setq org-cycle-subtree-status 'subtree)
  6090. (unless (org-before-first-heading-p)
  6091. (run-hook-with-args 'org-cycle-hook 'subtree)))
  6092. (t
  6093. ;; Default action: hide the subtree.
  6094. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  6095. (outline-flag-region eoh eos t)
  6096. (org-unlogged-message "FOLDED")
  6097. (setq org-cycle-subtree-status 'folded)
  6098. (unless (org-before-first-heading-p)
  6099. (run-hook-with-args 'org-cycle-hook 'folded))))))
  6100. ;;;###autoload
  6101. (defun org-global-cycle (&optional arg)
  6102. "Cycle the global visibility. For details see `org-cycle'.
  6103. With \\[universal-argument] prefix arg, switch to startup visibility.
  6104. With a numeric prefix, show all headlines up to that level."
  6105. (interactive "P")
  6106. (let ((org-cycle-include-plain-lists
  6107. (if (derived-mode-p 'org-mode) org-cycle-include-plain-lists nil)))
  6108. (cond
  6109. ((integerp arg)
  6110. (show-all)
  6111. (hide-sublevels arg)
  6112. (setq org-cycle-global-status 'contents))
  6113. ((equal arg '(4))
  6114. (org-set-startup-visibility)
  6115. (org-unlogged-message "Startup visibility, plus VISIBILITY properties."))
  6116. (t
  6117. (org-cycle '(4))))))
  6118. (defun org-set-startup-visibility ()
  6119. "Set the visibility required by startup options and properties."
  6120. (cond
  6121. ((eq org-startup-folded t)
  6122. (org-overview))
  6123. ((eq org-startup-folded 'content)
  6124. (org-content))
  6125. ((or (eq org-startup-folded 'showeverything)
  6126. (eq org-startup-folded nil))
  6127. (show-all)))
  6128. (unless (eq org-startup-folded 'showeverything)
  6129. (if org-hide-block-startup (org-hide-block-all))
  6130. (org-set-visibility-according-to-property 'no-cleanup)
  6131. (org-cycle-hide-archived-subtrees 'all)
  6132. (org-cycle-hide-drawers 'all)
  6133. (org-cycle-show-empty-lines t)))
  6134. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  6135. "Switch subtree visibilities according to :VISIBILITY: property."
  6136. (interactive)
  6137. (let (org-show-entry-below state)
  6138. (save-excursion
  6139. (goto-char (point-min))
  6140. (while (re-search-forward
  6141. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  6142. nil t)
  6143. (setq state (match-string 1))
  6144. (save-excursion
  6145. (org-back-to-heading t)
  6146. (hide-subtree)
  6147. (org-reveal)
  6148. (cond
  6149. ((equal state '("fold" "folded"))
  6150. (hide-subtree))
  6151. ((equal state "children")
  6152. (org-show-hidden-entry)
  6153. (show-children))
  6154. ((equal state "content")
  6155. (save-excursion
  6156. (save-restriction
  6157. (org-narrow-to-subtree)
  6158. (org-content))))
  6159. ((member state '("all" "showall"))
  6160. (show-subtree)))))
  6161. (unless no-cleanup
  6162. (org-cycle-hide-archived-subtrees 'all)
  6163. (org-cycle-hide-drawers 'all)
  6164. (org-cycle-show-empty-lines 'all)))))
  6165. ;; This function uses outline-regexp instead of the more fundamental
  6166. ;; org-outline-regexp so that org-cycle-global works outside of Org
  6167. ;; buffers, where outline-regexp is needed.
  6168. (defun org-overview ()
  6169. "Switch to overview mode, showing only top-level headlines.
  6170. This shows all headlines with a level equal or greater than the level
  6171. of the first headline in the buffer. This is important, because if the
  6172. first headline is not level one, then (hide-sublevels 1) gives confusing
  6173. results."
  6174. (interactive)
  6175. (save-excursion
  6176. (let ((level
  6177. (save-excursion
  6178. (goto-char (point-min))
  6179. (if (re-search-forward (concat "^" outline-regexp) nil t)
  6180. (progn
  6181. (goto-char (match-beginning 0))
  6182. (funcall outline-level))))))
  6183. (and level (hide-sublevels level)))))
  6184. (defun org-content (&optional arg)
  6185. "Show all headlines in the buffer, like a table of contents.
  6186. With numerical argument N, show content up to level N."
  6187. (interactive "P")
  6188. (org-overview)
  6189. (save-excursion
  6190. ;; Visit all headings and show their offspring
  6191. (and (integerp arg) (org-overview))
  6192. (goto-char (point-max))
  6193. (catch 'exit
  6194. (while (and (progn (condition-case nil
  6195. (outline-previous-visible-heading 1)
  6196. (error (goto-char (point-min))))
  6197. t)
  6198. (looking-at org-outline-regexp))
  6199. (if (integerp arg)
  6200. (show-children (1- arg))
  6201. (show-branches))
  6202. (if (bobp) (throw 'exit nil))))))
  6203. (defun org-optimize-window-after-visibility-change (state)
  6204. "Adjust the window after a change in outline visibility.
  6205. This function is the default value of the hook `org-cycle-hook'."
  6206. (when (get-buffer-window (current-buffer))
  6207. (cond
  6208. ((eq state 'content) nil)
  6209. ((eq state 'all) nil)
  6210. ((eq state 'folded) nil)
  6211. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  6212. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  6213. (defun org-remove-empty-overlays-at (pos)
  6214. "Remove outline overlays that do not contain non-white stuff."
  6215. (mapc
  6216. (lambda (o)
  6217. (and (eq 'outline (overlay-get o 'invisible))
  6218. (not (string-match "\\S-" (buffer-substring (overlay-start o)
  6219. (overlay-end o))))
  6220. (delete-overlay o)))
  6221. (overlays-at pos)))
  6222. (defun org-clean-visibility-after-subtree-move ()
  6223. "Fix visibility issues after moving a subtree."
  6224. ;; First, find a reasonable region to look at:
  6225. ;; Start two siblings above, end three below
  6226. (let* ((beg (save-excursion
  6227. (and (org-get-last-sibling)
  6228. (org-get-last-sibling))
  6229. (point)))
  6230. (end (save-excursion
  6231. (and (org-get-next-sibling)
  6232. (org-get-next-sibling)
  6233. (org-get-next-sibling))
  6234. (if (org-at-heading-p)
  6235. (point-at-eol)
  6236. (point))))
  6237. (level (looking-at "\\*+"))
  6238. (re (if level (concat "^" (regexp-quote (match-string 0)) " "))))
  6239. (save-excursion
  6240. (save-restriction
  6241. (narrow-to-region beg end)
  6242. (when re
  6243. ;; Properly fold already folded siblings
  6244. (goto-char (point-min))
  6245. (while (re-search-forward re nil t)
  6246. (if (and (not (outline-invisible-p))
  6247. (save-excursion
  6248. (goto-char (point-at-eol)) (outline-invisible-p)))
  6249. (hide-entry))))
  6250. (org-cycle-show-empty-lines 'overview)
  6251. (org-cycle-hide-drawers 'overview)))))
  6252. (defun org-cycle-show-empty-lines (state)
  6253. "Show empty lines above all visible headlines.
  6254. The region to be covered depends on STATE when called through
  6255. `org-cycle-hook'. Lisp program can use t for STATE to get the
  6256. entire buffer covered. Note that an empty line is only shown if there
  6257. are at least `org-cycle-separator-lines' empty lines before the headline."
  6258. (when (not (= org-cycle-separator-lines 0))
  6259. (save-excursion
  6260. (let* ((n (abs org-cycle-separator-lines))
  6261. (re (cond
  6262. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  6263. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  6264. (t (let ((ns (number-to-string (- n 2))))
  6265. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  6266. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  6267. beg end b e)
  6268. (cond
  6269. ((memq state '(overview contents t))
  6270. (setq beg (point-min) end (point-max)))
  6271. ((memq state '(children folded))
  6272. (setq beg (point) end (progn (org-end-of-subtree t t)
  6273. (beginning-of-line 2)
  6274. (point)))))
  6275. (when beg
  6276. (goto-char beg)
  6277. (while (re-search-forward re end t)
  6278. (unless (get-char-property (match-end 1) 'invisible)
  6279. (setq e (match-end 1))
  6280. (if (< org-cycle-separator-lines 0)
  6281. (setq b (save-excursion
  6282. (goto-char (match-beginning 0))
  6283. (org-back-over-empty-lines)
  6284. (if (save-excursion
  6285. (goto-char (max (point-min) (1- (point))))
  6286. (org-at-heading-p))
  6287. (1- (point))
  6288. (point))))
  6289. (setq b (match-beginning 1)))
  6290. (outline-flag-region b e nil)))))))
  6291. ;; Never hide empty lines at the end of the file.
  6292. (save-excursion
  6293. (goto-char (point-max))
  6294. (outline-previous-heading)
  6295. (outline-end-of-heading)
  6296. (if (and (looking-at "[ \t\n]+")
  6297. (= (match-end 0) (point-max)))
  6298. (outline-flag-region (point) (match-end 0) nil))))
  6299. (defun org-show-empty-lines-in-parent ()
  6300. "Move to the parent and re-show empty lines before visible headlines."
  6301. (save-excursion
  6302. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  6303. (org-cycle-show-empty-lines context))))
  6304. (defun org-files-list ()
  6305. "Return `org-agenda-files' list, plus all open org-mode files.
  6306. This is useful for operations that need to scan all of a user's
  6307. open and agenda-wise Org files."
  6308. (let ((files (mapcar 'expand-file-name (org-agenda-files))))
  6309. (dolist (buf (buffer-list))
  6310. (with-current-buffer buf
  6311. (if (and (derived-mode-p 'org-mode) (buffer-file-name))
  6312. (let ((file (expand-file-name (buffer-file-name))))
  6313. (unless (member file files)
  6314. (push file files))))))
  6315. files))
  6316. (defsubst org-entry-beginning-position ()
  6317. "Return the beginning position of the current entry."
  6318. (save-excursion (outline-back-to-heading t) (point)))
  6319. (defsubst org-entry-end-position ()
  6320. "Return the end position of the current entry."
  6321. (save-excursion (outline-next-heading) (point)))
  6322. (defun org-cycle-hide-drawers (state)
  6323. "Re-hide all drawers after a visibility state change."
  6324. (when (and (derived-mode-p 'org-mode)
  6325. (not (memq state '(overview folded contents))))
  6326. (save-excursion
  6327. (let* ((globalp (memq state '(contents all)))
  6328. (beg (if globalp (point-min) (point)))
  6329. (end (if globalp (point-max)
  6330. (if (eq state 'children)
  6331. (save-excursion (outline-next-heading) (point))
  6332. (org-end-of-subtree t)))))
  6333. (goto-char beg)
  6334. (while (re-search-forward org-drawer-regexp end t)
  6335. (org-flag-drawer t))))))
  6336. (defun org-cycle-hide-inline-tasks (state)
  6337. "Re-hide inline tasks when switching to 'contents or 'children
  6338. visibility state."
  6339. (case state
  6340. (contents
  6341. (when (org-bound-and-true-p org-inlinetask-min-level)
  6342. (hide-sublevels (1- org-inlinetask-min-level))))
  6343. (children
  6344. (when (featurep 'org-inlinetask)
  6345. (save-excursion
  6346. (while (and (outline-next-heading)
  6347. (org-inlinetask-at-task-p))
  6348. (org-inlinetask-toggle-visibility)
  6349. (org-inlinetask-goto-end)))))))
  6350. (defun org-flag-drawer (flag)
  6351. "When FLAG is non-nil, hide the drawer we are within.
  6352. Otherwise make it visible."
  6353. (save-excursion
  6354. (beginning-of-line 1)
  6355. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  6356. (let ((b (match-end 0)))
  6357. (if (re-search-forward
  6358. "^[ \t]*:END:"
  6359. (save-excursion (outline-next-heading) (point)) t)
  6360. (outline-flag-region b (point-at-eol) flag)
  6361. (user-error ":END: line missing at position %s" b))))))
  6362. (defun org-subtree-end-visible-p ()
  6363. "Is the end of the current subtree visible?"
  6364. (pos-visible-in-window-p
  6365. (save-excursion (org-end-of-subtree t) (point))))
  6366. (defun org-first-headline-recenter ()
  6367. "Move cursor to the first headline and recenter the headline."
  6368. (goto-char (point-min))
  6369. (when (re-search-forward (concat "^\\(" org-outline-regexp "\\)") nil t)
  6370. (set-window-start (selected-window) (point-at-bol))))
  6371. ;;; Saving and restoring visibility
  6372. (defun org-outline-overlay-data (&optional use-markers)
  6373. "Return a list of the locations of all outline overlays.
  6374. These are overlays with the `invisible' property value `outline'.
  6375. The return value is a list of cons cells, with start and stop
  6376. positions for each overlay.
  6377. If USE-MARKERS is set, return the positions as markers."
  6378. (let (beg end)
  6379. (save-excursion
  6380. (save-restriction
  6381. (widen)
  6382. (delq nil
  6383. (mapcar (lambda (o)
  6384. (when (eq (overlay-get o 'invisible) 'outline)
  6385. (setq beg (overlay-start o)
  6386. end (overlay-end o))
  6387. (and beg end (> end beg)
  6388. (if use-markers
  6389. (cons (move-marker (make-marker) beg)
  6390. (move-marker (make-marker) end))
  6391. (cons beg end)))))
  6392. (overlays-in (point-min) (point-max))))))))
  6393. (defun org-set-outline-overlay-data (data)
  6394. "Create visibility overlays for all positions in DATA.
  6395. DATA should have been made by `org-outline-overlay-data'."
  6396. (let (o)
  6397. (save-excursion
  6398. (save-restriction
  6399. (widen)
  6400. (show-all)
  6401. (mapc (lambda (c)
  6402. (outline-flag-region (car c) (cdr c) t))
  6403. data)))))
  6404. ;;; Folding of blocks
  6405. (defvar org-hide-block-overlays nil
  6406. "Overlays hiding blocks.")
  6407. (make-variable-buffer-local 'org-hide-block-overlays)
  6408. (defun org-block-map (function &optional start end)
  6409. "Call FUNCTION at the head of all source blocks in the current buffer.
  6410. Optional arguments START and END can be used to limit the range."
  6411. (let ((start (or start (point-min)))
  6412. (end (or end (point-max))))
  6413. (save-excursion
  6414. (goto-char start)
  6415. (while (and (< (point) end) (re-search-forward org-block-regexp end t))
  6416. (save-excursion
  6417. (save-match-data
  6418. (goto-char (match-beginning 0))
  6419. (funcall function)))))))
  6420. (defun org-hide-block-toggle-all ()
  6421. "Toggle the visibility of all blocks in the current buffer."
  6422. (org-block-map #'org-hide-block-toggle))
  6423. (defun org-hide-block-all ()
  6424. "Fold all blocks in the current buffer."
  6425. (interactive)
  6426. (org-show-block-all)
  6427. (org-block-map #'org-hide-block-toggle-maybe))
  6428. (defun org-show-block-all ()
  6429. "Unfold all blocks in the current buffer."
  6430. (interactive)
  6431. (mapc 'delete-overlay org-hide-block-overlays)
  6432. (setq org-hide-block-overlays nil))
  6433. (defun org-hide-block-toggle-maybe ()
  6434. "Toggle visibility of block at point."
  6435. (interactive)
  6436. (let ((case-fold-search t))
  6437. (if (save-excursion
  6438. (beginning-of-line 1)
  6439. (looking-at org-block-regexp))
  6440. (progn (org-hide-block-toggle)
  6441. t) ;; to signal that we took action
  6442. nil))) ;; to signal that we did not
  6443. (defun org-hide-block-toggle (&optional force)
  6444. "Toggle the visibility of the current block."
  6445. (interactive)
  6446. (save-excursion
  6447. (beginning-of-line)
  6448. (if (re-search-forward org-block-regexp nil t)
  6449. (let ((start (- (match-beginning 4) 1)) ;; beginning of body
  6450. (end (match-end 0)) ;; end of entire body
  6451. ov)
  6452. (if (memq t (mapcar (lambda (overlay)
  6453. (eq (overlay-get overlay 'invisible)
  6454. 'org-hide-block))
  6455. (overlays-at start)))
  6456. (if (or (not force) (eq force 'off))
  6457. (mapc (lambda (ov)
  6458. (when (member ov org-hide-block-overlays)
  6459. (setq org-hide-block-overlays
  6460. (delq ov org-hide-block-overlays)))
  6461. (when (eq (overlay-get ov 'invisible)
  6462. 'org-hide-block)
  6463. (delete-overlay ov)))
  6464. (overlays-at start)))
  6465. (setq ov (make-overlay start end))
  6466. (overlay-put ov 'invisible 'org-hide-block)
  6467. ;; make the block accessible to isearch
  6468. (overlay-put
  6469. ov 'isearch-open-invisible
  6470. (lambda (ov)
  6471. (when (member ov org-hide-block-overlays)
  6472. (setq org-hide-block-overlays
  6473. (delq ov org-hide-block-overlays)))
  6474. (when (eq (overlay-get ov 'invisible)
  6475. 'org-hide-block)
  6476. (delete-overlay ov))))
  6477. (push ov org-hide-block-overlays)))
  6478. (user-error "Not looking at a source block"))))
  6479. ;; org-tab-after-check-for-cycling-hook
  6480. (add-hook 'org-tab-first-hook 'org-hide-block-toggle-maybe)
  6481. ;; Remove overlays when changing major mode
  6482. (add-hook 'org-mode-hook
  6483. (lambda () (org-add-hook 'change-major-mode-hook
  6484. 'org-show-block-all 'append 'local)))
  6485. ;;; Org-goto
  6486. (defvar org-goto-window-configuration nil)
  6487. (defvar org-goto-marker nil)
  6488. (defvar org-goto-map)
  6489. (defun org-goto-map ()
  6490. "Set the keymap `org-goto'."
  6491. (setq org-goto-map
  6492. (let ((map (make-sparse-keymap)))
  6493. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command
  6494. mouse-drag-region universal-argument org-occur))
  6495. cmd)
  6496. (while (setq cmd (pop cmds))
  6497. (substitute-key-definition cmd cmd map global-map)))
  6498. (suppress-keymap map)
  6499. (org-defkey map "\C-m" 'org-goto-ret)
  6500. (org-defkey map [(return)] 'org-goto-ret)
  6501. (org-defkey map [(left)] 'org-goto-left)
  6502. (org-defkey map [(right)] 'org-goto-right)
  6503. (org-defkey map [(control ?g)] 'org-goto-quit)
  6504. (org-defkey map "\C-i" 'org-cycle)
  6505. (org-defkey map [(tab)] 'org-cycle)
  6506. (org-defkey map [(down)] 'outline-next-visible-heading)
  6507. (org-defkey map [(up)] 'outline-previous-visible-heading)
  6508. (if org-goto-auto-isearch
  6509. (if (fboundp 'define-key-after)
  6510. (define-key-after map [t] 'org-goto-local-auto-isearch)
  6511. nil)
  6512. (org-defkey map "q" 'org-goto-quit)
  6513. (org-defkey map "n" 'outline-next-visible-heading)
  6514. (org-defkey map "p" 'outline-previous-visible-heading)
  6515. (org-defkey map "f" 'outline-forward-same-level)
  6516. (org-defkey map "b" 'outline-backward-same-level)
  6517. (org-defkey map "u" 'outline-up-heading))
  6518. (org-defkey map "/" 'org-occur)
  6519. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  6520. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  6521. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  6522. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  6523. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  6524. map)))
  6525. (defconst org-goto-help
  6526. "Browse buffer copy, to find location or copy text.%s
  6527. RET=jump to location C-g=quit and return to previous location
  6528. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  6529. (defvar org-goto-start-pos) ; dynamically scoped parameter
  6530. (defun org-goto (&optional alternative-interface)
  6531. "Look up a different location in the current file, keeping current visibility.
  6532. When you want look-up or go to a different location in a
  6533. document, the fastest way is often to fold the entire buffer and
  6534. then dive into the tree. This method has the disadvantage, that
  6535. the previous location will be folded, which may not be what you
  6536. want.
  6537. This command works around this by showing a copy of the current
  6538. buffer in an indirect buffer, in overview mode. You can dive
  6539. into the tree in that copy, use org-occur and incremental search
  6540. to find a location. When pressing RET or `Q', the command
  6541. returns to the original buffer in which the visibility is still
  6542. unchanged. After RET it will also jump to the location selected
  6543. in the indirect buffer and expose the headline hierarchy above.
  6544. With a prefix argument, use the alternative interface: e.g. if
  6545. `org-goto-interface' is 'outline use 'outline-path-completion."
  6546. (interactive "P")
  6547. (org-goto-map)
  6548. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  6549. (org-refile-use-outline-path t)
  6550. (org-refile-target-verify-function nil)
  6551. (interface
  6552. (if (not alternative-interface)
  6553. org-goto-interface
  6554. (if (eq org-goto-interface 'outline)
  6555. 'outline-path-completion
  6556. 'outline)))
  6557. (org-goto-start-pos (point))
  6558. (selected-point
  6559. (if (eq interface 'outline)
  6560. (car (org-get-location (current-buffer) org-goto-help))
  6561. (let ((pa (org-refile-get-location "Goto" nil nil t)))
  6562. (org-refile-check-position pa)
  6563. (nth 3 pa)))))
  6564. (if selected-point
  6565. (progn
  6566. (org-mark-ring-push org-goto-start-pos)
  6567. (goto-char selected-point)
  6568. (if (or (outline-invisible-p) (org-invisible-p2))
  6569. (org-show-context 'org-goto)))
  6570. (message "Quit"))))
  6571. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  6572. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  6573. (defvar org-goto-local-auto-isearch-map) ; defined below
  6574. (defun org-get-location (buf help)
  6575. "Let the user select a location in the Org-mode buffer BUF.
  6576. This function uses a recursive edit. It returns the selected position
  6577. or nil."
  6578. (org-no-popups
  6579. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  6580. (isearch-hide-immediately nil)
  6581. (isearch-search-fun-function
  6582. (lambda () 'org-goto-local-search-headings))
  6583. (org-goto-selected-point org-goto-exit-command))
  6584. (save-excursion
  6585. (save-window-excursion
  6586. (delete-other-windows)
  6587. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  6588. (org-pop-to-buffer-same-window
  6589. (condition-case nil
  6590. (make-indirect-buffer (current-buffer) "*org-goto*")
  6591. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  6592. (with-output-to-temp-buffer "*Org Help*"
  6593. (princ (format help (if org-goto-auto-isearch
  6594. " Just type for auto-isearch."
  6595. " n/p/f/b/u to navigate, q to quit."))))
  6596. (org-fit-window-to-buffer (get-buffer-window "*Org Help*"))
  6597. (setq buffer-read-only nil)
  6598. (let ((org-startup-truncated t)
  6599. (org-startup-folded nil)
  6600. (org-startup-align-all-tables nil))
  6601. (org-mode)
  6602. (org-overview))
  6603. (setq buffer-read-only t)
  6604. (if (and (boundp 'org-goto-start-pos)
  6605. (integer-or-marker-p org-goto-start-pos))
  6606. (let ((org-show-hierarchy-above t)
  6607. (org-show-siblings t)
  6608. (org-show-following-heading t))
  6609. (goto-char org-goto-start-pos)
  6610. (and (outline-invisible-p) (org-show-context)))
  6611. (goto-char (point-min)))
  6612. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  6613. (message "Select location and press RET")
  6614. (use-local-map org-goto-map)
  6615. (recursive-edit)))
  6616. (kill-buffer "*org-goto*")
  6617. (cons org-goto-selected-point org-goto-exit-command))))
  6618. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  6619. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  6620. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  6621. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  6622. (defun org-goto-local-search-headings (string bound noerror)
  6623. "Search and make sure that any matches are in headlines."
  6624. (catch 'return
  6625. (while (if isearch-forward
  6626. (search-forward string bound noerror)
  6627. (search-backward string bound noerror))
  6628. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  6629. (and (member :headline context)
  6630. (not (member :tags context))))
  6631. (throw 'return (point))))))
  6632. (defun org-goto-local-auto-isearch ()
  6633. "Start isearch."
  6634. (interactive)
  6635. (goto-char (point-min))
  6636. (let ((keys (this-command-keys)))
  6637. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  6638. (isearch-mode t)
  6639. (isearch-process-search-char (string-to-char keys)))))
  6640. (defun org-goto-ret (&optional arg)
  6641. "Finish `org-goto' by going to the new location."
  6642. (interactive "P")
  6643. (setq org-goto-selected-point (point)
  6644. org-goto-exit-command 'return)
  6645. (throw 'exit nil))
  6646. (defun org-goto-left ()
  6647. "Finish `org-goto' by going to the new location."
  6648. (interactive)
  6649. (if (org-at-heading-p)
  6650. (progn
  6651. (beginning-of-line 1)
  6652. (setq org-goto-selected-point (point)
  6653. org-goto-exit-command 'left)
  6654. (throw 'exit nil))
  6655. (user-error "Not on a heading")))
  6656. (defun org-goto-right ()
  6657. "Finish `org-goto' by going to the new location."
  6658. (interactive)
  6659. (if (org-at-heading-p)
  6660. (progn
  6661. (setq org-goto-selected-point (point)
  6662. org-goto-exit-command 'right)
  6663. (throw 'exit nil))
  6664. (user-error "Not on a heading")))
  6665. (defun org-goto-quit ()
  6666. "Finish `org-goto' without cursor motion."
  6667. (interactive)
  6668. (setq org-goto-selected-point nil)
  6669. (setq org-goto-exit-command 'quit)
  6670. (throw 'exit nil))
  6671. ;;; Indirect buffer display of subtrees
  6672. (defvar org-indirect-dedicated-frame nil
  6673. "This is the frame being used for indirect tree display.")
  6674. (defvar org-last-indirect-buffer nil)
  6675. (defun org-tree-to-indirect-buffer (&optional arg)
  6676. "Create indirect buffer and narrow it to current subtree.
  6677. With a numerical prefix ARG, go up to this level and then take that tree.
  6678. If ARG is negative, go up that many levels.
  6679. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  6680. indirect buffer previously made with this command, to avoid proliferation of
  6681. indirect buffers. However, when you call the command with a \
  6682. \\[universal-argument] prefix, or
  6683. when `org-indirect-buffer-display' is `new-frame', the last buffer
  6684. is kept so that you can work with several indirect buffers at the same time.
  6685. If `org-indirect-buffer-display' is `dedicated-frame', the \
  6686. \\[universal-argument] prefix also
  6687. requests that a new frame be made for the new buffer, so that the dedicated
  6688. frame is not changed."
  6689. (interactive "P")
  6690. (let ((cbuf (current-buffer))
  6691. (cwin (selected-window))
  6692. (pos (point))
  6693. beg end level heading ibuf)
  6694. (save-excursion
  6695. (org-back-to-heading t)
  6696. (when (numberp arg)
  6697. (setq level (org-outline-level))
  6698. (if (< arg 0) (setq arg (+ level arg)))
  6699. (while (> (setq level (org-outline-level)) arg)
  6700. (org-up-heading-safe)))
  6701. (setq beg (point)
  6702. heading (org-get-heading))
  6703. (org-end-of-subtree t t)
  6704. (if (org-at-heading-p) (backward-char 1))
  6705. (setq end (point)))
  6706. (if (and (buffer-live-p org-last-indirect-buffer)
  6707. (not (eq org-indirect-buffer-display 'new-frame))
  6708. (not arg))
  6709. (kill-buffer org-last-indirect-buffer))
  6710. (setq ibuf (org-get-indirect-buffer cbuf)
  6711. org-last-indirect-buffer ibuf)
  6712. (cond
  6713. ((or (eq org-indirect-buffer-display 'new-frame)
  6714. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  6715. (select-frame (make-frame))
  6716. (delete-other-windows)
  6717. (org-pop-to-buffer-same-window ibuf)
  6718. (org-set-frame-title heading))
  6719. ((eq org-indirect-buffer-display 'dedicated-frame)
  6720. (raise-frame
  6721. (select-frame (or (and org-indirect-dedicated-frame
  6722. (frame-live-p org-indirect-dedicated-frame)
  6723. org-indirect-dedicated-frame)
  6724. (setq org-indirect-dedicated-frame (make-frame)))))
  6725. (delete-other-windows)
  6726. (org-pop-to-buffer-same-window ibuf)
  6727. (org-set-frame-title (concat "Indirect: " heading)))
  6728. ((eq org-indirect-buffer-display 'current-window)
  6729. (org-pop-to-buffer-same-window ibuf))
  6730. ((eq org-indirect-buffer-display 'other-window)
  6731. (pop-to-buffer ibuf))
  6732. (t (error "Invalid value")))
  6733. (if (featurep 'xemacs)
  6734. (save-excursion (org-mode) (turn-on-font-lock)))
  6735. (narrow-to-region beg end)
  6736. (show-all)
  6737. (goto-char pos)
  6738. (run-hook-with-args 'org-cycle-hook 'all)
  6739. (and (window-live-p cwin) (select-window cwin))))
  6740. (defun org-get-indirect-buffer (&optional buffer)
  6741. (setq buffer (or buffer (current-buffer)))
  6742. (let ((n 1) (base (buffer-name buffer)) bname)
  6743. (while (buffer-live-p
  6744. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  6745. (setq n (1+ n)))
  6746. (condition-case nil
  6747. (make-indirect-buffer buffer bname 'clone)
  6748. (error (make-indirect-buffer buffer bname)))))
  6749. (defun org-set-frame-title (title)
  6750. "Set the title of the current frame to the string TITLE."
  6751. ;; FIXME: how to name a single frame in XEmacs???
  6752. (unless (featurep 'xemacs)
  6753. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  6754. ;;;; Structure editing
  6755. ;;; Inserting headlines
  6756. (defun org-previous-line-empty-p (&optional next)
  6757. "Is the previous line a blank line?
  6758. When NEXT is non-nil, check the next line instead."
  6759. (save-excursion
  6760. (and (not (bobp))
  6761. (or (beginning-of-line (if next 2 0)) t)
  6762. (save-match-data
  6763. (looking-at "[ \t]*$")))))
  6764. (defun org-insert-heading (&optional arg invisible-ok)
  6765. "Insert a new heading or an item with the same depth at point.
  6766. If point is at the beginning of a heading or a list item, insert
  6767. a new heading or a new item above the current one. If point is
  6768. at the beginning of a normal line, turn the line into a heading.
  6769. If point is in the middle of a headline or a list item, split the
  6770. headline or the item and create a new headline/item with the text
  6771. in the current line after point \(see `org-M-RET-may-split-line'
  6772. on how to modify this behavior).
  6773. With one universal prefirx argument, set the user option
  6774. `org-insert-heading-respect-content' to t for the duration of
  6775. the command. This modifies the behavior described above in this
  6776. ways: on list items and at the beginning of normal lines, force
  6777. the insertion of a heading after the current subtree.
  6778. With two universal prefix arguments, insert the heading at the
  6779. end of the grandparent subtree. For example, if point is within
  6780. a 2nd-level heading, then it will insert a 2nd-level heading at
  6781. the end of the 1st-level parent heading.
  6782. When INVISIBLE-OK is set, stop at invisible headlines when going
  6783. back. This is important for non-interactive uses of the
  6784. command."
  6785. (interactive "P")
  6786. (if (org-called-interactively-p 'any) (org-reveal))
  6787. (let ((itemp (org-in-item-p))
  6788. (may-split (org-get-alist-option org-M-RET-may-split-line 'headline))
  6789. (respect-content (or org-insert-heading-respect-content
  6790. (equal arg '(4))))
  6791. (initial-content "")
  6792. (adjust-empty-lines t))
  6793. (cond
  6794. ((or (= (buffer-size) 0)
  6795. (and (not (save-excursion
  6796. (and (ignore-errors (org-back-to-heading invisible-ok))
  6797. (org-at-heading-p))))
  6798. (or arg (not itemp))))
  6799. ;; At beginning of buffer or so high up that only a heading
  6800. ;; makes sense.
  6801. (cond ((and (bolp) (not respect-content)) (insert "* "))
  6802. ((not respect-content)
  6803. (unless may-split (end-of-line))
  6804. (insert "\n* "))
  6805. ((re-search-forward org-outline-regexp-bol nil t)
  6806. (beginning-of-line)
  6807. (insert "* \n")
  6808. (backward-char))
  6809. (t (goto-char (point-max))
  6810. (insert "\n* ")))
  6811. (run-hooks 'org-insert-heading-hook))
  6812. ((and itemp (not (member arg '((4) (16)))))
  6813. ;; Insert an item
  6814. (org-insert-item))
  6815. (t
  6816. ;; Maybe move at the end of the subtree
  6817. (when (equal arg '(16))
  6818. (org-up-heading-safe)
  6819. (org-end-of-subtree t))
  6820. ;; Insert a heading
  6821. (save-restriction
  6822. (widen)
  6823. (let* ((level nil)
  6824. (on-heading (org-at-heading-p))
  6825. (empty-line-p (if on-heading
  6826. (org-previous-line-empty-p)
  6827. ;; We will decide later
  6828. nil))
  6829. ;; Get a level string to fall back on
  6830. (fix-level
  6831. (if (org-before-first-heading-p) "*"
  6832. (save-excursion
  6833. (org-back-to-heading t)
  6834. (if (org-previous-line-empty-p) (setq empty-line-p t))
  6835. (looking-at org-outline-regexp)
  6836. (make-string (1- (length (match-string 0))) ?*))))
  6837. (stars
  6838. (save-excursion
  6839. (condition-case nil
  6840. (progn
  6841. (org-back-to-heading invisible-ok)
  6842. (when (and (not on-heading)
  6843. (featurep 'org-inlinetask)
  6844. (integerp org-inlinetask-min-level)
  6845. (>= (length (match-string 0))
  6846. org-inlinetask-min-level))
  6847. ;; Find a heading level before the inline task
  6848. (while (and (setq level (org-up-heading-safe))
  6849. (>= level org-inlinetask-min-level)))
  6850. (if (org-at-heading-p)
  6851. (org-back-to-heading invisible-ok)
  6852. (error "This should not happen")))
  6853. (unless (and (save-excursion
  6854. (save-match-data
  6855. (org-backward-heading-same-level
  6856. 1 invisible-ok))
  6857. (= (point) (match-beginning 0)))
  6858. (not (org-previous-line-empty-p t)))
  6859. (setq empty-line-p (or empty-line-p
  6860. (org-previous-line-empty-p))))
  6861. (match-string 0))
  6862. (error (or fix-level "* ")))))
  6863. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  6864. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  6865. pos hide-previous previous-pos)
  6866. ;; If we insert after content, move there and clean up whitespace
  6867. (when (and respect-content
  6868. (not (org-looking-at-p org-outline-regexp-bol)))
  6869. (if (not (org-before-first-heading-p))
  6870. (org-end-of-subtree nil t)
  6871. (re-search-forward org-outline-regexp-bol)
  6872. (beginning-of-line 0))
  6873. (skip-chars-backward " \r\n")
  6874. (and (not (looking-back "^\*+"))
  6875. (looking-at "[ \t]+") (replace-match ""))
  6876. (unless (eobp) (forward-char 1))
  6877. (when (looking-at "^\\*")
  6878. (unless (bobp) (backward-char 1))
  6879. (insert "\n")))
  6880. ;; If we are splitting, grab the text that should be moved to the new headline
  6881. (when may-split
  6882. (if (org-on-heading-p)
  6883. ;; This is a heading, we split intelligently (keeping tags)
  6884. (let ((pos (point)))
  6885. (goto-char (point-at-bol))
  6886. (unless (looking-at org-complex-heading-regexp)
  6887. (error "This should not happen"))
  6888. (when (and (match-beginning 4)
  6889. (> pos (match-beginning 4))
  6890. (< pos (match-end 4)))
  6891. (setq initial-content (buffer-substring pos (match-end 4)))
  6892. (goto-char pos)
  6893. (delete-region (point) (match-end 4))
  6894. (if (looking-at "[ \t]*$")
  6895. (replace-match "")
  6896. (insert (make-string (length initial-content) ?\ )))
  6897. (setq initial-content (org-trim initial-content)))
  6898. (goto-char pos))
  6899. ;; a normal line
  6900. (setq initial-content
  6901. (org-trim (buffer-substring (point) (point-at-eol))))
  6902. (delete-region (point) (point-at-eol))))
  6903. ;; If we are at the beginning of the line, insert before it. Else after
  6904. (cond
  6905. ((and (bolp) (looking-at "[ \t]*$")))
  6906. ((and (bolp) (not (looking-at "[ \t]*$")))
  6907. (open-line 1))
  6908. (t
  6909. (goto-char (point-at-eol))
  6910. (insert "\n")))
  6911. ;; Insert the new heading
  6912. (insert stars)
  6913. (just-one-space)
  6914. (insert initial-content)
  6915. (when adjust-empty-lines
  6916. (if (or (not blank)
  6917. (and blank (not (org-previous-line-empty-p))))
  6918. (org-N-empty-lines-before-current (if blank 1 0))))
  6919. (run-hooks 'org-insert-heading-hook)))))))
  6920. (defun org-N-empty-lines-before-current (N)
  6921. "Make the number of empty lines before current exactly N.
  6922. So this will delete or add empty lines."
  6923. (save-excursion
  6924. (goto-char (point-at-bol))
  6925. (if (looking-back "\\s-+" nil 'greedy)
  6926. (replace-match ""))
  6927. (or (bobp) (insert "\n"))
  6928. (while (> N 0)
  6929. (insert "\n")
  6930. (setq N (1- N)))))
  6931. (defun org-get-heading (&optional no-tags no-todo)
  6932. "Return the heading of the current entry, without the stars.
  6933. When NO-TAGS is non-nil, don't include tags.
  6934. When NO-TODO is non-nil, don't include TODO keywords."
  6935. (save-excursion
  6936. (org-back-to-heading t)
  6937. (cond
  6938. ((and no-tags no-todo)
  6939. (looking-at org-complex-heading-regexp)
  6940. (match-string 4))
  6941. (no-tags
  6942. (looking-at (concat org-outline-regexp
  6943. "\\(.*?\\)"
  6944. "\\(?:[ \t]+:[[:alnum:]:_@#%]+:\\)?[ \t]*$"))
  6945. (match-string 1))
  6946. (no-todo
  6947. (looking-at org-todo-line-regexp)
  6948. (match-string 3))
  6949. (t (looking-at org-heading-regexp)
  6950. (match-string 2)))))
  6951. (defvar orgstruct-mode) ; defined below
  6952. (defun org-heading-components ()
  6953. "Return the components of the current heading.
  6954. This is a list with the following elements:
  6955. - the level as an integer
  6956. - the reduced level, different if `org-odd-levels-only' is set.
  6957. - the TODO keyword, or nil
  6958. - the priority character, like ?A, or nil if no priority is given
  6959. - the headline text itself, or the tags string if no headline text
  6960. - the tags string, or nil."
  6961. (save-excursion
  6962. (org-back-to-heading t)
  6963. (if (let (case-fold-search)
  6964. (looking-at
  6965. (if orgstruct-mode
  6966. org-heading-regexp
  6967. org-complex-heading-regexp)))
  6968. (if orgstruct-mode
  6969. (list (length (match-string 1))
  6970. (org-reduced-level (length (match-string 1)))
  6971. nil
  6972. nil
  6973. (match-string 2)
  6974. nil)
  6975. (list (length (match-string 1))
  6976. (org-reduced-level (length (match-string 1)))
  6977. (org-match-string-no-properties 2)
  6978. (and (match-end 3) (aref (match-string 3) 2))
  6979. (org-match-string-no-properties 4)
  6980. (org-match-string-no-properties 5))))))
  6981. (defun org-get-entry ()
  6982. "Get the entry text, after heading, entire subtree."
  6983. (save-excursion
  6984. (org-back-to-heading t)
  6985. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  6986. (defun org-insert-heading-after-current ()
  6987. "Insert a new heading with same level as current, after current subtree."
  6988. (interactive)
  6989. (org-back-to-heading)
  6990. (org-insert-heading)
  6991. (org-move-subtree-down)
  6992. (end-of-line 1))
  6993. (defun org-insert-heading-respect-content (&optional invisible-ok)
  6994. "Insert heading with `org-insert-heading-respect-content' set to t."
  6995. (interactive)
  6996. (org-insert-heading '(4) invisible-ok))
  6997. (defun org-insert-todo-heading-respect-content (&optional force-state)
  6998. "Insert TODO heading with `org-insert-heading-respect-content' set to t."
  6999. (interactive)
  7000. (org-insert-todo-heading force-state '(4)))
  7001. (defun org-insert-todo-heading (arg &optional force-heading)
  7002. "Insert a new heading with the same level and TODO state as current heading.
  7003. If the heading has no TODO state, or if the state is DONE, use the first
  7004. state (TODO by default). Also with one prefix arg, force first state. With
  7005. two prefix args, force inserting at the end of the parent subtree."
  7006. (interactive "P")
  7007. (when (or force-heading (not (org-insert-item 'checkbox)))
  7008. (org-insert-heading (or (and (equal arg '(16)) '(16))
  7009. force-heading))
  7010. (save-excursion
  7011. (org-back-to-heading)
  7012. (outline-previous-heading)
  7013. (looking-at org-todo-line-regexp))
  7014. (let*
  7015. ((new-mark-x
  7016. (if (or (equal arg '(4))
  7017. (not (match-beginning 2))
  7018. (member (match-string 2) org-done-keywords))
  7019. (car org-todo-keywords-1)
  7020. (match-string 2)))
  7021. (new-mark
  7022. (or
  7023. (run-hook-with-args-until-success
  7024. 'org-todo-get-default-hook new-mark-x nil)
  7025. new-mark-x)))
  7026. (beginning-of-line 1)
  7027. (and (looking-at org-outline-regexp) (goto-char (match-end 0))
  7028. (if org-treat-insert-todo-heading-as-state-change
  7029. (org-todo new-mark)
  7030. (insert new-mark " "))))
  7031. (when org-provide-todo-statistics
  7032. (org-update-parent-todo-statistics))))
  7033. (defun org-insert-subheading (arg)
  7034. "Insert a new subheading and demote it.
  7035. Works for outline headings and for plain lists alike."
  7036. (interactive "P")
  7037. (org-insert-heading arg)
  7038. (cond
  7039. ((org-at-heading-p) (org-do-demote))
  7040. ((org-at-item-p) (org-indent-item))))
  7041. (defun org-insert-todo-subheading (arg)
  7042. "Insert a new subheading with TODO keyword or checkbox and demote it.
  7043. Works for outline headings and for plain lists alike."
  7044. (interactive "P")
  7045. (org-insert-todo-heading arg)
  7046. (cond
  7047. ((org-at-heading-p) (org-do-demote))
  7048. ((org-at-item-p) (org-indent-item))))
  7049. ;;; Promotion and Demotion
  7050. (defvar org-after-demote-entry-hook nil
  7051. "Hook run after an entry has been demoted.
  7052. The cursor will be at the beginning of the entry.
  7053. When a subtree is being demoted, the hook will be called for each node.")
  7054. (defvar org-after-promote-entry-hook nil
  7055. "Hook run after an entry has been promoted.
  7056. The cursor will be at the beginning of the entry.
  7057. When a subtree is being promoted, the hook will be called for each node.")
  7058. (defun org-promote-subtree ()
  7059. "Promote the entire subtree.
  7060. See also `org-promote'."
  7061. (interactive)
  7062. (save-excursion
  7063. (org-with-limited-levels (org-map-tree 'org-promote)))
  7064. (org-fix-position-after-promote))
  7065. (defun org-demote-subtree ()
  7066. "Demote the entire subtree. See `org-demote'.
  7067. See also `org-promote'."
  7068. (interactive)
  7069. (save-excursion
  7070. (org-with-limited-levels (org-map-tree 'org-demote)))
  7071. (org-fix-position-after-promote))
  7072. (defun org-do-promote ()
  7073. "Promote the current heading higher up the tree.
  7074. If the region is active in `transient-mark-mode', promote all headings
  7075. in the region."
  7076. (interactive)
  7077. (save-excursion
  7078. (if (org-region-active-p)
  7079. (org-map-region 'org-promote (region-beginning) (region-end))
  7080. (org-promote)))
  7081. (org-fix-position-after-promote))
  7082. (defun org-do-demote ()
  7083. "Demote the current heading lower down the tree.
  7084. If the region is active in `transient-mark-mode', demote all headings
  7085. in the region."
  7086. (interactive)
  7087. (save-excursion
  7088. (if (org-region-active-p)
  7089. (org-map-region 'org-demote (region-beginning) (region-end))
  7090. (org-demote)))
  7091. (org-fix-position-after-promote))
  7092. (defun org-fix-position-after-promote ()
  7093. "Make sure that after pro/demotion cursor position is right."
  7094. (let ((pos (point)))
  7095. (when (save-excursion
  7096. (beginning-of-line 1)
  7097. (looking-at org-todo-line-regexp)
  7098. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  7099. (cond ((eobp) (insert " "))
  7100. ((eolp) (insert " "))
  7101. ((equal (char-after) ?\ ) (forward-char 1))))))
  7102. (defun org-current-level ()
  7103. "Return the level of the current entry, or nil if before the first headline.
  7104. The level is the number of stars at the beginning of the headline."
  7105. (save-excursion
  7106. (org-with-limited-levels
  7107. (if (ignore-errors (org-back-to-heading t))
  7108. (funcall outline-level)))))
  7109. (defun org-get-previous-line-level ()
  7110. "Return the outline depth of the last headline before the current line.
  7111. Returns 0 for the first headline in the buffer, and nil if before the
  7112. first headline."
  7113. (and (org-current-level)
  7114. (or (and (/= (line-beginning-position) (point-min))
  7115. (save-excursion (beginning-of-line 0) (org-current-level)))
  7116. 0)))
  7117. (defun org-reduced-level (l)
  7118. "Compute the effective level of a heading.
  7119. This takes into account the setting of `org-odd-levels-only'."
  7120. (cond
  7121. ((zerop l) 0)
  7122. (org-odd-levels-only (1+ (floor (/ l 2))))
  7123. (t l)))
  7124. (defun org-level-increment ()
  7125. "Return the number of stars that will be added or removed at a
  7126. time to headlines when structure editing, based on the value of
  7127. `org-odd-levels-only'."
  7128. (if org-odd-levels-only 2 1))
  7129. (defun org-get-valid-level (level &optional change)
  7130. "Rectify a level change under the influence of `org-odd-levels-only'
  7131. LEVEL is a current level, CHANGE is by how much the level should be
  7132. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  7133. even level numbers will become the next higher odd number."
  7134. (if org-odd-levels-only
  7135. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  7136. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  7137. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  7138. (max 1 (+ level (or change 0)))))
  7139. (if (boundp 'define-obsolete-function-alias)
  7140. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  7141. (define-obsolete-function-alias 'org-get-legal-level
  7142. 'org-get-valid-level)
  7143. (define-obsolete-function-alias 'org-get-legal-level
  7144. 'org-get-valid-level "23.1")))
  7145. (defun org-promote ()
  7146. "Promote the current heading higher up the tree.
  7147. If the region is active in `transient-mark-mode', promote all headings
  7148. in the region."
  7149. (org-back-to-heading t)
  7150. (let* ((level (save-match-data (funcall outline-level)))
  7151. (after-change-functions (remove 'flyspell-after-change-function
  7152. after-change-functions))
  7153. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  7154. (diff (abs (- level (length up-head) -1))))
  7155. (cond ((and (= level 1) org-called-with-limited-levels
  7156. org-allow-promoting-top-level-subtree)
  7157. (replace-match "# " nil t))
  7158. ((= level 1)
  7159. (user-error "Cannot promote to level 0. UNDO to recover if necessary"))
  7160. (t (replace-match up-head nil t)))
  7161. ;; Fixup tag positioning
  7162. (unless (= level 1)
  7163. (and org-auto-align-tags (org-set-tags nil 'ignore-column))
  7164. (if org-adapt-indentation (org-fixup-indentation (- diff))))
  7165. (run-hooks 'org-after-promote-entry-hook)))
  7166. (defun org-demote ()
  7167. "Demote the current heading lower down the tree.
  7168. If the region is active in `transient-mark-mode', demote all headings
  7169. in the region."
  7170. (org-back-to-heading t)
  7171. (let* ((level (save-match-data (funcall outline-level)))
  7172. (after-change-functions (remove 'flyspell-after-change-function
  7173. after-change-functions))
  7174. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  7175. (diff (abs (- level (length down-head) -1))))
  7176. (replace-match down-head nil t)
  7177. ;; Fixup tag positioning
  7178. (and org-auto-align-tags (org-set-tags nil 'ignore-column))
  7179. (if org-adapt-indentation (org-fixup-indentation diff))
  7180. (run-hooks 'org-after-demote-entry-hook)))
  7181. (defun org-cycle-level ()
  7182. "Cycle the level of an empty headline through possible states.
  7183. This goes first to child, then to parent, level, then up the hierarchy.
  7184. After top level, it switches back to sibling level."
  7185. (interactive)
  7186. (let ((org-adapt-indentation nil))
  7187. (when (org-point-at-end-of-empty-headline)
  7188. (setq this-command 'org-cycle-level) ; Only needed for caching
  7189. (let ((cur-level (org-current-level))
  7190. (prev-level (org-get-previous-line-level)))
  7191. (cond
  7192. ;; If first headline in file, promote to top-level.
  7193. ((= prev-level 0)
  7194. (loop repeat (/ (- cur-level 1) (org-level-increment))
  7195. do (org-do-promote)))
  7196. ;; If same level as prev, demote one.
  7197. ((= prev-level cur-level)
  7198. (org-do-demote))
  7199. ;; If parent is top-level, promote to top level if not already.
  7200. ((= prev-level 1)
  7201. (loop repeat (/ (- cur-level 1) (org-level-increment))
  7202. do (org-do-promote)))
  7203. ;; If top-level, return to prev-level.
  7204. ((= cur-level 1)
  7205. (loop repeat (/ (- prev-level 1) (org-level-increment))
  7206. do (org-do-demote)))
  7207. ;; If less than prev-level, promote one.
  7208. ((< cur-level prev-level)
  7209. (org-do-promote))
  7210. ;; If deeper than prev-level, promote until higher than
  7211. ;; prev-level.
  7212. ((> cur-level prev-level)
  7213. (loop repeat (+ 1 (/ (- cur-level prev-level) (org-level-increment)))
  7214. do (org-do-promote))))
  7215. t))))
  7216. (defun org-map-tree (fun)
  7217. "Call FUN for every heading underneath the current one."
  7218. (org-back-to-heading)
  7219. (let ((level (funcall outline-level)))
  7220. (save-excursion
  7221. (funcall fun)
  7222. (while (and (progn
  7223. (outline-next-heading)
  7224. (> (funcall outline-level) level))
  7225. (not (eobp)))
  7226. (funcall fun)))))
  7227. (defun org-map-region (fun beg end)
  7228. "Call FUN for every heading between BEG and END."
  7229. (let ((org-ignore-region t))
  7230. (save-excursion
  7231. (setq end (copy-marker end))
  7232. (goto-char beg)
  7233. (if (and (re-search-forward org-outline-regexp-bol nil t)
  7234. (< (point) end))
  7235. (funcall fun))
  7236. (while (and (progn
  7237. (outline-next-heading)
  7238. (< (point) end))
  7239. (not (eobp)))
  7240. (funcall fun)))))
  7241. (defvar org-property-end-re) ; silence byte-compiler
  7242. (defun org-fixup-indentation (diff)
  7243. "Change the indentation in the current entry by DIFF.
  7244. However, if any line in the current entry has no indentation, or if it
  7245. would end up with no indentation after the change, nothing at all is done."
  7246. (save-excursion
  7247. (let ((end (save-excursion (outline-next-heading)
  7248. (point-marker)))
  7249. (prohibit (if (> diff 0)
  7250. "^\\S-"
  7251. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  7252. col)
  7253. (unless (save-excursion (end-of-line 1)
  7254. (re-search-forward prohibit end t))
  7255. (while (and (< (point) end)
  7256. (re-search-forward "^[ \t]+" end t))
  7257. (goto-char (match-end 0))
  7258. (setq col (current-column))
  7259. (if (< diff 0) (replace-match ""))
  7260. (org-indent-to-column (+ diff col))))
  7261. (move-marker end nil))))
  7262. (defun org-convert-to-odd-levels ()
  7263. "Convert an org-mode file with all levels allowed to one with odd levels.
  7264. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  7265. level 5 etc."
  7266. (interactive)
  7267. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  7268. (let ((outline-level 'org-outline-level)
  7269. (org-odd-levels-only nil) n)
  7270. (save-excursion
  7271. (goto-char (point-min))
  7272. (while (re-search-forward "^\\*\\*+ " nil t)
  7273. (setq n (- (length (match-string 0)) 2))
  7274. (while (>= (setq n (1- n)) 0)
  7275. (org-demote))
  7276. (end-of-line 1))))))
  7277. (defun org-convert-to-oddeven-levels ()
  7278. "Convert an org-mode file with only odd levels to one with odd/even levels.
  7279. This promotes level 3 to level 2, level 5 to level 3 etc. If the
  7280. file contains a section with an even level, conversion would
  7281. destroy the structure of the file. An error is signaled in this
  7282. case."
  7283. (interactive)
  7284. (goto-char (point-min))
  7285. ;; First check if there are no even levels
  7286. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  7287. (org-show-context t)
  7288. (error "Not all levels are odd in this file. Conversion not possible"))
  7289. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  7290. (let ((outline-regexp org-outline-regexp)
  7291. (outline-level 'org-outline-level)
  7292. (org-odd-levels-only nil) n)
  7293. (save-excursion
  7294. (goto-char (point-min))
  7295. (while (re-search-forward "^\\*\\*+ " nil t)
  7296. (setq n (/ (1- (length (match-string 0))) 2))
  7297. (while (>= (setq n (1- n)) 0)
  7298. (org-promote))
  7299. (end-of-line 1))))))
  7300. (defun org-tr-level (n)
  7301. "Make N odd if required."
  7302. (if org-odd-levels-only (1+ (/ n 2)) n))
  7303. ;;; Vertical tree motion, cutting and pasting of subtrees
  7304. (defun org-move-subtree-up (&optional arg)
  7305. "Move the current subtree up past ARG headlines of the same level."
  7306. (interactive "p")
  7307. (org-move-subtree-down (- (prefix-numeric-value arg))))
  7308. (defun org-move-subtree-down (&optional arg)
  7309. "Move the current subtree down past ARG headlines of the same level."
  7310. (interactive "p")
  7311. (setq arg (prefix-numeric-value arg))
  7312. (let ((movfunc (if (> arg 0) 'org-get-next-sibling
  7313. 'org-get-last-sibling))
  7314. (ins-point (make-marker))
  7315. (cnt (abs arg))
  7316. (col (current-column))
  7317. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  7318. ;; Select the tree
  7319. (org-back-to-heading)
  7320. (setq beg0 (point))
  7321. (save-excursion
  7322. (setq ne-beg (org-back-over-empty-lines))
  7323. (setq beg (point)))
  7324. (save-match-data
  7325. (save-excursion (outline-end-of-heading)
  7326. (setq folded (outline-invisible-p)))
  7327. (progn (org-end-of-subtree nil t)
  7328. (unless (eobp) (backward-char))))
  7329. (outline-next-heading)
  7330. (setq ne-end (org-back-over-empty-lines))
  7331. (setq end (point))
  7332. (goto-char beg0)
  7333. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  7334. ;; include less whitespace
  7335. (save-excursion
  7336. (goto-char beg)
  7337. (forward-line (- ne-beg ne-end))
  7338. (setq beg (point))))
  7339. ;; Find insertion point, with error handling
  7340. (while (> cnt 0)
  7341. (or (and (funcall movfunc) (looking-at org-outline-regexp))
  7342. (progn (goto-char beg0)
  7343. (user-error "Cannot move past superior level or buffer limit")))
  7344. (setq cnt (1- cnt)))
  7345. (if (> arg 0)
  7346. ;; Moving forward - still need to move over subtree
  7347. (progn (org-end-of-subtree t t)
  7348. (save-excursion
  7349. (org-back-over-empty-lines)
  7350. (or (bolp) (newline)))))
  7351. (setq ne-ins (org-back-over-empty-lines))
  7352. (move-marker ins-point (point))
  7353. (setq txt (buffer-substring beg end))
  7354. (org-save-markers-in-region beg end)
  7355. (delete-region beg end)
  7356. (org-remove-empty-overlays-at beg)
  7357. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  7358. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  7359. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  7360. (let ((bbb (point)))
  7361. (insert-before-markers txt)
  7362. (org-reinstall-markers-in-region bbb)
  7363. (move-marker ins-point bbb))
  7364. (or (bolp) (insert "\n"))
  7365. (setq ins-end (point))
  7366. (goto-char ins-point)
  7367. (org-skip-whitespace)
  7368. (when (and (< arg 0)
  7369. (org-first-sibling-p)
  7370. (> ne-ins ne-beg))
  7371. ;; Move whitespace back to beginning
  7372. (save-excursion
  7373. (goto-char ins-end)
  7374. (let ((kill-whole-line t))
  7375. (kill-line (- ne-ins ne-beg)) (point)))
  7376. (insert (make-string (- ne-ins ne-beg) ?\n)))
  7377. (move-marker ins-point nil)
  7378. (if folded
  7379. (hide-subtree)
  7380. (org-show-entry)
  7381. (show-children)
  7382. (org-cycle-hide-drawers 'children))
  7383. (org-clean-visibility-after-subtree-move)
  7384. ;; move back to the initial column we were at
  7385. (move-to-column col)))
  7386. (defvar org-subtree-clip ""
  7387. "Clipboard for cut and paste of subtrees.
  7388. This is actually only a copy of the kill, because we use the normal kill
  7389. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  7390. (defvar org-subtree-clip-folded nil
  7391. "Was the last copied subtree folded?
  7392. This is used to fold the tree back after pasting.")
  7393. (defun org-cut-subtree (&optional n)
  7394. "Cut the current subtree into the clipboard.
  7395. With prefix arg N, cut this many sequential subtrees.
  7396. This is a short-hand for marking the subtree and then cutting it."
  7397. (interactive "p")
  7398. (org-copy-subtree n 'cut))
  7399. (defun org-copy-subtree (&optional n cut force-store-markers nosubtrees)
  7400. "Copy the current subtree it in the clipboard.
  7401. With prefix arg N, copy this many sequential subtrees.
  7402. This is a short-hand for marking the subtree and then copying it.
  7403. If CUT is non-nil, actually cut the subtree.
  7404. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  7405. of some markers in the region, even if CUT is non-nil. This is
  7406. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  7407. (interactive "p")
  7408. (let (beg end folded (beg0 (point)))
  7409. (if (org-called-interactively-p 'any)
  7410. (org-back-to-heading nil) ; take what looks like a subtree
  7411. (org-back-to-heading t)) ; take what is really there
  7412. (setq beg (point))
  7413. (skip-chars-forward " \t\r\n")
  7414. (save-match-data
  7415. (if nosubtrees
  7416. (outline-next-heading)
  7417. (save-excursion (outline-end-of-heading)
  7418. (setq folded (outline-invisible-p)))
  7419. (condition-case nil
  7420. (org-forward-heading-same-level (1- n) t)
  7421. (error nil))
  7422. (org-end-of-subtree t t)))
  7423. (setq end (point))
  7424. (goto-char beg0)
  7425. (when (> end beg)
  7426. (setq org-subtree-clip-folded folded)
  7427. (when (or cut force-store-markers)
  7428. (org-save-markers-in-region beg end))
  7429. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  7430. (setq org-subtree-clip (current-kill 0))
  7431. (message "%s: Subtree(s) with %d characters"
  7432. (if cut "Cut" "Copied")
  7433. (length org-subtree-clip)))))
  7434. (defun org-paste-subtree (&optional level tree for-yank)
  7435. "Paste the clipboard as a subtree, with modification of headline level.
  7436. The entire subtree is promoted or demoted in order to match a new headline
  7437. level.
  7438. If the cursor is at the beginning of a headline, the same level as
  7439. that headline is used to paste the tree.
  7440. If not, the new level is derived from the *visible* headings
  7441. before and after the insertion point, and taken to be the inferior headline
  7442. level of the two. So if the previous visible heading is level 3 and the
  7443. next is level 4 (or vice versa), level 4 will be used for insertion.
  7444. This makes sure that the subtree remains an independent subtree and does
  7445. not swallow low level entries.
  7446. You can also force a different level, either by using a numeric prefix
  7447. argument, or by inserting the heading marker by hand. For example, if the
  7448. cursor is after \"*****\", then the tree will be shifted to level 5.
  7449. If optional TREE is given, use this text instead of the kill ring.
  7450. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  7451. move back over whitespace before inserting, and move point to the end of
  7452. the inserted text when done."
  7453. (interactive "P")
  7454. (setq tree (or tree (and kill-ring (current-kill 0))))
  7455. (unless (org-kill-is-subtree-p tree)
  7456. (user-error "%s"
  7457. (substitute-command-keys
  7458. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  7459. (org-with-limited-levels
  7460. (let* ((visp (not (outline-invisible-p)))
  7461. (txt tree)
  7462. (^re_ "\\(\\*+\\)[ \t]*")
  7463. (old-level (if (string-match org-outline-regexp-bol txt)
  7464. (- (match-end 0) (match-beginning 0) 1)
  7465. -1))
  7466. (force-level (cond (level (prefix-numeric-value level))
  7467. ((and (looking-at "[ \t]*$")
  7468. (string-match
  7469. "^\\*+$" (buffer-substring
  7470. (point-at-bol) (point))))
  7471. (- (match-end 0) (match-beginning 0)))
  7472. ((and (bolp)
  7473. (looking-at org-outline-regexp))
  7474. (- (match-end 0) (point) 1))))
  7475. (previous-level (save-excursion
  7476. (condition-case nil
  7477. (progn
  7478. (outline-previous-visible-heading 1)
  7479. (if (looking-at ^re_)
  7480. (- (match-end 0) (match-beginning 0) 1)
  7481. 1))
  7482. (error 1))))
  7483. (next-level (save-excursion
  7484. (condition-case nil
  7485. (progn
  7486. (or (looking-at org-outline-regexp)
  7487. (outline-next-visible-heading 1))
  7488. (if (looking-at ^re_)
  7489. (- (match-end 0) (match-beginning 0) 1)
  7490. 1))
  7491. (error 1))))
  7492. (new-level (or force-level (max previous-level next-level)))
  7493. (shift (if (or (= old-level -1)
  7494. (= new-level -1)
  7495. (= old-level new-level))
  7496. 0
  7497. (- new-level old-level)))
  7498. (delta (if (> shift 0) -1 1))
  7499. (func (if (> shift 0) 'org-demote 'org-promote))
  7500. (org-odd-levels-only nil)
  7501. beg end newend)
  7502. ;; Remove the forced level indicator
  7503. (if force-level
  7504. (delete-region (point-at-bol) (point)))
  7505. ;; Paste
  7506. (beginning-of-line (if (bolp) 1 2))
  7507. (setq beg (point))
  7508. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  7509. (insert-before-markers txt)
  7510. (unless (string-match "\n\\'" txt) (insert "\n"))
  7511. (setq newend (point))
  7512. (org-reinstall-markers-in-region beg)
  7513. (setq end (point))
  7514. (goto-char beg)
  7515. (skip-chars-forward " \t\n\r")
  7516. (setq beg (point))
  7517. (if (and (outline-invisible-p) visp)
  7518. (save-excursion (outline-show-heading)))
  7519. ;; Shift if necessary
  7520. (unless (= shift 0)
  7521. (save-restriction
  7522. (narrow-to-region beg end)
  7523. (while (not (= shift 0))
  7524. (org-map-region func (point-min) (point-max))
  7525. (setq shift (+ delta shift)))
  7526. (goto-char (point-min))
  7527. (setq newend (point-max))))
  7528. (when (or (org-called-interactively-p 'interactive) for-yank)
  7529. (message "Clipboard pasted as level %d subtree" new-level))
  7530. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  7531. kill-ring
  7532. (eq org-subtree-clip (current-kill 0))
  7533. org-subtree-clip-folded)
  7534. ;; The tree was folded before it was killed/copied
  7535. (hide-subtree))
  7536. (and for-yank (goto-char newend)))))
  7537. (defun org-kill-is-subtree-p (&optional txt)
  7538. "Check if the current kill is an outline subtree, or a set of trees.
  7539. Returns nil if kill does not start with a headline, or if the first
  7540. headline level is not the largest headline level in the tree.
  7541. So this will actually accept several entries of equal levels as well,
  7542. which is OK for `org-paste-subtree'.
  7543. If optional TXT is given, check this string instead of the current kill."
  7544. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  7545. (re (org-get-limited-outline-regexp))
  7546. (^re (concat "^" re))
  7547. (start-level (and kill
  7548. (string-match
  7549. (concat "\\`\\([ \t\n\r]*?\n\\)?\\(" re "\\)")
  7550. kill)
  7551. (- (match-end 2) (match-beginning 2) 1)))
  7552. (start (1+ (or (match-beginning 2) -1))))
  7553. (if (not start-level)
  7554. (progn
  7555. nil) ;; does not even start with a heading
  7556. (catch 'exit
  7557. (while (setq start (string-match ^re kill (1+ start)))
  7558. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  7559. (throw 'exit nil)))
  7560. t))))
  7561. (defvar org-markers-to-move nil
  7562. "Markers that should be moved with a cut-and-paste operation.
  7563. Those markers are stored together with their positions relative to
  7564. the start of the region.")
  7565. (defun org-save-markers-in-region (beg end)
  7566. "Check markers in region.
  7567. If these markers are between BEG and END, record their position relative
  7568. to BEG, so that after moving the block of text, we can put the markers back
  7569. into place.
  7570. This function gets called just before an entry or tree gets cut from the
  7571. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  7572. called immediately, to move the markers with the entries."
  7573. (setq org-markers-to-move nil)
  7574. (when (featurep 'org-clock)
  7575. (org-clock-save-markers-for-cut-and-paste beg end))
  7576. (when (featurep 'org-agenda)
  7577. (org-agenda-save-markers-for-cut-and-paste beg end)))
  7578. (defun org-check-and-save-marker (marker beg end)
  7579. "Check if MARKER is between BEG and END.
  7580. If yes, remember the marker and the distance to BEG."
  7581. (when (and (marker-buffer marker)
  7582. (equal (marker-buffer marker) (current-buffer)))
  7583. (if (and (>= marker beg) (< marker end))
  7584. (push (cons marker (- marker beg)) org-markers-to-move))))
  7585. (defun org-reinstall-markers-in-region (beg)
  7586. "Move all remembered markers to their position relative to BEG."
  7587. (mapc (lambda (x)
  7588. (move-marker (car x) (+ beg (cdr x))))
  7589. org-markers-to-move)
  7590. (setq org-markers-to-move nil))
  7591. (defun org-narrow-to-subtree ()
  7592. "Narrow buffer to the current subtree."
  7593. (interactive)
  7594. (save-excursion
  7595. (save-match-data
  7596. (org-with-limited-levels
  7597. (narrow-to-region
  7598. (progn (org-back-to-heading t) (point))
  7599. (progn (org-end-of-subtree t t)
  7600. (if (and (org-at-heading-p) (not (eobp))) (backward-char 1))
  7601. (point)))))))
  7602. (defun org-narrow-to-block ()
  7603. "Narrow buffer to the current block."
  7604. (interactive)
  7605. (let* ((case-fold-search t)
  7606. (blockp (org-between-regexps-p "^[ \t]*#\\+begin_.*"
  7607. "^[ \t]*#\\+end_.*")))
  7608. (if blockp
  7609. (narrow-to-region (car blockp) (cdr blockp))
  7610. (user-error "Not in a block"))))
  7611. (eval-when-compile
  7612. (defvar org-property-drawer-re))
  7613. (defvar org-property-start-re) ;; defined below
  7614. (defun org-clone-subtree-with-time-shift (n &optional shift)
  7615. "Clone the task (subtree) at point N times.
  7616. The clones will be inserted as siblings.
  7617. In interactive use, the user will be prompted for the number of
  7618. clones to be produced. If the entry has a timestamp, the user
  7619. will also be prompted for a time shift, which may be a repeater
  7620. as used in time stamps, for example `+3d'. To disable this,
  7621. you can call the function with a universal prefix argument.
  7622. When a valid repeater is given and the entry contains any time
  7623. stamps, the clones will become a sequence in time, with time
  7624. stamps in the subtree shifted for each clone produced. If SHIFT
  7625. is nil or the empty string, time stamps will be left alone. The
  7626. ID property of the original subtree is removed.
  7627. If the original subtree did contain time stamps with a repeater,
  7628. the following will happen:
  7629. - the repeater will be removed in each clone
  7630. - an additional clone will be produced, with the current, unshifted
  7631. date(s) in the entry.
  7632. - the original entry will be placed *after* all the clones, with
  7633. repeater intact.
  7634. - the start days in the repeater in the original entry will be shifted
  7635. to past the last clone.
  7636. In this way you can spell out a number of instances of a repeating task,
  7637. and still retain the repeater to cover future instances of the task."
  7638. (interactive "nNumber of clones to produce: ")
  7639. (let ((shift
  7640. (or shift
  7641. (if (and (not (equal current-prefix-arg '(4)))
  7642. (save-excursion
  7643. (re-search-forward org-ts-regexp-both
  7644. (save-excursion
  7645. (org-end-of-subtree t)
  7646. (point)) t)))
  7647. (read-from-minibuffer
  7648. "Date shift per clone (e.g. +1w, empty to copy unchanged): ")
  7649. ""))) ;; No time shift
  7650. (n-no-remove -1)
  7651. (drawer-re org-drawer-regexp)
  7652. beg end template task idprop
  7653. shift-n shift-what doshift nmin nmax)
  7654. (if (not (and (integerp n) (> n 0)))
  7655. (error "Invalid number of replications %s" n))
  7656. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  7657. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([hdwmy]\\)[ \t]*\\'"
  7658. shift)))
  7659. (error "Invalid shift specification %s" shift))
  7660. (when doshift
  7661. (setq shift-n (string-to-number (match-string 1 shift))
  7662. shift-what (cdr (assoc (match-string 2 shift)
  7663. '(("d" . day) ("w" . week)
  7664. ("m" . month) ("y" . year))))))
  7665. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  7666. (setq nmin 1 nmax n)
  7667. (org-back-to-heading t)
  7668. (setq beg (point))
  7669. (setq idprop (org-entry-get nil "ID"))
  7670. (org-end-of-subtree t t)
  7671. (or (bolp) (insert "\n"))
  7672. (setq end (point))
  7673. (setq template (buffer-substring beg end))
  7674. (when (and doshift
  7675. (string-match "<[^<>\n]+ [.+]?\\+[0-9]+[hdwmy][^<>\n]*>" template))
  7676. (delete-region beg end)
  7677. (setq end beg)
  7678. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  7679. (goto-char end)
  7680. (loop for n from nmin to nmax do
  7681. ;; prepare clone
  7682. (with-temp-buffer
  7683. (insert template)
  7684. (org-mode)
  7685. (goto-char (point-min))
  7686. (org-show-subtree)
  7687. (and idprop (if org-clone-delete-id
  7688. (org-entry-delete nil "ID")
  7689. (org-id-get-create t)))
  7690. (unless (= n 0)
  7691. (while (re-search-forward "^[ \t]*CLOCK:.*$" nil t)
  7692. (kill-whole-line))
  7693. (goto-char (point-min))
  7694. (while (re-search-forward drawer-re nil t)
  7695. (mapc (lambda (d)
  7696. (org-remove-empty-drawer-at d (point))) org-drawers)))
  7697. (goto-char (point-min))
  7698. (when doshift
  7699. (while (re-search-forward org-ts-regexp-both nil t)
  7700. (org-timestamp-change (* n shift-n) shift-what))
  7701. (unless (= n n-no-remove)
  7702. (goto-char (point-min))
  7703. (while (re-search-forward org-ts-regexp nil t)
  7704. (save-excursion
  7705. (goto-char (match-beginning 0))
  7706. (if (looking-at "<[^<>\n]+\\( +[.+]?\\+[0-9]+[hdwmy]\\)")
  7707. (delete-region (match-beginning 1) (match-end 1)))))))
  7708. (setq task (buffer-string)))
  7709. (insert task))
  7710. (goto-char beg)))
  7711. ;;; Outline Sorting
  7712. (defun org-sort (with-case)
  7713. "Call `org-sort-entries', `org-table-sort-lines' or `org-sort-list'.
  7714. Optional argument WITH-CASE means sort case-sensitively."
  7715. (interactive "P")
  7716. (cond
  7717. ((org-at-table-p) (org-call-with-arg 'org-table-sort-lines with-case))
  7718. ((org-at-item-p) (org-call-with-arg 'org-sort-list with-case))
  7719. (t
  7720. (org-call-with-arg 'org-sort-entries with-case))))
  7721. (defun org-sort-remove-invisible (s)
  7722. "Remove invisible links from string S."
  7723. (remove-text-properties 0 (length s) org-rm-props s)
  7724. (while (string-match org-bracket-link-regexp s)
  7725. (setq s (replace-match (if (match-end 2)
  7726. (match-string 3 s)
  7727. (match-string 1 s)) t t s)))
  7728. (let ((st (format " %s " s)))
  7729. (while (string-match org-emph-re st)
  7730. (setq st (replace-match (format " %s " (match-string 4 st)) t t st)))
  7731. (setq s (substring st 1 -1)))
  7732. s)
  7733. (defvar org-priority-regexp) ; defined later in the file
  7734. (defvar org-after-sorting-entries-or-items-hook nil
  7735. "Hook that is run after a bunch of entries or items have been sorted.
  7736. When children are sorted, the cursor is in the parent line when this
  7737. hook gets called. When a region or a plain list is sorted, the cursor
  7738. will be in the first entry of the sorted region/list.")
  7739. (defun org-sort-entries
  7740. (&optional with-case sorting-type getkey-func compare-func property)
  7741. "Sort entries on a certain level of an outline tree.
  7742. If there is an active region, the entries in the region are sorted.
  7743. Else, if the cursor is before the first entry, sort the top-level items.
  7744. Else, the children of the entry at point are sorted.
  7745. Sorting can be alphabetically, numerically, by date/time as given by
  7746. a time stamp, by a property, by priority order, or by a custom function.
  7747. The command prompts for the sorting type unless it has been given to the
  7748. function through the SORTING-TYPE argument, which needs to be a character,
  7749. \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?o ?O ?r ?R ?f ?F). Here is the
  7750. precise meaning of each character:
  7751. n Numerically, by converting the beginning of the entry/item to a number.
  7752. a Alphabetically, ignoring the TODO keyword and the priority, if any.
  7753. o By order of TODO keywords.
  7754. t By date/time, either the first active time stamp in the entry, or, if
  7755. none exist, by the first inactive one.
  7756. s By the scheduled date/time.
  7757. d By deadline date/time.
  7758. c By creation time, which is assumed to be the first inactive time stamp
  7759. at the beginning of a line.
  7760. p By priority according to the cookie.
  7761. r By the value of a property.
  7762. Capital letters will reverse the sort order.
  7763. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  7764. called with point at the beginning of the record. It must return either
  7765. a string or a number that should serve as the sorting key for that record.
  7766. Comparing entries ignores case by default. However, with an optional argument
  7767. WITH-CASE, the sorting considers case as well.
  7768. Sorting is done against the visible part of the headlines, it ignores hidden
  7769. links."
  7770. (interactive "P")
  7771. (let ((case-func (if with-case 'identity 'downcase))
  7772. (cmstr
  7773. ;; The clock marker is lost when using `sort-subr', let's
  7774. ;; store the clocking string.
  7775. (when (equal (marker-buffer org-clock-marker) (current-buffer))
  7776. (save-excursion
  7777. (goto-char org-clock-marker)
  7778. (looking-back "^.*") (match-string-no-properties 0))))
  7779. start beg end stars re re2
  7780. txt what tmp)
  7781. ;; Find beginning and end of region to sort
  7782. (cond
  7783. ((org-region-active-p)
  7784. ;; we will sort the region
  7785. (setq end (region-end)
  7786. what "region")
  7787. (goto-char (region-beginning))
  7788. (if (not (org-at-heading-p)) (outline-next-heading))
  7789. (setq start (point)))
  7790. ((or (org-at-heading-p)
  7791. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  7792. ;; we will sort the children of the current headline
  7793. (org-back-to-heading)
  7794. (setq start (point)
  7795. end (progn (org-end-of-subtree t t)
  7796. (or (bolp) (insert "\n"))
  7797. (when (>= (org-back-over-empty-lines) 1)
  7798. (forward-line 1))
  7799. (point))
  7800. what "children")
  7801. (goto-char start)
  7802. (show-subtree)
  7803. (outline-next-heading))
  7804. (t
  7805. ;; we will sort the top-level entries in this file
  7806. (goto-char (point-min))
  7807. (or (org-at-heading-p) (outline-next-heading))
  7808. (setq start (point))
  7809. (goto-char (point-max))
  7810. (beginning-of-line 1)
  7811. (when (looking-at ".*?\\S-")
  7812. ;; File ends in a non-white line
  7813. (end-of-line 1)
  7814. (insert "\n"))
  7815. (setq end (point-max))
  7816. (setq what "top-level")
  7817. (goto-char start)
  7818. (show-all)))
  7819. (setq beg (point))
  7820. (when (>= beg end) (goto-char start) (user-error "Nothing to sort"))
  7821. (looking-at "\\(\\*+\\)")
  7822. (setq stars (match-string 1)
  7823. re (concat "^" (regexp-quote stars) " +")
  7824. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[ \t\n]")
  7825. txt (buffer-substring beg end))
  7826. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  7827. (if (and (not (equal stars "*")) (string-match re2 txt))
  7828. (user-error "Region to sort contains a level above the first entry"))
  7829. (unless sorting-type
  7830. (message
  7831. "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
  7832. [t]ime [s]cheduled [d]eadline [c]reated
  7833. A/N/P/R/O/F/T/S/D/C means reversed:"
  7834. what)
  7835. (setq sorting-type (read-char-exclusive))
  7836. (unless getkey-func
  7837. (and (= (downcase sorting-type) ?f)
  7838. (setq getkey-func
  7839. (org-icompleting-read "Sort using function: "
  7840. obarray 'fboundp t nil nil))
  7841. (setq getkey-func (intern getkey-func))))
  7842. (and (= (downcase sorting-type) ?r)
  7843. (not property)
  7844. (setq property
  7845. (org-icompleting-read "Property: "
  7846. (mapcar 'list (org-buffer-property-keys t))
  7847. nil t))))
  7848. (message "Sorting entries...")
  7849. (save-restriction
  7850. (narrow-to-region start end)
  7851. (let ((dcst (downcase sorting-type))
  7852. (case-fold-search nil)
  7853. (now (current-time)))
  7854. (sort-subr
  7855. (/= dcst sorting-type)
  7856. ;; This function moves to the beginning character of the "record" to
  7857. ;; be sorted.
  7858. (lambda nil
  7859. (if (re-search-forward re nil t)
  7860. (goto-char (match-beginning 0))
  7861. (goto-char (point-max))))
  7862. ;; This function moves to the last character of the "record" being
  7863. ;; sorted.
  7864. (lambda nil
  7865. (save-match-data
  7866. (condition-case nil
  7867. (outline-forward-same-level 1)
  7868. (error
  7869. (goto-char (point-max))))))
  7870. ;; This function returns the value that gets sorted against.
  7871. (lambda nil
  7872. (cond
  7873. ((= dcst ?n)
  7874. (if (looking-at org-complex-heading-regexp)
  7875. (string-to-number (org-sort-remove-invisible (match-string 4)))
  7876. nil))
  7877. ((= dcst ?a)
  7878. (if (looking-at org-complex-heading-regexp)
  7879. (funcall case-func (org-sort-remove-invisible (match-string 4)))
  7880. nil))
  7881. ((= dcst ?t)
  7882. (let ((end (save-excursion (outline-next-heading) (point))))
  7883. (if (or (re-search-forward org-ts-regexp end t)
  7884. (re-search-forward org-ts-regexp-both end t))
  7885. (org-time-string-to-seconds (match-string 0))
  7886. (org-float-time now))))
  7887. ((= dcst ?c)
  7888. (let ((end (save-excursion (outline-next-heading) (point))))
  7889. (if (re-search-forward
  7890. (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
  7891. end t)
  7892. (org-time-string-to-seconds (match-string 0))
  7893. (org-float-time now))))
  7894. ((= dcst ?s)
  7895. (let ((end (save-excursion (outline-next-heading) (point))))
  7896. (if (re-search-forward org-scheduled-time-regexp end t)
  7897. (org-time-string-to-seconds (match-string 1))
  7898. (org-float-time now))))
  7899. ((= dcst ?d)
  7900. (let ((end (save-excursion (outline-next-heading) (point))))
  7901. (if (re-search-forward org-deadline-time-regexp end t)
  7902. (org-time-string-to-seconds (match-string 1))
  7903. (org-float-time now))))
  7904. ((= dcst ?p)
  7905. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  7906. (string-to-char (match-string 2))
  7907. org-default-priority))
  7908. ((= dcst ?r)
  7909. (or (org-entry-get nil property) ""))
  7910. ((= dcst ?o)
  7911. (if (looking-at org-complex-heading-regexp)
  7912. (- 9999 (length (member (match-string 2)
  7913. org-todo-keywords-1)))))
  7914. ((= dcst ?f)
  7915. (if getkey-func
  7916. (progn
  7917. (setq tmp (funcall getkey-func))
  7918. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  7919. tmp)
  7920. (error "Invalid key function `%s'" getkey-func)))
  7921. (t (error "Invalid sorting type `%c'" sorting-type))))
  7922. nil
  7923. (cond
  7924. ((= dcst ?a) 'string<)
  7925. ((= dcst ?f) compare-func)
  7926. ((member dcst '(?p ?t ?s ?d ?c)) '<)))))
  7927. (run-hooks 'org-after-sorting-entries-or-items-hook)
  7928. ;; Reset the clock marker if needed
  7929. (when cmstr
  7930. (save-excursion
  7931. (goto-char start)
  7932. (search-forward cmstr nil t)
  7933. (move-marker org-clock-marker (point))))
  7934. (message "Sorting entries...done")))
  7935. (defun org-do-sort (table what &optional with-case sorting-type)
  7936. "Sort TABLE of WHAT according to SORTING-TYPE.
  7937. The user will be prompted for the SORTING-TYPE if the call to this
  7938. function does not specify it. WHAT is only for the prompt, to indicate
  7939. what is being sorted. The sorting key will be extracted from
  7940. the car of the elements of the table.
  7941. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  7942. (unless sorting-type
  7943. (message
  7944. "Sort %s: [a]lphabetic, [n]umeric, [t]ime. A/N/T means reversed:"
  7945. what)
  7946. (setq sorting-type (read-char-exclusive)))
  7947. (let ((dcst (downcase sorting-type))
  7948. extractfun comparefun)
  7949. ;; Define the appropriate functions
  7950. (cond
  7951. ((= dcst ?n)
  7952. (setq extractfun 'string-to-number
  7953. comparefun (if (= dcst sorting-type) '< '>)))
  7954. ((= dcst ?a)
  7955. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  7956. (lambda(x) (downcase (org-sort-remove-invisible x))))
  7957. comparefun (if (= dcst sorting-type)
  7958. 'string<
  7959. (lambda (a b) (and (not (string< a b))
  7960. (not (string= a b)))))))
  7961. ((= dcst ?t)
  7962. (setq extractfun
  7963. (lambda (x)
  7964. (if (or (string-match org-ts-regexp x)
  7965. (string-match org-ts-regexp-both x))
  7966. (org-float-time
  7967. (org-time-string-to-time (match-string 0 x)))
  7968. 0))
  7969. comparefun (if (= dcst sorting-type) '< '>)))
  7970. (t (error "Invalid sorting type `%c'" sorting-type)))
  7971. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  7972. table)
  7973. (lambda (a b) (funcall comparefun (car a) (car b))))))
  7974. ;;; The orgstruct minor mode
  7975. ;; Define a minor mode which can be used in other modes in order to
  7976. ;; integrate the org-mode structure editing commands.
  7977. ;; This is really a hack, because the org-mode structure commands use
  7978. ;; keys which normally belong to the major mode. Here is how it
  7979. ;; works: The minor mode defines all the keys necessary to operate the
  7980. ;; structure commands, but wraps the commands into a function which
  7981. ;; tests if the cursor is currently at a headline or a plain list
  7982. ;; item. If that is the case, the structure command is used,
  7983. ;; temporarily setting many Org-mode variables like regular
  7984. ;; expressions for filling etc. However, when any of those keys is
  7985. ;; used at a different location, function uses `key-binding' to look
  7986. ;; up if the key has an associated command in another currently active
  7987. ;; keymap (minor modes, major mode, global), and executes that
  7988. ;; command. There might be problems if any of the keys is otherwise
  7989. ;; used as a prefix key.
  7990. (defcustom orgstruct-heading-prefix-regexp ""
  7991. "Regexp that matches the custom prefix of Org headlines in
  7992. orgstruct(++)-mode."
  7993. :group 'org
  7994. :version "24.4"
  7995. :package-version '(Org . "8.3")
  7996. :type 'regexp)
  7997. ;;;###autoload(put 'orgstruct-heading-prefix-regexp 'safe-local-variable 'stringp)
  7998. (defcustom orgstruct-setup-hook nil
  7999. "Hook run after orgstruct-mode-map is filled."
  8000. :group 'org
  8001. :version "24.4"
  8002. :package-version '(Org . "8.0")
  8003. :type 'hook)
  8004. (defvar orgstruct-initialized nil)
  8005. (defvar org-local-vars nil
  8006. "List of local variables, for use by `orgstruct-mode'.")
  8007. ;;;###autoload
  8008. (define-minor-mode orgstruct-mode
  8009. "Toggle the minor mode `orgstruct-mode'.
  8010. This mode is for using Org-mode structure commands in other
  8011. modes. The following keys behave as if Org-mode were active, if
  8012. the cursor is on a headline, or on a plain list item (both as
  8013. defined by Org-mode)."
  8014. nil " OrgStruct" (make-sparse-keymap)
  8015. (funcall (if orgstruct-mode
  8016. 'add-to-invisibility-spec
  8017. 'remove-from-invisibility-spec)
  8018. '(outline . t))
  8019. (when orgstruct-mode
  8020. (org-load-modules-maybe)
  8021. (unless orgstruct-initialized
  8022. (orgstruct-setup)
  8023. (setq orgstruct-initialized t))))
  8024. ;;;###autoload
  8025. (defun turn-on-orgstruct ()
  8026. "Unconditionally turn on `orgstruct-mode'."
  8027. (orgstruct-mode 1))
  8028. (defvar org-fb-vars nil)
  8029. (make-variable-buffer-local 'org-fb-vars)
  8030. (defun orgstruct++-mode (&optional arg)
  8031. "Toggle `orgstruct-mode', the enhanced version of it.
  8032. In addition to setting orgstruct-mode, this also exports all
  8033. indentation and autofilling variables from org-mode into the
  8034. buffer. It will also recognize item context in multiline items."
  8035. (interactive "P")
  8036. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  8037. (if (< arg 1)
  8038. (progn (orgstruct-mode -1)
  8039. (mapc (lambda(v)
  8040. (org-set-local (car v)
  8041. (if (eq (car-safe (cadr v)) 'quote) (cadadr v) (cadr v))))
  8042. org-fb-vars))
  8043. (orgstruct-mode 1)
  8044. (setq org-fb-vars nil)
  8045. (unless org-local-vars
  8046. (setq org-local-vars (org-get-local-variables)))
  8047. (let (var val)
  8048. (mapc
  8049. (lambda (x)
  8050. (when (string-match
  8051. "^\\(paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|fill-prefix\\|indent-\\)"
  8052. (symbol-name (car x)))
  8053. (setq var (car x) val (nth 1 x))
  8054. (push (list var `(quote ,(eval var))) org-fb-vars)
  8055. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  8056. org-local-vars)
  8057. (org-set-local 'orgstruct-is-++ t))))
  8058. (defvar orgstruct-is-++ nil
  8059. "Is `orgstruct-mode' in ++ version in the current-buffer?")
  8060. (make-variable-buffer-local 'orgstruct-is-++)
  8061. ;;;###autoload
  8062. (defun turn-on-orgstruct++ ()
  8063. "Unconditionally turn on `orgstruct++-mode'."
  8064. (orgstruct++-mode 1))
  8065. (defun orgstruct-error ()
  8066. "Error when there is no default binding for a structure key."
  8067. (interactive)
  8068. (funcall (if (fboundp 'user-error)
  8069. 'user-error
  8070. 'error)
  8071. "This key has no function outside structure elements"))
  8072. (defun orgstruct-setup ()
  8073. "Setup orgstruct keymap."
  8074. (dolist (cell '((org-demote . t)
  8075. (org-metaleft . t)
  8076. (org-metaright . t)
  8077. (org-promote . t)
  8078. (org-shiftmetaleft . t)
  8079. (org-shiftmetaright . t)
  8080. org-backward-element
  8081. org-backward-heading-same-level
  8082. org-ctrl-c-ret
  8083. org-ctrl-c-minus
  8084. org-ctrl-c-star
  8085. org-cycle
  8086. org-forward-heading-same-level
  8087. org-insert-heading
  8088. org-insert-heading-respect-content
  8089. org-kill-note-or-show-branches
  8090. org-mark-subtree
  8091. org-meta-return
  8092. org-metadown
  8093. org-metaup
  8094. org-narrow-to-subtree
  8095. org-promote-subtree
  8096. org-reveal
  8097. org-shiftdown
  8098. org-shiftleft
  8099. org-shiftmetadown
  8100. org-shiftmetaup
  8101. org-shiftright
  8102. org-shifttab
  8103. org-shifttab
  8104. org-shiftup
  8105. org-show-subtree
  8106. org-sort
  8107. org-up-element
  8108. outline-demote
  8109. outline-next-visible-heading
  8110. outline-previous-visible-heading
  8111. outline-promote
  8112. outline-up-heading
  8113. show-children))
  8114. (let ((f (or (car-safe cell) cell))
  8115. (disable-when-heading-prefix (cdr-safe cell)))
  8116. (when (fboundp f)
  8117. (let ((new-bindings))
  8118. (dolist (binding (nconc (where-is-internal f org-mode-map)
  8119. (where-is-internal f outline-mode-map)))
  8120. (push binding new-bindings)
  8121. ;; TODO use local-function-key-map
  8122. (dolist (rep '(("<tab>" . "TAB")
  8123. ("<return>" . "RET")
  8124. ("<escape>" . "ESC")
  8125. ("<delete>" . "DEL")))
  8126. (setq binding (read-kbd-macro
  8127. (let ((case-fold-search))
  8128. (replace-regexp-in-string
  8129. (regexp-quote (cdr rep))
  8130. (car rep)
  8131. (key-description binding)))))
  8132. (pushnew binding new-bindings :test 'equal)))
  8133. (dolist (binding new-bindings)
  8134. (let ((key (lookup-key orgstruct-mode-map binding)))
  8135. (when (or (not key) (numberp key))
  8136. (condition-case nil
  8137. (org-defkey orgstruct-mode-map
  8138. binding
  8139. (orgstruct-make-binding f binding disable-when-heading-prefix))
  8140. (error nil)))))))))
  8141. (run-hooks 'orgstruct-setup-hook))
  8142. (defun orgstruct-make-binding (fun key disable-when-heading-prefix)
  8143. "Create a function for binding in the structure minor mode.
  8144. FUN is the command to call inside a table. KEY is the key that
  8145. should be checked in for a command to execute outside of tables.
  8146. Non-nil `disable-when-heading-prefix' means to disable the command
  8147. if `orgstruct-heading-prefix-regexp' is not empty."
  8148. (let ((name (concat "orgstruct-hijacker-" (symbol-name fun))))
  8149. (let ((nname name)
  8150. (i 0))
  8151. (while (fboundp (intern nname))
  8152. (setq nname (format "%s-%d" name (setq i (1+ i)))))
  8153. (setq name (intern nname)))
  8154. (eval
  8155. (let ((bindings '((org-heading-regexp
  8156. (concat "^"
  8157. orgstruct-heading-prefix-regexp
  8158. "\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ ]*$"))
  8159. (org-outline-regexp
  8160. (concat orgstruct-heading-prefix-regexp "\\*+ "))
  8161. (org-outline-regexp-bol
  8162. (concat "^" org-outline-regexp))
  8163. (outline-regexp org-outline-regexp)
  8164. (outline-heading-end-regexp "\n")
  8165. (outline-level 'org-outline-level)
  8166. (outline-heading-alist))))
  8167. `(defun ,name (arg)
  8168. ,(concat "In Structure, run `" (symbol-name fun) "'.\n"
  8169. "Outside of structure, run the binding of `"
  8170. (key-description key) "'."
  8171. (when disable-when-heading-prefix
  8172. (concat
  8173. "\nIf `orgstruct-heading-prefix-regexp' is not empty, this command will always fall\n"
  8174. "back to the default binding due to limitations of Org's implementation of\n"
  8175. "`" (symbol-name fun) "'.")))
  8176. (interactive "p")
  8177. (let* ((disable
  8178. ,(and disable-when-heading-prefix
  8179. '(not (string= orgstruct-heading-prefix-regexp ""))))
  8180. (fallback
  8181. (or disable
  8182. (not
  8183. (let* ,bindings
  8184. (org-context-p 'headline 'item
  8185. ,(when (memq fun
  8186. '(org-insert-heading
  8187. org-insert-heading-respect-content
  8188. org-meta-return))
  8189. '(when orgstruct-is-++
  8190. 'item-body))))))))
  8191. (if fallback
  8192. (let* ((orgstruct-mode)
  8193. (binding
  8194. (loop with key = ,key
  8195. for rep in
  8196. '(nil
  8197. ("<\\([^>]*\\)tab>" . "\\1TAB")
  8198. ("<\\([^>]*\\)return>" . "\\1RET")
  8199. ("<\\([^>]*\\)escape>" . "\\1ESC")
  8200. ("<\\([^>]*\\)delete>" . "\\1DEL"))
  8201. do
  8202. (when rep
  8203. (setq key (read-kbd-macro
  8204. (let ((case-fold-search))
  8205. (replace-regexp-in-string
  8206. (car rep)
  8207. (cdr rep)
  8208. (key-description key))))))
  8209. thereis (key-binding key))))
  8210. (if (keymapp binding)
  8211. (org-set-transient-map binding)
  8212. (let ((func (or binding
  8213. (unless disable
  8214. 'orgstruct-error))))
  8215. (when func
  8216. (call-interactively func)))))
  8217. (org-run-like-in-org-mode
  8218. (lambda ()
  8219. (interactive)
  8220. (let* ,bindings
  8221. (call-interactively ',fun)))))))))
  8222. name))
  8223. (defun org-contextualize-keys (alist contexts)
  8224. "Return valid elements in ALIST depending on CONTEXTS.
  8225. `org-agenda-custom-commands' or `org-capture-templates' are the
  8226. values used for ALIST, and `org-agenda-custom-commands-contexts'
  8227. or `org-capture-templates-contexts' are the associated contexts
  8228. definitions."
  8229. (let ((contexts
  8230. ;; normalize contexts
  8231. (mapcar
  8232. (lambda(c) (cond ((listp (cadr c))
  8233. (list (car c) (car c) (cadr c)))
  8234. ((string= "" (cadr c))
  8235. (list (car c) (car c) (caddr c)))
  8236. (t c))) contexts))
  8237. (a alist) c r s)
  8238. ;; loop over all commands or templates
  8239. (while (setq c (pop a))
  8240. (let (vrules repl)
  8241. (cond
  8242. ((not (assoc (car c) contexts))
  8243. (push c r))
  8244. ((and (assoc (car c) contexts)
  8245. (setq vrules (org-contextualize-validate-key
  8246. (car c) contexts)))
  8247. (mapc (lambda (vr)
  8248. (when (not (equal (car vr) (cadr vr)))
  8249. (setq repl vr))) vrules)
  8250. (if (not repl) (push c r)
  8251. (push (cadr repl) s)
  8252. (push
  8253. (cons (car c)
  8254. (cdr (or (assoc (cadr repl) alist)
  8255. (error "Undefined key `%s' as contextual replacement for `%s'"
  8256. (cadr repl) (car c)))))
  8257. r))))))
  8258. ;; Return limited ALIST, possibly with keys modified, and deduplicated
  8259. (delq
  8260. nil
  8261. (delete-dups
  8262. (mapcar (lambda (x)
  8263. (let ((tpl (car x)))
  8264. (when (not (delq
  8265. nil
  8266. (mapcar (lambda(y)
  8267. (equal y tpl)) s))) x)))
  8268. (reverse r))))))
  8269. (defun org-contextualize-validate-key (key contexts)
  8270. "Check CONTEXTS for agenda or capture KEY."
  8271. (let (r rr res)
  8272. (while (setq r (pop contexts))
  8273. (mapc
  8274. (lambda (rr)
  8275. (when
  8276. (and (equal key (car r))
  8277. (if (functionp rr) (funcall rr)
  8278. (or (and (eq (car rr) 'in-file)
  8279. (buffer-file-name)
  8280. (string-match (cdr rr) (buffer-file-name)))
  8281. (and (eq (car rr) 'in-mode)
  8282. (string-match (cdr rr) (symbol-name major-mode)))
  8283. (and (eq (car rr) 'in-buffer)
  8284. (string-match (cdr rr) (buffer-name)))
  8285. (when (and (eq (car rr) 'not-in-file)
  8286. (buffer-file-name))
  8287. (not (string-match (cdr rr) (buffer-file-name))))
  8288. (when (eq (car rr) 'not-in-mode)
  8289. (not (string-match (cdr rr) (symbol-name major-mode))))
  8290. (when (eq (car rr) 'not-in-buffer)
  8291. (not (string-match (cdr rr) (buffer-name)))))))
  8292. (push r res)))
  8293. (car (last r))))
  8294. (delete-dups (delq nil res))))
  8295. (defun org-context-p (&rest contexts)
  8296. "Check if local context is any of CONTEXTS.
  8297. Possible values in the list of contexts are `table', `headline', and `item'."
  8298. (let ((pos (point)))
  8299. (goto-char (point-at-bol))
  8300. (prog1 (or (and (memq 'table contexts)
  8301. (looking-at "[ \t]*|"))
  8302. (and (memq 'headline contexts)
  8303. (looking-at org-outline-regexp))
  8304. (and (memq 'item contexts)
  8305. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  8306. (and (memq 'item-body contexts)
  8307. (org-in-item-p)))
  8308. (goto-char pos))))
  8309. (defun org-get-local-variables ()
  8310. "Return a list of all local variables in an Org mode buffer."
  8311. (let (varlist)
  8312. (with-current-buffer (get-buffer-create "*Org tmp*")
  8313. (erase-buffer)
  8314. (org-mode)
  8315. (setq varlist (buffer-local-variables)))
  8316. (kill-buffer "*Org tmp*")
  8317. (delq nil
  8318. (mapcar
  8319. (lambda (x)
  8320. (setq x
  8321. (if (symbolp x)
  8322. (list x)
  8323. (list (car x) (cdr x))))
  8324. (if (and (not (get (car x) 'org-state))
  8325. (string-match
  8326. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|indent-\\)"
  8327. (symbol-name (car x))))
  8328. x nil))
  8329. varlist))))
  8330. (defun org-clone-local-variables (from-buffer &optional regexp)
  8331. "Clone local variables from FROM-BUFFER.
  8332. Optional argument REGEXP selects variables to clone."
  8333. (mapc
  8334. (lambda (pair)
  8335. (and (symbolp (car pair))
  8336. (or (null regexp)
  8337. (string-match regexp (symbol-name (car pair))))
  8338. (set (make-local-variable (car pair))
  8339. (cdr pair))))
  8340. (buffer-local-variables from-buffer)))
  8341. ;;;###autoload
  8342. (defun org-run-like-in-org-mode (cmd)
  8343. "Run a command, pretending that the current buffer is in Org-mode.
  8344. This will temporarily bind local variables that are typically bound in
  8345. Org-mode to the values they have in Org-mode, and then interactively
  8346. call CMD."
  8347. (org-load-modules-maybe)
  8348. (unless org-local-vars
  8349. (setq org-local-vars (org-get-local-variables)))
  8350. (let (binds)
  8351. (dolist (var org-local-vars)
  8352. (when (or (not (boundp (car var)))
  8353. (eq (symbol-value (car var))
  8354. (default-value (car var))))
  8355. (push (list (car var) `(quote ,(cadr var))) binds)))
  8356. (eval `(let ,binds
  8357. (call-interactively (quote ,cmd))))))
  8358. ;;;; Archiving
  8359. (defun org-get-category (&optional pos force-refresh)
  8360. "Get the category applying to position POS."
  8361. (save-match-data
  8362. (if force-refresh (org-refresh-category-properties))
  8363. (let ((pos (or pos (point))))
  8364. (or (get-text-property pos 'org-category)
  8365. (progn (org-refresh-category-properties)
  8366. (get-text-property pos 'org-category))))))
  8367. (defun org-refresh-category-properties ()
  8368. "Refresh category text properties in the buffer."
  8369. (let ((case-fold-search t)
  8370. (inhibit-read-only t)
  8371. (def-cat (cond
  8372. ((null org-category)
  8373. (if buffer-file-name
  8374. (file-name-sans-extension
  8375. (file-name-nondirectory buffer-file-name))
  8376. "???"))
  8377. ((symbolp org-category) (symbol-name org-category))
  8378. (t org-category)))
  8379. beg end cat pos optionp)
  8380. (org-with-silent-modifications
  8381. (save-excursion
  8382. (save-restriction
  8383. (widen)
  8384. (goto-char (point-min))
  8385. (put-text-property (point) (point-max) 'org-category def-cat)
  8386. (while (re-search-forward
  8387. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  8388. (setq pos (match-end 0)
  8389. optionp (equal (char-after (match-beginning 0)) ?#)
  8390. cat (org-trim (match-string 2)))
  8391. (if optionp
  8392. (setq beg (point-at-bol) end (point-max))
  8393. (org-back-to-heading t)
  8394. (setq beg (point) end (org-end-of-subtree t t)))
  8395. (put-text-property beg end 'org-category cat)
  8396. (put-text-property beg end 'org-category-position beg)
  8397. (goto-char pos)))))))
  8398. (defun org-refresh-properties (dprop tprop)
  8399. "Refresh buffer text properties.
  8400. DPROP is the drawer property and TPROP is the corresponding text
  8401. property to set."
  8402. (let ((case-fold-search t)
  8403. (inhibit-read-only t) p)
  8404. (org-with-silent-modifications
  8405. (save-excursion
  8406. (save-restriction
  8407. (widen)
  8408. (goto-char (point-min))
  8409. (while (re-search-forward (concat "^[ \t]*:" dprop ": +\\(.*\\)[ \t]*$") nil t)
  8410. (setq p (org-match-string-no-properties 1))
  8411. (save-excursion
  8412. (org-back-to-heading t)
  8413. (put-text-property
  8414. (point-at-bol) (or (outline-next-heading) (point-max)) tprop p))))))))
  8415. ;;;; Link Stuff
  8416. ;;; Link abbreviations
  8417. (defun org-link-expand-abbrev (link)
  8418. "Apply replacements as defined in `org-link-abbrev-alist'."
  8419. (if (string-match "^\\([^:]*\\)\\(::?\\(.*\\)\\)?$" link)
  8420. (let* ((key (match-string 1 link))
  8421. (as (or (assoc key org-link-abbrev-alist-local)
  8422. (assoc key org-link-abbrev-alist)))
  8423. (tag (and (match-end 2) (match-string 3 link)))
  8424. rpl)
  8425. (if (not as)
  8426. link
  8427. (setq rpl (cdr as))
  8428. (cond
  8429. ((symbolp rpl) (funcall rpl tag))
  8430. ((string-match "%(\\([^)]+\\))" rpl)
  8431. (replace-match
  8432. (save-match-data
  8433. (funcall (intern-soft (match-string 1 rpl)) tag)) t t rpl))
  8434. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  8435. ((string-match "%h" rpl)
  8436. (replace-match (url-hexify-string (or tag "")) t t rpl))
  8437. (t (concat rpl tag)))))
  8438. link))
  8439. ;;; Storing and inserting links
  8440. (defvar org-insert-link-history nil
  8441. "Minibuffer history for links inserted with `org-insert-link'.")
  8442. (defvar org-stored-links nil
  8443. "Contains the links stored with `org-store-link'.")
  8444. (defvar org-store-link-plist nil
  8445. "Plist with info about the most recently link created with `org-store-link'.")
  8446. (defvar org-link-protocols nil
  8447. "Link protocols added to Org-mode using `org-add-link-type'.")
  8448. (defvar org-store-link-functions nil
  8449. "List of functions that are called to create and store a link.
  8450. Each function will be called in turn until one returns a non-nil
  8451. value. Each function should check if it is responsible for creating
  8452. this link (for example by looking at the major mode).
  8453. If not, it must exit and return nil.
  8454. If yes, it should return a non-nil value after a calling
  8455. `org-store-link-props' with a list of properties and values.
  8456. Special properties are:
  8457. :type The link prefix, like \"http\". This must be given.
  8458. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  8459. This is obligatory as well.
  8460. :description Optional default description for the second pair
  8461. of brackets in an Org-mode link. The user can still change
  8462. this when inserting this link into an Org-mode buffer.
  8463. In addition to these, any additional properties can be specified
  8464. and then used in capture templates.")
  8465. (defun org-add-link-type (type &optional follow export)
  8466. "Add TYPE to the list of `org-link-types'.
  8467. Re-compute all regular expressions depending on `org-link-types'
  8468. FOLLOW and EXPORT are two functions.
  8469. FOLLOW should take the link path as the single argument and do whatever
  8470. is necessary to follow the link, for example find a file or display
  8471. a mail message.
  8472. EXPORT should format the link path for export to one of the export formats.
  8473. It should be a function accepting three arguments:
  8474. path the path of the link, the text after the prefix (like \"http:\")
  8475. desc the description of the link, if any, or a description added by
  8476. org-export-normalize-links if there is none
  8477. format the export format, a symbol like `html' or `latex' or `ascii'..
  8478. The function may use the FORMAT information to return different values
  8479. depending on the format. The return value will be put literally into
  8480. the exported file. If the return value is nil, this means Org should
  8481. do what it normally does with links which do not have EXPORT defined.
  8482. Org-mode has a built-in default for exporting links. If you are happy with
  8483. this default, there is no need to define an export function for the link
  8484. type. For a simple example of an export function, see `org-bbdb.el'."
  8485. (add-to-list 'org-link-types type t)
  8486. (org-make-link-regexps)
  8487. (if (assoc type org-link-protocols)
  8488. (setcdr (assoc type org-link-protocols) (list follow export))
  8489. (push (list type follow export) org-link-protocols)))
  8490. (defvar org-agenda-buffer-name) ; Defined in org-agenda.el
  8491. (defvar org-id-link-to-org-use-id) ; Defined in org-id.el
  8492. ;;;###autoload
  8493. (defun org-store-link (arg)
  8494. "\\<org-mode-map>Store an org-link to the current location.
  8495. This link is added to `org-stored-links' and can later be inserted
  8496. into an org-buffer with \\[org-insert-link].
  8497. For some link types, a prefix arg is interpreted.
  8498. For links to Usenet articles, arg negates `org-gnus-prefer-web-links'.
  8499. For file links, arg negates `org-context-in-file-links'.
  8500. A double prefix arg force skipping storing functions that are not
  8501. part of Org's core.
  8502. A triple prefix arg force storing a link for each line in the
  8503. active region."
  8504. (interactive "P")
  8505. (org-load-modules-maybe)
  8506. (if (and (equal arg '(64)) (org-region-active-p))
  8507. (save-excursion
  8508. (let ((end (region-end)))
  8509. (goto-char (region-beginning))
  8510. (set-mark (point))
  8511. (while (< (point-at-eol) end)
  8512. (move-end-of-line 1) (activate-mark)
  8513. (let (current-prefix-arg)
  8514. (call-interactively 'org-store-link))
  8515. (move-beginning-of-line 2)
  8516. (set-mark (point)))))
  8517. (org-with-limited-levels
  8518. (setq org-store-link-plist nil)
  8519. (let (link cpltxt desc description search
  8520. txt custom-id agenda-link sfuns sfunsn)
  8521. (cond
  8522. ;; Store a link using an external link type
  8523. ((and (not (equal arg '(16)))
  8524. (setq sfuns
  8525. (delq
  8526. nil (mapcar (lambda (f)
  8527. (let (fs) (if (funcall f) (push f fs))))
  8528. org-store-link-functions))
  8529. sfunsn (mapcar (lambda (fu) (symbol-name (car fu))) sfuns))
  8530. (or (and (cdr sfuns)
  8531. (funcall (intern
  8532. (completing-read
  8533. "Which function for creating the link? "
  8534. sfunsn nil t (car sfunsn)))))
  8535. (funcall (caar sfuns)))
  8536. (setq link (plist-get org-store-link-plist :link)
  8537. desc (or (plist-get org-store-link-plist
  8538. :description) link))))
  8539. ;; Store a link from a source code buffer
  8540. ((org-src-edit-buffer-p)
  8541. (let (label gc)
  8542. (while (or (not label)
  8543. (save-excursion
  8544. (save-restriction
  8545. (widen)
  8546. (goto-char (point-min))
  8547. (re-search-forward
  8548. (regexp-quote (format org-coderef-label-format label))
  8549. nil t))))
  8550. (when label (message "Label exists already") (sit-for 2))
  8551. (setq label (read-string "Code line label: " label)))
  8552. (end-of-line 1)
  8553. (setq link (format org-coderef-label-format label))
  8554. (setq gc (- 79 (length link)))
  8555. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  8556. (insert link)
  8557. (setq link (concat "(" label ")") desc nil)))
  8558. ;; We are in the agenda, link to referenced location
  8559. ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
  8560. (let ((m (or (get-text-property (point) 'org-hd-marker)
  8561. (get-text-property (point) 'org-marker))))
  8562. (when m
  8563. (org-with-point-at m
  8564. (setq agenda-link
  8565. (if (org-called-interactively-p 'any)
  8566. (call-interactively 'org-store-link)
  8567. (org-store-link nil)))))))
  8568. ((eq major-mode 'calendar-mode)
  8569. (let ((cd (calendar-cursor-to-date)))
  8570. (setq link
  8571. (format-time-string
  8572. (car org-time-stamp-formats)
  8573. (apply 'encode-time
  8574. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  8575. nil nil nil))))
  8576. (org-store-link-props :type "calendar" :date cd)))
  8577. ((eq major-mode 'help-mode)
  8578. (setq link (concat "help:" (save-excursion
  8579. (goto-char (point-min))
  8580. (looking-at "^[^ ]+")
  8581. (match-string 0))))
  8582. (org-store-link-props :type "help"))
  8583. ((eq major-mode 'w3-mode)
  8584. (setq cpltxt (if (and (buffer-name)
  8585. (not (string-match "Untitled" (buffer-name))))
  8586. (buffer-name)
  8587. (url-view-url t))
  8588. link (url-view-url t))
  8589. (org-store-link-props :type "w3" :url (url-view-url t)))
  8590. ((eq major-mode 'image-mode)
  8591. (setq cpltxt (concat "file:"
  8592. (abbreviate-file-name buffer-file-name))
  8593. link cpltxt)
  8594. (org-store-link-props :type "image" :file buffer-file-name))
  8595. ;; In dired, store a link to the file of the current line
  8596. ((eq major-mode 'dired-mode)
  8597. (let ((file (dired-get-filename nil t)))
  8598. (setq file (if file
  8599. (abbreviate-file-name
  8600. (expand-file-name (dired-get-filename nil t)))
  8601. ;; otherwise, no file so use current directory.
  8602. default-directory))
  8603. (setq cpltxt (concat "file:" file)
  8604. link cpltxt)))
  8605. ((setq search (run-hook-with-args-until-success
  8606. 'org-create-file-search-functions))
  8607. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  8608. "::" search))
  8609. (setq cpltxt (or description link)))
  8610. ((and (buffer-file-name (buffer-base-buffer)) (derived-mode-p 'org-mode))
  8611. (setq custom-id (org-entry-get nil "CUSTOM_ID"))
  8612. (cond
  8613. ;; Store a link using the target at point
  8614. ((org-in-regexp "[^<]<<\\([^<>]+\\)>>[^>]" 1)
  8615. (setq cpltxt
  8616. (concat "file:"
  8617. (abbreviate-file-name
  8618. (buffer-file-name (buffer-base-buffer)))
  8619. "::" (match-string 1))
  8620. link cpltxt))
  8621. ((and (featurep 'org-id)
  8622. (or (eq org-id-link-to-org-use-id t)
  8623. (and (org-called-interactively-p 'any)
  8624. (or (eq org-id-link-to-org-use-id 'create-if-interactive)
  8625. (and (eq org-id-link-to-org-use-id
  8626. 'create-if-interactive-and-no-custom-id)
  8627. (not custom-id))))
  8628. (and org-id-link-to-org-use-id (org-entry-get nil "ID"))))
  8629. ;; Store a link using the ID at point
  8630. (setq link (condition-case nil
  8631. (prog1 (org-id-store-link)
  8632. (setq desc (or (plist-get org-store-link-plist
  8633. :description)
  8634. "")))
  8635. (error
  8636. ;; Probably before first headline, link only to file
  8637. (concat "file:"
  8638. (abbreviate-file-name
  8639. (buffer-file-name (buffer-base-buffer))))))))
  8640. (t
  8641. ;; Just link to current headline
  8642. (setq cpltxt (concat "file:"
  8643. (abbreviate-file-name
  8644. (buffer-file-name (buffer-base-buffer)))))
  8645. ;; Add a context search string
  8646. (when (org-xor org-context-in-file-links arg)
  8647. (let* ((ee (org-element-at-point))
  8648. (et (org-element-type ee))
  8649. (ev (plist-get (cadr ee) :value))
  8650. (ek (plist-get (cadr ee) :key))
  8651. (eok (and (stringp ek) (string-match "name" ek))))
  8652. (setq txt (cond
  8653. ((org-at-heading-p) nil)
  8654. ((and (eq et 'keyword) eok) ev)
  8655. ((org-region-active-p)
  8656. (buffer-substring (region-beginning) (region-end)))))
  8657. (when (or (null txt) (string-match "\\S-" txt))
  8658. (setq cpltxt
  8659. (concat cpltxt "::"
  8660. (condition-case nil
  8661. (org-make-org-heading-search-string txt)
  8662. (error "")))
  8663. desc (or (and (eq et 'keyword) eok ev)
  8664. (nth 4 (ignore-errors (org-heading-components)))
  8665. "NONE")))))
  8666. (if (string-match "::\\'" cpltxt)
  8667. (setq cpltxt (substring cpltxt 0 -2)))
  8668. (setq link cpltxt))))
  8669. ((buffer-file-name (buffer-base-buffer))
  8670. ;; Just link to this file here.
  8671. (setq cpltxt (concat "file:"
  8672. (abbreviate-file-name
  8673. (buffer-file-name (buffer-base-buffer)))))
  8674. ;; Add a context string.
  8675. (when (org-xor org-context-in-file-links arg)
  8676. (setq txt (if (org-region-active-p)
  8677. (buffer-substring (region-beginning) (region-end))
  8678. (buffer-substring (point-at-bol) (point-at-eol))))
  8679. ;; Only use search option if there is some text.
  8680. (when (string-match "\\S-" txt)
  8681. (setq cpltxt
  8682. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  8683. desc "NONE")))
  8684. (setq link cpltxt))
  8685. ((org-called-interactively-p 'interactive)
  8686. (user-error "No method for storing a link from this buffer"))
  8687. (t (setq link nil)))
  8688. ;; We're done setting link and desc, clean up
  8689. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  8690. (setq link (or link cpltxt)
  8691. desc (or desc cpltxt))
  8692. (cond ((equal desc "NONE") (setq desc nil))
  8693. ((and desc (string-match org-bracket-link-analytic-regexp desc))
  8694. (let ((d0 (match-string 3 desc))
  8695. (p0 (match-string 5 desc)))
  8696. (setq desc
  8697. (replace-regexp-in-string
  8698. org-bracket-link-regexp
  8699. (concat (or p0 d0)
  8700. (if (equal (length (match-string 0 desc))
  8701. (length desc)) "*" "")) desc)))))
  8702. ;; Return the link
  8703. (if (not (and (or (org-called-interactively-p 'any)
  8704. executing-kbd-macro) link))
  8705. (or agenda-link (and link (org-make-link-string link desc)))
  8706. (push (list link desc) org-stored-links)
  8707. (message "Stored: %s" (or desc link))
  8708. (when custom-id
  8709. (setq link (concat "file:" (abbreviate-file-name
  8710. (buffer-file-name)) "::#" custom-id))
  8711. (push (list link desc) org-stored-links))
  8712. (car org-stored-links))))))
  8713. (defun org-store-link-props (&rest plist)
  8714. "Store link properties, extract names and addresses."
  8715. (let (x adr)
  8716. (when (setq x (plist-get plist :from))
  8717. (setq adr (mail-extract-address-components x))
  8718. (setq plist (plist-put plist :fromname (car adr)))
  8719. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  8720. (when (setq x (plist-get plist :to))
  8721. (setq adr (mail-extract-address-components x))
  8722. (setq plist (plist-put plist :toname (car adr)))
  8723. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  8724. (let ((from (plist-get plist :from))
  8725. (to (plist-get plist :to)))
  8726. (when (and from to org-from-is-user-regexp)
  8727. (setq plist
  8728. (plist-put plist :fromto
  8729. (if (string-match org-from-is-user-regexp from)
  8730. (concat "to %t")
  8731. (concat "from %f"))))))
  8732. (setq org-store-link-plist plist))
  8733. (defun org-add-link-props (&rest plist)
  8734. "Add these properties to the link property list."
  8735. (let (key value)
  8736. (while plist
  8737. (setq key (pop plist) value (pop plist))
  8738. (setq org-store-link-plist
  8739. (plist-put org-store-link-plist key value)))))
  8740. (defun org-email-link-description (&optional fmt)
  8741. "Return the description part of an email link.
  8742. This takes information from `org-store-link-plist' and formats it
  8743. according to FMT (default from `org-email-link-description-format')."
  8744. (setq fmt (or fmt org-email-link-description-format))
  8745. (let* ((p org-store-link-plist)
  8746. (to (plist-get p :toaddress))
  8747. (from (plist-get p :fromaddress))
  8748. (table
  8749. (list
  8750. (cons "%c" (plist-get p :fromto))
  8751. (cons "%F" (plist-get p :from))
  8752. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  8753. (cons "%T" (plist-get p :to))
  8754. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  8755. (cons "%s" (plist-get p :subject))
  8756. (cons "%d" (plist-get p :date))
  8757. (cons "%m" (plist-get p :message-id)))))
  8758. (when (string-match "%c" fmt)
  8759. ;; Check if the user wrote this message
  8760. (if (and org-from-is-user-regexp from to
  8761. (save-match-data (string-match org-from-is-user-regexp from)))
  8762. (setq fmt (replace-match "to %t" t t fmt))
  8763. (setq fmt (replace-match "from %f" t t fmt))))
  8764. (org-replace-escapes fmt table)))
  8765. (defun org-make-org-heading-search-string (&optional string)
  8766. "Make search string for the current headline or STRING."
  8767. (let ((s (or string
  8768. (and (derived-mode-p 'org-mode)
  8769. (save-excursion
  8770. (org-back-to-heading t)
  8771. (org-element-property :raw-value (org-element-at-point))))))
  8772. (lines org-context-in-file-links))
  8773. (or string (setq s (concat "*" s))) ; Add * for headlines
  8774. (setq s (replace-regexp-in-string "\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" "" s))
  8775. (when (and string (integerp lines) (> lines 0))
  8776. (let ((slines (org-split-string s "\n")))
  8777. (when (< lines (length slines))
  8778. (setq s (mapconcat
  8779. 'identity
  8780. (reverse (nthcdr (- (length slines) lines)
  8781. (reverse slines))) "\n")))))
  8782. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  8783. (defun org-make-link-string (link &optional description)
  8784. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  8785. (unless (string-match "\\S-" link)
  8786. (error "Empty link"))
  8787. (when (and description
  8788. (stringp description)
  8789. (not (string-match "\\S-" description)))
  8790. (setq description nil))
  8791. (when (stringp description)
  8792. ;; Remove brackets from the description, they are fatal.
  8793. (while (string-match "\\[" description)
  8794. (setq description (replace-match "{" t t description)))
  8795. (while (string-match "\\]" description)
  8796. (setq description (replace-match "}" t t description))))
  8797. (when (equal link description)
  8798. ;; No description needed, it is identical
  8799. (setq description nil))
  8800. (when (and (not description)
  8801. (not (string-match (org-image-file-name-regexp) link))
  8802. (not (equal link (org-link-escape link))))
  8803. (setq description (org-extract-attributes link)))
  8804. (setq link
  8805. (cond ((string-match (org-image-file-name-regexp) link) link)
  8806. ((string-match org-link-types-re link)
  8807. (concat (match-string 1 link)
  8808. (org-link-escape (substring link (match-end 1)))))
  8809. (t (org-link-escape link))))
  8810. (concat "[[" link "]"
  8811. (if description (concat "[" description "]") "")
  8812. "]"))
  8813. (defconst org-link-escape-chars
  8814. '(?\ ?\[ ?\] ?\; ?\= ?\+)
  8815. "List of characters that should be escaped in link.
  8816. This is the list that is used for internal purposes.")
  8817. (defconst org-link-escape-chars-browser
  8818. '(?\ ?\")
  8819. "List of escapes for characters that are problematic in links.
  8820. This is the list that is used before handing over to the browser.")
  8821. (defun org-link-escape (text &optional table merge)
  8822. "Return percent escaped representation of TEXT.
  8823. TEXT is a string with the text to escape.
  8824. Optional argument TABLE is a list with characters that should be
  8825. escaped. When nil, `org-link-escape-chars' is used.
  8826. If optional argument MERGE is set, merge TABLE into
  8827. `org-link-escape-chars'."
  8828. (cond
  8829. ((and table merge)
  8830. (mapc (lambda (defchr)
  8831. (unless (member defchr table)
  8832. (setq table (cons defchr table)))) org-link-escape-chars))
  8833. ((null table)
  8834. (setq table org-link-escape-chars)))
  8835. (mapconcat
  8836. (lambda (char)
  8837. (if (or (member char table)
  8838. (and (or (< char 32) (= char 37) (> char 126))
  8839. org-url-hexify-p))
  8840. (mapconcat (lambda (sequence-element)
  8841. (format "%%%.2X" sequence-element))
  8842. (or (encode-coding-char char 'utf-8)
  8843. (error "Unable to percent escape character: %s"
  8844. (char-to-string char))) "")
  8845. (char-to-string char))) text ""))
  8846. (defun org-link-unescape (str)
  8847. "Unhex hexified Unicode strings as returned from the JavaScript function
  8848. encodeURIComponent. E.g. `%C3%B6' is the german o-Umlaut."
  8849. (unless (and (null str) (string= "" str))
  8850. (let ((pos 0) (case-fold-search t) unhexed)
  8851. (while (setq pos (string-match "\\(%[0-9a-f][0-9a-f]\\)+" str pos))
  8852. (setq unhexed (org-link-unescape-compound (match-string 0 str)))
  8853. (setq str (replace-match unhexed t t str))
  8854. (setq pos (+ pos (length unhexed))))))
  8855. str)
  8856. (defun org-link-unescape-compound (hex)
  8857. "Unhexify Unicode hex-chars. E.g. `%C3%B6' is the German o-Umlaut.
  8858. Note: this function also decodes single byte encodings like
  8859. `%E1' (a-acute) if not followed by another `%[A-F0-9]{2}' group."
  8860. (save-match-data
  8861. (let* ((bytes (cdr (split-string hex "%")))
  8862. (ret "")
  8863. (eat 0)
  8864. (sum 0))
  8865. (while bytes
  8866. (let* ((val (string-to-number (pop bytes) 16))
  8867. (shift-xor
  8868. (if (= 0 eat)
  8869. (cond
  8870. ((>= val 252) (cons 6 252))
  8871. ((>= val 248) (cons 5 248))
  8872. ((>= val 240) (cons 4 240))
  8873. ((>= val 224) (cons 3 224))
  8874. ((>= val 192) (cons 2 192))
  8875. (t (cons 0 0)))
  8876. (cons 6 128))))
  8877. (if (>= val 192) (setq eat (car shift-xor)))
  8878. (setq val (logxor val (cdr shift-xor)))
  8879. (setq sum (+ (lsh sum (car shift-xor)) val))
  8880. (if (> eat 0) (setq eat (- eat 1)))
  8881. (cond
  8882. ((= 0 eat) ;multi byte
  8883. (setq ret (concat ret (org-char-to-string sum)))
  8884. (setq sum 0))
  8885. ((not bytes) ; single byte(s)
  8886. (setq ret (org-link-unescape-single-byte-sequence hex))))
  8887. )) ;; end (while bytes
  8888. ret )))
  8889. (defun org-link-unescape-single-byte-sequence (hex)
  8890. "Unhexify hex-encoded single byte character sequences."
  8891. (mapconcat (lambda (byte)
  8892. (char-to-string (string-to-number byte 16)))
  8893. (cdr (split-string hex "%")) ""))
  8894. (defun org-xor (a b)
  8895. "Exclusive or."
  8896. (if a (not b) b))
  8897. (defun org-fixup-message-id-for-http (s)
  8898. "Replace special characters in a message id, so it can be used in an http query."
  8899. (when (string-match "%" s)
  8900. (setq s (mapconcat (lambda (c)
  8901. (if (eq c ?%)
  8902. "%25"
  8903. (char-to-string c)))
  8904. s "")))
  8905. (while (string-match "<" s)
  8906. (setq s (replace-match "%3C" t t s)))
  8907. (while (string-match ">" s)
  8908. (setq s (replace-match "%3E" t t s)))
  8909. (while (string-match "@" s)
  8910. (setq s (replace-match "%40" t t s)))
  8911. s)
  8912. (defun org-link-prettify (link)
  8913. "Return a human-readable representation of LINK.
  8914. The car of LINK must be a raw link the cdr of LINK must be either
  8915. a link description or nil."
  8916. (let ((desc (or (cadr link) "<no description>")))
  8917. (concat (format "%-45s" (substring desc 0 (min (length desc) 40)))
  8918. "<" (car link) ">")))
  8919. ;;;###autoload
  8920. (defun org-insert-link-global ()
  8921. "Insert a link like Org-mode does.
  8922. This command can be called in any mode to insert a link in Org-mode syntax."
  8923. (interactive)
  8924. (org-load-modules-maybe)
  8925. (org-run-like-in-org-mode 'org-insert-link))
  8926. (defun org-insert-all-links (&optional keep)
  8927. "Insert all links in `org-stored-links'."
  8928. (interactive "P")
  8929. (let ((links (copy-sequence org-stored-links)) l)
  8930. (while (setq l (if keep (pop links) (pop org-stored-links)))
  8931. (insert "- ")
  8932. (org-insert-link nil (car l) (or (cadr l) "<no description>"))
  8933. (insert "\n"))))
  8934. (defun org-link-fontify-links-to-this-file ()
  8935. "Fontify links to the current file in `org-stored-links'."
  8936. (let ((f (buffer-file-name)) a b)
  8937. (setq a (mapcar (lambda(l)
  8938. (let ((ll (car l)))
  8939. (when (and (string-match "^file:\\(.+\\)::" ll)
  8940. (equal f (expand-file-name (match-string 1 ll))))
  8941. ll)))
  8942. org-stored-links))
  8943. (when (featurep 'org-id)
  8944. (setq b (mapcar (lambda(l)
  8945. (let ((ll (car l)))
  8946. (when (and (string-match "^id:\\(.+\\)$" ll)
  8947. (equal f (expand-file-name
  8948. (or (org-id-find-id-file
  8949. (match-string 1 ll)) ""))))
  8950. ll)))
  8951. org-stored-links)))
  8952. (mapcar (lambda(l)
  8953. (put-text-property 0 (length l) 'face 'font-lock-comment-face l))
  8954. (delq nil (append a b)))))
  8955. (defvar org-link-links-in-this-file nil)
  8956. (defun org-insert-link (&optional complete-file link-location default-description)
  8957. "Insert a link. At the prompt, enter the link.
  8958. Completion can be used to insert any of the link protocol prefixes like
  8959. http or ftp in use.
  8960. The history can be used to select a link previously stored with
  8961. `org-store-link'. When the empty string is entered (i.e. if you just
  8962. press RET at the prompt), the link defaults to the most recently
  8963. stored link. As SPC triggers completion in the minibuffer, you need to
  8964. use M-SPC or C-q SPC to force the insertion of a space character.
  8965. You will also be prompted for a description, and if one is given, it will
  8966. be displayed in the buffer instead of the link.
  8967. If there is already a link at point, this command will allow you to edit link
  8968. and description parts.
  8969. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  8970. be selected using completion. The path to the file will be relative to the
  8971. current directory if the file is in the current directory or a subdirectory.
  8972. Otherwise, the link will be the absolute path as completed in the minibuffer
  8973. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  8974. option `org-link-file-path-type'.
  8975. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  8976. the current directory or below.
  8977. With three \\[universal-argument] prefixes, negate the meaning of
  8978. `org-keep-stored-link-after-insertion'.
  8979. If `org-make-link-description-function' is non-nil, this function will be
  8980. called with the link target, and the result will be the default
  8981. link description.
  8982. If the LINK-LOCATION parameter is non-nil, this value will be
  8983. used as the link location instead of reading one interactively.
  8984. If the DEFAULT-DESCRIPTION parameter is non-nil, this value will
  8985. be used as the default description."
  8986. (interactive "P")
  8987. (let* ((wcf (current-window-configuration))
  8988. (origbuf (current-buffer))
  8989. (region (if (org-region-active-p)
  8990. (buffer-substring (region-beginning) (region-end))))
  8991. (remove (and region (list (region-beginning) (region-end))))
  8992. (desc region)
  8993. tmphist ; byte-compile incorrectly complains about this
  8994. (link link-location)
  8995. (abbrevs org-link-abbrev-alist-local)
  8996. entry file all-prefixes auto-desc)
  8997. (cond
  8998. (link-location) ; specified by arg, just use it.
  8999. ((org-in-regexp org-bracket-link-regexp 1)
  9000. ;; We do have a link at point, and we are going to edit it.
  9001. (setq remove (list (match-beginning 0) (match-end 0)))
  9002. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  9003. (setq link (read-string "Link: "
  9004. (org-link-unescape
  9005. (org-match-string-no-properties 1)))))
  9006. ((or (org-in-regexp org-angle-link-re)
  9007. (org-in-regexp org-plain-link-re))
  9008. ;; Convert to bracket link
  9009. (setq remove (list (match-beginning 0) (match-end 0))
  9010. link (read-string "Link: "
  9011. (org-remove-angle-brackets (match-string 0)))))
  9012. ((member complete-file '((4) (16)))
  9013. ;; Completing read for file names.
  9014. (setq link (org-file-complete-link complete-file)))
  9015. (t
  9016. ;; Read link, with completion for stored links.
  9017. (org-link-fontify-links-to-this-file)
  9018. (org-switch-to-buffer-other-window "*Org Links*")
  9019. (with-current-buffer "*Org Links*"
  9020. (erase-buffer)
  9021. (insert "Insert a link.
  9022. Use TAB to complete link prefixes, then RET for type-specific completion support\n")
  9023. (when org-stored-links
  9024. (insert "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  9025. (insert (mapconcat 'org-link-prettify
  9026. (reverse org-stored-links) "\n")))
  9027. (goto-char (point-min)))
  9028. (let ((cw (selected-window)))
  9029. (select-window (get-buffer-window "*Org Links*" 'visible))
  9030. (with-current-buffer "*Org Links*" (setq truncate-lines t))
  9031. (unless (pos-visible-in-window-p (point-max))
  9032. (org-fit-window-to-buffer))
  9033. (and (window-live-p cw) (select-window cw)))
  9034. ;; Fake a link history, containing the stored links.
  9035. (setq tmphist (append (mapcar 'car org-stored-links)
  9036. org-insert-link-history))
  9037. (setq all-prefixes (append (mapcar 'car abbrevs)
  9038. (mapcar 'car org-link-abbrev-alist)
  9039. org-link-types))
  9040. (unwind-protect
  9041. (progn
  9042. (setq link
  9043. (org-completing-read
  9044. "Link: "
  9045. (append
  9046. (mapcar (lambda (x) (concat x ":"))
  9047. all-prefixes)
  9048. (mapcar 'car org-stored-links))
  9049. nil nil nil
  9050. 'tmphist
  9051. (caar org-stored-links)))
  9052. (if (not (string-match "\\S-" link))
  9053. (user-error "No link selected"))
  9054. (mapc (lambda(l)
  9055. (when (equal link (cadr l)) (setq link (car l) auto-desc t)))
  9056. org-stored-links)
  9057. (if (or (member link all-prefixes)
  9058. (and (equal ":" (substring link -1))
  9059. (member (substring link 0 -1) all-prefixes)
  9060. (setq link (substring link 0 -1))))
  9061. (setq link (with-current-buffer origbuf
  9062. (org-link-try-special-completion link)))))
  9063. (set-window-configuration wcf)
  9064. (kill-buffer "*Org Links*"))
  9065. (setq entry (assoc link org-stored-links))
  9066. (or entry (push link org-insert-link-history))
  9067. (setq desc (or desc (nth 1 entry)))))
  9068. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  9069. (not org-keep-stored-link-after-insertion))
  9070. (setq org-stored-links (delq (assoc link org-stored-links)
  9071. org-stored-links)))
  9072. (if (and (string-match org-plain-link-re link)
  9073. (not (string-match org-ts-regexp link)))
  9074. ;; URL-like link, normalize the use of angular brackets.
  9075. (setq link (org-remove-angle-brackets link)))
  9076. ;; Check if we are linking to the current file with a search
  9077. ;; option If yes, simplify the link by using only the search
  9078. ;; option.
  9079. (when (and buffer-file-name
  9080. (string-match "^file:\\(.+?\\)::\\(.+\\)" link))
  9081. (let* ((path (match-string 1 link))
  9082. (case-fold-search nil)
  9083. (search (match-string 2 link)))
  9084. (save-match-data
  9085. (if (equal (file-truename buffer-file-name) (file-truename path))
  9086. ;; We are linking to this same file, with a search option
  9087. (setq link search)))))
  9088. ;; Check if we can/should use a relative path. If yes, simplify the link
  9089. (when (string-match "^\\(file:\\|docview:\\)\\(.*\\)" link)
  9090. (let* ((type (match-string 1 link))
  9091. (path (match-string 2 link))
  9092. (origpath path)
  9093. (case-fold-search nil))
  9094. (cond
  9095. ((or (eq org-link-file-path-type 'absolute)
  9096. (equal complete-file '(16)))
  9097. (setq path (abbreviate-file-name (expand-file-name path))))
  9098. ((eq org-link-file-path-type 'noabbrev)
  9099. (setq path (expand-file-name path)))
  9100. ((eq org-link-file-path-type 'relative)
  9101. (setq path (file-relative-name path)))
  9102. (t
  9103. (save-match-data
  9104. (if (string-match (concat "^" (regexp-quote
  9105. (expand-file-name
  9106. (file-name-as-directory
  9107. default-directory))))
  9108. (expand-file-name path))
  9109. ;; We are linking a file with relative path name.
  9110. (setq path (substring (expand-file-name path)
  9111. (match-end 0)))
  9112. (setq path (abbreviate-file-name (expand-file-name path)))))))
  9113. (setq link (concat type path))
  9114. (if (equal desc origpath)
  9115. (setq desc path))))
  9116. (if org-make-link-description-function
  9117. (setq desc
  9118. (or (condition-case nil
  9119. (funcall org-make-link-description-function link desc)
  9120. (error (progn (message "Can't get link description from `%s'"
  9121. (symbol-name org-make-link-description-function))
  9122. (sit-for 2) nil)))
  9123. (read-string "Description: " default-description)))
  9124. (if default-description (setq desc default-description)
  9125. (setq desc (or (and auto-desc desc)
  9126. (read-string "Description: " desc)))))
  9127. (unless (string-match "\\S-" desc) (setq desc nil))
  9128. (if remove (apply 'delete-region remove))
  9129. (insert (org-make-link-string link desc))))
  9130. (defun org-link-try-special-completion (type)
  9131. "If there is completion support for link type TYPE, offer it."
  9132. (let ((fun (intern (concat "org-" type "-complete-link"))))
  9133. (if (functionp fun)
  9134. (funcall fun)
  9135. (read-string "Link (no completion support): " (concat type ":")))))
  9136. (defun org-file-complete-link (&optional arg)
  9137. "Create a file link using completion."
  9138. (let (file link)
  9139. (setq file (org-iread-file-name "File: "))
  9140. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  9141. (pwd1 (file-name-as-directory (abbreviate-file-name
  9142. (expand-file-name ".")))))
  9143. (cond
  9144. ((equal arg '(16))
  9145. (setq link (concat
  9146. "file:"
  9147. (abbreviate-file-name (expand-file-name file)))))
  9148. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  9149. (setq link (concat "file:" (match-string 1 file))))
  9150. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  9151. (expand-file-name file))
  9152. (setq link (concat
  9153. "file:" (match-string 1 (expand-file-name file)))))
  9154. (t (setq link (concat "file:" file)))))
  9155. link))
  9156. (defun org-iread-file-name (&rest args)
  9157. "Read-file-name using `ido-mode' speedup if available.
  9158. ARGS are arguments that may be passed to `ido-read-file-name' or `read-file-name'.
  9159. See `read-file-name' for a description of parameters."
  9160. (org-without-partial-completion
  9161. (if (and org-completion-use-ido
  9162. (fboundp 'ido-read-file-name)
  9163. (boundp 'ido-mode) ido-mode
  9164. (listp (second args)))
  9165. (let ((ido-enter-matching-directory nil))
  9166. (apply 'ido-read-file-name args))
  9167. (apply 'read-file-name args))))
  9168. (defun org-completing-read (&rest args)
  9169. "Completing-read with SPACE being a normal character."
  9170. (let ((enable-recursive-minibuffers t)
  9171. (minibuffer-local-completion-map
  9172. (copy-keymap minibuffer-local-completion-map)))
  9173. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  9174. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  9175. (org-defkey minibuffer-local-completion-map (kbd "C-c !") 'org-time-stamp-inactive)
  9176. (apply 'org-icompleting-read args)))
  9177. (defun org-completing-read-no-i (&rest args)
  9178. (let (org-completion-use-ido org-completion-use-iswitchb)
  9179. (apply 'org-completing-read args)))
  9180. (defun org-iswitchb-completing-read (prompt choices &rest args)
  9181. "Use iswitch as a completing-read replacement to choose from choices.
  9182. PROMPT is a string to prompt with. CHOICES is a list of strings to choose
  9183. from."
  9184. (let* ((iswitchb-use-virtual-buffers nil)
  9185. (iswitchb-make-buflist-hook
  9186. (lambda ()
  9187. (setq iswitchb-temp-buflist choices))))
  9188. (iswitchb-read-buffer prompt)))
  9189. (defun org-icompleting-read (&rest args)
  9190. "Completing-read using `ido-mode' or `iswitchb' speedups if available."
  9191. (org-without-partial-completion
  9192. (if (and org-completion-use-ido
  9193. (fboundp 'ido-completing-read)
  9194. (boundp 'ido-mode) ido-mode
  9195. (listp (second args)))
  9196. (let ((ido-enter-matching-directory nil))
  9197. (apply 'ido-completing-read (concat (car args))
  9198. (if (consp (car (nth 1 args)))
  9199. (mapcar 'car (nth 1 args))
  9200. (nth 1 args))
  9201. (cddr args)))
  9202. (if (and org-completion-use-iswitchb
  9203. (boundp 'iswitchb-mode) iswitchb-mode
  9204. (listp (second args)))
  9205. (apply 'org-iswitchb-completing-read (concat (car args))
  9206. (if (consp (car (nth 1 args)))
  9207. (mapcar 'car (nth 1 args))
  9208. (nth 1 args))
  9209. (cddr args))
  9210. (apply 'completing-read args)))))
  9211. (defun org-extract-attributes (s)
  9212. "Extract the attributes cookie from a string and set as text property."
  9213. (let (a attr (start 0) key value)
  9214. (save-match-data
  9215. (when (string-match "{{\\([^}]+\\)}}$" s)
  9216. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  9217. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  9218. (setq key (match-string 1 a) value (match-string 2 a)
  9219. start (match-end 0)
  9220. attr (plist-put attr (intern key) value))))
  9221. (org-add-props s nil 'org-attr attr))
  9222. s))
  9223. ;;; Opening/following a link
  9224. (defvar org-link-search-failed nil)
  9225. (defvar org-open-link-functions nil
  9226. "Hook for functions finding a plain text link.
  9227. These functions must take a single argument, the link content.
  9228. They will be called for links that look like [[link text][description]]
  9229. when LINK TEXT does not have a protocol like \"http:\" and does not look
  9230. like a filename (e.g. \"./blue.png\").
  9231. These functions will be called *before* Org attempts to resolve the
  9232. link by doing text searches in the current buffer - so if you want a
  9233. link \"[[target]]\" to still find \"<<target>>\", your function should
  9234. handle this as a special case.
  9235. When the function does handle the link, it must return a non-nil value.
  9236. If it decides that it is not responsible for this link, it must return
  9237. nil to indicate that that Org-mode can continue with other options
  9238. like exact and fuzzy text search.")
  9239. (defun org-next-link (&optional search-backward)
  9240. "Move forward to the next link.
  9241. If the link is in hidden text, expose it."
  9242. (interactive "P")
  9243. (when (and org-link-search-failed (eq this-command last-command))
  9244. (goto-char (point-min))
  9245. (message "Link search wrapped back to beginning of buffer"))
  9246. (setq org-link-search-failed nil)
  9247. (let* ((pos (point))
  9248. (ct (org-context))
  9249. (a (assoc :link ct))
  9250. (srch-fun (if search-backward 're-search-backward 're-search-forward)))
  9251. (cond (a (goto-char (nth (if search-backward 1 2) a)))
  9252. ((looking-at org-any-link-re)
  9253. ;; Don't stay stuck at link without an org-link face
  9254. (forward-char (if search-backward -1 1))))
  9255. (if (funcall srch-fun org-any-link-re nil t)
  9256. (progn
  9257. (goto-char (match-beginning 0))
  9258. (if (outline-invisible-p) (org-show-context)))
  9259. (goto-char pos)
  9260. (setq org-link-search-failed t)
  9261. (message "No further link found"))))
  9262. (defun org-previous-link ()
  9263. "Move backward to the previous link.
  9264. If the link is in hidden text, expose it."
  9265. (interactive)
  9266. (funcall 'org-next-link t))
  9267. (defun org-translate-link (s)
  9268. "Translate a link string if a translation function has been defined."
  9269. (if (and org-link-translation-function
  9270. (fboundp org-link-translation-function)
  9271. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  9272. (progn
  9273. (setq s (funcall org-link-translation-function
  9274. (match-string 1 s) (match-string 2 s)))
  9275. (concat (car s) ":" (cdr s)))
  9276. s))
  9277. (defun org-translate-link-from-planner (type path)
  9278. "Translate a link from Emacs Planner syntax so that Org can follow it.
  9279. This is still an experimental function, your mileage may vary."
  9280. (cond
  9281. ((member type '("http" "https" "news" "ftp"))
  9282. ;; standard Internet links are the same.
  9283. nil)
  9284. ((and (equal type "irc") (string-match "^//" path))
  9285. ;; Planner has two / at the beginning of an irc link, we have 1.
  9286. ;; We should have zero, actually....
  9287. (setq path (substring path 1)))
  9288. ((and (equal type "lisp") (string-match "^/" path))
  9289. ;; Planner has a slash, we do not.
  9290. (setq type "elisp" path (substring path 1)))
  9291. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  9292. ;; A typical message link. Planner has the id after the final slash,
  9293. ;; we separate it with a hash mark
  9294. (setq path (concat (match-string 1 path) "#"
  9295. (org-remove-angle-brackets (match-string 2 path))))))
  9296. (cons type path))
  9297. (defun org-find-file-at-mouse (ev)
  9298. "Open file link or URL at mouse."
  9299. (interactive "e")
  9300. (mouse-set-point ev)
  9301. (org-open-at-point 'in-emacs))
  9302. (defun org-open-at-mouse (ev)
  9303. "Open file link or URL at mouse.
  9304. See the docstring of `org-open-file' for details."
  9305. (interactive "e")
  9306. (mouse-set-point ev)
  9307. (if (eq major-mode 'org-agenda-mode)
  9308. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  9309. (org-open-at-point))
  9310. (defvar org-window-config-before-follow-link nil
  9311. "The window configuration before following a link.
  9312. This is saved in case the need arises to restore it.")
  9313. (defvar org-open-link-marker (make-marker)
  9314. "Marker pointing to the location where `org-open-at-point' was called.")
  9315. ;;;###autoload
  9316. (defun org-open-at-point-global ()
  9317. "Follow a link like Org-mode does.
  9318. This command can be called in any mode to follow a link that has
  9319. Org-mode syntax."
  9320. (interactive)
  9321. (org-run-like-in-org-mode 'org-open-at-point))
  9322. ;;;###autoload
  9323. (defun org-open-link-from-string (s &optional arg reference-buffer)
  9324. "Open a link in the string S, as if it was in Org-mode."
  9325. (interactive "sLink: \nP")
  9326. (let ((reference-buffer (or reference-buffer (current-buffer))))
  9327. (with-temp-buffer
  9328. (let ((org-inhibit-startup (not reference-buffer)))
  9329. (org-mode)
  9330. (insert s)
  9331. (goto-char (point-min))
  9332. (when reference-buffer
  9333. (setq org-link-abbrev-alist-local
  9334. (with-current-buffer reference-buffer
  9335. org-link-abbrev-alist-local)))
  9336. (org-open-at-point arg reference-buffer)))))
  9337. (defvar org-open-at-point-functions nil
  9338. "Hook that is run when following a link at point.
  9339. Functions in this hook must return t if they identify and follow
  9340. a link at point. If they don't find anything interesting at point,
  9341. they must return nil.")
  9342. (defvar org-link-search-inhibit-query nil) ;; dynamically scoped
  9343. (defvar clean-buffer-list-kill-buffer-names) ; Defined in midnight.el
  9344. (defun org-open-at-point (&optional arg reference-buffer)
  9345. "Open link at or after point.
  9346. If there is no link at point, this function will search forward up to
  9347. the end of the current line.
  9348. Normally, files will be opened by an appropriate application. If the
  9349. optional prefix argument ARG is non-nil, Emacs will visit the file.
  9350. With a double prefix argument, try to open outside of Emacs, in the
  9351. application the system uses for this file type."
  9352. (interactive "P")
  9353. ;; if in a code block, then open the block's results
  9354. (unless (call-interactively #'org-babel-open-src-block-result)
  9355. (org-load-modules-maybe)
  9356. (move-marker org-open-link-marker (point))
  9357. (setq org-window-config-before-follow-link (current-window-configuration))
  9358. (org-remove-occur-highlights nil nil t)
  9359. (cond
  9360. ((and (org-at-heading-p)
  9361. (not (org-at-timestamp-p t))
  9362. (not (org-in-regexp
  9363. (concat org-plain-link-re "\\|"
  9364. org-bracket-link-regexp "\\|"
  9365. org-angle-link-re "\\|"
  9366. "[ \t]:[^ \t\n]+:[ \t]*$")))
  9367. (not (get-text-property (point) 'org-linked-text)))
  9368. (or (let* ((lkall (org-offer-links-in-entry (current-buffer) (point) arg))
  9369. (lk0 (car lkall))
  9370. (lk (if (stringp lk0) (list lk0) lk0))
  9371. (lkend (cdr lkall)))
  9372. (mapcar (lambda(l)
  9373. (search-forward l nil lkend)
  9374. (goto-char (match-beginning 0))
  9375. (org-open-at-point))
  9376. lk))
  9377. (progn (require 'org-attach) (org-attach-reveal 'if-exists))))
  9378. ((run-hook-with-args-until-success 'org-open-at-point-functions))
  9379. ((and (org-at-timestamp-p t)
  9380. (not (org-in-regexp org-bracket-link-regexp)))
  9381. (org-follow-timestamp-link))
  9382. ((and (or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  9383. (not (org-in-regexp org-any-link-re)))
  9384. (org-footnote-action))
  9385. (t
  9386. (let (type path link line search (pos (point)))
  9387. (catch 'match
  9388. (save-excursion
  9389. (or (org-in-regexp org-plain-link-re)
  9390. (skip-chars-forward "^]\n\r"))
  9391. (when (org-in-regexp org-bracket-link-regexp 1)
  9392. (setq link (org-extract-attributes
  9393. (org-link-unescape (org-match-string-no-properties 1))))
  9394. (while (string-match " *\n *" link)
  9395. (setq link (replace-match " " t t link)))
  9396. (setq link (org-link-expand-abbrev link))
  9397. (cond
  9398. ((or (file-name-absolute-p link)
  9399. (string-match "^\\.\\.?/" link))
  9400. (setq type "file" path link))
  9401. ((string-match org-link-re-with-space3 link)
  9402. (setq type (match-string 1 link) path (match-string 2 link)))
  9403. ((string-match "^help:+\\(.+\\)" link)
  9404. (setq type "help" path (match-string 1 link)))
  9405. (t (setq type "thisfile" path link)))
  9406. (throw 'match t)))
  9407. (when (get-text-property (point) 'org-linked-text)
  9408. (setq type "thisfile"
  9409. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  9410. (1+ (point)) (point))
  9411. path (buffer-substring
  9412. (or (previous-single-property-change pos 'org-linked-text)
  9413. (point-min))
  9414. (or (next-single-property-change pos 'org-linked-text)
  9415. (point-max)))
  9416. ;; Ensure we will search for a <<<radio>>> link, not
  9417. ;; a simple reference like <<ref>>
  9418. path (concat "<" path))
  9419. (throw 'match t))
  9420. (save-excursion
  9421. (when (or (org-in-regexp org-angle-link-re)
  9422. (let ((match (org-in-regexp org-plain-link-re)))
  9423. ;; Check a plain link is not within a bracket link
  9424. (and match
  9425. (save-excursion
  9426. (save-match-data
  9427. (progn
  9428. (goto-char (car match))
  9429. (not (org-in-regexp org-bracket-link-regexp)))))))
  9430. (let ((line_ending (save-excursion (end-of-line) (point))))
  9431. ;; We are in a line before a plain or bracket link
  9432. (or (re-search-forward org-plain-link-re line_ending t)
  9433. (re-search-forward org-bracket-link-regexp line_ending t))))
  9434. (setq type (match-string 1)
  9435. path (org-link-unescape (match-string 2)))
  9436. (throw 'match t)))
  9437. (save-excursion
  9438. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@#%:]+\\):[ \t]*$"))
  9439. (setq type "tags"
  9440. path (match-string 1))
  9441. (while (string-match ":" path)
  9442. (setq path (replace-match "+" t t path)))
  9443. (throw 'match t)))
  9444. (when (org-in-regexp "<\\([^><\n]+\\)>")
  9445. (setq type "tree-match"
  9446. path (match-string 1))
  9447. (throw 'match t)))
  9448. (unless path
  9449. (user-error "No link found"))
  9450. ;; switch back to reference buffer
  9451. ;; needed when if called in a temporary buffer through
  9452. ;; org-open-link-from-string
  9453. (with-current-buffer (or reference-buffer (current-buffer))
  9454. ;; Remove any trailing spaces in path
  9455. (if (string-match " +\\'" path)
  9456. (setq path (replace-match "" t t path)))
  9457. (if (and org-link-translation-function
  9458. (fboundp org-link-translation-function))
  9459. ;; Check if we need to translate the link
  9460. (let ((tmp (funcall org-link-translation-function type path)))
  9461. (setq type (car tmp) path (cdr tmp))))
  9462. (cond
  9463. ((assoc type org-link-protocols)
  9464. (funcall (nth 1 (assoc type org-link-protocols)) path))
  9465. ((equal type "help")
  9466. (let ((f-or-v (intern path)))
  9467. (cond ((fboundp f-or-v)
  9468. (describe-function f-or-v))
  9469. ((boundp f-or-v)
  9470. (describe-variable f-or-v))
  9471. (t (error "Not a known function or variable")))))
  9472. ((equal type "mailto")
  9473. (let ((cmd (car org-link-mailto-program))
  9474. (args (cdr org-link-mailto-program)) args1
  9475. (address path) (subject "") a)
  9476. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  9477. (setq address (match-string 1 path)
  9478. subject (org-link-escape (match-string 2 path))))
  9479. (while args
  9480. (cond
  9481. ((not (stringp (car args))) (push (pop args) args1))
  9482. (t (setq a (pop args))
  9483. (if (string-match "%a" a)
  9484. (setq a (replace-match address t t a)))
  9485. (if (string-match "%s" a)
  9486. (setq a (replace-match subject t t a)))
  9487. (push a args1))))
  9488. (apply cmd (nreverse args1))))
  9489. ((member type '("http" "https" "ftp" "news"))
  9490. (browse-url
  9491. (concat type ":"
  9492. (if (org-string-match-p
  9493. (concat "[[:nonascii:]"
  9494. org-link-escape-chars-browser "]")
  9495. path)
  9496. (org-link-escape path org-link-escape-chars-browser)
  9497. path))))
  9498. ((string= type "doi")
  9499. (browse-url
  9500. (concat org-doi-server-url
  9501. (if (org-string-match-p
  9502. (concat "[[:nonascii:]"
  9503. org-link-escape-chars-browser "]")
  9504. path)
  9505. (org-link-escape path org-link-escape-chars-browser)
  9506. path))))
  9507. ((member type '("message"))
  9508. (browse-url (concat type ":" path)))
  9509. ((string= type "tags")
  9510. (org-tags-view arg path))
  9511. ((string= type "tree-match")
  9512. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  9513. ((string= type "file")
  9514. (if (string-match "::\\([0-9]+\\)\\'" path)
  9515. (setq line (string-to-number (match-string 1 path))
  9516. path (substring path 0 (match-beginning 0)))
  9517. (if (string-match "::\\(.+\\)\\'" path)
  9518. (setq search (match-string 1 path)
  9519. path (substring path 0 (match-beginning 0)))))
  9520. (if (string-match "[*?{]" (file-name-nondirectory path))
  9521. (dired path)
  9522. (org-open-file path arg line search)))
  9523. ((string= type "shell")
  9524. (let ((buf (generate-new-buffer "*Org Shell Output"))
  9525. (cmd path))
  9526. (if (or (and (not (string= org-confirm-shell-link-not-regexp ""))
  9527. (string-match org-confirm-shell-link-not-regexp cmd))
  9528. (not org-confirm-shell-link-function)
  9529. (funcall org-confirm-shell-link-function
  9530. (format "Execute \"%s\" in shell? "
  9531. (org-add-props cmd nil
  9532. 'face 'org-warning))))
  9533. (progn
  9534. (message "Executing %s" cmd)
  9535. (shell-command cmd buf)
  9536. (if (featurep 'midnight)
  9537. (setq clean-buffer-list-kill-buffer-names
  9538. (cons buf clean-buffer-list-kill-buffer-names))))
  9539. (error "Abort"))))
  9540. ((string= type "elisp")
  9541. (let ((cmd path))
  9542. (if (or (and (not (string= org-confirm-elisp-link-not-regexp ""))
  9543. (string-match org-confirm-elisp-link-not-regexp cmd))
  9544. (not org-confirm-elisp-link-function)
  9545. (funcall org-confirm-elisp-link-function
  9546. (format "Execute \"%s\" as elisp? "
  9547. (org-add-props cmd nil
  9548. 'face 'org-warning))))
  9549. (message "%s => %s" cmd
  9550. (if (equal (string-to-char cmd) ?\()
  9551. (eval (read cmd))
  9552. (call-interactively (read cmd))))
  9553. (error "Abort"))))
  9554. ((and (string= type "thisfile")
  9555. (or (run-hook-with-args-until-success
  9556. 'org-open-link-functions path)
  9557. (and link
  9558. (string-match "^id:" link)
  9559. (or (featurep 'org-id) (require 'org-id))
  9560. (progn
  9561. (funcall (nth 1 (assoc "id" org-link-protocols))
  9562. (substring path 3))
  9563. t)))))
  9564. ((string= type "thisfile")
  9565. (if arg
  9566. (switch-to-buffer-other-window
  9567. (org-get-buffer-for-internal-link (current-buffer)))
  9568. (org-mark-ring-push))
  9569. (let ((cmd `(org-link-search
  9570. ,path
  9571. ,(cond ((equal arg '(4)) ''occur)
  9572. ((equal arg '(16)) ''org-occur))
  9573. ,pos)))
  9574. (condition-case nil (let ((org-link-search-inhibit-query t))
  9575. (eval cmd))
  9576. (error (progn (widen) (eval cmd))))))
  9577. (t (browse-url-at-point)))))))
  9578. (move-marker org-open-link-marker nil)
  9579. (run-hook-with-args 'org-follow-link-hook)))
  9580. (defsubst org-uniquify (list)
  9581. "Non-destructively remove duplicate elements from LIST."
  9582. (let ((res (copy-sequence list))) (delete-dups res)))
  9583. (defun org-offer-links-in-entry (buffer marker &optional nth zero)
  9584. "Offer links in the current entry and return the selected link.
  9585. If there is only one link, return it.
  9586. If NTH is an integer, return the NTH link found.
  9587. If ZERO is a string, check also this string for a link, and if
  9588. there is one, return it."
  9589. (with-current-buffer buffer
  9590. (save-excursion
  9591. (save-restriction
  9592. (widen)
  9593. (goto-char marker)
  9594. (let ((re (concat "\\(" org-bracket-link-regexp "\\)\\|"
  9595. "\\(" org-angle-link-re "\\)\\|"
  9596. "\\(" org-plain-link-re "\\)"))
  9597. (cnt ?0)
  9598. (in-emacs (if (integerp nth) nil nth))
  9599. have-zero end links link c)
  9600. (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
  9601. (push (match-string 0 zero) links)
  9602. (setq cnt (1- cnt) have-zero t))
  9603. (save-excursion
  9604. (org-back-to-heading t)
  9605. (setq end (save-excursion (outline-next-heading) (point)))
  9606. (while (re-search-forward re end t)
  9607. (push (match-string 0) links))
  9608. (setq links (org-uniquify (reverse links))))
  9609. (cond
  9610. ((null links)
  9611. (message "No links"))
  9612. ((equal (length links) 1)
  9613. (setq link (car links)))
  9614. ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
  9615. (setq link (nth (if have-zero nth (1- nth)) links)))
  9616. (t ; we have to select a link
  9617. (save-excursion
  9618. (save-window-excursion
  9619. (delete-other-windows)
  9620. (with-output-to-temp-buffer "*Select Link*"
  9621. (mapc (lambda (l)
  9622. (if (not (string-match org-bracket-link-regexp l))
  9623. (princ (format "[%c] %s\n" (incf cnt)
  9624. (org-remove-angle-brackets l)))
  9625. (if (match-end 3)
  9626. (princ (format "[%c] %s (%s)\n" (incf cnt)
  9627. (match-string 3 l) (match-string 1 l)))
  9628. (princ (format "[%c] %s\n" (incf cnt)
  9629. (match-string 1 l))))))
  9630. links))
  9631. (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
  9632. (message "Select link to open, RET to open all:")
  9633. (setq c (read-char-exclusive))
  9634. (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
  9635. (when (equal c ?q) (error "Abort"))
  9636. (if (equal c ?\C-m)
  9637. (setq link links)
  9638. (setq nth (- c ?0))
  9639. (if have-zero (setq nth (1+ nth)))
  9640. (unless (and (integerp nth) (>= (length links) nth))
  9641. (user-error "Invalid link selection"))
  9642. (setq link (nth (1- nth) links)))))
  9643. (cons link end))))))
  9644. ;; Add special file links that specify the way of opening
  9645. (org-add-link-type "file+sys" 'org-open-file-with-system)
  9646. (org-add-link-type "file+emacs" 'org-open-file-with-emacs)
  9647. (defun org-open-file-with-system (path)
  9648. "Open file at PATH using the system way of opening it."
  9649. (org-open-file path 'system))
  9650. (defun org-open-file-with-emacs (path)
  9651. "Open file at PATH in Emacs."
  9652. (org-open-file path 'emacs))
  9653. ;;; File search
  9654. (defvar org-create-file-search-functions nil
  9655. "List of functions to construct the right search string for a file link.
  9656. These functions are called in turn with point at the location to
  9657. which the link should point.
  9658. A function in the hook should first test if it would like to
  9659. handle this file type, for example by checking the `major-mode'
  9660. or the file extension. If it decides not to handle this file, it
  9661. should just return nil to give other functions a chance. If it
  9662. does handle the file, it must return the search string to be used
  9663. when following the link. The search string will be part of the
  9664. file link, given after a double colon, and `org-open-at-point'
  9665. will automatically search for it. If special measures must be
  9666. taken to make the search successful, another function should be
  9667. added to the companion hook `org-execute-file-search-functions',
  9668. which see.
  9669. A function in this hook may also use `setq' to set the variable
  9670. `description' to provide a suggestion for the descriptive text to
  9671. be used for this link when it gets inserted into an Org-mode
  9672. buffer with \\[org-insert-link].")
  9673. (defvar org-execute-file-search-functions nil
  9674. "List of functions to execute a file search triggered by a link.
  9675. Functions added to this hook must accept a single argument, the
  9676. search string that was part of the file link, the part after the
  9677. double colon. The function must first check if it would like to
  9678. handle this search, for example by checking the `major-mode' or
  9679. the file extension. If it decides not to handle this search, it
  9680. should just return nil to give other functions a chance. If it
  9681. does handle the search, it must return a non-nil value to keep
  9682. other functions from trying.
  9683. Each function can access the current prefix argument through the
  9684. variable `current-prefix-arg'. Note that a single prefix is used
  9685. to force opening a link in Emacs, so it may be good to only use a
  9686. numeric or double prefix to guide the search function.
  9687. In case this is needed, a function in this hook can also restore
  9688. the window configuration before `org-open-at-point' was called using:
  9689. (set-window-configuration org-window-config-before-follow-link)")
  9690. (defun org-link-search (s &optional type avoid-pos stealth)
  9691. "Search for a link search option.
  9692. If S is surrounded by forward slashes, it is interpreted as a
  9693. regular expression. In org-mode files, this will create an `org-occur'
  9694. sparse tree. In ordinary files, `occur' will be used to list matches.
  9695. If the current buffer is in `dired-mode', grep will be used to search
  9696. in all files. If AVOID-POS is given, ignore matches near that position.
  9697. When optional argument STEALTH is non-nil, do not modify
  9698. visibility around point, thus ignoring
  9699. `org-show-hierarchy-above', `org-show-following-heading' and
  9700. `org-show-siblings' variables."
  9701. (let ((case-fold-search t)
  9702. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  9703. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  9704. (append '(("") (" ") ("\t") ("\n"))
  9705. org-emphasis-alist)
  9706. "\\|") "\\)"))
  9707. (pos (point))
  9708. (pre nil) (post nil)
  9709. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  9710. (cond
  9711. ;; First check if there are any special search functions
  9712. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  9713. ;; Now try the builtin stuff
  9714. ((and (equal (string-to-char s0) ?#)
  9715. (> (length s0) 1)
  9716. (save-excursion
  9717. (goto-char (point-min))
  9718. (and
  9719. (re-search-forward
  9720. (concat "^[ \t]*:CUSTOM_ID:[ \t]+"
  9721. (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
  9722. (setq type 'dedicated
  9723. pos (match-beginning 0))))
  9724. ;; There is an exact target for this
  9725. (goto-char pos)
  9726. (org-back-to-heading t)))
  9727. ((save-excursion
  9728. (goto-char (point-min))
  9729. (and
  9730. (re-search-forward
  9731. (concat "<<" (regexp-quote s0) ">>") nil t)
  9732. (setq type 'dedicated
  9733. pos (match-beginning 0))))
  9734. ;; There is an exact target for this
  9735. (goto-char pos))
  9736. ((save-excursion
  9737. (goto-char (point-min))
  9738. (and
  9739. (re-search-forward
  9740. (format "^[ \t]*#\\+NAME: %s" (regexp-quote s0)) nil t)
  9741. (setq type 'dedicated pos (match-beginning 0))))
  9742. ;; Found an element with a matching #+name affiliated keyword.
  9743. (goto-char pos))
  9744. ((and (string-match "^(\\(.*\\))$" s0)
  9745. (save-excursion
  9746. (goto-char (point-min))
  9747. (and
  9748. (re-search-forward
  9749. (concat "[^[]" (regexp-quote
  9750. (format org-coderef-label-format
  9751. (match-string 1 s0))))
  9752. nil t)
  9753. (setq type 'dedicated
  9754. pos (1+ (match-beginning 0))))))
  9755. ;; There is a coderef target for this
  9756. (goto-char pos))
  9757. ((string-match "^/\\(.*\\)/$" s)
  9758. ;; A regular expression
  9759. (cond
  9760. ((derived-mode-p 'org-mode)
  9761. (org-occur (match-string 1 s)))
  9762. (t (org-do-occur (match-string 1 s)))))
  9763. ((and (derived-mode-p 'org-mode) org-link-search-must-match-exact-headline)
  9764. (and (equal (string-to-char s) ?*) (setq s (substring s 1)))
  9765. (goto-char (point-min))
  9766. (cond
  9767. ((let (case-fold-search)
  9768. (re-search-forward (format org-complex-heading-regexp-format
  9769. (regexp-quote s))
  9770. nil t))
  9771. ;; OK, found a match
  9772. (setq type 'dedicated)
  9773. (goto-char (match-beginning 0)))
  9774. ((and (not org-link-search-inhibit-query)
  9775. (eq org-link-search-must-match-exact-headline 'query-to-create)
  9776. (y-or-n-p "No match - create this as a new heading? "))
  9777. (goto-char (point-max))
  9778. (or (bolp) (newline))
  9779. (insert "* " s "\n")
  9780. (beginning-of-line 0))
  9781. (t
  9782. (goto-char pos)
  9783. (error "No match"))))
  9784. (t
  9785. ;; A normal search string
  9786. (when (equal (string-to-char s) ?*)
  9787. ;; Anchor on headlines, post may include tags.
  9788. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  9789. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@#%:+]:[ \t]*\\)?$")
  9790. s (substring s 1)))
  9791. (remove-text-properties
  9792. 0 (length s)
  9793. '(face nil mouse-face nil keymap nil fontified nil) s)
  9794. ;; Make a series of regular expressions to find a match
  9795. (setq words (org-split-string s "[ \n\r\t]+")
  9796. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  9797. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  9798. "\\)" markers)
  9799. re2a_ (concat "\\(" (mapconcat 'downcase words
  9800. "[ \t\r\n]+") "\\)[ \t\r\n]")
  9801. re2a (concat "[ \t\r\n]" re2a_)
  9802. re4_ (concat "\\(" (mapconcat 'downcase words
  9803. "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  9804. re4 (concat "[^a-zA-Z_]" re4_)
  9805. re1 (concat pre re2 post)
  9806. re3 (concat pre (if pre re4_ re4) post)
  9807. re5 (concat pre ".*" re4)
  9808. re2 (concat pre re2)
  9809. re2a (concat pre (if pre re2a_ re2a))
  9810. re4 (concat pre (if pre re4_ re4))
  9811. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  9812. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  9813. re5 "\\)"))
  9814. (cond
  9815. ((eq type 'org-occur) (org-occur reall))
  9816. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  9817. (t (goto-char (point-min))
  9818. (setq type 'fuzzy)
  9819. (if (or (and (org-search-not-self 1 re0 nil t)
  9820. (setq type 'dedicated))
  9821. (org-search-not-self 1 re1 nil t)
  9822. (org-search-not-self 1 re2 nil t)
  9823. (org-search-not-self 1 re2a nil t)
  9824. (org-search-not-self 1 re3 nil t)
  9825. (org-search-not-self 1 re4 nil t)
  9826. (org-search-not-self 1 re5 nil t))
  9827. (goto-char (match-beginning 1))
  9828. (goto-char pos)
  9829. (error "No match"))))))
  9830. (and (derived-mode-p 'org-mode)
  9831. (not stealth)
  9832. (org-show-context 'link-search))
  9833. type))
  9834. (defun org-search-not-self (group &rest args)
  9835. "Execute `re-search-forward', but only accept matches that do not
  9836. enclose the position of `org-open-link-marker'."
  9837. (let ((m org-open-link-marker))
  9838. (catch 'exit
  9839. (while (apply 're-search-forward args)
  9840. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  9841. (goto-char (match-end group))
  9842. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  9843. (> (match-beginning 0) (marker-position m))
  9844. (< (match-end 0) (marker-position m)))
  9845. (save-match-data
  9846. (or (not (org-in-regexp
  9847. org-bracket-link-analytic-regexp 1))
  9848. (not (match-end 4)) ; no description
  9849. (and (<= (match-beginning 4) (point))
  9850. (>= (match-end 4) (point))))))
  9851. (throw 'exit (point))))))))
  9852. (defun org-get-buffer-for-internal-link (buffer)
  9853. "Return a buffer to be used for displaying the link target of internal links."
  9854. (cond
  9855. ((not org-display-internal-link-with-indirect-buffer)
  9856. buffer)
  9857. ((string-match "(Clone)$" (buffer-name buffer))
  9858. (message "Buffer is already a clone, not making another one")
  9859. ;; we also do not modify visibility in this case
  9860. buffer)
  9861. (t ; make a new indirect buffer for displaying the link
  9862. (let* ((bn (buffer-name buffer))
  9863. (ibn (concat bn "(Clone)"))
  9864. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  9865. (with-current-buffer ib (org-overview))
  9866. ib))))
  9867. (defun org-do-occur (regexp &optional cleanup)
  9868. "Call the Emacs command `occur'.
  9869. If CLEANUP is non-nil, remove the printout of the regular expression
  9870. in the *Occur* buffer. This is useful if the regex is long and not useful
  9871. to read."
  9872. (occur regexp)
  9873. (when cleanup
  9874. (let ((cwin (selected-window)) win beg end)
  9875. (when (setq win (get-buffer-window "*Occur*"))
  9876. (select-window win))
  9877. (goto-char (point-min))
  9878. (when (re-search-forward "match[a-z]+" nil t)
  9879. (setq beg (match-end 0))
  9880. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  9881. (setq end (1- (match-beginning 0)))))
  9882. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  9883. (goto-char (point-min))
  9884. (select-window cwin))))
  9885. ;;; The mark ring for links jumps
  9886. (defvar org-mark-ring nil
  9887. "Mark ring for positions before jumps in Org-mode.")
  9888. (defvar org-mark-ring-last-goto nil
  9889. "Last position in the mark ring used to go back.")
  9890. ;; Fill and close the ring
  9891. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  9892. (loop for i from 1 to org-mark-ring-length do
  9893. (push (make-marker) org-mark-ring))
  9894. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  9895. org-mark-ring)
  9896. (defun org-mark-ring-push (&optional pos buffer)
  9897. "Put the current position or POS into the mark ring and rotate it."
  9898. (interactive)
  9899. (setq pos (or pos (point)))
  9900. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  9901. (move-marker (car org-mark-ring)
  9902. (or pos (point))
  9903. (or buffer (current-buffer)))
  9904. (message "%s"
  9905. (substitute-command-keys
  9906. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  9907. (defun org-mark-ring-goto (&optional n)
  9908. "Jump to the previous position in the mark ring.
  9909. With prefix arg N, jump back that many stored positions. When
  9910. called several times in succession, walk through the entire ring.
  9911. Org-mode commands jumping to a different position in the current file,
  9912. or to another Org-mode file, automatically push the old position
  9913. onto the ring."
  9914. (interactive "p")
  9915. (let (p m)
  9916. (if (eq last-command this-command)
  9917. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  9918. (setq p org-mark-ring))
  9919. (setq org-mark-ring-last-goto p)
  9920. (setq m (car p))
  9921. (org-pop-to-buffer-same-window (marker-buffer m))
  9922. (goto-char m)
  9923. (if (or (outline-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  9924. (defun org-remove-angle-brackets (s)
  9925. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  9926. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  9927. s)
  9928. (defun org-add-angle-brackets (s)
  9929. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  9930. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  9931. s)
  9932. (defun org-remove-double-quotes (s)
  9933. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  9934. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  9935. s)
  9936. ;;; Following specific links
  9937. (defun org-follow-timestamp-link ()
  9938. "Open an agenda view for the time-stamp date/range at point."
  9939. (cond
  9940. ((org-at-date-range-p t)
  9941. (let ((org-agenda-start-on-weekday)
  9942. (t1 (match-string 1))
  9943. (t2 (match-string 2)) tt1 tt2)
  9944. (setq tt1 (time-to-days (org-time-string-to-time t1))
  9945. tt2 (time-to-days (org-time-string-to-time t2)))
  9946. (let ((org-agenda-buffer-tmp-name
  9947. (format "*Org Agenda(a:%s)"
  9948. (concat (substring t1 0 10) "--" (substring t2 0 10)))))
  9949. (org-agenda-list nil tt1 (1+ (- tt2 tt1))))))
  9950. ((org-at-timestamp-p t)
  9951. (let ((org-agenda-buffer-tmp-name
  9952. (format "*Org Agenda(a:%s)" (substring (match-string 1) 0 10))))
  9953. (org-agenda-list nil (time-to-days (org-time-string-to-time
  9954. (substring (match-string 1) 0 10)))
  9955. 1)))
  9956. (t (error "This should not happen"))))
  9957. ;;; Following file links
  9958. (declare-function mailcap-parse-mailcaps "mailcap" (&optional path force))
  9959. (declare-function mailcap-extension-to-mime "mailcap" (extn))
  9960. (declare-function mailcap-mime-info
  9961. "mailcap" (string &optional request no-decode))
  9962. (defvar org-wait nil)
  9963. (defun org-open-file (path &optional in-emacs line search)
  9964. "Open the file at PATH.
  9965. First, this expands any special file name abbreviations. Then the
  9966. configuration variable `org-file-apps' is checked if it contains an
  9967. entry for this file type, and if yes, the corresponding command is launched.
  9968. If no application is found, Emacs simply visits the file.
  9969. With optional prefix argument IN-EMACS, Emacs will visit the file.
  9970. With a double \\[universal-argument] \\[universal-argument] \
  9971. prefix arg, Org tries to avoid opening in Emacs
  9972. and to use an external application to visit the file.
  9973. Optional LINE specifies a line to go to, optional SEARCH a string
  9974. to search for. If LINE or SEARCH is given, the file will be
  9975. opened in Emacs, unless an entry from org-file-apps that makes
  9976. use of groups in a regexp matches.
  9977. If you want to change the way frames are used when following a
  9978. link, please customize `org-link-frame-setup'.
  9979. If the file does not exist, an error is thrown."
  9980. (let* ((file (if (equal path "")
  9981. buffer-file-name
  9982. (substitute-in-file-name (expand-file-name path))))
  9983. (file-apps (append org-file-apps (org-default-apps)))
  9984. (apps (org-remove-if
  9985. 'org-file-apps-entry-match-against-dlink-p file-apps))
  9986. (apps-dlink (org-remove-if-not
  9987. 'org-file-apps-entry-match-against-dlink-p file-apps))
  9988. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  9989. (dirp (if remp nil (file-directory-p file)))
  9990. (file (if (and dirp org-open-directory-means-index-dot-org)
  9991. (concat (file-name-as-directory file) "index.org")
  9992. file))
  9993. (a-m-a-p (assq 'auto-mode apps))
  9994. (dfile (downcase file))
  9995. ;; reconstruct the original file: link from the PATH, LINE and SEARCH args
  9996. (link (cond ((and (eq line nil)
  9997. (eq search nil))
  9998. file)
  9999. (line
  10000. (concat file "::" (number-to-string line)))
  10001. (search
  10002. (concat file "::" search))))
  10003. (dlink (downcase link))
  10004. (old-buffer (current-buffer))
  10005. (old-pos (point))
  10006. (old-mode major-mode)
  10007. ext cmd link-match-data)
  10008. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  10009. (setq ext (match-string 1 dfile))
  10010. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  10011. (setq ext (match-string 1 dfile))))
  10012. (cond
  10013. ((member in-emacs '((16) system))
  10014. (setq cmd (cdr (assoc 'system apps))))
  10015. (in-emacs (setq cmd 'emacs))
  10016. (t
  10017. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  10018. (and dirp (cdr (assoc 'directory apps)))
  10019. ; first, try matching against apps-dlink
  10020. ; if we get a match here, store the match data for later
  10021. (let ((match (assoc-default dlink apps-dlink
  10022. 'string-match)))
  10023. (if match
  10024. (progn (setq link-match-data (match-data))
  10025. match)
  10026. (progn (setq in-emacs (or in-emacs line search))
  10027. nil))) ; if we have no match in apps-dlink,
  10028. ; always open the file in emacs if line or search
  10029. ; is given (for backwards compatibility)
  10030. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  10031. 'string-match)
  10032. (cdr (assoc ext apps))
  10033. (cdr (assoc t apps))))))
  10034. (when (eq cmd 'system)
  10035. (setq cmd (cdr (assoc 'system apps))))
  10036. (when (eq cmd 'default)
  10037. (setq cmd (cdr (assoc t apps))))
  10038. (when (eq cmd 'mailcap)
  10039. (require 'mailcap)
  10040. (mailcap-parse-mailcaps)
  10041. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  10042. (command (mailcap-mime-info mime-type)))
  10043. (if (stringp command)
  10044. (setq cmd command)
  10045. (setq cmd 'emacs))))
  10046. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  10047. (not (file-exists-p file))
  10048. (not org-open-non-existing-files))
  10049. (user-error "No such file: %s" file))
  10050. (cond
  10051. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  10052. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  10053. (while (string-match "['\"]%s['\"]" cmd)
  10054. (setq cmd (replace-match "%s" t t cmd)))
  10055. (while (string-match "%s" cmd)
  10056. (setq cmd (replace-match
  10057. (save-match-data
  10058. (shell-quote-argument
  10059. (convert-standard-filename file)))
  10060. t t cmd)))
  10061. ;; Replace "%1", "%2" etc. in command with group matches from regex
  10062. (save-match-data
  10063. (let ((match-index 1)
  10064. (number-of-groups (- (/ (length link-match-data) 2) 1)))
  10065. (set-match-data link-match-data)
  10066. (while (<= match-index number-of-groups)
  10067. (let ((regex (concat "%" (number-to-string match-index)))
  10068. (replace-with (match-string match-index dlink)))
  10069. (while (string-match regex cmd)
  10070. (setq cmd (replace-match replace-with t t cmd))))
  10071. (setq match-index (+ match-index 1)))))
  10072. (save-window-excursion
  10073. (message "Running %s...done" cmd)
  10074. (start-process-shell-command cmd nil cmd)
  10075. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))))
  10076. ((or (stringp cmd)
  10077. (eq cmd 'emacs))
  10078. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  10079. (widen)
  10080. (if line (org-goto-line line)
  10081. (if search (org-link-search search))))
  10082. ((consp cmd)
  10083. (let ((file (convert-standard-filename file)))
  10084. (save-match-data
  10085. (set-match-data link-match-data)
  10086. (eval cmd))))
  10087. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  10088. (and (derived-mode-p 'org-mode) (eq old-mode 'org-mode)
  10089. (or (not (equal old-buffer (current-buffer)))
  10090. (not (equal old-pos (point))))
  10091. (org-mark-ring-push old-pos old-buffer))))
  10092. (defun org-file-apps-entry-match-against-dlink-p (entry)
  10093. "This function returns non-nil if `entry' uses a regular
  10094. expression which should be matched against the whole link by
  10095. org-open-file.
  10096. It assumes that is the case when the entry uses a regular
  10097. expression which has at least one grouping construct and the
  10098. action is either a lisp form or a command string containing
  10099. '%1', i.e. using at least one subexpression match as a
  10100. parameter."
  10101. (let ((selector (car entry))
  10102. (action (cdr entry)))
  10103. (if (stringp selector)
  10104. (and (> (regexp-opt-depth selector) 0)
  10105. (or (and (stringp action)
  10106. (string-match "%[0-9]" action))
  10107. (consp action)))
  10108. nil)))
  10109. (defun org-default-apps ()
  10110. "Return the default applications for this operating system."
  10111. (cond
  10112. ((eq system-type 'darwin)
  10113. org-file-apps-defaults-macosx)
  10114. ((eq system-type 'windows-nt)
  10115. org-file-apps-defaults-windowsnt)
  10116. (t org-file-apps-defaults-gnu)))
  10117. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  10118. "Convert extensions to regular expressions in the cars of LIST.
  10119. Also, weed out any non-string entries, because the return value is used
  10120. only for regexp matching.
  10121. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  10122. point to the symbol `emacs', indicating that the file should
  10123. be opened in Emacs."
  10124. (append
  10125. (delq nil
  10126. (mapcar (lambda (x)
  10127. (if (not (stringp (car x)))
  10128. nil
  10129. (if (string-match "\\W" (car x))
  10130. x
  10131. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  10132. list))
  10133. (if add-auto-mode
  10134. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  10135. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  10136. (defun org-file-remote-p (file)
  10137. "Test whether FILE specifies a location on a remote system.
  10138. Return non-nil if the location is indeed remote.
  10139. For example, the filename \"/user@host:/foo\" specifies a location
  10140. on the system \"/user@host:\"."
  10141. (cond ((fboundp 'file-remote-p)
  10142. (file-remote-p file))
  10143. ((fboundp 'tramp-handle-file-remote-p)
  10144. (tramp-handle-file-remote-p file))
  10145. ((and (boundp 'ange-ftp-name-format)
  10146. (string-match (car ange-ftp-name-format) file))
  10147. t)))
  10148. ;;;; Refiling
  10149. (defun org-get-org-file ()
  10150. "Read a filename, with default directory `org-directory'."
  10151. (let ((default (or org-default-notes-file remember-data-file)))
  10152. (read-file-name (format "File name [%s]: " default)
  10153. (file-name-as-directory org-directory)
  10154. default)))
  10155. (defun org-notes-order-reversed-p ()
  10156. "Check if the current file should receive notes in reversed order."
  10157. (cond
  10158. ((not org-reverse-note-order) nil)
  10159. ((eq t org-reverse-note-order) t)
  10160. ((not (listp org-reverse-note-order)) nil)
  10161. (t (catch 'exit
  10162. (let ((all org-reverse-note-order)
  10163. entry)
  10164. (while (setq entry (pop all))
  10165. (if (string-match (car entry) buffer-file-name)
  10166. (throw 'exit (cdr entry))))
  10167. nil)))))
  10168. (defvar org-refile-target-table nil
  10169. "The list of refile targets, created by `org-refile'.")
  10170. (defvar org-agenda-new-buffers nil
  10171. "Buffers created to visit agenda files.")
  10172. (defvar org-refile-cache nil
  10173. "Cache for refile targets.")
  10174. (defvar org-refile-markers nil
  10175. "All the markers used for caching refile locations.")
  10176. (defun org-refile-marker (pos)
  10177. "Get a new refile marker, but only if caching is in use."
  10178. (if (not org-refile-use-cache)
  10179. pos
  10180. (let ((m (make-marker)))
  10181. (move-marker m pos)
  10182. (push m org-refile-markers)
  10183. m)))
  10184. (defun org-refile-cache-clear ()
  10185. "Clear the refile cache and disable all the markers."
  10186. (mapc (lambda (m) (move-marker m nil)) org-refile-markers)
  10187. (setq org-refile-markers nil)
  10188. (setq org-refile-cache nil)
  10189. (message "Refile cache has been cleared"))
  10190. (defun org-refile-cache-check-set (set)
  10191. "Check if all the markers in the cache still have live buffers."
  10192. (let (marker)
  10193. (catch 'exit
  10194. (while (and set (setq marker (nth 3 (pop set))))
  10195. ;; If `org-refile-use-outline-path' is 'file, marker may be nil
  10196. (when (and marker (null (marker-buffer marker)))
  10197. (message "Please regenerate the refile cache with `C-0 C-c C-w'")
  10198. (sit-for 3)
  10199. (throw 'exit nil)))
  10200. t)))
  10201. (defun org-refile-cache-put (set &rest identifiers)
  10202. "Push the refile targets SET into the cache, under IDENTIFIERS."
  10203. (let* ((key (sha1 (prin1-to-string identifiers)))
  10204. (entry (assoc key org-refile-cache)))
  10205. (if entry
  10206. (setcdr entry set)
  10207. (push (cons key set) org-refile-cache))))
  10208. (defun org-refile-cache-get (&rest identifiers)
  10209. "Retrieve the cached value for refile targets given by IDENTIFIERS."
  10210. (cond
  10211. ((not org-refile-cache) nil)
  10212. ((not org-refile-use-cache) (org-refile-cache-clear) nil)
  10213. (t
  10214. (let ((set (cdr (assoc (sha1 (prin1-to-string identifiers))
  10215. org-refile-cache))))
  10216. (and set (org-refile-cache-check-set set) set)))))
  10217. (defun org-refile-get-targets (&optional default-buffer excluded-entries)
  10218. "Produce a table with refile targets."
  10219. (let ((case-fold-search nil)
  10220. ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
  10221. (entries (or org-refile-targets '((nil . (:level . 1)))))
  10222. targets tgs txt re files f desc descre fast-path-p level pos0)
  10223. (message "Getting targets...")
  10224. (with-current-buffer (or default-buffer (current-buffer))
  10225. (while (setq entry (pop entries))
  10226. (setq files (car entry) desc (cdr entry))
  10227. (setq fast-path-p nil)
  10228. (cond
  10229. ((null files) (setq files (list (current-buffer))))
  10230. ((eq files 'org-agenda-files)
  10231. (setq files (org-agenda-files 'unrestricted)))
  10232. ((and (symbolp files) (fboundp files))
  10233. (setq files (funcall files)))
  10234. ((and (symbolp files) (boundp files))
  10235. (setq files (symbol-value files))))
  10236. (if (stringp files) (setq files (list files)))
  10237. (cond
  10238. ((eq (car desc) :tag)
  10239. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  10240. ((eq (car desc) :todo)
  10241. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  10242. ((eq (car desc) :regexp)
  10243. (setq descre (cdr desc)))
  10244. ((eq (car desc) :level)
  10245. (setq descre (concat "^\\*\\{" (number-to-string
  10246. (if org-odd-levels-only
  10247. (1- (* 2 (cdr desc)))
  10248. (cdr desc)))
  10249. "\\}[ \t]")))
  10250. ((eq (car desc) :maxlevel)
  10251. (setq fast-path-p t)
  10252. (setq descre (concat "^\\*\\{1," (number-to-string
  10253. (if org-odd-levels-only
  10254. (1- (* 2 (cdr desc)))
  10255. (cdr desc)))
  10256. "\\}[ \t]")))
  10257. (t (error "Bad refiling target description %s" desc)))
  10258. (while (setq f (pop files))
  10259. (with-current-buffer
  10260. (if (bufferp f) f (org-get-agenda-file-buffer f))
  10261. (or
  10262. (setq tgs (org-refile-cache-get (buffer-file-name) descre))
  10263. (progn
  10264. (if (bufferp f) (setq f (buffer-file-name
  10265. (buffer-base-buffer f))))
  10266. (setq f (and f (expand-file-name f)))
  10267. (if (eq org-refile-use-outline-path 'file)
  10268. (push (list (file-name-nondirectory f) f nil nil) tgs))
  10269. (save-excursion
  10270. (save-restriction
  10271. (widen)
  10272. (goto-char (point-min))
  10273. (while (re-search-forward descre nil t)
  10274. (goto-char (setq pos0 (point-at-bol)))
  10275. (catch 'next
  10276. (when org-refile-target-verify-function
  10277. (save-match-data
  10278. (or (funcall org-refile-target-verify-function)
  10279. (throw 'next t))))
  10280. (when (and (looking-at org-complex-heading-regexp)
  10281. (not (member (match-string 4) excluded-entries))
  10282. (match-string 4))
  10283. (setq level (org-reduced-level
  10284. (- (match-end 1) (match-beginning 1)))
  10285. txt (org-link-display-format (match-string 4))
  10286. txt (replace-regexp-in-string "\\( *\[[0-9]+/?[0-9]*%?\]\\)+$" "" txt)
  10287. re (format org-complex-heading-regexp-format
  10288. (regexp-quote (match-string 4))))
  10289. (when org-refile-use-outline-path
  10290. (setq txt (mapconcat
  10291. 'org-protect-slash
  10292. (append
  10293. (if (eq org-refile-use-outline-path
  10294. 'file)
  10295. (list (file-name-nondirectory
  10296. (buffer-file-name
  10297. (buffer-base-buffer))))
  10298. (if (eq org-refile-use-outline-path
  10299. 'full-file-path)
  10300. (list (buffer-file-name
  10301. (buffer-base-buffer)))))
  10302. (org-get-outline-path fast-path-p
  10303. level txt)
  10304. (list txt))
  10305. "/")))
  10306. (push (list txt f re (org-refile-marker (point)))
  10307. tgs)))
  10308. (when (= (point) pos0)
  10309. ;; verification function has not moved point
  10310. (goto-char (point-at-eol))))))))
  10311. (when org-refile-use-cache
  10312. (org-refile-cache-put tgs (buffer-file-name) descre))
  10313. (setq targets (append tgs targets))))))
  10314. (message "Getting targets...done")
  10315. (nreverse targets)))
  10316. (defun org-protect-slash (s)
  10317. (while (string-match "/" s)
  10318. (setq s (replace-match "\\" t t s)))
  10319. s)
  10320. (defvar org-olpa (make-vector 20 nil))
  10321. (defun org-get-outline-path (&optional fastp level heading)
  10322. "Return the outline path to the current entry, as a list.
  10323. The parameters FASTP, LEVEL, and HEADING are for use by a scanner
  10324. routine which makes outline path derivations for an entire file,
  10325. avoiding backtracing. Refile target collection makes use of that."
  10326. (if fastp
  10327. (progn
  10328. (if (> level 19)
  10329. (error "Outline path failure, more than 19 levels"))
  10330. (loop for i from level upto 19 do
  10331. (aset org-olpa i nil))
  10332. (prog1
  10333. (delq nil (append org-olpa nil))
  10334. (aset org-olpa level heading)))
  10335. (let (rtn case-fold-search)
  10336. (save-excursion
  10337. (save-restriction
  10338. (widen)
  10339. (while (org-up-heading-safe)
  10340. (when (looking-at org-complex-heading-regexp)
  10341. (push (org-trim
  10342. (replace-regexp-in-string
  10343. ;; Remove statistical/checkboxes cookies
  10344. "\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" ""
  10345. (org-match-string-no-properties 4)))
  10346. rtn)))
  10347. rtn)))))
  10348. (defun org-format-outline-path (path &optional width prefix separator)
  10349. "Format the outline path PATH for display.
  10350. WIDTH is the maximum number of characters that is available.
  10351. PREFIX is a prefix to be included in the returned string,
  10352. such as the file name.
  10353. SEPARATOR is inserted between the different parts of the path,
  10354. the default is \"/\"."
  10355. (setq width (or width 79))
  10356. (if prefix (setq width (- width (length prefix))))
  10357. (if (not path)
  10358. (or prefix "")
  10359. (let* ((nsteps (length path))
  10360. (total-width (+ nsteps (apply '+ (mapcar 'length path))))
  10361. (maxwidth (if (<= total-width width)
  10362. 10000 ;; everything fits
  10363. ;; we need to shorten the level headings
  10364. (/ (- width nsteps) nsteps)))
  10365. (org-odd-levels-only nil)
  10366. (n 0)
  10367. (total (1+ (length prefix))))
  10368. (setq maxwidth (max maxwidth 10))
  10369. (concat prefix
  10370. (if prefix (or separator "/"))
  10371. (mapconcat
  10372. (lambda (h)
  10373. (setq n (1+ n))
  10374. (if (and (= n nsteps) (< maxwidth 10000))
  10375. (setq maxwidth (- total-width total)))
  10376. (if (< (length h) maxwidth)
  10377. (progn (setq total (+ total (length h) 1)) h)
  10378. (setq h (substring h 0 (- maxwidth 2))
  10379. total (+ total maxwidth 1))
  10380. (if (string-match "[ \t]+\\'" h)
  10381. (setq h (substring h 0 (match-beginning 0))))
  10382. (setq h (concat h "..")))
  10383. (org-add-props h nil 'face
  10384. (nth (% (1- n) org-n-level-faces)
  10385. org-level-faces))
  10386. h)
  10387. path (or separator "/"))))))
  10388. (defun org-display-outline-path (&optional file current separator just-return-string)
  10389. "Display the current outline path in the echo area.
  10390. If FILE is non-nil, prepend the output with the file name.
  10391. If CURRENT is non-nil, append the current heading to the output.
  10392. SEPARATOR is passed through to `org-format-outline-path'. It separates
  10393. the different parts of the path and defaults to \"/\".
  10394. If JUST-RETURN-STRING is non-nil, return a string, don't display a message."
  10395. (interactive "P")
  10396. (let* (case-fold-search
  10397. (bfn (buffer-file-name (buffer-base-buffer)))
  10398. (path (and (derived-mode-p 'org-mode) (org-get-outline-path)))
  10399. res)
  10400. (if current (setq path (append path
  10401. (save-excursion
  10402. (org-back-to-heading t)
  10403. (if (looking-at org-complex-heading-regexp)
  10404. (list (match-string 4)))))))
  10405. (setq res
  10406. (org-format-outline-path
  10407. path
  10408. (1- (frame-width))
  10409. (and file bfn (concat (file-name-nondirectory bfn) separator))
  10410. separator))
  10411. (if just-return-string
  10412. (org-no-properties res)
  10413. (org-unlogged-message "%s" res))))
  10414. (defvar org-refile-history nil
  10415. "History for refiling operations.")
  10416. (defvar org-after-refile-insert-hook nil
  10417. "Hook run after `org-refile' has inserted its stuff at the new location.
  10418. Note that this is still *before* the stuff will be removed from
  10419. the *old* location.")
  10420. (defvar org-capture-last-stored-marker)
  10421. (defvar org-refile-keep nil
  10422. "Non-nil means `org-refile' will copy instead of refile.")
  10423. (defun org-copy ()
  10424. "Like `org-refile', but copy."
  10425. (interactive)
  10426. (let ((org-refile-keep t))
  10427. (funcall 'org-refile nil nil nil "Copy")))
  10428. (defun org-refile (&optional goto default-buffer rfloc msg)
  10429. "Move the entry or entries at point to another heading.
  10430. The list of target headings is compiled using the information in
  10431. `org-refile-targets', which see.
  10432. At the target location, the entry is filed as a subitem of the target
  10433. heading. Depending on `org-reverse-note-order', the new subitem will
  10434. either be the first or the last subitem.
  10435. If there is an active region, all entries in that region will be moved.
  10436. However, the region must fulfill the requirement that the first heading
  10437. is the first one sets the top-level of the moved text - at most siblings
  10438. below it are allowed.
  10439. With prefix arg GOTO, the command will only visit the target location
  10440. and not actually move anything.
  10441. With a double prefix arg \\[universal-argument] \\[universal-argument], \
  10442. go to the location where the last refiling operation has put the subtree.
  10443. With a numeric prefix argument of `2', refile to the running clock.
  10444. With a numeric prefix argument of `3', emulate `org-refile-keep'
  10445. being set to `t' and copy to the target location, don't move it.
  10446. Beware that keeping refiled entries may result in duplicated ID
  10447. properties.
  10448. RFLOC can be a refile location obtained in a different way.
  10449. MSG is a string to replace \"Refile\" in the default prompt with
  10450. another verb. E.g. `org-copy' sets this parameter to \"Copy\".
  10451. See also `org-refile-use-outline-path' and `org-completion-use-ido'.
  10452. If you are using target caching (see `org-refile-use-cache'),
  10453. you have to clear the target cache in order to find new targets.
  10454. This can be done with a 0 prefix (`C-0 C-c C-w') or a triple
  10455. prefix argument (`C-u C-u C-u C-c C-w')."
  10456. (interactive "P")
  10457. (if (member goto '(0 (64)))
  10458. (org-refile-cache-clear)
  10459. (let* ((actionmsg (cond (msg msg)
  10460. ((equal goto 3) "Refile (and keep)")
  10461. (t "Refile")))
  10462. (cbuf (current-buffer))
  10463. (regionp (org-region-active-p))
  10464. (region-start (and regionp (region-beginning)))
  10465. (region-end (and regionp (region-end)))
  10466. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  10467. (org-refile-keep (if (equal goto 3) t org-refile-keep))
  10468. pos it nbuf file re level reversed)
  10469. (setq last-command nil)
  10470. (when regionp
  10471. (goto-char region-start)
  10472. (or (bolp) (goto-char (point-at-bol)))
  10473. (setq region-start (point))
  10474. (unless (or (org-kill-is-subtree-p
  10475. (buffer-substring region-start region-end))
  10476. (prog1 org-refile-active-region-within-subtree
  10477. (let ((s (point-at-eol)))
  10478. (org-toggle-heading)
  10479. (setq region-end (+ (- (point-at-eol) s) region-end)))))
  10480. (user-error "The region is not a (sequence of) subtree(s)")))
  10481. (if (equal goto '(16))
  10482. (org-refile-goto-last-stored)
  10483. (when (or
  10484. (and (equal goto 2)
  10485. org-clock-hd-marker (marker-buffer org-clock-hd-marker)
  10486. (prog1
  10487. (setq it (list (or org-clock-heading "running clock")
  10488. (buffer-file-name
  10489. (marker-buffer org-clock-hd-marker))
  10490. ""
  10491. (marker-position org-clock-hd-marker)))
  10492. (setq goto nil)))
  10493. (setq it (or rfloc
  10494. (let (heading-text)
  10495. (save-excursion
  10496. (unless (and goto (listp goto))
  10497. (org-back-to-heading t)
  10498. (setq heading-text
  10499. (nth 4 (org-heading-components))))
  10500. (org-refile-get-location
  10501. (cond ((and goto (listp goto)) "Goto")
  10502. (regionp (concat actionmsg " region to"))
  10503. (t (concat actionmsg " subtree \""
  10504. heading-text "\" to")))
  10505. default-buffer
  10506. (and (not (equal '(4) goto))
  10507. org-refile-allow-creating-parent-nodes)
  10508. goto))))))
  10509. (setq file (nth 1 it)
  10510. re (nth 2 it)
  10511. pos (nth 3 it))
  10512. (if (and (not goto)
  10513. pos
  10514. (equal (buffer-file-name) file)
  10515. (if regionp
  10516. (and (>= pos region-start)
  10517. (<= pos region-end))
  10518. (and (>= pos (point))
  10519. (< pos (save-excursion
  10520. (org-end-of-subtree t t))))))
  10521. (error "Cannot refile to position inside the tree or region"))
  10522. (setq nbuf (or (find-buffer-visiting file)
  10523. (find-file-noselect file)))
  10524. (if (and goto (not (equal goto 3)))
  10525. (progn
  10526. (org-pop-to-buffer-same-window nbuf)
  10527. (goto-char pos)
  10528. (org-show-context 'org-goto))
  10529. (if regionp
  10530. (progn
  10531. (org-kill-new (buffer-substring region-start region-end))
  10532. (org-save-markers-in-region region-start region-end))
  10533. (org-copy-subtree 1 nil t))
  10534. (with-current-buffer (setq nbuf (or (find-buffer-visiting file)
  10535. (find-file-noselect file)))
  10536. (setq reversed (org-notes-order-reversed-p))
  10537. (save-excursion
  10538. (save-restriction
  10539. (widen)
  10540. (if pos
  10541. (progn
  10542. (goto-char pos)
  10543. (looking-at org-outline-regexp)
  10544. (setq level (org-get-valid-level (funcall outline-level) 1))
  10545. (goto-char
  10546. (if reversed
  10547. (or (outline-next-heading) (point-max))
  10548. (or (save-excursion (org-get-next-sibling))
  10549. (org-end-of-subtree t t)
  10550. (point-max)))))
  10551. (setq level 1)
  10552. (if (not reversed)
  10553. (goto-char (point-max))
  10554. (goto-char (point-min))
  10555. (or (outline-next-heading) (goto-char (point-max)))))
  10556. (if (not (bolp)) (newline))
  10557. (org-paste-subtree level)
  10558. (when org-log-refile
  10559. (org-add-log-setup 'refile nil nil 'findpos org-log-refile)
  10560. (unless (eq org-log-refile 'note)
  10561. (save-excursion (org-add-log-note))))
  10562. (and org-auto-align-tags
  10563. (let ((org-loop-over-headlines-in-active-region nil))
  10564. (org-set-tags nil t)))
  10565. (let ((bookmark-name (plist-get org-bookmark-names-plist
  10566. :last-refile)))
  10567. (when bookmark-name
  10568. (with-demoted-errors
  10569. (bookmark-set bookmark-name))))
  10570. ;; If we are refiling for capture, make sure that the
  10571. ;; last-capture pointers point here
  10572. (when (org-bound-and-true-p org-refile-for-capture)
  10573. (let ((bookmark-name (plist-get org-bookmark-names-plist
  10574. :last-capture-marker)))
  10575. (when bookmark-name
  10576. (with-demoted-errors
  10577. (bookmark-set bookmark-name))))
  10578. (move-marker org-capture-last-stored-marker (point)))
  10579. (if (fboundp 'deactivate-mark) (deactivate-mark))
  10580. (run-hooks 'org-after-refile-insert-hook))))
  10581. (unless org-refile-keep
  10582. (if regionp
  10583. (delete-region (point) (+ (point) (- region-end region-start)))
  10584. (delete-region
  10585. (and (org-back-to-heading t) (point))
  10586. (min (1+ (buffer-size)) (org-end-of-subtree t t) (point)))))
  10587. (when (featurep 'org-inlinetask)
  10588. (org-inlinetask-remove-END-maybe))
  10589. (setq org-markers-to-move nil)
  10590. (message (concat actionmsg " to \"%s\" in file %s: done") (car it) file)))))))
  10591. (defun org-refile-goto-last-stored ()
  10592. "Go to the location where the last refile was stored."
  10593. (interactive)
  10594. (bookmark-jump "org-refile-last-stored")
  10595. (message "This is the location of the last refile"))
  10596. (defun org-refile--get-location (refloc tbl)
  10597. "When user refile to REFLOC, find the associated target in TBL.
  10598. Also check `org-refile-target-table'."
  10599. (car (delq
  10600. nil
  10601. (mapcar
  10602. (lambda (r) (or (assoc r tbl)
  10603. (assoc r org-refile-target-table)))
  10604. (list (replace-regexp-in-string "/$" "" refloc)
  10605. (replace-regexp-in-string "\\([^/]\\)$" "\\1/" refloc))))))
  10606. (defun org-refile-get-location (&optional prompt default-buffer new-nodes
  10607. no-exclude)
  10608. "Prompt the user for a refile location, using PROMPT.
  10609. PROMPT should not be suffixed with a colon and a space, because
  10610. this function appends the default value from
  10611. `org-refile-history' automatically, if that is not empty.
  10612. When NO-EXCLUDE is set, do not exclude headlines in the current subtree,
  10613. this is used for the GOTO interface."
  10614. (let ((org-refile-targets org-refile-targets)
  10615. (org-refile-use-outline-path org-refile-use-outline-path)
  10616. excluded-entries)
  10617. (when (and (derived-mode-p 'org-mode)
  10618. (not org-refile-use-cache)
  10619. (not no-exclude))
  10620. (org-map-tree
  10621. (lambda()
  10622. (setq excluded-entries
  10623. (append excluded-entries (list (org-get-heading t t)))))))
  10624. (setq org-refile-target-table
  10625. (org-refile-get-targets default-buffer excluded-entries)))
  10626. (unless org-refile-target-table
  10627. (user-error "No refile targets"))
  10628. (let* ((cbuf (current-buffer))
  10629. (partial-completion-mode nil)
  10630. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  10631. (cfunc (if (and org-refile-use-outline-path
  10632. org-outline-path-complete-in-steps)
  10633. 'org-olpath-completing-read
  10634. 'org-icompleting-read))
  10635. (extra (if org-refile-use-outline-path "/" ""))
  10636. (cbnex (concat (buffer-name) extra))
  10637. (filename (and cfn (expand-file-name cfn)))
  10638. (tbl (mapcar
  10639. (lambda (x)
  10640. (if (and (not (member org-refile-use-outline-path
  10641. '(file full-file-path)))
  10642. (not (equal filename (nth 1 x))))
  10643. (cons (concat (car x) extra " ("
  10644. (file-name-nondirectory (nth 1 x)) ")")
  10645. (cdr x))
  10646. (cons (concat (car x) extra) (cdr x))))
  10647. org-refile-target-table))
  10648. (completion-ignore-case t)
  10649. cdef
  10650. (prompt (concat prompt
  10651. (or (and (car org-refile-history)
  10652. (concat " (default " (car org-refile-history) ")"))
  10653. (and (assoc cbnex tbl) (setq cdef cbnex)
  10654. (concat " (default " cbnex ")"))) ": "))
  10655. pa answ parent-target child parent old-hist)
  10656. (setq old-hist org-refile-history)
  10657. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  10658. nil 'org-refile-history (or cdef (car org-refile-history))))
  10659. (if (setq pa (org-refile--get-location answ tbl))
  10660. (progn
  10661. (org-refile-check-position pa)
  10662. (when (or (not org-refile-history)
  10663. (not (eq old-hist org-refile-history))
  10664. (not (equal (car pa) (car org-refile-history))))
  10665. (setq org-refile-history
  10666. (cons (car pa) (if (assoc (car org-refile-history) tbl)
  10667. org-refile-history
  10668. (cdr org-refile-history))))
  10669. (if (equal (car org-refile-history) (nth 1 org-refile-history))
  10670. (pop org-refile-history)))
  10671. pa)
  10672. (if (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  10673. (progn
  10674. (setq parent (match-string 1 answ)
  10675. child (match-string 2 answ))
  10676. (setq parent-target (org-refile--get-location parent tbl))
  10677. (when (and parent-target
  10678. (or (eq new-nodes t)
  10679. (and (eq new-nodes 'confirm)
  10680. (y-or-n-p (format "Create new node \"%s\"? "
  10681. child)))))
  10682. (org-refile-new-child parent-target child)))
  10683. (user-error "Invalid target location")))))
  10684. (declare-function org-string-nw-p "org-macs" (s))
  10685. (defun org-refile-check-position (refile-pointer)
  10686. "Check if the refile pointer matches the headline to which it points."
  10687. (let* ((file (nth 1 refile-pointer))
  10688. (re (nth 2 refile-pointer))
  10689. (pos (nth 3 refile-pointer))
  10690. buffer)
  10691. (if (and (not (markerp pos)) (not file))
  10692. (user-error "Please save the buffer to a file before refiling")
  10693. (when (org-string-nw-p re)
  10694. (setq buffer (if (markerp pos)
  10695. (marker-buffer pos)
  10696. (or (find-buffer-visiting file)
  10697. (find-file-noselect file))))
  10698. (with-current-buffer buffer
  10699. (save-excursion
  10700. (save-restriction
  10701. (widen)
  10702. (goto-char pos)
  10703. (beginning-of-line 1)
  10704. (unless (org-looking-at-p re)
  10705. (user-error "Invalid refile position, please clear the cache with `C-0 C-c C-w' before refiling")))))))))
  10706. (defun org-refile-new-child (parent-target child)
  10707. "Use refile target PARENT-TARGET to add new CHILD below it."
  10708. (unless parent-target
  10709. (error "Cannot find parent for new node"))
  10710. (let ((file (nth 1 parent-target))
  10711. (pos (nth 3 parent-target))
  10712. level)
  10713. (with-current-buffer (or (find-buffer-visiting file)
  10714. (find-file-noselect file))
  10715. (save-excursion
  10716. (save-restriction
  10717. (widen)
  10718. (if pos
  10719. (goto-char pos)
  10720. (goto-char (point-max))
  10721. (if (not (bolp)) (newline)))
  10722. (when (looking-at org-outline-regexp)
  10723. (setq level (funcall outline-level))
  10724. (org-end-of-subtree t t))
  10725. (org-back-over-empty-lines)
  10726. (insert "\n" (make-string
  10727. (if pos (org-get-valid-level level 1) 1) ?*)
  10728. " " child "\n")
  10729. (beginning-of-line 0)
  10730. (list (concat (car parent-target) "/" child) file "" (point)))))))
  10731. (defun org-olpath-completing-read (prompt collection &rest args)
  10732. "Read an outline path like a file name."
  10733. (let ((thetable collection)
  10734. (org-completion-use-ido nil) ; does not work with ido.
  10735. (org-completion-use-iswitchb nil)) ; or iswitchb
  10736. (apply
  10737. 'org-icompleting-read prompt
  10738. (lambda (string predicate &optional flag)
  10739. (let (rtn r f (l (length string)))
  10740. (cond
  10741. ((eq flag nil)
  10742. ;; try completion
  10743. (try-completion string thetable))
  10744. ((eq flag t)
  10745. ;; all-completions
  10746. (setq rtn (all-completions string thetable predicate))
  10747. (mapcar
  10748. (lambda (x)
  10749. (setq r (substring x l))
  10750. (if (string-match " ([^)]*)$" x)
  10751. (setq f (match-string 0 x))
  10752. (setq f ""))
  10753. (if (string-match "/" r)
  10754. (concat string (substring r 0 (match-end 0)) f)
  10755. x))
  10756. rtn))
  10757. ((eq flag 'lambda)
  10758. ;; exact match?
  10759. (assoc string thetable)))))
  10760. args)))
  10761. ;;;; Dynamic blocks
  10762. (defun org-find-dblock (name)
  10763. "Find the first dynamic block with name NAME in the buffer.
  10764. If not found, stay at current position and return nil."
  10765. (let ((case-fold-search t) pos)
  10766. (save-excursion
  10767. (goto-char (point-min))
  10768. (setq pos (and (re-search-forward
  10769. (concat "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+" name "\\>") nil t)
  10770. (match-beginning 0))))
  10771. (if pos (goto-char pos))
  10772. pos))
  10773. (defconst org-dblock-start-re
  10774. "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  10775. "Matches the start line of a dynamic block, with parameters.")
  10776. (defconst org-dblock-end-re "^[ \t]*#\\+\\(?:END\\|end\\)\\([: \t\r\n]\\|$\\)"
  10777. "Matches the end of a dynamic block.")
  10778. (defun org-create-dblock (plist)
  10779. "Create a dynamic block section, with parameters taken from PLIST.
  10780. PLIST must contain a :name entry which is used as name of the block."
  10781. (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
  10782. (end-of-line 1)
  10783. (newline))
  10784. (let ((col (current-column))
  10785. (name (plist-get plist :name)))
  10786. (insert "#+BEGIN: " name)
  10787. (while plist
  10788. (if (eq (car plist) :name)
  10789. (setq plist (cddr plist))
  10790. (insert " " (prin1-to-string (pop plist)))))
  10791. (insert "\n\n" (make-string col ?\ ) "#+END:\n")
  10792. (beginning-of-line -2)))
  10793. (defun org-prepare-dblock ()
  10794. "Prepare dynamic block for refresh.
  10795. This empties the block, puts the cursor at the insert position and returns
  10796. the property list including an extra property :name with the block name."
  10797. (unless (looking-at org-dblock-start-re)
  10798. (user-error "Not at a dynamic block"))
  10799. (let* ((begdel (1+ (match-end 0)))
  10800. (name (org-no-properties (match-string 1)))
  10801. (params (append (list :name name)
  10802. (read (concat "(" (match-string 3) ")")))))
  10803. (save-excursion
  10804. (beginning-of-line 1)
  10805. (skip-chars-forward " \t")
  10806. (setq params (plist-put params :indentation-column (current-column))))
  10807. (unless (re-search-forward org-dblock-end-re nil t)
  10808. (error "Dynamic block not terminated"))
  10809. (setq params
  10810. (append params
  10811. (list :content (buffer-substring
  10812. begdel (match-beginning 0)))))
  10813. (delete-region begdel (match-beginning 0))
  10814. (goto-char begdel)
  10815. (open-line 1)
  10816. params))
  10817. (defun org-map-dblocks (&optional command)
  10818. "Apply COMMAND to all dynamic blocks in the current buffer.
  10819. If COMMAND is not given, use `org-update-dblock'."
  10820. (let ((cmd (or command 'org-update-dblock)))
  10821. (save-excursion
  10822. (goto-char (point-min))
  10823. (while (re-search-forward org-dblock-start-re nil t)
  10824. (goto-char (match-beginning 0))
  10825. (save-excursion
  10826. (condition-case nil
  10827. (funcall cmd)
  10828. (error (message "Error during update of dynamic block"))))
  10829. (unless (re-search-forward org-dblock-end-re nil t)
  10830. (error "Dynamic block not terminated"))))))
  10831. (defun org-dblock-update (&optional arg)
  10832. "User command for updating dynamic blocks.
  10833. Update the dynamic block at point. With prefix ARG, update all dynamic
  10834. blocks in the buffer."
  10835. (interactive "P")
  10836. (if arg
  10837. (org-update-all-dblocks)
  10838. (or (looking-at org-dblock-start-re)
  10839. (org-beginning-of-dblock))
  10840. (org-update-dblock)))
  10841. (defun org-update-dblock ()
  10842. "Update the dynamic block at point.
  10843. This means to empty the block, parse for parameters and then call
  10844. the correct writing function."
  10845. (interactive)
  10846. (save-excursion
  10847. (let* ((win (selected-window))
  10848. (pos (point))
  10849. (line (org-current-line))
  10850. (params (org-prepare-dblock))
  10851. (name (plist-get params :name))
  10852. (indent (plist-get params :indentation-column))
  10853. (cmd (intern (concat "org-dblock-write:" name))))
  10854. (message "Updating dynamic block `%s' at line %d..." name line)
  10855. (funcall cmd params)
  10856. (message "Updating dynamic block `%s' at line %d...done" name line)
  10857. (goto-char pos)
  10858. (when (and indent (> indent 0))
  10859. (setq indent (make-string indent ?\ ))
  10860. (save-excursion
  10861. (select-window win)
  10862. (org-beginning-of-dblock)
  10863. (forward-line 1)
  10864. (while (not (looking-at org-dblock-end-re))
  10865. (insert indent)
  10866. (beginning-of-line 2))
  10867. (when (looking-at org-dblock-end-re)
  10868. (and (looking-at "[ \t]+")
  10869. (replace-match ""))
  10870. (insert indent)))))))
  10871. (defun org-beginning-of-dblock ()
  10872. "Find the beginning of the dynamic block at point.
  10873. Error if there is no such block at point."
  10874. (let ((pos (point))
  10875. beg)
  10876. (end-of-line 1)
  10877. (if (and (re-search-backward org-dblock-start-re nil t)
  10878. (setq beg (match-beginning 0))
  10879. (re-search-forward org-dblock-end-re nil t)
  10880. (> (match-end 0) pos))
  10881. (goto-char beg)
  10882. (goto-char pos)
  10883. (error "Not in a dynamic block"))))
  10884. (defun org-update-all-dblocks ()
  10885. "Update all dynamic blocks in the buffer.
  10886. This function can be used in a hook."
  10887. (interactive)
  10888. (when (derived-mode-p 'org-mode)
  10889. (org-map-dblocks 'org-update-dblock)))
  10890. ;;;; Completion
  10891. (declare-function org-export-backend-name "org-export" (cl-x))
  10892. (declare-function org-export-backend-options "org-export" (cl-x))
  10893. (defun org-get-export-keywords ()
  10894. "Return a list of all currently understood export keywords.
  10895. Export keywords include options, block names, attributes and
  10896. keywords relative to each registered export back-end."
  10897. (let (keywords)
  10898. (dolist (backend
  10899. (org-bound-and-true-p org-export--registered-backends)
  10900. (delq nil keywords))
  10901. ;; Back-end name (for keywords, like #+LATEX:)
  10902. (push (upcase (symbol-name (org-export-backend-name backend))) keywords)
  10903. (dolist (option-entry (org-export-backend-options backend))
  10904. ;; Back-end options.
  10905. (push (nth 1 option-entry) keywords)))))
  10906. (defconst org-options-keywords
  10907. '("ARCHIVE:" "AUTHOR:" "BIND:" "CATEGORY:" "COLUMNS:" "CREATOR:" "DATE:"
  10908. "DESCRIPTION:" "DRAWERS:" "EMAIL:" "EXCLUDE_TAGS:" "FILETAGS:" "INCLUDE:"
  10909. "INDEX:" "KEYWORDS:" "LANGUAGE:" "MACRO:" "OPTIONS:" "PROPERTY:"
  10910. "PRIORITIES:" "SELECT_TAGS:" "SEQ_TODO:" "SETUPFILE:" "STARTUP:" "TAGS:"
  10911. "TITLE:" "TODO:" "TYP_TODO:" "SELECT_TAGS:" "EXCLUDE_TAGS:"))
  10912. (defcustom org-structure-template-alist
  10913. '(("s" "#+BEGIN_SRC ?\n\n#+END_SRC" "<src lang=\"?\">\n\n</src>")
  10914. ("e" "#+BEGIN_EXAMPLE\n?\n#+END_EXAMPLE" "<example>\n?\n</example>")
  10915. ("q" "#+BEGIN_QUOTE\n?\n#+END_QUOTE" "<quote>\n?\n</quote>")
  10916. ("v" "#+BEGIN_VERSE\n?\n#+END_VERSE" "<verse>\n?\n</verse>")
  10917. ("V" "#+BEGIN_VERBATIM\n?\n#+END_VERBATIM" "<verbatim>\n?\n</verbatim>")
  10918. ("c" "#+BEGIN_CENTER\n?\n#+END_CENTER" "<center>\n?\n</center>")
  10919. ("l" "#+BEGIN_LaTeX\n?\n#+END_LaTeX"
  10920. "<literal style=\"latex\">\n?\n</literal>")
  10921. ("L" "#+LaTeX: " "<literal style=\"latex\">?</literal>")
  10922. ("h" "#+BEGIN_HTML\n?\n#+END_HTML"
  10923. "<literal style=\"html\">\n?\n</literal>")
  10924. ("H" "#+HTML: " "<literal style=\"html\">?</literal>")
  10925. ("a" "#+BEGIN_ASCII\n?\n#+END_ASCII" "")
  10926. ("A" "#+ASCII: " "")
  10927. ("i" "#+INDEX: ?" "#+INDEX: ?")
  10928. ("I" "#+INCLUDE: %file ?"
  10929. "<include file=%file markup=\"?\">"))
  10930. "Structure completion elements.
  10931. This is a list of abbreviation keys and values. The value gets inserted
  10932. if you type `<' followed by the key and then press the completion key,
  10933. usually `M-TAB'. %file will be replaced by a file name after prompting
  10934. for the file using completion. The cursor will be placed at the position
  10935. of the `?` in the template.
  10936. There are two templates for each key, the first uses the original Org syntax,
  10937. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  10938. the default when the /org-mtags.el/ module has been loaded. See also the
  10939. variable `org-mtags-prefer-muse-templates'."
  10940. :group 'org-completion
  10941. :type '(repeat
  10942. (list
  10943. (string :tag "Key")
  10944. (string :tag "Template")
  10945. (string :tag "Muse Template"))))
  10946. (defun org-try-structure-completion ()
  10947. "Try to complete a structure template before point.
  10948. This looks for strings like \"<e\" on an otherwise empty line and
  10949. expands them."
  10950. (let ((l (buffer-substring (point-at-bol) (point)))
  10951. a)
  10952. (when (and (looking-at "[ \t]*$")
  10953. (string-match "^[ \t]*<\\([a-zA-Z]+\\)$" l)
  10954. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  10955. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  10956. (match-beginning 1)) a)
  10957. t)))
  10958. (defun org-complete-expand-structure-template (start cell)
  10959. "Expand a structure template."
  10960. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  10961. (rpl (nth (if musep 2 1) cell))
  10962. (ind ""))
  10963. (delete-region start (point))
  10964. (when (string-match "\\`#\\+" rpl)
  10965. (cond
  10966. ((bolp))
  10967. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  10968. (setq ind (buffer-substring (point-at-bol) (point))))
  10969. (t (newline))))
  10970. (setq start (point))
  10971. (if (string-match "%file" rpl)
  10972. (setq rpl (replace-match
  10973. (concat
  10974. "\""
  10975. (save-match-data
  10976. (abbreviate-file-name (read-file-name "Include file: ")))
  10977. "\"")
  10978. t t rpl)))
  10979. (setq rpl (mapconcat 'identity (split-string rpl "\n")
  10980. (concat "\n" ind)))
  10981. (insert rpl)
  10982. (if (re-search-backward "\\?" start t) (delete-char 1))))
  10983. ;;;; TODO, DEADLINE, Comments
  10984. (defun org-toggle-comment ()
  10985. "Change the COMMENT state of an entry."
  10986. (interactive)
  10987. (save-excursion
  10988. (org-back-to-heading)
  10989. (let (case-fold-search)
  10990. (cond
  10991. ((looking-at (format org-heading-keyword-regexp-format
  10992. org-comment-string))
  10993. (goto-char (match-end 1))
  10994. (looking-at (concat " +" org-comment-string))
  10995. (replace-match "" t t)
  10996. (when (eolp) (insert " ")))
  10997. ((looking-at org-outline-regexp)
  10998. (goto-char (match-end 0))
  10999. (insert org-comment-string " "))))))
  11000. (defvar org-last-todo-state-is-todo nil
  11001. "This is non-nil when the last TODO state change led to a TODO state.
  11002. If the last change removed the TODO tag or switched to DONE, then
  11003. this is nil.")
  11004. (defvar org-setting-tags nil) ; dynamically skipped
  11005. (defvar org-todo-setup-filter-hook nil
  11006. "Hook for functions that pre-filter todo specs.
  11007. Each function takes a todo spec and returns either nil or the spec
  11008. transformed into canonical form." )
  11009. (defvar org-todo-get-default-hook nil
  11010. "Hook for functions that get a default item for todo.
  11011. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  11012. nil or a string to be used for the todo mark." )
  11013. (defvar org-agenda-headline-snapshot-before-repeat)
  11014. (defun org-current-effective-time ()
  11015. "Return current time adjusted for `org-extend-today-until' variable."
  11016. (let* ((ct (org-current-time))
  11017. (dct (decode-time ct))
  11018. (ct1
  11019. (cond
  11020. (org-use-last-clock-out-time-as-effective-time
  11021. (or (org-clock-get-last-clock-out-time) ct))
  11022. ((and org-use-effective-time (< (nth 2 dct) org-extend-today-until))
  11023. (encode-time 0 59 23 (1- (nth 3 dct)) (nth 4 dct) (nth 5 dct)))
  11024. (t ct))))
  11025. ct1))
  11026. (defun org-todo-yesterday (&optional arg)
  11027. "Like `org-todo' but the time of change will be 23:59 of yesterday."
  11028. (interactive "P")
  11029. (if (eq major-mode 'org-agenda-mode)
  11030. (apply 'org-agenda-todo-yesterday arg)
  11031. (let* ((hour (third (decode-time
  11032. (org-current-time))))
  11033. (org-extend-today-until (1+ hour)))
  11034. (org-todo arg))))
  11035. (defvar org-block-entry-blocking ""
  11036. "First entry preventing the TODO state change.")
  11037. (defun org-todo (&optional arg)
  11038. "Change the TODO state of an item.
  11039. The state of an item is given by a keyword at the start of the heading,
  11040. like
  11041. *** TODO Write paper
  11042. *** DONE Call mom
  11043. The different keywords are specified in the variable `org-todo-keywords'.
  11044. By default the available states are \"TODO\" and \"DONE\".
  11045. So for this example: when the item starts with TODO, it is changed to DONE.
  11046. When it starts with DONE, the DONE is removed. And when neither TODO nor
  11047. DONE are present, add TODO at the beginning of the heading.
  11048. With \\[universal-argument] prefix arg, use completion to determine the new \
  11049. state.
  11050. With numeric prefix arg, switch to that state.
  11051. With a double \\[universal-argument] prefix, switch to the next set of TODO \
  11052. keywords (nextset).
  11053. With a triple \\[universal-argument] prefix, circumvent any state blocking.
  11054. With a numeric prefix arg of 0, inhibit note taking for the change.
  11055. For calling through lisp, arg is also interpreted in the following way:
  11056. 'none -> empty state
  11057. \"\"(empty string) -> switch to empty state
  11058. 'done -> switch to DONE
  11059. 'nextset -> switch to the next set of keywords
  11060. 'previousset -> switch to the previous set of keywords
  11061. \"WAITING\" -> switch to the specified keyword, but only if it
  11062. really is a member of `org-todo-keywords'."
  11063. (interactive "P")
  11064. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  11065. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  11066. 'region-start-level 'region))
  11067. org-loop-over-headlines-in-active-region)
  11068. (org-map-entries
  11069. `(org-todo ,arg)
  11070. org-loop-over-headlines-in-active-region
  11071. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  11072. (if (equal arg '(16)) (setq arg 'nextset))
  11073. (let ((org-blocker-hook org-blocker-hook)
  11074. commentp
  11075. case-fold-search)
  11076. (when (equal arg '(64))
  11077. (setq arg nil org-blocker-hook nil))
  11078. (when (and org-blocker-hook
  11079. (or org-inhibit-blocking
  11080. (org-entry-get nil "NOBLOCKING")))
  11081. (setq org-blocker-hook nil))
  11082. (save-excursion
  11083. (catch 'exit
  11084. (org-back-to-heading t)
  11085. (when (looking-at (concat "^\\*+ " org-comment-string))
  11086. (org-toggle-comment)
  11087. (setq commentp t))
  11088. (if (looking-at org-outline-regexp) (goto-char (1- (match-end 0))))
  11089. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|[ \t]*$\\)"))
  11090. (looking-at "\\(?: *\\|[ \t]*$\\)"))
  11091. (let* ((match-data (match-data))
  11092. (startpos (point-at-bol))
  11093. (logging (save-match-data (org-entry-get nil "LOGGING" t t)))
  11094. (org-log-done org-log-done)
  11095. (org-log-repeat org-log-repeat)
  11096. (org-todo-log-states org-todo-log-states)
  11097. (org-inhibit-logging
  11098. (if (equal arg 0)
  11099. (progn (setq arg nil) 'note) org-inhibit-logging))
  11100. (this (match-string 1))
  11101. (hl-pos (match-beginning 0))
  11102. (head (org-get-todo-sequence-head this))
  11103. (ass (assoc head org-todo-kwd-alist))
  11104. (interpret (nth 1 ass))
  11105. (done-word (nth 3 ass))
  11106. (final-done-word (nth 4 ass))
  11107. (org-last-state (or this ""))
  11108. (completion-ignore-case t)
  11109. (member (member this org-todo-keywords-1))
  11110. (tail (cdr member))
  11111. (org-state (cond
  11112. ((and org-todo-key-trigger
  11113. (or (and (equal arg '(4))
  11114. (eq org-use-fast-todo-selection 'prefix))
  11115. (and (not arg) org-use-fast-todo-selection
  11116. (not (eq org-use-fast-todo-selection
  11117. 'prefix)))))
  11118. ;; Use fast selection
  11119. (org-fast-todo-selection))
  11120. ((and (equal arg '(4))
  11121. (or (not org-use-fast-todo-selection)
  11122. (not org-todo-key-trigger)))
  11123. ;; Read a state with completion
  11124. (org-icompleting-read
  11125. "State: " (mapcar 'list org-todo-keywords-1)
  11126. nil t))
  11127. ((eq arg 'right)
  11128. (if this
  11129. (if tail (car tail) nil)
  11130. (car org-todo-keywords-1)))
  11131. ((eq arg 'left)
  11132. (if (equal member org-todo-keywords-1)
  11133. nil
  11134. (if this
  11135. (nth (- (length org-todo-keywords-1)
  11136. (length tail) 2)
  11137. org-todo-keywords-1)
  11138. (org-last org-todo-keywords-1))))
  11139. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  11140. (setq arg nil))) ; hack to fall back to cycling
  11141. (arg
  11142. ;; user or caller requests a specific state
  11143. (cond
  11144. ((equal arg "") nil)
  11145. ((eq arg 'none) nil)
  11146. ((eq arg 'done) (or done-word (car org-done-keywords)))
  11147. ((eq arg 'nextset)
  11148. (or (car (cdr (member head org-todo-heads)))
  11149. (car org-todo-heads)))
  11150. ((eq arg 'previousset)
  11151. (let ((org-todo-heads (reverse org-todo-heads)))
  11152. (or (car (cdr (member head org-todo-heads)))
  11153. (car org-todo-heads))))
  11154. ((car (member arg org-todo-keywords-1)))
  11155. ((stringp arg)
  11156. (user-error "State `%s' not valid in this file" arg))
  11157. ((nth (1- (prefix-numeric-value arg))
  11158. org-todo-keywords-1))))
  11159. ((null member) (or head (car org-todo-keywords-1)))
  11160. ((equal this final-done-word) nil) ;; -> make empty
  11161. ((null tail) nil) ;; -> first entry
  11162. ((memq interpret '(type priority))
  11163. (if (eq this-command last-command)
  11164. (car tail)
  11165. (if (> (length tail) 0)
  11166. (or done-word (car org-done-keywords))
  11167. nil)))
  11168. (t
  11169. (car tail))))
  11170. (org-state (or
  11171. (run-hook-with-args-until-success
  11172. 'org-todo-get-default-hook org-state org-last-state)
  11173. org-state))
  11174. (next (if org-state (concat " " org-state " ") " "))
  11175. (change-plist (list :type 'todo-state-change :from this :to org-state
  11176. :position startpos))
  11177. dolog now-done-p)
  11178. (when org-blocker-hook
  11179. (setq org-last-todo-state-is-todo
  11180. (not (member this org-done-keywords)))
  11181. (unless (save-excursion
  11182. (save-match-data
  11183. (org-with-wide-buffer
  11184. (run-hook-with-args-until-failure
  11185. 'org-blocker-hook change-plist))))
  11186. (if (org-called-interactively-p 'interactive)
  11187. (user-error "TODO state change from %s to %s blocked (by \"%s\")"
  11188. this org-state org-block-entry-blocking)
  11189. ;; fail silently
  11190. (message "TODO state change from %s to %s blocked (by \"%s\")"
  11191. this org-state org-block-entry-blocking)
  11192. (throw 'exit nil))))
  11193. (store-match-data match-data)
  11194. (replace-match next t t)
  11195. (unless (pos-visible-in-window-p hl-pos)
  11196. (message "TODO state changed to %s" (org-trim next)))
  11197. (unless head
  11198. (setq head (org-get-todo-sequence-head org-state)
  11199. ass (assoc head org-todo-kwd-alist)
  11200. interpret (nth 1 ass)
  11201. done-word (nth 3 ass)
  11202. final-done-word (nth 4 ass)))
  11203. (when (memq arg '(nextset previousset))
  11204. (message "Keyword-Set %d/%d: %s"
  11205. (- (length org-todo-sets) -1
  11206. (length (memq (assoc org-state org-todo-sets) org-todo-sets)))
  11207. (length org-todo-sets)
  11208. (mapconcat 'identity (assoc org-state org-todo-sets) " ")))
  11209. (setq org-last-todo-state-is-todo
  11210. (not (member org-state org-done-keywords)))
  11211. (setq now-done-p (and (member org-state org-done-keywords)
  11212. (not (member this org-done-keywords))))
  11213. (and logging (org-local-logging logging))
  11214. (when (and (or org-todo-log-states org-log-done)
  11215. (not (eq org-inhibit-logging t))
  11216. (not (memq arg '(nextset previousset))))
  11217. ;; we need to look at recording a time and note
  11218. (setq dolog (or (nth 1 (assoc org-state org-todo-log-states))
  11219. (nth 2 (assoc this org-todo-log-states))))
  11220. (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
  11221. (setq dolog 'time))
  11222. (when (or (and (not org-state) (not org-closed-keep-when-no-todo))
  11223. (and org-state
  11224. (member org-state org-not-done-keywords)
  11225. (not (member this org-not-done-keywords))))
  11226. ;; This is now a todo state and was not one before
  11227. ;; If there was a CLOSED time stamp, get rid of it.
  11228. (org-add-planning-info nil nil 'closed))
  11229. (when (and now-done-p org-log-done)
  11230. ;; It is now done, and it was not done before
  11231. (org-add-planning-info 'closed (org-current-effective-time))
  11232. (if (and (not dolog) (eq 'note org-log-done))
  11233. (org-add-log-setup 'done org-state this 'findpos 'note)))
  11234. (when (and org-state dolog)
  11235. ;; This is a non-nil state, and we need to log it
  11236. (org-add-log-setup 'state org-state this 'findpos dolog)))
  11237. ;; Fixup tag positioning
  11238. (org-todo-trigger-tag-changes org-state)
  11239. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  11240. (when org-provide-todo-statistics
  11241. (org-update-parent-todo-statistics))
  11242. (run-hooks 'org-after-todo-state-change-hook)
  11243. (if (and arg (not (member org-state org-done-keywords)))
  11244. (setq head (org-get-todo-sequence-head org-state)))
  11245. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  11246. ;; Do we need to trigger a repeat?
  11247. (when now-done-p
  11248. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  11249. ;; This is for the agenda, take a snapshot of the headline.
  11250. (save-match-data
  11251. (setq org-agenda-headline-snapshot-before-repeat
  11252. (org-get-heading))))
  11253. (org-auto-repeat-maybe org-state))
  11254. ;; Fixup cursor location if close to the keyword
  11255. (if (and (outline-on-heading-p)
  11256. (not (bolp))
  11257. (save-excursion (beginning-of-line 1)
  11258. (looking-at org-todo-line-regexp))
  11259. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  11260. (progn
  11261. (goto-char (or (match-end 2) (match-end 1)))
  11262. (and (looking-at " ") (just-one-space))))
  11263. (when org-trigger-hook
  11264. (save-excursion
  11265. (run-hook-with-args 'org-trigger-hook change-plist)))
  11266. (when commentp (org-toggle-comment))))))))
  11267. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  11268. "Block turning an entry into a TODO, using the hierarchy.
  11269. This checks whether the current task should be blocked from state
  11270. changes. Such blocking occurs when:
  11271. 1. The task has children which are not all in a completed state.
  11272. 2. A task has a parent with the property :ORDERED:, and there
  11273. are siblings prior to the current task with incomplete
  11274. status.
  11275. 3. The parent of the task is blocked because it has siblings that should
  11276. be done first, or is child of a block grandparent TODO entry."
  11277. (if (not org-enforce-todo-dependencies)
  11278. t ; if locally turned off don't block
  11279. (catch 'dont-block
  11280. ;; If this is not a todo state change, or if this entry is already DONE,
  11281. ;; do not block
  11282. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  11283. (member (plist-get change-plist :from)
  11284. (cons 'done org-done-keywords))
  11285. (member (plist-get change-plist :to)
  11286. (cons 'todo org-not-done-keywords))
  11287. (not (plist-get change-plist :to)))
  11288. (throw 'dont-block t))
  11289. ;; If this task has children, and any are undone, it's blocked
  11290. (save-excursion
  11291. (org-back-to-heading t)
  11292. (let ((this-level (funcall outline-level)))
  11293. (outline-next-heading)
  11294. (let ((child-level (funcall outline-level)))
  11295. (while (and (not (eobp))
  11296. (> child-level this-level))
  11297. ;; this todo has children, check whether they are all
  11298. ;; completed
  11299. (if (and (not (org-entry-is-done-p))
  11300. (org-entry-is-todo-p))
  11301. (progn (setq org-block-entry-blocking (org-get-heading))
  11302. (throw 'dont-block nil)))
  11303. (outline-next-heading)
  11304. (setq child-level (funcall outline-level))))))
  11305. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  11306. ;; any previous siblings are undone, it's blocked
  11307. (save-excursion
  11308. (org-back-to-heading t)
  11309. (let* ((pos (point))
  11310. (parent-pos (and (org-up-heading-safe) (point))))
  11311. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  11312. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  11313. (forward-line 1)
  11314. (re-search-forward org-not-done-heading-regexp pos t))
  11315. (setq org-block-entry-blocking (match-string 0))
  11316. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  11317. ;; Search further up the hierarchy, to see if an ancestor is blocked
  11318. (while t
  11319. (goto-char parent-pos)
  11320. (if (not (looking-at org-not-done-heading-regexp))
  11321. (throw 'dont-block t)) ; do not block, parent is not a TODO
  11322. (setq pos (point))
  11323. (setq parent-pos (and (org-up-heading-safe) (point)))
  11324. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  11325. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  11326. (forward-line 1)
  11327. (re-search-forward org-not-done-heading-regexp pos t)
  11328. (setq org-block-entry-blocking (org-get-heading)))
  11329. (throw 'dont-block nil)))))))) ; block, older sibling not done.
  11330. (defcustom org-track-ordered-property-with-tag nil
  11331. "Should the ORDERED property also be shown as a tag?
  11332. The ORDERED property decides if an entry should require subtasks to be
  11333. completed in sequence. Since a property is not very visible, setting
  11334. this option means that toggling the ORDERED property with the command
  11335. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  11336. not relevant for the behavior, but it makes things more visible.
  11337. Note that toggling the tag with tags commands will not change the property
  11338. and therefore not influence behavior!
  11339. This can be t, meaning the tag ORDERED should be used, It can also be a
  11340. string to select a different tag for this task."
  11341. :group 'org-todo
  11342. :type '(choice
  11343. (const :tag "No tracking" nil)
  11344. (const :tag "Track with ORDERED tag" t)
  11345. (string :tag "Use other tag")))
  11346. (defun org-toggle-ordered-property ()
  11347. "Toggle the ORDERED property of the current entry.
  11348. For better visibility, you can track the value of this property with a tag.
  11349. See variable `org-track-ordered-property-with-tag'."
  11350. (interactive)
  11351. (let* ((t1 org-track-ordered-property-with-tag)
  11352. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  11353. (save-excursion
  11354. (org-back-to-heading)
  11355. (if (org-entry-get nil "ORDERED")
  11356. (progn
  11357. (org-delete-property "ORDERED" "PROPERTIES")
  11358. (and tag (org-toggle-tag tag 'off))
  11359. (message "Subtasks can be completed in arbitrary order"))
  11360. (org-entry-put nil "ORDERED" "t")
  11361. (and tag (org-toggle-tag tag 'on))
  11362. (message "Subtasks must be completed in sequence")))))
  11363. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  11364. (defun org-block-todo-from-checkboxes (change-plist)
  11365. "Block turning an entry into a TODO, using checkboxes.
  11366. This checks whether the current task should be blocked from state
  11367. changes because there are unchecked boxes in this entry."
  11368. (if (not org-enforce-todo-checkbox-dependencies)
  11369. t ; if locally turned off don't block
  11370. (catch 'dont-block
  11371. ;; If this is not a todo state change, or if this entry is already DONE,
  11372. ;; do not block
  11373. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  11374. (member (plist-get change-plist :from)
  11375. (cons 'done org-done-keywords))
  11376. (member (plist-get change-plist :to)
  11377. (cons 'todo org-not-done-keywords))
  11378. (not (plist-get change-plist :to)))
  11379. (throw 'dont-block t))
  11380. ;; If this task has checkboxes that are not checked, it's blocked
  11381. (save-excursion
  11382. (org-back-to-heading t)
  11383. (let ((beg (point)) end)
  11384. (outline-next-heading)
  11385. (setq end (point))
  11386. (goto-char beg)
  11387. (if (org-list-search-forward
  11388. (concat (org-item-beginning-re)
  11389. "\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?"
  11390. "\\[[- ]\\]")
  11391. end t)
  11392. (progn
  11393. (if (boundp 'org-blocked-by-checkboxes)
  11394. (setq org-blocked-by-checkboxes t))
  11395. (throw 'dont-block nil)))))
  11396. t))) ; do not block
  11397. (defun org-entry-blocked-p ()
  11398. "Is the current entry blocked?"
  11399. (org-with-silent-modifications
  11400. (if (org-entry-get nil "NOBLOCKING")
  11401. nil ;; Never block this entry
  11402. (not (run-hook-with-args-until-failure
  11403. 'org-blocker-hook
  11404. (list :type 'todo-state-change
  11405. :position (point)
  11406. :from 'todo
  11407. :to 'done))))))
  11408. (defun org-update-statistics-cookies (all)
  11409. "Update the statistics cookie, either from TODO or from checkboxes.
  11410. This should be called with the cursor in a line with a statistics cookie."
  11411. (interactive "P")
  11412. (if all
  11413. (progn
  11414. (org-update-checkbox-count 'all)
  11415. (org-map-entries 'org-update-parent-todo-statistics))
  11416. (if (not (org-at-heading-p))
  11417. (org-update-checkbox-count)
  11418. (let ((pos (point-marker))
  11419. end l1 l2)
  11420. (ignore-errors (org-back-to-heading t))
  11421. (if (not (org-at-heading-p))
  11422. (org-update-checkbox-count)
  11423. (setq l1 (org-outline-level))
  11424. (setq end (save-excursion
  11425. (outline-next-heading)
  11426. (if (org-at-heading-p) (setq l2 (org-outline-level)))
  11427. (point)))
  11428. (if (and (save-excursion
  11429. (re-search-forward
  11430. "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) \\[[- X]\\]" end t))
  11431. (not (save-excursion (re-search-forward
  11432. ":COOKIE_DATA:.*\\<todo\\>" end t))))
  11433. (org-update-checkbox-count)
  11434. (if (and l2 (> l2 l1))
  11435. (progn
  11436. (goto-char end)
  11437. (org-update-parent-todo-statistics))
  11438. (goto-char pos)
  11439. (beginning-of-line 1)
  11440. (while (re-search-forward
  11441. "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)"
  11442. (point-at-eol) t)
  11443. (replace-match (if (match-end 2) "[100%]" "[0/0]") t t)))))
  11444. (goto-char pos)
  11445. (move-marker pos nil)))))
  11446. (defvar org-entry-property-inherited-from) ;; defined below
  11447. (defun org-update-parent-todo-statistics ()
  11448. "Update any statistics cookie in the parent of the current headline.
  11449. When `org-hierarchical-todo-statistics' is nil, statistics will cover
  11450. the entire subtree and this will travel up the hierarchy and update
  11451. statistics everywhere."
  11452. (let* ((prop (save-excursion (org-up-heading-safe)
  11453. (org-entry-get nil "COOKIE_DATA" 'inherit)))
  11454. (recursive (or (not org-hierarchical-todo-statistics)
  11455. (and prop (string-match "\\<recursive\\>" prop))))
  11456. (lim (or (and prop (marker-position org-entry-property-inherited-from))
  11457. 0))
  11458. (first t)
  11459. (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  11460. level ltoggle l1 new ndel
  11461. (cnt-all 0) (cnt-done 0) is-percent kwd
  11462. checkbox-beg ov ovs ove cookie-present)
  11463. (catch 'exit
  11464. (save-excursion
  11465. (beginning-of-line 1)
  11466. (setq ltoggle (funcall outline-level))
  11467. ;; Three situations are to consider:
  11468. ;; 1. if `org-hierarchical-todo-statistics' is nil, repeat up
  11469. ;; to the top-level ancestor on the headline;
  11470. ;; 2. If parent has "recursive" property, repeat up to the
  11471. ;; headline setting that property, taking inheritance into
  11472. ;; account;
  11473. ;; 3. Else, move up to direct parent and proceed only once.
  11474. (while (and (setq level (org-up-heading-safe))
  11475. (or recursive first)
  11476. (>= (point) lim))
  11477. (setq first nil cookie-present nil)
  11478. (unless (and level
  11479. (not (string-match
  11480. "\\<checkbox\\>"
  11481. (downcase (or (org-entry-get nil "COOKIE_DATA")
  11482. "")))))
  11483. (throw 'exit nil))
  11484. (while (re-search-forward box-re (point-at-eol) t)
  11485. (setq cnt-all 0 cnt-done 0 cookie-present t)
  11486. (setq is-percent (match-end 2) checkbox-beg (match-beginning 0))
  11487. (save-match-data
  11488. (unless (outline-next-heading) (throw 'exit nil))
  11489. (while (and (looking-at org-complex-heading-regexp)
  11490. (> (setq l1 (length (match-string 1))) level))
  11491. (setq kwd (and (or recursive (= l1 ltoggle))
  11492. (match-string 2)))
  11493. (if (or (eq org-provide-todo-statistics 'all-headlines)
  11494. (and (listp org-provide-todo-statistics)
  11495. (or (member kwd org-provide-todo-statistics)
  11496. (member kwd org-done-keywords))))
  11497. (setq cnt-all (1+ cnt-all))
  11498. (if (eq org-provide-todo-statistics t)
  11499. (and kwd (setq cnt-all (1+ cnt-all)))))
  11500. (and (member kwd org-done-keywords)
  11501. (setq cnt-done (1+ cnt-done)))
  11502. (outline-next-heading)))
  11503. (setq new
  11504. (if is-percent
  11505. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  11506. (format "[%d/%d]" cnt-done cnt-all))
  11507. ndel (- (match-end 0) checkbox-beg))
  11508. ;; handle overlays when updating cookie from column view
  11509. (when (setq ov (car (overlays-at checkbox-beg)))
  11510. (setq ovs (overlay-start ov) ove (overlay-end ov))
  11511. (delete-overlay ov))
  11512. (goto-char checkbox-beg)
  11513. (insert new)
  11514. (delete-region (point) (+ (point) ndel))
  11515. (when org-auto-align-tags (org-fix-tags-on-the-fly))
  11516. (when ov (move-overlay ov ovs ove)))
  11517. (when cookie-present
  11518. (run-hook-with-args 'org-after-todo-statistics-hook
  11519. cnt-done (- cnt-all cnt-done))))))
  11520. (run-hooks 'org-todo-statistics-hook)))
  11521. (defvar org-after-todo-statistics-hook nil
  11522. "Hook that is called after a TODO statistics cookie has been updated.
  11523. Each function is called with two arguments: the number of not-done entries
  11524. and the number of done entries.
  11525. For example, the following function, when added to this hook, will switch
  11526. an entry to DONE when all children are done, and back to TODO when new
  11527. entries are set to a TODO status. Note that this hook is only called
  11528. when there is a statistics cookie in the headline!
  11529. (defun org-summary-todo (n-done n-not-done)
  11530. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  11531. (let (org-log-done org-log-states) ; turn off logging
  11532. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  11533. ")
  11534. (defvar org-todo-statistics-hook nil
  11535. "Hook that is run whenever Org thinks TODO statistics should be updated.
  11536. This hook runs even if there is no statistics cookie present, in which case
  11537. `org-after-todo-statistics-hook' would not run.")
  11538. (defun org-todo-trigger-tag-changes (state)
  11539. "Apply the changes defined in `org-todo-state-tags-triggers'."
  11540. (let ((l org-todo-state-tags-triggers)
  11541. changes)
  11542. (when (or (not state) (equal state ""))
  11543. (setq changes (append changes (cdr (assoc "" l)))))
  11544. (when (and (stringp state) (> (length state) 0))
  11545. (setq changes (append changes (cdr (assoc state l)))))
  11546. (when (member state org-not-done-keywords)
  11547. (setq changes (append changes (cdr (assoc 'todo l)))))
  11548. (when (member state org-done-keywords)
  11549. (setq changes (append changes (cdr (assoc 'done l)))))
  11550. (dolist (c changes)
  11551. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  11552. (defun org-local-logging (value)
  11553. "Get logging settings from a property VALUE."
  11554. (let* (words w a)
  11555. ;; directly set the variables, they are already local.
  11556. (setq org-log-done nil
  11557. org-log-repeat nil
  11558. org-todo-log-states nil)
  11559. (setq words (org-split-string value))
  11560. (while (setq w (pop words))
  11561. (cond
  11562. ((setq a (assoc w org-startup-options))
  11563. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  11564. (set (nth 1 a) (nth 2 a))))
  11565. ((setq a (org-extract-log-state-settings w))
  11566. (and (member (car a) org-todo-keywords-1)
  11567. (push a org-todo-log-states)))))))
  11568. (defun org-get-todo-sequence-head (kwd)
  11569. "Return the head of the TODO sequence to which KWD belongs.
  11570. If KWD is not set, check if there is a text property remembering the
  11571. right sequence."
  11572. (let (p)
  11573. (cond
  11574. ((not kwd)
  11575. (or (get-text-property (point-at-bol) 'org-todo-head)
  11576. (progn
  11577. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  11578. nil (point-at-eol)))
  11579. (get-text-property p 'org-todo-head))))
  11580. ((not (member kwd org-todo-keywords-1))
  11581. (car org-todo-keywords-1))
  11582. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  11583. (defun org-fast-todo-selection ()
  11584. "Fast TODO keyword selection with single keys.
  11585. Returns the new TODO keyword, or nil if no state change should occur."
  11586. (let* ((fulltable org-todo-key-alist)
  11587. (done-keywords org-done-keywords) ;; needed for the faces.
  11588. (maxlen (apply 'max (mapcar
  11589. (lambda (x)
  11590. (if (stringp (car x)) (string-width (car x)) 0))
  11591. fulltable)))
  11592. (expert nil)
  11593. (fwidth (+ maxlen 3 1 3))
  11594. (ncol (/ (- (window-width) 4) fwidth))
  11595. tg cnt e c tbl
  11596. groups ingroup)
  11597. (save-excursion
  11598. (save-window-excursion
  11599. (if expert
  11600. (set-buffer (get-buffer-create " *Org todo*"))
  11601. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  11602. (erase-buffer)
  11603. (org-set-local 'org-done-keywords done-keywords)
  11604. (setq tbl fulltable cnt 0)
  11605. (while (setq e (pop tbl))
  11606. (cond
  11607. ((equal e '(:startgroup))
  11608. (push '() groups) (setq ingroup t)
  11609. (when (not (= cnt 0))
  11610. (setq cnt 0)
  11611. (insert "\n"))
  11612. (insert "{ "))
  11613. ((equal e '(:endgroup))
  11614. (setq ingroup nil cnt 0)
  11615. (insert "}\n"))
  11616. ((equal e '(:newline))
  11617. (when (not (= cnt 0))
  11618. (setq cnt 0)
  11619. (insert "\n")
  11620. (setq e (car tbl))
  11621. (while (equal (car tbl) '(:newline))
  11622. (insert "\n")
  11623. (setq tbl (cdr tbl)))))
  11624. (t
  11625. (setq tg (car e) c (cdr e))
  11626. (if ingroup (push tg (car groups)))
  11627. (setq tg (org-add-props tg nil 'face
  11628. (org-get-todo-face tg)))
  11629. (if (and (= cnt 0) (not ingroup)) (insert " "))
  11630. (insert "[" c "] " tg (make-string
  11631. (- fwidth 4 (length tg)) ?\ ))
  11632. (when (= (setq cnt (1+ cnt)) ncol)
  11633. (insert "\n")
  11634. (if ingroup (insert " "))
  11635. (setq cnt 0)))))
  11636. (insert "\n")
  11637. (goto-char (point-min))
  11638. (if (not expert) (org-fit-window-to-buffer))
  11639. (message "[a-z..]:Set [SPC]:clear")
  11640. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  11641. (cond
  11642. ((or (= c ?\C-g)
  11643. (and (= c ?q) (not (rassoc c fulltable))))
  11644. (setq quit-flag t))
  11645. ((= c ?\ ) nil)
  11646. ((setq e (rassoc c fulltable) tg (car e))
  11647. tg)
  11648. (t (setq quit-flag t)))))))
  11649. (defun org-entry-is-todo-p ()
  11650. (member (org-get-todo-state) org-not-done-keywords))
  11651. (defun org-entry-is-done-p ()
  11652. (member (org-get-todo-state) org-done-keywords))
  11653. (defun org-get-todo-state ()
  11654. "Return the TODO keyword of the current subtree."
  11655. (save-excursion
  11656. (org-back-to-heading t)
  11657. (and (looking-at org-todo-line-regexp)
  11658. (match-end 2)
  11659. (match-string 2))))
  11660. (defun org-at-date-range-p (&optional inactive-ok)
  11661. "Is the cursor inside a date range?"
  11662. (interactive)
  11663. (save-excursion
  11664. (catch 'exit
  11665. (let ((pos (point)))
  11666. (skip-chars-backward "^[<\r\n")
  11667. (skip-chars-backward "<[")
  11668. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  11669. (>= (match-end 0) pos)
  11670. (throw 'exit t))
  11671. (skip-chars-backward "^<[\r\n")
  11672. (skip-chars-backward "<[")
  11673. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  11674. (>= (match-end 0) pos)
  11675. (throw 'exit t)))
  11676. nil)))
  11677. (defun org-get-repeat (&optional tagline)
  11678. "Check if there is a deadline/schedule with repeater in this entry."
  11679. (save-match-data
  11680. (save-excursion
  11681. (org-back-to-heading t)
  11682. (and (re-search-forward (if tagline
  11683. (concat tagline "\\s-*" org-repeat-re)
  11684. org-repeat-re)
  11685. (org-entry-end-position) t)
  11686. (match-string-no-properties 1)))))
  11687. (defvar org-last-changed-timestamp)
  11688. (defvar org-last-inserted-timestamp)
  11689. (defvar org-log-post-message)
  11690. (defvar org-log-note-purpose)
  11691. (defvar org-log-note-how)
  11692. (defvar org-log-note-extra)
  11693. (defun org-auto-repeat-maybe (done-word)
  11694. "Check if the current headline contains a repeated deadline/schedule.
  11695. If yes, set TODO state back to what it was and change the base date
  11696. of repeating deadline/scheduled time stamps to new date.
  11697. This function is run automatically after each state change to a DONE state."
  11698. ;; last-state is dynamically scoped into this function
  11699. (let* ((repeat (org-get-repeat))
  11700. (aa (assoc org-last-state org-todo-kwd-alist))
  11701. (interpret (nth 1 aa))
  11702. (head (nth 2 aa))
  11703. (whata '(("h" . hour) ("d" . day) ("m" . month) ("y" . year)))
  11704. (msg "Entry repeats: ")
  11705. (org-log-done nil)
  11706. (org-todo-log-states nil)
  11707. re type n what ts time to-state)
  11708. (when repeat
  11709. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  11710. (setq to-state (or (org-entry-get nil "REPEAT_TO_STATE")
  11711. org-todo-repeat-to-state))
  11712. (unless (and to-state (member to-state org-todo-keywords-1))
  11713. (setq to-state (if (eq interpret 'type) org-last-state head)))
  11714. (org-todo to-state)
  11715. (when (or org-log-repeat (org-entry-get nil "CLOCK"))
  11716. (org-entry-put nil "LAST_REPEAT" (format-time-string
  11717. (org-time-stamp-format t t))))
  11718. (when org-log-repeat
  11719. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  11720. (memq 'org-add-log-note post-command-hook))
  11721. ;; OK, we are already setup for some record
  11722. (if (eq org-log-repeat 'note)
  11723. ;; make sure we take a note, not only a time stamp
  11724. (setq org-log-note-how 'note))
  11725. ;; Set up for taking a record
  11726. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  11727. org-last-state
  11728. 'findpos org-log-repeat)))
  11729. (org-back-to-heading t)
  11730. (org-add-planning-info nil nil 'closed)
  11731. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  11732. org-deadline-time-regexp "\\)\\|\\("
  11733. org-ts-regexp "\\)"))
  11734. (while (re-search-forward
  11735. re (save-excursion (outline-next-heading) (point)) t)
  11736. (setq type (if (match-end 1) org-scheduled-string
  11737. (if (match-end 3) org-deadline-string "Plain:"))
  11738. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  11739. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts)
  11740. (setq n (string-to-number (match-string 2 ts))
  11741. what (match-string 3 ts))
  11742. (if (equal what "w") (setq n (* n 7) what "d"))
  11743. (if (and (equal what "h") (not (string-match "[0-9]\\{1,2\\}:[0-9]\\{2\\}" ts)))
  11744. (user-error "Cannot repeat in Repeat in %d hour(s) because no hour has been set" n))
  11745. ;; Preparation, see if we need to modify the start date for the change
  11746. (when (match-end 1)
  11747. (setq time (save-match-data (org-time-string-to-time ts)))
  11748. (cond
  11749. ((equal (match-string 1 ts) ".")
  11750. ;; Shift starting date to today
  11751. (org-timestamp-change
  11752. (- (org-today) (time-to-days time))
  11753. 'day))
  11754. ((equal (match-string 1 ts) "+")
  11755. (let ((nshiftmax 10) (nshift 0))
  11756. (while (or (= nshift 0)
  11757. (<= (time-to-days time)
  11758. (time-to-days (current-time))))
  11759. (when (= (incf nshift) nshiftmax)
  11760. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  11761. (error "Abort")))
  11762. (org-timestamp-change n (cdr (assoc what whata)))
  11763. (org-at-timestamp-p t)
  11764. (setq ts (match-string 1))
  11765. (setq time (save-match-data (org-time-string-to-time ts)))))
  11766. (org-timestamp-change (- n) (cdr (assoc what whata)))
  11767. ;; rematch, so that we have everything in place for the real shift
  11768. (org-at-timestamp-p t)
  11769. (setq ts (match-string 1))
  11770. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts))))
  11771. (save-excursion (org-timestamp-change n (cdr (assoc what whata)) nil t))
  11772. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  11773. (setq org-log-post-message msg)
  11774. (message "%s" msg))))
  11775. (defun org-show-todo-tree (arg)
  11776. "Make a compact tree which shows all headlines marked with TODO.
  11777. The tree will show the lines where the regexp matches, and all higher
  11778. headlines above the match.
  11779. With a \\[universal-argument] prefix, prompt for a regexp to match.
  11780. With a numeric prefix N, construct a sparse tree for the Nth element
  11781. of `org-todo-keywords-1'."
  11782. (interactive "P")
  11783. (let ((case-fold-search nil)
  11784. (kwd-re
  11785. (cond ((null arg) org-not-done-regexp)
  11786. ((equal arg '(4))
  11787. (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
  11788. (mapcar 'list org-todo-keywords-1))))
  11789. (concat "\\("
  11790. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  11791. "\\)\\>")))
  11792. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  11793. (regexp-quote (nth (1- (prefix-numeric-value arg))
  11794. org-todo-keywords-1)))
  11795. (t (user-error "Invalid prefix argument: %s" arg)))))
  11796. (message "%d TODO entries found"
  11797. (org-occur (concat "^" org-outline-regexp " *" kwd-re )))))
  11798. (defun org-deadline (arg &optional time)
  11799. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  11800. With one universal prefix argument, remove any deadline from the item.
  11801. With two universal prefix arguments, prompt for a warning delay.
  11802. With argument TIME, set the deadline at the corresponding date. TIME
  11803. can either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  11804. (interactive "P")
  11805. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  11806. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  11807. 'region-start-level 'region))
  11808. org-loop-over-headlines-in-active-region)
  11809. (org-map-entries
  11810. `(org-deadline ',arg ,time)
  11811. org-loop-over-headlines-in-active-region
  11812. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  11813. (let* ((old-date (org-entry-get nil "DEADLINE"))
  11814. (old-date-time (if old-date (org-time-string-to-time old-date)))
  11815. (repeater (and old-date
  11816. (string-match
  11817. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  11818. old-date)
  11819. (match-string 1 old-date))))
  11820. (cond
  11821. ((equal arg '(4))
  11822. (when (and old-date org-log-redeadline)
  11823. (org-add-log-setup 'deldeadline nil old-date 'findpos
  11824. org-log-redeadline))
  11825. (org-remove-timestamp-with-keyword org-deadline-string)
  11826. (message "Item no longer has a deadline."))
  11827. ((equal arg '(16))
  11828. (save-excursion
  11829. (org-back-to-heading t)
  11830. (if (re-search-forward
  11831. org-deadline-time-regexp
  11832. (save-excursion (outline-next-heading) (point)) t)
  11833. (let* ((rpl0 (match-string 1))
  11834. (rpl (replace-regexp-in-string " -[0-9]+[hdwmy]" "" rpl0)))
  11835. (replace-match
  11836. (concat org-deadline-string
  11837. " <" rpl
  11838. (format " -%dd"
  11839. (abs
  11840. (- (time-to-days
  11841. (save-match-data
  11842. (org-read-date nil t nil "Warn starting from" old-date-time)))
  11843. (time-to-days old-date-time))))
  11844. ">") t t))
  11845. (user-error "No deadline information to update"))))
  11846. (t
  11847. (org-add-planning-info 'deadline time 'closed)
  11848. (when (and old-date org-log-redeadline
  11849. (not (equal old-date
  11850. (substring org-last-inserted-timestamp 1 -1))))
  11851. (org-add-log-setup 'redeadline nil old-date 'findpos
  11852. org-log-redeadline))
  11853. (when repeater
  11854. (save-excursion
  11855. (org-back-to-heading t)
  11856. (when (re-search-forward (concat org-deadline-string " "
  11857. org-last-inserted-timestamp)
  11858. (save-excursion
  11859. (outline-next-heading) (point)) t)
  11860. (goto-char (1- (match-end 0)))
  11861. (insert " " repeater)
  11862. (setq org-last-inserted-timestamp
  11863. (concat (substring org-last-inserted-timestamp 0 -1)
  11864. " " repeater
  11865. (substring org-last-inserted-timestamp -1))))))
  11866. (message "Deadline on %s" org-last-inserted-timestamp))))))
  11867. (defun org-schedule (arg &optional time)
  11868. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  11869. With one universal prefix argument, remove any scheduling date from the item.
  11870. With two universal prefix arguments, prompt for a delay cookie.
  11871. With argument TIME, scheduled at the corresponding date. TIME can
  11872. either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  11873. (interactive "P")
  11874. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  11875. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  11876. 'region-start-level 'region))
  11877. org-loop-over-headlines-in-active-region)
  11878. (org-map-entries
  11879. `(org-schedule ',arg ,time)
  11880. org-loop-over-headlines-in-active-region
  11881. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  11882. (let* ((old-date (org-entry-get nil "SCHEDULED"))
  11883. (old-date-time (if old-date (org-time-string-to-time old-date)))
  11884. (repeater (and old-date
  11885. (string-match
  11886. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  11887. old-date)
  11888. (match-string 1 old-date))))
  11889. (cond
  11890. ((equal arg '(4))
  11891. (progn
  11892. (when (and old-date org-log-reschedule)
  11893. (org-add-log-setup 'delschedule nil old-date 'findpos
  11894. org-log-reschedule))
  11895. (org-remove-timestamp-with-keyword org-scheduled-string)
  11896. (message "Item is no longer scheduled.")))
  11897. ((equal arg '(16))
  11898. (save-excursion
  11899. (org-back-to-heading t)
  11900. (if (re-search-forward
  11901. org-scheduled-time-regexp
  11902. (save-excursion (outline-next-heading) (point)) t)
  11903. (let* ((rpl0 (match-string 1))
  11904. (rpl (replace-regexp-in-string " -[0-9]+[hdwmy]" "" rpl0)))
  11905. (replace-match
  11906. (concat org-scheduled-string
  11907. " <" rpl
  11908. (format " -%dd"
  11909. (abs
  11910. (- (time-to-days
  11911. (save-match-data
  11912. (org-read-date nil t nil "Delay until" old-date-time)))
  11913. (time-to-days old-date-time))))
  11914. ">") t t))
  11915. (user-error "No scheduled information to update"))))
  11916. (t
  11917. (org-add-planning-info 'scheduled time 'closed)
  11918. (when (and old-date org-log-reschedule
  11919. (not (equal old-date
  11920. (substring org-last-inserted-timestamp 1 -1))))
  11921. (org-add-log-setup 'reschedule nil old-date 'findpos
  11922. org-log-reschedule))
  11923. (when repeater
  11924. (save-excursion
  11925. (org-back-to-heading t)
  11926. (when (re-search-forward (concat org-scheduled-string " "
  11927. org-last-inserted-timestamp)
  11928. (save-excursion
  11929. (outline-next-heading) (point)) t)
  11930. (goto-char (1- (match-end 0)))
  11931. (insert " " repeater)
  11932. (setq org-last-inserted-timestamp
  11933. (concat (substring org-last-inserted-timestamp 0 -1)
  11934. " " repeater
  11935. (substring org-last-inserted-timestamp -1))))))
  11936. (message "Scheduled to %s" org-last-inserted-timestamp))))))
  11937. (defun org-get-scheduled-time (pom &optional inherit)
  11938. "Get the scheduled time as a time tuple, of a format suitable
  11939. for calling org-schedule with, or if there is no scheduling,
  11940. returns nil."
  11941. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  11942. (when time
  11943. (apply 'encode-time (org-parse-time-string time)))))
  11944. (defun org-get-deadline-time (pom &optional inherit)
  11945. "Get the deadline as a time tuple, of a format suitable for
  11946. calling org-deadline with, or if there is no scheduling, returns
  11947. nil."
  11948. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  11949. (when time
  11950. (apply 'encode-time (org-parse-time-string time)))))
  11951. (defun org-remove-timestamp-with-keyword (keyword)
  11952. "Remove all time stamps with KEYWORD in the current entry."
  11953. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  11954. beg)
  11955. (save-excursion
  11956. (org-back-to-heading t)
  11957. (setq beg (point))
  11958. (outline-next-heading)
  11959. (while (re-search-backward re beg t)
  11960. (replace-match "")
  11961. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  11962. (equal (char-before) ?\ ))
  11963. (backward-delete-char 1)
  11964. (if (string-match "^[ \t]*$" (buffer-substring
  11965. (point-at-bol) (point-at-eol)))
  11966. (delete-region (point-at-bol)
  11967. (min (point-max) (1+ (point-at-eol))))))))))
  11968. (defvar org-time-was-given) ; dynamically scoped parameter
  11969. (defvar org-end-time-was-given) ; dynamically scoped parameter
  11970. (defun org-add-planning-info (what &optional time &rest remove)
  11971. "Insert new timestamp with keyword in the line directly after the headline.
  11972. WHAT indicates what kind of time stamp to add. TIME indicates the time to use.
  11973. If non is given, the user is prompted for a date.
  11974. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  11975. be removed."
  11976. (interactive)
  11977. (let (org-time-was-given org-end-time-was-given ts
  11978. end default-time default-input)
  11979. (catch 'exit
  11980. (when (and (memq what '(scheduled deadline))
  11981. (or (not time)
  11982. (and (stringp time)
  11983. (string-match "^[-+]+[0-9]" time))))
  11984. ;; Try to get a default date/time from existing timestamp
  11985. (save-excursion
  11986. (org-back-to-heading t)
  11987. (setq end (save-excursion (outline-next-heading) (point)))
  11988. (when (re-search-forward (if (eq what 'scheduled)
  11989. org-scheduled-time-regexp
  11990. org-deadline-time-regexp)
  11991. end t)
  11992. (setq ts (match-string 1)
  11993. default-time
  11994. (apply 'encode-time (org-parse-time-string ts))
  11995. default-input (and ts (org-get-compact-tod ts))))))
  11996. (when what
  11997. (setq time
  11998. (if (stringp time)
  11999. ;; This is a string (relative or absolute), set proper date
  12000. (apply 'encode-time
  12001. (org-read-date-analyze
  12002. time default-time (decode-time default-time)))
  12003. ;; If necessary, get the time from the user
  12004. (or time (org-read-date nil 'to-time nil nil
  12005. default-time default-input)))))
  12006. (when (and org-insert-labeled-timestamps-at-point
  12007. (member what '(scheduled deadline)))
  12008. (insert
  12009. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  12010. (org-insert-time-stamp time org-time-was-given
  12011. nil nil nil (list org-end-time-was-given))
  12012. (setq what nil))
  12013. (save-excursion
  12014. (save-restriction
  12015. (let (col list elt ts buffer-invisibility-spec)
  12016. (org-back-to-heading t)
  12017. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"))
  12018. (goto-char (match-end 1))
  12019. (setq col (current-column))
  12020. (goto-char (match-end 0))
  12021. (if (eobp) (insert "\n") (forward-char 1))
  12022. (when (and (not what)
  12023. (not (looking-at
  12024. (concat "[ \t]*"
  12025. org-keyword-time-not-clock-regexp))))
  12026. ;; Nothing to add, nothing to remove...... :-)
  12027. (throw 'exit nil))
  12028. (if (and (not (looking-at org-outline-regexp))
  12029. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  12030. "[^\r\n]*"))
  12031. (not (equal (match-string 1) org-clock-string)))
  12032. (narrow-to-region (match-beginning 0) (match-end 0))
  12033. (insert-before-markers "\n")
  12034. (backward-char 1)
  12035. (narrow-to-region (point) (point))
  12036. (and org-adapt-indentation (org-indent-to-column col)))
  12037. ;; Check if we have to remove something.
  12038. (setq list (cons what remove))
  12039. (while list
  12040. (setq elt (pop list))
  12041. (when (or (and (eq elt 'scheduled)
  12042. (re-search-forward org-scheduled-time-regexp nil t))
  12043. (and (eq elt 'deadline)
  12044. (re-search-forward org-deadline-time-regexp nil t))
  12045. (and (eq elt 'closed)
  12046. (re-search-forward org-closed-time-regexp nil t)))
  12047. (replace-match "")
  12048. (if (looking-at "--+<[^>]+>") (replace-match ""))))
  12049. (and (looking-at "[ \t]+") (replace-match ""))
  12050. (and org-adapt-indentation (bolp) (org-indent-to-column col))
  12051. (when what
  12052. (insert
  12053. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  12054. (cond ((eq what 'scheduled) org-scheduled-string)
  12055. ((eq what 'deadline) org-deadline-string)
  12056. ((eq what 'closed) org-closed-string))
  12057. " ")
  12058. (setq ts (org-insert-time-stamp
  12059. time
  12060. (or org-time-was-given
  12061. (and (eq what 'closed) org-log-done-with-time))
  12062. (eq what 'closed)
  12063. nil nil (list org-end-time-was-given)))
  12064. (insert
  12065. (if (not (or (bolp) (eq (char-before) ?\ )
  12066. (memq (char-after) '(32 10))
  12067. (eobp))) " " ""))
  12068. (end-of-line 1))
  12069. (goto-char (point-min))
  12070. (widen)
  12071. (if (and (looking-at "[ \t]*\n")
  12072. (equal (char-before) ?\n))
  12073. (delete-region (1- (point)) (point-at-eol)))
  12074. ts))))))
  12075. (defvar org-log-note-marker (make-marker))
  12076. (defvar org-log-note-purpose nil)
  12077. (defvar org-log-note-state nil)
  12078. (defvar org-log-note-previous-state nil)
  12079. (defvar org-log-note-how nil)
  12080. (defvar org-log-note-extra nil)
  12081. (defvar org-log-note-window-configuration nil)
  12082. (defvar org-log-note-return-to (make-marker))
  12083. (defvar org-log-note-effective-time nil
  12084. "Remembered current time so that dynamically scoped
  12085. `org-extend-today-until' affects tha timestamps in state change
  12086. log")
  12087. (defvar org-log-post-message nil
  12088. "Message to be displayed after a log note has been stored.
  12089. The auto-repeater uses this.")
  12090. (defun org-add-note ()
  12091. "Add a note to the current entry.
  12092. This is done in the same way as adding a state change note."
  12093. (interactive)
  12094. (org-add-log-setup 'note nil nil 'findpos nil))
  12095. (defvar org-property-end-re)
  12096. (defun org-add-log-setup (&optional purpose state prev-state
  12097. findpos how extra)
  12098. "Set up the post command hook to take a note.
  12099. If this is about to TODO state change, the new state is expected in STATE.
  12100. When FINDPOS is non-nil, find the correct position for the note in
  12101. the current entry. If not, assume that it can be inserted at point.
  12102. HOW is an indicator what kind of note should be created.
  12103. EXTRA is additional text that will be inserted into the notes buffer."
  12104. (let* ((org-log-into-drawer (org-log-into-drawer))
  12105. (drawer (cond ((stringp org-log-into-drawer)
  12106. org-log-into-drawer)
  12107. (org-log-into-drawer "LOGBOOK"))))
  12108. (save-restriction
  12109. (save-excursion
  12110. (when findpos
  12111. (org-back-to-heading t)
  12112. (narrow-to-region (point) (save-excursion
  12113. (outline-next-heading) (point)))
  12114. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"
  12115. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  12116. "[^\r\n]*\\)?"))
  12117. (goto-char (match-end 0))
  12118. (cond
  12119. (drawer
  12120. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  12121. nil t)
  12122. (progn
  12123. (goto-char (match-end 0))
  12124. (or org-log-states-order-reversed
  12125. (and (re-search-forward org-property-end-re nil t)
  12126. (goto-char (1- (match-beginning 0))))))
  12127. (insert "\n:" drawer ":\n:END:")
  12128. (beginning-of-line 0)
  12129. (org-indent-line)
  12130. (beginning-of-line 2)
  12131. (org-indent-line)
  12132. (end-of-line 0)))
  12133. ((and org-log-state-notes-insert-after-drawers
  12134. (save-excursion
  12135. (forward-line) (looking-at org-drawer-regexp)))
  12136. (forward-line)
  12137. (while (looking-at org-drawer-regexp)
  12138. (goto-char (match-end 0))
  12139. (re-search-forward org-property-end-re (point-max) t)
  12140. (forward-line))
  12141. (forward-line -1)))
  12142. (unless org-log-states-order-reversed
  12143. (and (= (char-after) ?\n) (forward-char 1))
  12144. (org-skip-over-state-notes)
  12145. (skip-chars-backward " \t\n\r")))
  12146. (move-marker org-log-note-marker (point))
  12147. (setq org-log-note-purpose purpose
  12148. org-log-note-state state
  12149. org-log-note-previous-state prev-state
  12150. org-log-note-how how
  12151. org-log-note-extra extra
  12152. org-log-note-effective-time (org-current-effective-time))
  12153. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  12154. (defun org-skip-over-state-notes ()
  12155. "Skip past the list of State notes in an entry."
  12156. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  12157. (when (ignore-errors (goto-char (org-in-item-p)))
  12158. (let* ((struct (org-list-struct))
  12159. (prevs (org-list-prevs-alist struct)))
  12160. (while (looking-at "[ \t]*- State")
  12161. (goto-char (or (org-list-get-next-item (point) struct prevs)
  12162. (org-list-get-item-end (point) struct)))))))
  12163. (defun org-add-log-note (&optional purpose)
  12164. "Pop up a window for taking a note, and add this note later at point."
  12165. (remove-hook 'post-command-hook 'org-add-log-note)
  12166. (setq org-log-note-window-configuration (current-window-configuration))
  12167. (delete-other-windows)
  12168. (move-marker org-log-note-return-to (point))
  12169. (org-pop-to-buffer-same-window (marker-buffer org-log-note-marker))
  12170. (goto-char org-log-note-marker)
  12171. (org-switch-to-buffer-other-window "*Org Note*")
  12172. (erase-buffer)
  12173. (if (memq org-log-note-how '(time state))
  12174. (let (current-prefix-arg) (org-store-log-note))
  12175. (let ((org-inhibit-startup t)) (org-mode))
  12176. (insert (format "# Insert note for %s.
  12177. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  12178. (cond
  12179. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  12180. ((eq org-log-note-purpose 'done) "closed todo item")
  12181. ((eq org-log-note-purpose 'state)
  12182. (format "state change from \"%s\" to \"%s\""
  12183. (or org-log-note-previous-state "")
  12184. (or org-log-note-state "")))
  12185. ((eq org-log-note-purpose 'reschedule)
  12186. "rescheduling")
  12187. ((eq org-log-note-purpose 'delschedule)
  12188. "no longer scheduled")
  12189. ((eq org-log-note-purpose 'redeadline)
  12190. "changing deadline")
  12191. ((eq org-log-note-purpose 'deldeadline)
  12192. "removing deadline")
  12193. ((eq org-log-note-purpose 'refile)
  12194. "refiling")
  12195. ((eq org-log-note-purpose 'note)
  12196. "this entry")
  12197. (t (error "This should not happen")))))
  12198. (if org-log-note-extra (insert org-log-note-extra))
  12199. (org-set-local 'org-finish-function 'org-store-log-note)
  12200. (run-hooks 'org-log-buffer-setup-hook)))
  12201. (defvar org-note-abort nil) ; dynamically scoped
  12202. (defun org-store-log-note ()
  12203. "Finish taking a log note, and insert it to where it belongs."
  12204. (let ((txt (buffer-string)))
  12205. (kill-buffer (current-buffer))
  12206. (let ((note (cdr (assq org-log-note-purpose org-log-note-headings)))
  12207. lines ind bul)
  12208. (while (string-match "\\`# .*\n[ \t\n]*" txt)
  12209. (setq txt (replace-match "" t t txt)))
  12210. (if (string-match "\\s-+\\'" txt)
  12211. (setq txt (replace-match "" t t txt)))
  12212. (setq lines (org-split-string txt "\n"))
  12213. (when (and note (string-match "\\S-" note))
  12214. (setq note
  12215. (org-replace-escapes
  12216. note
  12217. (list (cons "%u" (user-login-name))
  12218. (cons "%U" user-full-name)
  12219. (cons "%t" (format-time-string
  12220. (org-time-stamp-format 'long 'inactive)
  12221. org-log-note-effective-time))
  12222. (cons "%T" (format-time-string
  12223. (org-time-stamp-format 'long nil)
  12224. org-log-note-effective-time))
  12225. (cons "%d" (format-time-string
  12226. (org-time-stamp-format nil 'inactive)
  12227. org-log-note-effective-time))
  12228. (cons "%D" (format-time-string
  12229. (org-time-stamp-format nil nil)
  12230. org-log-note-effective-time))
  12231. (cons "%s" (if org-log-note-state
  12232. (concat "\"" org-log-note-state "\"")
  12233. ""))
  12234. (cons "%S" (if org-log-note-previous-state
  12235. (concat "\"" org-log-note-previous-state "\"")
  12236. "\"\"")))))
  12237. (if lines (setq note (concat note " \\\\")))
  12238. (push note lines))
  12239. (when (or current-prefix-arg org-note-abort)
  12240. (when org-log-into-drawer
  12241. (org-remove-empty-drawer-at
  12242. (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
  12243. org-log-note-marker))
  12244. (setq lines nil))
  12245. (when lines
  12246. (with-current-buffer (marker-buffer org-log-note-marker)
  12247. (save-excursion
  12248. (goto-char org-log-note-marker)
  12249. (move-marker org-log-note-marker nil)
  12250. (end-of-line 1)
  12251. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  12252. (setq ind (save-excursion
  12253. (if (ignore-errors (goto-char (org-in-item-p)))
  12254. (let ((struct (org-list-struct)))
  12255. (org-list-get-ind
  12256. (org-list-get-top-point struct) struct))
  12257. (skip-chars-backward " \r\t\n")
  12258. (cond
  12259. ((and (org-at-heading-p)
  12260. org-adapt-indentation)
  12261. (1+ (org-current-level)))
  12262. ((org-at-heading-p) 0)
  12263. (t (org-get-indentation))))))
  12264. (setq bul (org-list-bullet-string "-"))
  12265. (org-indent-line-to ind)
  12266. (insert bul (pop lines))
  12267. (let ((ind-body (+ (length bul) ind)))
  12268. (while lines
  12269. (insert "\n")
  12270. (org-indent-line-to ind-body)
  12271. (insert (pop lines))))
  12272. (message "Note stored")
  12273. (org-back-to-heading t)
  12274. (org-cycle-hide-drawers 'children))
  12275. ;; Fix `buffer-undo-list' when `org-store-log-note' is called
  12276. ;; from within `org-add-log-note' because `buffer-undo-list'
  12277. ;; is then modified outside of `org-with-remote-undo'.
  12278. (when (eq this-command 'org-agenda-todo)
  12279. (setcdr buffer-undo-list (cddr buffer-undo-list)))))))
  12280. ;; Don't add undo information when called from `org-agenda-todo'
  12281. (let ((buffer-undo-list (eq this-command 'org-agenda-todo)))
  12282. (set-window-configuration org-log-note-window-configuration)
  12283. (with-current-buffer (marker-buffer org-log-note-return-to)
  12284. (goto-char org-log-note-return-to))
  12285. (move-marker org-log-note-return-to nil)
  12286. (and org-log-post-message (message "%s" org-log-post-message))))
  12287. (defun org-remove-empty-drawer-at (drawer pos)
  12288. "Remove an empty drawer DRAWER at position POS.
  12289. POS may also be a marker."
  12290. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  12291. (save-excursion
  12292. (save-restriction
  12293. (widen)
  12294. (goto-char pos)
  12295. (if (org-in-regexp
  12296. (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
  12297. (replace-match ""))))))
  12298. (defvar org-ts-type nil)
  12299. (defun org-sparse-tree (&optional arg type)
  12300. "Create a sparse tree, prompt for the details.
  12301. This command can create sparse trees. You first need to select the type
  12302. of match used to create the tree:
  12303. t Show all TODO entries.
  12304. T Show entries with a specific TODO keyword.
  12305. m Show entries selected by a tags/property match.
  12306. p Enter a property name and its value (both with completion on existing
  12307. names/values) and show entries with that property.
  12308. r Show entries matching a regular expression (`/' can be used as well).
  12309. b Show deadlines and scheduled items before a date.
  12310. a Show deadlines and scheduled items after a date.
  12311. d Show deadlines due within `org-deadline-warning-days'.
  12312. D Show deadlines and scheduled items between a date range."
  12313. (interactive "P")
  12314. (setq type (or type org-sparse-tree-default-date-type))
  12315. (setq org-ts-type type)
  12316. (message "Sparse tree: [/]regexp [t]odo [T]odo-kwd [m]atch [p]roperty
  12317. [d]eadlines [b]efore-date [a]fter-date [D]ates range
  12318. [c]ycle through date types: %s"
  12319. (case type
  12320. (all "all timestamps")
  12321. (scheduled "only scheduled")
  12322. (deadline "only deadline")
  12323. (active "only active timestamps")
  12324. (inactive "only inactive timestamps")
  12325. (scheduled-or-deadline "scheduled/deadline")
  12326. (closed "with a closed time-stamp")
  12327. (otherwise "scheduled/deadline")))
  12328. (let ((answer (read-char-exclusive)))
  12329. (case answer
  12330. (?c
  12331. (org-sparse-tree
  12332. arg
  12333. (cadr (memq type '(scheduled-or-deadline all scheduled deadline active
  12334. inactive closed)))))
  12335. (?d (call-interactively #'org-check-deadlines))
  12336. (?b (call-interactively #'org-check-before-date))
  12337. (?a (call-interactively #'org-check-after-date))
  12338. (?D (call-interactively #'org-check-dates-range))
  12339. (?t (call-interactively #'org-show-todo-tree))
  12340. (?T (org-show-todo-tree '(4)))
  12341. (?m (call-interactively #'org-match-sparse-tree))
  12342. ((?p ?P)
  12343. (let* ((kwd (org-icompleting-read
  12344. "Property: " (mapcar #'list (org-buffer-property-keys))))
  12345. (value (org-icompleting-read
  12346. "Value: " (mapcar #'list (org-property-values kwd)))))
  12347. (unless (string-match "\\`{.*}\\'" value)
  12348. (setq value (concat "\"" value "\"")))
  12349. (org-match-sparse-tree arg (concat kwd "=" value))))
  12350. ((?r ?R ?/) (call-interactively #'org-occur))
  12351. (otherwise (user-error "No such sparse tree command \"%c\"" answer)))))
  12352. (defvar org-occur-highlights nil
  12353. "List of overlays used for occur matches.")
  12354. (make-variable-buffer-local 'org-occur-highlights)
  12355. (defvar org-occur-parameters nil
  12356. "Parameters of the active org-occur calls.
  12357. This is a list, each call to org-occur pushes as cons cell,
  12358. containing the regular expression and the callback, onto the list.
  12359. The list can contain several entries if `org-occur' has been called
  12360. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  12361. will only contain one set of parameters. When the highlights are
  12362. removed (for example with `C-c C-c', or with the next edit (depending
  12363. on `org-remove-highlights-with-change'), this variable is emptied
  12364. as well.")
  12365. (make-variable-buffer-local 'org-occur-parameters)
  12366. (defun org-occur (regexp &optional keep-previous callback)
  12367. "Make a compact tree which shows all matches of REGEXP.
  12368. The tree will show the lines where the regexp matches, and all higher
  12369. headlines above the match. It will also show the heading after the match,
  12370. to make sure editing the matching entry is easy.
  12371. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  12372. call to `org-occur' will be kept, to allow stacking of calls to this
  12373. command.
  12374. If CALLBACK is non-nil, it is a function which is called to confirm
  12375. that the match should indeed be shown."
  12376. (interactive "sRegexp: \nP")
  12377. (when (equal regexp "")
  12378. (user-error "Regexp cannot be empty"))
  12379. (unless keep-previous
  12380. (org-remove-occur-highlights nil nil t))
  12381. (push (cons regexp callback) org-occur-parameters)
  12382. (let ((cnt 0))
  12383. (save-excursion
  12384. (goto-char (point-min))
  12385. (if (or (not keep-previous) ; do not want to keep
  12386. (not org-occur-highlights)) ; no previous matches
  12387. ;; hide everything
  12388. (org-overview))
  12389. (while (re-search-forward regexp nil t)
  12390. (when (or (not callback)
  12391. (save-match-data (funcall callback)))
  12392. (setq cnt (1+ cnt))
  12393. (when org-highlight-sparse-tree-matches
  12394. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  12395. (org-show-context 'occur-tree))))
  12396. (when org-remove-highlights-with-change
  12397. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  12398. nil 'local))
  12399. (unless org-sparse-tree-open-archived-trees
  12400. (org-hide-archived-subtrees (point-min) (point-max)))
  12401. (run-hooks 'org-occur-hook)
  12402. (if (org-called-interactively-p 'interactive)
  12403. (message "%d match(es) for regexp %s" cnt regexp))
  12404. cnt))
  12405. (defun org-occur-next-match (&optional n reset)
  12406. "Function for `next-error-function' to find sparse tree matches.
  12407. N is the number of matches to move, when negative move backwards.
  12408. RESET is entirely ignored - this function always goes back to the
  12409. starting point when no match is found."
  12410. (let* ((limit (if (< n 0) (point-min) (point-max)))
  12411. (search-func (if (< n 0)
  12412. 'previous-single-char-property-change
  12413. 'next-single-char-property-change))
  12414. (n (abs n))
  12415. (pos (point))
  12416. p1)
  12417. (catch 'exit
  12418. (while (setq p1 (funcall search-func (point) 'org-type))
  12419. (when (equal p1 limit)
  12420. (goto-char pos)
  12421. (error "No more matches"))
  12422. (when (equal (get-char-property p1 'org-type) 'org-occur)
  12423. (setq n (1- n))
  12424. (when (= n 0)
  12425. (goto-char p1)
  12426. (throw 'exit (point))))
  12427. (goto-char p1))
  12428. (goto-char p1)
  12429. (error "No more matches"))))
  12430. (defun org-show-context (&optional key)
  12431. "Make sure point and context are visible.
  12432. How much context is shown depends upon the variables
  12433. `org-show-hierarchy-above', `org-show-following-heading',
  12434. `org-show-entry-below' and `org-show-siblings'."
  12435. (let ((heading-p (org-at-heading-p t))
  12436. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  12437. (following-p (org-get-alist-option org-show-following-heading key))
  12438. (entry-p (org-get-alist-option org-show-entry-below key))
  12439. (siblings-p (org-get-alist-option org-show-siblings key)))
  12440. ;; Show heading or entry text
  12441. (if (and heading-p (not entry-p))
  12442. (org-flag-heading nil) ; only show the heading
  12443. (and (or entry-p (outline-invisible-p) (org-invisible-p2))
  12444. (org-show-hidden-entry))) ; show entire entry
  12445. (when following-p
  12446. ;; Show next sibling, or heading below text
  12447. (save-excursion
  12448. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  12449. (org-flag-heading nil))))
  12450. (when siblings-p (org-show-siblings))
  12451. (when hierarchy-p
  12452. ;; show all higher headings, possibly with siblings
  12453. (save-excursion
  12454. (while (and (condition-case nil
  12455. (progn (org-up-heading-all 1) t)
  12456. (error nil))
  12457. (not (bobp)))
  12458. (org-flag-heading nil)
  12459. (when siblings-p (org-show-siblings)))))))
  12460. (defvar org-reveal-start-hook nil
  12461. "Hook run before revealing a location.")
  12462. (defun org-reveal (&optional siblings)
  12463. "Show current entry, hierarchy above it, and the following headline.
  12464. This can be used to show a consistent set of context around locations
  12465. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  12466. not t for the search context.
  12467. With optional argument SIBLINGS, on each level of the hierarchy all
  12468. siblings are shown. This repairs the tree structure to what it would
  12469. look like when opened with hierarchical calls to `org-cycle'.
  12470. With double optional argument \\[universal-argument] \\[universal-argument], \
  12471. go to the parent and show the
  12472. entire tree."
  12473. (interactive "P")
  12474. (run-hooks 'org-reveal-start-hook)
  12475. (let ((org-show-hierarchy-above t)
  12476. (org-show-following-heading t)
  12477. (org-show-siblings (if siblings t org-show-siblings)))
  12478. (org-show-context nil))
  12479. (when (equal siblings '(16))
  12480. (save-excursion
  12481. (when (org-up-heading-safe)
  12482. (org-show-subtree)
  12483. (run-hook-with-args 'org-cycle-hook 'subtree)))))
  12484. (defun org-highlight-new-match (beg end)
  12485. "Highlight from BEG to END and mark the highlight is an occur headline."
  12486. (let ((ov (make-overlay beg end)))
  12487. (overlay-put ov 'face 'secondary-selection)
  12488. (overlay-put ov 'org-type 'org-occur)
  12489. (push ov org-occur-highlights)))
  12490. (defun org-remove-occur-highlights (&optional beg end noremove)
  12491. "Remove the occur highlights from the buffer.
  12492. BEG and END are ignored. If NOREMOVE is nil, remove this function
  12493. from the `before-change-functions' in the current buffer."
  12494. (interactive)
  12495. (unless org-inhibit-highlight-removal
  12496. (mapc 'delete-overlay org-occur-highlights)
  12497. (setq org-occur-highlights nil)
  12498. (setq org-occur-parameters nil)
  12499. (unless noremove
  12500. (remove-hook 'before-change-functions
  12501. 'org-remove-occur-highlights 'local))))
  12502. ;;;; Priorities
  12503. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  12504. "Regular expression matching the priority indicator.")
  12505. (defvar org-remove-priority-next-time nil)
  12506. (defun org-priority-up ()
  12507. "Increase the priority of the current item."
  12508. (interactive)
  12509. (org-priority 'up))
  12510. (defun org-priority-down ()
  12511. "Decrease the priority of the current item."
  12512. (interactive)
  12513. (org-priority 'down))
  12514. (defun org-priority (&optional action show)
  12515. "Change the priority of an item.
  12516. ACTION can be `set', `up', `down', or a character."
  12517. (interactive "P")
  12518. (if (equal action '(4))
  12519. (org-show-priority)
  12520. (unless org-enable-priority-commands
  12521. (user-error "Priority commands are disabled"))
  12522. (setq action (or action 'set))
  12523. (let (current new news have remove)
  12524. (save-excursion
  12525. (org-back-to-heading t)
  12526. (if (looking-at org-priority-regexp)
  12527. (setq current (string-to-char (match-string 2))
  12528. have t))
  12529. (cond
  12530. ((eq action 'remove)
  12531. (setq remove t new ?\ ))
  12532. ((or (eq action 'set)
  12533. (if (featurep 'xemacs) (characterp action) (integerp action)))
  12534. (if (not (eq action 'set))
  12535. (setq new action)
  12536. (message "Priority %c-%c, SPC to remove: "
  12537. org-highest-priority org-lowest-priority)
  12538. (save-match-data
  12539. (setq new (read-char-exclusive))))
  12540. (if (and (= (upcase org-highest-priority) org-highest-priority)
  12541. (= (upcase org-lowest-priority) org-lowest-priority))
  12542. (setq new (upcase new)))
  12543. (cond ((equal new ?\ ) (setq remove t))
  12544. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  12545. (user-error "Priority must be between `%c' and `%c'"
  12546. org-highest-priority org-lowest-priority))))
  12547. ((eq action 'up)
  12548. (setq new (if have
  12549. (1- current) ; normal cycling
  12550. ;; last priority was empty
  12551. (if (eq last-command this-command)
  12552. org-lowest-priority ; wrap around empty to lowest
  12553. ;; default
  12554. (if org-priority-start-cycle-with-default
  12555. org-default-priority
  12556. (1- org-default-priority))))))
  12557. ((eq action 'down)
  12558. (setq new (if have
  12559. (1+ current) ; normal cycling
  12560. ;; last priority was empty
  12561. (if (eq last-command this-command)
  12562. org-highest-priority ; wrap around empty to highest
  12563. ;; default
  12564. (if org-priority-start-cycle-with-default
  12565. org-default-priority
  12566. (1+ org-default-priority))))))
  12567. (t (user-error "Invalid action")))
  12568. (if (or (< (upcase new) org-highest-priority)
  12569. (> (upcase new) org-lowest-priority))
  12570. (if (and (memq action '(up down))
  12571. (not have) (not (eq last-command this-command)))
  12572. ;; `new' is from default priority
  12573. (error
  12574. "The default can not be set, see `org-default-priority' why")
  12575. ;; normal cycling: `new' is beyond highest/lowest priority
  12576. ;; and is wrapped around to the empty priority
  12577. (setq remove t)))
  12578. (setq news (format "%c" new))
  12579. (if have
  12580. (if remove
  12581. (replace-match "" t t nil 1)
  12582. (replace-match news t t nil 2))
  12583. (if remove
  12584. (user-error "No priority cookie found in line")
  12585. (let ((case-fold-search nil))
  12586. (looking-at org-todo-line-regexp))
  12587. (if (match-end 2)
  12588. (progn
  12589. (goto-char (match-end 2))
  12590. (insert " [#" news "]"))
  12591. (goto-char (match-beginning 3))
  12592. (insert "[#" news "] "))))
  12593. (org-preserve-lc (org-set-tags nil 'align)))
  12594. (if remove
  12595. (message "Priority removed")
  12596. (message "Priority of current item set to %s" news)))))
  12597. (defun org-show-priority ()
  12598. "Show the priority of the current item.
  12599. This priority is composed of the main priority given with the [#A] cookies,
  12600. and by additional input from the age of a schedules or deadline entry."
  12601. (interactive)
  12602. (let ((pri (if (eq major-mode 'org-agenda-mode)
  12603. (org-get-at-bol 'priority)
  12604. (save-excursion
  12605. (save-match-data
  12606. (beginning-of-line)
  12607. (and (looking-at org-heading-regexp)
  12608. (org-get-priority (match-string 0))))))))
  12609. (message "Priority is %d" (if pri pri -1000))))
  12610. (defun org-get-priority (s)
  12611. "Find priority cookie and return priority."
  12612. (save-match-data
  12613. (if (functionp org-get-priority-function)
  12614. (funcall org-get-priority-function)
  12615. (if (not (string-match org-priority-regexp s))
  12616. (* 1000 (- org-lowest-priority org-default-priority))
  12617. (* 1000 (- org-lowest-priority
  12618. (string-to-char (match-string 2 s))))))))
  12619. ;;;; Tags
  12620. (defvar org-agenda-archives-mode)
  12621. (defvar org-map-continue-from nil
  12622. "Position from where mapping should continue.
  12623. Can be set by the action argument to `org-scan-tags' and `org-map-entries'.")
  12624. (defvar org-scanner-tags nil
  12625. "The current tag list while the tags scanner is running.")
  12626. (defvar org-trust-scanner-tags nil
  12627. "Should `org-get-tags-at' use the tags for the scanner.
  12628. This is for internal dynamical scoping only.
  12629. When this is non-nil, the function `org-get-tags-at' will return the value
  12630. of `org-scanner-tags' instead of building the list by itself. This
  12631. can lead to large speed-ups when the tags scanner is used in a file with
  12632. many entries, and when the list of tags is retrieved, for example to
  12633. obtain a list of properties. Building the tags list for each entry in such
  12634. a file becomes an N^2 operation - but with this variable set, it scales
  12635. as N.")
  12636. (defun org-scan-tags (action matcher todo-only &optional start-level)
  12637. "Scan headline tags with inheritance and produce output ACTION.
  12638. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  12639. or `agenda' to produce an entry list for an agenda view. It can also be
  12640. a Lisp form or a function that should be called at each matched headline, in
  12641. this case the return value is a list of all return values from these calls.
  12642. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  12643. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  12644. only lines with a not-done TODO keyword are included in the output.
  12645. This should be the same variable that was scoped into
  12646. and set by `org-make-tags-matcher' when it constructed MATCHER.
  12647. START-LEVEL can be a string with asterisks, reducing the scope to
  12648. headlines matching this string."
  12649. (require 'org-agenda)
  12650. (let* ((re (concat "^"
  12651. (if start-level
  12652. ;; Get the correct level to match
  12653. (concat "\\*\\{" (number-to-string start-level) "\\} ")
  12654. org-outline-regexp)
  12655. " *\\(\\<\\("
  12656. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  12657. (org-re "\\)\\>\\)? *\\(.*?\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*$")))
  12658. (props (list 'face 'default
  12659. 'done-face 'org-agenda-done
  12660. 'undone-face 'default
  12661. 'mouse-face 'highlight
  12662. 'org-not-done-regexp org-not-done-regexp
  12663. 'org-todo-regexp org-todo-regexp
  12664. 'org-complex-heading-regexp org-complex-heading-regexp
  12665. 'help-echo
  12666. (format "mouse-2 or RET jump to org file %s"
  12667. (abbreviate-file-name
  12668. (or (buffer-file-name (buffer-base-buffer))
  12669. (buffer-name (buffer-base-buffer)))))))
  12670. (org-map-continue-from nil)
  12671. lspos tags tags-list
  12672. (tags-alist (list (cons 0 org-file-tags)))
  12673. (llast 0) rtn rtn1 level category i txt
  12674. todo marker entry priority)
  12675. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  12676. (setq action (list 'lambda nil action)))
  12677. (save-excursion
  12678. (goto-char (point-min))
  12679. (when (eq action 'sparse-tree)
  12680. (org-overview)
  12681. (org-remove-occur-highlights))
  12682. (while (let (case-fold-search)
  12683. (re-search-forward re nil t))
  12684. (setq org-map-continue-from nil)
  12685. (catch :skip
  12686. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  12687. tags (if (match-end 4) (org-match-string-no-properties 4)))
  12688. (goto-char (setq lspos (match-beginning 0)))
  12689. (setq level (org-reduced-level (org-outline-level))
  12690. category (org-get-category))
  12691. (setq i llast llast level)
  12692. ;; remove tag lists from same and sublevels
  12693. (while (>= i level)
  12694. (when (setq entry (assoc i tags-alist))
  12695. (setq tags-alist (delete entry tags-alist)))
  12696. (setq i (1- i)))
  12697. ;; add the next tags
  12698. (when tags
  12699. (setq tags (org-split-string tags ":")
  12700. tags-alist
  12701. (cons (cons level tags) tags-alist)))
  12702. ;; compile tags for current headline
  12703. (setq tags-list
  12704. (if org-use-tag-inheritance
  12705. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  12706. tags)
  12707. org-scanner-tags tags-list)
  12708. (when org-use-tag-inheritance
  12709. (setcdr (car tags-alist)
  12710. (mapcar (lambda (x)
  12711. (setq x (copy-sequence x))
  12712. (org-add-prop-inherited x))
  12713. (cdar tags-alist))))
  12714. (when (and tags org-use-tag-inheritance
  12715. (or (not (eq t org-use-tag-inheritance))
  12716. org-tags-exclude-from-inheritance))
  12717. ;; selective inheritance, remove uninherited ones
  12718. (setcdr (car tags-alist)
  12719. (org-remove-uninherited-tags (cdar tags-alist))))
  12720. (when (and
  12721. ;; eval matcher only when the todo condition is OK
  12722. (and (or (not todo-only) (member todo org-not-done-keywords))
  12723. (let ((case-fold-search t) (org-trust-scanner-tags t))
  12724. (eval matcher)))
  12725. ;; Call the skipper, but return t if it does not skip,
  12726. ;; so that the `and' form continues evaluating
  12727. (progn
  12728. (unless (eq action 'sparse-tree) (org-agenda-skip))
  12729. t)
  12730. ;; Check if timestamps are deselecting this entry
  12731. (or (not todo-only)
  12732. (and (member todo org-not-done-keywords)
  12733. (or (not org-agenda-tags-todo-honor-ignore-options)
  12734. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item))))))
  12735. ;; select this headline
  12736. (cond
  12737. ((eq action 'sparse-tree)
  12738. (and org-highlight-sparse-tree-matches
  12739. (org-get-heading) (match-end 0)
  12740. (org-highlight-new-match
  12741. (match-beginning 1) (match-end 1)))
  12742. (org-show-context 'tags-tree))
  12743. ((eq action 'agenda)
  12744. (setq txt (org-agenda-format-item
  12745. ""
  12746. (concat
  12747. (if (eq org-tags-match-list-sublevels 'indented)
  12748. (make-string (1- level) ?.) "")
  12749. (org-get-heading))
  12750. level category
  12751. tags-list)
  12752. priority (org-get-priority txt))
  12753. (goto-char lspos)
  12754. (setq marker (org-agenda-new-marker))
  12755. (org-add-props txt props
  12756. 'org-marker marker 'org-hd-marker marker 'org-category category
  12757. 'todo-state todo
  12758. 'priority priority 'type "tagsmatch")
  12759. (push txt rtn))
  12760. ((functionp action)
  12761. (setq org-map-continue-from nil)
  12762. (save-excursion
  12763. (setq rtn1 (funcall action))
  12764. (push rtn1 rtn)))
  12765. (t (user-error "Invalid action")))
  12766. ;; if we are to skip sublevels, jump to end of subtree
  12767. (unless org-tags-match-list-sublevels
  12768. (org-end-of-subtree t)
  12769. (backward-char 1))))
  12770. ;; Get the correct position from where to continue
  12771. (if org-map-continue-from
  12772. (goto-char org-map-continue-from)
  12773. (and (= (point) lspos) (end-of-line 1)))))
  12774. (when (and (eq action 'sparse-tree)
  12775. (not org-sparse-tree-open-archived-trees))
  12776. (org-hide-archived-subtrees (point-min) (point-max)))
  12777. (nreverse rtn)))
  12778. (defun org-remove-uninherited-tags (tags)
  12779. "Remove all tags that are not inherited from the list TAGS."
  12780. (cond
  12781. ((eq org-use-tag-inheritance t)
  12782. (if org-tags-exclude-from-inheritance
  12783. (org-delete-all org-tags-exclude-from-inheritance tags)
  12784. tags))
  12785. ((not org-use-tag-inheritance) nil)
  12786. ((stringp org-use-tag-inheritance)
  12787. (delq nil (mapcar
  12788. (lambda (x)
  12789. (if (and (string-match org-use-tag-inheritance x)
  12790. (not (member x org-tags-exclude-from-inheritance)))
  12791. x nil))
  12792. tags)))
  12793. ((listp org-use-tag-inheritance)
  12794. (delq nil (mapcar
  12795. (lambda (x)
  12796. (if (member x org-use-tag-inheritance) x nil))
  12797. tags)))))
  12798. (defun org-match-sparse-tree (&optional todo-only match)
  12799. "Create a sparse tree according to tags string MATCH.
  12800. MATCH can contain positive and negative selection of tags, like
  12801. \"+WORK+URGENT-WITHBOSS\".
  12802. If optional argument TODO-ONLY is non-nil, only select lines that are
  12803. also TODO lines."
  12804. (interactive "P")
  12805. (org-agenda-prepare-buffers (list (current-buffer)))
  12806. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  12807. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  12808. (defvar org-cached-props nil)
  12809. (defun org-cached-entry-get (pom property)
  12810. (if (or (eq t org-use-property-inheritance)
  12811. (and (stringp org-use-property-inheritance)
  12812. (string-match org-use-property-inheritance property))
  12813. (and (listp org-use-property-inheritance)
  12814. (member property org-use-property-inheritance)))
  12815. ;; Caching is not possible, check it directly
  12816. (org-entry-get pom property 'inherit)
  12817. ;; Get all properties, so that we can do complicated checks easily
  12818. (cdr (assoc property (or org-cached-props
  12819. (setq org-cached-props
  12820. (org-entry-properties pom)))))))
  12821. (defun org-global-tags-completion-table (&optional files)
  12822. "Return the list of all tags in all agenda buffer/files.
  12823. Optional FILES argument is a list of files which can be used
  12824. instead of the agenda files."
  12825. (save-excursion
  12826. (org-uniquify
  12827. (delq nil
  12828. (apply 'append
  12829. (mapcar
  12830. (lambda (file)
  12831. (set-buffer (find-file-noselect file))
  12832. (append (org-get-buffer-tags)
  12833. (mapcar (lambda (x) (if (stringp (car-safe x))
  12834. (list (car-safe x)) nil))
  12835. org-tag-alist)))
  12836. (if (and files (car files))
  12837. files
  12838. (org-agenda-files))))))))
  12839. (defun org-make-tags-matcher (match)
  12840. "Create the TAGS/TODO matcher form for the selection string MATCH.
  12841. The variable `todo-only' is scoped dynamically into this function.
  12842. It will be set to t if the matcher restricts matching to TODO entries,
  12843. otherwise will not be touched.
  12844. Returns a cons of the selection string MATCH and the constructed
  12845. lisp form implementing the matcher. The matcher is to be evaluated
  12846. at an Org entry, with point on the headline, and returns t if the
  12847. entry matches the selection string MATCH. The returned lisp form
  12848. references two variables with information about the entry, which
  12849. must be bound around the form's evaluation: todo, the TODO keyword
  12850. at the entry (or nil of none); and tags-list, the list of all tags
  12851. at the entry including inherited ones. Additionally, the category
  12852. of the entry (if any) must be specified as the text property
  12853. 'org-category on the headline.
  12854. See also `org-scan-tags'.
  12855. "
  12856. (declare (special todo-only))
  12857. (unless (boundp 'todo-only)
  12858. (error "`org-make-tags-matcher' expects todo-only to be scoped in"))
  12859. (unless match
  12860. ;; Get a new match request, with completion against the global
  12861. ;; tags table and the local tags in current buffer
  12862. (let ((org-last-tags-completion-table
  12863. (org-uniquify
  12864. (delq nil (append (org-get-buffer-tags)
  12865. (org-global-tags-completion-table))))))
  12866. (setq match (org-completing-read-no-i
  12867. "Match: " 'org-tags-completion-function nil nil nil
  12868. 'org-tags-history))))
  12869. ;; Parse the string and create a lisp form
  12870. (let ((match0 match)
  12871. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\(\\(?:[[:alnum:]_]+\\(?:\\\\-\\)*\\)+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@#%]+\\)"))
  12872. minus tag mm
  12873. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  12874. orterms term orlist re-p str-p level-p level-op time-p
  12875. prop-p pn pv po gv rest (start 0) (ss 0))
  12876. ;; Expand group tags
  12877. (setq match (org-tags-expand match))
  12878. ;; Check if there is a TODO part of this match, which would be the
  12879. ;; part after a "/". TO make sure that this slash is not part of
  12880. ;; a property value to be matched against, we also check that there
  12881. ;; is no " after that slash.
  12882. ;; First, find the last slash
  12883. (while (string-match "/+" match ss)
  12884. (setq start (match-beginning 0) ss (match-end 0)))
  12885. (if (and (string-match "/+" match start)
  12886. (not (save-match-data (string-match "\"" match start))))
  12887. ;; match contains also a todo-matching request
  12888. (progn
  12889. (setq tagsmatch (substring match 0 (match-beginning 0))
  12890. todomatch (substring match (match-end 0)))
  12891. (if (string-match "^!" todomatch)
  12892. (setq todo-only t todomatch (substring todomatch 1)))
  12893. (if (string-match "^\\s-*$" todomatch)
  12894. (setq todomatch nil)))
  12895. ;; only matching tags
  12896. (setq tagsmatch match todomatch nil))
  12897. ;; Make the tags matcher
  12898. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  12899. (setq tagsmatcher t)
  12900. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  12901. (while (setq term (pop orterms))
  12902. (while (and (equal (substring term -1) "\\") orterms)
  12903. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  12904. (while (string-match re term)
  12905. (setq rest (substring term (match-end 0))
  12906. minus (and (match-end 1)
  12907. (equal (match-string 1 term) "-"))
  12908. tag (save-match-data (replace-regexp-in-string
  12909. "\\\\-" "-"
  12910. (match-string 2 term)))
  12911. re-p (equal (string-to-char tag) ?{)
  12912. level-p (match-end 4)
  12913. prop-p (match-end 5)
  12914. mm (cond
  12915. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  12916. (level-p
  12917. (setq level-op (org-op-to-function (match-string 3 term)))
  12918. `(,level-op level ,(string-to-number
  12919. (match-string 4 term))))
  12920. (prop-p
  12921. (setq pn (match-string 5 term)
  12922. po (match-string 6 term)
  12923. pv (match-string 7 term)
  12924. re-p (equal (string-to-char pv) ?{)
  12925. str-p (equal (string-to-char pv) ?\")
  12926. time-p (save-match-data
  12927. (string-match "^\"[[<].*[]>]\"$" pv))
  12928. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  12929. (if time-p (setq pv (org-matcher-time pv)))
  12930. (setq po (org-op-to-function po (if time-p 'time str-p)))
  12931. (cond
  12932. ((equal pn "CATEGORY")
  12933. (setq gv '(get-text-property (point) 'org-category)))
  12934. ((equal pn "TODO")
  12935. (setq gv 'todo))
  12936. (t
  12937. (setq gv `(org-cached-entry-get nil ,pn))))
  12938. (if re-p
  12939. (if (eq po 'org<>)
  12940. `(not (string-match ,pv (or ,gv "")))
  12941. `(string-match ,pv (or ,gv "")))
  12942. (if str-p
  12943. `(,po (or ,gv "") ,pv)
  12944. `(,po (string-to-number (or ,gv ""))
  12945. ,(string-to-number pv) ))))
  12946. (t `(member ,tag tags-list)))
  12947. mm (if minus (list 'not mm) mm)
  12948. term rest)
  12949. (push mm tagsmatcher))
  12950. (push (if (> (length tagsmatcher) 1)
  12951. (cons 'and tagsmatcher)
  12952. (car tagsmatcher))
  12953. orlist)
  12954. (setq tagsmatcher nil))
  12955. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  12956. (setq tagsmatcher
  12957. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  12958. ;; Make the todo matcher
  12959. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  12960. (setq todomatcher t)
  12961. (setq orterms (org-split-string todomatch "|") orlist nil)
  12962. (while (setq term (pop orterms))
  12963. (while (string-match re term)
  12964. (setq minus (and (match-end 1)
  12965. (equal (match-string 1 term) "-"))
  12966. kwd (match-string 2 term)
  12967. re-p (equal (string-to-char kwd) ?{)
  12968. term (substring term (match-end 0))
  12969. mm (if re-p
  12970. `(string-match ,(substring kwd 1 -1) todo)
  12971. (list 'equal 'todo kwd))
  12972. mm (if minus (list 'not mm) mm))
  12973. (push mm todomatcher))
  12974. (push (if (> (length todomatcher) 1)
  12975. (cons 'and todomatcher)
  12976. (car todomatcher))
  12977. orlist)
  12978. (setq todomatcher nil))
  12979. (setq todomatcher (if (> (length orlist) 1)
  12980. (cons 'or orlist) (car orlist))))
  12981. ;; Return the string and lisp forms of the matcher
  12982. (setq matcher (if todomatcher
  12983. (list 'and tagsmatcher todomatcher)
  12984. tagsmatcher))
  12985. (when todo-only
  12986. (setq matcher (list 'and '(member todo org-not-done-keywords)
  12987. matcher)))
  12988. (cons match0 matcher)))
  12989. (defun org-tags-expand (match &optional single-as-list downcased)
  12990. "Expand group tags in MATCH.
  12991. This replaces every group tag in MATCH with a regexp tag search.
  12992. For example, a group tag \"Work\" defined as { Work : Lab Conf }
  12993. will be replaced like this:
  12994. Work => {\\(?:Work\\|Lab\\|Conf\\)}
  12995. +Work => +{\\(?:Work\\|Lab\\|Conf\\)}
  12996. -Work => -{\\(?:Work\\|Lab\\|Conf\\)}
  12997. Replacing by a regexp preserves the structure of the match.
  12998. E.g., this expansion
  12999. Work|Home => {\\(?:Work\\|Lab\\|Conf\\}|Home
  13000. will match anything tagged with \"Lab\" and \"Home\", or tagged
  13001. with \"Conf\" and \"Home\" or tagged with \"Work\" and \"home\".
  13002. When the optional argument SINGLE-AS-LIST is non-nil, MATCH is
  13003. assumed to be a single group tag, and the function will return
  13004. the list of tags in this group.
  13005. When DOWNCASE is non-nil, expand downcased TAGS."
  13006. (if org-group-tags
  13007. (let* ((case-fold-search t)
  13008. (stable org-mode-syntax-table)
  13009. (tal (or org-tag-groups-alist-for-agenda
  13010. org-tag-groups-alist))
  13011. (tal (if downcased
  13012. (mapcar (lambda(tg) (mapcar 'downcase tg)) tal) tal))
  13013. (tml (mapcar 'car tal))
  13014. (rtnmatch match) rpl)
  13015. ;; @ and _ are allowed as word-components in tags
  13016. (modify-syntax-entry ?@ "w" stable)
  13017. (modify-syntax-entry ?_ "w" stable)
  13018. (while (and tml
  13019. (with-syntax-table stable
  13020. (string-match
  13021. (concat "\\(?1:[+-]?\\)\\(?2:\\<"
  13022. (regexp-opt tml) "\\>\\)") rtnmatch)))
  13023. (let* ((dir (match-string 1 rtnmatch))
  13024. (tag (match-string 2 rtnmatch))
  13025. (tag (if downcased (downcase tag) tag)))
  13026. (setq tml (delete tag tml))
  13027. (when (not (get-text-property 0 'grouptag (match-string 2 rtnmatch)))
  13028. (setq rpl (append (org-uniquify rpl) (assoc tag tal)))
  13029. (setq rpl (concat dir "{\\<" (regexp-opt rpl) "\\>}"))
  13030. (if (stringp rpl) (org-add-props rpl '(grouptag t)))
  13031. (setq rtnmatch (replace-match rpl t t rtnmatch)))))
  13032. (if single-as-list
  13033. (or (reverse rpl) (list rtnmatch))
  13034. rtnmatch))
  13035. (if single-as-list (list (if downcased (downcase match) match))
  13036. match)))
  13037. (defun org-op-to-function (op &optional stringp)
  13038. "Turn an operator into the appropriate function."
  13039. (setq op
  13040. (cond
  13041. ((equal op "<" ) '(< string< org-time<))
  13042. ((equal op ">" ) '(> org-string> org-time>))
  13043. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  13044. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  13045. ((member op '("=" "==")) '(= string= org-time=))
  13046. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  13047. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  13048. (defun org<> (a b) (not (= a b)))
  13049. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  13050. (defun org-string>= (a b) (not (string< a b)))
  13051. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  13052. (defun org-string<> (a b) (not (string= a b)))
  13053. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  13054. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  13055. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  13056. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  13057. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  13058. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  13059. (defun org-2ft (s)
  13060. "Convert S to a floating point time.
  13061. If S is already a number, just return it. If it is a string, parse
  13062. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  13063. (cond
  13064. ((numberp s) s)
  13065. ((stringp s)
  13066. (condition-case nil
  13067. (float-time (apply 'encode-time (org-parse-time-string s)))
  13068. (error 0.)))
  13069. (t 0.)))
  13070. (defun org-time-today ()
  13071. "Time in seconds today at 0:00.
  13072. Returns the float number of seconds since the beginning of the
  13073. epoch to the beginning of today (00:00)."
  13074. (float-time (apply 'encode-time
  13075. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  13076. (defun org-matcher-time (s)
  13077. "Interpret a time comparison value."
  13078. (save-match-data
  13079. (cond
  13080. ((string= s "<now>") (float-time))
  13081. ((string= s "<today>") (org-time-today))
  13082. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  13083. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  13084. ((string-match "^<\\([-+][0-9]+\\)\\([hdwmy]\\)>$" s)
  13085. (+ (org-time-today)
  13086. (* (string-to-number (match-string 1 s))
  13087. (cdr (assoc (match-string 2 s)
  13088. '(("d" . 86400.0) ("w" . 604800.0)
  13089. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  13090. (t (org-2ft s)))))
  13091. (defun org-match-any-p (re list)
  13092. "Does re match any element of list?"
  13093. (setq list (mapcar (lambda (x) (string-match re x)) list))
  13094. (delq nil list))
  13095. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  13096. (defvar org-tags-overlay (make-overlay 1 1))
  13097. (org-detach-overlay org-tags-overlay)
  13098. (defun org-get-local-tags-at (&optional pos)
  13099. "Get a list of tags defined in the current headline."
  13100. (org-get-tags-at pos 'local))
  13101. (defun org-get-local-tags ()
  13102. "Get a list of tags defined in the current headline."
  13103. (org-get-tags-at nil 'local))
  13104. (defun org-get-tags-at (&optional pos local)
  13105. "Get a list of all headline tags applicable at POS.
  13106. POS defaults to point. If tags are inherited, the list contains
  13107. the targets in the same sequence as the headlines appear, i.e.
  13108. the tags of the current headline come last.
  13109. When LOCAL is non-nil, only return tags from the current headline,
  13110. ignore inherited ones."
  13111. (interactive)
  13112. (if (and org-trust-scanner-tags
  13113. (or (not pos) (equal pos (point)))
  13114. (not local))
  13115. org-scanner-tags
  13116. (let (tags ltags lastpos parent)
  13117. (save-excursion
  13118. (save-restriction
  13119. (widen)
  13120. (goto-char (or pos (point)))
  13121. (save-match-data
  13122. (catch 'done
  13123. (condition-case nil
  13124. (progn
  13125. (org-back-to-heading t)
  13126. (while (not (equal lastpos (point)))
  13127. (setq lastpos (point))
  13128. (when (looking-at
  13129. (org-re "[^\r\n]+?:\\([[:alnum:]_@#%:]+\\):[ \t]*$"))
  13130. (setq ltags (org-split-string
  13131. (org-match-string-no-properties 1) ":"))
  13132. (when parent
  13133. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  13134. (setq tags (append
  13135. (if parent
  13136. (org-remove-uninherited-tags ltags)
  13137. ltags)
  13138. tags)))
  13139. (or org-use-tag-inheritance (throw 'done t))
  13140. (if local (throw 'done t))
  13141. (or (org-up-heading-safe) (error nil))
  13142. (setq parent t)))
  13143. (error nil)))))
  13144. (if local
  13145. tags
  13146. (reverse (delete-dups
  13147. (reverse (append
  13148. (org-remove-uninherited-tags
  13149. org-file-tags) tags)))))))))
  13150. (defun org-add-prop-inherited (s)
  13151. (add-text-properties 0 (length s) '(inherited t) s)
  13152. s)
  13153. (defun org-toggle-tag (tag &optional onoff)
  13154. "Toggle the tag TAG for the current line.
  13155. If ONOFF is `on' or `off', don't toggle but set to this state."
  13156. (let (res current)
  13157. (save-excursion
  13158. (org-back-to-heading t)
  13159. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t]*$")
  13160. (point-at-eol) t)
  13161. (progn
  13162. (setq current (match-string 1))
  13163. (replace-match ""))
  13164. (setq current ""))
  13165. (setq current (nreverse (org-split-string current ":")))
  13166. (cond
  13167. ((eq onoff 'on)
  13168. (setq res t)
  13169. (or (member tag current) (push tag current)))
  13170. ((eq onoff 'off)
  13171. (or (not (member tag current)) (setq current (delete tag current))))
  13172. (t (if (member tag current)
  13173. (setq current (delete tag current))
  13174. (setq res t)
  13175. (push tag current))))
  13176. (end-of-line 1)
  13177. (if current
  13178. (progn
  13179. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  13180. (org-set-tags nil t))
  13181. (delete-horizontal-space))
  13182. (run-hooks 'org-after-tags-change-hook))
  13183. res))
  13184. (defun org-align-tags-here (to-col)
  13185. ;; Assumes that this is a headline
  13186. "Align tags on the current headline to TO-COL."
  13187. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  13188. (beginning-of-line 1)
  13189. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13190. (< pos (match-beginning 2)))
  13191. (progn
  13192. (setq tags-l (- (match-end 2) (match-beginning 2)))
  13193. (goto-char (match-beginning 1))
  13194. (insert " ")
  13195. (delete-region (point) (1+ (match-beginning 2)))
  13196. (setq ncol (max (current-column)
  13197. (1+ col)
  13198. (if (> to-col 0)
  13199. to-col
  13200. (- (abs to-col) tags-l))))
  13201. (setq p (point))
  13202. (insert (make-string (- ncol (current-column)) ?\ ))
  13203. (setq ncol (current-column))
  13204. (when indent-tabs-mode (tabify p (point-at-eol)))
  13205. (org-move-to-column (min ncol col)))
  13206. (goto-char pos))))
  13207. (defun org-set-tags-command (&optional arg just-align)
  13208. "Call the set-tags command for the current entry."
  13209. (interactive "P")
  13210. (if (or (org-at-heading-p) (and arg (org-before-first-heading-p)))
  13211. (org-set-tags arg just-align)
  13212. (save-excursion
  13213. (unless (and (org-region-active-p)
  13214. org-loop-over-headlines-in-active-region)
  13215. (org-back-to-heading t))
  13216. (org-set-tags arg just-align))))
  13217. (defun org-set-tags-to (data)
  13218. "Set the tags of the current entry to DATA, replacing the current tags.
  13219. DATA may be a tags string like :aa:bb:cc:, or a list of tags.
  13220. If DATA is nil or the empty string, any tags will be removed."
  13221. (interactive "sTags: ")
  13222. (setq data
  13223. (cond
  13224. ((eq data nil) "")
  13225. ((equal data "") "")
  13226. ((stringp data)
  13227. (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
  13228. ":"))
  13229. ((listp data)
  13230. (concat ":" (mapconcat 'identity data ":") ":"))))
  13231. (when data
  13232. (save-excursion
  13233. (org-back-to-heading t)
  13234. (when (looking-at org-complex-heading-regexp)
  13235. (if (match-end 5)
  13236. (progn
  13237. (goto-char (match-beginning 5))
  13238. (insert data)
  13239. (delete-region (point) (point-at-eol))
  13240. (org-set-tags nil 'align))
  13241. (goto-char (point-at-eol))
  13242. (insert " " data)
  13243. (org-set-tags nil 'align)))
  13244. (beginning-of-line 1)
  13245. (if (looking-at ".*?\\([ \t]+\\)$")
  13246. (delete-region (match-beginning 1) (match-end 1))))))
  13247. (defun org-align-all-tags ()
  13248. "Align the tags i all headings."
  13249. (interactive)
  13250. (save-excursion
  13251. (or (ignore-errors (org-back-to-heading t))
  13252. (outline-next-heading))
  13253. (if (org-at-heading-p)
  13254. (org-set-tags t)
  13255. (message "No headings"))))
  13256. (defvar org-indent-indentation-per-level)
  13257. (defun org-set-tags (&optional arg just-align)
  13258. "Set the tags for the current headline.
  13259. With prefix ARG, realign all tags in headings in the current buffer.
  13260. When JUST-ALIGN is non-nil, only align tags."
  13261. (interactive "P")
  13262. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  13263. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  13264. 'region-start-level 'region))
  13265. org-loop-over-headlines-in-active-region)
  13266. (org-map-entries
  13267. ;; We don't use ARG and JUST-ALIGN here because these args
  13268. ;; are not useful when looping over headlines.
  13269. `(org-set-tags)
  13270. org-loop-over-headlines-in-active-region
  13271. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  13272. (let* ((re org-outline-regexp-bol)
  13273. (current (unless arg (org-get-tags-string)))
  13274. (col (current-column))
  13275. (org-setting-tags t)
  13276. table current-tags inherited-tags ; computed below when needed
  13277. tags p0 c0 c1 rpl di tc level)
  13278. (if arg
  13279. (save-excursion
  13280. (goto-char (point-min))
  13281. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  13282. (while (re-search-forward re nil t)
  13283. (org-set-tags nil t)
  13284. (end-of-line 1)))
  13285. (message "All tags realigned to column %d" org-tags-column))
  13286. (if just-align
  13287. (setq tags current)
  13288. ;; Get a new set of tags from the user
  13289. (save-excursion
  13290. (setq table (append org-tag-persistent-alist
  13291. (or org-tag-alist (org-get-buffer-tags))
  13292. (and
  13293. org-complete-tags-always-offer-all-agenda-tags
  13294. (org-global-tags-completion-table
  13295. (org-agenda-files))))
  13296. org-last-tags-completion-table table
  13297. current-tags (org-split-string current ":")
  13298. inherited-tags (nreverse
  13299. (nthcdr (length current-tags)
  13300. (nreverse (org-get-tags-at))))
  13301. tags
  13302. (if (or (eq t org-use-fast-tag-selection)
  13303. (and org-use-fast-tag-selection
  13304. (delq nil (mapcar 'cdr table))))
  13305. (org-fast-tag-selection
  13306. current-tags inherited-tags table
  13307. (if org-fast-tag-selection-include-todo
  13308. org-todo-key-alist))
  13309. (let ((org-add-colon-after-tag-completion (< 1 (length table))))
  13310. (org-trim
  13311. (org-icompleting-read "Tags: "
  13312. 'org-tags-completion-function
  13313. nil nil current 'org-tags-history))))))
  13314. (while (string-match "[-+&]+" tags)
  13315. ;; No boolean logic, just a list
  13316. (setq tags (replace-match ":" t t tags))))
  13317. (setq tags (replace-regexp-in-string "[,]" ":" tags))
  13318. (if org-tags-sort-function
  13319. (setq tags (mapconcat 'identity
  13320. (sort (org-split-string
  13321. tags (org-re "[^[:alnum:]_@#%]+"))
  13322. org-tags-sort-function) ":")))
  13323. (if (string-match "\\`[\t ]*\\'" tags)
  13324. (setq tags "")
  13325. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  13326. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  13327. ;; Insert new tags at the correct column
  13328. (beginning-of-line 1)
  13329. (setq level (or (and (looking-at org-outline-regexp)
  13330. (- (match-end 0) (point) 1))
  13331. 1))
  13332. (cond
  13333. ((and (equal current "") (equal tags "")))
  13334. ((re-search-forward
  13335. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  13336. (point-at-eol) t)
  13337. (if (equal tags "")
  13338. (setq rpl "")
  13339. (goto-char (match-beginning 0))
  13340. (setq c0 (current-column)
  13341. ;; compute offset for the case of org-indent-mode active
  13342. di (if (org-bound-and-true-p org-indent-mode)
  13343. (* (1- org-indent-indentation-per-level) (1- level))
  13344. 0)
  13345. p0 (if (equal (char-before) ?*) (1+ (point)) (point))
  13346. tc (+ org-tags-column (if (> org-tags-column 0) (- di) di))
  13347. c1 (max (1+ c0) (if (> tc 0) tc (- (- tc) (length tags))))
  13348. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  13349. (replace-match rpl t t)
  13350. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  13351. tags)
  13352. (t (error "Tags alignment failed")))
  13353. (org-move-to-column col)
  13354. (unless just-align
  13355. (run-hooks 'org-after-tags-change-hook))))))
  13356. (defun org-change-tag-in-region (beg end tag off)
  13357. "Add or remove TAG for each entry in the region.
  13358. This works in the agenda, and also in an org-mode buffer."
  13359. (interactive
  13360. (list (region-beginning) (region-end)
  13361. (let ((org-last-tags-completion-table
  13362. (if (derived-mode-p 'org-mode)
  13363. (org-uniquify
  13364. (delq nil (append (org-get-buffer-tags)
  13365. (org-global-tags-completion-table))))
  13366. (org-global-tags-completion-table))))
  13367. (org-icompleting-read
  13368. "Tag: " 'org-tags-completion-function nil nil nil
  13369. 'org-tags-history))
  13370. (progn
  13371. (message "[s]et or [r]emove? ")
  13372. (equal (read-char-exclusive) ?r))))
  13373. (if (fboundp 'deactivate-mark) (deactivate-mark))
  13374. (let ((agendap (equal major-mode 'org-agenda-mode))
  13375. l1 l2 m buf pos newhead (cnt 0))
  13376. (goto-char end)
  13377. (setq l2 (1- (org-current-line)))
  13378. (goto-char beg)
  13379. (setq l1 (org-current-line))
  13380. (loop for l from l1 to l2 do
  13381. (org-goto-line l)
  13382. (setq m (get-text-property (point) 'org-hd-marker))
  13383. (when (or (and (derived-mode-p 'org-mode) (org-at-heading-p))
  13384. (and agendap m))
  13385. (setq buf (if agendap (marker-buffer m) (current-buffer))
  13386. pos (if agendap m (point)))
  13387. (with-current-buffer buf
  13388. (save-excursion
  13389. (save-restriction
  13390. (goto-char pos)
  13391. (setq cnt (1+ cnt))
  13392. (org-toggle-tag tag (if off 'off 'on))
  13393. (setq newhead (org-get-heading)))))
  13394. (and agendap (org-agenda-change-all-lines newhead m))))
  13395. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  13396. (defun org-tags-completion-function (string predicate &optional flag)
  13397. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  13398. (confirm (lambda (x) (stringp (car x)))))
  13399. (if (string-match "^\\(.*[-+:&,|]\\)\\([^-+:&,|]*\\)$" string)
  13400. (setq s1 (match-string 1 string)
  13401. s2 (match-string 2 string))
  13402. (setq s1 "" s2 string))
  13403. (cond
  13404. ((eq flag nil)
  13405. ;; try completion
  13406. (setq rtn (try-completion s2 ctable confirm))
  13407. (if (stringp rtn)
  13408. (setq rtn
  13409. (concat s1 s2 (substring rtn (length s2))
  13410. (if (and org-add-colon-after-tag-completion
  13411. (assoc rtn ctable))
  13412. ":" ""))))
  13413. rtn)
  13414. ((eq flag t)
  13415. ;; all-completions
  13416. (all-completions s2 ctable confirm))
  13417. ((eq flag 'lambda)
  13418. ;; exact match?
  13419. (assoc s2 ctable)))))
  13420. (defun org-fast-tag-insert (kwd tags face &optional end)
  13421. "Insert KDW, and the TAGS, the latter with face FACE.
  13422. Also insert END."
  13423. (insert (format "%-12s" (concat kwd ":"))
  13424. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  13425. (or end "")))
  13426. (defun org-fast-tag-show-exit (flag)
  13427. (save-excursion
  13428. (org-goto-line 3)
  13429. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  13430. (replace-match ""))
  13431. (when flag
  13432. (end-of-line 1)
  13433. (org-move-to-column (- (window-width) 19) t)
  13434. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  13435. (defun org-set-current-tags-overlay (current prefix)
  13436. "Add an overlay to CURRENT tag with PREFIX."
  13437. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  13438. (if (featurep 'xemacs)
  13439. (org-overlay-display org-tags-overlay (concat prefix s)
  13440. 'secondary-selection)
  13441. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  13442. (org-overlay-display org-tags-overlay (concat prefix s)))))
  13443. (defvar org-last-tag-selection-key nil)
  13444. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  13445. "Fast tag selection with single keys.
  13446. CURRENT is the current list of tags in the headline, INHERITED is the
  13447. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  13448. possibly with grouping information. TODO-TABLE is a similar table with
  13449. TODO keywords, should these have keys assigned to them.
  13450. If the keys are nil, a-z are automatically assigned.
  13451. Returns the new tags string, or nil to not change the current settings."
  13452. (let* ((fulltable (append table todo-table))
  13453. (maxlen (apply 'max (mapcar
  13454. (lambda (x)
  13455. (if (stringp (car x)) (string-width (car x)) 0))
  13456. fulltable)))
  13457. (buf (current-buffer))
  13458. (expert (eq org-fast-tag-selection-single-key 'expert))
  13459. (buffer-tags nil)
  13460. (fwidth (+ maxlen 3 1 3))
  13461. (ncol (/ (- (window-width) 4) fwidth))
  13462. (i-face 'org-done)
  13463. (c-face 'org-todo)
  13464. tg cnt e c char c1 c2 ntable tbl rtn
  13465. ov-start ov-end ov-prefix
  13466. (exit-after-next org-fast-tag-selection-single-key)
  13467. (done-keywords org-done-keywords)
  13468. groups ingroup)
  13469. (save-excursion
  13470. (beginning-of-line 1)
  13471. (if (looking-at
  13472. (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13473. (setq ov-start (match-beginning 1)
  13474. ov-end (match-end 1)
  13475. ov-prefix "")
  13476. (setq ov-start (1- (point-at-eol))
  13477. ov-end (1+ ov-start))
  13478. (skip-chars-forward "^\n\r")
  13479. (setq ov-prefix
  13480. (concat
  13481. (buffer-substring (1- (point)) (point))
  13482. (if (> (current-column) org-tags-column)
  13483. " "
  13484. (make-string (- org-tags-column (current-column)) ?\ ))))))
  13485. (move-overlay org-tags-overlay ov-start ov-end)
  13486. (save-window-excursion
  13487. (if expert
  13488. (set-buffer (get-buffer-create " *Org tags*"))
  13489. (delete-other-windows)
  13490. (set-window-buffer (split-window-vertically) (get-buffer-create " *Org tags*"))
  13491. (org-switch-to-buffer-other-window " *Org tags*"))
  13492. (erase-buffer)
  13493. (org-set-local 'org-done-keywords done-keywords)
  13494. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  13495. (org-fast-tag-insert "Current" current c-face "\n\n")
  13496. (org-fast-tag-show-exit exit-after-next)
  13497. (org-set-current-tags-overlay current ov-prefix)
  13498. (setq tbl fulltable char ?a cnt 0)
  13499. (while (setq e (pop tbl))
  13500. (cond
  13501. ((equal (car e) :startgroup)
  13502. (push '() groups) (setq ingroup t)
  13503. (when (not (= cnt 0))
  13504. (setq cnt 0)
  13505. (insert "\n"))
  13506. (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
  13507. ((equal (car e) :endgroup)
  13508. (setq ingroup nil cnt 0)
  13509. (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
  13510. ((equal e '(:newline))
  13511. (when (not (= cnt 0))
  13512. (setq cnt 0)
  13513. (insert "\n")
  13514. (setq e (car tbl))
  13515. (while (equal (car tbl) '(:newline))
  13516. (insert "\n")
  13517. (setq tbl (cdr tbl)))))
  13518. ((equal e '(:grouptags)) nil)
  13519. (t
  13520. (setq tg (copy-sequence (car e)) c2 nil)
  13521. (if (cdr e)
  13522. (setq c (cdr e))
  13523. ;; automatically assign a character.
  13524. (setq c1 (string-to-char
  13525. (downcase (substring
  13526. tg (if (= (string-to-char tg) ?@) 1 0)))))
  13527. (if (or (rassoc c1 ntable) (rassoc c1 table))
  13528. (while (or (rassoc char ntable) (rassoc char table))
  13529. (setq char (1+ char)))
  13530. (setq c2 c1))
  13531. (setq c (or c2 char)))
  13532. (if ingroup (push tg (car groups)))
  13533. (setq tg (org-add-props tg nil 'face
  13534. (cond
  13535. ((not (assoc tg table))
  13536. (org-get-todo-face tg))
  13537. ((member tg current) c-face)
  13538. ((member tg inherited) i-face))))
  13539. (if (equal (caar tbl) :grouptags)
  13540. (org-add-props tg nil 'face 'org-tag-group))
  13541. (if (and (= cnt 0) (not ingroup)) (insert " "))
  13542. (insert "[" c "] " tg (make-string
  13543. (- fwidth 4 (length tg)) ?\ ))
  13544. (push (cons tg c) ntable)
  13545. (when (= (setq cnt (1+ cnt)) ncol)
  13546. (insert "\n")
  13547. (if ingroup (insert " "))
  13548. (setq cnt 0)))))
  13549. (setq ntable (nreverse ntable))
  13550. (insert "\n")
  13551. (goto-char (point-min))
  13552. (if (not expert) (org-fit-window-to-buffer))
  13553. (setq rtn
  13554. (catch 'exit
  13555. (while t
  13556. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
  13557. (if (not groups) "no " "")
  13558. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  13559. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  13560. (setq org-last-tag-selection-key c)
  13561. (cond
  13562. ((= c ?\r) (throw 'exit t))
  13563. ((= c ?!)
  13564. (setq groups (not groups))
  13565. (goto-char (point-min))
  13566. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  13567. ((= c ?\C-c)
  13568. (if (not expert)
  13569. (org-fast-tag-show-exit
  13570. (setq exit-after-next (not exit-after-next)))
  13571. (setq expert nil)
  13572. (delete-other-windows)
  13573. (set-window-buffer (split-window-vertically) " *Org tags*")
  13574. (org-switch-to-buffer-other-window " *Org tags*")
  13575. (org-fit-window-to-buffer)))
  13576. ((or (= c ?\C-g)
  13577. (and (= c ?q) (not (rassoc c ntable))))
  13578. (org-detach-overlay org-tags-overlay)
  13579. (setq quit-flag t))
  13580. ((= c ?\ )
  13581. (setq current nil)
  13582. (if exit-after-next (setq exit-after-next 'now)))
  13583. ((= c ?\t)
  13584. (condition-case nil
  13585. (setq tg (org-icompleting-read
  13586. "Tag: "
  13587. (or buffer-tags
  13588. (with-current-buffer buf
  13589. (org-get-buffer-tags)))))
  13590. (quit (setq tg "")))
  13591. (when (string-match "\\S-" tg)
  13592. (add-to-list 'buffer-tags (list tg))
  13593. (if (member tg current)
  13594. (setq current (delete tg current))
  13595. (push tg current)))
  13596. (if exit-after-next (setq exit-after-next 'now)))
  13597. ((setq e (rassoc c todo-table) tg (car e))
  13598. (with-current-buffer buf
  13599. (save-excursion (org-todo tg)))
  13600. (if exit-after-next (setq exit-after-next 'now)))
  13601. ((setq e (rassoc c ntable) tg (car e))
  13602. (if (member tg current)
  13603. (setq current (delete tg current))
  13604. (loop for g in groups do
  13605. (if (member tg g)
  13606. (mapc (lambda (x)
  13607. (setq current (delete x current)))
  13608. g)))
  13609. (push tg current))
  13610. (if exit-after-next (setq exit-after-next 'now))))
  13611. ;; Create a sorted list
  13612. (setq current
  13613. (sort current
  13614. (lambda (a b)
  13615. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  13616. (if (eq exit-after-next 'now) (throw 'exit t))
  13617. (goto-char (point-min))
  13618. (beginning-of-line 2)
  13619. (delete-region (point) (point-at-eol))
  13620. (org-fast-tag-insert "Current" current c-face)
  13621. (org-set-current-tags-overlay current ov-prefix)
  13622. (while (re-search-forward
  13623. (org-re "\\[.\\] \\([[:alnum:]_@#%]+\\)") nil t)
  13624. (setq tg (match-string 1))
  13625. (add-text-properties
  13626. (match-beginning 1) (match-end 1)
  13627. (list 'face
  13628. (cond
  13629. ((member tg current) c-face)
  13630. ((member tg inherited) i-face)
  13631. (t (get-text-property (match-beginning 1) 'face))))))
  13632. (goto-char (point-min)))))
  13633. (org-detach-overlay org-tags-overlay)
  13634. (if rtn
  13635. (mapconcat 'identity current ":")
  13636. nil))))
  13637. (defun org-get-tags-string ()
  13638. "Get the TAGS string in the current headline."
  13639. (unless (org-at-heading-p t)
  13640. (user-error "Not on a heading"))
  13641. (save-excursion
  13642. (beginning-of-line 1)
  13643. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13644. (org-match-string-no-properties 1)
  13645. "")))
  13646. (defun org-get-tags ()
  13647. "Get the list of tags specified in the current headline."
  13648. (org-split-string (org-get-tags-string) ":"))
  13649. (defun org-get-buffer-tags ()
  13650. "Get a table of all tags used in the buffer, for completion."
  13651. (let (tags)
  13652. (save-excursion
  13653. (goto-char (point-min))
  13654. (while (re-search-forward
  13655. (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t\r\n]") nil t)
  13656. (when (equal (char-after (point-at-bol 0)) ?*)
  13657. (mapc (lambda (x) (add-to-list 'tags x))
  13658. (org-split-string (org-match-string-no-properties 1) ":")))))
  13659. (mapc (lambda (s) (add-to-list 'tags s)) org-file-tags)
  13660. (mapcar 'list tags)))
  13661. ;;;; The mapping API
  13662. (defun org-map-entries (func &optional match scope &rest skip)
  13663. "Call FUNC at each headline selected by MATCH in SCOPE.
  13664. FUNC is a function or a lisp form. The function will be called without
  13665. arguments, with the cursor positioned at the beginning of the headline.
  13666. The return values of all calls to the function will be collected and
  13667. returned as a list.
  13668. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  13669. does not need to preserve point. After evaluation, the cursor will be
  13670. moved to the end of the line (presumably of the headline of the
  13671. processed entry) and search continues from there. Under some
  13672. circumstances, this may not produce the wanted results. For example,
  13673. if you have removed (e.g. archived) the current (sub)tree it could
  13674. mean that the next entry will be skipped entirely. In such cases, you
  13675. can specify the position from where search should continue by making
  13676. FUNC set the variable `org-map-continue-from' to the desired buffer
  13677. position.
  13678. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  13679. Only headlines that are matched by this query will be considered during
  13680. the iteration. When MATCH is nil or t, all headlines will be
  13681. visited by the iteration.
  13682. SCOPE determines the scope of this command. It can be any of:
  13683. nil The current buffer, respecting the restriction if any
  13684. tree The subtree started with the entry at point
  13685. region The entries within the active region, if any
  13686. region-start-level
  13687. The entries within the active region, but only those at
  13688. the same level than the first one.
  13689. file The current buffer, without restriction
  13690. file-with-archives
  13691. The current buffer, and any archives associated with it
  13692. agenda All agenda files
  13693. agenda-with-archives
  13694. All agenda files with any archive files associated with them
  13695. \(file1 file2 ...)
  13696. If this is a list, all files in the list will be scanned
  13697. The remaining args are treated as settings for the skipping facilities of
  13698. the scanner. The following items can be given here:
  13699. archive skip trees with the archive tag
  13700. comment skip trees with the COMMENT keyword
  13701. function or Emacs Lisp form:
  13702. will be used as value for `org-agenda-skip-function', so
  13703. whenever the function returns a position, FUNC will not be
  13704. called for that entry and search will continue from the
  13705. position returned
  13706. If your function needs to retrieve the tags including inherited tags
  13707. at the *current* entry, you can use the value of the variable
  13708. `org-scanner-tags' which will be much faster than getting the value
  13709. with `org-get-tags-at'. If your function gets properties with
  13710. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  13711. to t around the call to `org-entry-properties' to get the same speedup.
  13712. Note that if your function moves around to retrieve tags and properties at
  13713. a *different* entry, you cannot use these techniques."
  13714. (unless (and (or (eq scope 'region) (eq scope 'region-start-level))
  13715. (not (org-region-active-p)))
  13716. (let* ((org-agenda-archives-mode nil) ; just to make sure
  13717. (org-agenda-skip-archived-trees (memq 'archive skip))
  13718. (org-agenda-skip-comment-trees (memq 'comment skip))
  13719. (org-agenda-skip-function
  13720. (car (org-delete-all '(comment archive) skip)))
  13721. (org-tags-match-list-sublevels t)
  13722. (start-level (eq scope 'region-start-level))
  13723. matcher file res
  13724. org-todo-keywords-for-agenda
  13725. org-done-keywords-for-agenda
  13726. org-todo-keyword-alist-for-agenda
  13727. org-drawers-for-agenda
  13728. org-tag-alist-for-agenda
  13729. todo-only)
  13730. (cond
  13731. ((eq match t) (setq matcher t))
  13732. ((eq match nil) (setq matcher t))
  13733. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  13734. (save-excursion
  13735. (save-restriction
  13736. (cond ((eq scope 'tree)
  13737. (org-back-to-heading t)
  13738. (org-narrow-to-subtree)
  13739. (setq scope nil))
  13740. ((and (or (eq scope 'region) (eq scope 'region-start-level))
  13741. (org-region-active-p))
  13742. ;; If needed, set start-level to a string like "2"
  13743. (when start-level
  13744. (save-excursion
  13745. (goto-char (region-beginning))
  13746. (unless (org-at-heading-p) (outline-next-heading))
  13747. (setq start-level (org-current-level))))
  13748. (narrow-to-region (region-beginning)
  13749. (save-excursion
  13750. (goto-char (region-end))
  13751. (unless (and (bolp) (org-at-heading-p))
  13752. (outline-next-heading))
  13753. (point)))
  13754. (setq scope nil)))
  13755. (if (not scope)
  13756. (progn
  13757. (org-agenda-prepare-buffers
  13758. (list (buffer-file-name (current-buffer))))
  13759. (setq res (org-scan-tags func matcher todo-only start-level)))
  13760. ;; Get the right scope
  13761. (cond
  13762. ((and scope (listp scope) (symbolp (car scope)))
  13763. (setq scope (eval scope)))
  13764. ((eq scope 'agenda)
  13765. (setq scope (org-agenda-files t)))
  13766. ((eq scope 'agenda-with-archives)
  13767. (setq scope (org-agenda-files t))
  13768. (setq scope (org-add-archive-files scope)))
  13769. ((eq scope 'file)
  13770. (setq scope (list (buffer-file-name))))
  13771. ((eq scope 'file-with-archives)
  13772. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  13773. (org-agenda-prepare-buffers scope)
  13774. (while (setq file (pop scope))
  13775. (with-current-buffer (org-find-base-buffer-visiting file)
  13776. (save-excursion
  13777. (save-restriction
  13778. (widen)
  13779. (goto-char (point-min))
  13780. (setq res (append res (org-scan-tags func matcher todo-only))))))))))
  13781. res)))
  13782. ;;;; Properties
  13783. ;;; Setting and retrieving properties
  13784. (defconst org-special-properties
  13785. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  13786. "TIMESTAMP" "TIMESTAMP_IA" "BLOCKED" "FILE" "CLOCKSUM" "CLOCKSUM_T")
  13787. "The special properties valid in Org-mode.
  13788. These are properties that are not defined in the property drawer,
  13789. but in some other way.")
  13790. (defconst org-default-properties
  13791. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  13792. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  13793. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  13794. "EXPORT_OPTIONS" "EXPORT_TEXT" "EXPORT_FILE_NAME"
  13795. "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
  13796. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER" "REPEAT_TO_STATE"
  13797. "CLOCK_MODELINE_TOTAL" "STYLE" "HTML_CONTAINER_CLASS")
  13798. "Some properties that are used by Org-mode for various purposes.
  13799. Being in this list makes sure that they are offered for completion.")
  13800. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  13801. "Regular expression matching the first line of a property drawer.")
  13802. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  13803. "Regular expression matching the last line of a property drawer.")
  13804. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  13805. "Regular expression matching the first line of a property drawer.")
  13806. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  13807. "Regular expression matching the first line of a property drawer.")
  13808. (defconst org-property-drawer-re
  13809. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  13810. org-property-end-re "\\)\n?")
  13811. "Matches an entire property drawer.")
  13812. (defconst org-clock-drawer-re
  13813. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  13814. org-property-end-re "\\)\n?")
  13815. "Matches an entire clock drawer.")
  13816. (defun org-property-action ()
  13817. "Do an action on properties."
  13818. (interactive)
  13819. (let (c)
  13820. (org-at-property-p)
  13821. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  13822. (setq c (read-char-exclusive))
  13823. (cond
  13824. ((equal c ?s)
  13825. (call-interactively 'org-set-property))
  13826. ((equal c ?d)
  13827. (call-interactively 'org-delete-property))
  13828. ((equal c ?D)
  13829. (call-interactively 'org-delete-property-globally))
  13830. ((equal c ?c)
  13831. (call-interactively 'org-compute-property-at-point))
  13832. (t (user-error "No such property action %c" c)))))
  13833. (defun org-inc-effort ()
  13834. "Increment the value of the effort property in the current entry."
  13835. (interactive)
  13836. (org-set-effort nil t))
  13837. (defvar org-clock-effort) ;; Defined in org-clock.el
  13838. (defvar org-clock-current-task) ;; Defined in org-clock.el
  13839. (defun org-set-effort (&optional value increment)
  13840. "Set the effort property of the current entry.
  13841. With numerical prefix arg, use the nth allowed value, 0 stands for the
  13842. 10th allowed value.
  13843. When INCREMENT is non-nil, set the property to the next allowed value."
  13844. (interactive "P")
  13845. (if (equal value 0) (setq value 10))
  13846. (let* ((completion-ignore-case t)
  13847. (prop org-effort-property)
  13848. (cur (org-entry-get nil prop))
  13849. (allowed (org-property-get-allowed-values nil prop 'table))
  13850. (existing (mapcar 'list (org-property-values prop)))
  13851. (heading (nth 4 (org-heading-components)))
  13852. rpl
  13853. (val (cond
  13854. ((stringp value) value)
  13855. ((and allowed (integerp value))
  13856. (or (car (nth (1- value) allowed))
  13857. (car (org-last allowed))))
  13858. ((and allowed increment)
  13859. (or (caadr (member (list cur) allowed))
  13860. (user-error "Allowed effort values are not set")))
  13861. (allowed
  13862. (message "Select 1-9,0, [RET%s]: %s"
  13863. (if cur (concat "=" cur) "")
  13864. (mapconcat 'car allowed " "))
  13865. (setq rpl (read-char-exclusive))
  13866. (if (equal rpl ?\r)
  13867. cur
  13868. (setq rpl (- rpl ?0))
  13869. (if (equal rpl 0) (setq rpl 10))
  13870. (if (and (> rpl 0) (<= rpl (length allowed)))
  13871. (car (nth (1- rpl) allowed))
  13872. (org-completing-read "Effort: " allowed nil))))
  13873. (t
  13874. (let (org-completion-use-ido org-completion-use-iswitchb)
  13875. (org-completing-read
  13876. (concat "Effort " (if (and cur (string-match "\\S-" cur))
  13877. (concat "[" cur "]") "")
  13878. ": ")
  13879. existing nil nil "" nil cur))))))
  13880. (unless (equal (org-entry-get nil prop) val)
  13881. (org-entry-put nil prop val))
  13882. (save-excursion
  13883. (org-back-to-heading t)
  13884. (put-text-property (point-at-bol) (point-at-eol) 'org-effort val))
  13885. (when (string= heading org-clock-current-task)
  13886. (setq org-clock-effort (get-text-property (point-at-bol) 'org-effort))
  13887. (org-clock-update-mode-line))
  13888. (message "%s is now %s" prop val)))
  13889. (defun org-at-property-p ()
  13890. "Is cursor inside a property drawer?"
  13891. (save-excursion
  13892. (when (equal 'node-property (car (org-element-at-point)))
  13893. (beginning-of-line 1)
  13894. (looking-at org-property-re))))
  13895. (defun org-get-property-block (&optional beg end force)
  13896. "Return the (beg . end) range of the body of the property drawer.
  13897. BEG and END are the beginning and end of the current subtree, or of
  13898. the part before the first headline. If they are not given, they will
  13899. be found. If the drawer does not exist and FORCE is non-nil, create
  13900. the drawer."
  13901. (catch 'exit
  13902. (save-excursion
  13903. (let* ((beg (or beg (and (org-before-first-heading-p) (point-min))
  13904. (progn (org-back-to-heading t) (point))))
  13905. (end (or end (and (not (outline-next-heading)) (point-max))
  13906. (point))))
  13907. (goto-char beg)
  13908. (if (re-search-forward org-property-start-re end t)
  13909. (setq beg (1+ (match-end 0)))
  13910. (if force
  13911. (save-excursion
  13912. (org-insert-property-drawer)
  13913. (setq end (progn (outline-next-heading) (point))))
  13914. (throw 'exit nil))
  13915. (goto-char beg)
  13916. (if (re-search-forward org-property-start-re end t)
  13917. (setq beg (1+ (match-end 0)))))
  13918. (if (re-search-forward org-property-end-re end t)
  13919. (setq end (match-beginning 0))
  13920. (or force (throw 'exit nil))
  13921. (goto-char beg)
  13922. (setq end beg)
  13923. (org-indent-line)
  13924. (insert ":END:\n"))
  13925. (cons beg end)))))
  13926. (defun org-entry-properties (&optional pom which specific)
  13927. "Get all properties of the entry at point-or-marker POM.
  13928. This includes the TODO keyword, the tags, time strings for deadline,
  13929. scheduled, and clocking, and any additional properties defined in the
  13930. entry. The return value is an alist, keys may occur multiple times
  13931. if the property key was used several times.
  13932. POM may also be nil, in which case the current entry is used.
  13933. If WHICH is nil or `all', get all properties. If WHICH is
  13934. `special' or `standard', only get that subclass. If WHICH
  13935. is a string only get exactly this property. SPECIFIC can be a string, the
  13936. specific property we are interested in. Specifying it can speed
  13937. things up because then unnecessary parsing is avoided."
  13938. (setq which (or which 'all))
  13939. (org-with-wide-buffer
  13940. (org-with-point-at pom
  13941. (let ((clockstr (substring org-clock-string 0 -1))
  13942. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY" "BLOCKED"))
  13943. (case-fold-search nil)
  13944. beg end range props sum-props key key1 value string clocksum clocksumt)
  13945. (when (and (derived-mode-p 'org-mode)
  13946. (ignore-errors (org-back-to-heading t)))
  13947. (setq beg (point))
  13948. (setq sum-props (get-text-property (point) 'org-summaries))
  13949. (setq clocksum (get-text-property (point) :org-clock-minutes)
  13950. clocksumt (get-text-property (point) :org-clock-minutes-today))
  13951. (outline-next-heading)
  13952. (setq end (point))
  13953. (when (memq which '(all special))
  13954. ;; Get the special properties, like TODO and tags
  13955. (goto-char beg)
  13956. (when (and (or (not specific) (string= specific "TODO"))
  13957. (looking-at org-todo-line-regexp) (match-end 2))
  13958. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  13959. (when (and (or (not specific) (string= specific "PRIORITY"))
  13960. (looking-at org-priority-regexp))
  13961. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  13962. (when (or (not specific) (string= specific "FILE"))
  13963. (push (cons "FILE" buffer-file-name) props))
  13964. (when (and (or (not specific) (string= specific "TAGS"))
  13965. (setq value (org-get-tags-string))
  13966. (string-match "\\S-" value))
  13967. (push (cons "TAGS" value) props))
  13968. (when (and (or (not specific) (string= specific "ALLTAGS"))
  13969. (setq value (org-get-tags-at)))
  13970. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":")
  13971. ":"))
  13972. props))
  13973. (when (or (not specific) (string= specific "BLOCKED"))
  13974. (push (cons "BLOCKED" (if (org-entry-blocked-p) "t" "")) props))
  13975. (when (or (not specific)
  13976. (member specific
  13977. '("SCHEDULED" "DEADLINE" "CLOCK" "CLOSED"
  13978. "TIMESTAMP" "TIMESTAMP_IA")))
  13979. (catch 'match
  13980. (while (and (re-search-forward org-maybe-keyword-time-regexp end t)
  13981. (not (text-property-any 0 (length (match-string 0))
  13982. 'face 'font-lock-comment-face
  13983. (match-string 0))))
  13984. (setq key (if (match-end 1)
  13985. (substring (org-match-string-no-properties 1)
  13986. 0 -1))
  13987. string (if (equal key clockstr)
  13988. (org-trim
  13989. (buffer-substring-no-properties
  13990. (match-beginning 3) (goto-char
  13991. (point-at-eol))))
  13992. (substring (org-match-string-no-properties 3)
  13993. 1 -1)))
  13994. ;; Get the correct property name from the key. This is
  13995. ;; necessary if the user has configured time keywords.
  13996. (setq key1 (concat key ":"))
  13997. (cond
  13998. ((not key)
  13999. (setq key
  14000. (if (= (char-after (match-beginning 3)) ?\[)
  14001. "TIMESTAMP_IA" "TIMESTAMP")))
  14002. ((equal key1 org-scheduled-string) (setq key "SCHEDULED"))
  14003. ((equal key1 org-deadline-string) (setq key "DEADLINE"))
  14004. ((equal key1 org-closed-string) (setq key "CLOSED"))
  14005. ((equal key1 org-clock-string) (setq key "CLOCK")))
  14006. (if (and specific (equal key specific) (not (equal key "CLOCK")))
  14007. (progn
  14008. (push (cons key string) props)
  14009. ;; no need to search further if match is found
  14010. (throw 'match t))
  14011. (when (or (equal key "CLOCK") (not (assoc key props)))
  14012. (push (cons key string) props)))))))
  14013. (when (memq which '(all standard))
  14014. ;; Get the standard properties, like :PROP: ...
  14015. (setq range (org-get-property-block beg end))
  14016. (when range
  14017. (goto-char (car range))
  14018. (while (re-search-forward org-property-re
  14019. (cdr range) t)
  14020. (setq key (org-match-string-no-properties 2)
  14021. value (org-trim (or (org-match-string-no-properties 3) "")))
  14022. (unless (member key excluded)
  14023. (push (cons key (or value "")) props)))))
  14024. (if clocksum
  14025. (push (cons "CLOCKSUM"
  14026. (org-columns-number-to-string (/ (float clocksum) 60.)
  14027. 'add_times))
  14028. props))
  14029. (if clocksumt
  14030. (push (cons "CLOCKSUM_T"
  14031. (org-columns-number-to-string (/ (float clocksumt) 60.)
  14032. 'add_times))
  14033. props))
  14034. (unless (assoc "CATEGORY" props)
  14035. (push (cons "CATEGORY" (org-get-category)) props))
  14036. (append sum-props (nreverse props)))))))
  14037. (defun org-entry-get (pom property &optional inherit literal-nil)
  14038. "Get value of PROPERTY for entry or content at point-or-marker POM.
  14039. If INHERIT is non-nil and the entry does not have the property,
  14040. then also check higher levels of the hierarchy.
  14041. If INHERIT is the symbol `selective', use inheritance only if the setting
  14042. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  14043. If the property is present but empty, the return value is the empty string.
  14044. If the property is not present at all, nil is returned.
  14045. Return the value as a string.
  14046. If LITERAL-NIL is set, return the string value \"nil\" as a string,
  14047. do not interpret it as the list atom nil. This is used for inheritance
  14048. when a \"nil\" value can supersede a non-nil value higher up the hierarchy."
  14049. (org-with-point-at pom
  14050. (if (and inherit (if (eq inherit 'selective)
  14051. (org-property-inherit-p property)
  14052. t))
  14053. (org-entry-get-with-inheritance property literal-nil)
  14054. (if (member property org-special-properties)
  14055. ;; We need a special property. Use `org-entry-properties'
  14056. ;; to retrieve it, but specify the wanted property
  14057. (cdr (assoc property (org-entry-properties nil 'special property)))
  14058. (org-with-wide-buffer
  14059. (let ((range (org-get-property-block)))
  14060. (when (and range (not (eq (car range) (cdr range)))
  14061. (save-excursion
  14062. (goto-char (car range))
  14063. (re-search-forward
  14064. (concat (org-re-property property) "\\|"
  14065. (org-re-property (concat property "+")))
  14066. (cdr range) t)))
  14067. (let* ((props
  14068. (list (or (assoc property org-file-properties)
  14069. (assoc property org-global-properties)
  14070. (assoc property org-global-properties-fixed))))
  14071. (ap (lambda (key)
  14072. (when (re-search-forward
  14073. (org-re-property key) (cdr range) t)
  14074. (setq props
  14075. (org-update-property-plist
  14076. key
  14077. (if (match-end 3)
  14078. (org-match-string-no-properties 3) "")
  14079. props)))))
  14080. val)
  14081. (goto-char (car range))
  14082. (funcall ap property)
  14083. (goto-char (car range))
  14084. (while (funcall ap (concat property "+")))
  14085. (setq val (cdr (assoc property props)))
  14086. (when val (if literal-nil val (org-not-nil val)))))))))))
  14087. (defun org-property-or-variable-value (var &optional inherit)
  14088. "Check if there is a property fixing the value of VAR.
  14089. If yes, return this value. If not, return the current value of the variable."
  14090. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  14091. (if (and prop (stringp prop) (string-match "\\S-" prop))
  14092. (read prop)
  14093. (symbol-value var))))
  14094. (defun org-entry-delete (pom property &optional delete-empty-drawer)
  14095. "Delete the property PROPERTY from entry at point-or-marker POM.
  14096. When optional argument DELETE-EMPTY-DRAWER is a string, it defines
  14097. an empty drawer to delete."
  14098. (org-with-point-at pom
  14099. (if (member property org-special-properties)
  14100. nil ; cannot delete these properties.
  14101. (let ((range (org-get-property-block)))
  14102. (if (and range
  14103. (goto-char (car range))
  14104. (re-search-forward
  14105. (org-re-property property nil t)
  14106. (cdr range) t))
  14107. (progn
  14108. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  14109. (and delete-empty-drawer
  14110. (org-remove-empty-drawer-at
  14111. delete-empty-drawer (car range)))
  14112. t)
  14113. nil)))))
  14114. ;; Multi-values properties are properties that contain multiple values
  14115. ;; These values are assumed to be single words, separated by whitespace.
  14116. (defun org-entry-add-to-multivalued-property (pom property value)
  14117. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  14118. (let* ((old (org-entry-get pom property))
  14119. (values (and old (org-split-string old "[ \t]"))))
  14120. (setq value (org-entry-protect-space value))
  14121. (unless (member value values)
  14122. (setq values (append values (list value)))
  14123. (org-entry-put pom property
  14124. (mapconcat 'identity values " ")))))
  14125. (defun org-entry-remove-from-multivalued-property (pom property value)
  14126. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  14127. (let* ((old (org-entry-get pom property))
  14128. (values (and old (org-split-string old "[ \t]"))))
  14129. (setq value (org-entry-protect-space value))
  14130. (when (member value values)
  14131. (setq values (delete value values))
  14132. (org-entry-put pom property
  14133. (mapconcat 'identity values " ")))))
  14134. (defun org-entry-member-in-multivalued-property (pom property value)
  14135. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  14136. (let* ((old (org-entry-get pom property))
  14137. (values (and old (org-split-string old "[ \t]"))))
  14138. (setq value (org-entry-protect-space value))
  14139. (member value values)))
  14140. (defun org-entry-get-multivalued-property (pom property)
  14141. "Return a list of values in a multivalued property."
  14142. (let* ((value (org-entry-get pom property))
  14143. (values (and value (org-split-string value "[ \t]"))))
  14144. (mapcar 'org-entry-restore-space values)))
  14145. (defun org-entry-put-multivalued-property (pom property &rest values)
  14146. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  14147. VALUES should be a list of strings. Spaces will be protected."
  14148. (org-entry-put pom property
  14149. (mapconcat 'org-entry-protect-space values " "))
  14150. (let* ((value (org-entry-get pom property))
  14151. (values (and value (org-split-string value "[ \t]"))))
  14152. (mapcar 'org-entry-restore-space values)))
  14153. (defun org-entry-protect-space (s)
  14154. "Protect spaces and newline in string S."
  14155. (while (string-match " " s)
  14156. (setq s (replace-match "%20" t t s)))
  14157. (while (string-match "\n" s)
  14158. (setq s (replace-match "%0A" t t s)))
  14159. s)
  14160. (defun org-entry-restore-space (s)
  14161. "Restore spaces and newline in string S."
  14162. (while (string-match "%20" s)
  14163. (setq s (replace-match " " t t s)))
  14164. (while (string-match "%0A" s)
  14165. (setq s (replace-match "\n" t t s)))
  14166. s)
  14167. (defvar org-entry-property-inherited-from (make-marker)
  14168. "Marker pointing to the entry from where a property was inherited.
  14169. Each call to `org-entry-get-with-inheritance' will set this marker to the
  14170. location of the entry where the inheritance search matched. If there was
  14171. no match, the marker will point nowhere.
  14172. Note that also `org-entry-get' calls this function, if the INHERIT flag
  14173. is set.")
  14174. (defun org-entry-get-with-inheritance (property &optional literal-nil)
  14175. "Get PROPERTY of entry or content at point, search higher levels if needed.
  14176. The search will stop at the first ancestor which has the property defined.
  14177. If the value found is \"nil\", return nil to show that the property
  14178. should be considered as undefined (this is the meaning of nil here).
  14179. However, if LITERAL-NIL is set, return the string value \"nil\" instead."
  14180. (move-marker org-entry-property-inherited-from nil)
  14181. (let (tmp)
  14182. (save-excursion
  14183. (save-restriction
  14184. (widen)
  14185. (catch 'ex
  14186. (while t
  14187. (when (setq tmp (org-entry-get nil property nil literal-nil))
  14188. (or (ignore-errors (org-back-to-heading t))
  14189. (goto-char (point-min)))
  14190. (move-marker org-entry-property-inherited-from (point))
  14191. (throw 'ex tmp))
  14192. (or (ignore-errors (org-up-heading-safe))
  14193. (throw 'ex nil))))))
  14194. (setq tmp (or tmp
  14195. (cdr (assoc property org-file-properties))
  14196. (cdr (assoc property org-global-properties))
  14197. (cdr (assoc property org-global-properties-fixed))))
  14198. (if literal-nil tmp (org-not-nil tmp))))
  14199. (defvar org-property-changed-functions nil
  14200. "Hook called when the value of a property has changed.
  14201. Each hook function should accept two arguments, the name of the property
  14202. and the new value.")
  14203. (defun org-entry-put (pom property value)
  14204. "Set PROPERTY to VALUE for entry at point-or-marker POM.
  14205. If the value is `nil', it is converted to the empty string.
  14206. If it is not a string, an error is raised."
  14207. (cond ((null value) (setq value ""))
  14208. ((not (stringp value))
  14209. (error "Properties values should be strings.")))
  14210. (org-with-point-at pom
  14211. (org-back-to-heading t)
  14212. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  14213. range)
  14214. (cond
  14215. ((equal property "TODO")
  14216. (when (and (string-match "\\S-" value)
  14217. (not (member value org-todo-keywords-1)))
  14218. (user-error "\"%s\" is not a valid TODO state" value))
  14219. (if (or (not value)
  14220. (not (string-match "\\S-" value)))
  14221. (setq value 'none))
  14222. (org-todo value)
  14223. (org-set-tags nil 'align))
  14224. ((equal property "PRIORITY")
  14225. (org-priority (if (and value (string-match "\\S-" value))
  14226. (string-to-char value) ?\ ))
  14227. (org-set-tags nil 'align))
  14228. ((equal property "CLOCKSUM")
  14229. (if (not (re-search-forward
  14230. (concat org-clock-string ".*\\]--\\(\\[[^]]+\\]\\)") nil t))
  14231. (error "Cannot find a clock log")
  14232. (goto-char (- (match-end 1) 2))
  14233. (cond
  14234. ((eq value 'earlier) (org-timestamp-down))
  14235. ((eq value 'later) (org-timestamp-up)))
  14236. (org-clock-sum-current-item)))
  14237. ((equal property "SCHEDULED")
  14238. (if (re-search-forward org-scheduled-time-regexp end t)
  14239. (cond
  14240. ((eq value 'earlier) (org-timestamp-change -1 'day))
  14241. ((eq value 'later) (org-timestamp-change 1 'day))
  14242. (t (call-interactively 'org-schedule)))
  14243. (call-interactively 'org-schedule)))
  14244. ((equal property "DEADLINE")
  14245. (if (re-search-forward org-deadline-time-regexp end t)
  14246. (cond
  14247. ((eq value 'earlier) (org-timestamp-change -1 'day))
  14248. ((eq value 'later) (org-timestamp-change 1 'day))
  14249. (t (call-interactively 'org-deadline)))
  14250. (call-interactively 'org-deadline)))
  14251. ((member property org-special-properties)
  14252. (error "The %s property can not yet be set with `org-entry-put'"
  14253. property))
  14254. (t ; a non-special property
  14255. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  14256. (setq range (org-get-property-block beg end 'force))
  14257. (goto-char (car range))
  14258. (if (re-search-forward
  14259. (org-re-property property nil t) (cdr range) t)
  14260. (progn
  14261. (delete-region (match-beginning 0) (match-end 0))
  14262. (goto-char (match-beginning 0)))
  14263. (goto-char (cdr range))
  14264. (insert "\n")
  14265. (backward-char 1)
  14266. (org-indent-line))
  14267. (insert ":" property ":")
  14268. (and value (insert " " value))
  14269. (org-indent-line)))))
  14270. (run-hook-with-args 'org-property-changed-functions property value)))
  14271. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  14272. "Get all property keys in the current buffer.
  14273. With INCLUDE-SPECIALS, also list the special properties that reflect things
  14274. like tags and TODO state.
  14275. With INCLUDE-DEFAULTS, also include properties that has special meaning
  14276. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING
  14277. and others.
  14278. With INCLUDE-COLUMNS, also include property names given in COLUMN
  14279. formats in the current buffer."
  14280. (let (rtn range cfmt s p)
  14281. (save-excursion
  14282. (save-restriction
  14283. (widen)
  14284. (goto-char (point-min))
  14285. (while (re-search-forward org-property-start-re nil t)
  14286. (setq range (org-get-property-block))
  14287. (goto-char (car range))
  14288. (while (re-search-forward org-property-re
  14289. (cdr range) t)
  14290. (add-to-list 'rtn (org-match-string-no-properties 2)))
  14291. (outline-next-heading))))
  14292. (when include-specials
  14293. (setq rtn (append org-special-properties rtn)))
  14294. (when include-defaults
  14295. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties)
  14296. (add-to-list 'rtn org-effort-property))
  14297. (when include-columns
  14298. (save-excursion
  14299. (save-restriction
  14300. (widen)
  14301. (goto-char (point-min))
  14302. (while (re-search-forward
  14303. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  14304. nil t)
  14305. (setq cfmt (match-string 2) s 0)
  14306. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  14307. cfmt s)
  14308. (setq s (match-end 0)
  14309. p (match-string 1 cfmt))
  14310. (unless (or (equal p "ITEM")
  14311. (member p org-special-properties))
  14312. (add-to-list 'rtn (match-string 1 cfmt))))))))
  14313. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  14314. (defun org-property-values (key)
  14315. "Return a list of all values of property KEY in the current buffer."
  14316. (save-excursion
  14317. (save-restriction
  14318. (widen)
  14319. (goto-char (point-min))
  14320. (let ((re (org-re-property key))
  14321. values)
  14322. (while (re-search-forward re nil t)
  14323. (add-to-list 'values (org-trim (match-string 3))))
  14324. (delete "" values)))))
  14325. (defun org-insert-property-drawer ()
  14326. "Insert a property drawer into the current entry."
  14327. (org-back-to-heading t)
  14328. (looking-at org-outline-regexp)
  14329. (let ((indent (if org-adapt-indentation
  14330. (- (match-end 0) (match-beginning 0))
  14331. 0))
  14332. (beg (point))
  14333. (re (concat "^[ \t]*" org-keyword-time-regexp))
  14334. end hiddenp)
  14335. (outline-next-heading)
  14336. (setq end (point))
  14337. (goto-char beg)
  14338. (while (re-search-forward re end t))
  14339. (setq hiddenp (outline-invisible-p))
  14340. (end-of-line 1)
  14341. (and (equal (char-after) ?\n) (forward-char 1))
  14342. (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
  14343. (if (member (match-string 1) '("CLOCK:" ":END:"))
  14344. ;; just skip this line
  14345. (beginning-of-line 2)
  14346. ;; Drawer start, find the end
  14347. (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
  14348. (beginning-of-line 1)))
  14349. (org-skip-over-state-notes)
  14350. (skip-chars-backward " \t\n\r")
  14351. (if (and (eq (char-before) ?*) (not (eq (char-after) ?\n)))
  14352. (forward-char 1))
  14353. (goto-char (point-at-eol))
  14354. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  14355. (beginning-of-line 0)
  14356. (org-indent-to-column indent)
  14357. (beginning-of-line 2)
  14358. (org-indent-to-column indent)
  14359. (beginning-of-line 0)
  14360. (if hiddenp
  14361. (save-excursion
  14362. (org-back-to-heading t)
  14363. (hide-entry))
  14364. (org-flag-drawer t))))
  14365. (defun org-insert-drawer (&optional arg drawer)
  14366. "Insert a drawer at point.
  14367. Optional argument DRAWER, when non-nil, is a string representing
  14368. drawer's name. Otherwise, the user is prompted for a name.
  14369. If a region is active, insert the drawer around that region
  14370. instead.
  14371. Point is left between drawer's boundaries."
  14372. (interactive "P")
  14373. (let* ((logbook (if (stringp org-log-into-drawer) org-log-into-drawer
  14374. "LOGBOOK"))
  14375. ;; SYSTEM-DRAWERS is a list of drawer names that are used
  14376. ;; internally by Org. They are meant to be inserted
  14377. ;; automatically.
  14378. (system-drawers `("CLOCK" ,logbook "PROPERTIES"))
  14379. ;; Remove system drawers from list. Note: For some reason,
  14380. ;; `org-completing-read' ignores the predicate while
  14381. ;; `completing-read' handles it fine.
  14382. (drawer (if arg "PROPERTIES"
  14383. (or drawer
  14384. (completing-read
  14385. "Drawer: " org-drawers
  14386. (lambda (d) (not (member d system-drawers))))))))
  14387. (cond
  14388. ;; With C-u, fall back on `org-insert-property-drawer'
  14389. (arg (org-insert-property-drawer))
  14390. ;; With an active region, insert a drawer at point.
  14391. ((not (org-region-active-p))
  14392. (progn
  14393. (unless (bolp) (insert "\n"))
  14394. (insert (format ":%s:\n\n:END:\n" drawer))
  14395. (forward-line -2)))
  14396. ;; Otherwise, insert the drawer at point
  14397. (t
  14398. (let ((rbeg (region-beginning))
  14399. (rend (copy-marker (region-end))))
  14400. (unwind-protect
  14401. (progn
  14402. (goto-char rbeg)
  14403. (beginning-of-line)
  14404. (when (save-excursion
  14405. (re-search-forward org-outline-regexp-bol rend t))
  14406. (user-error "Drawers cannot contain headlines"))
  14407. ;; Position point at the beginning of the first
  14408. ;; non-blank line in region. Insert drawer's opening
  14409. ;; there, then indent it.
  14410. (org-skip-whitespace)
  14411. (beginning-of-line)
  14412. (insert ":" drawer ":\n")
  14413. (forward-line -1)
  14414. (indent-for-tab-command)
  14415. ;; Move point to the beginning of the first blank line
  14416. ;; after the last non-blank line in region. Insert
  14417. ;; drawer's closing, then indent it.
  14418. (goto-char rend)
  14419. (skip-chars-backward " \r\t\n")
  14420. (insert "\n:END:")
  14421. (deactivate-mark t)
  14422. (indent-for-tab-command)
  14423. (unless (eolp) (insert "\n")))
  14424. ;; Clear marker, whatever the outcome of insertion is.
  14425. (set-marker rend nil)))))))
  14426. (defvar org-property-set-functions-alist nil
  14427. "Property set function alist.
  14428. Each entry should have the following format:
  14429. (PROPERTY . READ-FUNCTION)
  14430. The read function will be called with the same argument as
  14431. `org-completing-read'.")
  14432. (defun org-set-property-function (property)
  14433. "Get the function that should be used to set PROPERTY.
  14434. This is computed according to `org-property-set-functions-alist'."
  14435. (or (cdr (assoc property org-property-set-functions-alist))
  14436. 'org-completing-read))
  14437. (defun org-read-property-value (property)
  14438. "Read PROPERTY value from user."
  14439. (let* ((completion-ignore-case t)
  14440. (allowed (org-property-get-allowed-values nil property 'table))
  14441. (cur (org-entry-get nil property))
  14442. (prompt (concat property " value"
  14443. (if (and cur (string-match "\\S-" cur))
  14444. (concat " [" cur "]") "") ": "))
  14445. (set-function (org-set-property-function property))
  14446. (val (if allowed
  14447. (funcall set-function prompt allowed nil
  14448. (not (get-text-property 0 'org-unrestricted
  14449. (caar allowed))))
  14450. (let (org-completion-use-ido org-completion-use-iswitchb)
  14451. (funcall set-function prompt
  14452. (mapcar 'list (org-property-values property))
  14453. nil nil "" nil cur)))))
  14454. (org-trim val)))
  14455. (defvar org-last-set-property nil)
  14456. (defvar org-last-set-property-value nil)
  14457. (defun org-read-property-name ()
  14458. "Read a property name."
  14459. (let* ((completion-ignore-case t)
  14460. (keys (org-buffer-property-keys nil t t))
  14461. (default-prop (or (save-excursion
  14462. (save-match-data
  14463. (beginning-of-line)
  14464. (and (looking-at "^\\s-*:\\([^:\n]+\\):")
  14465. (null (string= (match-string 1) "END"))
  14466. (match-string 1))))
  14467. org-last-set-property))
  14468. (property (org-icompleting-read
  14469. (concat "Property"
  14470. (if default-prop (concat " [" default-prop "]") "")
  14471. ": ")
  14472. (mapcar 'list keys)
  14473. nil nil nil nil
  14474. default-prop)))
  14475. (if (member property keys)
  14476. property
  14477. (or (cdr (assoc (downcase property)
  14478. (mapcar (lambda (x) (cons (downcase x) x))
  14479. keys)))
  14480. property))))
  14481. (defun org-set-property-and-value (use-last)
  14482. "Allow to set [PROPERTY]: [value] direction from prompt.
  14483. When use-default, don't even ask, just use the last
  14484. \"[PROPERTY]: [value]\" string from the history."
  14485. (interactive "P")
  14486. (let* ((completion-ignore-case t)
  14487. (pv (or (and use-last org-last-set-property-value)
  14488. (org-completing-read
  14489. "Enter a \"[Property]: [value]\" pair: "
  14490. nil nil nil nil nil
  14491. org-last-set-property-value)))
  14492. prop val)
  14493. (when (string-match "^[ \t]*\\([^:]+\\):[ \t]*\\(.*\\)[ \t]*$" pv)
  14494. (setq prop (match-string 1 pv)
  14495. val (match-string 2 pv))
  14496. (org-set-property prop val))))
  14497. (defun org-set-property (property value)
  14498. "In the current entry, set PROPERTY to VALUE.
  14499. When called interactively, this will prompt for a property name, offering
  14500. completion on existing and default properties. And then it will prompt
  14501. for a value, offering completion either on allowed values (via an inherited
  14502. xxx_ALL property) or on existing values in other instances of this property
  14503. in the current file."
  14504. (interactive (list nil nil))
  14505. (let* ((property (or property (org-read-property-name)))
  14506. (value (or value (org-read-property-value property)))
  14507. (fn (cdr (assoc property org-properties-postprocess-alist))))
  14508. (setq org-last-set-property property)
  14509. (setq org-last-set-property-value (concat property ": " value))
  14510. ;; Possibly postprocess the inserted value:
  14511. (when fn (setq value (funcall fn value)))
  14512. (unless (equal (org-entry-get nil property) value)
  14513. (org-entry-put nil property value))))
  14514. (defun org-delete-property (property &optional delete-empty-drawer)
  14515. "In the current entry, delete PROPERTY.
  14516. When optional argument DELETE-EMPTY-DRAWER is a string, it defines
  14517. an empty drawer to delete."
  14518. (interactive
  14519. (let* ((completion-ignore-case t)
  14520. (cat (org-entry-get (point) "CATEGORY"))
  14521. (props0 (org-entry-properties nil 'standard))
  14522. (props (if cat props0
  14523. (delete `("CATEGORY" . ,(org-get-category)) props0)))
  14524. (prop (if (< 1 (length props))
  14525. (org-icompleting-read "Property: " props nil t)
  14526. (caar props))))
  14527. (list prop)))
  14528. (if (not property)
  14529. (message "No property to delete in this entry")
  14530. (org-entry-delete nil property delete-empty-drawer)
  14531. (message "Property \"%s\" deleted" property)))
  14532. (defun org-delete-property-globally (property)
  14533. "Remove PROPERTY globally, from all entries."
  14534. (interactive
  14535. (let* ((completion-ignore-case t)
  14536. (prop (org-icompleting-read
  14537. "Globally remove property: "
  14538. (mapcar 'list (org-buffer-property-keys)))))
  14539. (list prop)))
  14540. (save-excursion
  14541. (save-restriction
  14542. (widen)
  14543. (goto-char (point-min))
  14544. (let ((cnt 0))
  14545. (while (re-search-forward
  14546. (org-re-property property)
  14547. nil t)
  14548. (setq cnt (1+ cnt))
  14549. (delete-region (match-beginning 0) (1+ (point-at-eol))))
  14550. (message "Property \"%s\" removed from %d entries" property cnt)))))
  14551. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  14552. (defun org-compute-property-at-point ()
  14553. "Compute the property at point.
  14554. This looks for an enclosing column format, extracts the operator and
  14555. then applies it to the property in the column format's scope."
  14556. (interactive)
  14557. (unless (org-at-property-p)
  14558. (user-error "Not at a property"))
  14559. (let ((prop (org-match-string-no-properties 2)))
  14560. (org-columns-get-format-and-top-level)
  14561. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  14562. (user-error "No operator defined for property %s" prop))
  14563. (org-columns-compute prop)))
  14564. (defvar org-property-allowed-value-functions nil
  14565. "Hook for functions supplying allowed values for a specific property.
  14566. The functions must take a single argument, the name of the property, and
  14567. return a flat list of allowed values. If \":ETC\" is one of
  14568. the values, this means that these values are intended as defaults for
  14569. completion, but that other values should be allowed too.
  14570. The functions must return nil if they are not responsible for this
  14571. property.")
  14572. (defun org-property-get-allowed-values (pom property &optional table)
  14573. "Get allowed values for the property PROPERTY.
  14574. When TABLE is non-nil, return an alist that can directly be used for
  14575. completion."
  14576. (let (vals)
  14577. (cond
  14578. ((equal property "TODO")
  14579. (setq vals (org-with-point-at pom
  14580. (append org-todo-keywords-1 '("")))))
  14581. ((equal property "PRIORITY")
  14582. (let ((n org-lowest-priority))
  14583. (while (>= n org-highest-priority)
  14584. (push (char-to-string n) vals)
  14585. (setq n (1- n)))))
  14586. ((member property org-special-properties))
  14587. ((setq vals (run-hook-with-args-until-success
  14588. 'org-property-allowed-value-functions property)))
  14589. (t
  14590. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  14591. (when (and vals (string-match "\\S-" vals))
  14592. (setq vals (car (read-from-string (concat "(" vals ")"))))
  14593. (setq vals (mapcar (lambda (x)
  14594. (cond ((stringp x) x)
  14595. ((numberp x) (number-to-string x))
  14596. ((symbolp x) (symbol-name x))
  14597. (t "???")))
  14598. vals)))))
  14599. (when (member ":ETC" vals)
  14600. (setq vals (remove ":ETC" vals))
  14601. (org-add-props (car vals) '(org-unrestricted t)))
  14602. (if table (mapcar 'list vals) vals)))
  14603. (defun org-property-previous-allowed-value (&optional previous)
  14604. "Switch to the next allowed value for this property."
  14605. (interactive)
  14606. (org-property-next-allowed-value t))
  14607. (defun org-property-next-allowed-value (&optional previous)
  14608. "Switch to the next allowed value for this property."
  14609. (interactive)
  14610. (unless (org-at-property-p)
  14611. (user-error "Not at a property"))
  14612. (let* ((prop (car (save-match-data (org-split-string (match-string 1) ":"))))
  14613. (key (match-string 2))
  14614. (value (match-string 3))
  14615. (allowed (or (org-property-get-allowed-values (point) key)
  14616. (and (member value '("[ ]" "[-]" "[X]"))
  14617. '("[ ]" "[X]"))))
  14618. (heading (save-match-data (nth 4 (org-heading-components))))
  14619. nval)
  14620. (unless allowed
  14621. (user-error "Allowed values for this property have not been defined"))
  14622. (if previous (setq allowed (reverse allowed)))
  14623. (if (member value allowed)
  14624. (setq nval (car (cdr (member value allowed)))))
  14625. (setq nval (or nval (car allowed)))
  14626. (if (equal nval value)
  14627. (user-error "Only one allowed value for this property"))
  14628. (org-at-property-p)
  14629. (replace-match (concat " :" key ": " nval) t t)
  14630. (org-indent-line)
  14631. (beginning-of-line 1)
  14632. (skip-chars-forward " \t")
  14633. (when (equal prop org-effort-property)
  14634. (save-excursion
  14635. (org-back-to-heading t)
  14636. (put-text-property (point-at-bol) (point-at-eol) 'org-effort nval))
  14637. (when (string= org-clock-current-task heading)
  14638. (setq org-clock-effort nval)
  14639. (org-clock-update-mode-line)))
  14640. (run-hook-with-args 'org-property-changed-functions key nval)))
  14641. (defun org-find-olp (path &optional this-buffer)
  14642. "Return a marker pointing to the entry at outline path OLP.
  14643. If anything goes wrong, throw an error.
  14644. You can wrap this call to catch the error like this:
  14645. (condition-case msg
  14646. (org-mobile-locate-entry (match-string 4))
  14647. (error (nth 1 msg)))
  14648. The return value will then be either a string with the error message,
  14649. or a marker if everything is OK.
  14650. If THIS-BUFFER is set, the outline path does not contain a file,
  14651. only headings."
  14652. (let* ((file (if this-buffer buffer-file-name (pop path)))
  14653. (buffer (if this-buffer (current-buffer) (find-file-noselect file)))
  14654. (level 1)
  14655. (lmin 1)
  14656. (lmax 1)
  14657. limit re end found pos heading cnt flevel)
  14658. (unless buffer (error "File not found :%s" file))
  14659. (with-current-buffer buffer
  14660. (save-excursion
  14661. (save-restriction
  14662. (widen)
  14663. (setq limit (point-max))
  14664. (goto-char (point-min))
  14665. (while (setq heading (pop path))
  14666. (setq re (format org-complex-heading-regexp-format
  14667. (regexp-quote heading)))
  14668. (setq cnt 0 pos (point))
  14669. (while (re-search-forward re end t)
  14670. (setq level (- (match-end 1) (match-beginning 1)))
  14671. (if (and (>= level lmin) (<= level lmax))
  14672. (setq found (match-beginning 0) flevel level cnt (1+ cnt))))
  14673. (when (= cnt 0) (error "Heading not found on level %d: %s"
  14674. lmax heading))
  14675. (when (> cnt 1) (error "Heading not unique on level %d: %s"
  14676. lmax heading))
  14677. (goto-char found)
  14678. (setq lmin (1+ flevel) lmax (+ lmin (if org-odd-levels-only 1 0)))
  14679. (setq end (save-excursion (org-end-of-subtree t t))))
  14680. (when (org-at-heading-p)
  14681. (point-marker)))))))
  14682. (defun org-find-exact-headline-in-buffer (heading &optional buffer pos-only)
  14683. "Find node HEADING in BUFFER.
  14684. Return a marker to the heading if it was found, or nil if not.
  14685. If POS-ONLY is set, return just the position instead of a marker.
  14686. The heading text must match exact, but it may have a TODO keyword,
  14687. a priority cookie and tags in the standard locations."
  14688. (with-current-buffer (or buffer (current-buffer))
  14689. (save-excursion
  14690. (save-restriction
  14691. (widen)
  14692. (goto-char (point-min))
  14693. (let (case-fold-search)
  14694. (if (re-search-forward
  14695. (format org-complex-heading-regexp-format
  14696. (regexp-quote heading)) nil t)
  14697. (if pos-only
  14698. (match-beginning 0)
  14699. (move-marker (make-marker) (match-beginning 0)))))))))
  14700. (defun org-find-exact-heading-in-directory (heading &optional dir)
  14701. "Find Org node headline HEADING in all .org files in directory DIR.
  14702. When the target headline is found, return a marker to this location."
  14703. (let ((files (directory-files (or dir default-directory)
  14704. nil "\\`[^.#].*\\.org\\'"))
  14705. file visiting m buffer)
  14706. (catch 'found
  14707. (while (setq file (pop files))
  14708. (message "trying %s" file)
  14709. (setq visiting (org-find-base-buffer-visiting file))
  14710. (setq buffer (or visiting (find-file-noselect file)))
  14711. (setq m (org-find-exact-headline-in-buffer
  14712. heading buffer))
  14713. (when (and (not m) (not visiting)) (kill-buffer buffer))
  14714. (and m (throw 'found m))))))
  14715. (defun org-find-entry-with-id (ident)
  14716. "Locate the entry that contains the ID property with exact value IDENT.
  14717. IDENT can be a string, a symbol or a number, this function will search for
  14718. the string representation of it.
  14719. Return the position where this entry starts, or nil if there is no such entry."
  14720. (interactive "sID: ")
  14721. (let ((id (cond
  14722. ((stringp ident) ident)
  14723. ((symbol-name ident) (symbol-name ident))
  14724. ((numberp ident) (number-to-string ident))
  14725. (t (error "IDENT %s must be a string, symbol or number" ident))))
  14726. (case-fold-search nil))
  14727. (save-excursion
  14728. (save-restriction
  14729. (widen)
  14730. (goto-char (point-min))
  14731. (when (re-search-forward
  14732. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  14733. nil t)
  14734. (org-back-to-heading t)
  14735. (point))))))
  14736. ;;;; Timestamps
  14737. (defvar org-last-changed-timestamp nil)
  14738. (defvar org-last-inserted-timestamp nil
  14739. "The last time stamp inserted with `org-insert-time-stamp'.")
  14740. (defvar org-ts-what) ; dynamically scoped parameter
  14741. (defun org-time-stamp (arg &optional inactive)
  14742. "Prompt for a date/time and insert a time stamp.
  14743. If the user specifies a time like HH:MM or if this command is
  14744. called with at least one prefix argument, the time stamp contains
  14745. the date and the time. Otherwise, only the date is be included.
  14746. All parts of a date not specified by the user is filled in from
  14747. the current date/time. So if you just press return without
  14748. typing anything, the time stamp will represent the current
  14749. date/time.
  14750. If there is already a timestamp at the cursor, it will be
  14751. modified.
  14752. With two universal prefix arguments, insert an active timestamp
  14753. with the current time without prompting the user.
  14754. When called from lisp, the timestamp is inactive if INACTIVE is
  14755. non-nil."
  14756. (interactive "P")
  14757. (let* ((ts nil)
  14758. (default-time
  14759. ;; Default time is either today, or, when entering a range,
  14760. ;; the range start.
  14761. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  14762. (save-excursion
  14763. (re-search-backward
  14764. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  14765. (- (point) 20) t)))
  14766. (apply 'encode-time (org-parse-time-string (match-string 1)))
  14767. (current-time)))
  14768. (default-input (and ts (org-get-compact-tod ts)))
  14769. (repeater (save-excursion
  14770. (save-match-data
  14771. (beginning-of-line)
  14772. (when (re-search-forward
  14773. "\\([.+-]+[0-9]+[hdwmy] ?\\)+" ;;\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  14774. (save-excursion (progn (end-of-line) (point))) t)
  14775. (match-string 0)))))
  14776. org-time-was-given org-end-time-was-given time)
  14777. (cond
  14778. ((and (org-at-timestamp-p t)
  14779. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  14780. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  14781. (insert "--")
  14782. (setq time (let ((this-command this-command))
  14783. (org-read-date arg 'totime nil nil
  14784. default-time default-input inactive)))
  14785. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  14786. ((org-at-timestamp-p t)
  14787. (setq time (let ((this-command this-command))
  14788. (org-read-date arg 'totime nil nil default-time default-input inactive)))
  14789. (when (org-at-timestamp-p t) ; just to get the match data
  14790. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  14791. (replace-match "")
  14792. (setq org-last-changed-timestamp
  14793. (org-insert-time-stamp
  14794. time (or org-time-was-given arg)
  14795. inactive nil nil (list org-end-time-was-given)))
  14796. (when repeater (goto-char (1- (point))) (insert " " repeater)
  14797. (setq org-last-changed-timestamp
  14798. (concat (substring org-last-inserted-timestamp 0 -1)
  14799. " " repeater ">"))))
  14800. (message "Timestamp updated"))
  14801. ((equal arg '(16))
  14802. (org-insert-time-stamp (current-time) t inactive))
  14803. (t
  14804. (setq time (let ((this-command this-command))
  14805. (org-read-date arg 'totime nil nil default-time default-input inactive)))
  14806. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  14807. nil nil (list org-end-time-was-given))))))
  14808. ;; FIXME: can we use this for something else, like computing time differences?
  14809. (defun org-get-compact-tod (s)
  14810. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  14811. (let* ((t1 (match-string 1 s))
  14812. (h1 (string-to-number (match-string 2 s)))
  14813. (m1 (string-to-number (match-string 3 s)))
  14814. (t2 (and (match-end 4) (match-string 5 s)))
  14815. (h2 (and t2 (string-to-number (match-string 6 s))))
  14816. (m2 (and t2 (string-to-number (match-string 7 s))))
  14817. dh dm)
  14818. (if (not t2)
  14819. t1
  14820. (setq dh (- h2 h1) dm (- m2 m1))
  14821. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  14822. (concat t1 "+" (number-to-string dh)
  14823. (and (/= 0 dm) (format ":%02d" dm)))))))
  14824. (defun org-time-stamp-inactive (&optional arg)
  14825. "Insert an inactive time stamp.
  14826. An inactive time stamp is enclosed in square brackets instead of angle
  14827. brackets. It is inactive in the sense that it does not trigger agenda entries,
  14828. does not link to the calendar and cannot be changed with the S-cursor keys.
  14829. So these are more for recording a certain time/date."
  14830. (interactive "P")
  14831. (org-time-stamp arg 'inactive))
  14832. (defvar org-date-ovl (make-overlay 1 1))
  14833. (overlay-put org-date-ovl 'face 'org-date-selected)
  14834. (org-detach-overlay org-date-ovl)
  14835. (defvar org-ans1) ; dynamically scoped parameter
  14836. (defvar org-ans2) ; dynamically scoped parameter
  14837. (defvar org-plain-time-of-day-regexp) ; defined below
  14838. (defvar org-overriding-default-time nil) ; dynamically scoped
  14839. (defvar org-read-date-overlay nil)
  14840. (defvar org-dcst nil) ; dynamically scoped
  14841. (defvar org-read-date-history nil)
  14842. (defvar org-read-date-final-answer nil)
  14843. (defvar org-read-date-analyze-futurep nil)
  14844. (defvar org-read-date-analyze-forced-year nil)
  14845. (defvar org-read-date-inactive)
  14846. (defvar org-read-date-minibuffer-local-map
  14847. (let* ((org-replace-disputed-keys nil)
  14848. (map (make-sparse-keymap)))
  14849. (set-keymap-parent map minibuffer-local-map)
  14850. (org-defkey map (kbd ".")
  14851. (lambda () (interactive)
  14852. ;; Are we at the beginning of the prompt?
  14853. (if (looking-back "^[^:]+: ")
  14854. (org-eval-in-calendar '(calendar-goto-today))
  14855. (insert "."))))
  14856. (org-defkey map (kbd "C-.")
  14857. (lambda () (interactive)
  14858. (org-eval-in-calendar '(calendar-goto-today))))
  14859. (org-defkey map [(meta shift left)]
  14860. (lambda () (interactive)
  14861. (org-eval-in-calendar '(calendar-backward-month 1))))
  14862. (org-defkey map [(meta shift right)]
  14863. (lambda () (interactive)
  14864. (org-eval-in-calendar '(calendar-forward-month 1))))
  14865. (org-defkey map [(meta shift up)]
  14866. (lambda () (interactive)
  14867. (org-eval-in-calendar '(calendar-backward-year 1))))
  14868. (org-defkey map [(meta shift down)]
  14869. (lambda () (interactive)
  14870. (org-eval-in-calendar '(calendar-forward-year 1))))
  14871. (org-defkey map [?\e (shift left)]
  14872. (lambda () (interactive)
  14873. (org-eval-in-calendar '(calendar-backward-month 1))))
  14874. (org-defkey map [?\e (shift right)]
  14875. (lambda () (interactive)
  14876. (org-eval-in-calendar '(calendar-forward-month 1))))
  14877. (org-defkey map [?\e (shift up)]
  14878. (lambda () (interactive)
  14879. (org-eval-in-calendar '(calendar-backward-year 1))))
  14880. (org-defkey map [?\e (shift down)]
  14881. (lambda () (interactive)
  14882. (org-eval-in-calendar '(calendar-forward-year 1))))
  14883. (org-defkey map [(shift up)]
  14884. (lambda () (interactive)
  14885. (org-eval-in-calendar '(calendar-backward-week 1))))
  14886. (org-defkey map [(shift down)]
  14887. (lambda () (interactive)
  14888. (org-eval-in-calendar '(calendar-forward-week 1))))
  14889. (org-defkey map [(shift left)]
  14890. (lambda () (interactive)
  14891. (org-eval-in-calendar '(calendar-backward-day 1))))
  14892. (org-defkey map [(shift right)]
  14893. (lambda () (interactive)
  14894. (org-eval-in-calendar '(calendar-forward-day 1))))
  14895. (org-defkey map "!"
  14896. (lambda () (interactive)
  14897. (org-eval-in-calendar '(diary-view-entries))
  14898. (message "")))
  14899. (org-defkey map ">"
  14900. (lambda () (interactive)
  14901. (org-eval-in-calendar '(scroll-calendar-left 1))))
  14902. (org-defkey map "<"
  14903. (lambda () (interactive)
  14904. (org-eval-in-calendar '(scroll-calendar-right 1))))
  14905. (org-defkey map "\C-v"
  14906. (lambda () (interactive)
  14907. (org-eval-in-calendar
  14908. '(calendar-scroll-left-three-months 1))))
  14909. (org-defkey map "\M-v"
  14910. (lambda () (interactive)
  14911. (org-eval-in-calendar
  14912. '(calendar-scroll-right-three-months 1))))
  14913. map)
  14914. "Keymap for minibuffer commands when using `org-read-date'.")
  14915. (defvar org-def)
  14916. (defvar org-defdecode)
  14917. (defvar org-with-time)
  14918. (defun org-read-date (&optional org-with-time to-time from-string prompt
  14919. default-time default-input inactive)
  14920. "Read a date, possibly a time, and make things smooth for the user.
  14921. The prompt will suggest to enter an ISO date, but you can also enter anything
  14922. which will at least partially be understood by `parse-time-string'.
  14923. Unrecognized parts of the date will default to the current day, month, year,
  14924. hour and minute. If this command is called to replace a timestamp at point,
  14925. or to enter the second timestamp of a range, the default time is taken
  14926. from the existing stamp. Furthermore, the command prefers the future,
  14927. so if you are giving a date where the year is not given, and the day-month
  14928. combination is already past in the current year, it will assume you
  14929. mean next year. For details, see the manual. A few examples:
  14930. 3-2-5 --> 2003-02-05
  14931. feb 15 --> currentyear-02-15
  14932. 2/15 --> currentyear-02-15
  14933. sep 12 9 --> 2009-09-12
  14934. 12:45 --> today 12:45
  14935. 22 sept 0:34 --> currentyear-09-22 0:34
  14936. 12 --> currentyear-currentmonth-12
  14937. Fri --> nearest Friday after today
  14938. -Tue --> last Tuesday
  14939. etc.
  14940. Furthermore you can specify a relative date by giving, as the *first* thing
  14941. in the input: a plus/minus sign, a number and a letter [hdwmy] to indicate
  14942. change in days weeks, months, years.
  14943. With a single plus or minus, the date is relative to today. With a double
  14944. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  14945. +4d --> four days from today
  14946. +4 --> same as above
  14947. +2w --> two weeks from today
  14948. ++5 --> five days from default date
  14949. The function understands only English month and weekday abbreviations.
  14950. While prompting, a calendar is popped up - you can also select the
  14951. date with the mouse (button 1). The calendar shows a period of three
  14952. months. To scroll it to other months, use the keys `>' and `<'.
  14953. If you don't like the calendar, turn it off with
  14954. \(setq org-read-date-popup-calendar nil)
  14955. With optional argument TO-TIME, the date will immediately be converted
  14956. to an internal time.
  14957. With an optional argument ORG-WITH-TIME, the prompt will suggest to
  14958. also insert a time. Note that when ORG-WITH-TIME is not set, you can
  14959. still enter a time, and this function will inform the calling routine
  14960. about this change. The calling routine may then choose to change the
  14961. format used to insert the time stamp into the buffer to include the time.
  14962. With optional argument FROM-STRING, read from this string instead from
  14963. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  14964. the time/date that is used for everything that is not specified by the
  14965. user."
  14966. (require 'parse-time)
  14967. (let* ((org-time-stamp-rounding-minutes
  14968. (if (equal org-with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  14969. (org-dcst org-display-custom-times)
  14970. (ct (org-current-time))
  14971. (org-def (or org-overriding-default-time default-time ct))
  14972. (org-defdecode (decode-time org-def))
  14973. (dummy (progn
  14974. (when (< (nth 2 org-defdecode) org-extend-today-until)
  14975. (setcar (nthcdr 2 org-defdecode) -1)
  14976. (setcar (nthcdr 1 org-defdecode) 59)
  14977. (setq org-def (apply 'encode-time org-defdecode)
  14978. org-defdecode (decode-time org-def)))))
  14979. (mouse-autoselect-window nil) ; Don't let the mouse jump
  14980. (calendar-frame-setup nil)
  14981. (calendar-setup nil)
  14982. (calendar-move-hook nil)
  14983. (calendar-view-diary-initially-flag nil)
  14984. (calendar-view-holidays-initially-flag nil)
  14985. (timestr (format-time-string
  14986. (if org-with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") org-def))
  14987. (prompt (concat (if prompt (concat prompt " ") "")
  14988. (format "Date+time [%s]: " timestr)))
  14989. ans (org-ans0 "") org-ans1 org-ans2 final)
  14990. (cond
  14991. (from-string (setq ans from-string))
  14992. (org-read-date-popup-calendar
  14993. (save-excursion
  14994. (save-window-excursion
  14995. (calendar)
  14996. (org-eval-in-calendar '(setq cursor-type nil) t)
  14997. (unwind-protect
  14998. (progn
  14999. (calendar-forward-day (- (time-to-days org-def)
  15000. (calendar-absolute-from-gregorian
  15001. (calendar-current-date))))
  15002. (org-eval-in-calendar nil t)
  15003. (let* ((old-map (current-local-map))
  15004. (map (copy-keymap calendar-mode-map))
  15005. (minibuffer-local-map
  15006. (copy-keymap org-read-date-minibuffer-local-map)))
  15007. (org-defkey map (kbd "RET") 'org-calendar-select)
  15008. (org-defkey map [mouse-1] 'org-calendar-select-mouse)
  15009. (org-defkey map [mouse-2] 'org-calendar-select-mouse)
  15010. (unwind-protect
  15011. (progn
  15012. (use-local-map map)
  15013. (setq org-read-date-inactive inactive)
  15014. (add-hook 'post-command-hook 'org-read-date-display)
  15015. (setq org-ans0 (read-string prompt default-input
  15016. 'org-read-date-history nil))
  15017. ;; org-ans0: from prompt
  15018. ;; org-ans1: from mouse click
  15019. ;; org-ans2: from calendar motion
  15020. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  15021. (remove-hook 'post-command-hook 'org-read-date-display)
  15022. (use-local-map old-map)
  15023. (when org-read-date-overlay
  15024. (delete-overlay org-read-date-overlay)
  15025. (setq org-read-date-overlay nil)))))
  15026. (bury-buffer "*Calendar*")))))
  15027. (t ; Naked prompt only
  15028. (unwind-protect
  15029. (setq ans (read-string prompt default-input
  15030. 'org-read-date-history timestr))
  15031. (when org-read-date-overlay
  15032. (delete-overlay org-read-date-overlay)
  15033. (setq org-read-date-overlay nil)))))
  15034. (setq final (org-read-date-analyze ans org-def org-defdecode))
  15035. (when org-read-date-analyze-forced-year
  15036. (message "Year was forced into %s"
  15037. (if org-read-date-force-compatible-dates
  15038. "compatible range (1970-2037)"
  15039. "range representable on this machine"))
  15040. (ding))
  15041. ;; One round trip to get rid of 34th of August and stuff like that....
  15042. (setq final (decode-time (apply 'encode-time final)))
  15043. (setq org-read-date-final-answer ans)
  15044. (if to-time
  15045. (apply 'encode-time final)
  15046. (if (and (boundp 'org-time-was-given) org-time-was-given)
  15047. (format "%04d-%02d-%02d %02d:%02d"
  15048. (nth 5 final) (nth 4 final) (nth 3 final)
  15049. (nth 2 final) (nth 1 final))
  15050. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  15051. (defun org-read-date-display ()
  15052. "Display the current date prompt interpretation in the minibuffer."
  15053. (when org-read-date-display-live
  15054. (when org-read-date-overlay
  15055. (delete-overlay org-read-date-overlay))
  15056. (when (minibufferp (current-buffer))
  15057. (save-excursion
  15058. (end-of-line 1)
  15059. (while (not (equal (buffer-substring
  15060. (max (point-min) (- (point) 4)) (point))
  15061. " "))
  15062. (insert " ")))
  15063. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  15064. " " (or org-ans1 org-ans2)))
  15065. (org-end-time-was-given nil)
  15066. (f (org-read-date-analyze ans org-def org-defdecode))
  15067. (fmts (if org-dcst
  15068. org-time-stamp-custom-formats
  15069. org-time-stamp-formats))
  15070. (fmt (if (or org-with-time
  15071. (and (boundp 'org-time-was-given) org-time-was-given))
  15072. (cdr fmts)
  15073. (car fmts)))
  15074. (txt (format-time-string fmt (apply 'encode-time f)))
  15075. (txt (if org-read-date-inactive (concat "[" (substring txt 1 -1) "]") txt))
  15076. (txt (concat "=> " txt)))
  15077. (when (and org-end-time-was-given
  15078. (string-match org-plain-time-of-day-regexp txt))
  15079. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  15080. org-end-time-was-given
  15081. (substring txt (match-end 0)))))
  15082. (when org-read-date-analyze-futurep
  15083. (setq txt (concat txt " (=>F)")))
  15084. (setq org-read-date-overlay
  15085. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  15086. (org-overlay-display org-read-date-overlay txt 'secondary-selection)))))
  15087. (defun org-read-date-analyze (ans org-def org-defdecode)
  15088. "Analyze the combined answer of the date prompt."
  15089. ;; FIXME: cleanup and comment
  15090. (let ((nowdecode (decode-time (current-time)))
  15091. delta deltan deltaw deltadef year month day
  15092. hour minute second wday pm h2 m2 tl wday1
  15093. iso-year iso-weekday iso-week iso-year iso-date futurep kill-year)
  15094. (setq org-read-date-analyze-futurep nil
  15095. org-read-date-analyze-forced-year nil)
  15096. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  15097. (setq ans "+0"))
  15098. (when (setq delta (org-read-date-get-relative ans (current-time) org-def))
  15099. (setq ans (replace-match "" t t ans)
  15100. deltan (car delta)
  15101. deltaw (nth 1 delta)
  15102. deltadef (nth 2 delta)))
  15103. ;; Check if there is an iso week date in there. If yes, store the
  15104. ;; info and postpone interpreting it until the rest of the parsing
  15105. ;; is done.
  15106. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  15107. (setq iso-year (if (match-end 1)
  15108. (org-small-year-to-year
  15109. (string-to-number (match-string 1 ans))))
  15110. iso-weekday (if (match-end 3)
  15111. (string-to-number (match-string 3 ans)))
  15112. iso-week (string-to-number (match-string 2 ans)))
  15113. (setq ans (replace-match "" t t ans)))
  15114. ;; Help matching ISO dates with single digit month or day, like 2006-8-11.
  15115. (when (string-match
  15116. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  15117. (setq year (if (match-end 2)
  15118. (string-to-number (match-string 2 ans))
  15119. (progn (setq kill-year t)
  15120. (string-to-number (format-time-string "%Y"))))
  15121. month (string-to-number (match-string 3 ans))
  15122. day (string-to-number (match-string 4 ans)))
  15123. (if (< year 100) (setq year (+ 2000 year)))
  15124. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  15125. t nil ans)))
  15126. ;; Help matching dotted european dates
  15127. (when (string-match
  15128. "^ *\\(3[01]\\|0?[1-9]\\|[12][0-9]\\)\\. ?\\(0?[1-9]\\|1[012]\\)\\.\\( ?[1-9][0-9]\\{3\\}\\)?" ans)
  15129. (setq year (if (match-end 3) (string-to-number (match-string 3 ans))
  15130. (setq kill-year t)
  15131. (string-to-number (format-time-string "%Y")))
  15132. day (string-to-number (match-string 1 ans))
  15133. month (string-to-number (match-string 2 ans))
  15134. ans (replace-match (format "%04d-%02d-%02d" year month day)
  15135. t nil ans)))
  15136. ;; Help matching american dates, like 5/30 or 5/30/7
  15137. (when (string-match
  15138. "^ *\\(0?[1-9]\\|1[012]\\)/\\(0?[1-9]\\|[12][0-9]\\|3[01]\\)\\(/\\([0-9]+\\)\\)?\\([^/0-9]\\|$\\)" ans)
  15139. (setq year (if (match-end 4)
  15140. (string-to-number (match-string 4 ans))
  15141. (progn (setq kill-year t)
  15142. (string-to-number (format-time-string "%Y"))))
  15143. month (string-to-number (match-string 1 ans))
  15144. day (string-to-number (match-string 2 ans)))
  15145. (if (< year 100) (setq year (+ 2000 year)))
  15146. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  15147. t nil ans)))
  15148. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  15149. ;; If there is a time with am/pm, and *no* time without it, we convert
  15150. ;; so that matching will be successful.
  15151. (loop for i from 1 to 2 do ; twice, for end time as well
  15152. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  15153. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  15154. (setq hour (string-to-number (match-string 1 ans))
  15155. minute (if (match-end 3)
  15156. (string-to-number (match-string 3 ans))
  15157. 0)
  15158. pm (equal ?p
  15159. (string-to-char (downcase (match-string 4 ans)))))
  15160. (if (and (= hour 12) (not pm))
  15161. (setq hour 0)
  15162. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  15163. (setq ans (replace-match (format "%02d:%02d" hour minute)
  15164. t t ans))))
  15165. ;; Check if a time range is given as a duration
  15166. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  15167. (setq hour (string-to-number (match-string 1 ans))
  15168. h2 (+ hour (string-to-number (match-string 3 ans)))
  15169. minute (string-to-number (match-string 2 ans))
  15170. m2 (+ minute (if (match-end 5) (string-to-number
  15171. (match-string 5 ans))0)))
  15172. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  15173. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  15174. t t ans)))
  15175. ;; Check if there is a time range
  15176. (when (boundp 'org-end-time-was-given)
  15177. (setq org-time-was-given nil)
  15178. (when (and (string-match org-plain-time-of-day-regexp ans)
  15179. (match-end 8))
  15180. (setq org-end-time-was-given (match-string 8 ans))
  15181. (setq ans (concat (substring ans 0 (match-beginning 7))
  15182. (substring ans (match-end 7))))))
  15183. (setq tl (parse-time-string ans)
  15184. day (or (nth 3 tl) (nth 3 org-defdecode))
  15185. month (or (nth 4 tl)
  15186. (if (and org-read-date-prefer-future
  15187. (nth 3 tl) (< (nth 3 tl) (nth 3 nowdecode)))
  15188. (prog1 (1+ (nth 4 nowdecode)) (setq futurep t))
  15189. (nth 4 org-defdecode)))
  15190. year (or (and (not kill-year) (nth 5 tl))
  15191. (if (and org-read-date-prefer-future
  15192. (nth 4 tl) (< (nth 4 tl) (nth 4 nowdecode)))
  15193. (prog1 (1+ (nth 5 nowdecode)) (setq futurep t))
  15194. (nth 5 org-defdecode)))
  15195. hour (or (nth 2 tl) (nth 2 org-defdecode))
  15196. minute (or (nth 1 tl) (nth 1 org-defdecode))
  15197. second (or (nth 0 tl) 0)
  15198. wday (nth 6 tl))
  15199. (when (and (eq org-read-date-prefer-future 'time)
  15200. (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
  15201. (equal day (nth 3 nowdecode))
  15202. (equal month (nth 4 nowdecode))
  15203. (equal year (nth 5 nowdecode))
  15204. (nth 2 tl)
  15205. (or (< (nth 2 tl) (nth 2 nowdecode))
  15206. (and (= (nth 2 tl) (nth 2 nowdecode))
  15207. (nth 1 tl)
  15208. (< (nth 1 tl) (nth 1 nowdecode)))))
  15209. (setq day (1+ day)
  15210. futurep t))
  15211. ;; Special date definitions below
  15212. (cond
  15213. (iso-week
  15214. ;; There was an iso week
  15215. (require 'cal-iso)
  15216. (setq futurep nil)
  15217. (setq year (or iso-year year)
  15218. day (or iso-weekday wday 1)
  15219. wday nil ; to make sure that the trigger below does not match
  15220. iso-date (calendar-gregorian-from-absolute
  15221. (calendar-absolute-from-iso
  15222. (list iso-week day year))))
  15223. ; FIXME: Should we also push ISO weeks into the future?
  15224. ; (when (and org-read-date-prefer-future
  15225. ; (not iso-year)
  15226. ; (< (calendar-absolute-from-gregorian iso-date)
  15227. ; (time-to-days (current-time))))
  15228. ; (setq year (1+ year)
  15229. ; iso-date (calendar-gregorian-from-absolute
  15230. ; (calendar-absolute-from-iso
  15231. ; (list iso-week day year)))))
  15232. (setq month (car iso-date)
  15233. year (nth 2 iso-date)
  15234. day (nth 1 iso-date)))
  15235. (deltan
  15236. (setq futurep nil)
  15237. (unless deltadef
  15238. (let ((now (decode-time (current-time))))
  15239. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  15240. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  15241. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  15242. ((equal deltaw "m") (setq month (+ month deltan)))
  15243. ((equal deltaw "y") (setq year (+ year deltan)))))
  15244. ((and wday (not (nth 3 tl)))
  15245. ;; Weekday was given, but no day, so pick that day in the week
  15246. ;; on or after the derived date.
  15247. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  15248. (unless (equal wday wday1)
  15249. (setq day (+ day (% (- wday wday1 -7) 7))))))
  15250. (if (and (boundp 'org-time-was-given)
  15251. (nth 2 tl))
  15252. (setq org-time-was-given t))
  15253. (if (< year 100) (setq year (+ 2000 year)))
  15254. ;; Check of the date is representable
  15255. (if org-read-date-force-compatible-dates
  15256. (progn
  15257. (if (< year 1970)
  15258. (setq year 1970 org-read-date-analyze-forced-year t))
  15259. (if (> year 2037)
  15260. (setq year 2037 org-read-date-analyze-forced-year t)))
  15261. (condition-case nil
  15262. (ignore (encode-time second minute hour day month year))
  15263. (error
  15264. (setq year (nth 5 org-defdecode))
  15265. (setq org-read-date-analyze-forced-year t))))
  15266. (setq org-read-date-analyze-futurep futurep)
  15267. (list second minute hour day month year)))
  15268. (defvar parse-time-weekdays)
  15269. (defun org-read-date-get-relative (s today default)
  15270. "Check string S for special relative date string.
  15271. TODAY and DEFAULT are internal times, for today and for a default.
  15272. Return shift list (N what def-flag)
  15273. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  15274. N is the number of WHATs to shift.
  15275. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  15276. the DEFAULT date rather than TODAY."
  15277. (require 'parse-time)
  15278. (when (and
  15279. (string-match
  15280. (concat
  15281. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  15282. "\\([0-9]+\\)?"
  15283. "\\([hdwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  15284. "\\([ \t]\\|$\\)") s)
  15285. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  15286. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  15287. (string-to-char (substring (match-string 1 s) -1))
  15288. ?+))
  15289. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  15290. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  15291. (what (if (match-end 3) (match-string 3 s) "d"))
  15292. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  15293. (date (if rel default today))
  15294. (wday (nth 6 (decode-time date)))
  15295. delta)
  15296. (if wday1
  15297. (progn
  15298. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  15299. (if (= delta 0) (setq delta 7))
  15300. (if (= dir ?-)
  15301. (progn
  15302. (setq delta (- delta 7))
  15303. (if (= delta 0) (setq delta -7))))
  15304. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  15305. (list delta "d" rel))
  15306. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  15307. (defun org-order-calendar-date-args (arg1 arg2 arg3)
  15308. "Turn a user-specified date into the internal representation.
  15309. The internal representation needed by the calendar is (month day year).
  15310. This is a wrapper to handle the brain-dead convention in calendar that
  15311. user function argument order change dependent on argument order."
  15312. (if (boundp 'calendar-date-style)
  15313. (cond
  15314. ((eq calendar-date-style 'american)
  15315. (list arg1 arg2 arg3))
  15316. ((eq calendar-date-style 'european)
  15317. (list arg2 arg1 arg3))
  15318. ((eq calendar-date-style 'iso)
  15319. (list arg2 arg3 arg1)))
  15320. (org-no-warnings ;; european-calendar-style is obsolete as of version 23.1
  15321. (if (org-bound-and-true-p european-calendar-style)
  15322. (list arg2 arg1 arg3)
  15323. (list arg1 arg2 arg3)))))
  15324. (defun org-eval-in-calendar (form &optional keepdate)
  15325. "Eval FORM in the calendar window and return to current window.
  15326. When KEEPDATE is non-nil, update `org-ans2' from the cursor date,
  15327. otherwise stick to the current value of `org-ans2'."
  15328. (let ((sf (selected-frame))
  15329. (sw (selected-window)))
  15330. (select-window (get-buffer-window "*Calendar*" t))
  15331. (eval form)
  15332. (when (and (not keepdate) (calendar-cursor-to-date))
  15333. (let* ((date (calendar-cursor-to-date))
  15334. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15335. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  15336. (move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  15337. (select-window sw)
  15338. (org-select-frame-set-input-focus sf)))
  15339. (defun org-calendar-select ()
  15340. "Return to `org-read-date' with the date currently selected.
  15341. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  15342. (interactive)
  15343. (when (calendar-cursor-to-date)
  15344. (let* ((date (calendar-cursor-to-date))
  15345. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15346. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  15347. (if (active-minibuffer-window) (exit-minibuffer))))
  15348. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  15349. "Insert a date stamp for the date given by the internal TIME.
  15350. WITH-HM means use the stamp format that includes the time of the day.
  15351. INACTIVE means use square brackets instead of angular ones, so that the
  15352. stamp will not contribute to the agenda.
  15353. PRE and POST are optional strings to be inserted before and after the
  15354. stamp.
  15355. The command returns the inserted time stamp."
  15356. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  15357. stamp)
  15358. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  15359. (insert-before-markers (or pre ""))
  15360. (when (listp extra)
  15361. (setq extra (car extra))
  15362. (if (and (stringp extra)
  15363. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  15364. (setq extra (format "-%02d:%02d"
  15365. (string-to-number (match-string 1 extra))
  15366. (string-to-number (match-string 2 extra))))
  15367. (setq extra nil)))
  15368. (when extra
  15369. (setq fmt (concat (substring fmt 0 -1) extra (substring fmt -1))))
  15370. (insert-before-markers (setq stamp (format-time-string fmt time)))
  15371. (insert-before-markers (or post ""))
  15372. (setq org-last-inserted-timestamp stamp)))
  15373. (defun org-toggle-time-stamp-overlays ()
  15374. "Toggle the use of custom time stamp formats."
  15375. (interactive)
  15376. (setq org-display-custom-times (not org-display-custom-times))
  15377. (unless org-display-custom-times
  15378. (let ((p (point-min)) (bmp (buffer-modified-p)))
  15379. (while (setq p (next-single-property-change p 'display))
  15380. (if (and (get-text-property p 'display)
  15381. (eq (get-text-property p 'face) 'org-date))
  15382. (remove-text-properties
  15383. p (setq p (next-single-property-change p 'display))
  15384. '(display t))))
  15385. (set-buffer-modified-p bmp)))
  15386. (if (featurep 'xemacs)
  15387. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  15388. (org-restart-font-lock)
  15389. (setq org-table-may-need-update t)
  15390. (if org-display-custom-times
  15391. (message "Time stamps are overlaid with custom format")
  15392. (message "Time stamp overlays removed")))
  15393. (defun org-display-custom-time (beg end)
  15394. "Overlay modified time stamp format over timestamp between BEG and END."
  15395. (let* ((ts (buffer-substring beg end))
  15396. t1 w1 with-hm tf time str w2 (off 0))
  15397. (save-match-data
  15398. (setq t1 (org-parse-time-string ts t))
  15399. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)?\\'" ts)
  15400. (setq off (- (match-end 0) (match-beginning 0)))))
  15401. (setq end (- end off))
  15402. (setq w1 (- end beg)
  15403. with-hm (and (nth 1 t1) (nth 2 t1))
  15404. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  15405. time (org-fix-decoded-time t1)
  15406. str (org-add-props
  15407. (format-time-string
  15408. (substring tf 1 -1) (apply 'encode-time time))
  15409. nil 'mouse-face 'highlight)
  15410. w2 (length str))
  15411. (if (not (= w2 w1))
  15412. (add-text-properties (1+ beg) (+ 2 beg)
  15413. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  15414. (if (featurep 'xemacs)
  15415. (progn
  15416. (put-text-property beg end 'invisible t)
  15417. (put-text-property beg end 'end-glyph (make-glyph str)))
  15418. (put-text-property beg end 'display str))))
  15419. (defun org-translate-time (string)
  15420. "Translate all timestamps in STRING to custom format.
  15421. But do this only if the variable `org-display-custom-times' is set."
  15422. (when org-display-custom-times
  15423. (save-match-data
  15424. (let* ((start 0)
  15425. (re org-ts-regexp-both)
  15426. t1 with-hm inactive tf time str beg end)
  15427. (while (setq start (string-match re string start))
  15428. (setq beg (match-beginning 0)
  15429. end (match-end 0)
  15430. t1 (save-match-data
  15431. (org-parse-time-string (substring string beg end) t))
  15432. with-hm (and (nth 1 t1) (nth 2 t1))
  15433. inactive (equal (substring string beg (1+ beg)) "[")
  15434. tf (funcall (if with-hm 'cdr 'car)
  15435. org-time-stamp-custom-formats)
  15436. time (org-fix-decoded-time t1)
  15437. str (format-time-string
  15438. (concat
  15439. (if inactive "[" "<") (substring tf 1 -1)
  15440. (if inactive "]" ">"))
  15441. (apply 'encode-time time))
  15442. string (replace-match str t t string)
  15443. start (+ start (length str)))))))
  15444. string)
  15445. (defun org-fix-decoded-time (time)
  15446. "Set 0 instead of nil for the first 6 elements of time.
  15447. Don't touch the rest."
  15448. (let ((n 0))
  15449. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  15450. (define-obsolete-function-alias 'org-days-to-time 'org-time-stamp-to-now "24.4")
  15451. (defun org-time-stamp-to-now (timestamp-string &optional seconds)
  15452. "Difference between TIMESTAMP-STRING and now in days.
  15453. If SECONDS is non-nil, return the difference in seconds."
  15454. (let ((fdiff (if seconds 'org-float-time 'time-to-days)))
  15455. (- (funcall fdiff (org-time-string-to-time timestamp-string))
  15456. (funcall fdiff (current-time)))))
  15457. (defun org-deadline-close (timestamp-string &optional ndays)
  15458. "Is the time in TIMESTAMP-STRING close to the current date?"
  15459. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  15460. (and (< (org-time-stamp-to-now timestamp-string) ndays)
  15461. (not (org-entry-is-done-p))))
  15462. (defun org-get-wdays (ts &optional delay zero-delay)
  15463. "Get the deadline lead time appropriate for timestring TS.
  15464. When DELAY is non-nil, get the delay time for scheduled items
  15465. instead of the deadline lead time. When ZERO-DELAY is non-nil
  15466. and `org-scheduled-delay-days' is 0, enforce 0 as the delay,
  15467. don't try to find the delay cookie in the scheduled timestamp."
  15468. (let ((tv (if delay org-scheduled-delay-days
  15469. org-deadline-warning-days)))
  15470. (cond
  15471. ((or (and delay (< tv 0))
  15472. (and delay zero-delay (<= tv 0))
  15473. (and (not delay) (<= tv 0)))
  15474. ;; Enforce this value no matter what
  15475. (- tv))
  15476. ((string-match "-\\([0-9]+\\)\\([hdwmy]\\)\\(\\'\\|>\\| \\)" ts)
  15477. ;; lead time is specified.
  15478. (floor (* (string-to-number (match-string 1 ts))
  15479. (cdr (assoc (match-string 2 ts)
  15480. '(("d" . 1) ("w" . 7)
  15481. ("m" . 30.4) ("y" . 365.25)
  15482. ("h" . 0.041667)))))))
  15483. ;; go for the default.
  15484. (t tv))))
  15485. (defun org-calendar-select-mouse (ev)
  15486. "Return to `org-read-date' with the date currently selected.
  15487. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  15488. (interactive "e")
  15489. (mouse-set-point ev)
  15490. (when (calendar-cursor-to-date)
  15491. (let* ((date (calendar-cursor-to-date))
  15492. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15493. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  15494. (if (active-minibuffer-window) (exit-minibuffer))))
  15495. (defun org-check-deadlines (ndays)
  15496. "Check if there are any deadlines due or past due.
  15497. A deadline is considered due if it happens within `org-deadline-warning-days'
  15498. days from today's date. If the deadline appears in an entry marked DONE,
  15499. it is not shown. The prefix arg NDAYS can be used to test that many
  15500. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  15501. (interactive "P")
  15502. (let* ((org-warn-days
  15503. (cond
  15504. ((equal ndays '(4)) 100000)
  15505. (ndays (prefix-numeric-value ndays))
  15506. (t (abs org-deadline-warning-days))))
  15507. (case-fold-search nil)
  15508. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  15509. (callback
  15510. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  15511. (message "%d deadlines past-due or due within %d days"
  15512. (org-occur regexp nil callback)
  15513. org-warn-days)))
  15514. (defsubst org-re-timestamp (type)
  15515. "Return a regexp for timestamp TYPE.
  15516. Allowed values for TYPE are:
  15517. all: all timestamps
  15518. active: only active timestamps (<...>)
  15519. inactive: only inactive timestamps ([...])
  15520. scheduled: only scheduled timestamps
  15521. deadline: only deadline timestamps
  15522. closed: only closed time-stamps
  15523. When TYPE is nil, fall back on returning a regexp that matches
  15524. both scheduled and deadline timestamps."
  15525. (cond ((eq type 'all) "\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}\\(?: +[^]+0-9> \n -]+\\)?\\(?: +[0-9]\\{1,2\\}:[0-9]\\{2\\}\\)?\\)")
  15526. ((eq type 'active) org-ts-regexp)
  15527. ((eq type 'inactive) "\\[\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^ \n>]*?\\)\\]")
  15528. ((eq type 'scheduled) (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>"))
  15529. ((eq type 'deadline) (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  15530. ((eq type 'closed) (concat org-closed-string " \\[\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^ \n>]*?\\)\\]"))
  15531. ((eq type 'scheduled-or-deadline)
  15532. (concat "\\<\\(?:" org-deadline-string "\\|" org-scheduled-string "\\) *<\\([^>]+\\)>"))))
  15533. (defun org-check-before-date (date)
  15534. "Check if there are deadlines or scheduled entries before DATE."
  15535. (interactive (list (org-read-date)))
  15536. (let ((case-fold-search nil)
  15537. (regexp (org-re-timestamp org-ts-type))
  15538. (callback
  15539. (lambda () (time-less-p
  15540. (org-time-string-to-time (match-string 1))
  15541. (org-time-string-to-time date)))))
  15542. (message "%d entries before %s"
  15543. (org-occur regexp nil callback) date)))
  15544. (defun org-check-after-date (date)
  15545. "Check if there are deadlines or scheduled entries after DATE."
  15546. (interactive (list (org-read-date)))
  15547. (let ((case-fold-search nil)
  15548. (regexp (org-re-timestamp org-ts-type))
  15549. (callback
  15550. (lambda () (not
  15551. (time-less-p
  15552. (org-time-string-to-time (match-string 1))
  15553. (org-time-string-to-time date))))))
  15554. (message "%d entries after %s"
  15555. (org-occur regexp nil callback) date)))
  15556. (defun org-check-dates-range (start-date end-date)
  15557. "Check for deadlines/scheduled entries between START-DATE and END-DATE."
  15558. (interactive (list (org-read-date nil nil nil "Range starts")
  15559. (org-read-date nil nil nil "Range end")))
  15560. (let ((case-fold-search nil)
  15561. (regexp (org-re-timestamp org-ts-type))
  15562. (callback
  15563. (lambda ()
  15564. (let ((match (match-string 1)))
  15565. (and
  15566. (not (time-less-p
  15567. (org-time-string-to-time match)
  15568. (org-time-string-to-time start-date)))
  15569. (time-less-p
  15570. (org-time-string-to-time match)
  15571. (org-time-string-to-time end-date)))))))
  15572. (message "%d entries between %s and %s"
  15573. (org-occur regexp nil callback) start-date end-date)))
  15574. (defun org-evaluate-time-range (&optional to-buffer)
  15575. "Evaluate a time range by computing the difference between start and end.
  15576. Normally the result is just printed in the echo area, but with prefix arg
  15577. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  15578. If the time range is actually in a table, the result is inserted into the
  15579. next column.
  15580. For time difference computation, a year is assumed to be exactly 365
  15581. days in order to avoid rounding problems."
  15582. (interactive "P")
  15583. (or
  15584. (org-clock-update-time-maybe)
  15585. (save-excursion
  15586. (unless (org-at-date-range-p t)
  15587. (goto-char (point-at-bol))
  15588. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  15589. (if (not (org-at-date-range-p t))
  15590. (user-error "Not at a time-stamp range, and none found in current line")))
  15591. (let* ((ts1 (match-string 1))
  15592. (ts2 (match-string 2))
  15593. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  15594. (match-end (match-end 0))
  15595. (time1 (org-time-string-to-time ts1))
  15596. (time2 (org-time-string-to-time ts2))
  15597. (t1 (org-float-time time1))
  15598. (t2 (org-float-time time2))
  15599. (diff (abs (- t2 t1)))
  15600. (negative (< (- t2 t1) 0))
  15601. ;; (ys (floor (* 365 24 60 60)))
  15602. (ds (* 24 60 60))
  15603. (hs (* 60 60))
  15604. (fy "%dy %dd %02d:%02d")
  15605. (fy1 "%dy %dd")
  15606. (fd "%dd %02d:%02d")
  15607. (fd1 "%dd")
  15608. (fh "%02d:%02d")
  15609. y d h m align)
  15610. (if havetime
  15611. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  15612. y 0
  15613. d (floor (/ diff ds)) diff (mod diff ds)
  15614. h (floor (/ diff hs)) diff (mod diff hs)
  15615. m (floor (/ diff 60)))
  15616. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  15617. y 0
  15618. d (floor (+ (/ diff ds) 0.5))
  15619. h 0 m 0))
  15620. (if (not to-buffer)
  15621. (message "%s" (org-make-tdiff-string y d h m))
  15622. (if (org-at-table-p)
  15623. (progn
  15624. (goto-char match-end)
  15625. (setq align t)
  15626. (and (looking-at " *|") (goto-char (match-end 0))))
  15627. (goto-char match-end))
  15628. (if (looking-at
  15629. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  15630. (replace-match ""))
  15631. (if negative (insert " -"))
  15632. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  15633. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  15634. (insert " " (format fh h m))))
  15635. (if align (org-table-align))
  15636. (message "Time difference inserted")))))
  15637. (defun org-make-tdiff-string (y d h m)
  15638. (let ((fmt "")
  15639. (l nil))
  15640. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  15641. l (push y l)))
  15642. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  15643. l (push d l)))
  15644. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  15645. l (push h l)))
  15646. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  15647. l (push m l)))
  15648. (apply 'format fmt (nreverse l))))
  15649. (defun org-time-string-to-time (s &optional buffer pos)
  15650. "Convert a timestamp string into internal time."
  15651. (condition-case errdata
  15652. (apply 'encode-time (org-parse-time-string s))
  15653. (error (error "Bad timestamp `%s'%s\nError was: %s"
  15654. s (if (not (and buffer pos))
  15655. ""
  15656. (format " at %d in buffer `%s'" pos buffer))
  15657. (cdr errdata)))))
  15658. (defun org-time-string-to-seconds (s)
  15659. "Convert a timestamp string to a number of seconds."
  15660. (org-float-time (org-time-string-to-time s)))
  15661. (defun org-time-string-to-absolute (s &optional daynr prefer show-all buffer pos)
  15662. "Convert a time stamp to an absolute day number.
  15663. If there is a specifier for a cyclic time stamp, get the closest
  15664. date to DAYNR.
  15665. PREFER and SHOW-ALL are passed through to `org-closest-date'.
  15666. The variable `date' is bound by the calendar when this is called."
  15667. (cond
  15668. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  15669. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  15670. daynr
  15671. (+ daynr 1000)))
  15672. ((and daynr (string-match "\\+[0-9]+[hdwmy]" s))
  15673. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  15674. (time-to-days (current-time))) (match-string 0 s)
  15675. prefer show-all))
  15676. (t (time-to-days
  15677. (condition-case errdata
  15678. (apply 'encode-time (org-parse-time-string s))
  15679. (error (error "Bad timestamp `%s'%s\nError was: %s"
  15680. s (if (not (and buffer pos))
  15681. ""
  15682. (format " at %d in buffer `%s'" pos buffer))
  15683. (cdr errdata))))))))
  15684. (defun org-days-to-iso-week (days)
  15685. "Return the iso week number."
  15686. (require 'cal-iso)
  15687. (car (calendar-iso-from-absolute days)))
  15688. (defun org-small-year-to-year (year)
  15689. "Convert 2-digit years into 4-digit years.
  15690. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2037.
  15691. The year 2000 cannot be abbreviated. Any year larger than 99
  15692. is returned unchanged."
  15693. (if (< year 38)
  15694. (setq year (+ 2000 year))
  15695. (if (< year 100)
  15696. (setq year (+ 1900 year))))
  15697. year)
  15698. (defun org-time-from-absolute (d)
  15699. "Return the time corresponding to date D.
  15700. D may be an absolute day number, or a calendar-type list (month day year)."
  15701. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  15702. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  15703. (defun org-calendar-holiday ()
  15704. "List of holidays, for Diary display in Org-mode."
  15705. (require 'holidays)
  15706. (let ((hl (funcall
  15707. (if (fboundp 'calendar-check-holidays)
  15708. 'calendar-check-holidays 'check-calendar-holidays) date)))
  15709. (if hl (mapconcat 'identity hl "; "))))
  15710. (defun org-diary-sexp-entry (sexp entry date)
  15711. "Process a SEXP diary ENTRY for DATE."
  15712. (require 'diary-lib)
  15713. (let ((result (if calendar-debug-sexp
  15714. (let ((stack-trace-on-error t))
  15715. (eval (car (read-from-string sexp))))
  15716. (condition-case nil
  15717. (eval (car (read-from-string sexp)))
  15718. (error
  15719. (beep)
  15720. (message "Bad sexp at line %d in %s: %s"
  15721. (org-current-line)
  15722. (buffer-file-name) sexp)
  15723. (sleep-for 2))))))
  15724. (cond ((stringp result) (split-string result "; "))
  15725. ((and (consp result)
  15726. (not (consp (cdr result)))
  15727. (stringp (cdr result))) (cdr result))
  15728. ((and (consp result)
  15729. (stringp (car result))) result)
  15730. (result entry))))
  15731. (defun org-diary-to-ical-string (frombuf)
  15732. "Get iCalendar entries from diary entries in buffer FROMBUF.
  15733. This uses the icalendar.el library."
  15734. (let* ((tmpdir (if (featurep 'xemacs)
  15735. (temp-directory)
  15736. temporary-file-directory))
  15737. (tmpfile (make-temp-name
  15738. (expand-file-name "orgics" tmpdir)))
  15739. buf rtn b e)
  15740. (with-current-buffer frombuf
  15741. (icalendar-export-region (point-min) (point-max) tmpfile)
  15742. (setq buf (find-buffer-visiting tmpfile))
  15743. (set-buffer buf)
  15744. (goto-char (point-min))
  15745. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  15746. (setq b (match-beginning 0)))
  15747. (goto-char (point-max))
  15748. (if (re-search-backward "^END:VEVENT" nil t)
  15749. (setq e (match-end 0)))
  15750. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  15751. (kill-buffer buf)
  15752. (delete-file tmpfile)
  15753. rtn))
  15754. (defun org-closest-date (start current change prefer show-all)
  15755. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  15756. When PREFER is `past', return a date that is either CURRENT or past.
  15757. When PREFER is `future', return a date that is either CURRENT or future.
  15758. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  15759. ;; Make the proper lists from the dates
  15760. (catch 'exit
  15761. (let ((a1 '(("h" . hour)
  15762. ("d" . day)
  15763. ("w" . week)
  15764. ("m" . month)
  15765. ("y" . year)))
  15766. (shour (nth 2 (org-parse-time-string start)))
  15767. dn dw sday cday n1 n2 n0
  15768. d m y y1 y2 date1 date2 nmonths nm ny m2)
  15769. (setq start (org-date-to-gregorian start)
  15770. current (org-date-to-gregorian
  15771. (if show-all
  15772. current
  15773. (time-to-days (current-time))))
  15774. sday (calendar-absolute-from-gregorian start)
  15775. cday (calendar-absolute-from-gregorian current))
  15776. (if (<= cday sday) (throw 'exit sday))
  15777. (if (string-match "\\(\\+[0-9]+\\)\\([hdwmy]\\)" change)
  15778. (setq dn (string-to-number (match-string 1 change))
  15779. dw (cdr (assoc (match-string 2 change) a1)))
  15780. (user-error "Invalid change specifier: %s" change))
  15781. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  15782. (cond
  15783. ((eq dw 'hour)
  15784. (let ((missing-hours
  15785. (mod (+ (- (* 24 (- cday sday)) shour) org-extend-today-until)
  15786. dn)))
  15787. (setq n1 (if (zerop missing-hours) cday
  15788. (- cday (1+ (floor (/ missing-hours 24)))))
  15789. n2 (+ cday (floor (/ (- dn missing-hours) 24))))))
  15790. ((eq dw 'day)
  15791. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  15792. n2 (+ n1 dn)))
  15793. ((eq dw 'year)
  15794. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  15795. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  15796. (setq date1 (list m d y1)
  15797. n1 (calendar-absolute-from-gregorian date1)
  15798. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  15799. n2 (calendar-absolute-from-gregorian date2)))
  15800. ((eq dw 'month)
  15801. ;; approx number of month between the two dates
  15802. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  15803. ;; How often does dn fit in there?
  15804. (setq d (nth 1 start) m (car start) y (nth 2 start)
  15805. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  15806. m (+ m nm)
  15807. ny (floor (/ m 12))
  15808. y (+ y ny)
  15809. m (- m (* ny 12)))
  15810. (while (> m 12) (setq m (- m 12) y (1+ y)))
  15811. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  15812. (setq m2 (+ m dn) y2 y)
  15813. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  15814. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  15815. (while (<= n2 cday)
  15816. (setq n1 n2 m m2 y y2)
  15817. (setq m2 (+ m dn) y2 y)
  15818. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  15819. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  15820. ;; Make sure n1 is the earlier date
  15821. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  15822. (if show-all
  15823. (cond
  15824. ((eq prefer 'past) (if (= cday n2) n2 n1))
  15825. ((eq prefer 'future) (if (= cday n1) n1 n2))
  15826. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  15827. (cond
  15828. ((eq prefer 'past) (if (= cday n2) n2 n1))
  15829. ((eq prefer 'future) (if (= cday n1) n1 n2))
  15830. (t (if (= cday n1) n1 n2)))))))
  15831. (defun org-date-to-gregorian (date)
  15832. "Turn any specification of DATE into a Gregorian date for the calendar."
  15833. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  15834. ((and (listp date) (= (length date) 3)) date)
  15835. ((stringp date)
  15836. (setq date (org-parse-time-string date))
  15837. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  15838. ((listp date)
  15839. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  15840. (defun org-parse-time-string (s &optional nodefault)
  15841. "Parse the standard Org-mode time string.
  15842. This should be a lot faster than the normal `parse-time-string'.
  15843. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  15844. hour and minute fields will be nil if not given."
  15845. (cond ((string-match org-ts-regexp0 s)
  15846. (list 0
  15847. (if (or (match-beginning 8) (not nodefault))
  15848. (string-to-number (or (match-string 8 s) "0")))
  15849. (if (or (match-beginning 7) (not nodefault))
  15850. (string-to-number (or (match-string 7 s) "0")))
  15851. (string-to-number (match-string 4 s))
  15852. (string-to-number (match-string 3 s))
  15853. (string-to-number (match-string 2 s))
  15854. nil nil nil))
  15855. ((string-match "^<[^>]+>$" s)
  15856. (decode-time (seconds-to-time (org-matcher-time s))))
  15857. (t (error "Not a standard Org-mode time string: %s" s))))
  15858. (defun org-timestamp-up (&optional arg)
  15859. "Increase the date item at the cursor by one.
  15860. If the cursor is on the year, change the year. If it is on the month,
  15861. the day or the time, change that.
  15862. With prefix ARG, change by that many units."
  15863. (interactive "p")
  15864. (org-timestamp-change (prefix-numeric-value arg) nil 'updown))
  15865. (defun org-timestamp-down (&optional arg)
  15866. "Decrease the date item at the cursor by one.
  15867. If the cursor is on the year, change the year. If it is on the month,
  15868. the day or the time, change that.
  15869. With prefix ARG, change by that many units."
  15870. (interactive "p")
  15871. (org-timestamp-change (- (prefix-numeric-value arg)) nil 'updown))
  15872. (defun org-timestamp-up-day (&optional arg)
  15873. "Increase the date in the time stamp by one day.
  15874. With prefix ARG, change that many days."
  15875. (interactive "p")
  15876. (if (and (not (org-at-timestamp-p t))
  15877. (org-at-heading-p))
  15878. (org-todo 'up)
  15879. (org-timestamp-change (prefix-numeric-value arg) 'day 'updown)))
  15880. (defun org-timestamp-down-day (&optional arg)
  15881. "Decrease the date in the time stamp by one day.
  15882. With prefix ARG, change that many days."
  15883. (interactive "p")
  15884. (if (and (not (org-at-timestamp-p t))
  15885. (org-at-heading-p))
  15886. (org-todo 'down)
  15887. (org-timestamp-change (- (prefix-numeric-value arg)) 'day) 'updown))
  15888. (defun org-at-timestamp-p (&optional inactive-ok)
  15889. "Determine if the cursor is in or at a timestamp."
  15890. (interactive)
  15891. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  15892. (pos (point))
  15893. (ans (or (looking-at tsr)
  15894. (save-excursion
  15895. (skip-chars-backward "^[<\n\r\t")
  15896. (if (> (point) (point-min)) (backward-char 1))
  15897. (and (looking-at tsr)
  15898. (> (- (match-end 0) pos) -1))))))
  15899. (and ans
  15900. (boundp 'org-ts-what)
  15901. (setq org-ts-what
  15902. (cond
  15903. ((= pos (match-beginning 0)) 'bracket)
  15904. ;; Point is considered to be "on the bracket" whether
  15905. ;; it's really on it or right after it.
  15906. ((= pos (1- (match-end 0))) 'bracket)
  15907. ((= pos (match-end 0)) 'after)
  15908. ((org-pos-in-match-range pos 2) 'year)
  15909. ((org-pos-in-match-range pos 3) 'month)
  15910. ((org-pos-in-match-range pos 7) 'hour)
  15911. ((org-pos-in-match-range pos 8) 'minute)
  15912. ((or (org-pos-in-match-range pos 4)
  15913. (org-pos-in-match-range pos 5)) 'day)
  15914. ((and (> pos (or (match-end 8) (match-end 5)))
  15915. (< pos (match-end 0)))
  15916. (- pos (or (match-end 8) (match-end 5))))
  15917. (t 'day))))
  15918. ans))
  15919. (defun org-toggle-timestamp-type ()
  15920. "Toggle the type (<active> or [inactive]) of a time stamp."
  15921. (interactive)
  15922. (when (org-at-timestamp-p t)
  15923. (let ((beg (match-beginning 0)) (end (match-end 0))
  15924. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  15925. (save-excursion
  15926. (goto-char beg)
  15927. (while (re-search-forward "[][<>]" end t)
  15928. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  15929. t t)))
  15930. (message "Timestamp is now %sactive"
  15931. (if (equal (char-after beg) ?<) "" "in")))))
  15932. (defun org-at-clock-log-p nil
  15933. "Is the cursor on the clock log line?"
  15934. (save-excursion
  15935. (move-beginning-of-line 1)
  15936. (looking-at "^[ \t]*CLOCK:")))
  15937. (defvar org-clock-history) ; defined in org-clock.el
  15938. (defvar org-clock-adjust-closest nil) ; defined in org-clock.el
  15939. (defun org-timestamp-change (n &optional what updown suppress-tmp-delay)
  15940. "Change the date in the time stamp at point.
  15941. The date will be changed by N times WHAT. WHAT can be `day', `month',
  15942. `year', `minute', `second'. If WHAT is not given, the cursor position
  15943. in the timestamp determines what will be changed.
  15944. When SUPPRESS-TMP-DELAY is non-nil, suppress delays like \"--2d\"."
  15945. (let ((origin (point)) origin-cat
  15946. with-hm inactive
  15947. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  15948. org-ts-what
  15949. extra rem
  15950. ts time time0 fixnext clrgx)
  15951. (if (not (org-at-timestamp-p t))
  15952. (user-error "Not at a timestamp"))
  15953. (if (and (not what) (eq org-ts-what 'bracket))
  15954. (org-toggle-timestamp-type)
  15955. ;; Point isn't on brackets. Remember the part of the time-stamp
  15956. ;; the point was in. Indeed, size of time-stamps may change,
  15957. ;; but point must be kept in the same category nonetheless.
  15958. (setq origin-cat org-ts-what)
  15959. (if (and (not what) (not (eq org-ts-what 'day))
  15960. org-display-custom-times
  15961. (get-text-property (point) 'display)
  15962. (not (get-text-property (1- (point)) 'display)))
  15963. (setq org-ts-what 'day))
  15964. (setq org-ts-what (or what org-ts-what)
  15965. inactive (= (char-after (match-beginning 0)) ?\[)
  15966. ts (match-string 0))
  15967. (replace-match "")
  15968. (when (string-match
  15969. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?-?[-+][0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)*\\)[]>]"
  15970. ts)
  15971. (setq extra (match-string 1 ts))
  15972. (if suppress-tmp-delay
  15973. (setq extra (replace-regexp-in-string " --[0-9]+[hdwmy]" "" extra))))
  15974. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  15975. (setq with-hm t))
  15976. (setq time0 (org-parse-time-string ts))
  15977. (when (and updown
  15978. (eq org-ts-what 'minute)
  15979. (not current-prefix-arg))
  15980. ;; This looks like s-up and s-down. Change by one rounding step.
  15981. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  15982. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  15983. (setcar (cdr time0) (+ (nth 1 time0)
  15984. (if (> n 0) (- rem) (- dm rem))))))
  15985. (setq time
  15986. (encode-time (or (car time0) 0)
  15987. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  15988. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  15989. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  15990. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  15991. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  15992. (nthcdr 6 time0)))
  15993. (when (and (member org-ts-what '(hour minute))
  15994. extra
  15995. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  15996. (setq extra (org-modify-ts-extra
  15997. extra
  15998. (if (eq org-ts-what 'hour) 2 5)
  15999. n dm)))
  16000. (when (integerp org-ts-what)
  16001. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  16002. (if (eq what 'calendar)
  16003. (let ((cal-date (org-get-date-from-calendar)))
  16004. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  16005. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  16006. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  16007. (setcar time0 (or (car time0) 0))
  16008. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  16009. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  16010. (setq time (apply 'encode-time time0))))
  16011. ;; Insert the new time-stamp, and ensure point stays in the same
  16012. ;; category as before (i.e. not after the last position in that
  16013. ;; category).
  16014. (let ((pos (point)))
  16015. ;; Stay before inserted string. `save-excursion' is of no use.
  16016. (setq org-last-changed-timestamp
  16017. (org-insert-time-stamp time with-hm inactive nil nil extra))
  16018. (goto-char pos))
  16019. (save-match-data
  16020. (looking-at org-ts-regexp3)
  16021. (goto-char (cond
  16022. ;; `day' category ends before `hour' if any, or at
  16023. ;; the end of the day name.
  16024. ((eq origin-cat 'day)
  16025. (min (or (match-beginning 7) (1- (match-end 5))) origin))
  16026. ((eq origin-cat 'hour) (min (match-end 7) origin))
  16027. ((eq origin-cat 'minute) (min (1- (match-end 8)) origin))
  16028. ((integerp origin-cat) (min (1- (match-end 0)) origin))
  16029. ;; `year' and `month' have both fixed size: point
  16030. ;; couldn't have moved into another part.
  16031. (t origin))))
  16032. ;; Update clock if on a CLOCK line.
  16033. (org-clock-update-time-maybe)
  16034. ;; Maybe adjust the closest clock in `org-clock-history'
  16035. (when org-clock-adjust-closest
  16036. (if (not (and (org-at-clock-log-p)
  16037. (< 1 (length (delq nil (mapcar 'marker-position
  16038. org-clock-history))))))
  16039. (message "No clock to adjust")
  16040. (cond ((save-excursion ; fix previous clock?
  16041. (re-search-backward org-ts-regexp0 nil t)
  16042. (org-looking-back (concat org-clock-string " \\[")))
  16043. (setq fixnext 1 clrgx (concat org-ts-regexp0 "\\] =>.*$")))
  16044. ((save-excursion ; fix next clock?
  16045. (re-search-backward org-ts-regexp0 nil t)
  16046. (looking-at (concat org-ts-regexp0 "\\] =>")))
  16047. (setq fixnext -1 clrgx (concat org-clock-string " \\[" org-ts-regexp0))))
  16048. (save-window-excursion
  16049. ;; Find closest clock to point, adjust the previous/next one in history
  16050. (let* ((p (save-excursion (org-back-to-heading t)))
  16051. (cl (mapcar (lambda(c) (abs (- (marker-position c) p))) org-clock-history))
  16052. (clfixnth
  16053. (+ fixnext (- (length cl) (or (length (member (apply #'min cl) cl)) 100))))
  16054. (clfixpos (if (> 0 clfixnth) nil (nth clfixnth org-clock-history))))
  16055. (if (not clfixpos)
  16056. (message "No clock to adjust")
  16057. (save-excursion
  16058. (org-goto-marker-or-bmk clfixpos)
  16059. (org-show-subtree)
  16060. (when (re-search-forward clrgx nil t)
  16061. (goto-char (match-beginning 1))
  16062. (let (org-clock-adjust-closest)
  16063. (org-timestamp-change n org-ts-what updown))
  16064. (message "Clock adjusted in %s for heading: %s"
  16065. (file-name-nondirectory (buffer-file-name))
  16066. (org-get-heading t t)))))))))
  16067. ;; Try to recenter the calendar window, if any.
  16068. (if (and org-calendar-follow-timestamp-change
  16069. (get-buffer-window "*Calendar*" t)
  16070. (memq org-ts-what '(day month year)))
  16071. (org-recenter-calendar (time-to-days time))))))
  16072. (defun org-modify-ts-extra (s pos n dm)
  16073. "Change the different parts of the lead-time and repeat fields in timestamp."
  16074. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  16075. ng h m new rem)
  16076. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  16077. (cond
  16078. ((or (org-pos-in-match-range pos 2)
  16079. (org-pos-in-match-range pos 3))
  16080. (setq m (string-to-number (match-string 3 s))
  16081. h (string-to-number (match-string 2 s)))
  16082. (if (org-pos-in-match-range pos 2)
  16083. (setq h (+ h n))
  16084. (setq n (* dm (org-no-warnings (signum n))))
  16085. (when (not (= 0 (setq rem (% m dm))))
  16086. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  16087. (setq m (+ m n)))
  16088. (if (< m 0) (setq m (+ m 60) h (1- h)))
  16089. (if (> m 59) (setq m (- m 60) h (1+ h)))
  16090. (setq h (min 24 (max 0 h)))
  16091. (setq ng 1 new (format "-%02d:%02d" h m)))
  16092. ((org-pos-in-match-range pos 6)
  16093. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  16094. ((org-pos-in-match-range pos 5)
  16095. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  16096. ((org-pos-in-match-range pos 9)
  16097. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  16098. ((org-pos-in-match-range pos 8)
  16099. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  16100. (when ng
  16101. (setq s (concat
  16102. (substring s 0 (match-beginning ng))
  16103. new
  16104. (substring s (match-end ng))))))
  16105. s))
  16106. (defun org-recenter-calendar (date)
  16107. "If the calendar is visible, recenter it to DATE."
  16108. (let ((cwin (get-buffer-window "*Calendar*" t)))
  16109. (when cwin
  16110. (let ((calendar-move-hook nil))
  16111. (with-selected-window cwin
  16112. (calendar-goto-date (if (listp date) date
  16113. (calendar-gregorian-from-absolute date))))))))
  16114. (defun org-goto-calendar (&optional arg)
  16115. "Go to the Emacs calendar at the current date.
  16116. If there is a time stamp in the current line, go to that date.
  16117. A prefix ARG can be used to force the current date."
  16118. (interactive "P")
  16119. (let ((tsr org-ts-regexp) diff
  16120. (calendar-move-hook nil)
  16121. (calendar-view-holidays-initially-flag nil)
  16122. (calendar-view-diary-initially-flag nil))
  16123. (if (or (org-at-timestamp-p)
  16124. (save-excursion
  16125. (beginning-of-line 1)
  16126. (looking-at (concat ".*" tsr))))
  16127. (let ((d1 (time-to-days (current-time)))
  16128. (d2 (time-to-days
  16129. (org-time-string-to-time (match-string 1)))))
  16130. (setq diff (- d2 d1))))
  16131. (calendar)
  16132. (calendar-goto-today)
  16133. (if (and diff (not arg)) (calendar-forward-day diff))))
  16134. (defun org-get-date-from-calendar ()
  16135. "Return a list (month day year) of date at point in calendar."
  16136. (with-current-buffer "*Calendar*"
  16137. (save-match-data
  16138. (calendar-cursor-to-date))))
  16139. (defun org-date-from-calendar ()
  16140. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  16141. If there is already a time stamp at the cursor position, update it."
  16142. (interactive)
  16143. (if (org-at-timestamp-p t)
  16144. (org-timestamp-change 0 'calendar)
  16145. (let ((cal-date (org-get-date-from-calendar)))
  16146. (org-insert-time-stamp
  16147. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  16148. (defcustom org-effort-durations
  16149. `(("h" . 60)
  16150. ("d" . ,(* 60 8))
  16151. ("w" . ,(* 60 8 5))
  16152. ("m" . ,(* 60 8 5 4))
  16153. ("y" . ,(* 60 8 5 40)))
  16154. "Conversion factor to minutes for an effort modifier.
  16155. Each entry has the form (MODIFIER . MINUTES).
  16156. In an effort string, a number followed by MODIFIER is multiplied
  16157. by the specified number of MINUTES to obtain an effort in
  16158. minutes.
  16159. For example, if the value of this variable is ((\"hours\" . 60)), then an
  16160. effort string \"2hours\" is equivalent to 120 minutes."
  16161. :group 'org-agenda
  16162. :version "24.1"
  16163. :type '(alist :key-type (string :tag "Modifier")
  16164. :value-type (number :tag "Minutes")))
  16165. (defun org-minutes-to-clocksum-string (m)
  16166. "Format number of minutes as a clocksum string.
  16167. The format is determined by `org-time-clocksum-format',
  16168. `org-time-clocksum-use-fractional' and
  16169. `org-time-clocksum-fractional-format' and
  16170. `org-time-clocksum-use-effort-durations'."
  16171. (let ((clocksum "")
  16172. (m (round m)) ; Don't allow fractions of minutes
  16173. h d w mo y fmt n)
  16174. (setq h (if org-time-clocksum-use-effort-durations
  16175. (cdr (assoc "h" org-effort-durations)) 60)
  16176. d (if org-time-clocksum-use-effort-durations
  16177. (/ (cdr (assoc "d" org-effort-durations)) h) 24)
  16178. w (if org-time-clocksum-use-effort-durations
  16179. (/ (cdr (assoc "w" org-effort-durations)) (* d h)) 7)
  16180. mo (if org-time-clocksum-use-effort-durations
  16181. (/ (cdr (assoc "m" org-effort-durations)) (* d h)) 30)
  16182. y (if org-time-clocksum-use-effort-durations
  16183. (/ (cdr (assoc "y" org-effort-durations)) (* d h)) 365))
  16184. ;; fractional format
  16185. (if org-time-clocksum-use-fractional
  16186. (cond
  16187. ;; single format string
  16188. ((stringp org-time-clocksum-fractional-format)
  16189. (format org-time-clocksum-fractional-format (/ m (float h))))
  16190. ;; choice of fractional formats for different time units
  16191. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :years))
  16192. (> (/ (truncate m) (* y d h)) 0))
  16193. (format fmt (/ m (* y d (float h)))))
  16194. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :months))
  16195. (> (/ (truncate m) (* mo d h)) 0))
  16196. (format fmt (/ m (* mo d (float h)))))
  16197. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :weeks))
  16198. (> (/ (truncate m) (* w d h)) 0))
  16199. (format fmt (/ m (* w d (float h)))))
  16200. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :days))
  16201. (> (/ (truncate m) (* d h)) 0))
  16202. (format fmt (/ m (* d (float h)))))
  16203. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :hours))
  16204. (> (/ (truncate m) h) 0))
  16205. (format fmt (/ m (float h))))
  16206. ((setq fmt (plist-get org-time-clocksum-fractional-format :minutes))
  16207. (format fmt m))
  16208. ;; fall back to smallest time unit with a format
  16209. ((setq fmt (plist-get org-time-clocksum-fractional-format :hours))
  16210. (format fmt (/ m (float h))))
  16211. ((setq fmt (plist-get org-time-clocksum-fractional-format :days))
  16212. (format fmt (/ m (* d (float h)))))
  16213. ((setq fmt (plist-get org-time-clocksum-fractional-format :weeks))
  16214. (format fmt (/ m (* w d (float h)))))
  16215. ((setq fmt (plist-get org-time-clocksum-fractional-format :months))
  16216. (format fmt (/ m (* mo d (float h)))))
  16217. ((setq fmt (plist-get org-time-clocksum-fractional-format :years))
  16218. (format fmt (/ m (* y d (float h))))))
  16219. ;; standard (non-fractional) format, with single format string
  16220. (if (stringp org-time-clocksum-format)
  16221. (format org-time-clocksum-format (setq n (/ m h)) (- m (* h n)))
  16222. ;; separate formats components
  16223. (and (setq fmt (plist-get org-time-clocksum-format :years))
  16224. (or (> (setq n (/ (truncate m) (* y d h))) 0)
  16225. (plist-get org-time-clocksum-format :require-years))
  16226. (setq clocksum (concat clocksum (format fmt n))
  16227. m (- m (* n y d h))))
  16228. (and (setq fmt (plist-get org-time-clocksum-format :months))
  16229. (or (> (setq n (/ (truncate m) (* mo d h))) 0)
  16230. (plist-get org-time-clocksum-format :require-months))
  16231. (setq clocksum (concat clocksum (format fmt n))
  16232. m (- m (* n mo d h))))
  16233. (and (setq fmt (plist-get org-time-clocksum-format :weeks))
  16234. (or (> (setq n (/ (truncate m) (* w d h))) 0)
  16235. (plist-get org-time-clocksum-format :require-weeks))
  16236. (setq clocksum (concat clocksum (format fmt n))
  16237. m (- m (* n w d h))))
  16238. (and (setq fmt (plist-get org-time-clocksum-format :days))
  16239. (or (> (setq n (/ (truncate m) (* d h))) 0)
  16240. (plist-get org-time-clocksum-format :require-days))
  16241. (setq clocksum (concat clocksum (format fmt n))
  16242. m (- m (* n d h))))
  16243. (and (setq fmt (plist-get org-time-clocksum-format :hours))
  16244. (or (> (setq n (/ (truncate m) h)) 0)
  16245. (plist-get org-time-clocksum-format :require-hours))
  16246. (setq clocksum (concat clocksum (format fmt n))
  16247. m (- m (* n h))))
  16248. (and (setq fmt (plist-get org-time-clocksum-format :minutes))
  16249. (or (> m 0) (plist-get org-time-clocksum-format :require-minutes))
  16250. (setq clocksum (concat clocksum (format fmt m))))
  16251. ;; return formatted time duration
  16252. clocksum))))
  16253. (defalias 'org-minutes-to-hh:mm-string 'org-minutes-to-clocksum-string)
  16254. (make-obsolete 'org-minutes-to-hh:mm-string 'org-minutes-to-clocksum-string
  16255. "Org mode version 8.0")
  16256. (defun org-hours-to-clocksum-string (n)
  16257. (org-minutes-to-clocksum-string (* n 60)))
  16258. (defun org-hh:mm-string-to-minutes (s)
  16259. "Convert a string H:MM to a number of minutes.
  16260. If the string is just a number, interpret it as minutes.
  16261. In fact, the first hh:mm or number in the string will be taken,
  16262. there can be extra stuff in the string.
  16263. If no number is found, the return value is 0."
  16264. (cond
  16265. ((integerp s) s)
  16266. ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  16267. (+ (* (string-to-number (match-string 1 s)) 60)
  16268. (string-to-number (match-string 2 s))))
  16269. ((string-match "\\([0-9]+\\)" s)
  16270. (string-to-number (match-string 1 s)))
  16271. (t 0)))
  16272. (defcustom org-image-actual-width t
  16273. "Should we use the actual width of images when inlining them?
  16274. When set to `t', always use the image width.
  16275. When set to a number, use imagemagick (when available) to set
  16276. the image's width to this value.
  16277. When set to a number in a list, try to get the width from any
  16278. #+ATTR.* keyword if it matches a width specification like
  16279. #+ATTR_HTML: :width 300px
  16280. and fall back on that number if none is found.
  16281. When set to nil, try to get the width from an #+ATTR.* keyword
  16282. and fall back on the original width if none is found.
  16283. This requires Emacs >= 24.1, build with imagemagick support."
  16284. :group 'org-appearance
  16285. :version "24.4"
  16286. :package-version '(Org . "8.0")
  16287. :type '(choice
  16288. (const :tag "Use the image width" t)
  16289. (integer :tag "Use a number of pixels")
  16290. (list :tag "Use #+ATTR* or a number of pixels" (integer))
  16291. (const :tag "Use #+ATTR* or don't resize" nil)))
  16292. (defcustom org-agenda-inhibit-startup nil
  16293. "Inhibit startup when preparing agenda buffers.
  16294. When this variable is `t', the initialization of the Org agenda
  16295. buffers is inhibited: e.g. the visibility state is not set, the
  16296. tables are not re-aligned, etc."
  16297. :type 'boolean
  16298. :version "24.3"
  16299. :group 'org-agenda)
  16300. (defcustom org-agenda-ignore-drawer-properties nil
  16301. "Avoid updating text properties when building the agenda.
  16302. Properties are used to prepare buffers for effort estimates, appointments,
  16303. and subtree-local categories.
  16304. If you don't use these in the agenda, you can add them to this list and
  16305. agenda building will be a bit faster.
  16306. The value is a list, with zero or more of the symbols `effort', `appt',
  16307. or `category'."
  16308. :type '(set :greedy t
  16309. (const effort)
  16310. (const appt)
  16311. (const category))
  16312. :version "24.3"
  16313. :group 'org-agenda)
  16314. (defun org-duration-string-to-minutes (s &optional output-to-string)
  16315. "Convert a duration string S to minutes.
  16316. A bare number is interpreted as minutes, modifiers can be set by
  16317. customizing `org-effort-durations' (which see).
  16318. Entries containing a colon are interpreted as H:MM by
  16319. `org-hh:mm-string-to-minutes'."
  16320. (let ((result 0)
  16321. (re (concat "\\([0-9.]+\\) *\\("
  16322. (regexp-opt (mapcar 'car org-effort-durations))
  16323. "\\)")))
  16324. (while (string-match re s)
  16325. (incf result (* (cdr (assoc (match-string 2 s) org-effort-durations))
  16326. (string-to-number (match-string 1 s))))
  16327. (setq s (replace-match "" nil t s)))
  16328. (setq result (floor result))
  16329. (incf result (org-hh:mm-string-to-minutes s))
  16330. (if output-to-string (number-to-string result) result)))
  16331. ;;;; Files
  16332. (defun org-save-all-org-buffers ()
  16333. "Save all Org-mode buffers without user confirmation."
  16334. (interactive)
  16335. (message "Saving all Org-mode buffers...")
  16336. (save-some-buffers t (lambda () (derived-mode-p 'org-mode)))
  16337. (when (featurep 'org-id) (org-id-locations-save))
  16338. (message "Saving all Org-mode buffers... done"))
  16339. (defun org-revert-all-org-buffers ()
  16340. "Revert all Org-mode buffers.
  16341. Prompt for confirmation when there are unsaved changes.
  16342. Be sure you know what you are doing before letting this function
  16343. overwrite your changes.
  16344. This function is useful in a setup where one tracks org files
  16345. with a version control system, to revert on one machine after pulling
  16346. changes from another. I believe the procedure must be like this:
  16347. 1. M-x org-save-all-org-buffers
  16348. 2. Pull changes from the other machine, resolve conflicts
  16349. 3. M-x org-revert-all-org-buffers"
  16350. (interactive)
  16351. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  16352. (user-error "Abort"))
  16353. (save-excursion
  16354. (save-window-excursion
  16355. (mapc
  16356. (lambda (b)
  16357. (when (and (with-current-buffer b (derived-mode-p 'org-mode))
  16358. (with-current-buffer b buffer-file-name))
  16359. (org-pop-to-buffer-same-window b)
  16360. (revert-buffer t 'no-confirm)))
  16361. (buffer-list))
  16362. (when (and (featurep 'org-id) org-id-track-globally)
  16363. (org-id-locations-load)))))
  16364. ;;;; Agenda files
  16365. ;;;###autoload
  16366. (defun org-switchb (&optional arg)
  16367. "Switch between Org buffers.
  16368. With one prefix argument, restrict available buffers to files.
  16369. With two prefix arguments, restrict available buffers to agenda files.
  16370. Defaults to `iswitchb' for buffer name completion.
  16371. Set `org-completion-use-ido' to make it use ido instead."
  16372. (interactive "P")
  16373. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  16374. ((equal arg '(16)) (org-buffer-list 'agenda))
  16375. (t (org-buffer-list))))
  16376. (org-completion-use-iswitchb org-completion-use-iswitchb)
  16377. (org-completion-use-ido org-completion-use-ido))
  16378. (unless (or org-completion-use-ido org-completion-use-iswitchb)
  16379. (setq org-completion-use-iswitchb t))
  16380. (org-pop-to-buffer-same-window
  16381. (org-icompleting-read "Org buffer: "
  16382. (mapcar 'list (mapcar 'buffer-name blist))
  16383. nil t))))
  16384. ;;; Define some older names previously used for this functionality
  16385. ;;;###autoload
  16386. (defalias 'org-ido-switchb 'org-switchb)
  16387. ;;;###autoload
  16388. (defalias 'org-iswitchb 'org-switchb)
  16389. (defun org-buffer-list (&optional predicate exclude-tmp)
  16390. "Return a list of Org buffers.
  16391. PREDICATE can be `export', `files' or `agenda'.
  16392. export restrict the list to Export buffers.
  16393. files restrict the list to buffers visiting Org files.
  16394. agenda restrict the list to buffers visiting agenda files.
  16395. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  16396. (let* ((bfn nil)
  16397. (agenda-files (and (eq predicate 'agenda)
  16398. (mapcar 'file-truename (org-agenda-files t))))
  16399. (filter
  16400. (cond
  16401. ((eq predicate 'files)
  16402. (lambda (b) (with-current-buffer b (derived-mode-p 'org-mode))))
  16403. ((eq predicate 'export)
  16404. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  16405. ((eq predicate 'agenda)
  16406. (lambda (b)
  16407. (with-current-buffer b
  16408. (and (derived-mode-p 'org-mode)
  16409. (setq bfn (buffer-file-name b))
  16410. (member (file-truename bfn) agenda-files)))))
  16411. (t (lambda (b) (with-current-buffer b
  16412. (or (derived-mode-p 'org-mode)
  16413. (string-match "\*Org .*Export"
  16414. (buffer-name b)))))))))
  16415. (delq nil
  16416. (mapcar
  16417. (lambda(b)
  16418. (if (and (funcall filter b)
  16419. (or (not exclude-tmp)
  16420. (not (string-match "tmp" (buffer-name b)))))
  16421. b
  16422. nil))
  16423. (buffer-list)))))
  16424. (defun org-agenda-files (&optional unrestricted archives)
  16425. "Get the list of agenda files.
  16426. Optional UNRESTRICTED means return the full list even if a restriction
  16427. is currently in place.
  16428. When ARCHIVES is t, include all archive files that are really being
  16429. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  16430. `org-agenda-archives-mode' is t."
  16431. (let ((files
  16432. (cond
  16433. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  16434. ((stringp org-agenda-files) (org-read-agenda-file-list))
  16435. ((listp org-agenda-files) org-agenda-files)
  16436. (t (error "Invalid value of `org-agenda-files'")))))
  16437. (setq files (apply 'append
  16438. (mapcar (lambda (f)
  16439. (if (file-directory-p f)
  16440. (directory-files
  16441. f t org-agenda-file-regexp)
  16442. (list f)))
  16443. files)))
  16444. (when org-agenda-skip-unavailable-files
  16445. (setq files (delq nil
  16446. (mapcar (function
  16447. (lambda (file)
  16448. (and (file-readable-p file) file)))
  16449. files))))
  16450. (when (or (eq archives t)
  16451. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  16452. (setq files (org-add-archive-files files)))
  16453. files))
  16454. (defun org-agenda-file-p (&optional file)
  16455. "Return non-nil, if FILE is an agenda file.
  16456. If FILE is omitted, use the file associated with the current
  16457. buffer."
  16458. (member (or file (buffer-file-name))
  16459. (org-agenda-files t)))
  16460. (defun org-edit-agenda-file-list ()
  16461. "Edit the list of agenda files.
  16462. Depending on setup, this either uses customize to edit the variable
  16463. `org-agenda-files', or it visits the file that is holding the list. In the
  16464. latter case, the buffer is set up in a way that saving it automatically kills
  16465. the buffer and restores the previous window configuration."
  16466. (interactive)
  16467. (if (stringp org-agenda-files)
  16468. (let ((cw (current-window-configuration)))
  16469. (find-file org-agenda-files)
  16470. (org-set-local 'org-window-configuration cw)
  16471. (org-add-hook 'after-save-hook
  16472. (lambda ()
  16473. (set-window-configuration
  16474. (prog1 org-window-configuration
  16475. (kill-buffer (current-buffer))))
  16476. (org-install-agenda-files-menu)
  16477. (message "New agenda file list installed"))
  16478. nil 'local)
  16479. (message "%s" (substitute-command-keys
  16480. "Edit list and finish with \\[save-buffer]")))
  16481. (customize-variable 'org-agenda-files)))
  16482. (defun org-store-new-agenda-file-list (list)
  16483. "Set new value for the agenda file list and save it correctly."
  16484. (if (stringp org-agenda-files)
  16485. (let ((fe (org-read-agenda-file-list t)) b u)
  16486. (while (setq b (find-buffer-visiting org-agenda-files))
  16487. (kill-buffer b))
  16488. (with-temp-file org-agenda-files
  16489. (insert
  16490. (mapconcat
  16491. (lambda (f) ;; Keep un-expanded entries.
  16492. (if (setq u (assoc f fe))
  16493. (cdr u)
  16494. f))
  16495. list "\n")
  16496. "\n")))
  16497. (let ((org-mode-hook nil) (org-inhibit-startup t)
  16498. (org-insert-mode-line-in-empty-file nil))
  16499. (setq org-agenda-files list)
  16500. (customize-save-variable 'org-agenda-files org-agenda-files))))
  16501. (defun org-read-agenda-file-list (&optional pair-with-expansion)
  16502. "Read the list of agenda files from a file.
  16503. If PAIR-WITH-EXPANSION is t return pairs with un-expanded
  16504. filenames, used by `org-store-new-agenda-file-list' to write back
  16505. un-expanded file names."
  16506. (when (file-directory-p org-agenda-files)
  16507. (error "`org-agenda-files' cannot be a single directory"))
  16508. (when (stringp org-agenda-files)
  16509. (with-temp-buffer
  16510. (insert-file-contents org-agenda-files)
  16511. (mapcar
  16512. (lambda (f)
  16513. (let ((e (expand-file-name (substitute-in-file-name f)
  16514. org-directory)))
  16515. (if pair-with-expansion
  16516. (cons e f)
  16517. e)))
  16518. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*")))))
  16519. ;;;###autoload
  16520. (defun org-cycle-agenda-files ()
  16521. "Cycle through the files in `org-agenda-files'.
  16522. If the current buffer visits an agenda file, find the next one in the list.
  16523. If the current buffer does not, find the first agenda file."
  16524. (interactive)
  16525. (let* ((fs (org-agenda-files t))
  16526. (files (append fs (list (car fs))))
  16527. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  16528. file)
  16529. (unless files (user-error "No agenda files"))
  16530. (catch 'exit
  16531. (while (setq file (pop files))
  16532. (if (equal (file-truename file) tcf)
  16533. (when (car files)
  16534. (find-file (car files))
  16535. (throw 'exit t))))
  16536. (find-file (car fs)))
  16537. (if (buffer-base-buffer) (org-pop-to-buffer-same-window (buffer-base-buffer)))))
  16538. (defun org-agenda-file-to-front (&optional to-end)
  16539. "Move/add the current file to the top of the agenda file list.
  16540. If the file is not present in the list, it is added to the front. If it is
  16541. present, it is moved there. With optional argument TO-END, add/move to the
  16542. end of the list."
  16543. (interactive "P")
  16544. (let ((org-agenda-skip-unavailable-files nil)
  16545. (file-alist (mapcar (lambda (x)
  16546. (cons (file-truename x) x))
  16547. (org-agenda-files t)))
  16548. (ctf (file-truename
  16549. (or buffer-file-name
  16550. (user-error "Please save the current buffer to a file"))))
  16551. x had)
  16552. (setq x (assoc ctf file-alist) had x)
  16553. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  16554. (if to-end
  16555. (setq file-alist (append (delq x file-alist) (list x)))
  16556. (setq file-alist (cons x (delq x file-alist))))
  16557. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  16558. (org-install-agenda-files-menu)
  16559. (message "File %s to %s of agenda file list"
  16560. (if had "moved" "added") (if to-end "end" "front"))))
  16561. (defun org-remove-file (&optional file)
  16562. "Remove current file from the list of files in variable `org-agenda-files'.
  16563. These are the files which are being checked for agenda entries.
  16564. Optional argument FILE means use this file instead of the current."
  16565. (interactive)
  16566. (let* ((org-agenda-skip-unavailable-files nil)
  16567. (file (or file buffer-file-name
  16568. (user-error "Current buffer does not visit a file")))
  16569. (true-file (file-truename file))
  16570. (afile (abbreviate-file-name file))
  16571. (files (delq nil (mapcar
  16572. (lambda (x)
  16573. (if (equal true-file
  16574. (file-truename x))
  16575. nil x))
  16576. (org-agenda-files t)))))
  16577. (if (not (= (length files) (length (org-agenda-files t))))
  16578. (progn
  16579. (org-store-new-agenda-file-list files)
  16580. (org-install-agenda-files-menu)
  16581. (message "Removed file: %s" afile))
  16582. (message "File was not in list: %s (not removed)" afile))))
  16583. (defun org-file-menu-entry (file)
  16584. (vector file (list 'find-file file) t))
  16585. (defun org-check-agenda-file (file)
  16586. "Make sure FILE exists. If not, ask user what to do."
  16587. (when (not (file-exists-p file))
  16588. (message "Non-existent agenda file %s. [R]emove from list or [A]bort?"
  16589. (abbreviate-file-name file))
  16590. (let ((r (downcase (read-char-exclusive))))
  16591. (cond
  16592. ((equal r ?r)
  16593. (org-remove-file file)
  16594. (throw 'nextfile t))
  16595. (t (error "Abort"))))))
  16596. (defun org-get-agenda-file-buffer (file)
  16597. "Get a buffer visiting FILE. If the buffer needs to be created, add
  16598. it to the list of buffers which might be released later."
  16599. (let ((buf (org-find-base-buffer-visiting file)))
  16600. (if buf
  16601. buf ; just return it
  16602. ;; Make a new buffer and remember it
  16603. (setq buf (find-file-noselect file))
  16604. (if buf (push buf org-agenda-new-buffers))
  16605. buf)))
  16606. (defun org-release-buffers (blist)
  16607. "Release all buffers in list, asking the user for confirmation when needed.
  16608. When a buffer is unmodified, it is just killed. When modified, it is saved
  16609. \(if the user agrees) and then killed."
  16610. (let (buf file)
  16611. (while (setq buf (pop blist))
  16612. (setq file (buffer-file-name buf))
  16613. (when (and (buffer-modified-p buf)
  16614. file
  16615. (y-or-n-p (format "Save file %s? " file)))
  16616. (with-current-buffer buf (save-buffer)))
  16617. (kill-buffer buf))))
  16618. (defun org-agenda-prepare-buffers (files)
  16619. "Create buffers for all agenda files, protect archived trees and comments."
  16620. (interactive)
  16621. (let ((pa '(:org-archived t))
  16622. (pc '(:org-comment t))
  16623. (pall '(:org-archived t :org-comment t))
  16624. (inhibit-read-only t)
  16625. (org-inhibit-startup org-agenda-inhibit-startup)
  16626. (rea (concat ":" org-archive-tag ":"))
  16627. file re pos)
  16628. (setq org-tag-alist-for-agenda nil
  16629. org-tag-groups-alist-for-agenda nil)
  16630. (save-excursion
  16631. (save-restriction
  16632. (while (setq file (pop files))
  16633. (catch 'nextfile
  16634. (if (bufferp file)
  16635. (set-buffer file)
  16636. (org-check-agenda-file file)
  16637. (set-buffer (org-get-agenda-file-buffer file)))
  16638. (widen)
  16639. (org-set-regexps-and-options-for-tags)
  16640. (setq pos (point))
  16641. (goto-char (point-min))
  16642. (let ((case-fold-search t))
  16643. (when (search-forward "#+setupfile" nil t)
  16644. ;; Don't set all regexps and options systematically as
  16645. ;; this is only run for setting agenda tags from setup
  16646. ;; file
  16647. (org-set-regexps-and-options)))
  16648. (or (memq 'category org-agenda-ignore-drawer-properties)
  16649. (org-refresh-category-properties))
  16650. (or (memq 'effort org-agenda-ignore-drawer-properties)
  16651. (org-refresh-properties org-effort-property 'org-effort))
  16652. (or (memq 'appt org-agenda-ignore-drawer-properties)
  16653. (org-refresh-properties "APPT_WARNTIME" 'org-appt-warntime))
  16654. (setq org-todo-keywords-for-agenda
  16655. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  16656. (setq org-done-keywords-for-agenda
  16657. (append org-done-keywords-for-agenda org-done-keywords))
  16658. (setq org-todo-keyword-alist-for-agenda
  16659. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  16660. (setq org-drawers-for-agenda
  16661. (append org-drawers-for-agenda org-drawers))
  16662. (setq org-tag-alist-for-agenda
  16663. (org-uniquify
  16664. (append org-tag-alist-for-agenda
  16665. org-tag-alist
  16666. org-tag-persistent-alist)))
  16667. (if org-group-tags
  16668. (setq org-tag-groups-alist-for-agenda
  16669. (org-uniquify-alist
  16670. (append org-tag-groups-alist-for-agenda org-tag-groups-alist))))
  16671. (org-with-silent-modifications
  16672. (save-excursion
  16673. (remove-text-properties (point-min) (point-max) pall)
  16674. (when org-agenda-skip-archived-trees
  16675. (goto-char (point-min))
  16676. (while (re-search-forward rea nil t)
  16677. (if (org-at-heading-p t)
  16678. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  16679. (goto-char (point-min))
  16680. (setq re (format org-heading-keyword-regexp-format
  16681. org-comment-string))
  16682. (while (re-search-forward re nil t)
  16683. (add-text-properties
  16684. (match-beginning 0) (org-end-of-subtree t) pc))))
  16685. (goto-char pos)))))
  16686. (setq org-todo-keywords-for-agenda
  16687. (org-uniquify org-todo-keywords-for-agenda))
  16688. (setq org-todo-keyword-alist-for-agenda
  16689. (org-uniquify org-todo-keyword-alist-for-agenda))))
  16690. ;;;; CDLaTeX minor mode
  16691. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  16692. "Keymap for the minor `org-cdlatex-mode'.")
  16693. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  16694. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  16695. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  16696. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  16697. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  16698. (defvar org-cdlatex-texmathp-advice-is-done nil
  16699. "Flag remembering if we have applied the advice to texmathp already.")
  16700. (define-minor-mode org-cdlatex-mode
  16701. "Toggle the minor `org-cdlatex-mode'.
  16702. This mode supports entering LaTeX environment and math in LaTeX fragments
  16703. in Org-mode.
  16704. \\{org-cdlatex-mode-map}"
  16705. nil " OCDL" nil
  16706. (when org-cdlatex-mode
  16707. (require 'cdlatex)
  16708. (run-hooks 'cdlatex-mode-hook)
  16709. (cdlatex-compute-tables))
  16710. (unless org-cdlatex-texmathp-advice-is-done
  16711. (setq org-cdlatex-texmathp-advice-is-done t)
  16712. (defadvice texmathp (around org-math-always-on activate)
  16713. "Always return t in org-mode buffers.
  16714. This is because we want to insert math symbols without dollars even outside
  16715. the LaTeX math segments. If Orgmode thinks that point is actually inside
  16716. an embedded LaTeX fragment, let texmathp do its job.
  16717. \\[org-cdlatex-mode-map]"
  16718. (interactive)
  16719. (let (p)
  16720. (cond
  16721. ((not (derived-mode-p 'org-mode)) ad-do-it)
  16722. ((eq this-command 'cdlatex-math-symbol)
  16723. (setq ad-return-value t
  16724. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  16725. (t
  16726. (let ((p (org-inside-LaTeX-fragment-p)))
  16727. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  16728. (setq ad-return-value t
  16729. texmathp-why '("Org-mode embedded math" . 0))
  16730. (if p ad-do-it)))))))))
  16731. (defun turn-on-org-cdlatex ()
  16732. "Unconditionally turn on `org-cdlatex-mode'."
  16733. (org-cdlatex-mode 1))
  16734. (defun org-try-cdlatex-tab ()
  16735. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  16736. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  16737. - inside a LaTeX fragment, or
  16738. - after the first word in a line, where an abbreviation expansion could
  16739. insert a LaTeX environment."
  16740. (when org-cdlatex-mode
  16741. (cond
  16742. ;; Before any word on the line: No expansion possible.
  16743. ((save-excursion (skip-chars-backward " \t") (bolp)) nil)
  16744. ;; Just after first word on the line: Expand it. Make sure it
  16745. ;; cannot happen on headlines, though.
  16746. ((save-excursion
  16747. (skip-chars-backward "a-zA-Z0-9*")
  16748. (skip-chars-backward " \t")
  16749. (and (bolp) (not (org-at-heading-p))))
  16750. (cdlatex-tab) t)
  16751. ((org-inside-LaTeX-fragment-p) (cdlatex-tab) t))))
  16752. (defun org-cdlatex-underscore-caret (&optional arg)
  16753. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  16754. Revert to the normal definition outside of these fragments."
  16755. (interactive "P")
  16756. (if (org-inside-LaTeX-fragment-p)
  16757. (call-interactively 'cdlatex-sub-superscript)
  16758. (let (org-cdlatex-mode)
  16759. (call-interactively (key-binding (vector last-input-event))))))
  16760. (defun org-cdlatex-math-modify (&optional arg)
  16761. "Execute `cdlatex-math-modify' in LaTeX fragments.
  16762. Revert to the normal definition outside of these fragments."
  16763. (interactive "P")
  16764. (if (org-inside-LaTeX-fragment-p)
  16765. (call-interactively 'cdlatex-math-modify)
  16766. (let (org-cdlatex-mode)
  16767. (call-interactively (key-binding (vector last-input-event))))))
  16768. ;;;; LaTeX fragments
  16769. (defvar org-latex-regexps
  16770. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  16771. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  16772. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  16773. ("$1" "\\([^$]\\|^\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  16774. ("$" "\\([^$]\\|^\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  16775. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  16776. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
  16777. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
  16778. "Regular expressions for matching embedded LaTeX.")
  16779. (defun org-inside-LaTeX-fragment-p ()
  16780. "Test if point is inside a LaTeX fragment.
  16781. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  16782. sequence appearing also before point.
  16783. Even though the matchers for math are configurable, this function assumes
  16784. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  16785. delimiters are skipped when they have been removed by customization.
  16786. The return value is nil, or a cons cell with the delimiter and the
  16787. position of this delimiter.
  16788. This function does a reasonably good job, but can locally be fooled by
  16789. for example currency specifications. For example it will assume being in
  16790. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  16791. fragments that are properly closed, but during editing, we have to live
  16792. with the uncertainty caused by missing closing delimiters. This function
  16793. looks only before point, not after."
  16794. (catch 'exit
  16795. (let ((pos (point))
  16796. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  16797. (lim (progn
  16798. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  16799. (point)))
  16800. dd-on str (start 0) m re)
  16801. (goto-char pos)
  16802. (when dodollar
  16803. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  16804. re (nth 1 (assoc "$" org-latex-regexps)))
  16805. (while (string-match re str start)
  16806. (cond
  16807. ((= (match-end 0) (length str))
  16808. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  16809. ((= (match-end 0) (- (length str) 5))
  16810. (throw 'exit nil))
  16811. (t (setq start (match-end 0))))))
  16812. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  16813. (goto-char pos)
  16814. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  16815. (and (match-beginning 2) (throw 'exit nil))
  16816. ;; count $$
  16817. (while (re-search-backward "\\$\\$" lim t)
  16818. (setq dd-on (not dd-on)))
  16819. (goto-char pos)
  16820. (if dd-on (cons "$$" m))))))
  16821. (defun org-inside-latex-macro-p ()
  16822. "Is point inside a LaTeX macro or its arguments?"
  16823. (save-match-data
  16824. (org-in-regexp
  16825. "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")))
  16826. (defvar org-latex-fragment-image-overlays nil
  16827. "List of overlays carrying the images of latex fragments.")
  16828. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  16829. (defun org-remove-latex-fragment-image-overlays ()
  16830. "Remove all overlays with LaTeX fragment images in current buffer."
  16831. (mapc 'delete-overlay org-latex-fragment-image-overlays)
  16832. (setq org-latex-fragment-image-overlays nil))
  16833. (defun org-preview-latex-fragment (&optional subtree)
  16834. "Preview the LaTeX fragment at point, or all locally or globally.
  16835. If the cursor is in a LaTeX fragment, create the image and overlay
  16836. it over the source code. If there is no fragment at point, display
  16837. all fragments in the current text, from one headline to the next. With
  16838. prefix SUBTREE, display all fragments in the current subtree. With a
  16839. double prefix arg \\[universal-argument] \\[universal-argument], or when \
  16840. the cursor is before the first headline,
  16841. display all fragments in the buffer.
  16842. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  16843. (interactive "P")
  16844. (unless buffer-file-name
  16845. (user-error "Can't preview LaTeX fragment in a non-file buffer"))
  16846. (when (display-graphic-p)
  16847. (org-remove-latex-fragment-image-overlays)
  16848. (save-excursion
  16849. (save-restriction
  16850. (let (beg end at msg)
  16851. (cond
  16852. ((or (equal subtree '(16))
  16853. (not (save-excursion
  16854. (re-search-backward org-outline-regexp-bol nil t))))
  16855. (setq beg (point-min) end (point-max)
  16856. msg "Creating images for buffer...%s"))
  16857. ((equal subtree '(4))
  16858. (org-back-to-heading)
  16859. (setq beg (point) end (org-end-of-subtree t)
  16860. msg "Creating images for subtree...%s"))
  16861. (t
  16862. (if (setq at (org-inside-LaTeX-fragment-p))
  16863. (goto-char (max (point-min) (- (cdr at) 2)))
  16864. (org-back-to-heading))
  16865. (setq beg (point) end (progn (outline-next-heading) (point))
  16866. msg (if at "Creating image...%s"
  16867. "Creating images for entry...%s"))))
  16868. (message msg "")
  16869. (narrow-to-region beg end)
  16870. (goto-char beg)
  16871. (org-format-latex
  16872. (concat org-latex-preview-ltxpng-directory (file-name-sans-extension
  16873. (file-name-nondirectory
  16874. buffer-file-name)))
  16875. default-directory 'overlays msg at 'forbuffer
  16876. org-latex-create-formula-image-program)
  16877. (message msg "done. Use `C-c C-c' to remove images."))))))
  16878. (defun org-format-latex (prefix &optional dir overlays msg at
  16879. forbuffer processing-type)
  16880. "Replace LaTeX fragments with links to an image, and produce images.
  16881. Some of the options can be changed using the variable
  16882. `org-format-latex-options'."
  16883. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  16884. (let* ((prefixnodir (file-name-nondirectory prefix))
  16885. (absprefix (expand-file-name prefix dir))
  16886. (todir (file-name-directory absprefix))
  16887. (opt org-format-latex-options)
  16888. (optnew org-format-latex-options)
  16889. (matchers (plist-get opt :matchers))
  16890. (re-list org-latex-regexps)
  16891. (cnt 0) txt hash link beg end re e checkdir
  16892. string
  16893. m n block-type block linkfile movefile ov)
  16894. ;; Check the different regular expressions
  16895. (while (setq e (pop re-list))
  16896. (setq m (car e) re (nth 1 e) n (nth 2 e) block-type (nth 3 e)
  16897. block (if block-type "\n\n" ""))
  16898. (when (member m matchers)
  16899. (goto-char (point-min))
  16900. (while (re-search-forward re nil t)
  16901. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  16902. (or (not overlays)
  16903. (not (eq (get-char-property (match-beginning n)
  16904. 'org-overlay-type)
  16905. 'org-latex-overlay))))
  16906. (cond
  16907. ((eq processing-type 'verbatim))
  16908. ((eq processing-type 'mathjax)
  16909. ;; Prepare for MathJax processing.
  16910. (setq string (match-string n))
  16911. (when (member m '("$" "$1"))
  16912. (save-excursion
  16913. (delete-region (match-beginning n) (match-end n))
  16914. (goto-char (match-beginning n))
  16915. (insert (concat "\\(" (substring string 1 -1) "\\)")))))
  16916. ((or (eq processing-type 'dvipng)
  16917. (eq processing-type 'imagemagick))
  16918. ;; Process to an image.
  16919. (setq txt (match-string n)
  16920. beg (match-beginning n) end (match-end n)
  16921. cnt (1+ cnt))
  16922. (let ((face (face-at-point))
  16923. (fg (plist-get opt :foreground))
  16924. (bg (plist-get opt :background))
  16925. ;; Ensure full list is printed.
  16926. print-length print-level)
  16927. (when forbuffer
  16928. ;; Get the colors from the face at point.
  16929. (goto-char beg)
  16930. (when (eq fg 'auto)
  16931. (setq fg (face-attribute face :foreground nil 'default)))
  16932. (when (eq bg 'auto)
  16933. (setq bg (face-attribute face :background nil 'default)))
  16934. (setq optnew (copy-sequence opt))
  16935. (plist-put optnew :foreground fg)
  16936. (plist-put optnew :background bg))
  16937. (setq hash (sha1 (prin1-to-string
  16938. (list org-format-latex-header
  16939. org-latex-default-packages-alist
  16940. org-latex-packages-alist
  16941. org-format-latex-options
  16942. forbuffer txt fg bg)))
  16943. linkfile (format "%s_%s.png" prefix hash)
  16944. movefile (format "%s_%s.png" absprefix hash)))
  16945. (setq link (concat block "[[file:" linkfile "]]" block))
  16946. (if msg (message msg cnt))
  16947. (goto-char beg)
  16948. (unless checkdir ; Ensure the directory exists.
  16949. (setq checkdir t)
  16950. (or (file-directory-p todir) (make-directory todir t)))
  16951. (unless (file-exists-p movefile)
  16952. (org-create-formula-image
  16953. txt movefile optnew forbuffer processing-type))
  16954. (if overlays
  16955. (progn
  16956. (mapc (lambda (o)
  16957. (if (eq (overlay-get o 'org-overlay-type)
  16958. 'org-latex-overlay)
  16959. (delete-overlay o)))
  16960. (overlays-in beg end))
  16961. (setq ov (make-overlay beg end))
  16962. (overlay-put ov 'org-overlay-type 'org-latex-overlay)
  16963. (if (featurep 'xemacs)
  16964. (progn
  16965. (overlay-put ov 'invisible t)
  16966. (overlay-put
  16967. ov 'end-glyph
  16968. (make-glyph (vector 'png :file movefile))))
  16969. (overlay-put
  16970. ov 'display
  16971. (list 'image :type 'png :file movefile :ascent 'center)))
  16972. (push ov org-latex-fragment-image-overlays)
  16973. (goto-char end))
  16974. (delete-region beg end)
  16975. (insert (org-add-props link
  16976. (list 'org-latex-src
  16977. (replace-regexp-in-string
  16978. "\"" "" txt)
  16979. 'org-latex-src-embed-type
  16980. (if block-type 'paragraph 'character))))))
  16981. ((eq processing-type 'mathml)
  16982. ;; Process to MathML
  16983. (unless (save-match-data (org-format-latex-mathml-available-p))
  16984. (user-error "LaTeX to MathML converter not configured"))
  16985. (setq txt (match-string n)
  16986. beg (match-beginning n) end (match-end n)
  16987. cnt (1+ cnt))
  16988. (if msg (message msg cnt))
  16989. (goto-char beg)
  16990. (delete-region beg end)
  16991. (insert (org-format-latex-as-mathml
  16992. txt block-type prefix dir)))
  16993. (t
  16994. (error "Unknown conversion type %s for LaTeX fragments"
  16995. processing-type)))))))))
  16996. (defun org-create-math-formula (latex-frag &optional mathml-file)
  16997. "Convert LATEX-FRAG to MathML and store it in MATHML-FILE.
  16998. Use `org-latex-to-mathml-convert-command'. If the conversion is
  16999. sucessful, return the portion between \"<math...> </math>\"
  17000. elements otherwise return nil. When MATHML-FILE is specified,
  17001. write the results in to that file. When invoked as an
  17002. interactive command, prompt for LATEX-FRAG, with initial value
  17003. set to the current active region and echo the results for user
  17004. inspection."
  17005. (interactive (list (let ((frag (when (org-region-active-p)
  17006. (buffer-substring-no-properties
  17007. (region-beginning) (region-end)))))
  17008. (read-string "LaTeX Fragment: " frag nil frag))))
  17009. (unless latex-frag (error "Invalid LaTeX fragment"))
  17010. (let* ((tmp-in-file (file-relative-name
  17011. (make-temp-name (expand-file-name "ltxmathml-in"))))
  17012. (ignore (write-region latex-frag nil tmp-in-file))
  17013. (tmp-out-file (file-relative-name
  17014. (make-temp-name (expand-file-name "ltxmathml-out"))))
  17015. (cmd (format-spec
  17016. org-latex-to-mathml-convert-command
  17017. `((?j . ,(shell-quote-argument
  17018. (expand-file-name org-latex-to-mathml-jar-file)))
  17019. (?I . ,(shell-quote-argument tmp-in-file))
  17020. (?o . ,(shell-quote-argument tmp-out-file)))))
  17021. mathml shell-command-output)
  17022. (when (org-called-interactively-p 'any)
  17023. (unless (org-format-latex-mathml-available-p)
  17024. (user-error "LaTeX to MathML converter not configured")))
  17025. (message "Running %s" cmd)
  17026. (setq shell-command-output (shell-command-to-string cmd))
  17027. (setq mathml
  17028. (when (file-readable-p tmp-out-file)
  17029. (with-current-buffer (find-file-noselect tmp-out-file t)
  17030. (goto-char (point-min))
  17031. (when (re-search-forward
  17032. (concat
  17033. (regexp-quote
  17034. "<math xmlns=\"http://www.w3.org/1998/Math/MathML\">")
  17035. "\\(.\\|\n\\)*"
  17036. (regexp-quote "</math>")) nil t)
  17037. (prog1 (match-string 0) (kill-buffer))))))
  17038. (cond
  17039. (mathml
  17040. (setq mathml
  17041. (concat "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" mathml))
  17042. (when mathml-file
  17043. (write-region mathml nil mathml-file))
  17044. (when (org-called-interactively-p 'any)
  17045. (message mathml)))
  17046. ((message "LaTeX to MathML conversion failed")
  17047. (message shell-command-output)))
  17048. (delete-file tmp-in-file)
  17049. (when (file-exists-p tmp-out-file)
  17050. (delete-file tmp-out-file))
  17051. mathml))
  17052. (defun org-format-latex-as-mathml (latex-frag latex-frag-type
  17053. prefix &optional dir)
  17054. "Use `org-create-math-formula' but check local cache first."
  17055. (let* ((absprefix (expand-file-name prefix dir))
  17056. (print-length nil) (print-level nil)
  17057. (formula-id (concat
  17058. "formula-"
  17059. (sha1
  17060. (prin1-to-string
  17061. (list latex-frag
  17062. org-latex-to-mathml-convert-command)))))
  17063. (formula-cache (format "%s-%s.mathml" absprefix formula-id))
  17064. (formula-cache-dir (file-name-directory formula-cache)))
  17065. (unless (file-directory-p formula-cache-dir)
  17066. (make-directory formula-cache-dir t))
  17067. (unless (file-exists-p formula-cache)
  17068. (org-create-math-formula latex-frag formula-cache))
  17069. (if (file-exists-p formula-cache)
  17070. ;; Successful conversion. Return the link to MathML file.
  17071. (org-add-props
  17072. (format "[[file:%s]]" (file-relative-name formula-cache dir))
  17073. (list 'org-latex-src (replace-regexp-in-string "\"" "" latex-frag)
  17074. 'org-latex-src-embed-type (if latex-frag-type
  17075. 'paragraph 'character)))
  17076. ;; Failed conversion. Return the LaTeX fragment verbatim
  17077. latex-frag)))
  17078. (defun org-create-formula-image (string tofile options buffer &optional type)
  17079. "Create an image from LaTeX source using dvipng or convert.
  17080. This function calls either `org-create-formula-image-with-dvipng'
  17081. or `org-create-formula-image-with-imagemagick' depending on the
  17082. value of `org-latex-create-formula-image-program' or on the value
  17083. of the optional TYPE variable.
  17084. Note: ultimately these two function should be combined as they
  17085. share a good deal of logic."
  17086. (org-check-external-command
  17087. "latex" "needed to convert LaTeX fragments to images")
  17088. (funcall
  17089. (case (or type org-latex-create-formula-image-program)
  17090. ('dvipng
  17091. (org-check-external-command
  17092. "dvipng" "needed to convert LaTeX fragments to images")
  17093. #'org-create-formula-image-with-dvipng)
  17094. ('imagemagick
  17095. (org-check-external-command
  17096. "convert" "you need to install imagemagick")
  17097. #'org-create-formula-image-with-imagemagick)
  17098. (t (error
  17099. "Invalid value of `org-latex-create-formula-image-program'")))
  17100. string tofile options buffer))
  17101. (declare-function org-export-get-backend "ox" (name))
  17102. (declare-function org-export--get-global-options "ox" (&optional backend))
  17103. (declare-function org-export--get-inbuffer-options "ox" (&optional backend))
  17104. (declare-function org-latex-guess-inputenc "ox-latex" (header))
  17105. (declare-function org-latex-guess-babel-language "ox-latex" (header info))
  17106. (defun org-create-formula--latex-header ()
  17107. "Return LaTeX header appropriate for previewing a LaTeX snippet."
  17108. (let ((info (org-combine-plists (org-export--get-global-options
  17109. (org-export-get-backend 'latex))
  17110. (org-export--get-inbuffer-options
  17111. (org-export-get-backend 'latex)))))
  17112. (org-latex-guess-babel-language
  17113. (org-latex-guess-inputenc
  17114. (org-splice-latex-header
  17115. org-format-latex-header
  17116. org-latex-default-packages-alist
  17117. org-latex-packages-alist t
  17118. (plist-get info :latex-header)))
  17119. info)))
  17120. ;; This function borrows from Ganesh Swami's latex2png.el
  17121. (defun org-create-formula-image-with-dvipng (string tofile options buffer)
  17122. "This calls dvipng."
  17123. (require 'ox-latex)
  17124. (let* ((tmpdir (if (featurep 'xemacs)
  17125. (temp-directory)
  17126. temporary-file-directory))
  17127. (texfilebase (make-temp-name
  17128. (expand-file-name "orgtex" tmpdir)))
  17129. (texfile (concat texfilebase ".tex"))
  17130. (dvifile (concat texfilebase ".dvi"))
  17131. (pngfile (concat texfilebase ".png"))
  17132. (fnh (if (featurep 'xemacs)
  17133. (font-height (face-font 'default))
  17134. (face-attribute 'default :height nil)))
  17135. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  17136. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  17137. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  17138. "Black"))
  17139. (bg (or (plist-get options (if buffer :background :html-background))
  17140. "Transparent")))
  17141. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground))
  17142. (unless (string= fg "Transparent") (setq fg (org-dvipng-color-format fg))))
  17143. (if (eq bg 'default) (setq bg (org-dvipng-color :background))
  17144. (unless (string= bg "Transparent") (setq bg (org-dvipng-color-format bg))))
  17145. (let ((latex-header (org-create-formula--latex-header)))
  17146. (with-temp-file texfile
  17147. (insert latex-header)
  17148. (insert "\n\\begin{document}\n" string "\n\\end{document}\n")))
  17149. (let ((dir default-directory))
  17150. (condition-case nil
  17151. (progn
  17152. (cd tmpdir)
  17153. (call-process "latex" nil nil nil texfile))
  17154. (error nil))
  17155. (cd dir))
  17156. (if (not (file-exists-p dvifile))
  17157. (progn (message "Failed to create dvi file from %s" texfile) nil)
  17158. (condition-case nil
  17159. (if (featurep 'xemacs)
  17160. (call-process "dvipng" nil nil nil
  17161. "-fg" fg "-bg" bg
  17162. "-T" "tight"
  17163. "-o" pngfile
  17164. dvifile)
  17165. (call-process "dvipng" nil nil nil
  17166. "-fg" fg "-bg" bg
  17167. "-D" dpi
  17168. ;;"-x" scale "-y" scale
  17169. "-T" "tight"
  17170. "-o" pngfile
  17171. dvifile))
  17172. (error nil))
  17173. (if (not (file-exists-p pngfile))
  17174. (if org-format-latex-signal-error
  17175. (error "Failed to create png file from %s" texfile)
  17176. (message "Failed to create png file from %s" texfile)
  17177. nil)
  17178. ;; Use the requested file name and clean up
  17179. (copy-file pngfile tofile 'replace)
  17180. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png" ".out") do
  17181. (if (file-exists-p (concat texfilebase e))
  17182. (delete-file (concat texfilebase e))))
  17183. pngfile))))
  17184. (declare-function org-latex-compile "ox-latex" (texfile &optional snippet))
  17185. (defun org-create-formula-image-with-imagemagick (string tofile options buffer)
  17186. "This calls convert, which is included into imagemagick."
  17187. (require 'ox-latex)
  17188. (let* ((tmpdir (if (featurep 'xemacs)
  17189. (temp-directory)
  17190. temporary-file-directory))
  17191. (texfilebase (make-temp-name
  17192. (expand-file-name "orgtex" tmpdir)))
  17193. (texfile (concat texfilebase ".tex"))
  17194. (pdffile (concat texfilebase ".pdf"))
  17195. (pngfile (concat texfilebase ".png"))
  17196. (fnh (if (featurep 'xemacs)
  17197. (font-height (face-font 'default))
  17198. (face-attribute 'default :height nil)))
  17199. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  17200. (dpi (number-to-string (* scale (floor (if buffer fnh 120.)))))
  17201. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  17202. "black"))
  17203. (bg (or (plist-get options (if buffer :background :html-background))
  17204. "white")))
  17205. (if (eq fg 'default) (setq fg (org-latex-color :foreground))
  17206. (setq fg (org-latex-color-format fg)))
  17207. (if (eq bg 'default) (setq bg (org-latex-color :background))
  17208. (setq bg (org-latex-color-format
  17209. (if (string= bg "Transparent") "white" bg))))
  17210. (let ((latex-header (org-create-formula--latex-header)))
  17211. (with-temp-file texfile
  17212. (insert latex-header)
  17213. (insert "\n\\begin{document}\n"
  17214. "\\definecolor{fg}{rgb}{" fg "}\n"
  17215. "\\definecolor{bg}{rgb}{" bg "}\n"
  17216. "\n\\pagecolor{bg}\n"
  17217. "\n{\\color{fg}\n"
  17218. string
  17219. "\n}\n"
  17220. "\n\\end{document}\n")))
  17221. (org-latex-compile texfile t)
  17222. (if (not (file-exists-p pdffile))
  17223. (progn (message "Failed to create pdf file from %s" texfile) nil)
  17224. (condition-case nil
  17225. (if (featurep 'xemacs)
  17226. (call-process "convert" nil nil nil
  17227. "-density" "96"
  17228. "-trim"
  17229. "-antialias"
  17230. pdffile
  17231. "-quality" "100"
  17232. ;; "-sharpen" "0x1.0"
  17233. pngfile)
  17234. (call-process "convert" nil nil nil
  17235. "-density" dpi
  17236. "-trim"
  17237. "-antialias"
  17238. pdffile
  17239. "-quality" "100"
  17240. ;; "-sharpen" "0x1.0"
  17241. pngfile))
  17242. (error nil))
  17243. (if (not (file-exists-p pngfile))
  17244. (if org-format-latex-signal-error
  17245. (error "Failed to create png file from %s" texfile)
  17246. (message "Failed to create png file from %s" texfile)
  17247. nil)
  17248. ;; Use the requested file name and clean up
  17249. (copy-file pngfile tofile 'replace)
  17250. (loop for e in '(".pdf" ".tex" ".aux" ".log" ".png") do
  17251. (if (file-exists-p (concat texfilebase e))
  17252. (delete-file (concat texfilebase e))))
  17253. pngfile))))
  17254. (defun org-splice-latex-header (tpl def-pkg pkg snippets-p &optional extra)
  17255. "Fill a LaTeX header template TPL.
  17256. In the template, the following place holders will be recognized:
  17257. [DEFAULT-PACKAGES] \\usepackage statements for DEF-PKG
  17258. [NO-DEFAULT-PACKAGES] do not include DEF-PKG
  17259. [PACKAGES] \\usepackage statements for PKG
  17260. [NO-PACKAGES] do not include PKG
  17261. [EXTRA] the string EXTRA
  17262. [NO-EXTRA] do not include EXTRA
  17263. For backward compatibility, if both the positive and the negative place
  17264. holder is missing, the positive one (without the \"NO-\") will be
  17265. assumed to be present at the end of the template.
  17266. DEF-PKG and PKG are assumed to be alists of options/packagename lists.
  17267. EXTRA is a string.
  17268. SNIPPETS-P indicates if this is run to create snippet images for HTML."
  17269. (let (rpl (end ""))
  17270. (if (string-match "^[ \t]*\\[\\(NO-\\)?DEFAULT-PACKAGES\\][ \t]*\n?" tpl)
  17271. (setq rpl (if (or (match-end 1) (not def-pkg))
  17272. "" (org-latex-packages-to-string def-pkg snippets-p t))
  17273. tpl (replace-match rpl t t tpl))
  17274. (if def-pkg (setq end (org-latex-packages-to-string def-pkg snippets-p))))
  17275. (if (string-match "\\[\\(NO-\\)?PACKAGES\\][ \t]*\n?" tpl)
  17276. (setq rpl (if (or (match-end 1) (not pkg))
  17277. "" (org-latex-packages-to-string pkg snippets-p t))
  17278. tpl (replace-match rpl t t tpl))
  17279. (if pkg (setq end
  17280. (concat end "\n"
  17281. (org-latex-packages-to-string pkg snippets-p)))))
  17282. (if (string-match "\\[\\(NO-\\)?EXTRA\\][ \t]*\n?" tpl)
  17283. (setq rpl (if (or (match-end 1) (not extra))
  17284. "" (concat extra "\n"))
  17285. tpl (replace-match rpl t t tpl))
  17286. (if (and extra (string-match "\\S-" extra))
  17287. (setq end (concat end "\n" extra))))
  17288. (if (string-match "\\S-" end)
  17289. (concat tpl "\n" end)
  17290. tpl)))
  17291. (defun org-latex-packages-to-string (pkg &optional snippets-p newline)
  17292. "Turn an alist of packages into a string with the \\usepackage macros."
  17293. (setq pkg (mapconcat (lambda(p)
  17294. (cond
  17295. ((stringp p) p)
  17296. ((and snippets-p (>= (length p) 3) (not (nth 2 p)))
  17297. (format "%% Package %s omitted" (cadr p)))
  17298. ((equal "" (car p))
  17299. (format "\\usepackage{%s}" (cadr p)))
  17300. (t
  17301. (format "\\usepackage[%s]{%s}"
  17302. (car p) (cadr p)))))
  17303. pkg
  17304. "\n"))
  17305. (if newline (concat pkg "\n") pkg))
  17306. (defun org-dvipng-color (attr)
  17307. "Return a RGB color specification for dvipng."
  17308. (apply 'format "rgb %s %s %s"
  17309. (mapcar 'org-normalize-color
  17310. (if (featurep 'xemacs)
  17311. (color-rgb-components
  17312. (face-property 'default
  17313. (cond ((eq attr :foreground) 'foreground)
  17314. ((eq attr :background) 'background))))
  17315. (color-values (face-attribute 'default attr nil))))))
  17316. (defun org-dvipng-color-format (color-name)
  17317. "Convert COLOR-NAME to a RGB color value for dvipng."
  17318. (apply 'format "rgb %s %s %s"
  17319. (mapcar 'org-normalize-color
  17320. (color-values color-name))))
  17321. (defun org-latex-color (attr)
  17322. "Return a RGB color for the LaTeX color package."
  17323. (apply 'format "%s,%s,%s"
  17324. (mapcar 'org-normalize-color
  17325. (if (featurep 'xemacs)
  17326. (color-rgb-components
  17327. (face-property 'default
  17328. (cond ((eq attr :foreground) 'foreground)
  17329. ((eq attr :background) 'background))))
  17330. (color-values (face-attribute 'default attr nil))))))
  17331. (defun org-latex-color-format (color-name)
  17332. "Convert COLOR-NAME to a RGB color value."
  17333. (apply 'format "%s,%s,%s"
  17334. (mapcar 'org-normalize-color
  17335. (color-values color-name))))
  17336. (defun org-normalize-color (value)
  17337. "Return string to be used as color value for an RGB component."
  17338. (format "%g" (/ value 65535.0)))
  17339. ;; Image display
  17340. (defvar org-inline-image-overlays nil)
  17341. (make-variable-buffer-local 'org-inline-image-overlays)
  17342. (defun org-toggle-inline-images (&optional include-linked)
  17343. "Toggle the display of inline images.
  17344. INCLUDE-LINKED is passed to `org-display-inline-images'."
  17345. (interactive "P")
  17346. (if org-inline-image-overlays
  17347. (progn
  17348. (org-remove-inline-images)
  17349. (message "Inline image display turned off"))
  17350. (org-display-inline-images include-linked)
  17351. (if (and (org-called-interactively-p)
  17352. org-inline-image-overlays)
  17353. (message "%d images displayed inline"
  17354. (length org-inline-image-overlays))
  17355. (message "No images to display inline"))))
  17356. (defun org-redisplay-inline-images ()
  17357. "Refresh the display of inline images."
  17358. (interactive)
  17359. (if (not org-inline-image-overlays)
  17360. (org-toggle-inline-images)
  17361. (org-toggle-inline-images)
  17362. (org-toggle-inline-images)))
  17363. (defun org-display-inline-images (&optional include-linked refresh beg end)
  17364. "Display inline images.
  17365. Normally only links without a description part are inlined, because this
  17366. is how it will work for export. When INCLUDE-LINKED is set, also links
  17367. with a description part will be inlined. This can be nice for a quick
  17368. look at those images, but it does not reflect what exported files will look
  17369. like.
  17370. When REFRESH is set, refresh existing images between BEG and END.
  17371. This will create new image displays only if necessary.
  17372. BEG and END default to the buffer boundaries."
  17373. (interactive "P")
  17374. (when (display-graphic-p)
  17375. (unless refresh
  17376. (org-remove-inline-images)
  17377. (if (fboundp 'clear-image-cache) (clear-image-cache)))
  17378. (save-excursion
  17379. (save-restriction
  17380. (widen)
  17381. (setq beg (or beg (point-min)) end (or end (point-max)))
  17382. (goto-char beg)
  17383. (let ((re (concat "\\[\\[\\(\\(file:\\)\\|\\([./~]\\)\\)\\([^]\n]+?"
  17384. (substring (org-image-file-name-regexp) 0 -2)
  17385. "\\)\\]" (if include-linked "" "\\]")))
  17386. (case-fold-search t)
  17387. old file ov img type attrwidth width)
  17388. (while (re-search-forward re end t)
  17389. (setq old (get-char-property-and-overlay (match-beginning 1)
  17390. 'org-image-overlay)
  17391. file (expand-file-name
  17392. (concat (or (match-string 3) "") (match-string 4))))
  17393. (when (image-type-available-p 'imagemagick)
  17394. (setq attrwidth (if (or (listp org-image-actual-width)
  17395. (null org-image-actual-width))
  17396. (save-excursion
  17397. (save-match-data
  17398. (when (re-search-backward
  17399. "#\\+attr.*:width[ \t]+\\([^ ]+\\)"
  17400. (save-excursion
  17401. (re-search-backward "^[ \t]*$\\|\\`" nil t)) t)
  17402. (string-to-number (match-string 1))))))
  17403. width (cond ((eq org-image-actual-width t) nil)
  17404. ((null org-image-actual-width) attrwidth)
  17405. ((numberp org-image-actual-width)
  17406. org-image-actual-width)
  17407. ((listp org-image-actual-width)
  17408. (or attrwidth (car org-image-actual-width))))
  17409. type (if width 'imagemagick)))
  17410. (when (file-exists-p file)
  17411. (if (and (car-safe old) refresh)
  17412. (image-refresh (overlay-get (cdr old) 'display))
  17413. (setq img (save-match-data (create-image file type nil :width width)))
  17414. (when img
  17415. (setq ov (make-overlay (match-beginning 0) (match-end 0)))
  17416. (overlay-put ov 'display img)
  17417. (overlay-put ov 'face 'default)
  17418. (overlay-put ov 'org-image-overlay t)
  17419. (overlay-put ov 'modification-hooks
  17420. (list 'org-display-inline-remove-overlay))
  17421. (push ov org-inline-image-overlays))))))))))
  17422. (define-obsolete-function-alias
  17423. 'org-display-inline-modification-hook 'org-display-inline-remove-overlay "24.3")
  17424. (defun org-display-inline-remove-overlay (ov after beg end &optional len)
  17425. "Remove inline-display overlay if a corresponding region is modified."
  17426. (let ((inhibit-modification-hooks t))
  17427. (when (and ov after)
  17428. (delete ov org-inline-image-overlays)
  17429. (delete-overlay ov))))
  17430. (defun org-remove-inline-images ()
  17431. "Remove inline display of images."
  17432. (interactive)
  17433. (mapc 'delete-overlay org-inline-image-overlays)
  17434. (setq org-inline-image-overlays nil))
  17435. ;;;; Key bindings
  17436. ;; Outline functions from `outline-mode-prefix-map'
  17437. ;; that can be remapped in Org:
  17438. (define-key org-mode-map [remap outline-mark-subtree] 'org-mark-subtree)
  17439. (define-key org-mode-map [remap show-subtree] 'org-show-subtree)
  17440. (define-key org-mode-map [remap outline-forward-same-level]
  17441. 'org-forward-heading-same-level)
  17442. (define-key org-mode-map [remap outline-backward-same-level]
  17443. 'org-backward-heading-same-level)
  17444. (define-key org-mode-map [remap show-branches]
  17445. 'org-kill-note-or-show-branches)
  17446. (define-key org-mode-map [remap outline-promote] 'org-promote-subtree)
  17447. (define-key org-mode-map [remap outline-demote] 'org-demote-subtree)
  17448. (define-key org-mode-map [remap outline-insert-heading] 'org-ctrl-c-ret)
  17449. ;; Outline functions from `outline-mode-prefix-map' that can not
  17450. ;; be remapped in Org:
  17451. ;;
  17452. ;; - the column "key binding" shows whether the Outline function is still
  17453. ;; available in Org mode on the same key that it has been bound to in
  17454. ;; Outline mode:
  17455. ;; - "overridden": key used for a different functionality in Org mode
  17456. ;; - else: key still bound to the same Outline function in Org mode
  17457. ;;
  17458. ;; | Outline function | key binding | Org replacement |
  17459. ;; |------------------------------------+-------------+-----------------------|
  17460. ;; | `outline-next-visible-heading' | `C-c C-n' | still same function |
  17461. ;; | `outline-previous-visible-heading' | `C-c C-p' | still same function |
  17462. ;; | `outline-up-heading' | `C-c C-u' | still same function |
  17463. ;; | `outline-move-subtree-up' | overridden | better: org-shiftup |
  17464. ;; | `outline-move-subtree-down' | overridden | better: org-shiftdown |
  17465. ;; | `show-entry' | overridden | no replacement |
  17466. ;; | `show-children' | `C-c C-i' | visibility cycling |
  17467. ;; | `show-branches' | `C-c C-k' | still same function |
  17468. ;; | `show-subtree' | overridden | visibility cycling |
  17469. ;; | `show-all' | overridden | no replacement |
  17470. ;; | `hide-subtree' | overridden | visibility cycling |
  17471. ;; | `hide-body' | overridden | no replacement |
  17472. ;; | `hide-entry' | overridden | visibility cycling |
  17473. ;; | `hide-leaves' | overridden | no replacement |
  17474. ;; | `hide-sublevels' | overridden | no replacement |
  17475. ;; | `hide-other' | overridden | no replacement |
  17476. ;; Make `C-c C-x' a prefix key
  17477. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  17478. ;; TAB key with modifiers
  17479. (org-defkey org-mode-map "\C-i" 'org-cycle)
  17480. (org-defkey org-mode-map [(tab)] 'org-cycle)
  17481. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  17482. (org-defkey org-mode-map "\M-\t" 'pcomplete)
  17483. ;; The following line is necessary under Suse GNU/Linux
  17484. (unless (featurep 'xemacs)
  17485. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  17486. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  17487. (define-key org-mode-map [backtab] 'org-shifttab)
  17488. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  17489. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  17490. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  17491. ;; Cursor keys with modifiers
  17492. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  17493. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  17494. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  17495. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  17496. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  17497. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  17498. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  17499. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  17500. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  17501. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  17502. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  17503. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  17504. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  17505. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  17506. (org-defkey org-mode-map [(control shift up)] 'org-shiftcontrolup)
  17507. (org-defkey org-mode-map [(control shift down)] 'org-shiftcontroldown)
  17508. ;; Babel keys
  17509. (define-key org-mode-map org-babel-key-prefix org-babel-map)
  17510. (mapc (lambda (pair)
  17511. (define-key org-babel-map (car pair) (cdr pair)))
  17512. org-babel-key-bindings)
  17513. ;;; Extra keys for tty access.
  17514. ;; We only set them when really needed because otherwise the
  17515. ;; menus don't show the simple keys
  17516. (when (or org-use-extra-keys
  17517. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  17518. (not window-system))
  17519. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  17520. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  17521. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  17522. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  17523. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  17524. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  17525. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  17526. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  17527. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  17528. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  17529. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  17530. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  17531. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  17532. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  17533. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  17534. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  17535. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  17536. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  17537. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  17538. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  17539. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  17540. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
  17541. (org-defkey org-mode-map [?\e (tab)] 'pcomplete)
  17542. (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
  17543. (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
  17544. (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
  17545. (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
  17546. (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
  17547. ;; All the other keys
  17548. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  17549. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  17550. (if (boundp 'narrow-map)
  17551. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  17552. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  17553. (if (boundp 'narrow-map)
  17554. (org-defkey narrow-map "b" 'org-narrow-to-block)
  17555. (org-defkey org-mode-map "\C-xnb" 'org-narrow-to-block))
  17556. (if (boundp 'narrow-map)
  17557. (org-defkey narrow-map "e" 'org-narrow-to-element)
  17558. (org-defkey org-mode-map "\C-xne" 'org-narrow-to-element))
  17559. (org-defkey org-mode-map "\C-\M-t" 'org-transpose-element)
  17560. (org-defkey org-mode-map "\M-}" 'org-forward-element)
  17561. (org-defkey org-mode-map "\M-{" 'org-backward-element)
  17562. (org-defkey org-mode-map "\C-c\C-^" 'org-up-element)
  17563. (org-defkey org-mode-map "\C-c\C-_" 'org-down-element)
  17564. (org-defkey org-mode-map "\C-c\C-f" 'org-forward-heading-same-level)
  17565. (org-defkey org-mode-map "\C-c\C-b" 'org-backward-heading-same-level)
  17566. (org-defkey org-mode-map "\C-c\M-f" 'org-next-block)
  17567. (org-defkey org-mode-map "\C-c\M-b" 'org-previous-block)
  17568. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  17569. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  17570. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
  17571. (org-defkey org-mode-map "\C-c\C-xd" 'org-insert-drawer)
  17572. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  17573. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  17574. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  17575. (org-defkey org-mode-map "\C-c\C-xq" 'org-toggle-tags-groups)
  17576. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  17577. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  17578. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  17579. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  17580. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  17581. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  17582. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  17583. (org-defkey org-mode-map "\C-c\M-w" 'org-copy)
  17584. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  17585. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  17586. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  17587. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  17588. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  17589. (org-defkey org-mode-map "\C-c\C-xv" 'org-copy-visible)
  17590. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  17591. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  17592. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  17593. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  17594. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  17595. (org-defkey org-mode-map "\C-c\C-\M-l" 'org-insert-all-links)
  17596. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  17597. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  17598. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  17599. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  17600. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  17601. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  17602. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  17603. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  17604. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  17605. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  17606. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  17607. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  17608. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  17609. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  17610. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  17611. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  17612. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  17613. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  17614. (org-defkey org-mode-map "\C-c^" 'org-sort)
  17615. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  17616. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  17617. (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
  17618. (org-defkey org-mode-map [remap open-line] 'org-open-line)
  17619. (org-defkey org-mode-map [remap forward-paragraph] 'org-forward-paragraph)
  17620. (org-defkey org-mode-map [remap backward-paragraph] 'org-backward-paragraph)
  17621. (org-defkey org-mode-map "\C-m" 'org-return)
  17622. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  17623. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  17624. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  17625. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  17626. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  17627. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  17628. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  17629. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  17630. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  17631. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  17632. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  17633. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  17634. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  17635. (org-defkey org-mode-map "\C-c\C-e" 'org-export-dispatch)
  17636. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  17637. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  17638. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  17639. (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
  17640. (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
  17641. (org-defkey org-mode-map "\C-c@" 'org-mark-subtree)
  17642. (org-defkey org-mode-map "\M-h" 'org-mark-element)
  17643. (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
  17644. ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
  17645. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  17646. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  17647. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  17648. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  17649. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  17650. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-in-last)
  17651. (org-defkey org-mode-map "\C-c\C-x\C-z" 'org-resolve-clocks)
  17652. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  17653. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  17654. (org-defkey org-mode-map "\C-c\C-x\C-q" 'org-clock-cancel)
  17655. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  17656. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  17657. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  17658. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  17659. (org-defkey org-mode-map "\C-c\C-x\C-v" 'org-toggle-inline-images)
  17660. (org-defkey org-mode-map "\C-c\C-x\C-\M-v" 'org-redisplay-inline-images)
  17661. (org-defkey org-mode-map "\C-c\C-x\\" 'org-toggle-pretty-entities)
  17662. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  17663. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  17664. (org-defkey org-mode-map "\C-c\C-xP" 'org-set-property-and-value)
  17665. (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
  17666. (org-defkey org-mode-map "\C-c\C-xE" 'org-inc-effort)
  17667. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  17668. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  17669. (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
  17670. (org-defkey org-mode-map [(control ?c) (control ?x) ?\:] 'org-timer-cancel-timer)
  17671. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  17672. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  17673. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  17674. (org-defkey org-mode-map "\C-c\C-x_" 'org-timer-stop)
  17675. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  17676. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  17677. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  17678. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  17679. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  17680. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  17681. (when (featurep 'xemacs)
  17682. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  17683. (defconst org-speed-commands-default
  17684. '(
  17685. ("Outline Navigation")
  17686. ("n" . (org-speed-move-safe 'outline-next-visible-heading))
  17687. ("p" . (org-speed-move-safe 'outline-previous-visible-heading))
  17688. ("f" . (org-speed-move-safe 'org-forward-heading-same-level))
  17689. ("b" . (org-speed-move-safe 'org-backward-heading-same-level))
  17690. ("F" . org-next-block)
  17691. ("B" . org-previous-block)
  17692. ("u" . (org-speed-move-safe 'outline-up-heading))
  17693. ("j" . org-goto)
  17694. ("g" . (org-refile t))
  17695. ("Outline Visibility")
  17696. ("c" . org-cycle)
  17697. ("C" . org-shifttab)
  17698. (" " . org-display-outline-path)
  17699. ("s" . org-narrow-to-subtree)
  17700. ("=" . org-columns)
  17701. ("Outline Structure Editing")
  17702. ("U" . org-shiftmetaup)
  17703. ("D" . org-shiftmetadown)
  17704. ("r" . org-metaright)
  17705. ("l" . org-metaleft)
  17706. ("R" . org-shiftmetaright)
  17707. ("L" . org-shiftmetaleft)
  17708. ("i" . (progn (forward-char 1) (call-interactively
  17709. 'org-insert-heading-respect-content)))
  17710. ("^" . org-sort)
  17711. ("w" . org-refile)
  17712. ("a" . org-archive-subtree-default-with-confirmation)
  17713. ("@" . org-mark-subtree)
  17714. ("#" . org-toggle-comment)
  17715. ("Clock Commands")
  17716. ("I" . org-clock-in)
  17717. ("O" . org-clock-out)
  17718. ("Meta Data Editing")
  17719. ("t" . org-todo)
  17720. ("," . (org-priority))
  17721. ("0" . (org-priority ?\ ))
  17722. ("1" . (org-priority ?A))
  17723. ("2" . (org-priority ?B))
  17724. ("3" . (org-priority ?C))
  17725. (":" . org-set-tags-command)
  17726. ("e" . org-set-effort)
  17727. ("E" . org-inc-effort)
  17728. ("W" . (lambda(m) (interactive "sMinutes before warning: ")
  17729. (org-entry-put (point) "APPT_WARNTIME" m)))
  17730. ("Agenda Views etc")
  17731. ("v" . org-agenda)
  17732. ("/" . org-sparse-tree)
  17733. ("Misc")
  17734. ("o" . org-open-at-point)
  17735. ("?" . org-speed-command-help)
  17736. ("<" . (org-agenda-set-restriction-lock 'subtree))
  17737. (">" . (org-agenda-remove-restriction-lock))
  17738. )
  17739. "The default speed commands.")
  17740. (defun org-print-speed-command (e)
  17741. (if (> (length (car e)) 1)
  17742. (progn
  17743. (princ "\n")
  17744. (princ (car e))
  17745. (princ "\n")
  17746. (princ (make-string (length (car e)) ?-))
  17747. (princ "\n"))
  17748. (princ (car e))
  17749. (princ " ")
  17750. (if (symbolp (cdr e))
  17751. (princ (symbol-name (cdr e)))
  17752. (prin1 (cdr e)))
  17753. (princ "\n")))
  17754. (defun org-speed-command-help ()
  17755. "Show the available speed commands."
  17756. (interactive)
  17757. (if (not org-use-speed-commands)
  17758. (user-error "Speed commands are not activated, customize `org-use-speed-commands'")
  17759. (with-output-to-temp-buffer "*Help*"
  17760. (princ "User-defined Speed commands\n===========================\n")
  17761. (mapc 'org-print-speed-command org-speed-commands-user)
  17762. (princ "\n")
  17763. (princ "Built-in Speed commands\n=======================\n")
  17764. (mapc 'org-print-speed-command org-speed-commands-default))
  17765. (with-current-buffer "*Help*"
  17766. (setq truncate-lines t))))
  17767. (defun org-speed-move-safe (cmd)
  17768. "Execute CMD, but make sure that the cursor always ends up in a headline.
  17769. If not, return to the original position and throw an error."
  17770. (interactive)
  17771. (let ((pos (point)))
  17772. (call-interactively cmd)
  17773. (unless (and (bolp) (org-at-heading-p))
  17774. (goto-char pos)
  17775. (error "Boundary reached while executing %s" cmd))))
  17776. (defvar org-self-insert-command-undo-counter 0)
  17777. (defvar org-table-auto-blank-field) ; defined in org-table.el
  17778. (defvar org-speed-command nil)
  17779. (define-obsolete-function-alias
  17780. 'org-speed-command-default-hook 'org-speed-command-activate "24.3")
  17781. (defun org-speed-command-activate (keys)
  17782. "Hook for activating single-letter speed commands.
  17783. `org-speed-commands-default' specifies a minimal command set.
  17784. Use `org-speed-commands-user' for further customization."
  17785. (when (or (and (bolp) (looking-at org-outline-regexp))
  17786. (and (functionp org-use-speed-commands)
  17787. (funcall org-use-speed-commands)))
  17788. (cdr (assoc keys (append org-speed-commands-user
  17789. org-speed-commands-default)))))
  17790. (define-obsolete-function-alias
  17791. 'org-babel-speed-command-hook 'org-babel-speed-command-activate "24.3")
  17792. (defun org-babel-speed-command-activate (keys)
  17793. "Hook for activating single-letter code block commands."
  17794. (when (and (bolp) (looking-at org-babel-src-block-regexp))
  17795. (cdr (assoc keys org-babel-key-bindings))))
  17796. (defcustom org-speed-command-hook
  17797. '(org-speed-command-default-hook org-babel-speed-command-hook)
  17798. "Hook for activating speed commands at strategic locations.
  17799. Hook functions are called in sequence until a valid handler is
  17800. found.
  17801. Each hook takes a single argument, a user-pressed command key
  17802. which is also a `self-insert-command' from the global map.
  17803. Within the hook, examine the cursor position and the command key
  17804. and return nil or a valid handler as appropriate. Handler could
  17805. be one of an interactive command, a function, or a form.
  17806. Set `org-use-speed-commands' to non-nil value to enable this
  17807. hook. The default setting is `org-speed-command-activate'."
  17808. :group 'org-structure
  17809. :version "24.1"
  17810. :type 'hook)
  17811. (defun org-self-insert-command (N)
  17812. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  17813. If the cursor is in a table looking at whitespace, the whitespace is
  17814. overwritten, and the table is not marked as requiring realignment."
  17815. (interactive "p")
  17816. (org-check-before-invisible-edit 'insert)
  17817. (cond
  17818. ((and org-use-speed-commands
  17819. (setq org-speed-command
  17820. (run-hook-with-args-until-success
  17821. 'org-speed-command-hook (this-command-keys))))
  17822. (cond
  17823. ((commandp org-speed-command)
  17824. (setq this-command org-speed-command)
  17825. (call-interactively org-speed-command))
  17826. ((functionp org-speed-command)
  17827. (funcall org-speed-command))
  17828. ((and org-speed-command (listp org-speed-command))
  17829. (eval org-speed-command))
  17830. (t (let (org-use-speed-commands)
  17831. (call-interactively 'org-self-insert-command)))))
  17832. ((and
  17833. (org-table-p)
  17834. (progn
  17835. ;; check if we blank the field, and if that triggers align
  17836. (and (featurep 'org-table) org-table-auto-blank-field
  17837. (member last-command
  17838. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c yas/expand))
  17839. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  17840. ;; got extra space, this field does not determine column width
  17841. (let (org-table-may-need-update) (org-table-blank-field))
  17842. ;; no extra space, this field may determine column width
  17843. (org-table-blank-field)))
  17844. t)
  17845. (eq N 1)
  17846. (looking-at "[^|\n]* |"))
  17847. (let (org-table-may-need-update)
  17848. (goto-char (1- (match-end 0)))
  17849. (backward-delete-char 1)
  17850. (goto-char (match-beginning 0))
  17851. (self-insert-command N)))
  17852. (t
  17853. (setq org-table-may-need-update t)
  17854. (self-insert-command N)
  17855. (org-fix-tags-on-the-fly)
  17856. (if org-self-insert-cluster-for-undo
  17857. (if (not (eq last-command 'org-self-insert-command))
  17858. (setq org-self-insert-command-undo-counter 1)
  17859. (if (>= org-self-insert-command-undo-counter 20)
  17860. (setq org-self-insert-command-undo-counter 1)
  17861. (and (> org-self-insert-command-undo-counter 0)
  17862. buffer-undo-list (listp buffer-undo-list)
  17863. (not (cadr buffer-undo-list)) ; remove nil entry
  17864. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  17865. (setq org-self-insert-command-undo-counter
  17866. (1+ org-self-insert-command-undo-counter))))))))
  17867. (defun org-check-before-invisible-edit (kind)
  17868. "Check is editing if kind KIND would be dangerous with invisible text around.
  17869. The detailed reaction depends on the user option `org-catch-invisible-edits'."
  17870. ;; First, try to get out of here as quickly as possible, to reduce overhead
  17871. (if (and org-catch-invisible-edits
  17872. (or (not (boundp 'visible-mode)) (not visible-mode))
  17873. (or (get-char-property (point) 'invisible)
  17874. (get-char-property (max (point-min) (1- (point))) 'invisible)))
  17875. ;; OK, we need to take a closer look
  17876. (let* ((invisible-at-point (get-char-property (point) 'invisible))
  17877. (invisible-before-point (if (bobp) nil (get-char-property
  17878. (1- (point)) 'invisible)))
  17879. (border-and-ok-direction
  17880. (or
  17881. ;; Check if we are acting predictably before invisible text
  17882. (and invisible-at-point (not invisible-before-point)
  17883. (memq kind '(insert delete-backward)))
  17884. ;; Check if we are acting predictably after invisible text
  17885. ;; This works not well, and I have turned it off. It seems
  17886. ;; better to always show and stop after invisible text.
  17887. ;; (and (not invisible-at-point) invisible-before-point
  17888. ;; (memq kind '(insert delete)))
  17889. )))
  17890. (when (or (memq invisible-at-point '(outline org-hide-block t))
  17891. (memq invisible-before-point '(outline org-hide-block t)))
  17892. (if (eq org-catch-invisible-edits 'error)
  17893. (user-error "Editing in invisible areas is prohibited, make them visible first"))
  17894. (if (and org-custom-properties-overlays
  17895. (y-or-n-p "Display invisible properties in this buffer? "))
  17896. (org-toggle-custom-properties-visibility)
  17897. ;; Make the area visible
  17898. (save-excursion
  17899. (if invisible-before-point
  17900. (goto-char (previous-single-char-property-change
  17901. (point) 'invisible)))
  17902. (show-subtree))
  17903. (cond
  17904. ((eq org-catch-invisible-edits 'show)
  17905. ;; That's it, we do the edit after showing
  17906. (message
  17907. "Unfolding invisible region around point before editing")
  17908. (sit-for 1))
  17909. ((and (eq org-catch-invisible-edits 'smart)
  17910. border-and-ok-direction)
  17911. (message "Unfolding invisible region around point before editing"))
  17912. (t
  17913. ;; Don't do the edit, make the user repeat it in full visibility
  17914. (user-error "Edit in invisible region aborted, repeat to confirm with text visible"))))))))
  17915. (defun org-fix-tags-on-the-fly ()
  17916. (when (and (equal (char-after (point-at-bol)) ?*)
  17917. (org-at-heading-p))
  17918. (org-align-tags-here org-tags-column)))
  17919. (defun org-delete-backward-char (N)
  17920. "Like `delete-backward-char', insert whitespace at field end in tables.
  17921. When deleting backwards, in tables this function will insert whitespace in
  17922. front of the next \"|\" separator, to keep the table aligned. The table will
  17923. still be marked for re-alignment if the field did fill the entire column,
  17924. because, in this case the deletion might narrow the column."
  17925. (interactive "p")
  17926. (save-match-data
  17927. (org-check-before-invisible-edit 'delete-backward)
  17928. (if (and (org-table-p)
  17929. (eq N 1)
  17930. (string-match "|" (buffer-substring (point-at-bol) (point)))
  17931. (looking-at ".*?|"))
  17932. (let ((pos (point))
  17933. (noalign (looking-at "[^|\n\r]* |"))
  17934. (c org-table-may-need-update))
  17935. (backward-delete-char N)
  17936. (if (not overwrite-mode)
  17937. (progn
  17938. (skip-chars-forward "^|")
  17939. (insert " ")
  17940. (goto-char (1- pos))))
  17941. ;; noalign: if there were two spaces at the end, this field
  17942. ;; does not determine the width of the column.
  17943. (if noalign (setq org-table-may-need-update c)))
  17944. (backward-delete-char N)
  17945. (org-fix-tags-on-the-fly))))
  17946. (defun org-delete-char (N)
  17947. "Like `delete-char', but insert whitespace at field end in tables.
  17948. When deleting characters, in tables this function will insert whitespace in
  17949. front of the next \"|\" separator, to keep the table aligned. The table will
  17950. still be marked for re-alignment if the field did fill the entire column,
  17951. because, in this case the deletion might narrow the column."
  17952. (interactive "p")
  17953. (save-match-data
  17954. (org-check-before-invisible-edit 'delete)
  17955. (if (and (org-table-p)
  17956. (not (bolp))
  17957. (not (= (char-after) ?|))
  17958. (eq N 1))
  17959. (if (looking-at ".*?|")
  17960. (let ((pos (point))
  17961. (noalign (looking-at "[^|\n\r]* |"))
  17962. (c org-table-may-need-update))
  17963. (replace-match
  17964. (concat (substring (match-string 0) 1 -1) " |") nil t)
  17965. (goto-char pos)
  17966. ;; noalign: if there were two spaces at the end, this field
  17967. ;; does not determine the width of the column.
  17968. (if noalign (setq org-table-may-need-update c)))
  17969. (delete-char N))
  17970. (delete-char N)
  17971. (org-fix-tags-on-the-fly))))
  17972. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  17973. (put 'org-self-insert-command 'delete-selection
  17974. (lambda ()
  17975. (not (run-hook-with-args-until-success
  17976. 'self-insert-uses-region-functions))))
  17977. (put 'orgtbl-self-insert-command 'delete-selection
  17978. (lambda ()
  17979. (not (run-hook-with-args-until-success
  17980. 'self-insert-uses-region-functions))))
  17981. (put 'org-delete-char 'delete-selection 'supersede)
  17982. (put 'org-delete-backward-char 'delete-selection 'supersede)
  17983. (put 'org-yank 'delete-selection 'yank)
  17984. ;; Make `flyspell-mode' delay after some commands
  17985. (put 'org-self-insert-command 'flyspell-delayed t)
  17986. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  17987. (put 'org-delete-char 'flyspell-delayed t)
  17988. (put 'org-delete-backward-char 'flyspell-delayed t)
  17989. ;; Make pabbrev-mode expand after org-mode commands
  17990. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  17991. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  17992. (defun org-remap (map &rest commands)
  17993. "In MAP, remap the functions given in COMMANDS.
  17994. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  17995. (let (new old)
  17996. (while commands
  17997. (setq old (pop commands) new (pop commands))
  17998. (if (fboundp 'command-remapping)
  17999. (org-defkey map (vector 'remap old) new)
  18000. (substitute-key-definition old new map global-map)))))
  18001. (defun org-transpose-words ()
  18002. "Transpose words for Org.
  18003. This uses the `org-mode-transpose-word-syntax-table' syntax
  18004. table, which interprets characters in `org-emphasis-alist' as
  18005. word constituents."
  18006. (interactive)
  18007. (with-syntax-table org-mode-transpose-word-syntax-table
  18008. (call-interactively 'transpose-words)))
  18009. (org-remap org-mode-map 'transpose-words 'org-transpose-words)
  18010. (when (eq org-enable-table-editor 'optimized)
  18011. ;; If the user wants maximum table support, we need to hijack
  18012. ;; some standard editing functions
  18013. (org-remap org-mode-map
  18014. 'self-insert-command 'org-self-insert-command
  18015. 'delete-char 'org-delete-char
  18016. 'delete-backward-char 'org-delete-backward-char)
  18017. (org-defkey org-mode-map "|" 'org-force-self-insert))
  18018. (defvar org-ctrl-c-ctrl-c-hook nil
  18019. "Hook for functions attaching themselves to `C-c C-c'.
  18020. This can be used to add additional functionality to the C-c C-c
  18021. key which executes context-dependent commands. This hook is run
  18022. before any other test, while `org-ctrl-c-ctrl-c-final-hook' is
  18023. run after the last test.
  18024. Each function will be called with no arguments. The function
  18025. must check if the context is appropriate for it to act. If yes,
  18026. it should do its thing and then return a non-nil value. If the
  18027. context is wrong, just do nothing and return nil.")
  18028. (defvar org-ctrl-c-ctrl-c-final-hook nil
  18029. "Hook for functions attaching themselves to `C-c C-c'.
  18030. This can be used to add additional functionality to the C-c C-c
  18031. key which executes context-dependent commands. This hook is run
  18032. after any other test, while `org-ctrl-c-ctrl-c-hook' is run
  18033. before the first test.
  18034. Each function will be called with no arguments. The function
  18035. must check if the context is appropriate for it to act. If yes,
  18036. it should do its thing and then return a non-nil value. If the
  18037. context is wrong, just do nothing and return nil.")
  18038. (defvar org-tab-first-hook nil
  18039. "Hook for functions to attach themselves to TAB.
  18040. See `org-ctrl-c-ctrl-c-hook' for more information.
  18041. This hook runs as the first action when TAB is pressed, even before
  18042. `org-cycle' messes around with the `outline-regexp' to cater for
  18043. inline tasks and plain list item folding.
  18044. If any function in this hook returns t, any other actions that
  18045. would have been caused by TAB (such as table field motion or visibility
  18046. cycling) will not occur.")
  18047. (defvar org-tab-after-check-for-table-hook nil
  18048. "Hook for functions to attach themselves to TAB.
  18049. See `org-ctrl-c-ctrl-c-hook' for more information.
  18050. This hook runs after it has been established that the cursor is not in a
  18051. table, but before checking if the cursor is in a headline or if global cycling
  18052. should be done.
  18053. If any function in this hook returns t, not other actions like visibility
  18054. cycling will be done.")
  18055. (defvar org-tab-after-check-for-cycling-hook nil
  18056. "Hook for functions to attach themselves to TAB.
  18057. See `org-ctrl-c-ctrl-c-hook' for more information.
  18058. This hook runs after it has been established that not table field motion and
  18059. not visibility should be done because of current context. This is probably
  18060. the place where a package like yasnippets can hook in.")
  18061. (defvar org-tab-before-tab-emulation-hook nil
  18062. "Hook for functions to attach themselves to TAB.
  18063. See `org-ctrl-c-ctrl-c-hook' for more information.
  18064. This hook runs after every other options for TAB have been exhausted, but
  18065. before indentation and \t insertion takes place.")
  18066. (defvar org-metaleft-hook nil
  18067. "Hook for functions attaching themselves to `M-left'.
  18068. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18069. (defvar org-metaright-hook nil
  18070. "Hook for functions attaching themselves to `M-right'.
  18071. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18072. (defvar org-metaup-hook nil
  18073. "Hook for functions attaching themselves to `M-up'.
  18074. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18075. (defvar org-metadown-hook nil
  18076. "Hook for functions attaching themselves to `M-down'.
  18077. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18078. (defvar org-shiftmetaleft-hook nil
  18079. "Hook for functions attaching themselves to `M-S-left'.
  18080. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18081. (defvar org-shiftmetaright-hook nil
  18082. "Hook for functions attaching themselves to `M-S-right'.
  18083. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18084. (defvar org-shiftmetaup-hook nil
  18085. "Hook for functions attaching themselves to `M-S-up'.
  18086. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18087. (defvar org-shiftmetadown-hook nil
  18088. "Hook for functions attaching themselves to `M-S-down'.
  18089. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18090. (defvar org-metareturn-hook nil
  18091. "Hook for functions attaching themselves to `M-RET'.
  18092. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18093. (defvar org-shiftup-hook nil
  18094. "Hook for functions attaching themselves to `S-up'.
  18095. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18096. (defvar org-shiftup-final-hook nil
  18097. "Hook for functions attaching themselves to `S-up'.
  18098. This one runs after all other options except shift-select have been excluded.
  18099. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18100. (defvar org-shiftdown-hook nil
  18101. "Hook for functions attaching themselves to `S-down'.
  18102. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18103. (defvar org-shiftdown-final-hook nil
  18104. "Hook for functions attaching themselves to `S-down'.
  18105. This one runs after all other options except shift-select have been excluded.
  18106. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18107. (defvar org-shiftleft-hook nil
  18108. "Hook for functions attaching themselves to `S-left'.
  18109. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18110. (defvar org-shiftleft-final-hook nil
  18111. "Hook for functions attaching themselves to `S-left'.
  18112. This one runs after all other options except shift-select have been excluded.
  18113. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18114. (defvar org-shiftright-hook nil
  18115. "Hook for functions attaching themselves to `S-right'.
  18116. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18117. (defvar org-shiftright-final-hook nil
  18118. "Hook for functions attaching themselves to `S-right'.
  18119. This one runs after all other options except shift-select have been excluded.
  18120. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18121. (defun org-modifier-cursor-error ()
  18122. "Throw an error, a modified cursor command was applied in wrong context."
  18123. (user-error "This command is active in special context like tables, headlines or items"))
  18124. (defun org-shiftselect-error ()
  18125. "Throw an error because Shift-Cursor command was applied in wrong context."
  18126. (if (and (boundp 'shift-select-mode) shift-select-mode)
  18127. (user-error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
  18128. (user-error "This command works only in special context like headlines or timestamps")))
  18129. (defun org-call-for-shift-select (cmd)
  18130. (let ((this-command-keys-shift-translated t))
  18131. (call-interactively cmd)))
  18132. (defun org-shifttab (&optional arg)
  18133. "Global visibility cycling or move to previous table field.
  18134. Call `org-table-previous-field' within a table.
  18135. When ARG is nil, cycle globally through visibility states.
  18136. When ARG is a numeric prefix, show contents of this level."
  18137. (interactive "P")
  18138. (cond
  18139. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  18140. ((integerp arg)
  18141. (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
  18142. (message "Content view to level: %d" arg)
  18143. (org-content (prefix-numeric-value arg2))
  18144. (org-cycle-show-empty-lines t)
  18145. (setq org-cycle-global-status 'overview)))
  18146. (t (call-interactively 'org-global-cycle))))
  18147. (defun org-shiftmetaleft ()
  18148. "Promote subtree or delete table column.
  18149. Calls `org-promote-subtree', `org-outdent-item-tree', or
  18150. `org-table-delete-column', depending on context. See the
  18151. individual commands for more information."
  18152. (interactive)
  18153. (cond
  18154. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  18155. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  18156. ((org-at-heading-p) (call-interactively 'org-promote-subtree))
  18157. ((if (not (org-region-active-p)) (org-at-item-p)
  18158. (save-excursion (goto-char (region-beginning))
  18159. (org-at-item-p)))
  18160. (call-interactively 'org-outdent-item-tree))
  18161. (t (org-modifier-cursor-error))))
  18162. (defun org-shiftmetaright ()
  18163. "Demote subtree or insert table column.
  18164. Calls `org-demote-subtree', `org-indent-item-tree', or
  18165. `org-table-insert-column', depending on context. See the
  18166. individual commands for more information."
  18167. (interactive)
  18168. (cond
  18169. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  18170. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  18171. ((org-at-heading-p) (call-interactively 'org-demote-subtree))
  18172. ((if (not (org-region-active-p)) (org-at-item-p)
  18173. (save-excursion (goto-char (region-beginning))
  18174. (org-at-item-p)))
  18175. (call-interactively 'org-indent-item-tree))
  18176. (t (org-modifier-cursor-error))))
  18177. (defun org-shiftmetaup (&optional arg)
  18178. "Move subtree up or kill table row.
  18179. Calls `org-move-subtree-up' or `org-table-kill-row' or
  18180. `org-move-item-up' or `org-timestamp-up', depending on context.
  18181. See the individual commands for more information."
  18182. (interactive "P")
  18183. (cond
  18184. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  18185. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  18186. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  18187. ((org-at-item-p) (call-interactively 'org-move-item-up))
  18188. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  18189. (call-interactively 'org-timestamp-up)))
  18190. (t (call-interactively 'org-drag-line-backward))))
  18191. (defun org-shiftmetadown (&optional arg)
  18192. "Move subtree down or insert table row.
  18193. Calls `org-move-subtree-down' or `org-table-insert-row' or
  18194. `org-move-item-down' or `org-timestamp-up', depending on context.
  18195. See the individual commands for more information."
  18196. (interactive "P")
  18197. (cond
  18198. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  18199. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  18200. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  18201. ((org-at-item-p) (call-interactively 'org-move-item-down))
  18202. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  18203. (call-interactively 'org-timestamp-down)))
  18204. (t (call-interactively 'org-drag-line-forward))))
  18205. (defsubst org-hidden-tree-error ()
  18206. (user-error
  18207. "Hidden subtree, open with TAB or use subtree command M-S-<left>/<right>"))
  18208. (defun org-metaleft (&optional arg)
  18209. "Promote heading or move table column to left.
  18210. Calls `org-do-promote' or `org-table-move-column', depending on context.
  18211. With no specific context, calls the Emacs default `backward-word'.
  18212. See the individual commands for more information."
  18213. (interactive "P")
  18214. (cond
  18215. ((run-hook-with-args-until-success 'org-metaleft-hook))
  18216. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  18217. ((org-with-limited-levels
  18218. (or (org-at-heading-p)
  18219. (and (org-region-active-p)
  18220. (save-excursion
  18221. (goto-char (region-beginning))
  18222. (org-at-heading-p)))))
  18223. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  18224. (call-interactively 'org-do-promote))
  18225. ;; At an inline task.
  18226. ((org-at-heading-p)
  18227. (call-interactively 'org-inlinetask-promote))
  18228. ((or (org-at-item-p)
  18229. (and (org-region-active-p)
  18230. (save-excursion
  18231. (goto-char (region-beginning))
  18232. (org-at-item-p))))
  18233. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  18234. (call-interactively 'org-outdent-item))
  18235. (t (call-interactively 'backward-word))))
  18236. (defun org-metaright (&optional arg)
  18237. "Demote a subtree, a list item or move table column to right.
  18238. In front of a drawer or a block keyword, indent it correctly.
  18239. With no specific context, calls the Emacs default `forward-word'.
  18240. See the individual commands for more information."
  18241. (interactive "P")
  18242. (cond
  18243. ((run-hook-with-args-until-success 'org-metaright-hook))
  18244. ((org-at-table-p) (call-interactively 'org-table-move-column))
  18245. ((org-at-drawer-p) (call-interactively 'org-indent-drawer))
  18246. ((org-at-block-p) (call-interactively 'org-indent-block))
  18247. ((org-with-limited-levels
  18248. (or (org-at-heading-p)
  18249. (and (org-region-active-p)
  18250. (save-excursion
  18251. (goto-char (region-beginning))
  18252. (org-at-heading-p)))))
  18253. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  18254. (call-interactively 'org-do-demote))
  18255. ;; At an inline task.
  18256. ((org-at-heading-p)
  18257. (call-interactively 'org-inlinetask-demote))
  18258. ((or (org-at-item-p)
  18259. (and (org-region-active-p)
  18260. (save-excursion
  18261. (goto-char (region-beginning))
  18262. (org-at-item-p))))
  18263. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  18264. (call-interactively 'org-indent-item))
  18265. (t (call-interactively 'forward-word))))
  18266. (defun org-check-for-hidden (what)
  18267. "Check if there are hidden headlines/items in the current visual line.
  18268. WHAT can be either `headlines' or `items'. If the current line is
  18269. an outline or item heading and it has a folded subtree below it,
  18270. this function returns t, nil otherwise."
  18271. (let ((re (cond
  18272. ((eq what 'headlines) org-outline-regexp-bol)
  18273. ((eq what 'items) (org-item-beginning-re))
  18274. (t (error "This should not happen"))))
  18275. beg end)
  18276. (save-excursion
  18277. (catch 'exit
  18278. (unless (org-region-active-p)
  18279. (setq beg (point-at-bol))
  18280. (beginning-of-line 2)
  18281. (while (and (not (eobp)) ;; this is like `next-line'
  18282. (get-char-property (1- (point)) 'invisible))
  18283. (beginning-of-line 2))
  18284. (setq end (point))
  18285. (goto-char beg)
  18286. (goto-char (point-at-eol))
  18287. (setq end (max end (point)))
  18288. (while (re-search-forward re end t)
  18289. (if (get-char-property (match-beginning 0) 'invisible)
  18290. (throw 'exit t))))
  18291. nil))))
  18292. (defun org-metaup (&optional arg)
  18293. "Move subtree up or move table row up.
  18294. Calls `org-move-subtree-up' or `org-table-move-row' or
  18295. `org-move-item-up', depending on context. See the individual commands
  18296. for more information."
  18297. (interactive "P")
  18298. (cond
  18299. ((run-hook-with-args-until-success 'org-metaup-hook))
  18300. ((org-region-active-p)
  18301. (let* ((a (min (region-beginning) (region-end)))
  18302. (b (1- (max (region-beginning) (region-end))))
  18303. (c (save-excursion (goto-char a)
  18304. (move-beginning-of-line 0)))
  18305. (d (save-excursion (goto-char a)
  18306. (move-end-of-line 0) (point))))
  18307. (transpose-regions a b c d)
  18308. (goto-char c)))
  18309. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  18310. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  18311. ((org-at-item-p) (call-interactively 'org-move-item-up))
  18312. (t (org-drag-element-backward))))
  18313. (defun org-metadown (&optional arg)
  18314. "Move subtree down or move table row down.
  18315. Calls `org-move-subtree-down' or `org-table-move-row' or
  18316. `org-move-item-down', depending on context. See the individual
  18317. commands for more information."
  18318. (interactive "P")
  18319. (cond
  18320. ((run-hook-with-args-until-success 'org-metadown-hook))
  18321. ((org-region-active-p)
  18322. (let* ((a (min (region-beginning) (region-end)))
  18323. (b (max (region-beginning) (region-end)))
  18324. (c (save-excursion (goto-char b)
  18325. (move-beginning-of-line 1)))
  18326. (d (save-excursion (goto-char b)
  18327. (move-end-of-line 1) (1+ (point)))))
  18328. (transpose-regions a b c d)
  18329. (goto-char d)))
  18330. ((org-at-table-p) (call-interactively 'org-table-move-row))
  18331. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  18332. ((org-at-item-p) (call-interactively 'org-move-item-down))
  18333. (t (org-drag-element-forward))))
  18334. (defun org-shiftup (&optional arg)
  18335. "Increase item in timestamp or increase priority of current headline.
  18336. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  18337. depending on context. See the individual commands for more information."
  18338. (interactive "P")
  18339. (cond
  18340. ((run-hook-with-args-until-success 'org-shiftup-hook))
  18341. ((and org-support-shift-select (org-region-active-p))
  18342. (org-call-for-shift-select 'previous-line))
  18343. ((org-at-timestamp-p t)
  18344. (call-interactively (if org-edit-timestamp-down-means-later
  18345. 'org-timestamp-down 'org-timestamp-up)))
  18346. ((and (not (eq org-support-shift-select 'always))
  18347. org-enable-priority-commands
  18348. (org-at-heading-p))
  18349. (call-interactively 'org-priority-up))
  18350. ((and (not org-support-shift-select) (org-at-item-p))
  18351. (call-interactively 'org-previous-item))
  18352. ((org-clocktable-try-shift 'up arg))
  18353. ((run-hook-with-args-until-success 'org-shiftup-final-hook))
  18354. (org-support-shift-select
  18355. (org-call-for-shift-select 'previous-line))
  18356. (t (org-shiftselect-error))))
  18357. (defun org-shiftdown (&optional arg)
  18358. "Decrease item in timestamp or decrease priority of current headline.
  18359. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  18360. depending on context. See the individual commands for more information."
  18361. (interactive "P")
  18362. (cond
  18363. ((run-hook-with-args-until-success 'org-shiftdown-hook))
  18364. ((and org-support-shift-select (org-region-active-p))
  18365. (org-call-for-shift-select 'next-line))
  18366. ((org-at-timestamp-p t)
  18367. (call-interactively (if org-edit-timestamp-down-means-later
  18368. 'org-timestamp-up 'org-timestamp-down)))
  18369. ((and (not (eq org-support-shift-select 'always))
  18370. org-enable-priority-commands
  18371. (org-at-heading-p))
  18372. (call-interactively 'org-priority-down))
  18373. ((and (not org-support-shift-select) (org-at-item-p))
  18374. (call-interactively 'org-next-item))
  18375. ((org-clocktable-try-shift 'down arg))
  18376. ((run-hook-with-args-until-success 'org-shiftdown-final-hook))
  18377. (org-support-shift-select
  18378. (org-call-for-shift-select 'next-line))
  18379. (t (org-shiftselect-error))))
  18380. (defun org-shiftright (&optional arg)
  18381. "Cycle the thing at point or in the current line, depending on context.
  18382. Depending on context, this does one of the following:
  18383. - switch a timestamp at point one day into the future
  18384. - on a headline, switch to the next TODO keyword.
  18385. - on an item, switch entire list to the next bullet type
  18386. - on a property line, switch to the next allowed value
  18387. - on a clocktable definition line, move time block into the future"
  18388. (interactive "P")
  18389. (cond
  18390. ((run-hook-with-args-until-success 'org-shiftright-hook))
  18391. ((and org-support-shift-select (org-region-active-p))
  18392. (org-call-for-shift-select 'forward-char))
  18393. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  18394. ((and (not (eq org-support-shift-select 'always))
  18395. (org-at-heading-p))
  18396. (let ((org-inhibit-logging
  18397. (not org-treat-S-cursor-todo-selection-as-state-change))
  18398. (org-inhibit-blocking
  18399. (not org-treat-S-cursor-todo-selection-as-state-change)))
  18400. (org-call-with-arg 'org-todo 'right)))
  18401. ((or (and org-support-shift-select
  18402. (not (eq org-support-shift-select 'always))
  18403. (org-at-item-bullet-p))
  18404. (and (not org-support-shift-select) (org-at-item-p)))
  18405. (org-call-with-arg 'org-cycle-list-bullet nil))
  18406. ((and (not (eq org-support-shift-select 'always))
  18407. (org-at-property-p))
  18408. (call-interactively 'org-property-next-allowed-value))
  18409. ((org-clocktable-try-shift 'right arg))
  18410. ((run-hook-with-args-until-success 'org-shiftright-final-hook))
  18411. (org-support-shift-select
  18412. (org-call-for-shift-select 'forward-char))
  18413. (t (org-shiftselect-error))))
  18414. (defun org-shiftleft (&optional arg)
  18415. "Cycle the thing at point or in the current line, depending on context.
  18416. Depending on context, this does one of the following:
  18417. - switch a timestamp at point one day into the past
  18418. - on a headline, switch to the previous TODO keyword.
  18419. - on an item, switch entire list to the previous bullet type
  18420. - on a property line, switch to the previous allowed value
  18421. - on a clocktable definition line, move time block into the past"
  18422. (interactive "P")
  18423. (cond
  18424. ((run-hook-with-args-until-success 'org-shiftleft-hook))
  18425. ((and org-support-shift-select (org-region-active-p))
  18426. (org-call-for-shift-select 'backward-char))
  18427. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  18428. ((and (not (eq org-support-shift-select 'always))
  18429. (org-at-heading-p))
  18430. (let ((org-inhibit-logging
  18431. (not org-treat-S-cursor-todo-selection-as-state-change))
  18432. (org-inhibit-blocking
  18433. (not org-treat-S-cursor-todo-selection-as-state-change)))
  18434. (org-call-with-arg 'org-todo 'left)))
  18435. ((or (and org-support-shift-select
  18436. (not (eq org-support-shift-select 'always))
  18437. (org-at-item-bullet-p))
  18438. (and (not org-support-shift-select) (org-at-item-p)))
  18439. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  18440. ((and (not (eq org-support-shift-select 'always))
  18441. (org-at-property-p))
  18442. (call-interactively 'org-property-previous-allowed-value))
  18443. ((org-clocktable-try-shift 'left arg))
  18444. ((run-hook-with-args-until-success 'org-shiftleft-final-hook))
  18445. (org-support-shift-select
  18446. (org-call-for-shift-select 'backward-char))
  18447. (t (org-shiftselect-error))))
  18448. (defun org-shiftcontrolright ()
  18449. "Switch to next TODO set."
  18450. (interactive)
  18451. (cond
  18452. ((and org-support-shift-select (org-region-active-p))
  18453. (org-call-for-shift-select 'forward-word))
  18454. ((and (not (eq org-support-shift-select 'always))
  18455. (org-at-heading-p))
  18456. (org-call-with-arg 'org-todo 'nextset))
  18457. (org-support-shift-select
  18458. (org-call-for-shift-select 'forward-word))
  18459. (t (org-shiftselect-error))))
  18460. (defun org-shiftcontrolleft ()
  18461. "Switch to previous TODO set."
  18462. (interactive)
  18463. (cond
  18464. ((and org-support-shift-select (org-region-active-p))
  18465. (org-call-for-shift-select 'backward-word))
  18466. ((and (not (eq org-support-shift-select 'always))
  18467. (org-at-heading-p))
  18468. (org-call-with-arg 'org-todo 'previousset))
  18469. (org-support-shift-select
  18470. (org-call-for-shift-select 'backward-word))
  18471. (t (org-shiftselect-error))))
  18472. (defun org-shiftcontrolup (&optional n)
  18473. "Change timestamps synchronously up in CLOCK log lines.
  18474. Optional argument N tells to change by that many units."
  18475. (interactive "P")
  18476. (if (and (org-at-clock-log-p) (org-at-timestamp-p t))
  18477. (let (org-support-shift-select)
  18478. (org-clock-timestamps-up n))
  18479. (user-error "Not at a clock log")))
  18480. (defun org-shiftcontroldown (&optional n)
  18481. "Change timestamps synchronously down in CLOCK log lines.
  18482. Optional argument N tells to change by that many units."
  18483. (interactive "P")
  18484. (if (and (org-at-clock-log-p) (org-at-timestamp-p t))
  18485. (let (org-support-shift-select)
  18486. (org-clock-timestamps-down n))
  18487. (user-error "Not at a clock log")))
  18488. (defun org-ctrl-c-ret ()
  18489. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  18490. (interactive)
  18491. (cond
  18492. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  18493. (t (call-interactively 'org-insert-heading))))
  18494. (defun org-find-visible ()
  18495. (let ((s (point)))
  18496. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  18497. (get-char-property s 'invisible)))
  18498. s))
  18499. (defun org-find-invisible ()
  18500. (let ((s (point)))
  18501. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  18502. (not (get-char-property s 'invisible))))
  18503. s))
  18504. (defun org-copy-visible (beg end)
  18505. "Copy the visible parts of the region."
  18506. (interactive "r")
  18507. (let (snippets s)
  18508. (save-excursion
  18509. (save-restriction
  18510. (narrow-to-region beg end)
  18511. (setq s (goto-char (point-min)))
  18512. (while (not (= (point) (point-max)))
  18513. (goto-char (org-find-invisible))
  18514. (push (buffer-substring s (point)) snippets)
  18515. (setq s (goto-char (org-find-visible))))))
  18516. (kill-new (apply 'concat (nreverse snippets)))))
  18517. (defun org-copy-special ()
  18518. "Copy region in table or copy current subtree.
  18519. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  18520. See the individual commands for more information."
  18521. (interactive)
  18522. (call-interactively
  18523. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  18524. (defun org-cut-special ()
  18525. "Cut region in table or cut current subtree.
  18526. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  18527. See the individual commands for more information."
  18528. (interactive)
  18529. (call-interactively
  18530. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  18531. (defun org-paste-special (arg)
  18532. "Paste rectangular region into table, or past subtree relative to level.
  18533. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  18534. See the individual commands for more information."
  18535. (interactive "P")
  18536. (if (org-at-table-p)
  18537. (org-table-paste-rectangle)
  18538. (org-paste-subtree arg)))
  18539. (defsubst org-in-fixed-width-region-p ()
  18540. "Is point in a fixed-width region?"
  18541. (save-match-data
  18542. (eq 'fixed-width (org-element-type (org-element-at-point)))))
  18543. (defun org-edit-special (&optional arg)
  18544. "Call a special editor for the element at point.
  18545. When at a table, call the formula editor with `org-table-edit-formulas'.
  18546. When in a source code block, call `org-edit-src-code'.
  18547. When in a fixed-width region, call `org-edit-fixed-width-region'.
  18548. When at an #+INCLUDE keyword, visit the included file.
  18549. On a link, call `ffap' to visit the link at point.
  18550. Otherwise, return a user error."
  18551. (interactive "P")
  18552. (let ((element (org-element-at-point)))
  18553. (assert (not buffer-read-only) nil
  18554. "Buffer is read-only: %s" (buffer-name))
  18555. (case (org-element-type element)
  18556. (src-block
  18557. (if (not arg) (org-edit-src-code)
  18558. (let* ((info (org-babel-get-src-block-info))
  18559. (lang (nth 0 info))
  18560. (params (nth 2 info))
  18561. (session (cdr (assq :session params))))
  18562. (if (not session) (org-edit-src-code)
  18563. ;; At a src-block with a session and function called with
  18564. ;; an ARG: switch to the buffer related to the inferior
  18565. ;; process.
  18566. (switch-to-buffer
  18567. (funcall (intern (concat "org-babel-prep-session:" lang))
  18568. session params))))))
  18569. (keyword
  18570. (if (member (org-element-property :key element) '("INCLUDE" "SETUPFILE"))
  18571. (find-file
  18572. (org-remove-double-quotes
  18573. (car (org-split-string (org-element-property :value element)))))
  18574. (user-error "No special environment to edit here")))
  18575. (table
  18576. (if (eq (org-element-property :type element) 'table.el)
  18577. (org-edit-src-code)
  18578. (call-interactively 'org-table-edit-formulas)))
  18579. ;; Only Org tables contain `table-row' type elements.
  18580. (table-row (call-interactively 'org-table-edit-formulas))
  18581. ((example-block export-block) (org-edit-src-code))
  18582. (fixed-width (org-edit-fixed-width-region))
  18583. (otherwise
  18584. ;; No notable element at point. Though, we may be at a link,
  18585. ;; which is an object. Thus, scan deeper.
  18586. (if (eq (org-element-type (org-element-context element)) 'link)
  18587. (call-interactively 'ffap)
  18588. (user-error "No special environment to edit here"))))))
  18589. (defvar org-table-coordinate-overlays) ; defined in org-table.el
  18590. (defun org-ctrl-c-ctrl-c (&optional arg)
  18591. "Set tags in headline, or update according to changed information at point.
  18592. This command does many different things, depending on context:
  18593. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  18594. this is what we do.
  18595. - If the cursor is on a statistics cookie, update it.
  18596. - If the cursor is in a headline, prompt for tags and insert them
  18597. into the current line, aligned to `org-tags-column'. When called
  18598. with prefix arg, realign all tags in the current buffer.
  18599. - If the cursor is in one of the special #+KEYWORD lines, this
  18600. triggers scanning the buffer for these lines and updating the
  18601. information.
  18602. - If the cursor is inside a table, realign the table. This command
  18603. works even if the automatic table editor has been turned off.
  18604. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  18605. the entire table.
  18606. - If the cursor is at a footnote reference or definition, jump to
  18607. the corresponding definition or references, respectively.
  18608. - If the cursor is a the beginning of a dynamic block, update it.
  18609. - If the current buffer is a capture buffer, close note and file it.
  18610. - If the cursor is on a <<<target>>>, update radio targets and
  18611. corresponding links in this buffer.
  18612. - If the cursor is on a numbered item in a plain list, renumber the
  18613. ordered list.
  18614. - If the cursor is on a checkbox, toggle it.
  18615. - If the cursor is on a code block, evaluate it. The variable
  18616. `org-confirm-babel-evaluate' can be used to control prompting
  18617. before code block evaluation, by default every code block
  18618. evaluation requires confirmation. Code block evaluation can be
  18619. inhibited by setting `org-babel-no-eval-on-ctrl-c-ctrl-c'."
  18620. (interactive "P")
  18621. (cond
  18622. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  18623. org-occur-highlights
  18624. org-latex-fragment-image-overlays)
  18625. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  18626. (org-remove-occur-highlights)
  18627. (org-remove-latex-fragment-image-overlays)
  18628. (message "Temporary highlights/overlays removed from current buffer"))
  18629. ((and (local-variable-p 'org-finish-function (current-buffer))
  18630. (fboundp org-finish-function))
  18631. (funcall org-finish-function))
  18632. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  18633. (t
  18634. (let* ((context (org-element-context)) (type (org-element-type context)))
  18635. ;; Test if point is within a blank line.
  18636. (if (save-excursion (beginning-of-line) (looking-at "[ \t]*$"))
  18637. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  18638. (user-error "C-c C-c can do nothing useful at this location"))
  18639. (case type
  18640. ;; When at a link, act according to the parent instead.
  18641. (link (setq context (org-element-property :parent context))
  18642. (setq type (org-element-type context)))
  18643. ;; Unsupported object types: refer to the first supported
  18644. ;; element or object containing it.
  18645. ((bold code entity export-snippet inline-babel-call inline-src-block
  18646. italic latex-fragment line-break macro strike-through subscript
  18647. superscript underline verbatim)
  18648. (while (and (setq context (org-element-property :parent context))
  18649. (not (memq (setq type (org-element-type context))
  18650. '(radio-target paragraph verse-block
  18651. table-cell)))))))
  18652. ;; For convenience: at the first line of a paragraph on the
  18653. ;; same line as an item, apply function on that item instead.
  18654. (when (eq type 'paragraph)
  18655. (let ((parent (org-element-property :parent context)))
  18656. (when (and (eq (org-element-type parent) 'item)
  18657. (= (point-at-bol) (org-element-property :begin parent)))
  18658. (setq context parent type 'item))))
  18659. ;; Act according to type of element or object at point.
  18660. (case type
  18661. (clock (org-clock-update-time-maybe))
  18662. (dynamic-block
  18663. (save-excursion
  18664. (goto-char (org-element-property :post-affiliated context))
  18665. (org-update-dblock)))
  18666. (footnote-definition
  18667. (goto-char (org-element-property :post-affiliated context))
  18668. (call-interactively 'org-footnote-action))
  18669. (footnote-reference (call-interactively 'org-footnote-action))
  18670. ((headline inlinetask)
  18671. (save-excursion (goto-char (org-element-property :begin context))
  18672. (call-interactively 'org-set-tags)))
  18673. (item
  18674. ;; At an item: a double C-u set checkbox to "[-]"
  18675. ;; unconditionally, whereas a single one will toggle its
  18676. ;; presence. Without an universal argument, if the item
  18677. ;; has a checkbox, toggle it. Otherwise repair the list.
  18678. (let* ((box (org-element-property :checkbox context))
  18679. (struct (org-element-property :structure context))
  18680. (old-struct (copy-tree struct))
  18681. (parents (org-list-parents-alist struct))
  18682. (prevs (org-list-prevs-alist struct))
  18683. (orderedp (org-not-nil (org-entry-get nil "ORDERED"))))
  18684. (org-list-set-checkbox
  18685. (org-element-property :begin context) struct
  18686. (cond ((equal arg '(16)) "[-]")
  18687. ((and (not box) (equal arg '(4))) "[ ]")
  18688. ((or (not box) (equal arg '(4))) nil)
  18689. ((eq box 'on) "[ ]")
  18690. (t "[X]")))
  18691. ;; Mimic `org-list-write-struct' but with grabbing
  18692. ;; a return value from `org-list-struct-fix-box'.
  18693. (org-list-struct-fix-ind struct parents 2)
  18694. (org-list-struct-fix-item-end struct)
  18695. (org-list-struct-fix-bul struct prevs)
  18696. (org-list-struct-fix-ind struct parents)
  18697. (let ((block-item
  18698. (org-list-struct-fix-box struct parents prevs orderedp)))
  18699. (if (and box (equal struct old-struct))
  18700. (if (equal arg '(16))
  18701. (message "Checkboxes already reset")
  18702. (user-error "Cannot toggle this checkbox: %s"
  18703. (if (eq box 'on)
  18704. "all subitems checked"
  18705. "unchecked subitems")))
  18706. (org-list-struct-apply-struct struct old-struct)
  18707. (org-update-checkbox-count-maybe))
  18708. (when block-item
  18709. (message "Checkboxes were removed due to empty box at line %d"
  18710. (org-current-line block-item))))))
  18711. (keyword
  18712. (let ((org-inhibit-startup-visibility-stuff t)
  18713. (org-startup-align-all-tables nil))
  18714. (when (boundp 'org-table-coordinate-overlays)
  18715. (mapc 'delete-overlay org-table-coordinate-overlays)
  18716. (setq org-table-coordinate-overlays nil))
  18717. (org-save-outline-visibility 'use-markers (org-mode-restart)))
  18718. (message "Local setup has been refreshed"))
  18719. (plain-list
  18720. ;; At a plain list, with a double C-u argument, set
  18721. ;; checkboxes of each item to "[-]", whereas a single one
  18722. ;; will toggle their presence according to the state of the
  18723. ;; first item in the list. Without an argument, repair the
  18724. ;; list.
  18725. (let* ((begin (org-element-property :contents-begin context))
  18726. (beginm (move-marker (make-marker) begin))
  18727. (struct (org-element-property :structure context))
  18728. (old-struct (copy-tree struct))
  18729. (first-box (save-excursion
  18730. (goto-char begin)
  18731. (looking-at org-list-full-item-re)
  18732. (match-string-no-properties 3)))
  18733. (new-box (cond ((equal arg '(16)) "[-]")
  18734. ((equal arg '(4)) (unless first-box "[ ]"))
  18735. ((equal first-box "[X]") "[ ]")
  18736. (t "[X]"))))
  18737. (cond
  18738. (arg
  18739. (mapc (lambda (pos) (org-list-set-checkbox pos struct new-box))
  18740. (org-list-get-all-items
  18741. begin struct (org-list-prevs-alist struct))))
  18742. ((and first-box (eq (point) begin))
  18743. ;; For convenience, when point is at bol on the first
  18744. ;; item of the list and no argument is provided, simply
  18745. ;; toggle checkbox of that item, if any.
  18746. (org-list-set-checkbox begin struct new-box)))
  18747. (org-list-write-struct
  18748. struct (org-list-parents-alist struct) old-struct)
  18749. (org-update-checkbox-count-maybe)
  18750. (save-excursion (goto-char beginm) (org-list-send-list 'maybe))))
  18751. ((property-drawer node-property)
  18752. (call-interactively 'org-property-action))
  18753. ((radio-target target)
  18754. (call-interactively 'org-update-radio-target-regexp))
  18755. (statistics-cookie
  18756. (call-interactively 'org-update-statistics-cookies))
  18757. ((table table-cell table-row)
  18758. ;; At a table, recalculate every field and align it. Also
  18759. ;; send the table if necessary. If the table has
  18760. ;; a `table.el' type, just give up. At a table row or
  18761. ;; cell, maybe recalculate line but always align table.
  18762. (if (eq (org-element-property :type context) 'table.el)
  18763. (message "Use C-c ' to edit table.el tables")
  18764. (let ((org-enable-table-editor t))
  18765. (if (or (eq type 'table)
  18766. ;; Check if point is at a TBLFM line.
  18767. (and (eq type 'table-row)
  18768. (= (point) (org-element-property :end context))))
  18769. (save-excursion
  18770. (if (org-at-TBLFM-p)
  18771. (progn (require 'org-table)
  18772. (org-table-calc-current-TBLFM))
  18773. (goto-char (org-element-property :contents-begin context))
  18774. (org-call-with-arg 'org-table-recalculate (or arg t))
  18775. (orgtbl-send-table 'maybe)))
  18776. (org-table-maybe-eval-formula)
  18777. (cond (arg (call-interactively 'org-table-recalculate))
  18778. ((org-table-maybe-recalculate-line))
  18779. (t (org-table-align)))))))
  18780. (timestamp (org-timestamp-change 0 'day))
  18781. (otherwise
  18782. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  18783. (user-error
  18784. "C-c C-c can do nothing useful at this location")))))))))
  18785. (defun org-mode-restart ()
  18786. (interactive)
  18787. (let ((indent-status (org-bound-and-true-p org-indent-mode)))
  18788. (funcall major-mode)
  18789. (hack-local-variables)
  18790. (when (and indent-status (not (org-bound-and-true-p org-indent-mode)))
  18791. (org-indent-mode -1)))
  18792. (message "%s restarted" major-mode))
  18793. (defun org-kill-note-or-show-branches ()
  18794. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  18795. (interactive)
  18796. (if (not org-finish-function)
  18797. (progn
  18798. (hide-subtree)
  18799. (call-interactively 'show-branches))
  18800. (let ((org-note-abort t))
  18801. (funcall org-finish-function))))
  18802. (defun org-open-line (n)
  18803. "Insert a new row in tables, call `open-line' elsewhere.
  18804. If `org-special-ctrl-o' is nil, just call `open-line' everywhere."
  18805. (interactive "*p")
  18806. (cond
  18807. ((not org-special-ctrl-o)
  18808. (open-line n))
  18809. ((org-at-table-p)
  18810. (org-table-insert-row))
  18811. (t
  18812. (open-line n))))
  18813. (defun org-return (&optional indent)
  18814. "Goto next table row or insert a newline.
  18815. Calls `org-table-next-row' or `newline', depending on context.
  18816. See the individual commands for more information."
  18817. (interactive)
  18818. (let (org-ts-what)
  18819. (cond
  18820. ((or (bobp) (org-in-src-block-p))
  18821. (if indent (newline-and-indent) (newline)))
  18822. ((org-at-table-p)
  18823. (org-table-justify-field-maybe)
  18824. (call-interactively 'org-table-next-row))
  18825. ;; when `newline-and-indent' is called within a list, make sure
  18826. ;; text moved stays inside the item.
  18827. ((and (org-in-item-p) indent)
  18828. (if (and (org-at-item-p) (>= (point) (match-end 0)))
  18829. (progn
  18830. (save-match-data (newline))
  18831. (org-indent-line-to (length (match-string 0))))
  18832. (let ((ind (org-get-indentation)))
  18833. (newline)
  18834. (if (org-looking-back org-list-end-re)
  18835. (org-indent-line)
  18836. (org-indent-line-to ind)))))
  18837. ((and org-return-follows-link
  18838. (org-at-timestamp-p t)
  18839. (not (eq org-ts-what 'after)))
  18840. (org-follow-timestamp-link))
  18841. ((and org-return-follows-link
  18842. (let ((tprop (get-text-property (point) 'face)))
  18843. (or (eq tprop 'org-link)
  18844. (and (listp tprop) (memq 'org-link tprop)))))
  18845. (call-interactively 'org-open-at-point))
  18846. ((and (org-at-heading-p)
  18847. (looking-at
  18848. (org-re "\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$")))
  18849. (org-show-entry)
  18850. (end-of-line 1)
  18851. (newline))
  18852. (t (if indent (newline-and-indent) (newline))))))
  18853. (defun org-return-indent ()
  18854. "Goto next table row or insert a newline and indent.
  18855. Calls `org-table-next-row' or `newline-and-indent', depending on
  18856. context. See the individual commands for more information."
  18857. (interactive)
  18858. (org-return t))
  18859. (defun org-ctrl-c-star ()
  18860. "Compute table, or change heading status of lines.
  18861. Calls `org-table-recalculate' or `org-toggle-heading',
  18862. depending on context."
  18863. (interactive)
  18864. (cond
  18865. ((org-at-table-p)
  18866. (call-interactively 'org-table-recalculate))
  18867. (t
  18868. ;; Convert all lines in region to list items
  18869. (call-interactively 'org-toggle-heading))))
  18870. (defun org-ctrl-c-minus ()
  18871. "Insert separator line in table or modify bullet status of line.
  18872. Also turns a plain line or a region of lines into list items.
  18873. Calls `org-table-insert-hline', `org-toggle-item', or
  18874. `org-cycle-list-bullet', depending on context."
  18875. (interactive)
  18876. (cond
  18877. ((org-at-table-p)
  18878. (call-interactively 'org-table-insert-hline))
  18879. ((org-region-active-p)
  18880. (call-interactively 'org-toggle-item))
  18881. ((org-in-item-p)
  18882. (call-interactively 'org-cycle-list-bullet))
  18883. (t
  18884. (call-interactively 'org-toggle-item))))
  18885. (defun org-toggle-item (arg)
  18886. "Convert headings or normal lines to items, items to normal lines.
  18887. If there is no active region, only the current line is considered.
  18888. If the first non blank line in the region is a headline, convert
  18889. all headlines to items, shifting text accordingly.
  18890. If it is an item, convert all items to normal lines.
  18891. If it is normal text, change region into a list of items.
  18892. With a prefix argument ARG, change the region in a single item."
  18893. (interactive "P")
  18894. (let ((shift-text
  18895. (function
  18896. ;; Shift text in current section to IND, from point to END.
  18897. ;; The function leaves point to END line.
  18898. (lambda (ind end)
  18899. (let ((min-i 1000) (end (copy-marker end)))
  18900. ;; First determine the minimum indentation (MIN-I) of
  18901. ;; the text.
  18902. (save-excursion
  18903. (catch 'exit
  18904. (while (< (point) end)
  18905. (let ((i (org-get-indentation)))
  18906. (cond
  18907. ;; Skip blank lines and inline tasks.
  18908. ((looking-at "^[ \t]*$"))
  18909. ((looking-at org-outline-regexp-bol))
  18910. ;; We can't find less than 0 indentation.
  18911. ((zerop i) (throw 'exit (setq min-i 0)))
  18912. ((< i min-i) (setq min-i i))))
  18913. (forward-line))))
  18914. ;; Then indent each line so that a line indented to
  18915. ;; MIN-I becomes indented to IND. Ignore blank lines
  18916. ;; and inline tasks in the process.
  18917. (let ((delta (- ind min-i)))
  18918. (while (< (point) end)
  18919. (unless (or (looking-at "^[ \t]*$")
  18920. (looking-at org-outline-regexp-bol))
  18921. (org-indent-line-to (+ (org-get-indentation) delta)))
  18922. (forward-line)))))))
  18923. (skip-blanks
  18924. (function
  18925. ;; Return beginning of first non-blank line, starting from
  18926. ;; line at POS.
  18927. (lambda (pos)
  18928. (save-excursion
  18929. (goto-char pos)
  18930. (skip-chars-forward " \r\t\n")
  18931. (point-at-bol)))))
  18932. beg end)
  18933. ;; Determine boundaries of changes.
  18934. (if (org-region-active-p)
  18935. (setq beg (funcall skip-blanks (region-beginning))
  18936. end (copy-marker (region-end)))
  18937. (setq beg (funcall skip-blanks (point-at-bol))
  18938. end (copy-marker (point-at-eol))))
  18939. ;; Depending on the starting line, choose an action on the text
  18940. ;; between BEG and END.
  18941. (org-with-limited-levels
  18942. (save-excursion
  18943. (goto-char beg)
  18944. (cond
  18945. ;; Case 1. Start at an item: de-itemize. Note that it only
  18946. ;; happens when a region is active: `org-ctrl-c-minus'
  18947. ;; would call `org-cycle-list-bullet' otherwise.
  18948. ((org-at-item-p)
  18949. (while (< (point) end)
  18950. (when (org-at-item-p)
  18951. (skip-chars-forward " \t")
  18952. (delete-region (point) (match-end 0)))
  18953. (forward-line)))
  18954. ;; Case 2. Start at an heading: convert to items.
  18955. ((org-at-heading-p)
  18956. (let* ((bul (org-list-bullet-string "-"))
  18957. (bul-len (length bul))
  18958. ;; Indentation of the first heading. It should be
  18959. ;; relative to the indentation of its parent, if any.
  18960. (start-ind (save-excursion
  18961. (cond
  18962. ((not org-adapt-indentation) 0)
  18963. ((not (outline-previous-heading)) 0)
  18964. (t (length (match-string 0))))))
  18965. ;; Level of first heading. Further headings will be
  18966. ;; compared to it to determine hierarchy in the list.
  18967. (ref-level (org-reduced-level (org-outline-level))))
  18968. (while (< (point) end)
  18969. (let* ((level (org-reduced-level (org-outline-level)))
  18970. (delta (max 0 (- level ref-level))))
  18971. ;; If current headline is less indented than the first
  18972. ;; one, set it as reference, in order to preserve
  18973. ;; subtrees.
  18974. (when (< level ref-level) (setq ref-level level))
  18975. (replace-match bul t t)
  18976. (org-indent-line-to (+ start-ind (* delta bul-len)))
  18977. ;; Ensure all text down to END (or SECTION-END) belongs
  18978. ;; to the newly created item.
  18979. (let ((section-end (save-excursion
  18980. (or (outline-next-heading) (point)))))
  18981. (forward-line)
  18982. (funcall shift-text
  18983. (+ start-ind (* (1+ delta) bul-len))
  18984. (min end section-end)))))))
  18985. ;; Case 3. Normal line with ARG: make the first line of region
  18986. ;; an item, and shift indentation of others lines to
  18987. ;; set them as item's body.
  18988. (arg (let* ((bul (org-list-bullet-string "-"))
  18989. (bul-len (length bul))
  18990. (ref-ind (org-get-indentation)))
  18991. (skip-chars-forward " \t")
  18992. (insert bul)
  18993. (forward-line)
  18994. (while (< (point) end)
  18995. ;; Ensure that lines less indented than first one
  18996. ;; still get included in item body.
  18997. (funcall shift-text
  18998. (+ ref-ind bul-len)
  18999. (min end (save-excursion (or (outline-next-heading)
  19000. (point)))))
  19001. (forward-line))))
  19002. ;; Case 4. Normal line without ARG: turn each non-item line
  19003. ;; into an item.
  19004. (t
  19005. (while (< (point) end)
  19006. (unless (or (org-at-heading-p) (org-at-item-p))
  19007. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  19008. (replace-match
  19009. (concat "\\1" (org-list-bullet-string "-") "\\2"))))
  19010. (forward-line))))))))
  19011. (defun org-toggle-heading (&optional nstars)
  19012. "Convert headings to normal text, or items or text to headings.
  19013. If there is no active region, only convert the current line.
  19014. With a \\[universal-argument] prefix, convert the whole list at
  19015. point into heading.
  19016. In a region:
  19017. - If the first non blank line is a headline, remove the stars
  19018. from all headlines in the region.
  19019. - If it is a normal line, turn each and every normal line (i.e.,
  19020. not an heading or an item) in the region into headings. If you
  19021. want to convert only the first line of this region, use one
  19022. universal prefix argument.
  19023. - If it is a plain list item, turn all plain list items into headings.
  19024. When converting a line into a heading, the number of stars is chosen
  19025. such that the lines become children of the current entry. However,
  19026. when a numeric prefix argument is given, its value determines the
  19027. number of stars to add."
  19028. (interactive "P")
  19029. (let ((skip-blanks
  19030. (function
  19031. ;; Return beginning of first non-blank line, starting from
  19032. ;; line at POS.
  19033. (lambda (pos)
  19034. (save-excursion
  19035. (goto-char pos)
  19036. (while (org-at-comment-p) (forward-line))
  19037. (skip-chars-forward " \r\t\n")
  19038. (point-at-bol)))))
  19039. beg end toggled)
  19040. ;; Determine boundaries of changes. If a universal prefix has
  19041. ;; been given, put the list in a region. If region ends at a bol,
  19042. ;; do not consider the last line to be in the region.
  19043. (when (and current-prefix-arg (org-at-item-p))
  19044. (if (listp current-prefix-arg) (setq current-prefix-arg 1))
  19045. (org-mark-element))
  19046. (if (org-region-active-p)
  19047. (setq beg (funcall skip-blanks (region-beginning))
  19048. end (copy-marker (save-excursion
  19049. (goto-char (region-end))
  19050. (if (bolp) (point) (point-at-eol)))))
  19051. (setq beg (funcall skip-blanks (point-at-bol))
  19052. end (copy-marker (point-at-eol))))
  19053. ;; Ensure inline tasks don't count as headings.
  19054. (org-with-limited-levels
  19055. (save-excursion
  19056. (goto-char beg)
  19057. (cond
  19058. ;; Case 1. Started at an heading: de-star headings.
  19059. ((org-at-heading-p)
  19060. (while (< (point) end)
  19061. (when (org-at-heading-p t)
  19062. (looking-at org-outline-regexp) (replace-match "")
  19063. (setq toggled t))
  19064. (forward-line)))
  19065. ;; Case 2. Started at an item: change items into headlines.
  19066. ;; One star will be added by `org-list-to-subtree'.
  19067. ((org-at-item-p)
  19068. (let* ((stars (make-string
  19069. ;; subtract the star that will be added again by
  19070. ;; `org-list-to-subtree'
  19071. (if (numberp nstars) (1- nstars)
  19072. (or (org-current-level) 0))
  19073. ?*))
  19074. (add-stars
  19075. (cond (nstars "") ; stars from prefix only
  19076. ((equal stars "") "") ; before first heading
  19077. (org-odd-levels-only "*") ; inside heading, odd
  19078. (t "")))) ; inside heading, oddeven
  19079. (while (< (point) end)
  19080. (when (org-at-item-p)
  19081. ;; Pay attention to cases when region ends before list.
  19082. (let* ((struct (org-list-struct))
  19083. (list-end (min (org-list-get-bottom-point struct) (1+ end))))
  19084. (save-restriction
  19085. (narrow-to-region (point) list-end)
  19086. (insert
  19087. (org-list-to-subtree
  19088. (org-list-parse-list t)
  19089. '(:istart (concat stars add-stars (funcall get-stars depth))
  19090. :icount (concat stars add-stars (funcall get-stars depth)))))))
  19091. (setq toggled t))
  19092. (forward-line))))
  19093. ;; Case 3. Started at normal text: make every line an heading,
  19094. ;; skipping headlines and items.
  19095. (t (let* ((stars
  19096. (make-string
  19097. (if (numberp nstars) nstars (or (org-current-level) 0)) ?*))
  19098. (add-stars
  19099. (cond (nstars "") ; stars from prefix only
  19100. ((equal stars "") "*") ; before first heading
  19101. (org-odd-levels-only "**") ; inside heading, odd
  19102. (t "*"))) ; inside heading, oddeven
  19103. (rpl (concat stars add-stars " "))
  19104. (lend (if (listp nstars) (save-excursion (end-of-line) (point)))))
  19105. (while (< (point) (if (equal nstars '(4)) lend end))
  19106. (when (and (not (or (org-at-heading-p) (org-at-item-p) (org-at-comment-p)))
  19107. (looking-at "\\([ \t]*\\)\\(\\S-\\)"))
  19108. (replace-match (concat rpl (match-string 2))) (setq toggled t))
  19109. (forward-line)))))))
  19110. (unless toggled (message "Cannot toggle heading from here"))))
  19111. (defun org-meta-return (&optional arg)
  19112. "Insert a new heading or wrap a region in a table.
  19113. Calls `org-insert-heading' or `org-table-wrap-region', depending
  19114. on context. See the individual commands for more information."
  19115. (interactive "P")
  19116. (org-check-before-invisible-edit 'insert)
  19117. (or (run-hook-with-args-until-success 'org-metareturn-hook)
  19118. (let* ((element (org-element-at-point))
  19119. (type (org-element-type element)))
  19120. (when (eq type 'table-row)
  19121. (setq element (org-element-property :parent element))
  19122. (setq type 'table))
  19123. (if (and (eq type 'table)
  19124. (eq (org-element-property :type element) 'org)
  19125. (>= (point) (org-element-property :contents-begin element))
  19126. (< (point) (org-element-property :contents-end element)))
  19127. (call-interactively 'org-table-wrap-region)
  19128. (call-interactively 'org-insert-heading)))))
  19129. ;;; Menu entries
  19130. (defsubst org-in-subtree-not-table-p ()
  19131. "Are we in a subtree and not in a table?"
  19132. (and (not (org-before-first-heading-p))
  19133. (not (org-at-table-p))))
  19134. ;; Define the Org-mode menus
  19135. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  19136. '("Tbl"
  19137. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  19138. ["Next Field" org-cycle (org-at-table-p)]
  19139. ["Previous Field" org-shifttab (org-at-table-p)]
  19140. ["Next Row" org-return (org-at-table-p)]
  19141. "--"
  19142. ["Blank Field" org-table-blank-field (org-at-table-p)]
  19143. ["Edit Field" org-table-edit-field (org-at-table-p)]
  19144. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  19145. "--"
  19146. ("Column"
  19147. ["Move Column Left" org-metaleft (org-at-table-p)]
  19148. ["Move Column Right" org-metaright (org-at-table-p)]
  19149. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  19150. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  19151. ("Row"
  19152. ["Move Row Up" org-metaup (org-at-table-p)]
  19153. ["Move Row Down" org-metadown (org-at-table-p)]
  19154. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  19155. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  19156. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  19157. "--"
  19158. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  19159. ("Rectangle"
  19160. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  19161. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  19162. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  19163. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  19164. "--"
  19165. ("Calculate"
  19166. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  19167. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  19168. ["Edit Formulas" org-edit-special (org-at-table-p)]
  19169. "--"
  19170. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  19171. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  19172. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  19173. "--"
  19174. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  19175. "--"
  19176. ["Sum Column/Rectangle" org-table-sum
  19177. (or (org-at-table-p) (org-region-active-p))]
  19178. ["Which Column?" org-table-current-column (org-at-table-p)])
  19179. ["Debug Formulas"
  19180. org-table-toggle-formula-debugger
  19181. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  19182. ["Show Col/Row Numbers"
  19183. org-table-toggle-coordinate-overlays
  19184. :style toggle
  19185. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  19186. "--"
  19187. ["Create" org-table-create (and (not (org-at-table-p))
  19188. org-enable-table-editor)]
  19189. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  19190. ["Import from File" org-table-import (not (org-at-table-p))]
  19191. ["Export to File" org-table-export (org-at-table-p)]
  19192. "--"
  19193. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  19194. (easy-menu-define org-org-menu org-mode-map "Org menu"
  19195. '("Org"
  19196. ("Show/Hide"
  19197. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  19198. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  19199. ["Sparse Tree..." org-sparse-tree t]
  19200. ["Reveal Context" org-reveal t]
  19201. ["Show All" show-all t]
  19202. "--"
  19203. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  19204. "--"
  19205. ["New Heading" org-insert-heading t]
  19206. ("Navigate Headings"
  19207. ["Up" outline-up-heading t]
  19208. ["Next" outline-next-visible-heading t]
  19209. ["Previous" outline-previous-visible-heading t]
  19210. ["Next Same Level" outline-forward-same-level t]
  19211. ["Previous Same Level" outline-backward-same-level t]
  19212. "--"
  19213. ["Jump" org-goto t])
  19214. ("Edit Structure"
  19215. ["Refile Subtree" org-refile (org-in-subtree-not-table-p)]
  19216. "--"
  19217. ["Move Subtree Up" org-shiftmetaup (org-in-subtree-not-table-p)]
  19218. ["Move Subtree Down" org-shiftmetadown (org-in-subtree-not-table-p)]
  19219. "--"
  19220. ["Copy Subtree" org-copy-special (org-in-subtree-not-table-p)]
  19221. ["Cut Subtree" org-cut-special (org-in-subtree-not-table-p)]
  19222. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  19223. "--"
  19224. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  19225. "--"
  19226. ["Copy visible text" org-copy-visible t]
  19227. "--"
  19228. ["Promote Heading" org-metaleft (org-in-subtree-not-table-p)]
  19229. ["Promote Subtree" org-shiftmetaleft (org-in-subtree-not-table-p)]
  19230. ["Demote Heading" org-metaright (org-in-subtree-not-table-p)]
  19231. ["Demote Subtree" org-shiftmetaright (org-in-subtree-not-table-p)]
  19232. "--"
  19233. ["Sort Region/Children" org-sort t]
  19234. "--"
  19235. ["Convert to odd levels" org-convert-to-odd-levels t]
  19236. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  19237. ("Editing"
  19238. ["Emphasis..." org-emphasize t]
  19239. ["Edit Source Example" org-edit-special t]
  19240. "--"
  19241. ["Footnote new/jump" org-footnote-action t]
  19242. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  19243. ("Archive"
  19244. ["Archive (default method)" org-archive-subtree-default (org-in-subtree-not-table-p)]
  19245. "--"
  19246. ["Move Subtree to Archive file" org-advertized-archive-subtree (org-in-subtree-not-table-p)]
  19247. ["Toggle ARCHIVE tag" org-toggle-archive-tag (org-in-subtree-not-table-p)]
  19248. ["Move subtree to Archive sibling" org-archive-to-archive-sibling (org-in-subtree-not-table-p)]
  19249. )
  19250. "--"
  19251. ("Hyperlinks"
  19252. ["Store Link (Global)" org-store-link t]
  19253. ["Find existing link to here" org-occur-link-in-agenda-files t]
  19254. ["Insert Link" org-insert-link t]
  19255. ["Follow Link" org-open-at-point t]
  19256. "--"
  19257. ["Next link" org-next-link t]
  19258. ["Previous link" org-previous-link t]
  19259. "--"
  19260. ["Descriptive Links"
  19261. org-toggle-link-display
  19262. :style radio
  19263. :selected org-descriptive-links
  19264. ]
  19265. ["Literal Links"
  19266. org-toggle-link-display
  19267. :style radio
  19268. :selected (not org-descriptive-links)])
  19269. "--"
  19270. ("TODO Lists"
  19271. ["TODO/DONE/-" org-todo t]
  19272. ("Select keyword"
  19273. ["Next keyword" org-shiftright (org-at-heading-p)]
  19274. ["Previous keyword" org-shiftleft (org-at-heading-p)]
  19275. ["Complete Keyword" pcomplete (assq :todo-keyword (org-context))]
  19276. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))]
  19277. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))])
  19278. ["Show TODO Tree" org-show-todo-tree :active t :keys "C-c / t"]
  19279. ["Global TODO list" org-todo-list :active t :keys "C-c a t"]
  19280. "--"
  19281. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  19282. :selected org-enforce-todo-dependencies :style toggle :active t]
  19283. "Settings for tree at point"
  19284. ["Do Children sequentially" org-toggle-ordered-property :style radio
  19285. :selected (org-entry-get nil "ORDERED")
  19286. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  19287. ["Do Children parallel" org-toggle-ordered-property :style radio
  19288. :selected (not (org-entry-get nil "ORDERED"))
  19289. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  19290. "--"
  19291. ["Set Priority" org-priority t]
  19292. ["Priority Up" org-shiftup t]
  19293. ["Priority Down" org-shiftdown t]
  19294. "--"
  19295. ["Get news from all feeds" org-feed-update-all t]
  19296. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  19297. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  19298. ("TAGS and Properties"
  19299. ["Set Tags" org-set-tags-command (not (org-before-first-heading-p))]
  19300. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  19301. "--"
  19302. ["Set property" org-set-property (not (org-before-first-heading-p))]
  19303. ["Column view of properties" org-columns t]
  19304. ["Insert Column View DBlock" org-insert-columns-dblock t])
  19305. ("Dates and Scheduling"
  19306. ["Timestamp" org-time-stamp (not (org-before-first-heading-p))]
  19307. ["Timestamp (inactive)" org-time-stamp-inactive (not (org-before-first-heading-p))]
  19308. ("Change Date"
  19309. ["1 Day Later" org-shiftright (org-at-timestamp-p)]
  19310. ["1 Day Earlier" org-shiftleft (org-at-timestamp-p)]
  19311. ["1 ... Later" org-shiftup (org-at-timestamp-p)]
  19312. ["1 ... Earlier" org-shiftdown (org-at-timestamp-p)])
  19313. ["Compute Time Range" org-evaluate-time-range t]
  19314. ["Schedule Item" org-schedule (not (org-before-first-heading-p))]
  19315. ["Deadline" org-deadline (not (org-before-first-heading-p))]
  19316. "--"
  19317. ["Custom time format" org-toggle-time-stamp-overlays
  19318. :style radio :selected org-display-custom-times]
  19319. "--"
  19320. ["Goto Calendar" org-goto-calendar t]
  19321. ["Date from Calendar" org-date-from-calendar t]
  19322. "--"
  19323. ["Start/Restart Timer" org-timer-start t]
  19324. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  19325. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  19326. ["Insert Timer String" org-timer t]
  19327. ["Insert Timer Item" org-timer-item t])
  19328. ("Logging work"
  19329. ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
  19330. ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
  19331. ["Clock out" org-clock-out t]
  19332. ["Clock cancel" org-clock-cancel t]
  19333. "--"
  19334. ["Mark as default task" org-clock-mark-default-task t]
  19335. ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
  19336. ["Goto running clock" org-clock-goto t]
  19337. "--"
  19338. ["Display times" org-clock-display t]
  19339. ["Create clock table" org-clock-report t]
  19340. "--"
  19341. ["Record DONE time"
  19342. (progn (setq org-log-done (not org-log-done))
  19343. (message "Switching to %s will %s record a timestamp"
  19344. (car org-done-keywords)
  19345. (if org-log-done "automatically" "not")))
  19346. :style toggle :selected org-log-done])
  19347. "--"
  19348. ["Agenda Command..." org-agenda t]
  19349. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  19350. ("File List for Agenda")
  19351. ("Special views current file"
  19352. ["TODO Tree" org-show-todo-tree t]
  19353. ["Check Deadlines" org-check-deadlines t]
  19354. ["Timeline" org-timeline t]
  19355. ["Tags/Property tree" org-match-sparse-tree t])
  19356. "--"
  19357. ["Export/Publish..." org-export-dispatch t]
  19358. ("LaTeX"
  19359. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  19360. :selected org-cdlatex-mode]
  19361. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  19362. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  19363. ["Modify math symbol" org-cdlatex-math-modify
  19364. (org-inside-LaTeX-fragment-p)]
  19365. ["Insert citation" org-reftex-citation t]
  19366. "--"
  19367. ["Template for BEAMER" (org-beamer-insert-options-template) t])
  19368. "--"
  19369. ("MobileOrg"
  19370. ["Push Files and Views" org-mobile-push t]
  19371. ["Get Captured and Flagged" org-mobile-pull t]
  19372. ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
  19373. "--"
  19374. ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
  19375. "--"
  19376. ("Documentation"
  19377. ["Show Version" org-version t]
  19378. ["Info Documentation" org-info t])
  19379. ("Customize"
  19380. ["Browse Org Group" org-customize t]
  19381. "--"
  19382. ["Expand This Menu" org-create-customize-menu
  19383. (fboundp 'customize-menu-create)])
  19384. ["Send bug report" org-submit-bug-report t]
  19385. "--"
  19386. ("Refresh/Reload"
  19387. ["Refresh setup current buffer" org-mode-restart t]
  19388. ["Reload Org (after update)" org-reload t]
  19389. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x !"])
  19390. ))
  19391. (defun org-info (&optional node)
  19392. "Read documentation for Org-mode in the info system.
  19393. With optional NODE, go directly to that node."
  19394. (interactive)
  19395. (info (format "(org)%s" (or node ""))))
  19396. ;;;###autoload
  19397. (defun org-submit-bug-report ()
  19398. "Submit a bug report on Org-mode via mail.
  19399. Don't hesitate to report any problems or inaccurate documentation.
  19400. If you don't have setup sending mail from (X)Emacs, please copy the
  19401. output buffer into your mail program, as it gives us important
  19402. information about your Org-mode version and configuration."
  19403. (interactive)
  19404. (require 'reporter)
  19405. (org-load-modules-maybe)
  19406. (org-require-autoloaded-modules)
  19407. (let ((reporter-prompt-for-summary-p "Bug report subject: "))
  19408. (reporter-submit-bug-report
  19409. "emacs-orgmode@gnu.org"
  19410. (org-version nil 'full)
  19411. (let (list)
  19412. (save-window-excursion
  19413. (org-pop-to-buffer-same-window (get-buffer-create "*Warn about privacy*"))
  19414. (delete-other-windows)
  19415. (erase-buffer)
  19416. (insert "You are about to submit a bug report to the Org-mode mailing list.
  19417. We would like to add your full Org-mode and Outline configuration to the
  19418. bug report. This greatly simplifies the work of the maintainer and
  19419. other experts on the mailing list.
  19420. HOWEVER, some variables you have customized may contain private
  19421. information. The names of customers, colleagues, or friends, might
  19422. appear in the form of file names, tags, todo states, or search strings.
  19423. If you answer yes to the prompt, you might want to check and remove
  19424. such private information before sending the email.")
  19425. (add-text-properties (point-min) (point-max) '(face org-warning))
  19426. (when (yes-or-no-p "Include your Org-mode configuration ")
  19427. (mapatoms
  19428. (lambda (v)
  19429. (and (boundp v)
  19430. (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
  19431. (or (and (symbol-value v)
  19432. (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
  19433. (and
  19434. (get v 'custom-type) (get v 'standard-value)
  19435. (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
  19436. (push v list)))))
  19437. (kill-buffer (get-buffer "*Warn about privacy*"))
  19438. list))
  19439. nil nil
  19440. "Remember to cover the basics, that is, what you expected to happen and
  19441. what in fact did happen. You don't know how to make a good report? See
  19442. http://orgmode.org/manual/Feedback.html#Feedback
  19443. Your bug report will be posted to the Org-mode mailing list.
  19444. ------------------------------------------------------------------------")
  19445. (save-excursion
  19446. (if (re-search-backward "^\\(Subject: \\)Org-mode version \\(.*?\\);[ \t]*\\(.*\\)" nil t)
  19447. (replace-match "\\1Bug: \\3 [\\2]")))))
  19448. (defun org-install-agenda-files-menu ()
  19449. (let ((bl (buffer-list)))
  19450. (save-excursion
  19451. (while bl
  19452. (set-buffer (pop bl))
  19453. (if (derived-mode-p 'org-mode) (setq bl nil)))
  19454. (when (derived-mode-p 'org-mode)
  19455. (easy-menu-change
  19456. '("Org") "File List for Agenda"
  19457. (append
  19458. (list
  19459. ["Edit File List" (org-edit-agenda-file-list) t]
  19460. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  19461. ["Remove Current File from List" org-remove-file t]
  19462. ["Cycle through agenda files" org-cycle-agenda-files t]
  19463. ["Occur in all agenda files" org-occur-in-agenda-files t]
  19464. "--")
  19465. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  19466. ;;;; Documentation
  19467. (defun org-require-autoloaded-modules ()
  19468. (interactive)
  19469. (mapc 'require
  19470. '(org-agenda org-archive org-attach org-clock org-colview org-id
  19471. org-table org-timer)))
  19472. ;;;###autoload
  19473. (defun org-reload (&optional uncompiled)
  19474. "Reload all org lisp files.
  19475. With prefix arg UNCOMPILED, load the uncompiled versions."
  19476. (interactive "P")
  19477. (require 'loadhist)
  19478. (let* ((org-dir (org-find-library-dir "org"))
  19479. (contrib-dir (or (org-find-library-dir "org-contribdir") org-dir))
  19480. (feature-re "^\\(org\\|ob\\|ox\\)\\(-.*\\)?")
  19481. (remove-re (mapconcat 'identity
  19482. (mapcar (lambda (f) (concat "^" f "$"))
  19483. (list (if (featurep 'xemacs)
  19484. "org-colview"
  19485. "org-colview-xemacs")
  19486. "org" "org-loaddefs" "org-version"))
  19487. "\\|"))
  19488. (feats (delete-dups
  19489. (mapcar 'file-name-sans-extension
  19490. (mapcar 'file-name-nondirectory
  19491. (delq nil
  19492. (mapcar 'feature-file
  19493. features))))))
  19494. (lfeat (append
  19495. (sort
  19496. (setq feats
  19497. (delq nil (mapcar
  19498. (lambda (f)
  19499. (if (and (string-match feature-re f)
  19500. (not (string-match remove-re f)))
  19501. f nil))
  19502. feats)))
  19503. 'string-lessp)
  19504. (list "org-version" "org")))
  19505. (load-suffixes (when (boundp 'load-suffixes) load-suffixes))
  19506. (load-suffixes (if uncompiled (reverse load-suffixes) load-suffixes))
  19507. load-uncore load-misses)
  19508. (setq load-misses
  19509. (delq 't
  19510. (mapcar (lambda (f)
  19511. (or (org-load-noerror-mustsuffix (concat org-dir f))
  19512. (and (string= org-dir contrib-dir)
  19513. (org-load-noerror-mustsuffix (concat contrib-dir f)))
  19514. (and (org-load-noerror-mustsuffix (concat (org-find-library-dir f) f))
  19515. (add-to-list 'load-uncore f 'append)
  19516. 't)
  19517. f))
  19518. lfeat)))
  19519. (if load-uncore
  19520. (message "The following feature%s found in load-path, please check if that's correct:\n%s"
  19521. (if (> (length load-uncore) 1) "s were" " was") load-uncore))
  19522. (if load-misses
  19523. (message "Some error occurred while reloading Org feature%s\n%s\nPlease check *Messages*!\n%s"
  19524. (if (> (length load-misses) 1) "s" "") load-misses (org-version nil 'full))
  19525. (message "Successfully reloaded Org\n%s" (org-version nil 'full)))))
  19526. ;;;###autoload
  19527. (defun org-customize ()
  19528. "Call the customize function with org as argument."
  19529. (interactive)
  19530. (org-load-modules-maybe)
  19531. (org-require-autoloaded-modules)
  19532. (customize-browse 'org))
  19533. (defun org-create-customize-menu ()
  19534. "Create a full customization menu for Org-mode, insert it into the menu."
  19535. (interactive)
  19536. (org-load-modules-maybe)
  19537. (org-require-autoloaded-modules)
  19538. (if (fboundp 'customize-menu-create)
  19539. (progn
  19540. (easy-menu-change
  19541. '("Org") "Customize"
  19542. `(["Browse Org group" org-customize t]
  19543. "--"
  19544. ,(customize-menu-create 'org)
  19545. ["Set" Custom-set t]
  19546. ["Save" Custom-save t]
  19547. ["Reset to Current" Custom-reset-current t]
  19548. ["Reset to Saved" Custom-reset-saved t]
  19549. ["Reset to Standard Settings" Custom-reset-standard t]))
  19550. (message "\"Org\"-menu now contains full customization menu"))
  19551. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  19552. ;;;; Miscellaneous stuff
  19553. ;;; Generally useful functions
  19554. (defun org-get-at-bol (property)
  19555. "Get text property PROPERTY at beginning of line."
  19556. (get-text-property (point-at-bol) property))
  19557. (defun org-find-text-property-in-string (prop s)
  19558. "Return the first non-nil value of property PROP in string S."
  19559. (or (get-text-property 0 prop s)
  19560. (get-text-property (or (next-single-property-change 0 prop s) 0)
  19561. prop s)))
  19562. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  19563. "Display the given MESSAGE as a warning."
  19564. (if (fboundp 'display-warning)
  19565. (display-warning 'org message
  19566. (if (featurep 'xemacs) 'warning :warning))
  19567. (let ((buf (get-buffer-create "*Org warnings*")))
  19568. (with-current-buffer buf
  19569. (goto-char (point-max))
  19570. (insert "Warning (Org): " message)
  19571. (unless (bolp)
  19572. (newline)))
  19573. (display-buffer buf)
  19574. (sit-for 0))))
  19575. (defun org-eval (form)
  19576. "Eval FORM and return result."
  19577. (condition-case error
  19578. (eval form)
  19579. (error (format "%%![Error: %s]" error))))
  19580. (defun org-in-clocktable-p ()
  19581. "Check if the cursor is in a clocktable."
  19582. (let ((pos (point)) start)
  19583. (save-excursion
  19584. (end-of-line 1)
  19585. (and (re-search-backward "^[ \t]*#\\+BEGIN:[ \t]+clocktable" nil t)
  19586. (setq start (match-beginning 0))
  19587. (re-search-forward "^[ \t]*#\\+END:.*" nil t)
  19588. (>= (match-end 0) pos)
  19589. start))))
  19590. (defun org-in-commented-line ()
  19591. "Is point in a line starting with `#'?"
  19592. (equal (char-after (point-at-bol)) ?#))
  19593. (defun org-in-indented-comment-line ()
  19594. "Is point in a line starting with `#' after some white space?"
  19595. (save-excursion
  19596. (save-match-data
  19597. (goto-char (point-at-bol))
  19598. (looking-at "[ \t]*#"))))
  19599. (defun org-in-verbatim-emphasis ()
  19600. (save-match-data
  19601. (and (org-in-regexp org-emph-re 2)
  19602. (>= (point) (match-beginning 3))
  19603. (<= (point) (match-end 4))
  19604. (member (match-string 3) '("=" "~")))))
  19605. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  19606. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  19607. (if (and marker (marker-buffer marker)
  19608. (buffer-live-p (marker-buffer marker)))
  19609. (progn
  19610. (org-pop-to-buffer-same-window (marker-buffer marker))
  19611. (if (or (> marker (point-max)) (< marker (point-min)))
  19612. (widen))
  19613. (goto-char marker)
  19614. (org-show-context 'org-goto))
  19615. (if bookmark
  19616. (bookmark-jump bookmark)
  19617. (error "Cannot find location"))))
  19618. (defun org-quote-csv-field (s)
  19619. "Quote field for inclusion in CSV material."
  19620. (if (string-match "[\",]" s)
  19621. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  19622. s))
  19623. (defun org-force-self-insert (N)
  19624. "Needed to enforce self-insert under remapping."
  19625. (interactive "p")
  19626. (self-insert-command N))
  19627. (defun org-string-width (s)
  19628. "Compute width of string, ignoring invisible characters.
  19629. This ignores character with invisibility property `org-link', and also
  19630. characters with property `org-cwidth', because these will become invisible
  19631. upon the next fontification round."
  19632. (let (b l)
  19633. (when (or (eq t buffer-invisibility-spec)
  19634. (assq 'org-link buffer-invisibility-spec))
  19635. (while (setq b (text-property-any 0 (length s)
  19636. 'invisible 'org-link s))
  19637. (setq s (concat (substring s 0 b)
  19638. (substring s (or (next-single-property-change
  19639. b 'invisible s) (length s)))))))
  19640. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  19641. (setq s (concat (substring s 0 b)
  19642. (substring s (or (next-single-property-change
  19643. b 'org-cwidth s) (length s))))))
  19644. (setq l (string-width s) b -1)
  19645. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  19646. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  19647. l))
  19648. (defun org-shorten-string (s maxlength)
  19649. "Shorten string S so tht it is no longer than MAXLENGTH characters.
  19650. If the string is shorter or has length MAXLENGTH, just return the
  19651. original string. If it is longer, the functions finds a space in the
  19652. string, breaks this string off at that locations and adds three dots
  19653. as ellipsis. Including the ellipsis, the string will not be longer
  19654. than MAXLENGTH. If finding a good breaking point in the string does
  19655. not work, the string is just chopped off in the middle of a word
  19656. if necessary."
  19657. (if (<= (length s) maxlength)
  19658. s
  19659. (let* ((n (max (- maxlength 4) 1))
  19660. (re (concat "\\`\\(.\\{1," (int-to-string n) "\\}[^ ]\\)\\([ ]\\|\\'\\)")))
  19661. (if (string-match re s)
  19662. (concat (match-string 1 s) "...")
  19663. (concat (substring s 0 (max (- maxlength 3) 0)) "...")))))
  19664. (defun org-get-indentation (&optional line)
  19665. "Get the indentation of the current line, interpreting tabs.
  19666. When LINE is given, assume it represents a line and compute its indentation."
  19667. (if line
  19668. (if (string-match "^ *" (org-remove-tabs line))
  19669. (match-end 0))
  19670. (save-excursion
  19671. (beginning-of-line 1)
  19672. (skip-chars-forward " \t")
  19673. (current-column))))
  19674. (defun org-get-string-indentation (s)
  19675. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  19676. (let ((n -1) (i 0) (w tab-width) c)
  19677. (catch 'exit
  19678. (while (< (setq n (1+ n)) (length s))
  19679. (setq c (aref s n))
  19680. (cond ((= c ?\ ) (setq i (1+ i)))
  19681. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  19682. (t (throw 'exit t)))))
  19683. i))
  19684. (defun org-remove-tabs (s &optional width)
  19685. "Replace tabulators in S with spaces.
  19686. Assumes that s is a single line, starting in column 0."
  19687. (setq width (or width tab-width))
  19688. (while (string-match "\t" s)
  19689. (setq s (replace-match
  19690. (make-string
  19691. (- (* width (/ (+ (match-beginning 0) width) width))
  19692. (match-beginning 0)) ?\ )
  19693. t t s)))
  19694. s)
  19695. (defun org-fix-indentation (line ind)
  19696. "Fix indentation in LINE.
  19697. IND is a cons cell with target and minimum indentation.
  19698. If the current indentation in LINE is smaller than the minimum,
  19699. leave it alone. If it is larger than ind, set it to the target."
  19700. (let* ((l (org-remove-tabs line))
  19701. (i (org-get-indentation l))
  19702. (i1 (car ind)) (i2 (cdr ind)))
  19703. (if (>= i i2) (setq l (substring line i2)))
  19704. (if (> i1 0)
  19705. (concat (make-string i1 ?\ ) l)
  19706. l)))
  19707. (defun org-remove-indentation (code &optional n)
  19708. "Remove the maximum common indentation from the lines in CODE.
  19709. N may optionally be the number of spaces to remove."
  19710. (with-temp-buffer
  19711. (insert code)
  19712. (org-do-remove-indentation n)
  19713. (buffer-string)))
  19714. (defun org-do-remove-indentation (&optional n)
  19715. "Remove the maximum common indentation from the buffer."
  19716. (untabify (point-min) (point-max))
  19717. (let ((min 10000) re)
  19718. (if n
  19719. (setq min n)
  19720. (goto-char (point-min))
  19721. (while (re-search-forward "^ *[^ \n]" nil t)
  19722. (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
  19723. (unless (or (= min 0) (= min 10000))
  19724. (setq re (format "^ \\{%d\\}" min))
  19725. (goto-char (point-min))
  19726. (while (re-search-forward re nil t)
  19727. (replace-match "")
  19728. (end-of-line 1))
  19729. min)))
  19730. (defun org-fill-template (template alist)
  19731. "Find each %key of ALIST in TEMPLATE and replace it."
  19732. (let ((case-fold-search nil)
  19733. entry key value)
  19734. (setq alist (sort (copy-sequence alist)
  19735. (lambda (a b) (< (length (car a)) (length (car b))))))
  19736. (while (setq entry (pop alist))
  19737. (setq template
  19738. (replace-regexp-in-string
  19739. (concat "%" (regexp-quote (car entry)))
  19740. (or (cdr entry) "") template t t)))
  19741. template))
  19742. (defun org-base-buffer (buffer)
  19743. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  19744. (if (not buffer)
  19745. buffer
  19746. (or (buffer-base-buffer buffer)
  19747. buffer)))
  19748. (defun org-trim (s)
  19749. "Remove whitespace at beginning and end of string."
  19750. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  19751. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  19752. s)
  19753. (defun org-wrap (string &optional width lines)
  19754. "Wrap string to either a number of lines, or a width in characters.
  19755. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  19756. that costs. If there is a word longer than WIDTH, the text is actually
  19757. wrapped to the length of that word.
  19758. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  19759. many lines, whatever width that takes.
  19760. The return value is a list of lines, without newlines at the end."
  19761. (let* ((words (org-split-string string "[ \t\n]+"))
  19762. (maxword (apply 'max (mapcar 'org-string-width words)))
  19763. w ll)
  19764. (cond (width
  19765. (org-do-wrap words (max maxword width)))
  19766. (lines
  19767. (setq w maxword)
  19768. (setq ll (org-do-wrap words maxword))
  19769. (if (<= (length ll) lines)
  19770. ll
  19771. (setq ll words)
  19772. (while (> (length ll) lines)
  19773. (setq w (1+ w))
  19774. (setq ll (org-do-wrap words w)))
  19775. ll))
  19776. (t (error "Cannot wrap this")))))
  19777. (defun org-do-wrap (words width)
  19778. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  19779. (let (lines line)
  19780. (while words
  19781. (setq line (pop words))
  19782. (while (and words (< (+ (length line) (length (car words))) width))
  19783. (setq line (concat line " " (pop words))))
  19784. (setq lines (push line lines)))
  19785. (nreverse lines)))
  19786. (defun org-split-string (string &optional separators)
  19787. "Splits STRING into substrings at SEPARATORS.
  19788. No empty strings are returned if there are matches at the beginning
  19789. and end of string."
  19790. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  19791. (start 0)
  19792. notfirst
  19793. (list nil))
  19794. (while (and (string-match rexp string
  19795. (if (and notfirst
  19796. (= start (match-beginning 0))
  19797. (< start (length string)))
  19798. (1+ start) start))
  19799. (< (match-beginning 0) (length string)))
  19800. (setq notfirst t)
  19801. (or (eq (match-beginning 0) 0)
  19802. (and (eq (match-beginning 0) (match-end 0))
  19803. (eq (match-beginning 0) start))
  19804. (setq list
  19805. (cons (substring string start (match-beginning 0))
  19806. list)))
  19807. (setq start (match-end 0)))
  19808. (or (eq start (length string))
  19809. (setq list
  19810. (cons (substring string start)
  19811. list)))
  19812. (nreverse list)))
  19813. (defun org-quote-vert (s)
  19814. "Replace \"|\" with \"\\vert\"."
  19815. (while (string-match "|" s)
  19816. (setq s (replace-match "\\vert" t t s)))
  19817. s)
  19818. (defun org-uuidgen-p (s)
  19819. "Is S an ID created by UUIDGEN?"
  19820. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  19821. (defun org-in-src-block-p (&optional inside)
  19822. "Whether point is in a code source block.
  19823. When INSIDE is non-nil, don't consider we are within a src block
  19824. when point is at #+BEGIN_SRC or #+END_SRC."
  19825. (let ((case-fold-search t) ov)
  19826. (or (and (setq ov (overlays-at (point)))
  19827. (memq 'org-block-background
  19828. (overlay-properties (car ov))))
  19829. (and (not inside)
  19830. (save-match-data
  19831. (save-excursion
  19832. (beginning-of-line)
  19833. (looking-at ".*#\\+\\(begin\\|end\\)_src")))))))
  19834. (defun org-context ()
  19835. "Return a list of contexts of the current cursor position.
  19836. If several contexts apply, all are returned.
  19837. Each context entry is a list with a symbol naming the context, and
  19838. two positions indicating start and end of the context. Possible
  19839. contexts are:
  19840. :headline anywhere in a headline
  19841. :headline-stars on the leading stars in a headline
  19842. :todo-keyword on a TODO keyword (including DONE) in a headline
  19843. :tags on the TAGS in a headline
  19844. :priority on the priority cookie in a headline
  19845. :item on the first line of a plain list item
  19846. :item-bullet on the bullet/number of a plain list item
  19847. :checkbox on the checkbox in a plain list item
  19848. :table in an org-mode table
  19849. :table-special on a special filed in a table
  19850. :table-table in a table.el table
  19851. :clocktable in a clocktable
  19852. :src-block in a source block
  19853. :link on a hyperlink
  19854. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE, COMMENT, QUOTE.
  19855. :target on a <<target>>
  19856. :radio-target on a <<<radio-target>>>
  19857. :latex-fragment on a LaTeX fragment
  19858. :latex-preview on a LaTeX fragment with overlaid preview image
  19859. This function expects the position to be visible because it uses font-lock
  19860. faces as a help to recognize the following contexts: :table-special, :link,
  19861. and :keyword."
  19862. (let* ((f (get-text-property (point) 'face))
  19863. (faces (if (listp f) f (list f)))
  19864. (case-fold-search t)
  19865. (p (point)) clist o)
  19866. ;; First the large context
  19867. (cond
  19868. ((org-at-heading-p t)
  19869. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  19870. (when (progn
  19871. (beginning-of-line 1)
  19872. (looking-at org-todo-line-tags-regexp))
  19873. (push (org-point-in-group p 1 :headline-stars) clist)
  19874. (push (org-point-in-group p 2 :todo-keyword) clist)
  19875. (push (org-point-in-group p 4 :tags) clist))
  19876. (goto-char p)
  19877. (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
  19878. (if (looking-at "\\[#[A-Z0-9]\\]")
  19879. (push (org-point-in-group p 0 :priority) clist)))
  19880. ((org-at-item-p)
  19881. (push (org-point-in-group p 2 :item-bullet) clist)
  19882. (push (list :item (point-at-bol)
  19883. (save-excursion (org-end-of-item) (point)))
  19884. clist)
  19885. (and (org-at-item-checkbox-p)
  19886. (push (org-point-in-group p 0 :checkbox) clist)))
  19887. ((org-at-table-p)
  19888. (push (list :table (org-table-begin) (org-table-end)) clist)
  19889. (if (memq 'org-formula faces)
  19890. (push (list :table-special
  19891. (previous-single-property-change p 'face)
  19892. (next-single-property-change p 'face)) clist)))
  19893. ((org-at-table-p 'any)
  19894. (push (list :table-table) clist)))
  19895. (goto-char p)
  19896. (let ((case-fold-search t))
  19897. ;; New the "medium" contexts: clocktables, source blocks
  19898. (cond ((org-in-clocktable-p)
  19899. (push (list :clocktable
  19900. (and (or (looking-at "#\\+BEGIN: clocktable")
  19901. (search-backward "#+BEGIN: clocktable" nil t))
  19902. (match-beginning 0))
  19903. (and (re-search-forward "#\\+END:?" nil t)
  19904. (match-end 0))) clist))
  19905. ((org-in-src-block-p)
  19906. (push (list :src-block
  19907. (and (or (looking-at "#\\+BEGIN_SRC")
  19908. (search-backward "#+BEGIN_SRC" nil t))
  19909. (match-beginning 0))
  19910. (and (search-forward "#+END_SRC" nil t)
  19911. (match-beginning 0))) clist))))
  19912. (goto-char p)
  19913. ;; Now the small context
  19914. (cond
  19915. ((org-at-timestamp-p)
  19916. (push (org-point-in-group p 0 :timestamp) clist))
  19917. ((memq 'org-link faces)
  19918. (push (list :link
  19919. (previous-single-property-change p 'face)
  19920. (next-single-property-change p 'face)) clist))
  19921. ((memq 'org-special-keyword faces)
  19922. (push (list :keyword
  19923. (previous-single-property-change p 'face)
  19924. (next-single-property-change p 'face)) clist))
  19925. ((org-at-target-p)
  19926. (push (org-point-in-group p 0 :target) clist)
  19927. (goto-char (1- (match-beginning 0)))
  19928. (if (looking-at org-radio-target-regexp)
  19929. (push (org-point-in-group p 0 :radio-target) clist))
  19930. (goto-char p))
  19931. ((setq o (car (delq nil
  19932. (mapcar
  19933. (lambda (x)
  19934. (if (memq x org-latex-fragment-image-overlays) x))
  19935. (overlays-at (point))))))
  19936. (push (list :latex-fragment
  19937. (overlay-start o) (overlay-end o)) clist)
  19938. (push (list :latex-preview
  19939. (overlay-start o) (overlay-end o)) clist))
  19940. ((org-inside-LaTeX-fragment-p)
  19941. ;; FIXME: positions wrong.
  19942. (push (list :latex-fragment (point) (point)) clist)))
  19943. (setq clist (nreverse (delq nil clist)))
  19944. clist))
  19945. ;; FIXME: Compare with at-regexp-p Do we need both?
  19946. (defun org-in-regexp (re &optional nlines visually)
  19947. "Check if point is inside a match of regexp.
  19948. Normally only the current line is checked, but you can include NLINES extra
  19949. lines both before and after point into the search.
  19950. If VISUALLY is set, require that the cursor is not after the match but
  19951. really on, so that the block visually is on the match."
  19952. (catch 'exit
  19953. (let ((pos (point))
  19954. (eol (point-at-eol (+ 1 (or nlines 0))))
  19955. (inc (if visually 1 0)))
  19956. (save-excursion
  19957. (beginning-of-line (- 1 (or nlines 0)))
  19958. (while (re-search-forward re eol t)
  19959. (if (and (<= (match-beginning 0) pos)
  19960. (>= (+ inc (match-end 0)) pos))
  19961. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  19962. (defun org-at-regexp-p (regexp)
  19963. "Is point inside a match of REGEXP in the current line?"
  19964. (catch 'exit
  19965. (save-excursion
  19966. (let ((pos (point)) (end (point-at-eol)))
  19967. (beginning-of-line 1)
  19968. (while (re-search-forward regexp end t)
  19969. (if (and (<= (match-beginning 0) pos)
  19970. (>= (match-end 0) pos))
  19971. (throw 'exit t)))
  19972. nil))))
  19973. (defun org-between-regexps-p (start-re end-re &optional lim-up lim-down)
  19974. "Non-nil when point is between matches of START-RE and END-RE.
  19975. Also return a non-nil value when point is on one of the matches.
  19976. Optional arguments LIM-UP and LIM-DOWN bound the search; they are
  19977. buffer positions. Default values are the positions of headlines
  19978. surrounding the point.
  19979. The functions returns a cons cell whose car (resp. cdr) is the
  19980. position before START-RE (resp. after END-RE)."
  19981. (save-match-data
  19982. (let ((pos (point))
  19983. (limit-up (or lim-up (save-excursion (outline-previous-heading))))
  19984. (limit-down (or lim-down (save-excursion (outline-next-heading))))
  19985. beg end)
  19986. (save-excursion
  19987. ;; Point is on a block when on START-RE or if START-RE can be
  19988. ;; found before it...
  19989. (and (or (org-at-regexp-p start-re)
  19990. (re-search-backward start-re limit-up t))
  19991. (setq beg (match-beginning 0))
  19992. ;; ... and END-RE after it...
  19993. (goto-char (match-end 0))
  19994. (re-search-forward end-re limit-down t)
  19995. (> (setq end (match-end 0)) pos)
  19996. ;; ... without another START-RE in-between.
  19997. (goto-char (match-beginning 0))
  19998. (not (re-search-backward start-re (1+ beg) t))
  19999. ;; Return value.
  20000. (cons beg end))))))
  20001. (defun org-in-block-p (names)
  20002. "Non-nil when point belongs to a block whose name belongs to NAMES.
  20003. NAMES is a list of strings containing names of blocks.
  20004. Return first block name matched, or nil. Beware that in case of
  20005. nested blocks, the returned name may not belong to the closest
  20006. block from point."
  20007. (save-match-data
  20008. (catch 'exit
  20009. (let ((case-fold-search t)
  20010. (lim-up (save-excursion (outline-previous-heading)))
  20011. (lim-down (save-excursion (outline-next-heading))))
  20012. (mapc (lambda (name)
  20013. (let ((n (regexp-quote name)))
  20014. (when (org-between-regexps-p
  20015. (concat "^[ \t]*#\\+begin_" n)
  20016. (concat "^[ \t]*#\\+end_" n)
  20017. lim-up lim-down)
  20018. (throw 'exit n))))
  20019. names))
  20020. nil)))
  20021. (defun org-in-drawer-p ()
  20022. "Is point within a drawer?"
  20023. (save-match-data
  20024. (let ((case-fold-search t)
  20025. (lim-up (save-excursion (outline-previous-heading)))
  20026. (lim-down (save-excursion (outline-next-heading))))
  20027. (org-between-regexps-p
  20028. (concat "^[ \t]*:" (regexp-opt org-drawers) ":")
  20029. "^[ \t]*:end:.*$"
  20030. lim-up lim-down))))
  20031. (defun org-occur-in-agenda-files (regexp &optional nlines)
  20032. "Call `multi-occur' with buffers for all agenda files."
  20033. (interactive "sOrg-files matching: \np")
  20034. (let* ((files (org-agenda-files))
  20035. (tnames (mapcar 'file-truename files))
  20036. (extra org-agenda-text-search-extra-files)
  20037. f)
  20038. (when (eq (car extra) 'agenda-archives)
  20039. (setq extra (cdr extra))
  20040. (setq files (org-add-archive-files files)))
  20041. (while (setq f (pop extra))
  20042. (unless (member (file-truename f) tnames)
  20043. (add-to-list 'files f 'append)
  20044. (add-to-list 'tnames (file-truename f) 'append)))
  20045. (multi-occur
  20046. (mapcar (lambda (x)
  20047. (with-current-buffer
  20048. (or (get-file-buffer x) (find-file-noselect x))
  20049. (widen)
  20050. (current-buffer)))
  20051. files)
  20052. regexp)))
  20053. (if (boundp 'occur-mode-find-occurrence-hook)
  20054. ;; Emacs 23
  20055. (add-hook 'occur-mode-find-occurrence-hook
  20056. (lambda ()
  20057. (when (derived-mode-p 'org-mode)
  20058. (org-reveal))))
  20059. ;; Emacs 22
  20060. (defadvice occur-mode-goto-occurrence
  20061. (after org-occur-reveal activate)
  20062. (and (derived-mode-p 'org-mode) (org-reveal)))
  20063. (defadvice occur-mode-goto-occurrence-other-window
  20064. (after org-occur-reveal activate)
  20065. (and (derived-mode-p 'org-mode) (org-reveal)))
  20066. (defadvice occur-mode-display-occurrence
  20067. (after org-occur-reveal activate)
  20068. (when (derived-mode-p 'org-mode)
  20069. (let ((pos (occur-mode-find-occurrence)))
  20070. (with-current-buffer (marker-buffer pos)
  20071. (save-excursion
  20072. (goto-char pos)
  20073. (org-reveal)))))))
  20074. (defun org-occur-link-in-agenda-files ()
  20075. "Create a link and search for it in the agendas.
  20076. The link is not stored in `org-stored-links', it is just created
  20077. for the search purpose."
  20078. (interactive)
  20079. (let ((link (condition-case nil
  20080. (org-store-link nil)
  20081. (error "Unable to create a link to here"))))
  20082. (org-occur-in-agenda-files (regexp-quote link))))
  20083. (defun org-reverse-string (string)
  20084. "Return the reverse of STRING."
  20085. (apply 'string (reverse (string-to-list string))))
  20086. ;; defsubst org-uniquify must be defined before first use
  20087. (defun org-uniquify-alist (alist)
  20088. "Merge elements of ALIST with the same key.
  20089. For example, in this alist:
  20090. \(org-uniquify-alist '((a 1) (b 2) (a 3)))
  20091. => '((a 1 3) (b 2))
  20092. merge (a 1) and (a 3) into (a 1 3).
  20093. The function returns the new ALIST."
  20094. (let (rtn)
  20095. (mapc
  20096. (lambda (e)
  20097. (let (n)
  20098. (if (not (assoc (car e) rtn))
  20099. (push e rtn)
  20100. (setq n (cons (car e) (append (cdr (assoc (car e) rtn)) (cdr e))))
  20101. (setq rtn (assq-delete-all (car e) rtn))
  20102. (push n rtn))))
  20103. alist)
  20104. rtn))
  20105. (defun org-delete-all (elts list)
  20106. "Remove all elements in ELTS from LIST."
  20107. (while elts
  20108. (setq list (delete (pop elts) list)))
  20109. list)
  20110. (defun org-count (cl-item cl-seq)
  20111. "Count the number of occurrences of ITEM in SEQ.
  20112. Taken from `count' in cl-seq.el with all keyword arguments removed."
  20113. (let ((cl-end (length cl-seq)) (cl-start 0) (cl-count 0) cl-x)
  20114. (when (consp cl-seq) (setq cl-seq (nthcdr cl-start cl-seq)))
  20115. (while (< cl-start cl-end)
  20116. (setq cl-x (if (consp cl-seq) (pop cl-seq) (aref cl-seq cl-start)))
  20117. (if (equal cl-item cl-x) (setq cl-count (1+ cl-count)))
  20118. (setq cl-start (1+ cl-start)))
  20119. cl-count))
  20120. (defun org-remove-if (predicate seq)
  20121. "Remove everything from SEQ that fulfills PREDICATE."
  20122. (let (res e)
  20123. (while seq
  20124. (setq e (pop seq))
  20125. (if (not (funcall predicate e)) (push e res)))
  20126. (nreverse res)))
  20127. (defun org-remove-if-not (predicate seq)
  20128. "Remove everything from SEQ that does not fulfill PREDICATE."
  20129. (let (res e)
  20130. (while seq
  20131. (setq e (pop seq))
  20132. (if (funcall predicate e) (push e res)))
  20133. (nreverse res)))
  20134. (defun org-reduce (cl-func cl-seq &rest cl-keys)
  20135. "Reduce two-argument FUNCTION across SEQ.
  20136. Taken from `reduce' in cl-seq.el with all keyword arguments but
  20137. \":initial-value\" removed."
  20138. (let ((cl-accum (cond ((memq :initial-value cl-keys)
  20139. (cadr (memq :initial-value cl-keys)))
  20140. (cl-seq (pop cl-seq))
  20141. (t (funcall cl-func)))))
  20142. (while cl-seq
  20143. (setq cl-accum (funcall cl-func cl-accum (pop cl-seq))))
  20144. cl-accum))
  20145. (defun org-every (pred seq)
  20146. "Return true if PREDICATE is true of every element of SEQ.
  20147. Adapted from `every' in cl.el."
  20148. (catch 'org-every
  20149. (mapc (lambda (e) (unless (funcall pred e) (throw 'org-every nil))) seq)
  20150. t))
  20151. (defun org-some (pred seq)
  20152. "Return true if PREDICATE is true of any element of SEQ.
  20153. Adapted from `some' in cl.el."
  20154. (catch 'org-some
  20155. (mapc (lambda (e) (when (funcall pred e) (throw 'org-some t))) seq)
  20156. nil))
  20157. (defun org-back-over-empty-lines ()
  20158. "Move backwards over whitespace, to the beginning of the first empty line.
  20159. Returns the number of empty lines passed."
  20160. (let ((pos (point)))
  20161. (if (cdr (assoc 'heading org-blank-before-new-entry))
  20162. (skip-chars-backward " \t\n\r")
  20163. (unless (eobp)
  20164. (forward-line -1)))
  20165. (beginning-of-line 2)
  20166. (goto-char (min (point) pos))
  20167. (count-lines (point) pos)))
  20168. (defun org-skip-whitespace ()
  20169. (skip-chars-forward " \t\n\r"))
  20170. (defun org-point-in-group (point group &optional context)
  20171. "Check if POINT is in match-group GROUP.
  20172. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  20173. match. If the match group does not exist or point is not inside it,
  20174. return nil."
  20175. (and (match-beginning group)
  20176. (>= point (match-beginning group))
  20177. (<= point (match-end group))
  20178. (if context
  20179. (list context (match-beginning group) (match-end group))
  20180. t)))
  20181. (defun org-switch-to-buffer-other-window (&rest args)
  20182. "Switch to buffer in a second window on the current frame.
  20183. In particular, do not allow pop-up frames.
  20184. Returns the newly created buffer."
  20185. (org-no-popups
  20186. (apply 'switch-to-buffer-other-window args)))
  20187. (defun org-combine-plists (&rest plists)
  20188. "Create a single property list from all plists in PLISTS.
  20189. The process starts by copying the first list, and then setting properties
  20190. from the other lists. Settings in the last list are the most significant
  20191. ones and overrule settings in the other lists."
  20192. (let ((rtn (copy-sequence (pop plists)))
  20193. p v ls)
  20194. (while plists
  20195. (setq ls (pop plists))
  20196. (while ls
  20197. (setq p (pop ls) v (pop ls))
  20198. (setq rtn (plist-put rtn p v))))
  20199. rtn))
  20200. (defun org-replace-escapes (string table)
  20201. "Replace %-escapes in STRING with values in TABLE.
  20202. TABLE is an association list with keys like \"%a\" and string values.
  20203. The sequences in STRING may contain normal field width and padding information,
  20204. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  20205. so values can contain further %-escapes if they are define later in TABLE."
  20206. (let ((tbl (copy-alist table))
  20207. (case-fold-search nil)
  20208. (pchg 0)
  20209. e re rpl)
  20210. (while (setq e (pop tbl))
  20211. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  20212. (when (and (cdr e) (string-match re (cdr e)))
  20213. (let ((sref (substring (cdr e) (match-beginning 0) (match-end 0)))
  20214. (safe "SREF"))
  20215. (add-text-properties 0 3 (list 'sref sref) safe)
  20216. (setcdr e (replace-match safe t t (cdr e)))))
  20217. (while (string-match re string)
  20218. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  20219. (cdr e)))
  20220. (setq string (replace-match rpl t t string))))
  20221. (while (setq pchg (next-property-change pchg string))
  20222. (let ((sref (get-text-property pchg 'sref string)))
  20223. (when (and sref (string-match "SREF" string pchg))
  20224. (setq string (replace-match sref t t string)))))
  20225. string))
  20226. (defun org-sublist (list start end)
  20227. "Return a section of LIST, from START to END.
  20228. Counting starts at 1."
  20229. (let (rtn (c start))
  20230. (setq list (nthcdr (1- start) list))
  20231. (while (and list (<= c end))
  20232. (push (pop list) rtn)
  20233. (setq c (1+ c)))
  20234. (nreverse rtn)))
  20235. (defun org-find-base-buffer-visiting (file)
  20236. "Like `find-buffer-visiting' but always return the base buffer and
  20237. not an indirect buffer."
  20238. (let ((buf (or (get-file-buffer file)
  20239. (find-buffer-visiting file))))
  20240. (if buf
  20241. (or (buffer-base-buffer buf) buf)
  20242. nil)))
  20243. (defun org-image-file-name-regexp (&optional extensions)
  20244. "Return regexp matching the file names of images.
  20245. If EXTENSIONS is given, only match these."
  20246. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  20247. (image-file-name-regexp)
  20248. (let ((image-file-name-extensions
  20249. (or extensions
  20250. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  20251. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  20252. (concat "\\."
  20253. (regexp-opt (nconc (mapcar 'upcase
  20254. image-file-name-extensions)
  20255. image-file-name-extensions)
  20256. t)
  20257. "\\'"))))
  20258. (defun org-file-image-p (file &optional extensions)
  20259. "Return non-nil if FILE is an image."
  20260. (save-match-data
  20261. (string-match (org-image-file-name-regexp extensions) file)))
  20262. (defun org-get-cursor-date (&optional with-time)
  20263. "Return the date at cursor in as a time.
  20264. This works in the calendar and in the agenda, anywhere else it just
  20265. returns the current time.
  20266. If WITH-TIME is non-nil, returns the time of the event at point (in
  20267. the agenda) or the current time of the day."
  20268. (let (date day defd tp tm hod mod)
  20269. (when with-time
  20270. (setq tp (get-text-property (point) 'time))
  20271. (when (and tp (string-match "\\([0-9][0-9]\\):\\([0-9][0-9]\\)" tp))
  20272. (setq hod (string-to-number (match-string 1 tp))
  20273. mod (string-to-number (match-string 2 tp))))
  20274. (or tp (setq hod (nth 2 (decode-time (current-time)))
  20275. mod (nth 1 (decode-time (current-time))))))
  20276. (cond
  20277. ((eq major-mode 'calendar-mode)
  20278. (setq date (calendar-cursor-to-date)
  20279. defd (encode-time 0 (or mod 0) (or hod 0)
  20280. (nth 1 date) (nth 0 date) (nth 2 date))))
  20281. ((eq major-mode 'org-agenda-mode)
  20282. (setq day (get-text-property (point) 'day))
  20283. (if day
  20284. (setq date (calendar-gregorian-from-absolute day)
  20285. defd (encode-time 0 (or mod 0) (or hod 0)
  20286. (nth 1 date) (nth 0 date) (nth 2 date))))))
  20287. (or defd (current-time))))
  20288. (defun org-mark-subtree (&optional up)
  20289. "Mark the current subtree.
  20290. This puts point at the start of the current subtree, and mark at
  20291. the end. If a numeric prefix UP is given, move up into the
  20292. hierarchy of headlines by UP levels before marking the subtree."
  20293. (interactive "P")
  20294. (org-with-limited-levels
  20295. (cond ((org-at-heading-p) (beginning-of-line))
  20296. ((org-before-first-heading-p) (user-error "Not in a subtree"))
  20297. (t (outline-previous-visible-heading 1))))
  20298. (when up (while (and (> up 0) (org-up-heading-safe)) (decf up)))
  20299. (if (org-called-interactively-p 'any)
  20300. (call-interactively 'org-mark-element)
  20301. (org-mark-element)))
  20302. ;;; Indentation
  20303. (defun org-indent-line ()
  20304. "Indent line depending on context."
  20305. (interactive)
  20306. (let* ((pos (point))
  20307. (itemp (org-at-item-p))
  20308. (case-fold-search t)
  20309. (org-drawer-regexp (or org-drawer-regexp "\000"))
  20310. (inline-task-p (and (featurep 'org-inlinetask)
  20311. (org-inlinetask-in-task-p)))
  20312. (inline-re (and inline-task-p
  20313. (org-inlinetask-outline-regexp)))
  20314. column)
  20315. (if (and orgstruct-is-++ (eq pos (point)))
  20316. (let ((indent-line-function (cadadr (assoc 'indent-line-function org-fb-vars))))
  20317. (indent-according-to-mode))
  20318. (beginning-of-line 1)
  20319. (cond
  20320. ;; Headings
  20321. ((looking-at org-outline-regexp) (setq column 0))
  20322. ;; Footnote definition
  20323. ((looking-at org-footnote-definition-re) (setq column 0))
  20324. ;; Literal examples
  20325. ((looking-at "[ \t]*:\\( \\|$\\)")
  20326. (setq column (org-get-indentation))) ; do nothing
  20327. ;; Lists
  20328. ((ignore-errors (goto-char (org-in-item-p)))
  20329. (setq column (if itemp
  20330. (org-get-indentation)
  20331. (org-list-item-body-column (point))))
  20332. (goto-char pos))
  20333. ;; Drawers
  20334. ((and (looking-at "[ \t]*:END:")
  20335. (save-excursion (re-search-backward org-drawer-regexp nil t)))
  20336. (save-excursion
  20337. (goto-char (1- (match-beginning 1)))
  20338. (setq column (current-column))))
  20339. ;; Special blocks
  20340. ((and (looking-at "[ \t]*#\\+end_\\([a-z]+\\)")
  20341. (save-excursion
  20342. (re-search-backward
  20343. (concat "^[ \t]*#\\+begin_" (downcase (match-string 1))) nil t)))
  20344. (setq column (org-get-indentation (match-string 0))))
  20345. ((and (not (looking-at "[ \t]*#\\+begin_"))
  20346. (org-between-regexps-p "^[ \t]*#\\+begin_" "[ \t]*#\\+end_"))
  20347. (save-excursion
  20348. (re-search-backward "^[ \t]*#\\+begin_\\([a-z]+\\)" nil t))
  20349. (setq column
  20350. (cond ((equal (downcase (match-string 1)) "src")
  20351. ;; src blocks: let `org-edit-src-exit' handle them
  20352. (org-get-indentation))
  20353. ((equal (downcase (match-string 1)) "example")
  20354. (max (org-get-indentation)
  20355. (org-get-indentation (match-string 0))))
  20356. (t
  20357. (org-get-indentation (match-string 0))))))
  20358. ;; This line has nothing special, look at the previous relevant
  20359. ;; line to compute indentation
  20360. (t
  20361. (beginning-of-line 0)
  20362. (while (and (not (bobp))
  20363. (not (looking-at org-table-line-regexp))
  20364. (not (looking-at org-drawer-regexp))
  20365. ;; When point started in an inline task, do not move
  20366. ;; above task starting line.
  20367. (not (and inline-task-p (looking-at inline-re)))
  20368. ;; Skip drawers, blocks, empty lines, verbatim,
  20369. ;; comments, tables, footnotes definitions, lists,
  20370. ;; inline tasks.
  20371. (or (and (looking-at "[ \t]*:END:")
  20372. (re-search-backward org-drawer-regexp nil t))
  20373. (and (looking-at "[ \t]*#\\+end_")
  20374. (re-search-backward "[ \t]*#\\+begin_"nil t))
  20375. (looking-at "[ \t]*[\n:#|]")
  20376. (looking-at org-footnote-definition-re)
  20377. (and (not inline-task-p)
  20378. (featurep 'org-inlinetask)
  20379. (org-inlinetask-in-task-p)
  20380. (or (org-inlinetask-goto-beginning) t))))
  20381. (beginning-of-line 0))
  20382. (cond
  20383. ;; There was a list item above.
  20384. ((ignore-errors (goto-char (org-in-item-p)))
  20385. (goto-char (org-list-get-top-point (org-list-struct)))
  20386. (setq column (org-get-indentation)))
  20387. ;; There was an heading above.
  20388. ((looking-at "\\*+[ \t]+")
  20389. (if (not org-adapt-indentation)
  20390. (setq column 0)
  20391. (goto-char (match-end 0))
  20392. (setq column (current-column))))
  20393. ;; A drawer had started and is unfinished
  20394. ((looking-at org-drawer-regexp)
  20395. (goto-char (1- (match-beginning 1)))
  20396. (setq column (current-column)))
  20397. ;; Else, nothing noticeable found: get indentation and go on.
  20398. (t (setq column (org-get-indentation))))))
  20399. ;; Now apply indentation and move cursor accordingly
  20400. (goto-char pos)
  20401. (if (<= (current-column) (current-indentation))
  20402. (org-indent-line-to column)
  20403. (save-excursion (org-indent-line-to column)))
  20404. ;; Special polishing for properties, see `org-property-format'
  20405. (setq column (current-column))
  20406. (beginning-of-line 1)
  20407. (if (looking-at org-property-re)
  20408. (replace-match (concat (match-string 4)
  20409. (format org-property-format
  20410. (match-string 1) (match-string 3)))
  20411. t t))
  20412. (org-move-to-column column))))
  20413. (defun org-indent-drawer ()
  20414. "Indent the drawer at point."
  20415. (interactive)
  20416. (let ((p (point))
  20417. (e (and (save-excursion (re-search-forward ":END:" nil t))
  20418. (match-end 0)))
  20419. (folded
  20420. (save-excursion
  20421. (end-of-line)
  20422. (when (overlays-at (point))
  20423. (member 'invisible (overlay-properties
  20424. (car (overlays-at (point)))))))))
  20425. (when folded (org-cycle))
  20426. (indent-for-tab-command)
  20427. (while (and (move-beginning-of-line 2) (< (point) e))
  20428. (indent-for-tab-command))
  20429. (goto-char p)
  20430. (when folded (org-cycle)))
  20431. (message "Drawer at point indented"))
  20432. (defun org-indent-block ()
  20433. "Indent the block at point."
  20434. (interactive)
  20435. (let ((p (point))
  20436. (case-fold-search t)
  20437. (e (and (save-excursion (re-search-forward "#\\+end_?\\(?:[a-z]+\\)?" nil t))
  20438. (match-end 0)))
  20439. (folded
  20440. (save-excursion
  20441. (end-of-line)
  20442. (when (overlays-at (point))
  20443. (member 'invisible (overlay-properties
  20444. (car (overlays-at (point)))))))))
  20445. (when folded (org-cycle))
  20446. (indent-for-tab-command)
  20447. (while (and (move-beginning-of-line 2) (< (point) e))
  20448. (indent-for-tab-command))
  20449. (goto-char p)
  20450. (when folded (org-cycle)))
  20451. (message "Block at point indented"))
  20452. (defun org-indent-region (start end)
  20453. "Indent region."
  20454. (interactive "r")
  20455. (save-excursion
  20456. (let ((line-end (org-current-line end)))
  20457. (goto-char start)
  20458. (while (< (org-current-line) line-end)
  20459. (cond ((org-in-src-block-p t) (org-src-native-tab-command-maybe))
  20460. (t (call-interactively 'org-indent-line)))
  20461. (move-beginning-of-line 2)))))
  20462. ;;; Filling
  20463. ;; We use our own fill-paragraph and auto-fill functions.
  20464. ;; `org-fill-paragraph' relies on adaptive filling and context
  20465. ;; checking. Appropriate `fill-prefix' is computed with
  20466. ;; `org-adaptive-fill-function'.
  20467. ;; `org-auto-fill-function' takes care of auto-filling. It calls
  20468. ;; `do-auto-fill' only on valid areas with `fill-prefix' shadowed with
  20469. ;; `org-adaptive-fill-function' value. Internally,
  20470. ;; `org-comment-line-break-function' breaks the line.
  20471. ;; `org-setup-filling' installs filling and auto-filling related
  20472. ;; variables during `org-mode' initialization.
  20473. (defvar org-element-paragraph-separate) ; org-element.el
  20474. (defun org-setup-filling ()
  20475. (require 'org-element)
  20476. ;; Prevent auto-fill from inserting unwanted new items.
  20477. (when (boundp 'fill-nobreak-predicate)
  20478. (org-set-local
  20479. 'fill-nobreak-predicate
  20480. (org-uniquify
  20481. (append fill-nobreak-predicate
  20482. '(org-fill-line-break-nobreak-p
  20483. org-fill-paragraph-with-timestamp-nobreak-p)))))
  20484. (let ((paragraph-ending (substring org-element-paragraph-separate 1)))
  20485. (org-set-local 'paragraph-start paragraph-ending)
  20486. (org-set-local 'paragraph-separate paragraph-ending))
  20487. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  20488. (org-set-local 'auto-fill-inhibit-regexp nil)
  20489. (org-set-local 'adaptive-fill-function 'org-adaptive-fill-function)
  20490. (org-set-local 'normal-auto-fill-function 'org-auto-fill-function)
  20491. (org-set-local 'comment-line-break-function 'org-comment-line-break-function))
  20492. (defun org-fill-line-break-nobreak-p ()
  20493. "Non-nil when a new line at point would create an Org line break."
  20494. (save-excursion
  20495. (skip-chars-backward "[ \t]")
  20496. (skip-chars-backward "\\\\")
  20497. (looking-at "\\\\\\\\\\($\\|[^\\\\]\\)")))
  20498. (defun org-fill-paragraph-with-timestamp-nobreak-p ()
  20499. "Non-nil when a new line at point would split a timestamp."
  20500. (and (org-at-timestamp-p t)
  20501. (not (looking-at org-ts-regexp-both))))
  20502. (declare-function message-in-body-p "message" ())
  20503. (defvar orgtbl-line-start-regexp) ; From org-table.el
  20504. (defun org-adaptive-fill-function ()
  20505. "Compute a fill prefix for the current line.
  20506. Return fill prefix, as a string, or nil if current line isn't
  20507. meant to be filled. For convenience, if `adaptive-fill-regexp'
  20508. matches in paragraphs or comments, use it."
  20509. (catch 'exit
  20510. (when (derived-mode-p 'message-mode)
  20511. (save-excursion
  20512. (beginning-of-line)
  20513. (cond ((or (not (message-in-body-p))
  20514. (looking-at orgtbl-line-start-regexp))
  20515. (throw 'exit nil))
  20516. ((looking-at message-cite-prefix-regexp)
  20517. (throw 'exit (match-string-no-properties 0)))
  20518. ((looking-at org-outline-regexp)
  20519. (throw 'exit (make-string (length (match-string 0)) ? ))))))
  20520. (org-with-wide-buffer
  20521. (let* ((p (line-beginning-position))
  20522. (element (save-excursion
  20523. (beginning-of-line)
  20524. (or (ignore-errors (org-element-at-point))
  20525. (user-error "An element cannot be parsed line %d"
  20526. (line-number-at-pos (point))))))
  20527. (type (org-element-type element))
  20528. (post-affiliated (org-element-property :post-affiliated element)))
  20529. (unless (and post-affiliated (< p post-affiliated))
  20530. (case type
  20531. (comment
  20532. (save-excursion
  20533. (beginning-of-line)
  20534. (looking-at "[ \t]*")
  20535. (concat (match-string 0) "# ")))
  20536. (footnote-definition "")
  20537. ((item plain-list)
  20538. (make-string (org-list-item-body-column
  20539. (or post-affiliated
  20540. (org-element-property :begin element)))
  20541. ? ))
  20542. (paragraph
  20543. ;; Fill prefix is usually the same as the current line,
  20544. ;; unless the paragraph is at the beginning of an item.
  20545. (let ((parent (org-element-property :parent element)))
  20546. (save-excursion
  20547. (beginning-of-line)
  20548. (cond ((eq (org-element-type parent) 'item)
  20549. (make-string (org-list-item-body-column
  20550. (org-element-property :begin parent))
  20551. ? ))
  20552. ((and adaptive-fill-regexp
  20553. ;; Locally disable
  20554. ;; `adaptive-fill-function' to let
  20555. ;; `fill-context-prefix' handle
  20556. ;; `adaptive-fill-regexp' variable.
  20557. (let (adaptive-fill-function)
  20558. (fill-context-prefix
  20559. post-affiliated
  20560. (org-element-property :end element)))))
  20561. ((looking-at "[ \t]+") (match-string 0))
  20562. (t "")))))
  20563. (comment-block
  20564. ;; Only fill contents if P is within block boundaries.
  20565. (let* ((cbeg (save-excursion (goto-char post-affiliated)
  20566. (forward-line)
  20567. (point)))
  20568. (cend (save-excursion
  20569. (goto-char (org-element-property :end element))
  20570. (skip-chars-backward " \r\t\n")
  20571. (line-beginning-position))))
  20572. (when (and (>= p cbeg) (< p cend))
  20573. (if (save-excursion (beginning-of-line) (looking-at "[ \t]+"))
  20574. (match-string 0)
  20575. ""))))))))))
  20576. (declare-function message-goto-body "message" ())
  20577. (defvar message-cite-prefix-regexp) ; From message.el
  20578. (defun org-fill-paragraph (&optional justify)
  20579. "Fill element at point, when applicable.
  20580. This function only applies to comment blocks, comments, example
  20581. blocks and paragraphs. Also, as a special case, re-align table
  20582. when point is at one.
  20583. If JUSTIFY is non-nil (interactively, with prefix argument),
  20584. justify as well. If `sentence-end-double-space' is non-nil, then
  20585. period followed by one space does not end a sentence, so don't
  20586. break a line there. The variable `fill-column' controls the
  20587. width for filling.
  20588. For convenience, when point is at a plain list, an item or
  20589. a footnote definition, try to fill the first paragraph within."
  20590. (interactive)
  20591. (if (and (derived-mode-p 'message-mode)
  20592. (or (not (message-in-body-p))
  20593. (save-excursion (move-beginning-of-line 1)
  20594. (looking-at message-cite-prefix-regexp))))
  20595. ;; First ensure filling is correct in message-mode.
  20596. (let ((fill-paragraph-function
  20597. (cadadr (assoc 'fill-paragraph-function org-fb-vars)))
  20598. (fill-prefix (cadadr (assoc 'fill-prefix org-fb-vars)))
  20599. (paragraph-start (cadadr (assoc 'paragraph-start org-fb-vars)))
  20600. (paragraph-separate
  20601. (cadadr (assoc 'paragraph-separate org-fb-vars))))
  20602. (fill-paragraph nil))
  20603. (with-syntax-table org-mode-transpose-word-syntax-table
  20604. ;; Move to end of line in order to get the first paragraph
  20605. ;; within a plain list or a footnote definition.
  20606. (let ((element (save-excursion
  20607. (end-of-line)
  20608. (or (ignore-errors (org-element-at-point))
  20609. (user-error "An element cannot be parsed line %d"
  20610. (line-number-at-pos (point)))))))
  20611. ;; First check if point is in a blank line at the beginning of
  20612. ;; the buffer. In that case, ignore filling.
  20613. (case (org-element-type element)
  20614. ;; Use major mode filling function is src blocks.
  20615. (src-block (org-babel-do-key-sequence-in-edit-buffer (kbd "M-q")))
  20616. ;; Align Org tables, leave table.el tables as-is.
  20617. (table-row (org-table-align) t)
  20618. (table
  20619. (when (eq (org-element-property :type element) 'org)
  20620. (save-excursion
  20621. (goto-char (org-element-property :post-affiliated element))
  20622. (org-table-align)))
  20623. t)
  20624. (paragraph
  20625. ;; Paragraphs may contain `line-break' type objects.
  20626. (let ((beg (max (point-min)
  20627. (org-element-property :contents-begin element)))
  20628. (end (min (point-max)
  20629. (org-element-property :contents-end element))))
  20630. ;; Do nothing if point is at an affiliated keyword.
  20631. (if (< (line-end-position) beg) t
  20632. (when (derived-mode-p 'message-mode)
  20633. ;; In `message-mode', do not fill following citation
  20634. ;; in current paragraph nor text before message body.
  20635. (let ((body-start (save-excursion (message-goto-body))))
  20636. (when body-start (setq beg (max body-start beg))))
  20637. (when (save-excursion
  20638. (re-search-forward
  20639. (concat "^" message-cite-prefix-regexp) end t))
  20640. (setq end (match-beginning 0))))
  20641. ;; Fill paragraph, taking line breaks into account.
  20642. ;; For that, slice the paragraph using line breaks as
  20643. ;; separators, and fill the parts in reverse order to
  20644. ;; avoid messing with markers.
  20645. (save-excursion
  20646. (goto-char end)
  20647. (mapc
  20648. (lambda (pos)
  20649. (fill-region-as-paragraph pos (point) justify)
  20650. (goto-char pos))
  20651. ;; Find the list of ending positions for line breaks
  20652. ;; in the current paragraph. Add paragraph
  20653. ;; beginning to include first slice.
  20654. (nreverse
  20655. (cons beg
  20656. (org-element-map
  20657. (org-element--parse-objects
  20658. beg end nil (org-element-restriction 'paragraph))
  20659. 'line-break
  20660. (lambda (lb) (org-element-property :end lb)))))))
  20661. t)))
  20662. ;; Contents of `comment-block' type elements should be
  20663. ;; filled as plain text, but only if point is within block
  20664. ;; markers.
  20665. (comment-block
  20666. (let* ((case-fold-search t)
  20667. (beg (save-excursion
  20668. (goto-char (org-element-property :begin element))
  20669. (re-search-forward "^[ \t]*#\\+begin_comment" nil t)
  20670. (forward-line)
  20671. (point)))
  20672. (end (save-excursion
  20673. (goto-char (org-element-property :end element))
  20674. (re-search-backward "^[ \t]*#\\+end_comment" nil t)
  20675. (line-beginning-position))))
  20676. (if (or (< (point) beg) (> (point) end)) t
  20677. (fill-region-as-paragraph
  20678. (save-excursion (end-of-line)
  20679. (re-search-backward "^[ \t]*$" beg 'move)
  20680. (line-beginning-position))
  20681. (save-excursion (beginning-of-line)
  20682. (re-search-forward "^[ \t]*$" end 'move)
  20683. (line-beginning-position))
  20684. justify))))
  20685. ;; Fill comments.
  20686. (comment
  20687. (let ((begin (org-element-property :post-affiliated element))
  20688. (end (org-element-property :end element)))
  20689. (when (and (>= (point) begin) (<= (point) end))
  20690. (let ((begin (save-excursion
  20691. (end-of-line)
  20692. (if (re-search-backward "^[ \t]*#[ \t]*$" begin t)
  20693. (progn (forward-line) (point))
  20694. begin)))
  20695. (end (save-excursion
  20696. (end-of-line)
  20697. (if (re-search-forward "^[ \t]*#[ \t]*$" end 'move)
  20698. (1- (line-beginning-position))
  20699. (skip-chars-backward " \r\t\n")
  20700. (line-end-position)))))
  20701. ;; Do not fill comments when at a blank line.
  20702. (when (> end begin)
  20703. (let ((fill-prefix
  20704. (save-excursion
  20705. (beginning-of-line)
  20706. (looking-at "[ \t]*#")
  20707. (let ((comment-prefix (match-string 0)))
  20708. (goto-char (match-end 0))
  20709. (if (looking-at adaptive-fill-regexp)
  20710. (concat comment-prefix (match-string 0))
  20711. (concat comment-prefix " "))))))
  20712. (save-excursion
  20713. (fill-region-as-paragraph begin end justify))))))
  20714. t))
  20715. ;; Ignore every other element.
  20716. (otherwise t))))))
  20717. (defun org-auto-fill-function ()
  20718. "Auto-fill function."
  20719. ;; Check if auto-filling is meaningful.
  20720. (let ((fc (current-fill-column)))
  20721. (when (and fc (> (current-column) fc))
  20722. (let* ((fill-prefix (org-adaptive-fill-function))
  20723. ;; Enforce empty fill prefix, if required. Otherwise, it
  20724. ;; will be computed again.
  20725. (adaptive-fill-mode (not (equal fill-prefix ""))))
  20726. (when fill-prefix (do-auto-fill))))))
  20727. (defun org-comment-line-break-function (&optional soft)
  20728. "Break line at point and indent, continuing comment if within one.
  20729. The inserted newline is marked hard if variable
  20730. `use-hard-newlines' is true, unless optional argument SOFT is
  20731. non-nil."
  20732. (if soft (insert-and-inherit ?\n) (newline 1))
  20733. (save-excursion (forward-char -1) (delete-horizontal-space))
  20734. (delete-horizontal-space)
  20735. (indent-to-left-margin)
  20736. (insert-before-markers-and-inherit fill-prefix))
  20737. ;;; Comments
  20738. ;; Org comments syntax is quite complex. It requires the entire line
  20739. ;; to be just a comment. Also, even with the right syntax at the
  20740. ;; beginning of line, some some elements (i.e. verse-block or
  20741. ;; example-block) don't accept comments. Usual Emacs comment commands
  20742. ;; cannot cope with those requirements. Therefore, Org replaces them.
  20743. ;; Org still relies on `comment-dwim', but cannot trust
  20744. ;; `comment-only-p'. So, `comment-region-function' and
  20745. ;; `uncomment-region-function' both point
  20746. ;; to`org-comment-or-uncomment-region'. Eventually,
  20747. ;; `org-insert-comment' takes care of insertion of comments at the
  20748. ;; beginning of line.
  20749. ;; `org-setup-comments-handling' install comments related variables
  20750. ;; during `org-mode' initialization.
  20751. (defun org-setup-comments-handling ()
  20752. (interactive)
  20753. (org-set-local 'comment-use-syntax nil)
  20754. (org-set-local 'comment-start "# ")
  20755. (org-set-local 'comment-start-skip "^\\s-*#\\(?: \\|$\\)")
  20756. (org-set-local 'comment-insert-comment-function 'org-insert-comment)
  20757. (org-set-local 'comment-region-function 'org-comment-or-uncomment-region)
  20758. (org-set-local 'uncomment-region-function 'org-comment-or-uncomment-region))
  20759. (defun org-insert-comment ()
  20760. "Insert an empty comment above current line.
  20761. If the line is empty, insert comment at its beginning."
  20762. (beginning-of-line)
  20763. (if (looking-at "\\s-*$") (replace-match "") (open-line 1))
  20764. (org-indent-line)
  20765. (insert "# "))
  20766. (defvar comment-empty-lines) ; From newcomment.el.
  20767. (defun org-comment-or-uncomment-region (beg end &rest ignore)
  20768. "Comment or uncomment each non-blank line in the region.
  20769. Uncomment each non-blank line between BEG and END if it only
  20770. contains commented lines. Otherwise, comment them."
  20771. (save-restriction
  20772. ;; Restrict region
  20773. (narrow-to-region (save-excursion (goto-char beg)
  20774. (skip-chars-forward " \r\t\n" end)
  20775. (line-beginning-position))
  20776. (save-excursion (goto-char end)
  20777. (skip-chars-backward " \r\t\n" beg)
  20778. (line-end-position)))
  20779. (let ((uncommentp
  20780. ;; UNCOMMENTP is non-nil when every non blank line between
  20781. ;; BEG and END is a comment.
  20782. (save-excursion
  20783. (goto-char (point-min))
  20784. (while (and (not (eobp))
  20785. (let ((element (org-element-at-point)))
  20786. (and (eq (org-element-type element) 'comment)
  20787. (goto-char (min (point-max)
  20788. (org-element-property
  20789. :end element)))))))
  20790. (eobp))))
  20791. (if uncommentp
  20792. ;; Only blank lines and comments in region: uncomment it.
  20793. (save-excursion
  20794. (goto-char (point-min))
  20795. (while (not (eobp))
  20796. (when (looking-at "[ \t]*\\(#\\(?: \\|$\\)\\)")
  20797. (replace-match "" nil nil nil 1))
  20798. (forward-line)))
  20799. ;; Comment each line in region.
  20800. (let ((min-indent (point-max)))
  20801. ;; First find the minimum indentation across all lines.
  20802. (save-excursion
  20803. (goto-char (point-min))
  20804. (while (and (not (eobp)) (not (zerop min-indent)))
  20805. (unless (looking-at "[ \t]*$")
  20806. (setq min-indent (min min-indent (current-indentation))))
  20807. (forward-line)))
  20808. ;; Then loop over all lines.
  20809. (save-excursion
  20810. (goto-char (point-min))
  20811. (while (not (eobp))
  20812. (unless (and (not comment-empty-lines) (looking-at "[ \t]*$"))
  20813. ;; Don't get fooled by invisible text (e.g. link path)
  20814. ;; when moving to column MIN-INDENT.
  20815. (let ((buffer-invisibility-spec nil))
  20816. (org-move-to-column min-indent t))
  20817. (insert comment-start))
  20818. (forward-line))))))))
  20819. ;;; Planning
  20820. ;; This section contains tools to operate on timestamp objects, as
  20821. ;; returned by, e.g. `org-element-context'.
  20822. (defun org-timestamp-has-time-p (timestamp)
  20823. "Non-nil when TIMESTAMP has a time specified."
  20824. (org-element-property :hour-start timestamp))
  20825. (defun org-timestamp-format (timestamp format &optional end utc)
  20826. "Format a TIMESTAMP element into a string.
  20827. FORMAT is a format specifier to be passed to
  20828. `format-time-string'.
  20829. When optional argument END is non-nil, use end of date-range or
  20830. time-range, if possible.
  20831. When optional argument UTC is non-nil, time will be expressed as
  20832. Universal Time."
  20833. (format-time-string
  20834. format
  20835. (apply 'encode-time
  20836. (cons 0
  20837. (mapcar
  20838. (lambda (prop) (or (org-element-property prop timestamp) 0))
  20839. (if end '(:minute-end :hour-end :day-end :month-end :year-end)
  20840. '(:minute-start :hour-start :day-start :month-start
  20841. :year-start)))))
  20842. utc))
  20843. (defun org-timestamp-split-range (timestamp &optional end)
  20844. "Extract a timestamp object from a date or time range.
  20845. TIMESTAMP is a timestamp object. END, when non-nil, means extract
  20846. the end of the range. Otherwise, extract its start.
  20847. Return a new timestamp object sharing the same parent as
  20848. TIMESTAMP."
  20849. (let ((type (org-element-property :type timestamp)))
  20850. (if (memq type '(active inactive diary)) timestamp
  20851. (let ((split-ts (list 'timestamp (copy-sequence (nth 1 timestamp)))))
  20852. ;; Set new type.
  20853. (org-element-put-property
  20854. split-ts :type (if (eq type 'active-range) 'active 'inactive))
  20855. ;; Copy start properties over end properties if END is
  20856. ;; non-nil. Otherwise, copy end properties over `start' ones.
  20857. (let ((p-alist '((:minute-start . :minute-end)
  20858. (:hour-start . :hour-end)
  20859. (:day-start . :day-end)
  20860. (:month-start . :month-end)
  20861. (:year-start . :year-end))))
  20862. (dolist (p-cell p-alist)
  20863. (org-element-put-property
  20864. split-ts
  20865. (funcall (if end 'car 'cdr) p-cell)
  20866. (org-element-property
  20867. (funcall (if end 'cdr 'car) p-cell) split-ts)))
  20868. ;; Eventually refresh `:raw-value'.
  20869. (org-element-put-property split-ts :raw-value nil)
  20870. (org-element-put-property
  20871. split-ts :raw-value (org-element-interpret-data split-ts)))))))
  20872. (defun org-timestamp-translate (timestamp &optional boundary)
  20873. "Apply `org-translate-time' on a TIMESTAMP object.
  20874. When optional argument BOUNDARY is non-nil, it is either the
  20875. symbol `start' or `end'. In this case, only translate the
  20876. starting or ending part of TIMESTAMP if it is a date or time
  20877. range. Otherwise, translate both parts."
  20878. (if (and (not boundary)
  20879. (memq (org-element-property :type timestamp)
  20880. '(active-range inactive-range)))
  20881. (concat
  20882. (org-translate-time
  20883. (org-element-property :raw-value
  20884. (org-timestamp-split-range timestamp)))
  20885. "--"
  20886. (org-translate-time
  20887. (org-element-property :raw-value
  20888. (org-timestamp-split-range timestamp t))))
  20889. (org-translate-time
  20890. (org-element-property
  20891. :raw-value
  20892. (if (not boundary) timestamp
  20893. (org-timestamp-split-range timestamp (eq boundary 'end)))))))
  20894. ;;; Other stuff.
  20895. (defun org-toggle-fixed-width-section (arg)
  20896. "Toggle the fixed-width export.
  20897. If there is no active region, the QUOTE keyword at the current headline is
  20898. inserted or removed. When present, it causes the text between this headline
  20899. and the next to be exported as fixed-width text, and unmodified.
  20900. If there is an active region, this command adds or removes a colon as the
  20901. first character of this line. If the first character of a line is a colon,
  20902. this line is also exported in fixed-width font."
  20903. (interactive "P")
  20904. (let* ((cc 0)
  20905. (regionp (org-region-active-p))
  20906. (beg (if regionp (region-beginning) (point)))
  20907. (end (if regionp (region-end)))
  20908. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  20909. (case-fold-search nil)
  20910. (re "[ \t]*\\(:\\(?: \\|$\\)\\)")
  20911. off)
  20912. (if regionp
  20913. (save-excursion
  20914. (goto-char beg)
  20915. (setq cc (current-column))
  20916. (beginning-of-line 1)
  20917. (setq off (looking-at re))
  20918. (while (> nlines 0)
  20919. (setq nlines (1- nlines))
  20920. (beginning-of-line 1)
  20921. (cond
  20922. (arg
  20923. (org-move-to-column cc t)
  20924. (insert ": \n")
  20925. (forward-line -1))
  20926. ((and off (looking-at re))
  20927. (replace-match "" t t nil 1))
  20928. ((not off) (org-move-to-column cc t) (insert ": ")))
  20929. (forward-line 1)))
  20930. (save-excursion
  20931. (org-back-to-heading)
  20932. (cond
  20933. ((looking-at (format org-heading-keyword-regexp-format
  20934. org-quote-string))
  20935. (goto-char (match-end 1))
  20936. (looking-at (concat " +" org-quote-string))
  20937. (replace-match "" t t)
  20938. (when (eolp) (insert " ")))
  20939. ((looking-at org-outline-regexp)
  20940. (goto-char (match-end 0))
  20941. (insert org-quote-string " ")))))))
  20942. (defun org-reftex-citation ()
  20943. "Use reftex-citation to insert a citation into the buffer.
  20944. This looks for a line like
  20945. #+BIBLIOGRAPHY: foo plain option:-d
  20946. and derives from it that foo.bib is the bibliography file relevant
  20947. for this document. It then installs the necessary environment for RefTeX
  20948. to work in this buffer and calls `reftex-citation' to insert a citation
  20949. into the buffer.
  20950. Export of such citations to both LaTeX and HTML is handled by the contributed
  20951. package ox-bibtex by Taru Karttunen."
  20952. (interactive)
  20953. (let ((reftex-docstruct-symbol 'rds)
  20954. (reftex-cite-format "\\cite{%l}")
  20955. rds bib)
  20956. (save-excursion
  20957. (save-restriction
  20958. (widen)
  20959. (let ((case-fold-search t)
  20960. (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
  20961. (if (not (save-excursion
  20962. (or (re-search-forward re nil t)
  20963. (re-search-backward re nil t))))
  20964. (error "No bibliography defined in file")
  20965. (setq bib (concat (match-string 1) ".bib")
  20966. rds (list (list 'bib bib)))))))
  20967. (call-interactively 'reftex-citation)))
  20968. ;;;; Functions extending outline functionality
  20969. (defun org-beginning-of-line (&optional arg)
  20970. "Go to the beginning of the current line. If that is invisible, continue
  20971. to a visible line beginning. This makes the function of C-a more intuitive.
  20972. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  20973. first attempt, and only move to after the tags when the cursor is already
  20974. beyond the end of the headline."
  20975. (interactive "P")
  20976. (let ((pos (point))
  20977. (special (if (consp org-special-ctrl-a/e)
  20978. (car org-special-ctrl-a/e)
  20979. org-special-ctrl-a/e))
  20980. deactivate-mark refpos)
  20981. (if (org-bound-and-true-p visual-line-mode)
  20982. (beginning-of-visual-line 1)
  20983. (beginning-of-line 1))
  20984. (if (and arg (fboundp 'move-beginning-of-line))
  20985. (call-interactively 'move-beginning-of-line)
  20986. (if (bobp)
  20987. nil
  20988. (backward-char 1)
  20989. (if (org-truely-invisible-p)
  20990. (while (and (not (bobp)) (org-truely-invisible-p))
  20991. (backward-char 1)
  20992. (beginning-of-line 1))
  20993. (forward-char 1))))
  20994. (when special
  20995. (cond
  20996. ((and (looking-at org-complex-heading-regexp)
  20997. (= (char-after (match-end 1)) ?\ ))
  20998. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  20999. (point-at-eol)))
  21000. (goto-char
  21001. (if (eq special t)
  21002. (cond ((> pos refpos) refpos)
  21003. ((= pos (point)) refpos)
  21004. (t (point)))
  21005. (cond ((> pos (point)) (point))
  21006. ((not (eq last-command this-command)) (point))
  21007. (t refpos)))))
  21008. ((org-at-item-p)
  21009. ;; Being at an item and not looking at an the item means point
  21010. ;; was previously moved to beginning of a visual line, which
  21011. ;; doesn't contain the item. Therefore, do nothing special,
  21012. ;; just stay here.
  21013. (when (looking-at org-list-full-item-re)
  21014. ;; Set special position at first white space character after
  21015. ;; bullet, and check-box, if any.
  21016. (let ((after-bullet
  21017. (let ((box (match-end 3)))
  21018. (if (not box) (match-end 1)
  21019. (let ((after (char-after box)))
  21020. (if (and after (= after ? )) (1+ box) box))))))
  21021. ;; Special case: Move point to special position when
  21022. ;; currently after it or at beginning of line.
  21023. (if (eq special t)
  21024. (when (or (> pos after-bullet) (= (point) pos))
  21025. (goto-char after-bullet))
  21026. ;; Reversed case: Move point to special position when
  21027. ;; point was already at beginning of line and command is
  21028. ;; repeated.
  21029. (when (and (= (point) pos) (eq last-command this-command))
  21030. (goto-char after-bullet))))))))
  21031. (org-no-warnings
  21032. (and (featurep 'xemacs) (setq zmacs-region-stays t))))
  21033. (setq disable-point-adjustment
  21034. (or (not (invisible-p (point)))
  21035. (not (invisible-p (max (point-min) (1- (point))))))))
  21036. (defun org-end-of-line (&optional arg)
  21037. "Go to the end of the line.
  21038. If this is a headline, and `org-special-ctrl-a/e' is set, ignore
  21039. tags on the first attempt, and only move to after the tags when
  21040. the cursor is already beyond the end of the headline."
  21041. (interactive "P")
  21042. (let ((special (if (consp org-special-ctrl-a/e) (cdr org-special-ctrl-a/e)
  21043. org-special-ctrl-a/e))
  21044. (move-fun (cond ((org-bound-and-true-p visual-line-mode)
  21045. 'end-of-visual-line)
  21046. ((fboundp 'move-end-of-line) 'move-end-of-line)
  21047. (t 'end-of-line)))
  21048. deactivate-mark)
  21049. (if (or (not special) arg) (call-interactively move-fun)
  21050. (let* ((element (save-excursion (beginning-of-line)
  21051. (org-element-at-point)))
  21052. (type (org-element-type element)))
  21053. (cond
  21054. ((memq type '(headline inlinetask))
  21055. (let ((pos (point)))
  21056. (beginning-of-line 1)
  21057. (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*\\)?$"))
  21058. (if (eq special t)
  21059. (if (or (< pos (match-beginning 1)) (= pos (match-end 0)))
  21060. (goto-char (match-beginning 1))
  21061. (goto-char (match-end 0)))
  21062. (if (or (< pos (match-end 0))
  21063. (not (eq this-command last-command)))
  21064. (goto-char (match-end 0))
  21065. (goto-char (match-beginning 1))))
  21066. (call-interactively move-fun))))
  21067. ((org-element-property :hiddenp element)
  21068. ;; If element is hidden, `move-end-of-line' would put point
  21069. ;; after it. Use `end-of-line' to stay on current line.
  21070. (call-interactively 'end-of-line))
  21071. (t (call-interactively move-fun)))))
  21072. (org-no-warnings (and (featurep 'xemacs) (setq zmacs-region-stays t))))
  21073. (setq disable-point-adjustment
  21074. (or (not (invisible-p (point)))
  21075. (not (invisible-p (max (point-min) (1- (point))))))))
  21076. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  21077. (define-key org-mode-map "\C-e" 'org-end-of-line)
  21078. (defun org-backward-sentence (&optional arg)
  21079. "Go to beginning of sentence, or beginning of table field.
  21080. This will call `backward-sentence' or `org-table-beginning-of-field',
  21081. depending on context."
  21082. (interactive "P")
  21083. (cond
  21084. ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
  21085. (t (call-interactively 'backward-sentence))))
  21086. (defun org-forward-sentence (&optional arg)
  21087. "Go to end of sentence, or end of table field.
  21088. This will call `forward-sentence' or `org-table-end-of-field',
  21089. depending on context."
  21090. (interactive "P")
  21091. (cond
  21092. ((org-at-table-p) (call-interactively 'org-table-end-of-field))
  21093. (t (call-interactively 'forward-sentence))))
  21094. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  21095. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  21096. (defun org-kill-line (&optional arg)
  21097. "Kill line, to tags or end of line."
  21098. (interactive "P")
  21099. (cond
  21100. ((or (not org-special-ctrl-k)
  21101. (bolp)
  21102. (not (org-at-heading-p)))
  21103. (if (and (get-char-property (min (point-max) (point-at-eol)) 'invisible)
  21104. org-ctrl-k-protect-subtree)
  21105. (if (or (eq org-ctrl-k-protect-subtree 'error)
  21106. (not (y-or-n-p "Kill hidden subtree along with headline? ")))
  21107. (user-error "C-k aborted as it would kill a hidden subtree")))
  21108. (call-interactively
  21109. (if (org-bound-and-true-p visual-line-mode) 'kill-visual-line 'kill-line)))
  21110. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$"))
  21111. (kill-region (point) (match-beginning 1))
  21112. (org-set-tags nil t))
  21113. (t (kill-region (point) (point-at-eol)))))
  21114. (define-key org-mode-map "\C-k" 'org-kill-line)
  21115. (defun org-yank (&optional arg)
  21116. "Yank. If the kill is a subtree, treat it specially.
  21117. This command will look at the current kill and check if is a single
  21118. subtree, or a series of subtrees[1]. If it passes the test, and if the
  21119. cursor is at the beginning of a line or after the stars of a currently
  21120. empty headline, then the yank is handled specially. How exactly depends
  21121. on the value of the following variables, both set by default.
  21122. org-yank-folded-subtrees
  21123. When set, the subtree(s) will be folded after insertion, but only
  21124. if doing so would now swallow text after the yanked text.
  21125. org-yank-adjusted-subtrees
  21126. When set, the subtree will be promoted or demoted in order to
  21127. fit into the local outline tree structure, which means that the level
  21128. will be adjusted so that it becomes the smaller one of the two
  21129. *visible* surrounding headings.
  21130. Any prefix to this command will cause `yank' to be called directly with
  21131. no special treatment. In particular, a simple \\[universal-argument] prefix \
  21132. will just
  21133. plainly yank the text as it is.
  21134. \[1] The test checks if the first non-white line is a heading
  21135. and if there are no other headings with fewer stars."
  21136. (interactive "P")
  21137. (org-yank-generic 'yank arg))
  21138. (defun org-yank-generic (command arg)
  21139. "Perform some yank-like command.
  21140. This function implements the behavior described in the `org-yank'
  21141. documentation. However, it has been generalized to work for any
  21142. interactive command with similar behavior."
  21143. ;; pretend to be command COMMAND
  21144. (setq this-command command)
  21145. (if arg
  21146. (call-interactively command)
  21147. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  21148. (and (org-kill-is-subtree-p)
  21149. (or (bolp)
  21150. (and (looking-at "[ \t]*$")
  21151. (string-match
  21152. "\\`\\*+\\'"
  21153. (buffer-substring (point-at-bol) (point)))))))
  21154. swallowp)
  21155. (cond
  21156. ((and subtreep org-yank-folded-subtrees)
  21157. (let ((beg (point))
  21158. end)
  21159. (if (and subtreep org-yank-adjusted-subtrees)
  21160. (org-paste-subtree nil nil 'for-yank)
  21161. (call-interactively command))
  21162. (setq end (point))
  21163. (goto-char beg)
  21164. (when (and (bolp) subtreep
  21165. (not (setq swallowp
  21166. (org-yank-folding-would-swallow-text beg end))))
  21167. (org-with-limited-levels
  21168. (or (looking-at org-outline-regexp)
  21169. (re-search-forward org-outline-regexp-bol end t))
  21170. (while (and (< (point) end) (looking-at org-outline-regexp))
  21171. (hide-subtree)
  21172. (org-cycle-show-empty-lines 'folded)
  21173. (condition-case nil
  21174. (outline-forward-same-level 1)
  21175. (error (goto-char end))))))
  21176. (when swallowp
  21177. (message
  21178. "Inserted text not folded because that would swallow text"))
  21179. (goto-char end)
  21180. (skip-chars-forward " \t\n\r")
  21181. (beginning-of-line 1)
  21182. (push-mark beg 'nomsg)))
  21183. ((and subtreep org-yank-adjusted-subtrees)
  21184. (let ((beg (point-at-bol)))
  21185. (org-paste-subtree nil nil 'for-yank)
  21186. (push-mark beg 'nomsg)))
  21187. (t
  21188. (call-interactively command))))))
  21189. (defun org-yank-folding-would-swallow-text (beg end)
  21190. "Would hide-subtree at BEG swallow any text after END?"
  21191. (let (level)
  21192. (org-with-limited-levels
  21193. (save-excursion
  21194. (goto-char beg)
  21195. (when (or (looking-at org-outline-regexp)
  21196. (re-search-forward org-outline-regexp-bol end t))
  21197. (setq level (org-outline-level)))
  21198. (goto-char end)
  21199. (skip-chars-forward " \t\r\n\v\f")
  21200. (if (or (eobp)
  21201. (and (bolp) (looking-at org-outline-regexp)
  21202. (<= (org-outline-level) level)))
  21203. nil ; Nothing would be swallowed
  21204. t))))) ; something would swallow
  21205. (define-key org-mode-map "\C-y" 'org-yank)
  21206. (defun org-truely-invisible-p ()
  21207. "Check if point is at a character currently not visible.
  21208. This version does not only check the character property, but also
  21209. `visible-mode'."
  21210. ;; Early versions of noutline don't have `outline-invisible-p'.
  21211. (if (org-bound-and-true-p visible-mode)
  21212. nil
  21213. (outline-invisible-p)))
  21214. (defun org-invisible-p2 ()
  21215. "Check if point is at a character currently not visible."
  21216. (save-excursion
  21217. (if (and (eolp) (not (bobp))) (backward-char 1))
  21218. ;; Early versions of noutline don't have `outline-invisible-p'.
  21219. (outline-invisible-p)))
  21220. (defun org-back-to-heading (&optional invisible-ok)
  21221. "Call `outline-back-to-heading', but provide a better error message."
  21222. (condition-case nil
  21223. (outline-back-to-heading invisible-ok)
  21224. (error (error "Before first headline at position %d in buffer %s"
  21225. (point) (current-buffer)))))
  21226. (defun org-before-first-heading-p ()
  21227. "Before first heading?"
  21228. (save-excursion
  21229. (end-of-line)
  21230. (null (re-search-backward org-outline-regexp-bol nil t))))
  21231. (defun org-at-heading-p (&optional ignored)
  21232. (outline-on-heading-p t))
  21233. ;; Compatibility alias with Org versions < 7.8.03
  21234. (defalias 'org-on-heading-p 'org-at-heading-p)
  21235. (defun org-at-comment-p nil
  21236. "Is cursor in a line starting with a # character?"
  21237. (save-excursion
  21238. (beginning-of-line)
  21239. (looking-at "^#")))
  21240. (defun org-at-drawer-p nil
  21241. "Is cursor at a drawer keyword?"
  21242. (save-excursion
  21243. (move-beginning-of-line 1)
  21244. (looking-at org-drawer-regexp)))
  21245. (defun org-at-block-p nil
  21246. "Is cursor at a block keyword?"
  21247. (save-excursion
  21248. (move-beginning-of-line 1)
  21249. (looking-at org-block-regexp)))
  21250. (defun org-point-at-end-of-empty-headline ()
  21251. "If point is at the end of an empty headline, return t, else nil.
  21252. If the heading only contains a TODO keyword, it is still still considered
  21253. empty."
  21254. (and (looking-at "[ \t]*$")
  21255. (when org-todo-line-regexp
  21256. (save-excursion
  21257. (beginning-of-line 1)
  21258. (let ((case-fold-search nil))
  21259. (looking-at org-todo-line-regexp)
  21260. (string= (match-string 3) ""))))))
  21261. (defun org-at-heading-or-item-p ()
  21262. (or (org-at-heading-p) (org-at-item-p)))
  21263. (defun org-at-target-p ()
  21264. (or (org-in-regexp org-radio-target-regexp)
  21265. (org-in-regexp org-target-regexp)))
  21266. ;; Compatibility alias with Org versions < 7.8.03
  21267. (defalias 'org-on-target-p 'org-at-target-p)
  21268. (defun org-up-heading-all (arg)
  21269. "Move to the heading line of which the present line is a subheading.
  21270. This function considers both visible and invisible heading lines.
  21271. With argument, move up ARG levels."
  21272. (if (fboundp 'outline-up-heading-all)
  21273. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  21274. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  21275. (defun org-up-heading-safe ()
  21276. "Move to the heading line of which the present line is a subheading.
  21277. This version will not throw an error. It will return the level of the
  21278. headline found, or nil if no higher level is found.
  21279. Also, this function will be a lot faster than `outline-up-heading',
  21280. because it relies on stars being the outline starters. This can really
  21281. make a significant difference in outlines with very many siblings."
  21282. (let (start-level re)
  21283. (org-back-to-heading t)
  21284. (setq start-level (funcall outline-level))
  21285. (if (equal start-level 1)
  21286. nil
  21287. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  21288. (if (re-search-backward re nil t)
  21289. (funcall outline-level)))))
  21290. (defun org-first-sibling-p ()
  21291. "Is this heading the first child of its parents?"
  21292. (interactive)
  21293. (let ((re org-outline-regexp-bol)
  21294. level l)
  21295. (unless (org-at-heading-p t)
  21296. (user-error "Not at a heading"))
  21297. (setq level (funcall outline-level))
  21298. (save-excursion
  21299. (if (not (re-search-backward re nil t))
  21300. t
  21301. (setq l (funcall outline-level))
  21302. (< l level)))))
  21303. (defun org-goto-sibling (&optional previous)
  21304. "Goto the next sibling, even if it is invisible.
  21305. When PREVIOUS is set, go to the previous sibling instead. Returns t
  21306. when a sibling was found. When none is found, return nil and don't
  21307. move point."
  21308. (let ((fun (if previous 're-search-backward 're-search-forward))
  21309. (pos (point))
  21310. (re org-outline-regexp-bol)
  21311. level l)
  21312. (when (condition-case nil (org-back-to-heading t) (error nil))
  21313. (setq level (funcall outline-level))
  21314. (catch 'exit
  21315. (or previous (forward-char 1))
  21316. (while (funcall fun re nil t)
  21317. (setq l (funcall outline-level))
  21318. (when (< l level) (goto-char pos) (throw 'exit nil))
  21319. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  21320. (goto-char pos)
  21321. nil))))
  21322. (defun org-show-siblings ()
  21323. "Show all siblings of the current headline."
  21324. (save-excursion
  21325. (while (org-goto-sibling) (org-flag-heading nil)))
  21326. (save-excursion
  21327. (while (org-goto-sibling 'previous)
  21328. (org-flag-heading nil))))
  21329. (defun org-goto-first-child ()
  21330. "Goto the first child, even if it is invisible.
  21331. Return t when a child was found. Otherwise don't move point and
  21332. return nil."
  21333. (let (level (pos (point)) (re org-outline-regexp-bol))
  21334. (when (condition-case nil (org-back-to-heading t) (error nil))
  21335. (setq level (outline-level))
  21336. (forward-char 1)
  21337. (if (and (re-search-forward re nil t) (> (outline-level) level))
  21338. (progn (goto-char (match-beginning 0)) t)
  21339. (goto-char pos) nil))))
  21340. (defun org-show-hidden-entry ()
  21341. "Show an entry where even the heading is hidden."
  21342. (save-excursion
  21343. (org-show-entry)))
  21344. (defun org-flag-heading (flag &optional entry)
  21345. "Flag the current heading. FLAG non-nil means make invisible.
  21346. When ENTRY is non-nil, show the entire entry."
  21347. (save-excursion
  21348. (org-back-to-heading t)
  21349. ;; Check if we should show the entire entry
  21350. (if entry
  21351. (progn
  21352. (org-show-entry)
  21353. (save-excursion
  21354. (and (outline-next-heading)
  21355. (org-flag-heading nil))))
  21356. (outline-flag-region (max (point-min) (1- (point)))
  21357. (save-excursion (outline-end-of-heading) (point))
  21358. flag))))
  21359. (defun org-get-next-sibling ()
  21360. "Move to next heading of the same level, and return point.
  21361. If there is no such heading, return nil.
  21362. This is like outline-next-sibling, but invisible headings are ok."
  21363. (let ((level (funcall outline-level)))
  21364. (outline-next-heading)
  21365. (while (and (not (eobp)) (> (funcall outline-level) level))
  21366. (outline-next-heading))
  21367. (if (or (eobp) (< (funcall outline-level) level))
  21368. nil
  21369. (point))))
  21370. (defun org-get-last-sibling ()
  21371. "Move to previous heading of the same level, and return point.
  21372. If there is no such heading, return nil."
  21373. (let ((opoint (point))
  21374. (level (funcall outline-level)))
  21375. (outline-previous-heading)
  21376. (when (and (/= (point) opoint) (outline-on-heading-p t))
  21377. (while (and (> (funcall outline-level) level)
  21378. (not (bobp)))
  21379. (outline-previous-heading))
  21380. (if (< (funcall outline-level) level)
  21381. nil
  21382. (point)))))
  21383. (defun org-end-of-subtree (&optional invisible-ok to-heading)
  21384. "Goto to the end of a subtree."
  21385. ;; This contains an exact copy of the original function, but it uses
  21386. ;; `org-back-to-heading', to make it work also in invisible
  21387. ;; trees. And is uses an invisible-ok argument.
  21388. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  21389. ;; Furthermore, when used inside Org, finding the end of a large subtree
  21390. ;; with many children and grandchildren etc, this can be much faster
  21391. ;; than the outline version.
  21392. (org-back-to-heading invisible-ok)
  21393. (let ((first t)
  21394. (level (funcall outline-level)))
  21395. (if (and (derived-mode-p 'org-mode) (< level 1000))
  21396. ;; A true heading (not a plain list item), in Org-mode
  21397. ;; This means we can easily find the end by looking
  21398. ;; only for the right number of stars. Using a regexp to do
  21399. ;; this is so much faster than using a Lisp loop.
  21400. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  21401. (forward-char 1)
  21402. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  21403. ;; something else, do it the slow way
  21404. (while (and (not (eobp))
  21405. (or first (> (funcall outline-level) level)))
  21406. (setq first nil)
  21407. (outline-next-heading)))
  21408. (unless to-heading
  21409. (if (memq (preceding-char) '(?\n ?\^M))
  21410. (progn
  21411. ;; Go to end of line before heading
  21412. (forward-char -1)
  21413. (if (memq (preceding-char) '(?\n ?\^M))
  21414. ;; leave blank line before heading
  21415. (forward-char -1))))))
  21416. (point))
  21417. (defun org-end-of-meta-data-and-drawers ()
  21418. "Jump to the first text after meta data and drawers in the current entry.
  21419. This will move over empty lines, lines with planning time stamps,
  21420. clocking lines, and drawers."
  21421. (org-back-to-heading t)
  21422. (let ((end (save-excursion (outline-next-heading) (point)))
  21423. (re (concat "\\(" org-drawer-regexp "\\)"
  21424. "\\|" "[ \t]*" org-keyword-time-regexp)))
  21425. (forward-line 1)
  21426. (while (re-search-forward re end t)
  21427. (if (not (match-end 1))
  21428. ;; empty or planning line
  21429. (forward-line 1)
  21430. ;; a drawer, find the end
  21431. (re-search-forward "^[ \t]*:END:" end 'move)
  21432. (forward-line 1)))
  21433. (and (re-search-forward "[^\n]" nil t) (backward-char 1))
  21434. (point)))
  21435. (defun org-forward-heading-same-level (arg &optional invisible-ok)
  21436. "Move forward to the ARG'th subheading at same level as this one.
  21437. Stop at the first and last subheadings of a superior heading.
  21438. Normally this only looks at visible headings, but when INVISIBLE-OK is
  21439. non-nil it will also look at invisible ones."
  21440. (interactive "p")
  21441. (if (not (ignore-errors (org-back-to-heading invisible-ok)))
  21442. (if (and arg (< arg 0))
  21443. (goto-char (point-min))
  21444. (outline-next-heading))
  21445. (org-at-heading-p)
  21446. (let ((level (- (match-end 0) (match-beginning 0) 1))
  21447. (f (if (and arg (< arg 0))
  21448. 're-search-backward
  21449. 're-search-forward))
  21450. (count (if arg (abs arg) 1))
  21451. (result (point)))
  21452. (while (and (prog1 (> count 0)
  21453. (forward-char (if (and arg (< arg 0)) -1 1)))
  21454. (funcall f org-outline-regexp-bol nil 'move))
  21455. (let ((l (- (match-end 0) (match-beginning 0) 1)))
  21456. (cond ((< l level) (setq count 0))
  21457. ((and (= l level)
  21458. (or invisible-ok
  21459. (progn
  21460. (goto-char (line-beginning-position))
  21461. (not (outline-invisible-p)))))
  21462. (setq count (1- count))
  21463. (when (eq l level)
  21464. (setq result (point)))))))
  21465. (goto-char result))
  21466. (beginning-of-line 1)))
  21467. (defun org-backward-heading-same-level (arg &optional invisible-ok)
  21468. "Move backward to the ARG'th subheading at same level as this one.
  21469. Stop at the first and last subheadings of a superior heading."
  21470. (interactive "p")
  21471. (org-forward-heading-same-level (if arg (- arg) -1) invisible-ok))
  21472. (defun org-next-block (arg &optional backward block-regexp)
  21473. "Jump to the next block.
  21474. With a prefix argument ARG, jump forward ARG many source blocks.
  21475. When BACKWARD is non-nil, jump to the previous block.
  21476. When BLOCK-REGEXP is non-nil, use this regexp to find blocks."
  21477. (interactive "p")
  21478. (let ((re (or block-regexp org-block-regexp))
  21479. (re-search-fn (or (and backward 're-search-backward)
  21480. 're-search-forward)))
  21481. (if (looking-at re) (forward-char 1))
  21482. (condition-case nil
  21483. (funcall re-search-fn re nil nil arg)
  21484. (error (error "No %s code blocks" (if backward "previous" "further" ))))
  21485. (goto-char (match-beginning 0)) (org-show-context)))
  21486. (defun org-previous-block (arg &optional block-regexp)
  21487. "Jump to the previous block.
  21488. With a prefix argument ARG, jump backward ARG many source blocks.
  21489. When BLOCK-REGEXP is non-nil, use this regexp to find blocks."
  21490. (interactive "p")
  21491. (org-next-block arg t block-regexp))
  21492. (defun org-forward-paragraph ()
  21493. "Move forward to beginning of next paragraph or equivalent.
  21494. The function moves point to the beginning of the next visible
  21495. structural element, which can be a paragraph, a table, a list
  21496. item, etc. It also provides some special moves for convenience:
  21497. - On an affiliated keyword, jump to the beginning of the
  21498. relative element.
  21499. - On an item or a footnote definition, move to the second
  21500. element inside, if any.
  21501. - On a table or a property drawer, jump after it.
  21502. - On a verse or source block, stop after blank lines."
  21503. (interactive)
  21504. (when (eobp) (user-error "Cannot move further down"))
  21505. (let* ((deactivate-mark nil)
  21506. (element (org-element-at-point))
  21507. (type (org-element-type element))
  21508. (post-affiliated (org-element-property :post-affiliated element))
  21509. (contents-begin (org-element-property :contents-begin element))
  21510. (contents-end (org-element-property :contents-end element))
  21511. (end (let ((end (org-element-property :end element)) (parent element))
  21512. (while (and (setq parent (org-element-property :parent parent))
  21513. (= (org-element-property :contents-end parent) end))
  21514. (setq end (org-element-property :end parent)))
  21515. end)))
  21516. (cond ((not element)
  21517. (skip-chars-forward " \r\t\n")
  21518. (or (eobp) (beginning-of-line)))
  21519. ;; On affiliated keywords, move to element's beginning.
  21520. ((and post-affiliated (< (point) post-affiliated))
  21521. (goto-char post-affiliated))
  21522. ;; At a table row, move to the end of the table. Similarly,
  21523. ;; at a node property, move to the end of the property
  21524. ;; drawer.
  21525. ((memq type '(node-property table-row))
  21526. (goto-char (org-element-property
  21527. :end (org-element-property :parent element))))
  21528. ((memq type '(property-drawer table)) (goto-char end))
  21529. ;; Consider blank lines as separators in verse and source
  21530. ;; blocks to ease editing.
  21531. ((memq type '(src-block verse-block))
  21532. (when (eq type 'src-block)
  21533. (setq contents-end
  21534. (save-excursion (goto-char end)
  21535. (skip-chars-backward " \r\t\n")
  21536. (line-beginning-position))))
  21537. (beginning-of-line)
  21538. (when (looking-at "[ \t]*$") (skip-chars-forward " \r\t\n"))
  21539. (if (not (re-search-forward "^[ \t]*$" contents-end t))
  21540. (goto-char end)
  21541. (skip-chars-forward " \r\t\n")
  21542. (if (= (point) contents-end) (goto-char end)
  21543. (beginning-of-line))))
  21544. ;; With no contents, just skip element.
  21545. ((not contents-begin) (goto-char end))
  21546. ;; If contents are invisible, skip the element altogether.
  21547. ((outline-invisible-p (line-end-position))
  21548. (case type
  21549. (headline
  21550. (org-with-limited-levels (outline-next-visible-heading 1)))
  21551. ;; At a plain list, make sure we move to the next item
  21552. ;; instead of skipping the whole list.
  21553. (plain-list (forward-char)
  21554. (org-forward-paragraph))
  21555. (otherwise (goto-char end))))
  21556. ((>= (point) contents-end) (goto-char end))
  21557. ((>= (point) contents-begin)
  21558. ;; This can only happen on paragraphs and plain lists.
  21559. (case type
  21560. (paragraph (goto-char end))
  21561. ;; At a plain list, try to move to second element in
  21562. ;; first item, if possible.
  21563. (plain-list (end-of-line)
  21564. (org-forward-paragraph))))
  21565. ;; When contents start on the middle of a line (e.g. in
  21566. ;; items and footnote definitions), try to reach first
  21567. ;; element starting after current line.
  21568. ((> (line-end-position) contents-begin)
  21569. (end-of-line)
  21570. (org-forward-paragraph))
  21571. (t (goto-char contents-begin)))))
  21572. (defun org-backward-paragraph ()
  21573. "Move backward to start of previous paragraph or equivalent.
  21574. The function moves point to the beginning of the current
  21575. structural element, which can be a paragraph, a table, a list
  21576. item, etc., or to the beginning of the previous visible one if
  21577. point is already there. It also provides some special moves for
  21578. convenience:
  21579. - On an affiliated keyword, jump to the first one.
  21580. - On a table or a property drawer, move to its beginning.
  21581. - On a verse or source block, stop before blank lines."
  21582. (interactive)
  21583. (when (bobp) (user-error "Cannot move further up"))
  21584. (let* ((deactivate-mark nil)
  21585. (element (org-element-at-point))
  21586. (type (org-element-type element))
  21587. (contents-begin (org-element-property :contents-begin element))
  21588. (contents-end (org-element-property :contents-end element))
  21589. (post-affiliated (org-element-property :post-affiliated element))
  21590. (begin (org-element-property :begin element)))
  21591. (cond
  21592. ((not element) (goto-char (point-min)))
  21593. ((= (point) begin)
  21594. (backward-char)
  21595. (org-backward-paragraph))
  21596. ((and post-affiliated (<= (point) post-affiliated)) (goto-char begin))
  21597. ((memq type '(node-property table-row))
  21598. (goto-char (org-element-property
  21599. :post-affiliated (org-element-property :parent element))))
  21600. ((memq type '(property-drawer table)) (goto-char begin))
  21601. ((memq type '(src-block verse-block))
  21602. (when (eq type 'src-block)
  21603. (setq contents-begin
  21604. (save-excursion (goto-char begin) (forward-line) (point))))
  21605. (if (= (point) contents-begin) (goto-char post-affiliated)
  21606. ;; Inside a verse block, see blank lines as paragraph
  21607. ;; separators.
  21608. (let ((origin (point)))
  21609. (skip-chars-backward " \r\t\n" contents-begin)
  21610. (when (re-search-backward "^[ \t]*$" contents-begin 'move)
  21611. (skip-chars-forward " \r\t\n" origin)
  21612. (if (= (point) origin) (goto-char contents-begin)
  21613. (beginning-of-line))))))
  21614. ((not contents-begin) (goto-char (or post-affiliated begin)))
  21615. ((eq type 'paragraph)
  21616. (goto-char contents-begin)
  21617. ;; When at first paragraph in an item or a footnote definition,
  21618. ;; move directly to beginning of line.
  21619. (let ((parent-contents
  21620. (org-element-property
  21621. :contents-begin (org-element-property :parent element))))
  21622. (when (and parent-contents (= parent-contents contents-begin))
  21623. (beginning-of-line))))
  21624. ;; At the end of a greater element, move to the beginning of the
  21625. ;; last element within.
  21626. ((>= (point) contents-end)
  21627. (goto-char (1- contents-end))
  21628. (org-backward-paragraph))
  21629. (t (goto-char (or post-affiliated begin))))
  21630. ;; Ensure we never leave point invisible.
  21631. (when (outline-invisible-p (point)) (beginning-of-visual-line))))
  21632. (defun org-forward-element ()
  21633. "Move forward by one element.
  21634. Move to the next element at the same level, when possible."
  21635. (interactive)
  21636. (cond ((eobp) (user-error "Cannot move further down"))
  21637. ((org-with-limited-levels (org-at-heading-p))
  21638. (let ((origin (point)))
  21639. (goto-char (org-end-of-subtree nil t))
  21640. (unless (org-with-limited-levels (org-at-heading-p))
  21641. (goto-char origin)
  21642. (user-error "Cannot move further down"))))
  21643. (t
  21644. (let* ((elem (org-element-at-point))
  21645. (end (org-element-property :end elem))
  21646. (parent (org-element-property :parent elem)))
  21647. (cond ((and parent (= (org-element-property :contents-end parent) end))
  21648. (goto-char (org-element-property :end parent)))
  21649. ((integer-or-marker-p end) (goto-char end))
  21650. (t (message "No element at point")))))))
  21651. (defun org-backward-element ()
  21652. "Move backward by one element.
  21653. Move to the previous element at the same level, when possible."
  21654. (interactive)
  21655. (cond ((bobp) (user-error "Cannot move further up"))
  21656. ((org-with-limited-levels (org-at-heading-p))
  21657. ;; At a headline, move to the previous one, if any, or stay
  21658. ;; here.
  21659. (let ((origin (point)))
  21660. (org-with-limited-levels (org-backward-heading-same-level 1))
  21661. ;; When current headline has no sibling above, move to its
  21662. ;; parent.
  21663. (when (= (point) origin)
  21664. (or (org-with-limited-levels (org-up-heading-safe))
  21665. (progn (goto-char origin)
  21666. (user-error "Cannot move further up"))))))
  21667. (t
  21668. (let* ((trail (org-element-at-point 'keep-trail))
  21669. (elem (car trail))
  21670. (prev-elem (nth 1 trail))
  21671. (beg (org-element-property :begin elem)))
  21672. (cond
  21673. ;; Move to beginning of current element if point isn't
  21674. ;; there already.
  21675. ((null beg) (message "No element at point"))
  21676. ((/= (point) beg) (goto-char beg))
  21677. (prev-elem (goto-char (org-element-property :begin prev-elem)))
  21678. ((org-before-first-heading-p) (goto-char (point-min)))
  21679. (t (org-back-to-heading)))))))
  21680. (defun org-up-element ()
  21681. "Move to upper element."
  21682. (interactive)
  21683. (if (org-with-limited-levels (org-at-heading-p))
  21684. (unless (org-up-heading-safe) (user-error "No surrounding element"))
  21685. (let* ((elem (org-element-at-point))
  21686. (parent (org-element-property :parent elem)))
  21687. (if parent (goto-char (org-element-property :begin parent))
  21688. (if (org-with-limited-levels (org-before-first-heading-p))
  21689. (user-error "No surrounding element")
  21690. (org-with-limited-levels (org-back-to-heading)))))))
  21691. (defvar org-element-greater-elements)
  21692. (defun org-down-element ()
  21693. "Move to inner element."
  21694. (interactive)
  21695. (let ((element (org-element-at-point)))
  21696. (cond
  21697. ((memq (org-element-type element) '(plain-list table))
  21698. (goto-char (org-element-property :contents-begin element))
  21699. (forward-char))
  21700. ((memq (org-element-type element) org-element-greater-elements)
  21701. ;; If contents are hidden, first disclose them.
  21702. (when (org-element-property :hiddenp element) (org-cycle))
  21703. (goto-char (or (org-element-property :contents-begin element)
  21704. (user-error "No content for this element"))))
  21705. (t (user-error "No inner element")))))
  21706. (defun org-drag-element-backward ()
  21707. "Move backward element at point."
  21708. (interactive)
  21709. (if (org-with-limited-levels (org-at-heading-p)) (org-move-subtree-up)
  21710. (let* ((trail (org-element-at-point 'keep-trail))
  21711. (elem (car trail))
  21712. (prev-elem (nth 1 trail)))
  21713. ;; Error out if no previous element or previous element is
  21714. ;; a parent of the current one.
  21715. (if (or (not prev-elem) (org-element-nested-p elem prev-elem))
  21716. (user-error "Cannot drag element backward")
  21717. (let ((pos (point)))
  21718. (org-element-swap-A-B prev-elem elem)
  21719. (goto-char (+ (org-element-property :begin prev-elem)
  21720. (- pos (org-element-property :begin elem)))))))))
  21721. (defun org-drag-element-forward ()
  21722. "Move forward element at point."
  21723. (interactive)
  21724. (let* ((pos (point))
  21725. (elem (org-element-at-point)))
  21726. (when (= (point-max) (org-element-property :end elem))
  21727. (user-error "Cannot drag element forward"))
  21728. (goto-char (org-element-property :end elem))
  21729. (let ((next-elem (org-element-at-point)))
  21730. (when (or (org-element-nested-p elem next-elem)
  21731. (and (eq (org-element-type next-elem) 'headline)
  21732. (not (eq (org-element-type elem) 'headline))))
  21733. (goto-char pos)
  21734. (user-error "Cannot drag element forward"))
  21735. ;; Compute new position of point: it's shifted by NEXT-ELEM
  21736. ;; body's length (without final blanks) and by the length of
  21737. ;; blanks between ELEM and NEXT-ELEM.
  21738. (let ((size-next (- (save-excursion
  21739. (goto-char (org-element-property :end next-elem))
  21740. (skip-chars-backward " \r\t\n")
  21741. (forward-line)
  21742. ;; Small correction if buffer doesn't end
  21743. ;; with a newline character.
  21744. (if (and (eolp) (not (bolp))) (1+ (point)) (point)))
  21745. (org-element-property :begin next-elem)))
  21746. (size-blank (- (org-element-property :end elem)
  21747. (save-excursion
  21748. (goto-char (org-element-property :end elem))
  21749. (skip-chars-backward " \r\t\n")
  21750. (forward-line)
  21751. (point)))))
  21752. (org-element-swap-A-B elem next-elem)
  21753. (goto-char (+ pos size-next size-blank))))))
  21754. (defun org-drag-line-forward (arg)
  21755. "Drag the line at point ARG lines forward."
  21756. (interactive "p")
  21757. (dotimes (n (abs arg))
  21758. (let ((c (current-column)))
  21759. (if (< 0 arg)
  21760. (progn
  21761. (beginning-of-line 2)
  21762. (transpose-lines 1)
  21763. (beginning-of-line 0))
  21764. (transpose-lines 1)
  21765. (beginning-of-line -1))
  21766. (org-move-to-column c))))
  21767. (defun org-drag-line-backward (arg)
  21768. "Drag the line at point ARG lines backward."
  21769. (interactive "p")
  21770. (org-drag-line-forward (- arg)))
  21771. (defun org-mark-element ()
  21772. "Put point at beginning of this element, mark at end.
  21773. Interactively, if this command is repeated or (in Transient Mark
  21774. mode) if the mark is active, it marks the next element after the
  21775. ones already marked."
  21776. (interactive)
  21777. (let (deactivate-mark)
  21778. (if (and (org-called-interactively-p 'any)
  21779. (or (and (eq last-command this-command) (mark t))
  21780. (and transient-mark-mode mark-active)))
  21781. (set-mark
  21782. (save-excursion
  21783. (goto-char (mark))
  21784. (goto-char (org-element-property :end (org-element-at-point)))))
  21785. (let ((element (org-element-at-point)))
  21786. (end-of-line)
  21787. (push-mark (org-element-property :end element) t t)
  21788. (goto-char (org-element-property :begin element))))))
  21789. (defun org-narrow-to-element ()
  21790. "Narrow buffer to current element."
  21791. (interactive)
  21792. (let ((elem (org-element-at-point)))
  21793. (cond
  21794. ((eq (car elem) 'headline)
  21795. (narrow-to-region
  21796. (org-element-property :begin elem)
  21797. (org-element-property :end elem)))
  21798. ((memq (car elem) org-element-greater-elements)
  21799. (narrow-to-region
  21800. (org-element-property :contents-begin elem)
  21801. (org-element-property :contents-end elem)))
  21802. (t
  21803. (narrow-to-region
  21804. (org-element-property :begin elem)
  21805. (org-element-property :end elem))))))
  21806. (defun org-transpose-element ()
  21807. "Transpose current and previous elements, keeping blank lines between.
  21808. Point is moved after both elements."
  21809. (interactive)
  21810. (org-skip-whitespace)
  21811. (let ((end (org-element-property :end (org-element-at-point))))
  21812. (org-drag-element-backward)
  21813. (goto-char end)))
  21814. (defun org-unindent-buffer ()
  21815. "Un-indent the visible part of the buffer.
  21816. Relative indentation (between items, inside blocks, etc.) isn't
  21817. modified."
  21818. (interactive)
  21819. (unless (eq major-mode 'org-mode)
  21820. (user-error "Cannot un-indent a buffer not in Org mode"))
  21821. (let* ((parse-tree (org-element-parse-buffer 'greater-element))
  21822. unindent-tree ; For byte-compiler.
  21823. (unindent-tree
  21824. (function
  21825. (lambda (contents)
  21826. (mapc
  21827. (lambda (element)
  21828. (if (memq (org-element-type element) '(headline section))
  21829. (funcall unindent-tree (org-element-contents element))
  21830. (save-excursion
  21831. (save-restriction
  21832. (narrow-to-region
  21833. (org-element-property :begin element)
  21834. (org-element-property :end element))
  21835. (org-do-remove-indentation)))))
  21836. (reverse contents))))))
  21837. (funcall unindent-tree (org-element-contents parse-tree))))
  21838. (defun org-show-subtree ()
  21839. "Show everything after this heading at deeper levels."
  21840. (interactive)
  21841. (outline-flag-region
  21842. (point)
  21843. (save-excursion
  21844. (org-end-of-subtree t t))
  21845. nil))
  21846. (defun org-show-entry ()
  21847. "Show the body directly following this heading.
  21848. Show the heading too, if it is currently invisible."
  21849. (interactive)
  21850. (save-excursion
  21851. (condition-case nil
  21852. (progn
  21853. (org-back-to-heading t)
  21854. (outline-flag-region
  21855. (max (point-min) (1- (point)))
  21856. (save-excursion
  21857. (if (re-search-forward
  21858. (concat "[\r\n]\\(" org-outline-regexp "\\)") nil t)
  21859. (match-beginning 1)
  21860. (point-max)))
  21861. nil)
  21862. (org-cycle-hide-drawers 'children))
  21863. (error nil))))
  21864. (defun org-make-options-regexp (kwds &optional extra)
  21865. "Make a regular expression for keyword lines."
  21866. (concat
  21867. "^#\\+\\("
  21868. (mapconcat 'regexp-quote kwds "\\|")
  21869. (if extra (concat "\\|" extra))
  21870. "\\):[ \t]*\\(.*\\)"))
  21871. ;; Make isearch reveal the necessary context
  21872. (defun org-isearch-end ()
  21873. "Reveal context after isearch exits."
  21874. (when isearch-success ; only if search was successful
  21875. (if (featurep 'xemacs)
  21876. ;; Under XEmacs, the hook is run in the correct place,
  21877. ;; we directly show the context.
  21878. (org-show-context 'isearch)
  21879. ;; In Emacs the hook runs *before* restoring the overlays.
  21880. ;; So we have to use a one-time post-command-hook to do this.
  21881. ;; (Emacs 22 has a special variable, see function `org-mode')
  21882. (unless (and (boundp 'isearch-mode-end-hook-quit)
  21883. isearch-mode-end-hook-quit)
  21884. ;; Only when the isearch was not quitted.
  21885. (org-add-hook 'post-command-hook 'org-isearch-post-command
  21886. 'append 'local)))))
  21887. (defun org-isearch-post-command ()
  21888. "Remove self from hook, and show context."
  21889. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  21890. (org-show-context 'isearch))
  21891. ;;;; Integration with and fixes for other packages
  21892. ;;; Imenu support
  21893. (defvar org-imenu-markers nil
  21894. "All markers currently used by Imenu.")
  21895. (make-variable-buffer-local 'org-imenu-markers)
  21896. (defun org-imenu-new-marker (&optional pos)
  21897. "Return a new marker for use by Imenu, and remember the marker."
  21898. (let ((m (make-marker)))
  21899. (move-marker m (or pos (point)))
  21900. (push m org-imenu-markers)
  21901. m))
  21902. (defun org-imenu-get-tree ()
  21903. "Produce the index for Imenu."
  21904. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  21905. (setq org-imenu-markers nil)
  21906. (let* ((n org-imenu-depth)
  21907. (re (concat "^" (org-get-limited-outline-regexp)))
  21908. (subs (make-vector (1+ n) nil))
  21909. (last-level 0)
  21910. m level head0 head)
  21911. (save-excursion
  21912. (save-restriction
  21913. (widen)
  21914. (goto-char (point-max))
  21915. (while (re-search-backward re nil t)
  21916. (setq level (org-reduced-level (funcall outline-level)))
  21917. (when (and (<= level n)
  21918. (looking-at org-complex-heading-regexp)
  21919. (setq head0 (org-match-string-no-properties 4)))
  21920. (setq head (org-link-display-format head0)
  21921. m (org-imenu-new-marker))
  21922. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  21923. (if (>= level last-level)
  21924. (push (cons head m) (aref subs level))
  21925. (push (cons head (aref subs (1+ level))) (aref subs level))
  21926. (loop for i from (1+ level) to n do (aset subs i nil)))
  21927. (setq last-level level)))))
  21928. (aref subs 1)))
  21929. (eval-after-load "imenu"
  21930. '(progn
  21931. (add-hook 'imenu-after-jump-hook
  21932. (lambda ()
  21933. (if (derived-mode-p 'org-mode)
  21934. (org-show-context 'org-goto))))))
  21935. (defun org-link-display-format (link)
  21936. "Replace a link with its the description.
  21937. If there is no description, use the link target."
  21938. (save-match-data
  21939. (if (string-match org-bracket-link-analytic-regexp link)
  21940. (replace-match (if (match-end 5)
  21941. (match-string 5 link)
  21942. (concat (match-string 1 link)
  21943. (match-string 3 link)))
  21944. nil t link)
  21945. link)))
  21946. (defun org-toggle-link-display ()
  21947. "Toggle the literal or descriptive display of links."
  21948. (interactive)
  21949. (if org-descriptive-links
  21950. (progn (org-remove-from-invisibility-spec '(org-link))
  21951. (org-restart-font-lock)
  21952. (setq org-descriptive-links nil))
  21953. (progn (add-to-invisibility-spec '(org-link))
  21954. (org-restart-font-lock)
  21955. (setq org-descriptive-links t))))
  21956. ;; Speedbar support
  21957. (defvar org-speedbar-restriction-lock-overlay (make-overlay 1 1)
  21958. "Overlay marking the agenda restriction line in speedbar.")
  21959. (overlay-put org-speedbar-restriction-lock-overlay
  21960. 'face 'org-agenda-restriction-lock)
  21961. (overlay-put org-speedbar-restriction-lock-overlay
  21962. 'help-echo "Agendas are currently limited to this item.")
  21963. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  21964. (defun org-speedbar-set-agenda-restriction ()
  21965. "Restrict future agenda commands to the location at point in speedbar.
  21966. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  21967. (interactive)
  21968. (require 'org-agenda)
  21969. (let (p m tp np dir txt)
  21970. (cond
  21971. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  21972. 'org-imenu t))
  21973. (setq m (get-text-property p 'org-imenu-marker))
  21974. (with-current-buffer (marker-buffer m)
  21975. (goto-char m)
  21976. (org-agenda-set-restriction-lock 'subtree)))
  21977. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  21978. 'speedbar-function 'speedbar-find-file))
  21979. (setq tp (previous-single-property-change
  21980. (1+ p) 'speedbar-function)
  21981. np (next-single-property-change
  21982. tp 'speedbar-function)
  21983. dir (speedbar-line-directory)
  21984. txt (buffer-substring-no-properties (or tp (point-min))
  21985. (or np (point-max))))
  21986. (with-current-buffer (find-file-noselect
  21987. (let ((default-directory dir))
  21988. (expand-file-name txt)))
  21989. (unless (derived-mode-p 'org-mode)
  21990. (user-error "Cannot restrict to non-Org-mode file"))
  21991. (org-agenda-set-restriction-lock 'file)))
  21992. (t (user-error "Don't know how to restrict Org-mode's agenda")))
  21993. (move-overlay org-speedbar-restriction-lock-overlay
  21994. (point-at-bol) (point-at-eol))
  21995. (setq current-prefix-arg nil)
  21996. (org-agenda-maybe-redo)))
  21997. (defvar speedbar-file-key-map)
  21998. (declare-function speedbar-add-supported-extension "speedbar" (extension))
  21999. (eval-after-load "speedbar"
  22000. '(progn
  22001. (speedbar-add-supported-extension ".org")
  22002. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  22003. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  22004. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  22005. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  22006. (add-hook 'speedbar-visiting-tag-hook
  22007. (lambda () (and (derived-mode-p 'org-mode) (org-show-context 'org-goto))))))
  22008. ;;; Fixes and Hacks for problems with other packages
  22009. ;; Make flyspell not check words in links, to not mess up our keymap
  22010. (defvar org-element-affiliated-keywords) ; From org-element.el
  22011. (defvar org-element-block-name-alist) ; From org-element.el
  22012. (defun org-mode-flyspell-verify ()
  22013. "Don't let flyspell put overlays at active buttons, or on
  22014. {todo,all-time,additional-option-like}-keywords."
  22015. (require 'org-element) ; For `org-element-affiliated-keywords'
  22016. (let ((pos (max (1- (point)) (point-min)))
  22017. (word (thing-at-point 'word)))
  22018. (and (not (get-text-property pos 'keymap))
  22019. (not (get-text-property pos 'org-no-flyspell))
  22020. (not (member word org-todo-keywords-1))
  22021. (not (member word org-all-time-keywords))
  22022. (not (member word org-options-keywords))
  22023. (not (member word (mapcar 'car org-startup-options)))
  22024. (not (member-ignore-case word org-element-affiliated-keywords))
  22025. (not (member-ignore-case word (org-get-export-keywords)))
  22026. (not (member-ignore-case
  22027. word (mapcar 'car org-element-block-name-alist)))
  22028. (not (member-ignore-case word '("BEGIN" "END" "ATTR")))
  22029. (not (org-in-src-block-p)))))
  22030. (defun org-remove-flyspell-overlays-in (beg end)
  22031. "Remove flyspell overlays in region."
  22032. (and (org-bound-and-true-p flyspell-mode)
  22033. (fboundp 'flyspell-delete-region-overlays)
  22034. (flyspell-delete-region-overlays beg end))
  22035. (add-text-properties beg end '(org-no-flyspell t)))
  22036. ;; Make `bookmark-jump' shows the jump location if it was hidden.
  22037. (eval-after-load "bookmark"
  22038. '(if (boundp 'bookmark-after-jump-hook)
  22039. ;; We can use the hook
  22040. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  22041. ;; Hook not available, use advice
  22042. (defadvice bookmark-jump (after org-make-visible activate)
  22043. "Make the position visible."
  22044. (org-bookmark-jump-unhide))))
  22045. ;; Make sure saveplace shows the location if it was hidden
  22046. (eval-after-load "saveplace"
  22047. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  22048. "Make the position visible."
  22049. (org-bookmark-jump-unhide)))
  22050. ;; Make sure ecb shows the location if it was hidden
  22051. (eval-after-load "ecb"
  22052. '(defadvice ecb-method-clicked (after esf/org-show-context activate)
  22053. "Make hierarchy visible when jumping into location from ECB tree buffer."
  22054. (if (derived-mode-p 'org-mode)
  22055. (org-show-context))))
  22056. (defun org-bookmark-jump-unhide ()
  22057. "Unhide the current position, to show the bookmark location."
  22058. (and (derived-mode-p 'org-mode)
  22059. (or (outline-invisible-p)
  22060. (save-excursion (goto-char (max (point-min) (1- (point))))
  22061. (outline-invisible-p)))
  22062. (org-show-context 'bookmark-jump)))
  22063. ;; Make session.el ignore our circular variable
  22064. (defvar session-globals-exclude)
  22065. (eval-after-load "session"
  22066. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  22067. ;;;; Finish up
  22068. (provide 'org)
  22069. (run-hooks 'org-load-hook)
  22070. ;;; org.el ends here