org.el 909 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046170471704817049170501705117052170531705417055170561705717058170591706017061170621706317064170651706617067170681706917070170711707217073170741707517076170771707817079170801708117082170831708417085170861708717088170891709017091170921709317094170951709617097170981709917100171011710217103171041710517106171071710817109171101711117112171131711417115171161711717118171191712017121171221712317124171251712617127171281712917130171311713217133171341713517136171371713817139171401714117142171431714417145171461714717148171491715017151171521715317154171551715617157171581715917160171611716217163171641716517166171671716817169171701717117172171731717417175171761717717178171791718017181171821718317184171851718617187171881718917190171911719217193171941719517196171971719817199172001720117202172031720417205172061720717208172091721017211172121721317214172151721617217172181721917220172211722217223172241722517226172271722817229172301723117232172331723417235172361723717238172391724017241172421724317244172451724617247172481724917250172511725217253172541725517256172571725817259172601726117262172631726417265172661726717268172691727017271172721727317274172751727617277172781727917280172811728217283172841728517286172871728817289172901729117292172931729417295172961729717298172991730017301173021730317304173051730617307173081730917310173111731217313173141731517316173171731817319173201732117322173231732417325173261732717328173291733017331173321733317334173351733617337173381733917340173411734217343173441734517346173471734817349173501735117352173531735417355173561735717358173591736017361173621736317364173651736617367173681736917370173711737217373173741737517376173771737817379173801738117382173831738417385173861738717388173891739017391173921739317394173951739617397173981739917400174011740217403174041740517406174071740817409174101741117412174131741417415174161741717418174191742017421174221742317424174251742617427174281742917430174311743217433174341743517436174371743817439174401744117442174431744417445174461744717448174491745017451174521745317454174551745617457174581745917460174611746217463174641746517466174671746817469174701747117472174731747417475174761747717478174791748017481174821748317484174851748617487174881748917490174911749217493174941749517496174971749817499175001750117502175031750417505175061750717508175091751017511175121751317514175151751617517175181751917520175211752217523175241752517526175271752817529175301753117532175331753417535175361753717538175391754017541175421754317544175451754617547175481754917550175511755217553175541755517556175571755817559175601756117562175631756417565175661756717568175691757017571175721757317574175751757617577175781757917580175811758217583175841758517586175871758817589175901759117592175931759417595175961759717598175991760017601176021760317604176051760617607176081760917610176111761217613176141761517616176171761817619176201762117622176231762417625176261762717628176291763017631176321763317634176351763617637176381763917640176411764217643176441764517646176471764817649176501765117652176531765417655176561765717658176591766017661176621766317664176651766617667176681766917670176711767217673176741767517676176771767817679176801768117682176831768417685176861768717688176891769017691176921769317694176951769617697176981769917700177011770217703177041770517706177071770817709177101771117712177131771417715177161771717718177191772017721177221772317724177251772617727177281772917730177311773217733177341773517736177371773817739177401774117742177431774417745177461774717748177491775017751177521775317754177551775617757177581775917760177611776217763177641776517766177671776817769177701777117772177731777417775177761777717778177791778017781177821778317784177851778617787177881778917790177911779217793177941779517796177971779817799178001780117802178031780417805178061780717808178091781017811178121781317814178151781617817178181781917820178211782217823178241782517826178271782817829178301783117832178331783417835178361783717838178391784017841178421784317844178451784617847178481784917850178511785217853178541785517856178571785817859178601786117862178631786417865178661786717868178691787017871178721787317874178751787617877178781787917880178811788217883178841788517886178871788817889178901789117892178931789417895178961789717898178991790017901179021790317904179051790617907179081790917910179111791217913179141791517916179171791817919179201792117922179231792417925179261792717928179291793017931179321793317934179351793617937179381793917940179411794217943179441794517946179471794817949179501795117952179531795417955179561795717958179591796017961179621796317964179651796617967179681796917970179711797217973179741797517976179771797817979179801798117982179831798417985179861798717988179891799017991179921799317994179951799617997179981799918000180011800218003180041800518006180071800818009180101801118012180131801418015180161801718018180191802018021180221802318024180251802618027180281802918030180311803218033180341803518036180371803818039180401804118042180431804418045180461804718048180491805018051180521805318054180551805618057180581805918060180611806218063180641806518066180671806818069180701807118072180731807418075180761807718078180791808018081180821808318084180851808618087180881808918090180911809218093180941809518096180971809818099181001810118102181031810418105181061810718108181091811018111181121811318114181151811618117181181811918120181211812218123181241812518126181271812818129181301813118132181331813418135181361813718138181391814018141181421814318144181451814618147181481814918150181511815218153181541815518156181571815818159181601816118162181631816418165181661816718168181691817018171181721817318174181751817618177181781817918180181811818218183181841818518186181871818818189181901819118192181931819418195181961819718198181991820018201182021820318204182051820618207182081820918210182111821218213182141821518216182171821818219182201822118222182231822418225182261822718228182291823018231182321823318234182351823618237182381823918240182411824218243182441824518246182471824818249182501825118252182531825418255182561825718258182591826018261182621826318264182651826618267182681826918270182711827218273182741827518276182771827818279182801828118282182831828418285182861828718288182891829018291182921829318294182951829618297182981829918300183011830218303183041830518306183071830818309183101831118312183131831418315183161831718318183191832018321183221832318324183251832618327183281832918330183311833218333183341833518336183371833818339183401834118342183431834418345183461834718348183491835018351183521835318354183551835618357183581835918360183611836218363183641836518366183671836818369183701837118372183731837418375183761837718378183791838018381183821838318384183851838618387183881838918390183911839218393183941839518396183971839818399184001840118402184031840418405184061840718408184091841018411184121841318414184151841618417184181841918420184211842218423184241842518426184271842818429184301843118432184331843418435184361843718438184391844018441184421844318444184451844618447184481844918450184511845218453184541845518456184571845818459184601846118462184631846418465184661846718468184691847018471184721847318474184751847618477184781847918480184811848218483184841848518486184871848818489184901849118492184931849418495184961849718498184991850018501185021850318504185051850618507185081850918510185111851218513185141851518516185171851818519185201852118522185231852418525185261852718528185291853018531185321853318534185351853618537185381853918540185411854218543185441854518546185471854818549185501855118552185531855418555185561855718558185591856018561185621856318564185651856618567185681856918570185711857218573185741857518576185771857818579185801858118582185831858418585185861858718588185891859018591185921859318594185951859618597185981859918600186011860218603186041860518606186071860818609186101861118612186131861418615186161861718618186191862018621186221862318624186251862618627186281862918630186311863218633186341863518636186371863818639186401864118642186431864418645186461864718648186491865018651186521865318654186551865618657186581865918660186611866218663186641866518666186671866818669186701867118672186731867418675186761867718678186791868018681186821868318684186851868618687186881868918690186911869218693186941869518696186971869818699187001870118702187031870418705187061870718708187091871018711187121871318714187151871618717187181871918720187211872218723187241872518726187271872818729187301873118732187331873418735187361873718738187391874018741187421874318744187451874618747187481874918750187511875218753187541875518756187571875818759187601876118762187631876418765187661876718768187691877018771187721877318774187751877618777187781877918780187811878218783187841878518786187871878818789187901879118792187931879418795187961879718798187991880018801188021880318804188051880618807188081880918810188111881218813188141881518816188171881818819188201882118822188231882418825188261882718828188291883018831188321883318834188351883618837188381883918840188411884218843188441884518846188471884818849188501885118852188531885418855188561885718858188591886018861188621886318864188651886618867188681886918870188711887218873188741887518876188771887818879188801888118882188831888418885188861888718888188891889018891188921889318894188951889618897188981889918900189011890218903189041890518906189071890818909189101891118912189131891418915189161891718918189191892018921189221892318924189251892618927189281892918930189311893218933189341893518936189371893818939189401894118942189431894418945189461894718948189491895018951189521895318954189551895618957189581895918960189611896218963189641896518966189671896818969189701897118972189731897418975189761897718978189791898018981189821898318984189851898618987189881898918990189911899218993189941899518996189971899818999190001900119002190031900419005190061900719008190091901019011190121901319014190151901619017190181901919020190211902219023190241902519026190271902819029190301903119032190331903419035190361903719038190391904019041190421904319044190451904619047190481904919050190511905219053190541905519056190571905819059190601906119062190631906419065190661906719068190691907019071190721907319074190751907619077190781907919080190811908219083190841908519086190871908819089190901909119092190931909419095190961909719098190991910019101191021910319104191051910619107191081910919110191111911219113191141911519116191171911819119191201912119122191231912419125191261912719128191291913019131191321913319134191351913619137191381913919140191411914219143191441914519146191471914819149191501915119152191531915419155191561915719158191591916019161191621916319164191651916619167191681916919170191711917219173191741917519176191771917819179191801918119182191831918419185191861918719188191891919019191191921919319194191951919619197191981919919200192011920219203192041920519206192071920819209192101921119212192131921419215192161921719218192191922019221192221922319224192251922619227192281922919230192311923219233192341923519236192371923819239192401924119242192431924419245192461924719248192491925019251192521925319254192551925619257192581925919260192611926219263192641926519266192671926819269192701927119272192731927419275192761927719278192791928019281192821928319284192851928619287192881928919290192911929219293192941929519296192971929819299193001930119302193031930419305193061930719308193091931019311193121931319314193151931619317193181931919320193211932219323193241932519326193271932819329193301933119332193331933419335193361933719338193391934019341193421934319344193451934619347193481934919350193511935219353193541935519356193571935819359193601936119362193631936419365193661936719368193691937019371193721937319374193751937619377193781937919380193811938219383193841938519386193871938819389193901939119392193931939419395193961939719398193991940019401194021940319404194051940619407194081940919410194111941219413194141941519416194171941819419194201942119422194231942419425194261942719428194291943019431194321943319434194351943619437194381943919440194411944219443194441944519446194471944819449194501945119452194531945419455194561945719458194591946019461194621946319464194651946619467194681946919470194711947219473194741947519476194771947819479194801948119482194831948419485194861948719488194891949019491194921949319494194951949619497194981949919500195011950219503195041950519506195071950819509195101951119512195131951419515195161951719518195191952019521195221952319524195251952619527195281952919530195311953219533195341953519536195371953819539195401954119542195431954419545195461954719548195491955019551195521955319554195551955619557195581955919560195611956219563195641956519566195671956819569195701957119572195731957419575195761957719578195791958019581195821958319584195851958619587195881958919590195911959219593195941959519596195971959819599196001960119602196031960419605196061960719608196091961019611196121961319614196151961619617196181961919620196211962219623196241962519626196271962819629196301963119632196331963419635196361963719638196391964019641196421964319644196451964619647196481964919650196511965219653196541965519656196571965819659196601966119662196631966419665196661966719668196691967019671196721967319674196751967619677196781967919680196811968219683196841968519686196871968819689196901969119692196931969419695196961969719698196991970019701197021970319704197051970619707197081970919710197111971219713197141971519716197171971819719197201972119722197231972419725197261972719728197291973019731197321973319734197351973619737197381973919740197411974219743197441974519746197471974819749197501975119752197531975419755197561975719758197591976019761197621976319764197651976619767197681976919770197711977219773197741977519776197771977819779197801978119782197831978419785197861978719788197891979019791197921979319794197951979619797197981979919800198011980219803198041980519806198071980819809198101981119812198131981419815198161981719818198191982019821198221982319824198251982619827198281982919830198311983219833198341983519836198371983819839198401984119842198431984419845198461984719848198491985019851198521985319854198551985619857198581985919860198611986219863198641986519866198671986819869198701987119872198731987419875198761987719878198791988019881198821988319884198851988619887198881988919890198911989219893198941989519896198971989819899199001990119902199031990419905199061990719908199091991019911199121991319914199151991619917199181991919920199211992219923199241992519926199271992819929199301993119932199331993419935199361993719938199391994019941199421994319944199451994619947199481994919950199511995219953199541995519956199571995819959199601996119962199631996419965199661996719968199691997019971199721997319974199751997619977199781997919980199811998219983199841998519986199871998819989199901999119992199931999419995199961999719998199992000020001200022000320004200052000620007200082000920010200112001220013200142001520016200172001820019200202002120022200232002420025200262002720028200292003020031200322003320034200352003620037200382003920040200412004220043200442004520046200472004820049200502005120052200532005420055200562005720058200592006020061200622006320064200652006620067200682006920070200712007220073200742007520076200772007820079200802008120082200832008420085200862008720088200892009020091200922009320094200952009620097200982009920100201012010220103201042010520106201072010820109201102011120112201132011420115201162011720118201192012020121201222012320124201252012620127201282012920130201312013220133201342013520136201372013820139201402014120142201432014420145201462014720148201492015020151201522015320154201552015620157201582015920160201612016220163201642016520166201672016820169201702017120172201732017420175201762017720178201792018020181201822018320184201852018620187201882018920190201912019220193201942019520196201972019820199202002020120202202032020420205202062020720208202092021020211202122021320214202152021620217202182021920220202212022220223202242022520226202272022820229202302023120232202332023420235202362023720238202392024020241202422024320244202452024620247202482024920250202512025220253202542025520256202572025820259202602026120262202632026420265202662026720268202692027020271202722027320274202752027620277202782027920280202812028220283202842028520286202872028820289202902029120292202932029420295202962029720298202992030020301203022030320304203052030620307203082030920310203112031220313203142031520316203172031820319203202032120322203232032420325203262032720328203292033020331203322033320334203352033620337203382033920340203412034220343203442034520346203472034820349203502035120352203532035420355203562035720358203592036020361203622036320364203652036620367203682036920370203712037220373203742037520376203772037820379203802038120382203832038420385203862038720388203892039020391203922039320394203952039620397203982039920400204012040220403204042040520406204072040820409204102041120412204132041420415204162041720418204192042020421204222042320424204252042620427204282042920430204312043220433204342043520436204372043820439204402044120442204432044420445204462044720448204492045020451204522045320454204552045620457204582045920460204612046220463204642046520466204672046820469204702047120472204732047420475204762047720478204792048020481204822048320484204852048620487204882048920490204912049220493204942049520496204972049820499205002050120502205032050420505205062050720508205092051020511205122051320514205152051620517205182051920520205212052220523205242052520526205272052820529205302053120532205332053420535205362053720538205392054020541205422054320544205452054620547205482054920550205512055220553205542055520556205572055820559205602056120562205632056420565205662056720568205692057020571205722057320574205752057620577205782057920580205812058220583205842058520586205872058820589205902059120592205932059420595205962059720598205992060020601206022060320604206052060620607206082060920610206112061220613206142061520616206172061820619206202062120622206232062420625206262062720628206292063020631206322063320634206352063620637206382063920640206412064220643206442064520646206472064820649206502065120652206532065420655206562065720658206592066020661206622066320664206652066620667206682066920670206712067220673206742067520676206772067820679206802068120682206832068420685206862068720688206892069020691206922069320694206952069620697206982069920700207012070220703207042070520706207072070820709207102071120712207132071420715207162071720718207192072020721207222072320724207252072620727207282072920730207312073220733207342073520736207372073820739207402074120742207432074420745207462074720748207492075020751207522075320754207552075620757207582075920760207612076220763207642076520766207672076820769207702077120772207732077420775207762077720778207792078020781207822078320784207852078620787207882078920790207912079220793207942079520796207972079820799208002080120802208032080420805208062080720808208092081020811208122081320814208152081620817208182081920820208212082220823208242082520826208272082820829208302083120832208332083420835208362083720838208392084020841208422084320844208452084620847208482084920850208512085220853208542085520856208572085820859208602086120862208632086420865208662086720868208692087020871208722087320874208752087620877208782087920880208812088220883208842088520886208872088820889208902089120892208932089420895208962089720898208992090020901209022090320904209052090620907209082090920910209112091220913209142091520916209172091820919209202092120922209232092420925209262092720928209292093020931209322093320934209352093620937209382093920940209412094220943209442094520946209472094820949209502095120952209532095420955209562095720958209592096020961209622096320964209652096620967209682096920970209712097220973209742097520976209772097820979209802098120982209832098420985209862098720988209892099020991209922099320994209952099620997209982099921000210012100221003210042100521006210072100821009210102101121012210132101421015210162101721018210192102021021210222102321024210252102621027210282102921030210312103221033210342103521036210372103821039210402104121042210432104421045210462104721048210492105021051210522105321054210552105621057210582105921060210612106221063210642106521066210672106821069210702107121072210732107421075210762107721078210792108021081210822108321084210852108621087210882108921090210912109221093210942109521096210972109821099211002110121102211032110421105211062110721108211092111021111211122111321114211152111621117211182111921120211212112221123211242112521126211272112821129211302113121132211332113421135211362113721138211392114021141211422114321144211452114621147211482114921150211512115221153211542115521156211572115821159211602116121162211632116421165211662116721168211692117021171211722117321174211752117621177211782117921180211812118221183211842118521186211872118821189211902119121192211932119421195211962119721198211992120021201212022120321204212052120621207212082120921210212112121221213212142121521216212172121821219212202122121222212232122421225212262122721228212292123021231212322123321234212352123621237212382123921240212412124221243212442124521246212472124821249212502125121252212532125421255212562125721258212592126021261212622126321264212652126621267212682126921270212712127221273212742127521276212772127821279212802128121282212832128421285212862128721288212892129021291212922129321294212952129621297212982129921300213012130221303213042130521306213072130821309213102131121312213132131421315213162131721318213192132021321213222132321324213252132621327213282132921330213312133221333213342133521336213372133821339213402134121342213432134421345213462134721348213492135021351213522135321354213552135621357213582135921360213612136221363213642136521366213672136821369213702137121372213732137421375213762137721378213792138021381213822138321384213852138621387213882138921390213912139221393213942139521396213972139821399214002140121402214032140421405214062140721408214092141021411214122141321414214152141621417214182141921420214212142221423214242142521426214272142821429214302143121432214332143421435214362143721438214392144021441214422144321444214452144621447214482144921450214512145221453214542145521456214572145821459214602146121462214632146421465214662146721468214692147021471214722147321474214752147621477214782147921480214812148221483214842148521486214872148821489214902149121492214932149421495214962149721498214992150021501215022150321504215052150621507215082150921510215112151221513215142151521516215172151821519215202152121522215232152421525215262152721528215292153021531215322153321534215352153621537215382153921540215412154221543215442154521546215472154821549215502155121552215532155421555215562155721558215592156021561215622156321564215652156621567215682156921570215712157221573215742157521576215772157821579215802158121582215832158421585215862158721588215892159021591215922159321594215952159621597215982159921600216012160221603216042160521606216072160821609216102161121612216132161421615216162161721618216192162021621216222162321624216252162621627216282162921630216312163221633216342163521636216372163821639216402164121642216432164421645216462164721648216492165021651216522165321654216552165621657216582165921660216612166221663216642166521666216672166821669216702167121672216732167421675216762167721678216792168021681216822168321684216852168621687216882168921690216912169221693216942169521696216972169821699217002170121702217032170421705217062170721708217092171021711217122171321714217152171621717217182171921720217212172221723217242172521726217272172821729217302173121732217332173421735217362173721738217392174021741217422174321744217452174621747217482174921750217512175221753217542175521756217572175821759217602176121762217632176421765217662176721768217692177021771217722177321774217752177621777217782177921780217812178221783217842178521786217872178821789217902179121792217932179421795217962179721798217992180021801218022180321804218052180621807218082180921810218112181221813218142181521816218172181821819218202182121822218232182421825218262182721828218292183021831218322183321834218352183621837218382183921840218412184221843218442184521846218472184821849218502185121852218532185421855218562185721858218592186021861218622186321864218652186621867218682186921870218712187221873218742187521876218772187821879218802188121882218832188421885218862188721888218892189021891218922189321894218952189621897218982189921900219012190221903219042190521906219072190821909219102191121912219132191421915219162191721918219192192021921219222192321924219252192621927219282192921930219312193221933219342193521936219372193821939219402194121942219432194421945219462194721948219492195021951219522195321954219552195621957219582195921960219612196221963219642196521966219672196821969219702197121972219732197421975219762197721978219792198021981219822198321984219852198621987219882198921990219912199221993219942199521996219972199821999220002200122002220032200422005220062200722008220092201022011220122201322014220152201622017220182201922020220212202222023220242202522026220272202822029220302203122032220332203422035220362203722038220392204022041220422204322044220452204622047220482204922050220512205222053220542205522056220572205822059220602206122062220632206422065220662206722068220692207022071220722207322074220752207622077220782207922080220812208222083220842208522086220872208822089220902209122092220932209422095220962209722098220992210022101221022210322104221052210622107221082210922110221112211222113221142211522116221172211822119221202212122122221232212422125221262212722128221292213022131221322213322134221352213622137221382213922140221412214222143221442214522146221472214822149221502215122152221532215422155221562215722158221592216022161221622216322164221652216622167221682216922170221712217222173221742217522176221772217822179221802218122182221832218422185221862218722188221892219022191221922219322194221952219622197221982219922200222012220222203222042220522206222072220822209222102221122212222132221422215222162221722218222192222022221222222222322224222252222622227222282222922230222312223222233222342223522236222372223822239222402224122242222432224422245222462224722248222492225022251222522225322254222552225622257222582225922260222612226222263222642226522266222672226822269222702227122272222732227422275222762227722278222792228022281222822228322284222852228622287222882228922290222912229222293222942229522296222972229822299223002230122302223032230422305223062230722308223092231022311223122231322314223152231622317223182231922320223212232222323223242232522326223272232822329223302233122332223332233422335223362233722338223392234022341223422234322344223452234622347223482234922350223512235222353223542235522356223572235822359223602236122362223632236422365223662236722368223692237022371223722237322374223752237622377223782237922380223812238222383223842238522386223872238822389223902239122392223932239422395223962239722398223992240022401224022240322404224052240622407224082240922410224112241222413224142241522416224172241822419224202242122422224232242422425224262242722428224292243022431224322243322434224352243622437224382243922440224412244222443224442244522446224472244822449224502245122452224532245422455224562245722458224592246022461224622246322464224652246622467224682246922470224712247222473224742247522476224772247822479224802248122482224832248422485224862248722488224892249022491224922249322494224952249622497224982249922500225012250222503225042250522506225072250822509225102251122512225132251422515225162251722518225192252022521225222252322524225252252622527225282252922530225312253222533225342253522536225372253822539225402254122542225432254422545225462254722548225492255022551225522255322554225552255622557225582255922560225612256222563225642256522566225672256822569225702257122572225732257422575225762257722578225792258022581225822258322584225852258622587225882258922590225912259222593225942259522596225972259822599226002260122602226032260422605226062260722608226092261022611226122261322614226152261622617226182261922620226212262222623226242262522626226272262822629226302263122632226332263422635226362263722638226392264022641226422264322644226452264622647226482264922650226512265222653226542265522656226572265822659226602266122662226632266422665226662266722668226692267022671226722267322674226752267622677226782267922680226812268222683226842268522686226872268822689226902269122692226932269422695226962269722698226992270022701227022270322704227052270622707227082270922710227112271222713227142271522716227172271822719227202272122722227232272422725227262272722728227292273022731227322273322734227352273622737227382273922740227412274222743227442274522746227472274822749227502275122752227532275422755227562275722758227592276022761227622276322764227652276622767227682276922770227712277222773227742277522776227772277822779227802278122782227832278422785227862278722788227892279022791227922279322794227952279622797227982279922800228012280222803228042280522806228072280822809228102281122812228132281422815228162281722818228192282022821228222282322824228252282622827228282282922830228312283222833228342283522836228372283822839228402284122842228432284422845228462284722848228492285022851228522285322854228552285622857228582285922860228612286222863228642286522866228672286822869228702287122872228732287422875228762287722878228792288022881228822288322884228852288622887228882288922890228912289222893228942289522896228972289822899229002290122902229032290422905229062290722908229092291022911229122291322914229152291622917229182291922920229212292222923229242292522926229272292822929229302293122932229332293422935229362293722938229392294022941229422294322944229452294622947229482294922950229512295222953229542295522956229572295822959229602296122962229632296422965229662296722968229692297022971229722297322974229752297622977229782297922980229812298222983229842298522986229872298822989229902299122992229932299422995229962299722998229992300023001230022300323004230052300623007230082300923010230112301223013230142301523016230172301823019230202302123022230232302423025230262302723028230292303023031230322303323034230352303623037230382303923040230412304223043230442304523046230472304823049230502305123052230532305423055230562305723058230592306023061230622306323064230652306623067230682306923070230712307223073230742307523076230772307823079230802308123082230832308423085230862308723088230892309023091230922309323094230952309623097230982309923100231012310223103231042310523106231072310823109231102311123112231132311423115231162311723118231192312023121231222312323124231252312623127231282312923130231312313223133231342313523136231372313823139231402314123142231432314423145231462314723148231492315023151231522315323154231552315623157231582315923160231612316223163231642316523166231672316823169231702317123172231732317423175231762317723178231792318023181231822318323184231852318623187231882318923190231912319223193231942319523196231972319823199232002320123202232032320423205232062320723208232092321023211232122321323214232152321623217232182321923220232212322223223232242322523226232272322823229232302323123232232332323423235232362323723238232392324023241232422324323244232452324623247232482324923250232512325223253232542325523256232572325823259232602326123262232632326423265232662326723268232692327023271232722327323274232752327623277232782327923280232812328223283232842328523286232872328823289232902329123292232932329423295232962329723298232992330023301233022330323304233052330623307233082330923310233112331223313233142331523316233172331823319233202332123322233232332423325233262332723328233292333023331233322333323334233352333623337233382333923340233412334223343233442334523346233472334823349233502335123352233532335423355233562335723358233592336023361233622336323364233652336623367233682336923370233712337223373233742337523376233772337823379233802338123382233832338423385233862338723388233892339023391233922339323394233952339623397233982339923400234012340223403234042340523406234072340823409234102341123412234132341423415234162341723418234192342023421234222342323424234252342623427234282342923430234312343223433234342343523436234372343823439234402344123442234432344423445234462344723448234492345023451234522345323454234552345623457234582345923460234612346223463234642346523466234672346823469234702347123472234732347423475234762347723478234792348023481234822348323484234852348623487234882348923490234912349223493234942349523496234972349823499235002350123502235032350423505235062350723508235092351023511235122351323514235152351623517235182351923520235212352223523235242352523526235272352823529235302353123532235332353423535235362353723538235392354023541235422354323544235452354623547235482354923550235512355223553235542355523556235572355823559235602356123562235632356423565235662356723568235692357023571235722357323574235752357623577235782357923580235812358223583235842358523586235872358823589235902359123592235932359423595235962359723598235992360023601236022360323604236052360623607236082360923610236112361223613236142361523616236172361823619236202362123622236232362423625236262362723628236292363023631236322363323634236352363623637236382363923640236412364223643236442364523646236472364823649236502365123652236532365423655236562365723658236592366023661236622366323664236652366623667236682366923670236712367223673236742367523676236772367823679236802368123682236832368423685236862368723688236892369023691236922369323694236952369623697236982369923700237012370223703237042370523706237072370823709237102371123712237132371423715237162371723718237192372023721237222372323724237252372623727237282372923730237312373223733237342373523736237372373823739237402374123742237432374423745237462374723748237492375023751237522375323754237552375623757237582375923760237612376223763237642376523766237672376823769237702377123772237732377423775237762377723778237792378023781237822378323784237852378623787237882378923790237912379223793237942379523796237972379823799238002380123802238032380423805238062380723808238092381023811238122381323814238152381623817238182381923820238212382223823238242382523826238272382823829238302383123832238332383423835238362383723838238392384023841238422384323844238452384623847238482384923850238512385223853238542385523856238572385823859238602386123862238632386423865238662386723868238692387023871238722387323874238752387623877238782387923880238812388223883238842388523886238872388823889238902389123892238932389423895238962389723898238992390023901239022390323904239052390623907239082390923910239112391223913239142391523916239172391823919239202392123922239232392423925239262392723928239292393023931239322393323934239352393623937239382393923940239412394223943239442394523946239472394823949239502395123952239532395423955239562395723958239592396023961239622396323964239652396623967239682396923970239712397223973239742397523976239772397823979239802398123982239832398423985239862398723988239892399023991239922399323994239952399623997239982399924000240012400224003240042400524006240072400824009240102401124012
  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. When optional argument LITERAL is non-nil, do not quote PROPERTY.
  5485. This is useful when PROPERTY is a regexp. When ALLOW-NULL is
  5486. non-nil, match properties even without a value.
  5487. Match group 3 is set to the value when it exists. If there is no
  5488. value and ALLOW-NULL is non-nil, it is set to the empty string."
  5489. (concat
  5490. "^\\(?4:[ \t]*\\)"
  5491. (format "\\(?1::\\(?2:%s\\):\\)"
  5492. (if literal property (regexp-quote property)))
  5493. (if allow-null
  5494. "\\(?:\\(?3:$\\)\\|[ \t]+\\(?3:.*?\\)\\)\\(?5:[ \t]*\\)$"
  5495. "[ \t]+\\(?3:[^ \r\t\n]+.*?\\)\\(?5:[ \t]*\\)$")))
  5496. (defconst org-property-re
  5497. (org-re-property ".*?" 'literal t)
  5498. "Regular expression matching a property line.
  5499. There are four matching groups:
  5500. 1: :PROPKEY: including the leading and trailing colon,
  5501. 2: PROPKEY without the leading and trailing colon,
  5502. 3: PROPVAL without leading or trailing spaces,
  5503. 4: the indentation of the current line,
  5504. 5: trailing whitespace.")
  5505. (defvar org-font-lock-hook nil
  5506. "Functions to be called for special font lock stuff.")
  5507. (defvar org-font-lock-set-keywords-hook nil
  5508. "Functions that can manipulate `org-font-lock-extra-keywords'.
  5509. This is called after `org-font-lock-extra-keywords' is defined, but before
  5510. it is installed to be used by font lock. This can be useful if something
  5511. needs to be inserted at a specific position in the font-lock sequence.")
  5512. (defun org-font-lock-hook (limit)
  5513. "Run `org-font-lock-hook' within LIMIT."
  5514. (run-hook-with-args 'org-font-lock-hook limit))
  5515. (defun org-set-font-lock-defaults ()
  5516. "Set font lock defaults for the current buffer."
  5517. (let* ((em org-fontify-emphasized-text)
  5518. (lk org-activate-links)
  5519. (org-font-lock-extra-keywords
  5520. (list
  5521. ;; Call the hook
  5522. '(org-font-lock-hook)
  5523. ;; Headlines
  5524. `(,(if org-fontify-whole-heading-line
  5525. "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
  5526. "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
  5527. (1 (org-get-level-face 1))
  5528. (2 (org-get-level-face 2))
  5529. (3 (org-get-level-face 3)))
  5530. ;; Table lines
  5531. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  5532. (1 'org-table t))
  5533. ;; Table internals
  5534. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  5535. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  5536. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  5537. '("| *\\(<[lrc]?[0-9]*>\\)" (1 'org-formula t))
  5538. ;; Drawers
  5539. (list org-drawer-regexp '(0 'org-special-keyword t))
  5540. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  5541. ;; Properties
  5542. (list org-property-re
  5543. '(1 'org-special-keyword t)
  5544. '(3 'org-property-value t))
  5545. ;; Links
  5546. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  5547. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  5548. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  5549. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  5550. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  5551. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  5552. (if (memq 'footnote lk) '(org-activate-footnote-links))
  5553. ;; Targets.
  5554. (list org-any-target-regexp '(0 'org-target t))
  5555. ;; Diary sexps.
  5556. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  5557. ;; Macro
  5558. '("{{{.+}}}" (0 'org-macro t))
  5559. '(org-hide-wide-columns (0 nil append))
  5560. ;; TODO keyword
  5561. (list (format org-heading-keyword-regexp-format
  5562. org-todo-regexp)
  5563. '(2 (org-get-todo-face 2) t))
  5564. ;; DONE
  5565. (if org-fontify-done-headline
  5566. (list (format org-heading-keyword-regexp-format
  5567. (concat
  5568. "\\(?:"
  5569. (mapconcat 'regexp-quote org-done-keywords "\\|")
  5570. "\\)"))
  5571. '(2 'org-headline-done t))
  5572. nil)
  5573. ;; Priorities
  5574. '(org-font-lock-add-priority-faces)
  5575. ;; Tags
  5576. '(org-font-lock-add-tag-faces)
  5577. ;; Tags groups
  5578. (if (and org-group-tags org-tag-groups-alist)
  5579. (list (concat org-outline-regexp-bol ".+\\(:"
  5580. (regexp-opt (mapcar 'car org-tag-groups-alist))
  5581. ":\\).*$")
  5582. '(1 'org-tag-group prepend)))
  5583. ;; Special keywords
  5584. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  5585. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  5586. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  5587. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  5588. ;; Emphasis
  5589. (if em
  5590. (if (featurep 'xemacs)
  5591. '(org-do-emphasis-faces (0 nil append))
  5592. '(org-do-emphasis-faces)))
  5593. ;; Checkboxes
  5594. '("^[ \t]*\\(?:[-+*]\\|[0-9]+[.)]\\)[ \t]+\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?\\(\\[[- X]\\]\\)"
  5595. 1 'org-checkbox prepend)
  5596. (if (cdr (assq 'checkbox org-list-automatic-rules))
  5597. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  5598. (0 (org-get-checkbox-statistics-face) t)))
  5599. ;; Description list items
  5600. '("^[ \t]*[-+*][ \t]+\\(.*?[ \t]+::\\)\\([ \t]+\\|$\\)"
  5601. 1 'org-list-dt prepend)
  5602. ;; ARCHIVEd headings
  5603. (list (concat
  5604. org-outline-regexp-bol
  5605. "\\(.*:" org-archive-tag ":.*\\)")
  5606. '(1 'org-archived prepend))
  5607. ;; Specials
  5608. '(org-do-latex-and-related)
  5609. '(org-fontify-entities)
  5610. '(org-raise-scripts)
  5611. ;; Code
  5612. '(org-activate-code (1 'org-code t))
  5613. ;; COMMENT
  5614. (list (format org-heading-keyword-regexp-format
  5615. (concat "\\("
  5616. org-comment-string "\\|" org-quote-string
  5617. "\\)"))
  5618. '(2 'org-special-keyword t))
  5619. ;; Blocks and meta lines
  5620. '(org-fontify-meta-lines-and-blocks))))
  5621. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  5622. (run-hooks 'org-font-lock-set-keywords-hook)
  5623. ;; Now set the full font-lock-keywords
  5624. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  5625. (org-set-local 'font-lock-defaults
  5626. '(org-font-lock-keywords t nil nil backward-paragraph))
  5627. (kill-local-variable 'font-lock-keywords) nil))
  5628. (defun org-toggle-pretty-entities ()
  5629. "Toggle the composition display of entities as UTF8 characters."
  5630. (interactive)
  5631. (org-set-local 'org-pretty-entities (not org-pretty-entities))
  5632. (org-restart-font-lock)
  5633. (if org-pretty-entities
  5634. (message "Entities are now displayed as UTF8 characters")
  5635. (save-restriction
  5636. (widen)
  5637. (org-decompose-region (point-min) (point-max))
  5638. (message "Entities are now displayed as plain text"))))
  5639. (defvar org-custom-properties-overlays nil
  5640. "List of overlays used for custom properties.")
  5641. (make-variable-buffer-local 'org-custom-properties-overlays)
  5642. (defun org-toggle-custom-properties-visibility ()
  5643. "Display or hide properties in `org-custom-properties'."
  5644. (interactive)
  5645. (if org-custom-properties-overlays
  5646. (progn (mapc 'delete-overlay org-custom-properties-overlays)
  5647. (setq org-custom-properties-overlays nil))
  5648. (unless (not org-custom-properties)
  5649. (save-excursion
  5650. (save-restriction
  5651. (widen)
  5652. (goto-char (point-min))
  5653. (while (re-search-forward org-property-re nil t)
  5654. (mapc (lambda(p)
  5655. (when (equal p (substring (match-string 1) 1 -1))
  5656. (let ((o (make-overlay (match-beginning 0) (1+ (match-end 0)))))
  5657. (overlay-put o 'invisible t)
  5658. (overlay-put o 'org-custom-property t)
  5659. (push o org-custom-properties-overlays))))
  5660. org-custom-properties)))))))
  5661. (defun org-fontify-entities (limit)
  5662. "Find an entity to fontify."
  5663. (let (ee)
  5664. (when org-pretty-entities
  5665. (catch 'match
  5666. (while (re-search-forward
  5667. "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]\n]\\)"
  5668. limit t)
  5669. (if (and (not (org-in-indented-comment-line))
  5670. (setq ee (org-entity-get (match-string 1)))
  5671. (= (length (nth 6 ee)) 1))
  5672. (let*
  5673. ((end (if (equal (match-string 2) "{}")
  5674. (match-end 2)
  5675. (match-end 1))))
  5676. (add-text-properties
  5677. (match-beginning 0) end
  5678. (list 'font-lock-fontified t))
  5679. (compose-region (match-beginning 0) end
  5680. (nth 6 ee) nil)
  5681. (backward-char 1)
  5682. (throw 'match t))))
  5683. nil))))
  5684. (defun org-fontify-like-in-org-mode (s &optional odd-levels)
  5685. "Fontify string S like in Org-mode."
  5686. (with-temp-buffer
  5687. (insert s)
  5688. (let ((org-odd-levels-only odd-levels))
  5689. (org-mode)
  5690. (font-lock-fontify-buffer)
  5691. (buffer-string))))
  5692. (defvar org-m nil)
  5693. (defvar org-l nil)
  5694. (defvar org-f nil)
  5695. (defun org-get-level-face (n)
  5696. "Get the right face for match N in font-lock matching of headlines."
  5697. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  5698. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  5699. (if org-cycle-level-faces
  5700. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  5701. (setq org-f (nth (1- (min org-l org-n-level-faces)) org-level-faces)))
  5702. (cond
  5703. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  5704. ((eq n 2) org-f)
  5705. (t (if org-level-color-stars-only nil org-f))))
  5706. (defun org-get-todo-face (kwd)
  5707. "Get the right face for a TODO keyword KWD.
  5708. If KWD is a number, get the corresponding match group."
  5709. (if (numberp kwd) (setq kwd (match-string kwd)))
  5710. (or (org-face-from-face-or-color
  5711. 'todo 'org-todo (cdr (assoc kwd org-todo-keyword-faces)))
  5712. (and (member kwd org-done-keywords) 'org-done)
  5713. 'org-todo))
  5714. (defun org-face-from-face-or-color (context inherit face-or-color)
  5715. "Create a face list that inherits INHERIT, but sets the foreground color.
  5716. When FACE-OR-COLOR is not a string, just return it."
  5717. (if (stringp face-or-color)
  5718. (list :inherit inherit
  5719. (cdr (assoc context org-faces-easy-properties))
  5720. face-or-color)
  5721. face-or-color))
  5722. (defun org-font-lock-add-tag-faces (limit)
  5723. "Add the special tag faces."
  5724. (when (and org-tag-faces org-tags-special-faces-re)
  5725. (while (re-search-forward org-tags-special-faces-re limit t)
  5726. (add-text-properties (match-beginning 1) (match-end 1)
  5727. (list 'face (org-get-tag-face 1)
  5728. 'font-lock-fontified t))
  5729. (backward-char 1))))
  5730. (defun org-font-lock-add-priority-faces (limit)
  5731. "Add the special priority faces."
  5732. (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
  5733. (when (save-match-data (org-at-heading-p))
  5734. (add-text-properties
  5735. (match-beginning 0) (match-end 0)
  5736. (list 'face (or (org-face-from-face-or-color
  5737. 'priority 'org-priority
  5738. (cdr (assoc (char-after (match-beginning 1))
  5739. org-priority-faces)))
  5740. 'org-priority)
  5741. 'font-lock-fontified t)))))
  5742. (defun org-get-tag-face (kwd)
  5743. "Get the right face for a TODO keyword KWD.
  5744. If KWD is a number, get the corresponding match group."
  5745. (if (numberp kwd) (setq kwd (match-string kwd)))
  5746. (or (org-face-from-face-or-color
  5747. 'tag 'org-tag (cdr (assoc kwd org-tag-faces)))
  5748. 'org-tag))
  5749. (defun org-unfontify-region (beg end &optional maybe_loudly)
  5750. "Remove fontification and activation overlays from links."
  5751. (font-lock-default-unfontify-region beg end)
  5752. (let* ((buffer-undo-list t)
  5753. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  5754. (inhibit-modification-hooks t)
  5755. deactivate-mark buffer-file-name buffer-file-truename)
  5756. (org-decompose-region beg end)
  5757. (remove-text-properties beg end
  5758. '(mouse-face t keymap t org-linked-text t
  5759. invisible t intangible t
  5760. org-no-flyspell t org-emphasis t))
  5761. (org-remove-font-lock-display-properties beg end)))
  5762. (defconst org-script-display '(((raise -0.3) (height 0.7))
  5763. ((raise 0.3) (height 0.7))
  5764. ((raise -0.5))
  5765. ((raise 0.5)))
  5766. "Display properties for showing superscripts and subscripts.")
  5767. (defun org-remove-font-lock-display-properties (beg end)
  5768. "Remove specific display properties that have been added by font lock.
  5769. The will remove the raise properties that are used to show superscripts
  5770. and subscripts."
  5771. (let (next prop)
  5772. (while (< beg end)
  5773. (setq next (next-single-property-change beg 'display nil end)
  5774. prop (get-text-property beg 'display))
  5775. (if (member prop org-script-display)
  5776. (put-text-property beg next 'display nil))
  5777. (setq beg next))))
  5778. (defun org-raise-scripts (limit)
  5779. "Add raise properties to sub/superscripts."
  5780. (when (and org-pretty-entities org-pretty-entities-include-sub-superscripts)
  5781. (if (re-search-forward
  5782. (if (eq org-use-sub-superscripts t)
  5783. org-match-substring-regexp
  5784. org-match-substring-with-braces-regexp)
  5785. limit t)
  5786. (let* ((pos (point)) table-p comment-p
  5787. (mpos (match-beginning 3))
  5788. (emph-p (get-text-property mpos 'org-emphasis))
  5789. (link-p (get-text-property mpos 'mouse-face))
  5790. (keyw-p (eq 'org-special-keyword (get-text-property mpos 'face))))
  5791. (goto-char (point-at-bol))
  5792. (setq table-p (org-looking-at-p org-table-dataline-regexp)
  5793. comment-p (org-looking-at-p "^[ \t]*#[ +]"))
  5794. (goto-char pos)
  5795. ;; Handle a_b^c
  5796. (if (member (char-after) '(?_ ?^)) (goto-char (1- pos)))
  5797. (if (or comment-p emph-p link-p keyw-p)
  5798. t
  5799. (put-text-property (match-beginning 3) (match-end 0)
  5800. 'display
  5801. (if (equal (char-after (match-beginning 2)) ?^)
  5802. (nth (if table-p 3 1) org-script-display)
  5803. (nth (if table-p 2 0) org-script-display)))
  5804. (add-text-properties (match-beginning 2) (match-end 2)
  5805. (list 'invisible t
  5806. 'org-dwidth t 'org-dwidth-n 1))
  5807. (if (and (eq (char-after (match-beginning 3)) ?{)
  5808. (eq (char-before (match-end 3)) ?}))
  5809. (progn
  5810. (add-text-properties
  5811. (match-beginning 3) (1+ (match-beginning 3))
  5812. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))
  5813. (add-text-properties
  5814. (1- (match-end 3)) (match-end 3)
  5815. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))))
  5816. t)))))
  5817. ;;;; Visibility cycling, including org-goto and indirect buffer
  5818. ;;; Cycling
  5819. (defvar org-cycle-global-status nil)
  5820. (make-variable-buffer-local 'org-cycle-global-status)
  5821. (put 'org-cycle-global-status 'org-state t)
  5822. (defvar org-cycle-subtree-status nil)
  5823. (make-variable-buffer-local 'org-cycle-subtree-status)
  5824. (put 'org-cycle-subtree-status 'org-state t)
  5825. (defvar org-inlinetask-min-level)
  5826. (defun org-unlogged-message (&rest args)
  5827. "Display a message, but avoid logging it in the *Messages* buffer."
  5828. (let ((message-log-max nil))
  5829. (apply 'message args)))
  5830. ;;;###autoload
  5831. (defun org-cycle (&optional arg)
  5832. "TAB-action and visibility cycling for Org-mode.
  5833. This is the command invoked in Org-mode by the TAB key. Its main purpose
  5834. is outline visibility cycling, but it also invokes other actions
  5835. in special contexts.
  5836. - When this function is called with a prefix argument, rotate the entire
  5837. buffer through 3 states (global cycling)
  5838. 1. OVERVIEW: Show only top-level headlines.
  5839. 2. CONTENTS: Show all headlines of all levels, but no body text.
  5840. 3. SHOW ALL: Show everything.
  5841. When called with two `C-u C-u' prefixes, switch to the startup visibility,
  5842. determined by the variable `org-startup-folded', and by any VISIBILITY
  5843. properties in the buffer.
  5844. When called with three `C-u C-u C-u' prefixed, show the entire buffer,
  5845. including any drawers.
  5846. - When inside a table, re-align the table and move to the next field.
  5847. - When point is at the beginning of a headline, rotate the subtree started
  5848. by this line through 3 different states (local cycling)
  5849. 1. FOLDED: Only the main headline is shown.
  5850. 2. CHILDREN: The main headline and the direct children are shown.
  5851. From this state, you can move to one of the children
  5852. and zoom in further.
  5853. 3. SUBTREE: Show the entire subtree, including body text.
  5854. If there is no subtree, switch directly from CHILDREN to FOLDED.
  5855. - When point is at the beginning of an empty headline and the variable
  5856. `org-cycle-level-after-item/entry-creation' is set, cycle the level
  5857. of the headline by demoting and promoting it to likely levels. This
  5858. speeds up creation document structure by pressing TAB once or several
  5859. times right after creating a new headline.
  5860. - When there is a numeric prefix, go up to a heading with level ARG, do
  5861. a `show-subtree' and return to the previous cursor position. If ARG
  5862. is negative, go up that many levels.
  5863. - When point is not at the beginning of a headline, execute the global
  5864. binding for TAB, which is re-indenting the line. See the option
  5865. `org-cycle-emulate-tab' for details.
  5866. - Special case: if point is at the beginning of the buffer and there is
  5867. no headline in line 1, this function will act as if called with prefix arg
  5868. (C-u TAB, same as S-TAB) also when called without prefix arg.
  5869. But only if also the variable `org-cycle-global-at-bob' is t."
  5870. (interactive "P")
  5871. (org-load-modules-maybe)
  5872. (unless (or (run-hook-with-args-until-success 'org-tab-first-hook)
  5873. (and org-cycle-level-after-item/entry-creation
  5874. (or (org-cycle-level)
  5875. (org-cycle-item-indentation))))
  5876. (let* ((limit-level
  5877. (or org-cycle-max-level
  5878. (and (boundp 'org-inlinetask-min-level)
  5879. org-inlinetask-min-level
  5880. (1- org-inlinetask-min-level))))
  5881. (nstars (and limit-level
  5882. (if org-odd-levels-only
  5883. (and limit-level (1- (* limit-level 2)))
  5884. limit-level)))
  5885. (org-outline-regexp
  5886. (if (not (derived-mode-p 'org-mode))
  5887. outline-regexp
  5888. (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ "))))
  5889. (bob-special (and org-cycle-global-at-bob (not arg) (bobp)
  5890. (not (looking-at org-outline-regexp))))
  5891. (org-cycle-hook
  5892. (if bob-special
  5893. (delq 'org-optimize-window-after-visibility-change
  5894. (copy-sequence org-cycle-hook))
  5895. org-cycle-hook))
  5896. (pos (point)))
  5897. (if (or bob-special (equal arg '(4)))
  5898. ;; special case: use global cycling
  5899. (setq arg t))
  5900. (cond
  5901. ((equal arg '(16))
  5902. (setq last-command 'dummy)
  5903. (org-set-startup-visibility)
  5904. (org-unlogged-message "Startup visibility, plus VISIBILITY properties"))
  5905. ((equal arg '(64))
  5906. (show-all)
  5907. (org-unlogged-message "Entire buffer visible, including drawers"))
  5908. ;; Try cdlatex TAB completion
  5909. ((org-try-cdlatex-tab))
  5910. ;; Table: enter it or move to the next field.
  5911. ((org-at-table-p 'any)
  5912. (if (org-at-table.el-p)
  5913. (message "Use C-c ' to edit table.el tables")
  5914. (if arg (org-table-edit-field t)
  5915. (org-table-justify-field-maybe)
  5916. (call-interactively 'org-table-next-field))))
  5917. ((run-hook-with-args-until-success
  5918. 'org-tab-after-check-for-table-hook))
  5919. ;; Global cycling: delegate to `org-cycle-internal-global'.
  5920. ((eq arg t) (org-cycle-internal-global))
  5921. ;; Drawers: delegate to `org-flag-drawer'.
  5922. ((and org-drawers org-drawer-regexp
  5923. (save-excursion
  5924. (beginning-of-line 1)
  5925. (looking-at org-drawer-regexp)))
  5926. (org-flag-drawer ; toggle block visibility
  5927. (not (get-char-property (match-end 0) 'invisible))))
  5928. ;; Show-subtree, ARG levels up from here.
  5929. ((integerp arg)
  5930. (save-excursion
  5931. (org-back-to-heading)
  5932. (outline-up-heading (if (< arg 0) (- arg)
  5933. (- (funcall outline-level) arg)))
  5934. (org-show-subtree)))
  5935. ;; Inline task: delegate to `org-inlinetask-toggle-visibility'.
  5936. ((and (featurep 'org-inlinetask)
  5937. (org-inlinetask-at-task-p)
  5938. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5939. (org-inlinetask-toggle-visibility))
  5940. ;; At an item/headline: delegate to `org-cycle-internal-local'.
  5941. ((and (or (and org-cycle-include-plain-lists (org-at-item-p))
  5942. (save-excursion (beginning-of-line 1)
  5943. (looking-at org-outline-regexp)))
  5944. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5945. (org-cycle-internal-local))
  5946. ;; From there: TAB emulation and template completion.
  5947. (buffer-read-only (org-back-to-heading))
  5948. ((run-hook-with-args-until-success
  5949. 'org-tab-after-check-for-cycling-hook))
  5950. ((org-try-structure-completion))
  5951. ((run-hook-with-args-until-success
  5952. 'org-tab-before-tab-emulation-hook))
  5953. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  5954. (or (not (bolp))
  5955. (not (looking-at org-outline-regexp))))
  5956. (call-interactively (global-key-binding "\t")))
  5957. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  5958. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  5959. (or (and (eq org-cycle-emulate-tab 'white)
  5960. (= (match-end 0) (point-at-eol)))
  5961. (and (eq org-cycle-emulate-tab 'whitestart)
  5962. (>= (match-end 0) pos))))
  5963. t
  5964. (eq org-cycle-emulate-tab t))
  5965. (call-interactively (global-key-binding "\t")))
  5966. (t (save-excursion
  5967. (org-back-to-heading)
  5968. (org-cycle)))))))
  5969. (defun org-cycle-internal-global ()
  5970. "Do the global cycling action."
  5971. ;; Hack to avoid display of messages for .org attachments in Gnus
  5972. (let ((ga (string-match "\\*fontification" (buffer-name))))
  5973. (cond
  5974. ((and (eq last-command this-command)
  5975. (eq org-cycle-global-status 'overview))
  5976. ;; We just created the overview - now do table of contents
  5977. ;; This can be slow in very large buffers, so indicate action
  5978. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  5979. (unless ga (org-unlogged-message "CONTENTS..."))
  5980. (org-content)
  5981. (unless ga (org-unlogged-message "CONTENTS...done"))
  5982. (setq org-cycle-global-status 'contents)
  5983. (run-hook-with-args 'org-cycle-hook 'contents))
  5984. ((and (eq last-command this-command)
  5985. (eq org-cycle-global-status 'contents))
  5986. ;; We just showed the table of contents - now show everything
  5987. (run-hook-with-args 'org-pre-cycle-hook 'all)
  5988. (show-all)
  5989. (unless ga (org-unlogged-message "SHOW ALL"))
  5990. (setq org-cycle-global-status 'all)
  5991. (run-hook-with-args 'org-cycle-hook 'all))
  5992. (t
  5993. ;; Default action: go to overview
  5994. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  5995. (org-overview)
  5996. (unless ga (org-unlogged-message "OVERVIEW"))
  5997. (setq org-cycle-global-status 'overview)
  5998. (run-hook-with-args 'org-cycle-hook 'overview)))))
  5999. (defvar org-called-with-limited-levels nil
  6000. "Non-nil when `org-with-limited-levels' is currently active.")
  6001. (defun org-cycle-internal-local ()
  6002. "Do the local cycling action."
  6003. (let ((goal-column 0) eoh eol eos has-children children-skipped struct)
  6004. ;; First, determine end of headline (EOH), end of subtree or item
  6005. ;; (EOS), and if item or heading has children (HAS-CHILDREN).
  6006. (save-excursion
  6007. (if (org-at-item-p)
  6008. (progn
  6009. (beginning-of-line)
  6010. (setq struct (org-list-struct))
  6011. (setq eoh (point-at-eol))
  6012. (setq eos (org-list-get-item-end-before-blank (point) struct))
  6013. (setq has-children (org-list-has-child-p (point) struct)))
  6014. (org-back-to-heading)
  6015. (setq eoh (save-excursion (outline-end-of-heading) (point)))
  6016. (setq eos (save-excursion (org-end-of-subtree t t)
  6017. (when (bolp) (backward-char)) (point)))
  6018. (setq has-children
  6019. (or (save-excursion
  6020. (let ((level (funcall outline-level)))
  6021. (outline-next-heading)
  6022. (and (org-at-heading-p t)
  6023. (> (funcall outline-level) level))))
  6024. (save-excursion
  6025. (org-list-search-forward (org-item-beginning-re) eos t)))))
  6026. ;; Determine end invisible part of buffer (EOL)
  6027. (beginning-of-line 2)
  6028. ;; XEmacs doesn't have `next-single-char-property-change'
  6029. (if (featurep 'xemacs)
  6030. (while (and (not (eobp)) ;; this is like `next-line'
  6031. (get-char-property (1- (point)) 'invisible))
  6032. (beginning-of-line 2))
  6033. (while (and (not (eobp)) ;; this is like `next-line'
  6034. (get-char-property (1- (point)) 'invisible))
  6035. (goto-char (next-single-char-property-change (point) 'invisible))
  6036. (and (eolp) (beginning-of-line 2))))
  6037. (setq eol (point)))
  6038. ;; Find out what to do next and set `this-command'
  6039. (cond
  6040. ((= eos eoh)
  6041. ;; Nothing is hidden behind this heading
  6042. (unless (org-before-first-heading-p)
  6043. (run-hook-with-args 'org-pre-cycle-hook 'empty))
  6044. (org-unlogged-message "EMPTY ENTRY")
  6045. (setq org-cycle-subtree-status nil)
  6046. (save-excursion
  6047. (goto-char eos)
  6048. (outline-next-heading)
  6049. (if (outline-invisible-p) (org-flag-heading nil))))
  6050. ((and (or (>= eol eos)
  6051. (not (string-match "\\S-" (buffer-substring eol eos))))
  6052. (or has-children
  6053. (not (setq children-skipped
  6054. org-cycle-skip-children-state-if-no-children))))
  6055. ;; Entire subtree is hidden in one line: children view
  6056. (unless (org-before-first-heading-p)
  6057. (run-hook-with-args 'org-pre-cycle-hook 'children))
  6058. (if (org-at-item-p)
  6059. (org-list-set-item-visibility (point-at-bol) struct 'children)
  6060. (org-show-entry)
  6061. (org-with-limited-levels (show-children))
  6062. ;; FIXME: This slows down the func way too much.
  6063. ;; How keep drawers hidden in subtree anyway?
  6064. ;; (when (memq 'org-cycle-hide-drawers org-cycle-hook)
  6065. ;; (org-cycle-hide-drawers 'subtree))
  6066. ;; Fold every list in subtree to top-level items.
  6067. (when (eq org-cycle-include-plain-lists 'integrate)
  6068. (save-excursion
  6069. (org-back-to-heading)
  6070. (while (org-list-search-forward (org-item-beginning-re) eos t)
  6071. (beginning-of-line 1)
  6072. (let* ((struct (org-list-struct))
  6073. (prevs (org-list-prevs-alist struct))
  6074. (end (org-list-get-bottom-point struct)))
  6075. (mapc (lambda (e) (org-list-set-item-visibility e struct 'folded))
  6076. (org-list-get-all-items (point) struct prevs))
  6077. (goto-char (if (< end eos) end eos)))))))
  6078. (org-unlogged-message "CHILDREN")
  6079. (save-excursion
  6080. (goto-char eos)
  6081. (outline-next-heading)
  6082. (if (outline-invisible-p) (org-flag-heading nil)))
  6083. (setq org-cycle-subtree-status 'children)
  6084. (unless (org-before-first-heading-p)
  6085. (run-hook-with-args 'org-cycle-hook 'children)))
  6086. ((or children-skipped
  6087. (and (eq last-command this-command)
  6088. (eq org-cycle-subtree-status 'children)))
  6089. ;; We just showed the children, or no children are there,
  6090. ;; now show everything.
  6091. (unless (org-before-first-heading-p)
  6092. (run-hook-with-args 'org-pre-cycle-hook 'subtree))
  6093. (outline-flag-region eoh eos nil)
  6094. (org-unlogged-message
  6095. (if children-skipped "SUBTREE (NO CHILDREN)" "SUBTREE"))
  6096. (setq org-cycle-subtree-status 'subtree)
  6097. (unless (org-before-first-heading-p)
  6098. (run-hook-with-args 'org-cycle-hook 'subtree)))
  6099. (t
  6100. ;; Default action: hide the subtree.
  6101. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  6102. (outline-flag-region eoh eos t)
  6103. (org-unlogged-message "FOLDED")
  6104. (setq org-cycle-subtree-status 'folded)
  6105. (unless (org-before-first-heading-p)
  6106. (run-hook-with-args 'org-cycle-hook 'folded))))))
  6107. ;;;###autoload
  6108. (defun org-global-cycle (&optional arg)
  6109. "Cycle the global visibility. For details see `org-cycle'.
  6110. With \\[universal-argument] prefix arg, switch to startup visibility.
  6111. With a numeric prefix, show all headlines up to that level."
  6112. (interactive "P")
  6113. (let ((org-cycle-include-plain-lists
  6114. (if (derived-mode-p 'org-mode) org-cycle-include-plain-lists nil)))
  6115. (cond
  6116. ((integerp arg)
  6117. (show-all)
  6118. (hide-sublevels arg)
  6119. (setq org-cycle-global-status 'contents))
  6120. ((equal arg '(4))
  6121. (org-set-startup-visibility)
  6122. (org-unlogged-message "Startup visibility, plus VISIBILITY properties."))
  6123. (t
  6124. (org-cycle '(4))))))
  6125. (defun org-set-startup-visibility ()
  6126. "Set the visibility required by startup options and properties."
  6127. (cond
  6128. ((eq org-startup-folded t)
  6129. (org-overview))
  6130. ((eq org-startup-folded 'content)
  6131. (org-content))
  6132. ((or (eq org-startup-folded 'showeverything)
  6133. (eq org-startup-folded nil))
  6134. (show-all)))
  6135. (unless (eq org-startup-folded 'showeverything)
  6136. (if org-hide-block-startup (org-hide-block-all))
  6137. (org-set-visibility-according-to-property 'no-cleanup)
  6138. (org-cycle-hide-archived-subtrees 'all)
  6139. (org-cycle-hide-drawers 'all)
  6140. (org-cycle-show-empty-lines t)))
  6141. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  6142. "Switch subtree visibilities according to :VISIBILITY: property."
  6143. (interactive)
  6144. (let (org-show-entry-below state)
  6145. (save-excursion
  6146. (goto-char (point-min))
  6147. (while (re-search-forward
  6148. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  6149. nil t)
  6150. (setq state (match-string 1))
  6151. (save-excursion
  6152. (org-back-to-heading t)
  6153. (hide-subtree)
  6154. (org-reveal)
  6155. (cond
  6156. ((equal state '("fold" "folded"))
  6157. (hide-subtree))
  6158. ((equal state "children")
  6159. (org-show-hidden-entry)
  6160. (show-children))
  6161. ((equal state "content")
  6162. (save-excursion
  6163. (save-restriction
  6164. (org-narrow-to-subtree)
  6165. (org-content))))
  6166. ((member state '("all" "showall"))
  6167. (show-subtree)))))
  6168. (unless no-cleanup
  6169. (org-cycle-hide-archived-subtrees 'all)
  6170. (org-cycle-hide-drawers 'all)
  6171. (org-cycle-show-empty-lines 'all)))))
  6172. ;; This function uses outline-regexp instead of the more fundamental
  6173. ;; org-outline-regexp so that org-cycle-global works outside of Org
  6174. ;; buffers, where outline-regexp is needed.
  6175. (defun org-overview ()
  6176. "Switch to overview mode, showing only top-level headlines.
  6177. This shows all headlines with a level equal or greater than the level
  6178. of the first headline in the buffer. This is important, because if the
  6179. first headline is not level one, then (hide-sublevels 1) gives confusing
  6180. results."
  6181. (interactive)
  6182. (save-excursion
  6183. (let ((level
  6184. (save-excursion
  6185. (goto-char (point-min))
  6186. (if (re-search-forward (concat "^" outline-regexp) nil t)
  6187. (progn
  6188. (goto-char (match-beginning 0))
  6189. (funcall outline-level))))))
  6190. (and level (hide-sublevels level)))))
  6191. (defun org-content (&optional arg)
  6192. "Show all headlines in the buffer, like a table of contents.
  6193. With numerical argument N, show content up to level N."
  6194. (interactive "P")
  6195. (org-overview)
  6196. (save-excursion
  6197. ;; Visit all headings and show their offspring
  6198. (and (integerp arg) (org-overview))
  6199. (goto-char (point-max))
  6200. (catch 'exit
  6201. (while (and (progn (condition-case nil
  6202. (outline-previous-visible-heading 1)
  6203. (error (goto-char (point-min))))
  6204. t)
  6205. (looking-at org-outline-regexp))
  6206. (if (integerp arg)
  6207. (show-children (1- arg))
  6208. (show-branches))
  6209. (if (bobp) (throw 'exit nil))))))
  6210. (defun org-optimize-window-after-visibility-change (state)
  6211. "Adjust the window after a change in outline visibility.
  6212. This function is the default value of the hook `org-cycle-hook'."
  6213. (when (get-buffer-window (current-buffer))
  6214. (cond
  6215. ((eq state 'content) nil)
  6216. ((eq state 'all) nil)
  6217. ((eq state 'folded) nil)
  6218. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  6219. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  6220. (defun org-remove-empty-overlays-at (pos)
  6221. "Remove outline overlays that do not contain non-white stuff."
  6222. (mapc
  6223. (lambda (o)
  6224. (and (eq 'outline (overlay-get o 'invisible))
  6225. (not (string-match "\\S-" (buffer-substring (overlay-start o)
  6226. (overlay-end o))))
  6227. (delete-overlay o)))
  6228. (overlays-at pos)))
  6229. (defun org-clean-visibility-after-subtree-move ()
  6230. "Fix visibility issues after moving a subtree."
  6231. ;; First, find a reasonable region to look at:
  6232. ;; Start two siblings above, end three below
  6233. (let* ((beg (save-excursion
  6234. (and (org-get-last-sibling)
  6235. (org-get-last-sibling))
  6236. (point)))
  6237. (end (save-excursion
  6238. (and (org-get-next-sibling)
  6239. (org-get-next-sibling)
  6240. (org-get-next-sibling))
  6241. (if (org-at-heading-p)
  6242. (point-at-eol)
  6243. (point))))
  6244. (level (looking-at "\\*+"))
  6245. (re (if level (concat "^" (regexp-quote (match-string 0)) " "))))
  6246. (save-excursion
  6247. (save-restriction
  6248. (narrow-to-region beg end)
  6249. (when re
  6250. ;; Properly fold already folded siblings
  6251. (goto-char (point-min))
  6252. (while (re-search-forward re nil t)
  6253. (if (and (not (outline-invisible-p))
  6254. (save-excursion
  6255. (goto-char (point-at-eol)) (outline-invisible-p)))
  6256. (hide-entry))))
  6257. (org-cycle-show-empty-lines 'overview)
  6258. (org-cycle-hide-drawers 'overview)))))
  6259. (defun org-cycle-show-empty-lines (state)
  6260. "Show empty lines above all visible headlines.
  6261. The region to be covered depends on STATE when called through
  6262. `org-cycle-hook'. Lisp program can use t for STATE to get the
  6263. entire buffer covered. Note that an empty line is only shown if there
  6264. are at least `org-cycle-separator-lines' empty lines before the headline."
  6265. (when (not (= org-cycle-separator-lines 0))
  6266. (save-excursion
  6267. (let* ((n (abs org-cycle-separator-lines))
  6268. (re (cond
  6269. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  6270. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  6271. (t (let ((ns (number-to-string (- n 2))))
  6272. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  6273. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  6274. beg end b e)
  6275. (cond
  6276. ((memq state '(overview contents t))
  6277. (setq beg (point-min) end (point-max)))
  6278. ((memq state '(children folded))
  6279. (setq beg (point) end (progn (org-end-of-subtree t t)
  6280. (beginning-of-line 2)
  6281. (point)))))
  6282. (when beg
  6283. (goto-char beg)
  6284. (while (re-search-forward re end t)
  6285. (unless (get-char-property (match-end 1) 'invisible)
  6286. (setq e (match-end 1))
  6287. (if (< org-cycle-separator-lines 0)
  6288. (setq b (save-excursion
  6289. (goto-char (match-beginning 0))
  6290. (org-back-over-empty-lines)
  6291. (if (save-excursion
  6292. (goto-char (max (point-min) (1- (point))))
  6293. (org-at-heading-p))
  6294. (1- (point))
  6295. (point))))
  6296. (setq b (match-beginning 1)))
  6297. (outline-flag-region b e nil)))))))
  6298. ;; Never hide empty lines at the end of the file.
  6299. (save-excursion
  6300. (goto-char (point-max))
  6301. (outline-previous-heading)
  6302. (outline-end-of-heading)
  6303. (if (and (looking-at "[ \t\n]+")
  6304. (= (match-end 0) (point-max)))
  6305. (outline-flag-region (point) (match-end 0) nil))))
  6306. (defun org-show-empty-lines-in-parent ()
  6307. "Move to the parent and re-show empty lines before visible headlines."
  6308. (save-excursion
  6309. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  6310. (org-cycle-show-empty-lines context))))
  6311. (defun org-files-list ()
  6312. "Return `org-agenda-files' list, plus all open org-mode files.
  6313. This is useful for operations that need to scan all of a user's
  6314. open and agenda-wise Org files."
  6315. (let ((files (mapcar 'expand-file-name (org-agenda-files))))
  6316. (dolist (buf (buffer-list))
  6317. (with-current-buffer buf
  6318. (if (and (derived-mode-p 'org-mode) (buffer-file-name))
  6319. (let ((file (expand-file-name (buffer-file-name))))
  6320. (unless (member file files)
  6321. (push file files))))))
  6322. files))
  6323. (defsubst org-entry-beginning-position ()
  6324. "Return the beginning position of the current entry."
  6325. (save-excursion (outline-back-to-heading t) (point)))
  6326. (defsubst org-entry-end-position ()
  6327. "Return the end position of the current entry."
  6328. (save-excursion (outline-next-heading) (point)))
  6329. (defun org-cycle-hide-drawers (state)
  6330. "Re-hide all drawers after a visibility state change."
  6331. (when (and (derived-mode-p 'org-mode)
  6332. (not (memq state '(overview folded contents))))
  6333. (save-excursion
  6334. (let* ((globalp (memq state '(contents all)))
  6335. (beg (if globalp (point-min) (point)))
  6336. (end (if globalp (point-max)
  6337. (if (eq state 'children)
  6338. (save-excursion (outline-next-heading) (point))
  6339. (org-end-of-subtree t)))))
  6340. (goto-char beg)
  6341. (while (re-search-forward org-drawer-regexp end t)
  6342. (org-flag-drawer t))))))
  6343. (defun org-cycle-hide-inline-tasks (state)
  6344. "Re-hide inline tasks when switching to 'contents or 'children
  6345. visibility state."
  6346. (case state
  6347. (contents
  6348. (when (org-bound-and-true-p org-inlinetask-min-level)
  6349. (hide-sublevels (1- org-inlinetask-min-level))))
  6350. (children
  6351. (when (featurep 'org-inlinetask)
  6352. (save-excursion
  6353. (while (and (outline-next-heading)
  6354. (org-inlinetask-at-task-p))
  6355. (org-inlinetask-toggle-visibility)
  6356. (org-inlinetask-goto-end)))))))
  6357. (defun org-flag-drawer (flag)
  6358. "When FLAG is non-nil, hide the drawer we are within.
  6359. Otherwise make it visible."
  6360. (save-excursion
  6361. (beginning-of-line 1)
  6362. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  6363. (let ((b (match-end 0)))
  6364. (if (re-search-forward
  6365. "^[ \t]*:END:"
  6366. (save-excursion (outline-next-heading) (point)) t)
  6367. (outline-flag-region b (point-at-eol) flag)
  6368. (user-error ":END: line missing at position %s" b))))))
  6369. (defun org-subtree-end-visible-p ()
  6370. "Is the end of the current subtree visible?"
  6371. (pos-visible-in-window-p
  6372. (save-excursion (org-end-of-subtree t) (point))))
  6373. (defun org-first-headline-recenter ()
  6374. "Move cursor to the first headline and recenter the headline."
  6375. (goto-char (point-min))
  6376. (when (re-search-forward (concat "^\\(" org-outline-regexp "\\)") nil t)
  6377. (set-window-start (selected-window) (point-at-bol))))
  6378. ;;; Saving and restoring visibility
  6379. (defun org-outline-overlay-data (&optional use-markers)
  6380. "Return a list of the locations of all outline overlays.
  6381. These are overlays with the `invisible' property value `outline'.
  6382. The return value is a list of cons cells, with start and stop
  6383. positions for each overlay.
  6384. If USE-MARKERS is set, return the positions as markers."
  6385. (let (beg end)
  6386. (save-excursion
  6387. (save-restriction
  6388. (widen)
  6389. (delq nil
  6390. (mapcar (lambda (o)
  6391. (when (eq (overlay-get o 'invisible) 'outline)
  6392. (setq beg (overlay-start o)
  6393. end (overlay-end o))
  6394. (and beg end (> end beg)
  6395. (if use-markers
  6396. (cons (move-marker (make-marker) beg)
  6397. (move-marker (make-marker) end))
  6398. (cons beg end)))))
  6399. (overlays-in (point-min) (point-max))))))))
  6400. (defun org-set-outline-overlay-data (data)
  6401. "Create visibility overlays for all positions in DATA.
  6402. DATA should have been made by `org-outline-overlay-data'."
  6403. (let (o)
  6404. (save-excursion
  6405. (save-restriction
  6406. (widen)
  6407. (show-all)
  6408. (mapc (lambda (c)
  6409. (outline-flag-region (car c) (cdr c) t))
  6410. data)))))
  6411. ;;; Folding of blocks
  6412. (defvar org-hide-block-overlays nil
  6413. "Overlays hiding blocks.")
  6414. (make-variable-buffer-local 'org-hide-block-overlays)
  6415. (defun org-block-map (function &optional start end)
  6416. "Call FUNCTION at the head of all source blocks in the current buffer.
  6417. Optional arguments START and END can be used to limit the range."
  6418. (let ((start (or start (point-min)))
  6419. (end (or end (point-max))))
  6420. (save-excursion
  6421. (goto-char start)
  6422. (while (and (< (point) end) (re-search-forward org-block-regexp end t))
  6423. (save-excursion
  6424. (save-match-data
  6425. (goto-char (match-beginning 0))
  6426. (funcall function)))))))
  6427. (defun org-hide-block-toggle-all ()
  6428. "Toggle the visibility of all blocks in the current buffer."
  6429. (org-block-map #'org-hide-block-toggle))
  6430. (defun org-hide-block-all ()
  6431. "Fold all blocks in the current buffer."
  6432. (interactive)
  6433. (org-show-block-all)
  6434. (org-block-map #'org-hide-block-toggle-maybe))
  6435. (defun org-show-block-all ()
  6436. "Unfold all blocks in the current buffer."
  6437. (interactive)
  6438. (mapc 'delete-overlay org-hide-block-overlays)
  6439. (setq org-hide-block-overlays nil))
  6440. (defun org-hide-block-toggle-maybe ()
  6441. "Toggle visibility of block at point."
  6442. (interactive)
  6443. (let ((case-fold-search t))
  6444. (if (save-excursion
  6445. (beginning-of-line 1)
  6446. (looking-at org-block-regexp))
  6447. (progn (org-hide-block-toggle)
  6448. t) ;; to signal that we took action
  6449. nil))) ;; to signal that we did not
  6450. (defun org-hide-block-toggle (&optional force)
  6451. "Toggle the visibility of the current block."
  6452. (interactive)
  6453. (save-excursion
  6454. (beginning-of-line)
  6455. (if (re-search-forward org-block-regexp nil t)
  6456. (let ((start (- (match-beginning 4) 1)) ;; beginning of body
  6457. (end (match-end 0)) ;; end of entire body
  6458. ov)
  6459. (if (memq t (mapcar (lambda (overlay)
  6460. (eq (overlay-get overlay 'invisible)
  6461. 'org-hide-block))
  6462. (overlays-at start)))
  6463. (if (or (not force) (eq force 'off))
  6464. (mapc (lambda (ov)
  6465. (when (member ov org-hide-block-overlays)
  6466. (setq org-hide-block-overlays
  6467. (delq ov org-hide-block-overlays)))
  6468. (when (eq (overlay-get ov 'invisible)
  6469. 'org-hide-block)
  6470. (delete-overlay ov)))
  6471. (overlays-at start)))
  6472. (setq ov (make-overlay start end))
  6473. (overlay-put ov 'invisible 'org-hide-block)
  6474. ;; make the block accessible to isearch
  6475. (overlay-put
  6476. ov 'isearch-open-invisible
  6477. (lambda (ov)
  6478. (when (member ov org-hide-block-overlays)
  6479. (setq org-hide-block-overlays
  6480. (delq ov org-hide-block-overlays)))
  6481. (when (eq (overlay-get ov 'invisible)
  6482. 'org-hide-block)
  6483. (delete-overlay ov))))
  6484. (push ov org-hide-block-overlays)))
  6485. (user-error "Not looking at a source block"))))
  6486. ;; org-tab-after-check-for-cycling-hook
  6487. (add-hook 'org-tab-first-hook 'org-hide-block-toggle-maybe)
  6488. ;; Remove overlays when changing major mode
  6489. (add-hook 'org-mode-hook
  6490. (lambda () (org-add-hook 'change-major-mode-hook
  6491. 'org-show-block-all 'append 'local)))
  6492. ;;; Org-goto
  6493. (defvar org-goto-window-configuration nil)
  6494. (defvar org-goto-marker nil)
  6495. (defvar org-goto-map)
  6496. (defun org-goto-map ()
  6497. "Set the keymap `org-goto'."
  6498. (setq org-goto-map
  6499. (let ((map (make-sparse-keymap)))
  6500. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command
  6501. mouse-drag-region universal-argument org-occur))
  6502. cmd)
  6503. (while (setq cmd (pop cmds))
  6504. (substitute-key-definition cmd cmd map global-map)))
  6505. (suppress-keymap map)
  6506. (org-defkey map "\C-m" 'org-goto-ret)
  6507. (org-defkey map [(return)] 'org-goto-ret)
  6508. (org-defkey map [(left)] 'org-goto-left)
  6509. (org-defkey map [(right)] 'org-goto-right)
  6510. (org-defkey map [(control ?g)] 'org-goto-quit)
  6511. (org-defkey map "\C-i" 'org-cycle)
  6512. (org-defkey map [(tab)] 'org-cycle)
  6513. (org-defkey map [(down)] 'outline-next-visible-heading)
  6514. (org-defkey map [(up)] 'outline-previous-visible-heading)
  6515. (if org-goto-auto-isearch
  6516. (if (fboundp 'define-key-after)
  6517. (define-key-after map [t] 'org-goto-local-auto-isearch)
  6518. nil)
  6519. (org-defkey map "q" 'org-goto-quit)
  6520. (org-defkey map "n" 'outline-next-visible-heading)
  6521. (org-defkey map "p" 'outline-previous-visible-heading)
  6522. (org-defkey map "f" 'outline-forward-same-level)
  6523. (org-defkey map "b" 'outline-backward-same-level)
  6524. (org-defkey map "u" 'outline-up-heading))
  6525. (org-defkey map "/" 'org-occur)
  6526. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  6527. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  6528. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  6529. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  6530. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  6531. map)))
  6532. (defconst org-goto-help
  6533. "Browse buffer copy, to find location or copy text.%s
  6534. RET=jump to location C-g=quit and return to previous location
  6535. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  6536. (defvar org-goto-start-pos) ; dynamically scoped parameter
  6537. (defun org-goto (&optional alternative-interface)
  6538. "Look up a different location in the current file, keeping current visibility.
  6539. When you want look-up or go to a different location in a
  6540. document, the fastest way is often to fold the entire buffer and
  6541. then dive into the tree. This method has the disadvantage, that
  6542. the previous location will be folded, which may not be what you
  6543. want.
  6544. This command works around this by showing a copy of the current
  6545. buffer in an indirect buffer, in overview mode. You can dive
  6546. into the tree in that copy, use org-occur and incremental search
  6547. to find a location. When pressing RET or `Q', the command
  6548. returns to the original buffer in which the visibility is still
  6549. unchanged. After RET it will also jump to the location selected
  6550. in the indirect buffer and expose the headline hierarchy above.
  6551. With a prefix argument, use the alternative interface: e.g. if
  6552. `org-goto-interface' is 'outline use 'outline-path-completion."
  6553. (interactive "P")
  6554. (org-goto-map)
  6555. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  6556. (org-refile-use-outline-path t)
  6557. (org-refile-target-verify-function nil)
  6558. (interface
  6559. (if (not alternative-interface)
  6560. org-goto-interface
  6561. (if (eq org-goto-interface 'outline)
  6562. 'outline-path-completion
  6563. 'outline)))
  6564. (org-goto-start-pos (point))
  6565. (selected-point
  6566. (if (eq interface 'outline)
  6567. (car (org-get-location (current-buffer) org-goto-help))
  6568. (let ((pa (org-refile-get-location "Goto" nil nil t)))
  6569. (org-refile-check-position pa)
  6570. (nth 3 pa)))))
  6571. (if selected-point
  6572. (progn
  6573. (org-mark-ring-push org-goto-start-pos)
  6574. (goto-char selected-point)
  6575. (if (or (outline-invisible-p) (org-invisible-p2))
  6576. (org-show-context 'org-goto)))
  6577. (message "Quit"))))
  6578. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  6579. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  6580. (defvar org-goto-local-auto-isearch-map) ; defined below
  6581. (defun org-get-location (buf help)
  6582. "Let the user select a location in the Org-mode buffer BUF.
  6583. This function uses a recursive edit. It returns the selected position
  6584. or nil."
  6585. (org-no-popups
  6586. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  6587. (isearch-hide-immediately nil)
  6588. (isearch-search-fun-function
  6589. (lambda () 'org-goto-local-search-headings))
  6590. (org-goto-selected-point org-goto-exit-command))
  6591. (save-excursion
  6592. (save-window-excursion
  6593. (delete-other-windows)
  6594. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  6595. (org-pop-to-buffer-same-window
  6596. (condition-case nil
  6597. (make-indirect-buffer (current-buffer) "*org-goto*")
  6598. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  6599. (with-output-to-temp-buffer "*Org Help*"
  6600. (princ (format help (if org-goto-auto-isearch
  6601. " Just type for auto-isearch."
  6602. " n/p/f/b/u to navigate, q to quit."))))
  6603. (org-fit-window-to-buffer (get-buffer-window "*Org Help*"))
  6604. (setq buffer-read-only nil)
  6605. (let ((org-startup-truncated t)
  6606. (org-startup-folded nil)
  6607. (org-startup-align-all-tables nil))
  6608. (org-mode)
  6609. (org-overview))
  6610. (setq buffer-read-only t)
  6611. (if (and (boundp 'org-goto-start-pos)
  6612. (integer-or-marker-p org-goto-start-pos))
  6613. (let ((org-show-hierarchy-above t)
  6614. (org-show-siblings t)
  6615. (org-show-following-heading t))
  6616. (goto-char org-goto-start-pos)
  6617. (and (outline-invisible-p) (org-show-context)))
  6618. (goto-char (point-min)))
  6619. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  6620. (message "Select location and press RET")
  6621. (use-local-map org-goto-map)
  6622. (recursive-edit)))
  6623. (kill-buffer "*org-goto*")
  6624. (cons org-goto-selected-point org-goto-exit-command))))
  6625. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  6626. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  6627. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  6628. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  6629. (defun org-goto-local-search-headings (string bound noerror)
  6630. "Search and make sure that any matches are in headlines."
  6631. (catch 'return
  6632. (while (if isearch-forward
  6633. (search-forward string bound noerror)
  6634. (search-backward string bound noerror))
  6635. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  6636. (and (member :headline context)
  6637. (not (member :tags context))))
  6638. (throw 'return (point))))))
  6639. (defun org-goto-local-auto-isearch ()
  6640. "Start isearch."
  6641. (interactive)
  6642. (goto-char (point-min))
  6643. (let ((keys (this-command-keys)))
  6644. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  6645. (isearch-mode t)
  6646. (isearch-process-search-char (string-to-char keys)))))
  6647. (defun org-goto-ret (&optional arg)
  6648. "Finish `org-goto' by going to the new location."
  6649. (interactive "P")
  6650. (setq org-goto-selected-point (point)
  6651. org-goto-exit-command 'return)
  6652. (throw 'exit nil))
  6653. (defun org-goto-left ()
  6654. "Finish `org-goto' by going to the new location."
  6655. (interactive)
  6656. (if (org-at-heading-p)
  6657. (progn
  6658. (beginning-of-line 1)
  6659. (setq org-goto-selected-point (point)
  6660. org-goto-exit-command 'left)
  6661. (throw 'exit nil))
  6662. (user-error "Not on a heading")))
  6663. (defun org-goto-right ()
  6664. "Finish `org-goto' by going to the new location."
  6665. (interactive)
  6666. (if (org-at-heading-p)
  6667. (progn
  6668. (setq org-goto-selected-point (point)
  6669. org-goto-exit-command 'right)
  6670. (throw 'exit nil))
  6671. (user-error "Not on a heading")))
  6672. (defun org-goto-quit ()
  6673. "Finish `org-goto' without cursor motion."
  6674. (interactive)
  6675. (setq org-goto-selected-point nil)
  6676. (setq org-goto-exit-command 'quit)
  6677. (throw 'exit nil))
  6678. ;;; Indirect buffer display of subtrees
  6679. (defvar org-indirect-dedicated-frame nil
  6680. "This is the frame being used for indirect tree display.")
  6681. (defvar org-last-indirect-buffer nil)
  6682. (defun org-tree-to-indirect-buffer (&optional arg)
  6683. "Create indirect buffer and narrow it to current subtree.
  6684. With a numerical prefix ARG, go up to this level and then take that tree.
  6685. If ARG is negative, go up that many levels.
  6686. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  6687. indirect buffer previously made with this command, to avoid proliferation of
  6688. indirect buffers. However, when you call the command with a \
  6689. \\[universal-argument] prefix, or
  6690. when `org-indirect-buffer-display' is `new-frame', the last buffer
  6691. is kept so that you can work with several indirect buffers at the same time.
  6692. If `org-indirect-buffer-display' is `dedicated-frame', the \
  6693. \\[universal-argument] prefix also
  6694. requests that a new frame be made for the new buffer, so that the dedicated
  6695. frame is not changed."
  6696. (interactive "P")
  6697. (let ((cbuf (current-buffer))
  6698. (cwin (selected-window))
  6699. (pos (point))
  6700. beg end level heading ibuf)
  6701. (save-excursion
  6702. (org-back-to-heading t)
  6703. (when (numberp arg)
  6704. (setq level (org-outline-level))
  6705. (if (< arg 0) (setq arg (+ level arg)))
  6706. (while (> (setq level (org-outline-level)) arg)
  6707. (org-up-heading-safe)))
  6708. (setq beg (point)
  6709. heading (org-get-heading))
  6710. (org-end-of-subtree t t)
  6711. (if (org-at-heading-p) (backward-char 1))
  6712. (setq end (point)))
  6713. (if (and (buffer-live-p org-last-indirect-buffer)
  6714. (not (eq org-indirect-buffer-display 'new-frame))
  6715. (not arg))
  6716. (kill-buffer org-last-indirect-buffer))
  6717. (setq ibuf (org-get-indirect-buffer cbuf)
  6718. org-last-indirect-buffer ibuf)
  6719. (cond
  6720. ((or (eq org-indirect-buffer-display 'new-frame)
  6721. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  6722. (select-frame (make-frame))
  6723. (delete-other-windows)
  6724. (org-pop-to-buffer-same-window ibuf)
  6725. (org-set-frame-title heading))
  6726. ((eq org-indirect-buffer-display 'dedicated-frame)
  6727. (raise-frame
  6728. (select-frame (or (and org-indirect-dedicated-frame
  6729. (frame-live-p org-indirect-dedicated-frame)
  6730. org-indirect-dedicated-frame)
  6731. (setq org-indirect-dedicated-frame (make-frame)))))
  6732. (delete-other-windows)
  6733. (org-pop-to-buffer-same-window ibuf)
  6734. (org-set-frame-title (concat "Indirect: " heading)))
  6735. ((eq org-indirect-buffer-display 'current-window)
  6736. (org-pop-to-buffer-same-window ibuf))
  6737. ((eq org-indirect-buffer-display 'other-window)
  6738. (pop-to-buffer ibuf))
  6739. (t (error "Invalid value")))
  6740. (if (featurep 'xemacs)
  6741. (save-excursion (org-mode) (turn-on-font-lock)))
  6742. (narrow-to-region beg end)
  6743. (show-all)
  6744. (goto-char pos)
  6745. (run-hook-with-args 'org-cycle-hook 'all)
  6746. (and (window-live-p cwin) (select-window cwin))))
  6747. (defun org-get-indirect-buffer (&optional buffer)
  6748. (setq buffer (or buffer (current-buffer)))
  6749. (let ((n 1) (base (buffer-name buffer)) bname)
  6750. (while (buffer-live-p
  6751. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  6752. (setq n (1+ n)))
  6753. (condition-case nil
  6754. (make-indirect-buffer buffer bname 'clone)
  6755. (error (make-indirect-buffer buffer bname)))))
  6756. (defun org-set-frame-title (title)
  6757. "Set the title of the current frame to the string TITLE."
  6758. ;; FIXME: how to name a single frame in XEmacs???
  6759. (unless (featurep 'xemacs)
  6760. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  6761. ;;;; Structure editing
  6762. ;;; Inserting headlines
  6763. (defun org-previous-line-empty-p (&optional next)
  6764. "Is the previous line a blank line?
  6765. When NEXT is non-nil, check the next line instead."
  6766. (save-excursion
  6767. (and (not (bobp))
  6768. (or (beginning-of-line (if next 2 0)) t)
  6769. (save-match-data
  6770. (looking-at "[ \t]*$")))))
  6771. (defun org-insert-heading (&optional arg invisible-ok)
  6772. "Insert a new heading or an item with the same depth at point.
  6773. If point is at the beginning of a heading or a list item, insert
  6774. a new heading or a new item above the current one. If point is
  6775. at the beginning of a normal line, turn the line into a heading.
  6776. If point is in the middle of a headline or a list item, split the
  6777. headline or the item and create a new headline/item with the text
  6778. in the current line after point \(see `org-M-RET-may-split-line'
  6779. on how to modify this behavior).
  6780. With one universal prefirx argument, set the user option
  6781. `org-insert-heading-respect-content' to t for the duration of
  6782. the command. This modifies the behavior described above in this
  6783. ways: on list items and at the beginning of normal lines, force
  6784. the insertion of a heading after the current subtree.
  6785. With two universal prefix arguments, insert the heading at the
  6786. end of the grandparent subtree. For example, if point is within
  6787. a 2nd-level heading, then it will insert a 2nd-level heading at
  6788. the end of the 1st-level parent heading.
  6789. When INVISIBLE-OK is set, stop at invisible headlines when going
  6790. back. This is important for non-interactive uses of the
  6791. command."
  6792. (interactive "P")
  6793. (if (org-called-interactively-p 'any) (org-reveal))
  6794. (let ((itemp (org-in-item-p))
  6795. (may-split (org-get-alist-option org-M-RET-may-split-line 'headline))
  6796. (respect-content (or org-insert-heading-respect-content
  6797. (equal arg '(4))))
  6798. (initial-content "")
  6799. (adjust-empty-lines t))
  6800. (cond
  6801. ((or (= (buffer-size) 0)
  6802. (and (not (save-excursion
  6803. (and (ignore-errors (org-back-to-heading invisible-ok))
  6804. (org-at-heading-p))))
  6805. (or arg (not itemp))))
  6806. ;; At beginning of buffer or so high up that only a heading
  6807. ;; makes sense.
  6808. (cond ((and (bolp) (not respect-content)) (insert "* "))
  6809. ((not respect-content)
  6810. (unless may-split (end-of-line))
  6811. (insert "\n* "))
  6812. ((re-search-forward org-outline-regexp-bol nil t)
  6813. (beginning-of-line)
  6814. (insert "* \n")
  6815. (backward-char))
  6816. (t (goto-char (point-max))
  6817. (insert "\n* ")))
  6818. (run-hooks 'org-insert-heading-hook))
  6819. ((and itemp (not (member arg '((4) (16)))))
  6820. ;; Insert an item
  6821. (org-insert-item))
  6822. (t
  6823. ;; Maybe move at the end of the subtree
  6824. (when (equal arg '(16))
  6825. (org-up-heading-safe)
  6826. (org-end-of-subtree t))
  6827. ;; Insert a heading
  6828. (save-restriction
  6829. (widen)
  6830. (let* ((level nil)
  6831. (on-heading (org-at-heading-p))
  6832. (empty-line-p (if on-heading
  6833. (org-previous-line-empty-p)
  6834. ;; We will decide later
  6835. nil))
  6836. ;; Get a level string to fall back on
  6837. (fix-level
  6838. (if (org-before-first-heading-p) "*"
  6839. (save-excursion
  6840. (org-back-to-heading t)
  6841. (if (org-previous-line-empty-p) (setq empty-line-p t))
  6842. (looking-at org-outline-regexp)
  6843. (make-string (1- (length (match-string 0))) ?*))))
  6844. (stars
  6845. (save-excursion
  6846. (condition-case nil
  6847. (progn
  6848. (org-back-to-heading invisible-ok)
  6849. (when (and (not on-heading)
  6850. (featurep 'org-inlinetask)
  6851. (integerp org-inlinetask-min-level)
  6852. (>= (length (match-string 0))
  6853. org-inlinetask-min-level))
  6854. ;; Find a heading level before the inline task
  6855. (while (and (setq level (org-up-heading-safe))
  6856. (>= level org-inlinetask-min-level)))
  6857. (if (org-at-heading-p)
  6858. (org-back-to-heading invisible-ok)
  6859. (error "This should not happen")))
  6860. (unless (and (save-excursion
  6861. (save-match-data
  6862. (org-backward-heading-same-level
  6863. 1 invisible-ok))
  6864. (= (point) (match-beginning 0)))
  6865. (not (org-previous-line-empty-p t)))
  6866. (setq empty-line-p (or empty-line-p
  6867. (org-previous-line-empty-p))))
  6868. (match-string 0))
  6869. (error (or fix-level "* ")))))
  6870. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  6871. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  6872. pos hide-previous previous-pos)
  6873. ;; If we insert after content, move there and clean up whitespace
  6874. (when (and respect-content
  6875. (not (org-looking-at-p org-outline-regexp-bol)))
  6876. (if (not (org-before-first-heading-p))
  6877. (org-end-of-subtree nil t)
  6878. (re-search-forward org-outline-regexp-bol)
  6879. (beginning-of-line 0))
  6880. (skip-chars-backward " \r\n")
  6881. (and (not (looking-back "^\*+"))
  6882. (looking-at "[ \t]+") (replace-match ""))
  6883. (unless (eobp) (forward-char 1))
  6884. (when (looking-at "^\\*")
  6885. (unless (bobp) (backward-char 1))
  6886. (insert "\n")))
  6887. ;; If we are splitting, grab the text that should be moved to the new headline
  6888. (when may-split
  6889. (if (org-on-heading-p)
  6890. ;; This is a heading, we split intelligently (keeping tags)
  6891. (let ((pos (point)))
  6892. (goto-char (point-at-bol))
  6893. (unless (looking-at org-complex-heading-regexp)
  6894. (error "This should not happen"))
  6895. (when (and (match-beginning 4)
  6896. (> pos (match-beginning 4))
  6897. (< pos (match-end 4)))
  6898. (setq initial-content (buffer-substring pos (match-end 4)))
  6899. (goto-char pos)
  6900. (delete-region (point) (match-end 4))
  6901. (if (looking-at "[ \t]*$")
  6902. (replace-match "")
  6903. (insert (make-string (length initial-content) ?\ )))
  6904. (setq initial-content (org-trim initial-content)))
  6905. (goto-char pos))
  6906. ;; a normal line
  6907. (setq initial-content
  6908. (org-trim (buffer-substring (point) (point-at-eol))))
  6909. (delete-region (point) (point-at-eol))))
  6910. ;; If we are at the beginning of the line, insert before it. Else after
  6911. (cond
  6912. ((and (bolp) (looking-at "[ \t]*$")))
  6913. ((and (bolp) (not (looking-at "[ \t]*$")))
  6914. (open-line 1))
  6915. (t
  6916. (goto-char (point-at-eol))
  6917. (insert "\n")))
  6918. ;; Insert the new heading
  6919. (insert stars)
  6920. (just-one-space)
  6921. (insert initial-content)
  6922. (when adjust-empty-lines
  6923. (if (or (not blank)
  6924. (and blank (not (org-previous-line-empty-p))))
  6925. (org-N-empty-lines-before-current (if blank 1 0))))
  6926. (run-hooks 'org-insert-heading-hook)))))))
  6927. (defun org-N-empty-lines-before-current (N)
  6928. "Make the number of empty lines before current exactly N.
  6929. So this will delete or add empty lines."
  6930. (save-excursion
  6931. (beginning-of-line)
  6932. (let ((p (point)))
  6933. (skip-chars-backward " \r\t\n")
  6934. (unless (bolp) (forward-line))
  6935. (delete-region (point) p))
  6936. (when (> N 0) (insert (make-string N ?\n)))))
  6937. (defun org-get-heading (&optional no-tags no-todo)
  6938. "Return the heading of the current entry, without the stars.
  6939. When NO-TAGS is non-nil, don't include tags.
  6940. When NO-TODO is non-nil, don't include TODO keywords."
  6941. (save-excursion
  6942. (org-back-to-heading t)
  6943. (cond
  6944. ((and no-tags no-todo)
  6945. (looking-at org-complex-heading-regexp)
  6946. (match-string 4))
  6947. (no-tags
  6948. (looking-at (concat org-outline-regexp
  6949. "\\(.*?\\)"
  6950. "\\(?:[ \t]+:[[:alnum:]:_@#%]+:\\)?[ \t]*$"))
  6951. (match-string 1))
  6952. (no-todo
  6953. (looking-at org-todo-line-regexp)
  6954. (match-string 3))
  6955. (t (looking-at org-heading-regexp)
  6956. (match-string 2)))))
  6957. (defvar orgstruct-mode) ; defined below
  6958. (defun org-heading-components ()
  6959. "Return the components of the current heading.
  6960. This is a list with the following elements:
  6961. - the level as an integer
  6962. - the reduced level, different if `org-odd-levels-only' is set.
  6963. - the TODO keyword, or nil
  6964. - the priority character, like ?A, or nil if no priority is given
  6965. - the headline text itself, or the tags string if no headline text
  6966. - the tags string, or nil."
  6967. (save-excursion
  6968. (org-back-to-heading t)
  6969. (if (let (case-fold-search)
  6970. (looking-at
  6971. (if orgstruct-mode
  6972. org-heading-regexp
  6973. org-complex-heading-regexp)))
  6974. (if orgstruct-mode
  6975. (list (length (match-string 1))
  6976. (org-reduced-level (length (match-string 1)))
  6977. nil
  6978. nil
  6979. (match-string 2)
  6980. nil)
  6981. (list (length (match-string 1))
  6982. (org-reduced-level (length (match-string 1)))
  6983. (org-match-string-no-properties 2)
  6984. (and (match-end 3) (aref (match-string 3) 2))
  6985. (org-match-string-no-properties 4)
  6986. (org-match-string-no-properties 5))))))
  6987. (defun org-get-entry ()
  6988. "Get the entry text, after heading, entire subtree."
  6989. (save-excursion
  6990. (org-back-to-heading t)
  6991. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  6992. (defun org-insert-heading-after-current ()
  6993. "Insert a new heading with same level as current, after current subtree."
  6994. (interactive)
  6995. (org-back-to-heading)
  6996. (org-insert-heading)
  6997. (org-move-subtree-down)
  6998. (end-of-line 1))
  6999. (defun org-insert-heading-respect-content (&optional invisible-ok)
  7000. "Insert heading with `org-insert-heading-respect-content' set to t."
  7001. (interactive)
  7002. (org-insert-heading '(4) invisible-ok))
  7003. (defun org-insert-todo-heading-respect-content (&optional force-state)
  7004. "Insert TODO heading with `org-insert-heading-respect-content' set to t."
  7005. (interactive)
  7006. (org-insert-todo-heading force-state '(4)))
  7007. (defun org-insert-todo-heading (arg &optional force-heading)
  7008. "Insert a new heading with the same level and TODO state as current heading.
  7009. If the heading has no TODO state, or if the state is DONE, use the first
  7010. state (TODO by default). Also with one prefix arg, force first state. With
  7011. two prefix args, force inserting at the end of the parent subtree."
  7012. (interactive "P")
  7013. (when (or force-heading (not (org-insert-item 'checkbox)))
  7014. (org-insert-heading (or (and (equal arg '(16)) '(16))
  7015. force-heading))
  7016. (save-excursion
  7017. (org-back-to-heading)
  7018. (outline-previous-heading)
  7019. (looking-at org-todo-line-regexp))
  7020. (let*
  7021. ((new-mark-x
  7022. (if (or (equal arg '(4))
  7023. (not (match-beginning 2))
  7024. (member (match-string 2) org-done-keywords))
  7025. (car org-todo-keywords-1)
  7026. (match-string 2)))
  7027. (new-mark
  7028. (or
  7029. (run-hook-with-args-until-success
  7030. 'org-todo-get-default-hook new-mark-x nil)
  7031. new-mark-x)))
  7032. (beginning-of-line 1)
  7033. (and (looking-at org-outline-regexp) (goto-char (match-end 0))
  7034. (if org-treat-insert-todo-heading-as-state-change
  7035. (org-todo new-mark)
  7036. (insert new-mark " "))))
  7037. (when org-provide-todo-statistics
  7038. (org-update-parent-todo-statistics))))
  7039. (defun org-insert-subheading (arg)
  7040. "Insert a new subheading and demote it.
  7041. Works for outline headings and for plain lists alike."
  7042. (interactive "P")
  7043. (org-insert-heading arg)
  7044. (cond
  7045. ((org-at-heading-p) (org-do-demote))
  7046. ((org-at-item-p) (org-indent-item))))
  7047. (defun org-insert-todo-subheading (arg)
  7048. "Insert a new subheading with TODO keyword or checkbox and demote it.
  7049. Works for outline headings and for plain lists alike."
  7050. (interactive "P")
  7051. (org-insert-todo-heading arg)
  7052. (cond
  7053. ((org-at-heading-p) (org-do-demote))
  7054. ((org-at-item-p) (org-indent-item))))
  7055. ;;; Promotion and Demotion
  7056. (defvar org-after-demote-entry-hook nil
  7057. "Hook run after an entry has been demoted.
  7058. The cursor will be at the beginning of the entry.
  7059. When a subtree is being demoted, the hook will be called for each node.")
  7060. (defvar org-after-promote-entry-hook nil
  7061. "Hook run after an entry has been promoted.
  7062. The cursor will be at the beginning of the entry.
  7063. When a subtree is being promoted, the hook will be called for each node.")
  7064. (defun org-promote-subtree ()
  7065. "Promote the entire subtree.
  7066. See also `org-promote'."
  7067. (interactive)
  7068. (save-excursion
  7069. (org-with-limited-levels (org-map-tree 'org-promote)))
  7070. (org-fix-position-after-promote))
  7071. (defun org-demote-subtree ()
  7072. "Demote the entire subtree. See `org-demote'.
  7073. See also `org-promote'."
  7074. (interactive)
  7075. (save-excursion
  7076. (org-with-limited-levels (org-map-tree 'org-demote)))
  7077. (org-fix-position-after-promote))
  7078. (defun org-do-promote ()
  7079. "Promote the current heading higher up the tree.
  7080. If the region is active in `transient-mark-mode', promote all headings
  7081. in the region."
  7082. (interactive)
  7083. (save-excursion
  7084. (if (org-region-active-p)
  7085. (org-map-region 'org-promote (region-beginning) (region-end))
  7086. (org-promote)))
  7087. (org-fix-position-after-promote))
  7088. (defun org-do-demote ()
  7089. "Demote the current heading lower down the tree.
  7090. If the region is active in `transient-mark-mode', demote all headings
  7091. in the region."
  7092. (interactive)
  7093. (save-excursion
  7094. (if (org-region-active-p)
  7095. (org-map-region 'org-demote (region-beginning) (region-end))
  7096. (org-demote)))
  7097. (org-fix-position-after-promote))
  7098. (defun org-fix-position-after-promote ()
  7099. "Make sure that after pro/demotion cursor position is right."
  7100. (let ((pos (point)))
  7101. (when (save-excursion
  7102. (beginning-of-line 1)
  7103. (looking-at org-todo-line-regexp)
  7104. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  7105. (cond ((eobp) (insert " "))
  7106. ((eolp) (insert " "))
  7107. ((equal (char-after) ?\ ) (forward-char 1))))))
  7108. (defun org-current-level ()
  7109. "Return the level of the current entry, or nil if before the first headline.
  7110. The level is the number of stars at the beginning of the headline."
  7111. (save-excursion
  7112. (org-with-limited-levels
  7113. (if (ignore-errors (org-back-to-heading t))
  7114. (funcall outline-level)))))
  7115. (defun org-get-previous-line-level ()
  7116. "Return the outline depth of the last headline before the current line.
  7117. Returns 0 for the first headline in the buffer, and nil if before the
  7118. first headline."
  7119. (and (org-current-level)
  7120. (or (and (/= (line-beginning-position) (point-min))
  7121. (save-excursion (beginning-of-line 0) (org-current-level)))
  7122. 0)))
  7123. (defun org-reduced-level (l)
  7124. "Compute the effective level of a heading.
  7125. This takes into account the setting of `org-odd-levels-only'."
  7126. (cond
  7127. ((zerop l) 0)
  7128. (org-odd-levels-only (1+ (floor (/ l 2))))
  7129. (t l)))
  7130. (defun org-level-increment ()
  7131. "Return the number of stars that will be added or removed at a
  7132. time to headlines when structure editing, based on the value of
  7133. `org-odd-levels-only'."
  7134. (if org-odd-levels-only 2 1))
  7135. (defun org-get-valid-level (level &optional change)
  7136. "Rectify a level change under the influence of `org-odd-levels-only'
  7137. LEVEL is a current level, CHANGE is by how much the level should be
  7138. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  7139. even level numbers will become the next higher odd number."
  7140. (if org-odd-levels-only
  7141. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  7142. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  7143. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  7144. (max 1 (+ level (or change 0)))))
  7145. (if (boundp 'define-obsolete-function-alias)
  7146. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  7147. (define-obsolete-function-alias 'org-get-legal-level
  7148. 'org-get-valid-level)
  7149. (define-obsolete-function-alias 'org-get-legal-level
  7150. 'org-get-valid-level "23.1")))
  7151. (defun org-promote ()
  7152. "Promote the current heading higher up the tree.
  7153. If the region is active in `transient-mark-mode', promote all headings
  7154. in the region."
  7155. (org-back-to-heading t)
  7156. (let* ((level (save-match-data (funcall outline-level)))
  7157. (after-change-functions (remove 'flyspell-after-change-function
  7158. after-change-functions))
  7159. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  7160. (diff (abs (- level (length up-head) -1))))
  7161. (cond ((and (= level 1) org-called-with-limited-levels
  7162. org-allow-promoting-top-level-subtree)
  7163. (replace-match "# " nil t))
  7164. ((= level 1)
  7165. (user-error "Cannot promote to level 0. UNDO to recover if necessary"))
  7166. (t (replace-match up-head nil t)))
  7167. ;; Fixup tag positioning
  7168. (unless (= level 1)
  7169. (and org-auto-align-tags (org-set-tags nil 'ignore-column))
  7170. (if org-adapt-indentation (org-fixup-indentation (- diff))))
  7171. (run-hooks 'org-after-promote-entry-hook)))
  7172. (defun org-demote ()
  7173. "Demote the current heading lower down the tree.
  7174. If the region is active in `transient-mark-mode', demote all headings
  7175. in the region."
  7176. (org-back-to-heading t)
  7177. (let* ((level (save-match-data (funcall outline-level)))
  7178. (after-change-functions (remove 'flyspell-after-change-function
  7179. after-change-functions))
  7180. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  7181. (diff (abs (- level (length down-head) -1))))
  7182. (replace-match down-head nil t)
  7183. ;; Fixup tag positioning
  7184. (and org-auto-align-tags (org-set-tags nil 'ignore-column))
  7185. (if org-adapt-indentation (org-fixup-indentation diff))
  7186. (run-hooks 'org-after-demote-entry-hook)))
  7187. (defun org-cycle-level ()
  7188. "Cycle the level of an empty headline through possible states.
  7189. This goes first to child, then to parent, level, then up the hierarchy.
  7190. After top level, it switches back to sibling level."
  7191. (interactive)
  7192. (let ((org-adapt-indentation nil))
  7193. (when (org-point-at-end-of-empty-headline)
  7194. (setq this-command 'org-cycle-level) ; Only needed for caching
  7195. (let ((cur-level (org-current-level))
  7196. (prev-level (org-get-previous-line-level)))
  7197. (cond
  7198. ;; If first headline in file, promote to top-level.
  7199. ((= prev-level 0)
  7200. (loop repeat (/ (- cur-level 1) (org-level-increment))
  7201. do (org-do-promote)))
  7202. ;; If same level as prev, demote one.
  7203. ((= prev-level cur-level)
  7204. (org-do-demote))
  7205. ;; If parent is top-level, promote to top level if not already.
  7206. ((= prev-level 1)
  7207. (loop repeat (/ (- cur-level 1) (org-level-increment))
  7208. do (org-do-promote)))
  7209. ;; If top-level, return to prev-level.
  7210. ((= cur-level 1)
  7211. (loop repeat (/ (- prev-level 1) (org-level-increment))
  7212. do (org-do-demote)))
  7213. ;; If less than prev-level, promote one.
  7214. ((< cur-level prev-level)
  7215. (org-do-promote))
  7216. ;; If deeper than prev-level, promote until higher than
  7217. ;; prev-level.
  7218. ((> cur-level prev-level)
  7219. (loop repeat (+ 1 (/ (- cur-level prev-level) (org-level-increment)))
  7220. do (org-do-promote))))
  7221. t))))
  7222. (defun org-map-tree (fun)
  7223. "Call FUN for every heading underneath the current one."
  7224. (org-back-to-heading)
  7225. (let ((level (funcall outline-level)))
  7226. (save-excursion
  7227. (funcall fun)
  7228. (while (and (progn
  7229. (outline-next-heading)
  7230. (> (funcall outline-level) level))
  7231. (not (eobp)))
  7232. (funcall fun)))))
  7233. (defun org-map-region (fun beg end)
  7234. "Call FUN for every heading between BEG and END."
  7235. (let ((org-ignore-region t))
  7236. (save-excursion
  7237. (setq end (copy-marker end))
  7238. (goto-char beg)
  7239. (if (and (re-search-forward org-outline-regexp-bol nil t)
  7240. (< (point) end))
  7241. (funcall fun))
  7242. (while (and (progn
  7243. (outline-next-heading)
  7244. (< (point) end))
  7245. (not (eobp)))
  7246. (funcall fun)))))
  7247. (defvar org-property-end-re) ; silence byte-compiler
  7248. (defun org-fixup-indentation (diff)
  7249. "Change the indentation in the current entry by DIFF.
  7250. However, if any line in the current entry has no indentation, or if it
  7251. would end up with no indentation after the change, nothing at all is done."
  7252. (save-excursion
  7253. (let ((end (save-excursion (outline-next-heading)
  7254. (point-marker)))
  7255. (prohibit (if (> diff 0)
  7256. "^\\S-"
  7257. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  7258. col)
  7259. (unless (save-excursion (end-of-line 1)
  7260. (re-search-forward prohibit end t))
  7261. (while (and (< (point) end)
  7262. (re-search-forward "^[ \t]+" end t))
  7263. (goto-char (match-end 0))
  7264. (setq col (current-column))
  7265. (if (< diff 0) (replace-match ""))
  7266. (org-indent-to-column (+ diff col))))
  7267. (move-marker end nil))))
  7268. (defun org-convert-to-odd-levels ()
  7269. "Convert an org-mode file with all levels allowed to one with odd levels.
  7270. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  7271. level 5 etc."
  7272. (interactive)
  7273. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  7274. (let ((outline-level 'org-outline-level)
  7275. (org-odd-levels-only nil) n)
  7276. (save-excursion
  7277. (goto-char (point-min))
  7278. (while (re-search-forward "^\\*\\*+ " nil t)
  7279. (setq n (- (length (match-string 0)) 2))
  7280. (while (>= (setq n (1- n)) 0)
  7281. (org-demote))
  7282. (end-of-line 1))))))
  7283. (defun org-convert-to-oddeven-levels ()
  7284. "Convert an org-mode file with only odd levels to one with odd/even levels.
  7285. This promotes level 3 to level 2, level 5 to level 3 etc. If the
  7286. file contains a section with an even level, conversion would
  7287. destroy the structure of the file. An error is signaled in this
  7288. case."
  7289. (interactive)
  7290. (goto-char (point-min))
  7291. ;; First check if there are no even levels
  7292. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  7293. (org-show-context t)
  7294. (error "Not all levels are odd in this file. Conversion not possible"))
  7295. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  7296. (let ((outline-regexp org-outline-regexp)
  7297. (outline-level 'org-outline-level)
  7298. (org-odd-levels-only nil) n)
  7299. (save-excursion
  7300. (goto-char (point-min))
  7301. (while (re-search-forward "^\\*\\*+ " nil t)
  7302. (setq n (/ (1- (length (match-string 0))) 2))
  7303. (while (>= (setq n (1- n)) 0)
  7304. (org-promote))
  7305. (end-of-line 1))))))
  7306. (defun org-tr-level (n)
  7307. "Make N odd if required."
  7308. (if org-odd-levels-only (1+ (/ n 2)) n))
  7309. ;;; Vertical tree motion, cutting and pasting of subtrees
  7310. (defun org-move-subtree-up (&optional arg)
  7311. "Move the current subtree up past ARG headlines of the same level."
  7312. (interactive "p")
  7313. (org-move-subtree-down (- (prefix-numeric-value arg))))
  7314. (defun org-move-subtree-down (&optional arg)
  7315. "Move the current subtree down past ARG headlines of the same level."
  7316. (interactive "p")
  7317. (setq arg (prefix-numeric-value arg))
  7318. (let ((movfunc (if (> arg 0) 'org-get-next-sibling
  7319. 'org-get-last-sibling))
  7320. (ins-point (make-marker))
  7321. (cnt (abs arg))
  7322. (col (current-column))
  7323. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  7324. ;; Select the tree
  7325. (org-back-to-heading)
  7326. (setq beg0 (point))
  7327. (save-excursion
  7328. (setq ne-beg (org-back-over-empty-lines))
  7329. (setq beg (point)))
  7330. (save-match-data
  7331. (save-excursion (outline-end-of-heading)
  7332. (setq folded (outline-invisible-p)))
  7333. (progn (org-end-of-subtree nil t)
  7334. (unless (eobp) (backward-char))))
  7335. (outline-next-heading)
  7336. (setq ne-end (org-back-over-empty-lines))
  7337. (setq end (point))
  7338. (goto-char beg0)
  7339. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  7340. ;; include less whitespace
  7341. (save-excursion
  7342. (goto-char beg)
  7343. (forward-line (- ne-beg ne-end))
  7344. (setq beg (point))))
  7345. ;; Find insertion point, with error handling
  7346. (while (> cnt 0)
  7347. (or (and (funcall movfunc) (looking-at org-outline-regexp))
  7348. (progn (goto-char beg0)
  7349. (user-error "Cannot move past superior level or buffer limit")))
  7350. (setq cnt (1- cnt)))
  7351. (if (> arg 0)
  7352. ;; Moving forward - still need to move over subtree
  7353. (progn (org-end-of-subtree t t)
  7354. (save-excursion
  7355. (org-back-over-empty-lines)
  7356. (or (bolp) (newline)))))
  7357. (setq ne-ins (org-back-over-empty-lines))
  7358. (move-marker ins-point (point))
  7359. (setq txt (buffer-substring beg end))
  7360. (org-save-markers-in-region beg end)
  7361. (delete-region beg end)
  7362. (org-remove-empty-overlays-at beg)
  7363. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  7364. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  7365. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  7366. (let ((bbb (point)))
  7367. (insert-before-markers txt)
  7368. (org-reinstall-markers-in-region bbb)
  7369. (move-marker ins-point bbb))
  7370. (or (bolp) (insert "\n"))
  7371. (setq ins-end (point))
  7372. (goto-char ins-point)
  7373. (org-skip-whitespace)
  7374. (when (and (< arg 0)
  7375. (org-first-sibling-p)
  7376. (> ne-ins ne-beg))
  7377. ;; Move whitespace back to beginning
  7378. (save-excursion
  7379. (goto-char ins-end)
  7380. (let ((kill-whole-line t))
  7381. (kill-line (- ne-ins ne-beg)) (point)))
  7382. (insert (make-string (- ne-ins ne-beg) ?\n)))
  7383. (move-marker ins-point nil)
  7384. (if folded
  7385. (hide-subtree)
  7386. (org-show-entry)
  7387. (show-children)
  7388. (org-cycle-hide-drawers 'children))
  7389. (org-clean-visibility-after-subtree-move)
  7390. ;; move back to the initial column we were at
  7391. (move-to-column col)))
  7392. (defvar org-subtree-clip ""
  7393. "Clipboard for cut and paste of subtrees.
  7394. This is actually only a copy of the kill, because we use the normal kill
  7395. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  7396. (defvar org-subtree-clip-folded nil
  7397. "Was the last copied subtree folded?
  7398. This is used to fold the tree back after pasting.")
  7399. (defun org-cut-subtree (&optional n)
  7400. "Cut the current subtree into the clipboard.
  7401. With prefix arg N, cut this many sequential subtrees.
  7402. This is a short-hand for marking the subtree and then cutting it."
  7403. (interactive "p")
  7404. (org-copy-subtree n 'cut))
  7405. (defun org-copy-subtree (&optional n cut force-store-markers nosubtrees)
  7406. "Copy the current subtree it in the clipboard.
  7407. With prefix arg N, copy this many sequential subtrees.
  7408. This is a short-hand for marking the subtree and then copying it.
  7409. If CUT is non-nil, actually cut the subtree.
  7410. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  7411. of some markers in the region, even if CUT is non-nil. This is
  7412. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  7413. (interactive "p")
  7414. (let (beg end folded (beg0 (point)))
  7415. (if (org-called-interactively-p 'any)
  7416. (org-back-to-heading nil) ; take what looks like a subtree
  7417. (org-back-to-heading t)) ; take what is really there
  7418. (setq beg (point))
  7419. (skip-chars-forward " \t\r\n")
  7420. (save-match-data
  7421. (if nosubtrees
  7422. (outline-next-heading)
  7423. (save-excursion (outline-end-of-heading)
  7424. (setq folded (outline-invisible-p)))
  7425. (condition-case nil
  7426. (org-forward-heading-same-level (1- n) t)
  7427. (error nil))
  7428. (org-end-of-subtree t t)))
  7429. (setq end (point))
  7430. (goto-char beg0)
  7431. (when (> end beg)
  7432. (setq org-subtree-clip-folded folded)
  7433. (when (or cut force-store-markers)
  7434. (org-save-markers-in-region beg end))
  7435. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  7436. (setq org-subtree-clip (current-kill 0))
  7437. (message "%s: Subtree(s) with %d characters"
  7438. (if cut "Cut" "Copied")
  7439. (length org-subtree-clip)))))
  7440. (defun org-paste-subtree (&optional level tree for-yank)
  7441. "Paste the clipboard as a subtree, with modification of headline level.
  7442. The entire subtree is promoted or demoted in order to match a new headline
  7443. level.
  7444. If the cursor is at the beginning of a headline, the same level as
  7445. that headline is used to paste the tree.
  7446. If not, the new level is derived from the *visible* headings
  7447. before and after the insertion point, and taken to be the inferior headline
  7448. level of the two. So if the previous visible heading is level 3 and the
  7449. next is level 4 (or vice versa), level 4 will be used for insertion.
  7450. This makes sure that the subtree remains an independent subtree and does
  7451. not swallow low level entries.
  7452. You can also force a different level, either by using a numeric prefix
  7453. argument, or by inserting the heading marker by hand. For example, if the
  7454. cursor is after \"*****\", then the tree will be shifted to level 5.
  7455. If optional TREE is given, use this text instead of the kill ring.
  7456. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  7457. move back over whitespace before inserting, and move point to the end of
  7458. the inserted text when done."
  7459. (interactive "P")
  7460. (setq tree (or tree (and kill-ring (current-kill 0))))
  7461. (unless (org-kill-is-subtree-p tree)
  7462. (user-error "%s"
  7463. (substitute-command-keys
  7464. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  7465. (org-with-limited-levels
  7466. (let* ((visp (not (outline-invisible-p)))
  7467. (txt tree)
  7468. (^re_ "\\(\\*+\\)[ \t]*")
  7469. (old-level (if (string-match org-outline-regexp-bol txt)
  7470. (- (match-end 0) (match-beginning 0) 1)
  7471. -1))
  7472. (force-level (cond (level (prefix-numeric-value level))
  7473. ((and (looking-at "[ \t]*$")
  7474. (string-match
  7475. "^\\*+$" (buffer-substring
  7476. (point-at-bol) (point))))
  7477. (- (match-end 0) (match-beginning 0)))
  7478. ((and (bolp)
  7479. (looking-at org-outline-regexp))
  7480. (- (match-end 0) (point) 1))))
  7481. (previous-level (save-excursion
  7482. (condition-case nil
  7483. (progn
  7484. (outline-previous-visible-heading 1)
  7485. (if (looking-at ^re_)
  7486. (- (match-end 0) (match-beginning 0) 1)
  7487. 1))
  7488. (error 1))))
  7489. (next-level (save-excursion
  7490. (condition-case nil
  7491. (progn
  7492. (or (looking-at org-outline-regexp)
  7493. (outline-next-visible-heading 1))
  7494. (if (looking-at ^re_)
  7495. (- (match-end 0) (match-beginning 0) 1)
  7496. 1))
  7497. (error 1))))
  7498. (new-level (or force-level (max previous-level next-level)))
  7499. (shift (if (or (= old-level -1)
  7500. (= new-level -1)
  7501. (= old-level new-level))
  7502. 0
  7503. (- new-level old-level)))
  7504. (delta (if (> shift 0) -1 1))
  7505. (func (if (> shift 0) 'org-demote 'org-promote))
  7506. (org-odd-levels-only nil)
  7507. beg end newend)
  7508. ;; Remove the forced level indicator
  7509. (if force-level
  7510. (delete-region (point-at-bol) (point)))
  7511. ;; Paste
  7512. (beginning-of-line (if (bolp) 1 2))
  7513. (setq beg (point))
  7514. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  7515. (insert-before-markers txt)
  7516. (unless (string-match "\n\\'" txt) (insert "\n"))
  7517. (setq newend (point))
  7518. (org-reinstall-markers-in-region beg)
  7519. (setq end (point))
  7520. (goto-char beg)
  7521. (skip-chars-forward " \t\n\r")
  7522. (setq beg (point))
  7523. (if (and (outline-invisible-p) visp)
  7524. (save-excursion (outline-show-heading)))
  7525. ;; Shift if necessary
  7526. (unless (= shift 0)
  7527. (save-restriction
  7528. (narrow-to-region beg end)
  7529. (while (not (= shift 0))
  7530. (org-map-region func (point-min) (point-max))
  7531. (setq shift (+ delta shift)))
  7532. (goto-char (point-min))
  7533. (setq newend (point-max))))
  7534. (when (or (org-called-interactively-p 'interactive) for-yank)
  7535. (message "Clipboard pasted as level %d subtree" new-level))
  7536. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  7537. kill-ring
  7538. (eq org-subtree-clip (current-kill 0))
  7539. org-subtree-clip-folded)
  7540. ;; The tree was folded before it was killed/copied
  7541. (hide-subtree))
  7542. (and for-yank (goto-char newend)))))
  7543. (defun org-kill-is-subtree-p (&optional txt)
  7544. "Check if the current kill is an outline subtree, or a set of trees.
  7545. Returns nil if kill does not start with a headline, or if the first
  7546. headline level is not the largest headline level in the tree.
  7547. So this will actually accept several entries of equal levels as well,
  7548. which is OK for `org-paste-subtree'.
  7549. If optional TXT is given, check this string instead of the current kill."
  7550. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  7551. (re (org-get-limited-outline-regexp))
  7552. (^re (concat "^" re))
  7553. (start-level (and kill
  7554. (string-match
  7555. (concat "\\`\\([ \t\n\r]*?\n\\)?\\(" re "\\)")
  7556. kill)
  7557. (- (match-end 2) (match-beginning 2) 1)))
  7558. (start (1+ (or (match-beginning 2) -1))))
  7559. (if (not start-level)
  7560. (progn
  7561. nil) ;; does not even start with a heading
  7562. (catch 'exit
  7563. (while (setq start (string-match ^re kill (1+ start)))
  7564. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  7565. (throw 'exit nil)))
  7566. t))))
  7567. (defvar org-markers-to-move nil
  7568. "Markers that should be moved with a cut-and-paste operation.
  7569. Those markers are stored together with their positions relative to
  7570. the start of the region.")
  7571. (defun org-save-markers-in-region (beg end)
  7572. "Check markers in region.
  7573. If these markers are between BEG and END, record their position relative
  7574. to BEG, so that after moving the block of text, we can put the markers back
  7575. into place.
  7576. This function gets called just before an entry or tree gets cut from the
  7577. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  7578. called immediately, to move the markers with the entries."
  7579. (setq org-markers-to-move nil)
  7580. (when (featurep 'org-clock)
  7581. (org-clock-save-markers-for-cut-and-paste beg end))
  7582. (when (featurep 'org-agenda)
  7583. (org-agenda-save-markers-for-cut-and-paste beg end)))
  7584. (defun org-check-and-save-marker (marker beg end)
  7585. "Check if MARKER is between BEG and END.
  7586. If yes, remember the marker and the distance to BEG."
  7587. (when (and (marker-buffer marker)
  7588. (equal (marker-buffer marker) (current-buffer)))
  7589. (if (and (>= marker beg) (< marker end))
  7590. (push (cons marker (- marker beg)) org-markers-to-move))))
  7591. (defun org-reinstall-markers-in-region (beg)
  7592. "Move all remembered markers to their position relative to BEG."
  7593. (mapc (lambda (x)
  7594. (move-marker (car x) (+ beg (cdr x))))
  7595. org-markers-to-move)
  7596. (setq org-markers-to-move nil))
  7597. (defun org-narrow-to-subtree ()
  7598. "Narrow buffer to the current subtree."
  7599. (interactive)
  7600. (save-excursion
  7601. (save-match-data
  7602. (org-with-limited-levels
  7603. (narrow-to-region
  7604. (progn (org-back-to-heading t) (point))
  7605. (progn (org-end-of-subtree t t)
  7606. (if (and (org-at-heading-p) (not (eobp))) (backward-char 1))
  7607. (point)))))))
  7608. (defun org-narrow-to-block ()
  7609. "Narrow buffer to the current block."
  7610. (interactive)
  7611. (let* ((case-fold-search t)
  7612. (blockp (org-between-regexps-p "^[ \t]*#\\+begin_.*"
  7613. "^[ \t]*#\\+end_.*")))
  7614. (if blockp
  7615. (narrow-to-region (car blockp) (cdr blockp))
  7616. (user-error "Not in a block"))))
  7617. (eval-when-compile
  7618. (defvar org-property-drawer-re))
  7619. (defvar org-property-start-re) ;; defined below
  7620. (defun org-clone-subtree-with-time-shift (n &optional shift)
  7621. "Clone the task (subtree) at point N times.
  7622. The clones will be inserted as siblings.
  7623. In interactive use, the user will be prompted for the number of
  7624. clones to be produced. If the entry has a timestamp, the user
  7625. will also be prompted for a time shift, which may be a repeater
  7626. as used in time stamps, for example `+3d'. To disable this,
  7627. you can call the function with a universal prefix argument.
  7628. When a valid repeater is given and the entry contains any time
  7629. stamps, the clones will become a sequence in time, with time
  7630. stamps in the subtree shifted for each clone produced. If SHIFT
  7631. is nil or the empty string, time stamps will be left alone. The
  7632. ID property of the original subtree is removed.
  7633. If the original subtree did contain time stamps with a repeater,
  7634. the following will happen:
  7635. - the repeater will be removed in each clone
  7636. - an additional clone will be produced, with the current, unshifted
  7637. date(s) in the entry.
  7638. - the original entry will be placed *after* all the clones, with
  7639. repeater intact.
  7640. - the start days in the repeater in the original entry will be shifted
  7641. to past the last clone.
  7642. In this way you can spell out a number of instances of a repeating task,
  7643. and still retain the repeater to cover future instances of the task."
  7644. (interactive "nNumber of clones to produce: ")
  7645. (let ((shift
  7646. (or shift
  7647. (if (and (not (equal current-prefix-arg '(4)))
  7648. (save-excursion
  7649. (re-search-forward org-ts-regexp-both
  7650. (save-excursion
  7651. (org-end-of-subtree t)
  7652. (point)) t)))
  7653. (read-from-minibuffer
  7654. "Date shift per clone (e.g. +1w, empty to copy unchanged): ")
  7655. ""))) ;; No time shift
  7656. (n-no-remove -1)
  7657. (drawer-re org-drawer-regexp)
  7658. beg end template task idprop
  7659. shift-n shift-what doshift nmin nmax)
  7660. (if (not (and (integerp n) (> n 0)))
  7661. (error "Invalid number of replications %s" n))
  7662. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  7663. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([hdwmy]\\)[ \t]*\\'"
  7664. shift)))
  7665. (error "Invalid shift specification %s" shift))
  7666. (when doshift
  7667. (setq shift-n (string-to-number (match-string 1 shift))
  7668. shift-what (cdr (assoc (match-string 2 shift)
  7669. '(("d" . day) ("w" . week)
  7670. ("m" . month) ("y" . year))))))
  7671. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  7672. (setq nmin 1 nmax n)
  7673. (org-back-to-heading t)
  7674. (setq beg (point))
  7675. (setq idprop (org-entry-get nil "ID"))
  7676. (org-end-of-subtree t t)
  7677. (or (bolp) (insert "\n"))
  7678. (setq end (point))
  7679. (setq template (buffer-substring beg end))
  7680. (when (and doshift
  7681. (string-match "<[^<>\n]+ [.+]?\\+[0-9]+[hdwmy][^<>\n]*>" template))
  7682. (delete-region beg end)
  7683. (setq end beg)
  7684. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  7685. (goto-char end)
  7686. (loop for n from nmin to nmax do
  7687. ;; prepare clone
  7688. (with-temp-buffer
  7689. (insert template)
  7690. (org-mode)
  7691. (goto-char (point-min))
  7692. (org-show-subtree)
  7693. (and idprop (if org-clone-delete-id
  7694. (org-entry-delete nil "ID")
  7695. (org-id-get-create t)))
  7696. (unless (= n 0)
  7697. (while (re-search-forward "^[ \t]*CLOCK:.*$" nil t)
  7698. (kill-whole-line))
  7699. (goto-char (point-min))
  7700. (while (re-search-forward drawer-re nil t)
  7701. (mapc (lambda (d)
  7702. (org-remove-empty-drawer-at d (point))) org-drawers)))
  7703. (goto-char (point-min))
  7704. (when doshift
  7705. (while (re-search-forward org-ts-regexp-both nil t)
  7706. (org-timestamp-change (* n shift-n) shift-what))
  7707. (unless (= n n-no-remove)
  7708. (goto-char (point-min))
  7709. (while (re-search-forward org-ts-regexp nil t)
  7710. (save-excursion
  7711. (goto-char (match-beginning 0))
  7712. (if (looking-at "<[^<>\n]+\\( +[.+]?\\+[0-9]+[hdwmy]\\)")
  7713. (delete-region (match-beginning 1) (match-end 1)))))))
  7714. (setq task (buffer-string)))
  7715. (insert task))
  7716. (goto-char beg)))
  7717. ;;; Outline Sorting
  7718. (defun org-sort (with-case)
  7719. "Call `org-sort-entries', `org-table-sort-lines' or `org-sort-list'.
  7720. Optional argument WITH-CASE means sort case-sensitively."
  7721. (interactive "P")
  7722. (cond
  7723. ((org-at-table-p) (org-call-with-arg 'org-table-sort-lines with-case))
  7724. ((org-at-item-p) (org-call-with-arg 'org-sort-list with-case))
  7725. (t
  7726. (org-call-with-arg 'org-sort-entries with-case))))
  7727. (defun org-sort-remove-invisible (s)
  7728. "Remove invisible links from string S."
  7729. (remove-text-properties 0 (length s) org-rm-props s)
  7730. (while (string-match org-bracket-link-regexp s)
  7731. (setq s (replace-match (if (match-end 2)
  7732. (match-string 3 s)
  7733. (match-string 1 s)) t t s)))
  7734. (let ((st (format " %s " s)))
  7735. (while (string-match org-emph-re st)
  7736. (setq st (replace-match (format " %s " (match-string 4 st)) t t st)))
  7737. (setq s (substring st 1 -1)))
  7738. s)
  7739. (defvar org-priority-regexp) ; defined later in the file
  7740. (defvar org-after-sorting-entries-or-items-hook nil
  7741. "Hook that is run after a bunch of entries or items have been sorted.
  7742. When children are sorted, the cursor is in the parent line when this
  7743. hook gets called. When a region or a plain list is sorted, the cursor
  7744. will be in the first entry of the sorted region/list.")
  7745. (defun org-sort-entries
  7746. (&optional with-case sorting-type getkey-func compare-func property)
  7747. "Sort entries on a certain level of an outline tree.
  7748. If there is an active region, the entries in the region are sorted.
  7749. Else, if the cursor is before the first entry, sort the top-level items.
  7750. Else, the children of the entry at point are sorted.
  7751. Sorting can be alphabetically, numerically, by date/time as given by
  7752. a time stamp, by a property, by priority order, or by a custom function.
  7753. The command prompts for the sorting type unless it has been given to the
  7754. function through the SORTING-TYPE argument, which needs to be a character,
  7755. \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?o ?O ?r ?R ?f ?F). Here is the
  7756. precise meaning of each character:
  7757. n Numerically, by converting the beginning of the entry/item to a number.
  7758. a Alphabetically, ignoring the TODO keyword and the priority, if any.
  7759. o By order of TODO keywords.
  7760. t By date/time, either the first active time stamp in the entry, or, if
  7761. none exist, by the first inactive one.
  7762. s By the scheduled date/time.
  7763. d By deadline date/time.
  7764. c By creation time, which is assumed to be the first inactive time stamp
  7765. at the beginning of a line.
  7766. p By priority according to the cookie.
  7767. r By the value of a property.
  7768. Capital letters will reverse the sort order.
  7769. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  7770. called with point at the beginning of the record. It must return either
  7771. a string or a number that should serve as the sorting key for that record.
  7772. Comparing entries ignores case by default. However, with an optional argument
  7773. WITH-CASE, the sorting considers case as well.
  7774. Sorting is done against the visible part of the headlines, it ignores hidden
  7775. links."
  7776. (interactive "P")
  7777. (let ((case-func (if with-case 'identity 'downcase))
  7778. (cmstr
  7779. ;; The clock marker is lost when using `sort-subr', let's
  7780. ;; store the clocking string.
  7781. (when (equal (marker-buffer org-clock-marker) (current-buffer))
  7782. (save-excursion
  7783. (goto-char org-clock-marker)
  7784. (looking-back "^.*") (match-string-no-properties 0))))
  7785. start beg end stars re re2
  7786. txt what tmp)
  7787. ;; Find beginning and end of region to sort
  7788. (cond
  7789. ((org-region-active-p)
  7790. ;; we will sort the region
  7791. (setq end (region-end)
  7792. what "region")
  7793. (goto-char (region-beginning))
  7794. (if (not (org-at-heading-p)) (outline-next-heading))
  7795. (setq start (point)))
  7796. ((or (org-at-heading-p)
  7797. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  7798. ;; we will sort the children of the current headline
  7799. (org-back-to-heading)
  7800. (setq start (point)
  7801. end (progn (org-end-of-subtree t t)
  7802. (or (bolp) (insert "\n"))
  7803. (when (>= (org-back-over-empty-lines) 1)
  7804. (forward-line 1))
  7805. (point))
  7806. what "children")
  7807. (goto-char start)
  7808. (show-subtree)
  7809. (outline-next-heading))
  7810. (t
  7811. ;; we will sort the top-level entries in this file
  7812. (goto-char (point-min))
  7813. (or (org-at-heading-p) (outline-next-heading))
  7814. (setq start (point))
  7815. (goto-char (point-max))
  7816. (beginning-of-line 1)
  7817. (when (looking-at ".*?\\S-")
  7818. ;; File ends in a non-white line
  7819. (end-of-line 1)
  7820. (insert "\n"))
  7821. (setq end (point-max))
  7822. (setq what "top-level")
  7823. (goto-char start)
  7824. (show-all)))
  7825. (setq beg (point))
  7826. (when (>= beg end) (goto-char start) (user-error "Nothing to sort"))
  7827. (looking-at "\\(\\*+\\)")
  7828. (setq stars (match-string 1)
  7829. re (concat "^" (regexp-quote stars) " +")
  7830. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[ \t\n]")
  7831. txt (buffer-substring beg end))
  7832. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  7833. (if (and (not (equal stars "*")) (string-match re2 txt))
  7834. (user-error "Region to sort contains a level above the first entry"))
  7835. (unless sorting-type
  7836. (message
  7837. "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
  7838. [t]ime [s]cheduled [d]eadline [c]reated
  7839. A/N/P/R/O/F/T/S/D/C means reversed:"
  7840. what)
  7841. (setq sorting-type (read-char-exclusive))
  7842. (unless getkey-func
  7843. (and (= (downcase sorting-type) ?f)
  7844. (setq getkey-func
  7845. (org-icompleting-read "Sort using function: "
  7846. obarray 'fboundp t nil nil))
  7847. (setq getkey-func (intern getkey-func))))
  7848. (and (= (downcase sorting-type) ?r)
  7849. (not property)
  7850. (setq property
  7851. (org-icompleting-read "Property: "
  7852. (mapcar 'list (org-buffer-property-keys t))
  7853. nil t))))
  7854. (message "Sorting entries...")
  7855. (save-restriction
  7856. (narrow-to-region start end)
  7857. (let ((dcst (downcase sorting-type))
  7858. (case-fold-search nil)
  7859. (now (current-time)))
  7860. (sort-subr
  7861. (/= dcst sorting-type)
  7862. ;; This function moves to the beginning character of the "record" to
  7863. ;; be sorted.
  7864. (lambda nil
  7865. (if (re-search-forward re nil t)
  7866. (goto-char (match-beginning 0))
  7867. (goto-char (point-max))))
  7868. ;; This function moves to the last character of the "record" being
  7869. ;; sorted.
  7870. (lambda nil
  7871. (save-match-data
  7872. (condition-case nil
  7873. (outline-forward-same-level 1)
  7874. (error
  7875. (goto-char (point-max))))))
  7876. ;; This function returns the value that gets sorted against.
  7877. (lambda nil
  7878. (cond
  7879. ((= dcst ?n)
  7880. (if (looking-at org-complex-heading-regexp)
  7881. (string-to-number (org-sort-remove-invisible (match-string 4)))
  7882. nil))
  7883. ((= dcst ?a)
  7884. (if (looking-at org-complex-heading-regexp)
  7885. (funcall case-func (org-sort-remove-invisible (match-string 4)))
  7886. nil))
  7887. ((= dcst ?t)
  7888. (let ((end (save-excursion (outline-next-heading) (point))))
  7889. (if (or (re-search-forward org-ts-regexp end t)
  7890. (re-search-forward org-ts-regexp-both end t))
  7891. (org-time-string-to-seconds (match-string 0))
  7892. (org-float-time now))))
  7893. ((= dcst ?c)
  7894. (let ((end (save-excursion (outline-next-heading) (point))))
  7895. (if (re-search-forward
  7896. (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
  7897. end t)
  7898. (org-time-string-to-seconds (match-string 0))
  7899. (org-float-time now))))
  7900. ((= dcst ?s)
  7901. (let ((end (save-excursion (outline-next-heading) (point))))
  7902. (if (re-search-forward org-scheduled-time-regexp end t)
  7903. (org-time-string-to-seconds (match-string 1))
  7904. (org-float-time now))))
  7905. ((= dcst ?d)
  7906. (let ((end (save-excursion (outline-next-heading) (point))))
  7907. (if (re-search-forward org-deadline-time-regexp end t)
  7908. (org-time-string-to-seconds (match-string 1))
  7909. (org-float-time now))))
  7910. ((= dcst ?p)
  7911. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  7912. (string-to-char (match-string 2))
  7913. org-default-priority))
  7914. ((= dcst ?r)
  7915. (or (org-entry-get nil property) ""))
  7916. ((= dcst ?o)
  7917. (if (looking-at org-complex-heading-regexp)
  7918. (- 9999 (length (member (match-string 2)
  7919. org-todo-keywords-1)))))
  7920. ((= dcst ?f)
  7921. (if getkey-func
  7922. (progn
  7923. (setq tmp (funcall getkey-func))
  7924. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  7925. tmp)
  7926. (error "Invalid key function `%s'" getkey-func)))
  7927. (t (error "Invalid sorting type `%c'" sorting-type))))
  7928. nil
  7929. (cond
  7930. ((= dcst ?a) 'string<)
  7931. ((= dcst ?f) compare-func)
  7932. ((member dcst '(?p ?t ?s ?d ?c)) '<)))))
  7933. (run-hooks 'org-after-sorting-entries-or-items-hook)
  7934. ;; Reset the clock marker if needed
  7935. (when cmstr
  7936. (save-excursion
  7937. (goto-char start)
  7938. (search-forward cmstr nil t)
  7939. (move-marker org-clock-marker (point))))
  7940. (message "Sorting entries...done")))
  7941. (defun org-do-sort (table what &optional with-case sorting-type)
  7942. "Sort TABLE of WHAT according to SORTING-TYPE.
  7943. The user will be prompted for the SORTING-TYPE if the call to this
  7944. function does not specify it. WHAT is only for the prompt, to indicate
  7945. what is being sorted. The sorting key will be extracted from
  7946. the car of the elements of the table.
  7947. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  7948. (unless sorting-type
  7949. (message
  7950. "Sort %s: [a]lphabetic, [n]umeric, [t]ime. A/N/T means reversed:"
  7951. what)
  7952. (setq sorting-type (read-char-exclusive)))
  7953. (let ((dcst (downcase sorting-type))
  7954. extractfun comparefun)
  7955. ;; Define the appropriate functions
  7956. (cond
  7957. ((= dcst ?n)
  7958. (setq extractfun 'string-to-number
  7959. comparefun (if (= dcst sorting-type) '< '>)))
  7960. ((= dcst ?a)
  7961. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  7962. (lambda(x) (downcase (org-sort-remove-invisible x))))
  7963. comparefun (if (= dcst sorting-type)
  7964. 'string<
  7965. (lambda (a b) (and (not (string< a b))
  7966. (not (string= a b)))))))
  7967. ((= dcst ?t)
  7968. (setq extractfun
  7969. (lambda (x)
  7970. (if (or (string-match org-ts-regexp x)
  7971. (string-match org-ts-regexp-both x))
  7972. (org-float-time
  7973. (org-time-string-to-time (match-string 0 x)))
  7974. 0))
  7975. comparefun (if (= dcst sorting-type) '< '>)))
  7976. (t (error "Invalid sorting type `%c'" sorting-type)))
  7977. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  7978. table)
  7979. (lambda (a b) (funcall comparefun (car a) (car b))))))
  7980. ;;; The orgstruct minor mode
  7981. ;; Define a minor mode which can be used in other modes in order to
  7982. ;; integrate the org-mode structure editing commands.
  7983. ;; This is really a hack, because the org-mode structure commands use
  7984. ;; keys which normally belong to the major mode. Here is how it
  7985. ;; works: The minor mode defines all the keys necessary to operate the
  7986. ;; structure commands, but wraps the commands into a function which
  7987. ;; tests if the cursor is currently at a headline or a plain list
  7988. ;; item. If that is the case, the structure command is used,
  7989. ;; temporarily setting many Org-mode variables like regular
  7990. ;; expressions for filling etc. However, when any of those keys is
  7991. ;; used at a different location, function uses `key-binding' to look
  7992. ;; up if the key has an associated command in another currently active
  7993. ;; keymap (minor modes, major mode, global), and executes that
  7994. ;; command. There might be problems if any of the keys is otherwise
  7995. ;; used as a prefix key.
  7996. (defcustom orgstruct-heading-prefix-regexp ""
  7997. "Regexp that matches the custom prefix of Org headlines in
  7998. orgstruct(++)-mode."
  7999. :group 'org
  8000. :version "24.4"
  8001. :package-version '(Org . "8.3")
  8002. :type 'regexp)
  8003. ;;;###autoload(put 'orgstruct-heading-prefix-regexp 'safe-local-variable 'stringp)
  8004. (defcustom orgstruct-setup-hook nil
  8005. "Hook run after orgstruct-mode-map is filled."
  8006. :group 'org
  8007. :version "24.4"
  8008. :package-version '(Org . "8.0")
  8009. :type 'hook)
  8010. (defvar orgstruct-initialized nil)
  8011. (defvar org-local-vars nil
  8012. "List of local variables, for use by `orgstruct-mode'.")
  8013. ;;;###autoload
  8014. (define-minor-mode orgstruct-mode
  8015. "Toggle the minor mode `orgstruct-mode'.
  8016. This mode is for using Org-mode structure commands in other
  8017. modes. The following keys behave as if Org-mode were active, if
  8018. the cursor is on a headline, or on a plain list item (both as
  8019. defined by Org-mode)."
  8020. nil " OrgStruct" (make-sparse-keymap)
  8021. (funcall (if orgstruct-mode
  8022. 'add-to-invisibility-spec
  8023. 'remove-from-invisibility-spec)
  8024. '(outline . t))
  8025. (when orgstruct-mode
  8026. (org-load-modules-maybe)
  8027. (unless orgstruct-initialized
  8028. (orgstruct-setup)
  8029. (setq orgstruct-initialized t))))
  8030. ;;;###autoload
  8031. (defun turn-on-orgstruct ()
  8032. "Unconditionally turn on `orgstruct-mode'."
  8033. (orgstruct-mode 1))
  8034. (defvar org-fb-vars nil)
  8035. (make-variable-buffer-local 'org-fb-vars)
  8036. (defun orgstruct++-mode (&optional arg)
  8037. "Toggle `orgstruct-mode', the enhanced version of it.
  8038. In addition to setting orgstruct-mode, this also exports all
  8039. indentation and autofilling variables from org-mode into the
  8040. buffer. It will also recognize item context in multiline items."
  8041. (interactive "P")
  8042. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  8043. (if (< arg 1)
  8044. (progn (orgstruct-mode -1)
  8045. (mapc (lambda(v)
  8046. (org-set-local (car v)
  8047. (if (eq (car-safe (cadr v)) 'quote) (cadadr v) (cadr v))))
  8048. org-fb-vars))
  8049. (orgstruct-mode 1)
  8050. (setq org-fb-vars nil)
  8051. (unless org-local-vars
  8052. (setq org-local-vars (org-get-local-variables)))
  8053. (let (var val)
  8054. (mapc
  8055. (lambda (x)
  8056. (when (string-match
  8057. "^\\(paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|fill-prefix\\|indent-\\)"
  8058. (symbol-name (car x)))
  8059. (setq var (car x) val (nth 1 x))
  8060. (push (list var `(quote ,(eval var))) org-fb-vars)
  8061. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  8062. org-local-vars)
  8063. (org-set-local 'orgstruct-is-++ t))))
  8064. (defvar orgstruct-is-++ nil
  8065. "Is `orgstruct-mode' in ++ version in the current-buffer?")
  8066. (make-variable-buffer-local 'orgstruct-is-++)
  8067. ;;;###autoload
  8068. (defun turn-on-orgstruct++ ()
  8069. "Unconditionally turn on `orgstruct++-mode'."
  8070. (orgstruct++-mode 1))
  8071. (defun orgstruct-error ()
  8072. "Error when there is no default binding for a structure key."
  8073. (interactive)
  8074. (funcall (if (fboundp 'user-error)
  8075. 'user-error
  8076. 'error)
  8077. "This key has no function outside structure elements"))
  8078. (defun orgstruct-setup ()
  8079. "Setup orgstruct keymap."
  8080. (dolist (cell '((org-demote . t)
  8081. (org-metaleft . t)
  8082. (org-metaright . t)
  8083. (org-promote . t)
  8084. (org-shiftmetaleft . t)
  8085. (org-shiftmetaright . t)
  8086. org-backward-element
  8087. org-backward-heading-same-level
  8088. org-ctrl-c-ret
  8089. org-ctrl-c-minus
  8090. org-ctrl-c-star
  8091. org-cycle
  8092. org-forward-heading-same-level
  8093. org-insert-heading
  8094. org-insert-heading-respect-content
  8095. org-kill-note-or-show-branches
  8096. org-mark-subtree
  8097. org-meta-return
  8098. org-metadown
  8099. org-metaup
  8100. org-narrow-to-subtree
  8101. org-promote-subtree
  8102. org-reveal
  8103. org-shiftdown
  8104. org-shiftleft
  8105. org-shiftmetadown
  8106. org-shiftmetaup
  8107. org-shiftright
  8108. org-shifttab
  8109. org-shifttab
  8110. org-shiftup
  8111. org-show-subtree
  8112. org-sort
  8113. org-up-element
  8114. outline-demote
  8115. outline-next-visible-heading
  8116. outline-previous-visible-heading
  8117. outline-promote
  8118. outline-up-heading
  8119. show-children))
  8120. (let ((f (or (car-safe cell) cell))
  8121. (disable-when-heading-prefix (cdr-safe cell)))
  8122. (when (fboundp f)
  8123. (let ((new-bindings))
  8124. (dolist (binding (nconc (where-is-internal f org-mode-map)
  8125. (where-is-internal f outline-mode-map)))
  8126. (push binding new-bindings)
  8127. ;; TODO use local-function-key-map
  8128. (dolist (rep '(("<tab>" . "TAB")
  8129. ("<return>" . "RET")
  8130. ("<escape>" . "ESC")
  8131. ("<delete>" . "DEL")))
  8132. (setq binding (read-kbd-macro
  8133. (let ((case-fold-search))
  8134. (replace-regexp-in-string
  8135. (regexp-quote (cdr rep))
  8136. (car rep)
  8137. (key-description binding)))))
  8138. (pushnew binding new-bindings :test 'equal)))
  8139. (dolist (binding new-bindings)
  8140. (let ((key (lookup-key orgstruct-mode-map binding)))
  8141. (when (or (not key) (numberp key))
  8142. (condition-case nil
  8143. (org-defkey orgstruct-mode-map
  8144. binding
  8145. (orgstruct-make-binding f binding disable-when-heading-prefix))
  8146. (error nil)))))))))
  8147. (run-hooks 'orgstruct-setup-hook))
  8148. (defun orgstruct-make-binding (fun key disable-when-heading-prefix)
  8149. "Create a function for binding in the structure minor mode.
  8150. FUN is the command to call inside a table. KEY is the key that
  8151. should be checked in for a command to execute outside of tables.
  8152. Non-nil `disable-when-heading-prefix' means to disable the command
  8153. if `orgstruct-heading-prefix-regexp' is not empty."
  8154. (let ((name (concat "orgstruct-hijacker-" (symbol-name fun))))
  8155. (let ((nname name)
  8156. (i 0))
  8157. (while (fboundp (intern nname))
  8158. (setq nname (format "%s-%d" name (setq i (1+ i)))))
  8159. (setq name (intern nname)))
  8160. (eval
  8161. (let ((bindings '((org-heading-regexp
  8162. (concat "^"
  8163. orgstruct-heading-prefix-regexp
  8164. "\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ ]*$"))
  8165. (org-outline-regexp
  8166. (concat orgstruct-heading-prefix-regexp "\\*+ "))
  8167. (org-outline-regexp-bol
  8168. (concat "^" org-outline-regexp))
  8169. (outline-regexp org-outline-regexp)
  8170. (outline-heading-end-regexp "\n")
  8171. (outline-level 'org-outline-level)
  8172. (outline-heading-alist))))
  8173. `(defun ,name (arg)
  8174. ,(concat "In Structure, run `" (symbol-name fun) "'.\n"
  8175. "Outside of structure, run the binding of `"
  8176. (key-description key) "'."
  8177. (when disable-when-heading-prefix
  8178. (concat
  8179. "\nIf `orgstruct-heading-prefix-regexp' is not empty, this command will always fall\n"
  8180. "back to the default binding due to limitations of Org's implementation of\n"
  8181. "`" (symbol-name fun) "'.")))
  8182. (interactive "p")
  8183. (let* ((disable
  8184. ,(and disable-when-heading-prefix
  8185. '(not (string= orgstruct-heading-prefix-regexp ""))))
  8186. (fallback
  8187. (or disable
  8188. (not
  8189. (let* ,bindings
  8190. (org-context-p 'headline 'item
  8191. ,(when (memq fun
  8192. '(org-insert-heading
  8193. org-insert-heading-respect-content
  8194. org-meta-return))
  8195. '(when orgstruct-is-++
  8196. 'item-body))))))))
  8197. (if fallback
  8198. (let* ((orgstruct-mode)
  8199. (binding
  8200. (let ((key ,key))
  8201. (catch 'exit
  8202. (dolist
  8203. (rep
  8204. '(nil
  8205. ("<\\([^>]*\\)tab>" . "\\1TAB")
  8206. ("<\\([^>]*\\)return>" . "\\1RET")
  8207. ("<\\([^>]*\\)escape>" . "\\1ESC")
  8208. ("<\\([^>]*\\)delete>" . "\\1DEL"))
  8209. nil)
  8210. (when rep
  8211. (setq key (read-kbd-macro
  8212. (let ((case-fold-search))
  8213. (replace-regexp-in-string
  8214. (car rep)
  8215. (cdr rep)
  8216. (key-description key))))))
  8217. (when (key-binding key)
  8218. (throw 'exit (key-binding key))))))))
  8219. (if (keymapp binding)
  8220. (org-set-transient-map binding)
  8221. (let ((func (or binding
  8222. (unless disable
  8223. 'orgstruct-error))))
  8224. (when func
  8225. (call-interactively func)))))
  8226. (org-run-like-in-org-mode
  8227. (lambda ()
  8228. (interactive)
  8229. (let* ,bindings
  8230. (call-interactively ',fun)))))))))
  8231. name))
  8232. (defun org-contextualize-keys (alist contexts)
  8233. "Return valid elements in ALIST depending on CONTEXTS.
  8234. `org-agenda-custom-commands' or `org-capture-templates' are the
  8235. values used for ALIST, and `org-agenda-custom-commands-contexts'
  8236. or `org-capture-templates-contexts' are the associated contexts
  8237. definitions."
  8238. (let ((contexts
  8239. ;; normalize contexts
  8240. (mapcar
  8241. (lambda(c) (cond ((listp (cadr c))
  8242. (list (car c) (car c) (cadr c)))
  8243. ((string= "" (cadr c))
  8244. (list (car c) (car c) (caddr c)))
  8245. (t c))) contexts))
  8246. (a alist) c r s)
  8247. ;; loop over all commands or templates
  8248. (while (setq c (pop a))
  8249. (let (vrules repl)
  8250. (cond
  8251. ((not (assoc (car c) contexts))
  8252. (push c r))
  8253. ((and (assoc (car c) contexts)
  8254. (setq vrules (org-contextualize-validate-key
  8255. (car c) contexts)))
  8256. (mapc (lambda (vr)
  8257. (when (not (equal (car vr) (cadr vr)))
  8258. (setq repl vr))) vrules)
  8259. (if (not repl) (push c r)
  8260. (push (cadr repl) s)
  8261. (push
  8262. (cons (car c)
  8263. (cdr (or (assoc (cadr repl) alist)
  8264. (error "Undefined key `%s' as contextual replacement for `%s'"
  8265. (cadr repl) (car c)))))
  8266. r))))))
  8267. ;; Return limited ALIST, possibly with keys modified, and deduplicated
  8268. (delq
  8269. nil
  8270. (delete-dups
  8271. (mapcar (lambda (x)
  8272. (let ((tpl (car x)))
  8273. (when (not (delq
  8274. nil
  8275. (mapcar (lambda(y)
  8276. (equal y tpl)) s))) x)))
  8277. (reverse r))))))
  8278. (defun org-contextualize-validate-key (key contexts)
  8279. "Check CONTEXTS for agenda or capture KEY."
  8280. (let (r rr res)
  8281. (while (setq r (pop contexts))
  8282. (mapc
  8283. (lambda (rr)
  8284. (when
  8285. (and (equal key (car r))
  8286. (if (functionp rr) (funcall rr)
  8287. (or (and (eq (car rr) 'in-file)
  8288. (buffer-file-name)
  8289. (string-match (cdr rr) (buffer-file-name)))
  8290. (and (eq (car rr) 'in-mode)
  8291. (string-match (cdr rr) (symbol-name major-mode)))
  8292. (and (eq (car rr) 'in-buffer)
  8293. (string-match (cdr rr) (buffer-name)))
  8294. (when (and (eq (car rr) 'not-in-file)
  8295. (buffer-file-name))
  8296. (not (string-match (cdr rr) (buffer-file-name))))
  8297. (when (eq (car rr) 'not-in-mode)
  8298. (not (string-match (cdr rr) (symbol-name major-mode))))
  8299. (when (eq (car rr) 'not-in-buffer)
  8300. (not (string-match (cdr rr) (buffer-name)))))))
  8301. (push r res)))
  8302. (car (last r))))
  8303. (delete-dups (delq nil res))))
  8304. (defun org-context-p (&rest contexts)
  8305. "Check if local context is any of CONTEXTS.
  8306. Possible values in the list of contexts are `table', `headline', and `item'."
  8307. (let ((pos (point)))
  8308. (goto-char (point-at-bol))
  8309. (prog1 (or (and (memq 'table contexts)
  8310. (looking-at "[ \t]*|"))
  8311. (and (memq 'headline contexts)
  8312. (looking-at org-outline-regexp))
  8313. (and (memq 'item contexts)
  8314. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  8315. (and (memq 'item-body contexts)
  8316. (org-in-item-p)))
  8317. (goto-char pos))))
  8318. (defun org-get-local-variables ()
  8319. "Return a list of all local variables in an Org mode buffer."
  8320. (let (varlist)
  8321. (with-current-buffer (get-buffer-create "*Org tmp*")
  8322. (erase-buffer)
  8323. (org-mode)
  8324. (setq varlist (buffer-local-variables)))
  8325. (kill-buffer "*Org tmp*")
  8326. (delq nil
  8327. (mapcar
  8328. (lambda (x)
  8329. (setq x
  8330. (if (symbolp x)
  8331. (list x)
  8332. (list (car x) (cdr x))))
  8333. (if (and (not (get (car x) 'org-state))
  8334. (string-match
  8335. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|indent-\\)"
  8336. (symbol-name (car x))))
  8337. x nil))
  8338. varlist))))
  8339. (defun org-clone-local-variables (from-buffer &optional regexp)
  8340. "Clone local variables from FROM-BUFFER.
  8341. Optional argument REGEXP selects variables to clone."
  8342. (mapc
  8343. (lambda (pair)
  8344. (and (symbolp (car pair))
  8345. (or (null regexp)
  8346. (string-match regexp (symbol-name (car pair))))
  8347. (set (make-local-variable (car pair))
  8348. (cdr pair))))
  8349. (buffer-local-variables from-buffer)))
  8350. ;;;###autoload
  8351. (defun org-run-like-in-org-mode (cmd)
  8352. "Run a command, pretending that the current buffer is in Org-mode.
  8353. This will temporarily bind local variables that are typically bound in
  8354. Org-mode to the values they have in Org-mode, and then interactively
  8355. call CMD."
  8356. (org-load-modules-maybe)
  8357. (unless org-local-vars
  8358. (setq org-local-vars (org-get-local-variables)))
  8359. (let (binds)
  8360. (dolist (var org-local-vars)
  8361. (when (or (not (boundp (car var)))
  8362. (eq (symbol-value (car var))
  8363. (default-value (car var))))
  8364. (push (list (car var) `(quote ,(cadr var))) binds)))
  8365. (eval `(let ,binds
  8366. (call-interactively (quote ,cmd))))))
  8367. ;;;; Archiving
  8368. (defun org-get-category (&optional pos force-refresh)
  8369. "Get the category applying to position POS."
  8370. (save-match-data
  8371. (if force-refresh (org-refresh-category-properties))
  8372. (let ((pos (or pos (point))))
  8373. (or (get-text-property pos 'org-category)
  8374. (progn (org-refresh-category-properties)
  8375. (get-text-property pos 'org-category))))))
  8376. (defun org-refresh-category-properties ()
  8377. "Refresh category text properties in the buffer."
  8378. (let ((case-fold-search t)
  8379. (inhibit-read-only t)
  8380. (def-cat (cond
  8381. ((null org-category)
  8382. (if buffer-file-name
  8383. (file-name-sans-extension
  8384. (file-name-nondirectory buffer-file-name))
  8385. "???"))
  8386. ((symbolp org-category) (symbol-name org-category))
  8387. (t org-category)))
  8388. beg end cat pos optionp)
  8389. (org-with-silent-modifications
  8390. (save-excursion
  8391. (save-restriction
  8392. (widen)
  8393. (goto-char (point-min))
  8394. (put-text-property (point) (point-max) 'org-category def-cat)
  8395. (while (re-search-forward
  8396. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  8397. (setq pos (match-end 0)
  8398. optionp (equal (char-after (match-beginning 0)) ?#)
  8399. cat (org-trim (match-string 2)))
  8400. (if optionp
  8401. (setq beg (point-at-bol) end (point-max))
  8402. (org-back-to-heading t)
  8403. (setq beg (point) end (org-end-of-subtree t t)))
  8404. (put-text-property beg end 'org-category cat)
  8405. (put-text-property beg end 'org-category-position beg)
  8406. (goto-char pos)))))))
  8407. (defun org-refresh-properties (dprop tprop)
  8408. "Refresh buffer text properties.
  8409. DPROP is the drawer property and TPROP is the corresponding text
  8410. property to set."
  8411. (let ((case-fold-search t)
  8412. (inhibit-read-only t) p)
  8413. (org-with-silent-modifications
  8414. (save-excursion
  8415. (save-restriction
  8416. (widen)
  8417. (goto-char (point-min))
  8418. (while (re-search-forward (concat "^[ \t]*:" dprop ": +\\(.*\\)[ \t]*$") nil t)
  8419. (setq p (org-match-string-no-properties 1))
  8420. (save-excursion
  8421. (org-back-to-heading t)
  8422. (put-text-property
  8423. (point-at-bol) (or (outline-next-heading) (point-max)) tprop p))))))))
  8424. ;;;; Link Stuff
  8425. ;;; Link abbreviations
  8426. (defun org-link-expand-abbrev (link)
  8427. "Apply replacements as defined in `org-link-abbrev-alist'."
  8428. (if (string-match "^\\([^:]*\\)\\(::?\\(.*\\)\\)?$" link)
  8429. (let* ((key (match-string 1 link))
  8430. (as (or (assoc key org-link-abbrev-alist-local)
  8431. (assoc key org-link-abbrev-alist)))
  8432. (tag (and (match-end 2) (match-string 3 link)))
  8433. rpl)
  8434. (if (not as)
  8435. link
  8436. (setq rpl (cdr as))
  8437. (cond
  8438. ((symbolp rpl) (funcall rpl tag))
  8439. ((string-match "%(\\([^)]+\\))" rpl)
  8440. (replace-match
  8441. (save-match-data
  8442. (funcall (intern-soft (match-string 1 rpl)) tag)) t t rpl))
  8443. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  8444. ((string-match "%h" rpl)
  8445. (replace-match (url-hexify-string (or tag "")) t t rpl))
  8446. (t (concat rpl tag)))))
  8447. link))
  8448. ;;; Storing and inserting links
  8449. (defvar org-insert-link-history nil
  8450. "Minibuffer history for links inserted with `org-insert-link'.")
  8451. (defvar org-stored-links nil
  8452. "Contains the links stored with `org-store-link'.")
  8453. (defvar org-store-link-plist nil
  8454. "Plist with info about the most recently link created with `org-store-link'.")
  8455. (defvar org-link-protocols nil
  8456. "Link protocols added to Org-mode using `org-add-link-type'.")
  8457. (defvar org-store-link-functions nil
  8458. "List of functions that are called to create and store a link.
  8459. Each function will be called in turn until one returns a non-nil
  8460. value. Each function should check if it is responsible for creating
  8461. this link (for example by looking at the major mode).
  8462. If not, it must exit and return nil.
  8463. If yes, it should return a non-nil value after a calling
  8464. `org-store-link-props' with a list of properties and values.
  8465. Special properties are:
  8466. :type The link prefix, like \"http\". This must be given.
  8467. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  8468. This is obligatory as well.
  8469. :description Optional default description for the second pair
  8470. of brackets in an Org-mode link. The user can still change
  8471. this when inserting this link into an Org-mode buffer.
  8472. In addition to these, any additional properties can be specified
  8473. and then used in capture templates.")
  8474. (defun org-add-link-type (type &optional follow export)
  8475. "Add TYPE to the list of `org-link-types'.
  8476. Re-compute all regular expressions depending on `org-link-types'
  8477. FOLLOW and EXPORT are two functions.
  8478. FOLLOW should take the link path as the single argument and do whatever
  8479. is necessary to follow the link, for example find a file or display
  8480. a mail message.
  8481. EXPORT should format the link path for export to one of the export formats.
  8482. It should be a function accepting three arguments:
  8483. path the path of the link, the text after the prefix (like \"http:\")
  8484. desc the description of the link, if any, or a description added by
  8485. org-export-normalize-links if there is none
  8486. format the export format, a symbol like `html' or `latex' or `ascii'..
  8487. The function may use the FORMAT information to return different values
  8488. depending on the format. The return value will be put literally into
  8489. the exported file. If the return value is nil, this means Org should
  8490. do what it normally does with links which do not have EXPORT defined.
  8491. Org-mode has a built-in default for exporting links. If you are happy with
  8492. this default, there is no need to define an export function for the link
  8493. type. For a simple example of an export function, see `org-bbdb.el'."
  8494. (add-to-list 'org-link-types type t)
  8495. (org-make-link-regexps)
  8496. (if (assoc type org-link-protocols)
  8497. (setcdr (assoc type org-link-protocols) (list follow export))
  8498. (push (list type follow export) org-link-protocols)))
  8499. (defvar org-agenda-buffer-name) ; Defined in org-agenda.el
  8500. (defvar org-id-link-to-org-use-id) ; Defined in org-id.el
  8501. ;;;###autoload
  8502. (defun org-store-link (arg)
  8503. "\\<org-mode-map>Store an org-link to the current location.
  8504. This link is added to `org-stored-links' and can later be inserted
  8505. into an org-buffer with \\[org-insert-link].
  8506. For some link types, a prefix arg is interpreted.
  8507. For links to Usenet articles, arg negates `org-gnus-prefer-web-links'.
  8508. For file links, arg negates `org-context-in-file-links'.
  8509. A double prefix arg force skipping storing functions that are not
  8510. part of Org's core.
  8511. A triple prefix arg force storing a link for each line in the
  8512. active region."
  8513. (interactive "P")
  8514. (org-load-modules-maybe)
  8515. (if (and (equal arg '(64)) (org-region-active-p))
  8516. (save-excursion
  8517. (let ((end (region-end)))
  8518. (goto-char (region-beginning))
  8519. (set-mark (point))
  8520. (while (< (point-at-eol) end)
  8521. (move-end-of-line 1) (activate-mark)
  8522. (let (current-prefix-arg)
  8523. (call-interactively 'org-store-link))
  8524. (move-beginning-of-line 2)
  8525. (set-mark (point)))))
  8526. (org-with-limited-levels
  8527. (setq org-store-link-plist nil)
  8528. (let (link cpltxt desc description search
  8529. txt custom-id agenda-link sfuns sfunsn)
  8530. (cond
  8531. ;; Store a link using an external link type
  8532. ((and (not (equal arg '(16)))
  8533. (setq sfuns
  8534. (delq
  8535. nil (mapcar (lambda (f)
  8536. (let (fs) (if (funcall f) (push f fs))))
  8537. org-store-link-functions))
  8538. sfunsn (mapcar (lambda (fu) (symbol-name (car fu))) sfuns))
  8539. (or (and (cdr sfuns)
  8540. (funcall (intern
  8541. (completing-read
  8542. "Which function for creating the link? "
  8543. sfunsn nil t (car sfunsn)))))
  8544. (funcall (caar sfuns)))
  8545. (setq link (plist-get org-store-link-plist :link)
  8546. desc (or (plist-get org-store-link-plist
  8547. :description) link))))
  8548. ;; Store a link from a source code buffer
  8549. ((org-src-edit-buffer-p)
  8550. (let (label gc)
  8551. (while (or (not label)
  8552. (save-excursion
  8553. (save-restriction
  8554. (widen)
  8555. (goto-char (point-min))
  8556. (re-search-forward
  8557. (regexp-quote (format org-coderef-label-format label))
  8558. nil t))))
  8559. (when label (message "Label exists already") (sit-for 2))
  8560. (setq label (read-string "Code line label: " label)))
  8561. (end-of-line 1)
  8562. (setq link (format org-coderef-label-format label))
  8563. (setq gc (- 79 (length link)))
  8564. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  8565. (insert link)
  8566. (setq link (concat "(" label ")") desc nil)))
  8567. ;; We are in the agenda, link to referenced location
  8568. ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
  8569. (let ((m (or (get-text-property (point) 'org-hd-marker)
  8570. (get-text-property (point) 'org-marker))))
  8571. (when m
  8572. (org-with-point-at m
  8573. (setq agenda-link
  8574. (if (org-called-interactively-p 'any)
  8575. (call-interactively 'org-store-link)
  8576. (org-store-link nil)))))))
  8577. ((eq major-mode 'calendar-mode)
  8578. (let ((cd (calendar-cursor-to-date)))
  8579. (setq link
  8580. (format-time-string
  8581. (car org-time-stamp-formats)
  8582. (apply 'encode-time
  8583. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  8584. nil nil nil))))
  8585. (org-store-link-props :type "calendar" :date cd)))
  8586. ((eq major-mode 'help-mode)
  8587. (setq link (concat "help:" (save-excursion
  8588. (goto-char (point-min))
  8589. (looking-at "^[^ ]+")
  8590. (match-string 0))))
  8591. (org-store-link-props :type "help"))
  8592. ((eq major-mode 'w3-mode)
  8593. (setq cpltxt (if (and (buffer-name)
  8594. (not (string-match "Untitled" (buffer-name))))
  8595. (buffer-name)
  8596. (url-view-url t))
  8597. link (url-view-url t))
  8598. (org-store-link-props :type "w3" :url (url-view-url t)))
  8599. ((eq major-mode 'image-mode)
  8600. (setq cpltxt (concat "file:"
  8601. (abbreviate-file-name buffer-file-name))
  8602. link cpltxt)
  8603. (org-store-link-props :type "image" :file buffer-file-name))
  8604. ;; In dired, store a link to the file of the current line
  8605. ((eq major-mode 'dired-mode)
  8606. (let ((file (dired-get-filename nil t)))
  8607. (setq file (if file
  8608. (abbreviate-file-name
  8609. (expand-file-name (dired-get-filename nil t)))
  8610. ;; otherwise, no file so use current directory.
  8611. default-directory))
  8612. (setq cpltxt (concat "file:" file)
  8613. link cpltxt)))
  8614. ((setq search (run-hook-with-args-until-success
  8615. 'org-create-file-search-functions))
  8616. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  8617. "::" search))
  8618. (setq cpltxt (or description link)))
  8619. ((and (buffer-file-name (buffer-base-buffer)) (derived-mode-p 'org-mode))
  8620. (setq custom-id (org-entry-get nil "CUSTOM_ID"))
  8621. (cond
  8622. ;; Store a link using the target at point
  8623. ((org-in-regexp "[^<]<<\\([^<>]+\\)>>[^>]" 1)
  8624. (setq cpltxt
  8625. (concat "file:"
  8626. (abbreviate-file-name
  8627. (buffer-file-name (buffer-base-buffer)))
  8628. "::" (match-string 1))
  8629. link cpltxt))
  8630. ((and (featurep 'org-id)
  8631. (or (eq org-id-link-to-org-use-id t)
  8632. (and (org-called-interactively-p 'any)
  8633. (or (eq org-id-link-to-org-use-id 'create-if-interactive)
  8634. (and (eq org-id-link-to-org-use-id
  8635. 'create-if-interactive-and-no-custom-id)
  8636. (not custom-id))))
  8637. (and org-id-link-to-org-use-id (org-entry-get nil "ID"))))
  8638. ;; Store a link using the ID at point
  8639. (setq link (condition-case nil
  8640. (prog1 (org-id-store-link)
  8641. (setq desc (or (plist-get org-store-link-plist
  8642. :description)
  8643. "")))
  8644. (error
  8645. ;; Probably before first headline, link only to file
  8646. (concat "file:"
  8647. (abbreviate-file-name
  8648. (buffer-file-name (buffer-base-buffer))))))))
  8649. (t
  8650. ;; Just link to current headline
  8651. (setq cpltxt (concat "file:"
  8652. (abbreviate-file-name
  8653. (buffer-file-name (buffer-base-buffer)))))
  8654. ;; Add a context search string
  8655. (when (org-xor org-context-in-file-links arg)
  8656. (let* ((ee (org-element-at-point))
  8657. (et (org-element-type ee))
  8658. (ev (plist-get (cadr ee) :value))
  8659. (ek (plist-get (cadr ee) :key))
  8660. (eok (and (stringp ek) (string-match "name" ek))))
  8661. (setq txt (cond
  8662. ((org-at-heading-p) nil)
  8663. ((and (eq et 'keyword) eok) ev)
  8664. ((org-region-active-p)
  8665. (buffer-substring (region-beginning) (region-end)))))
  8666. (when (or (null txt) (string-match "\\S-" txt))
  8667. (setq cpltxt
  8668. (concat cpltxt "::"
  8669. (condition-case nil
  8670. (org-make-org-heading-search-string txt)
  8671. (error "")))
  8672. desc (or (and (eq et 'keyword) eok ev)
  8673. (nth 4 (ignore-errors (org-heading-components)))
  8674. "NONE")))))
  8675. (if (string-match "::\\'" cpltxt)
  8676. (setq cpltxt (substring cpltxt 0 -2)))
  8677. (setq link cpltxt))))
  8678. ((buffer-file-name (buffer-base-buffer))
  8679. ;; Just link to this file here.
  8680. (setq cpltxt (concat "file:"
  8681. (abbreviate-file-name
  8682. (buffer-file-name (buffer-base-buffer)))))
  8683. ;; Add a context string.
  8684. (when (org-xor org-context-in-file-links arg)
  8685. (setq txt (if (org-region-active-p)
  8686. (buffer-substring (region-beginning) (region-end))
  8687. (buffer-substring (point-at-bol) (point-at-eol))))
  8688. ;; Only use search option if there is some text.
  8689. (when (string-match "\\S-" txt)
  8690. (setq cpltxt
  8691. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  8692. desc "NONE")))
  8693. (setq link cpltxt))
  8694. ((org-called-interactively-p 'interactive)
  8695. (user-error "No method for storing a link from this buffer"))
  8696. (t (setq link nil)))
  8697. ;; We're done setting link and desc, clean up
  8698. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  8699. (setq link (or link cpltxt)
  8700. desc (or desc cpltxt))
  8701. (cond ((equal desc "NONE") (setq desc nil))
  8702. ((and desc (string-match org-bracket-link-analytic-regexp desc))
  8703. (let ((d0 (match-string 3 desc))
  8704. (p0 (match-string 5 desc)))
  8705. (setq desc
  8706. (replace-regexp-in-string
  8707. org-bracket-link-regexp
  8708. (concat (or p0 d0)
  8709. (if (equal (length (match-string 0 desc))
  8710. (length desc)) "*" "")) desc)))))
  8711. ;; Return the link
  8712. (if (not (and (or (org-called-interactively-p 'any)
  8713. executing-kbd-macro) link))
  8714. (or agenda-link (and link (org-make-link-string link desc)))
  8715. (push (list link desc) org-stored-links)
  8716. (message "Stored: %s" (or desc link))
  8717. (when custom-id
  8718. (setq link (concat "file:" (abbreviate-file-name
  8719. (buffer-file-name)) "::#" custom-id))
  8720. (push (list link desc) org-stored-links))
  8721. (car org-stored-links))))))
  8722. (defun org-store-link-props (&rest plist)
  8723. "Store link properties, extract names and addresses."
  8724. (let (x adr)
  8725. (when (setq x (plist-get plist :from))
  8726. (setq adr (mail-extract-address-components x))
  8727. (setq plist (plist-put plist :fromname (car adr)))
  8728. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  8729. (when (setq x (plist-get plist :to))
  8730. (setq adr (mail-extract-address-components x))
  8731. (setq plist (plist-put plist :toname (car adr)))
  8732. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  8733. (let ((from (plist-get plist :from))
  8734. (to (plist-get plist :to)))
  8735. (when (and from to org-from-is-user-regexp)
  8736. (setq plist
  8737. (plist-put plist :fromto
  8738. (if (string-match org-from-is-user-regexp from)
  8739. (concat "to %t")
  8740. (concat "from %f"))))))
  8741. (setq org-store-link-plist plist))
  8742. (defun org-add-link-props (&rest plist)
  8743. "Add these properties to the link property list."
  8744. (let (key value)
  8745. (while plist
  8746. (setq key (pop plist) value (pop plist))
  8747. (setq org-store-link-plist
  8748. (plist-put org-store-link-plist key value)))))
  8749. (defun org-email-link-description (&optional fmt)
  8750. "Return the description part of an email link.
  8751. This takes information from `org-store-link-plist' and formats it
  8752. according to FMT (default from `org-email-link-description-format')."
  8753. (setq fmt (or fmt org-email-link-description-format))
  8754. (let* ((p org-store-link-plist)
  8755. (to (plist-get p :toaddress))
  8756. (from (plist-get p :fromaddress))
  8757. (table
  8758. (list
  8759. (cons "%c" (plist-get p :fromto))
  8760. (cons "%F" (plist-get p :from))
  8761. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  8762. (cons "%T" (plist-get p :to))
  8763. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  8764. (cons "%s" (plist-get p :subject))
  8765. (cons "%d" (plist-get p :date))
  8766. (cons "%m" (plist-get p :message-id)))))
  8767. (when (string-match "%c" fmt)
  8768. ;; Check if the user wrote this message
  8769. (if (and org-from-is-user-regexp from to
  8770. (save-match-data (string-match org-from-is-user-regexp from)))
  8771. (setq fmt (replace-match "to %t" t t fmt))
  8772. (setq fmt (replace-match "from %f" t t fmt))))
  8773. (org-replace-escapes fmt table)))
  8774. (defun org-make-org-heading-search-string (&optional string)
  8775. "Make search string for the current headline or STRING."
  8776. (let ((s (or string
  8777. (and (derived-mode-p 'org-mode)
  8778. (save-excursion
  8779. (org-back-to-heading t)
  8780. (org-element-property :raw-value (org-element-at-point))))))
  8781. (lines org-context-in-file-links))
  8782. (or string (setq s (concat "*" s))) ; Add * for headlines
  8783. (setq s (replace-regexp-in-string "\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" "" s))
  8784. (when (and string (integerp lines) (> lines 0))
  8785. (let ((slines (org-split-string s "\n")))
  8786. (when (< lines (length slines))
  8787. (setq s (mapconcat
  8788. 'identity
  8789. (reverse (nthcdr (- (length slines) lines)
  8790. (reverse slines))) "\n")))))
  8791. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  8792. (defun org-make-link-string (link &optional description)
  8793. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  8794. (unless (string-match "\\S-" link)
  8795. (error "Empty link"))
  8796. (when (and description
  8797. (stringp description)
  8798. (not (string-match "\\S-" description)))
  8799. (setq description nil))
  8800. (when (stringp description)
  8801. ;; Remove brackets from the description, they are fatal.
  8802. (while (string-match "\\[" description)
  8803. (setq description (replace-match "{" t t description)))
  8804. (while (string-match "\\]" description)
  8805. (setq description (replace-match "}" t t description))))
  8806. (when (equal link description)
  8807. ;; No description needed, it is identical
  8808. (setq description nil))
  8809. (when (and (not description)
  8810. (not (string-match (org-image-file-name-regexp) link))
  8811. (not (equal link (org-link-escape link))))
  8812. (setq description (org-extract-attributes link)))
  8813. (setq link
  8814. (cond ((string-match (org-image-file-name-regexp) link) link)
  8815. ((string-match org-link-types-re link)
  8816. (concat (match-string 1 link)
  8817. (org-link-escape (substring link (match-end 1)))))
  8818. (t (org-link-escape link))))
  8819. (concat "[[" link "]"
  8820. (if description (concat "[" description "]") "")
  8821. "]"))
  8822. (defconst org-link-escape-chars
  8823. '(?\ ?\[ ?\] ?\; ?\= ?\+)
  8824. "List of characters that should be escaped in link.
  8825. This is the list that is used for internal purposes.")
  8826. (defconst org-link-escape-chars-browser
  8827. '(?\ ?\")
  8828. "List of escapes for characters that are problematic in links.
  8829. This is the list that is used before handing over to the browser.")
  8830. (defun org-link-escape (text &optional table merge)
  8831. "Return percent escaped representation of TEXT.
  8832. TEXT is a string with the text to escape.
  8833. Optional argument TABLE is a list with characters that should be
  8834. escaped. When nil, `org-link-escape-chars' is used.
  8835. If optional argument MERGE is set, merge TABLE into
  8836. `org-link-escape-chars'."
  8837. (cond
  8838. ((and table merge)
  8839. (mapc (lambda (defchr)
  8840. (unless (member defchr table)
  8841. (setq table (cons defchr table)))) org-link-escape-chars))
  8842. ((null table)
  8843. (setq table org-link-escape-chars)))
  8844. (mapconcat
  8845. (lambda (char)
  8846. (if (or (member char table)
  8847. (and (or (< char 32) (= char 37) (> char 126))
  8848. org-url-hexify-p))
  8849. (mapconcat (lambda (sequence-element)
  8850. (format "%%%.2X" sequence-element))
  8851. (or (encode-coding-char char 'utf-8)
  8852. (error "Unable to percent escape character: %s"
  8853. (char-to-string char))) "")
  8854. (char-to-string char))) text ""))
  8855. (defun org-link-unescape (str)
  8856. "Unhex hexified Unicode strings as returned from the JavaScript function
  8857. encodeURIComponent. E.g. `%C3%B6' is the german o-Umlaut."
  8858. (unless (and (null str) (string= "" str))
  8859. (let ((pos 0) (case-fold-search t) unhexed)
  8860. (while (setq pos (string-match "\\(%[0-9a-f][0-9a-f]\\)+" str pos))
  8861. (setq unhexed (org-link-unescape-compound (match-string 0 str)))
  8862. (setq str (replace-match unhexed t t str))
  8863. (setq pos (+ pos (length unhexed))))))
  8864. str)
  8865. (defun org-link-unescape-compound (hex)
  8866. "Unhexify Unicode hex-chars. E.g. `%C3%B6' is the German o-Umlaut.
  8867. Note: this function also decodes single byte encodings like
  8868. `%E1' (a-acute) if not followed by another `%[A-F0-9]{2}' group."
  8869. (save-match-data
  8870. (let* ((bytes (cdr (split-string hex "%")))
  8871. (ret "")
  8872. (eat 0)
  8873. (sum 0))
  8874. (while bytes
  8875. (let* ((val (string-to-number (pop bytes) 16))
  8876. (shift-xor
  8877. (if (= 0 eat)
  8878. (cond
  8879. ((>= val 252) (cons 6 252))
  8880. ((>= val 248) (cons 5 248))
  8881. ((>= val 240) (cons 4 240))
  8882. ((>= val 224) (cons 3 224))
  8883. ((>= val 192) (cons 2 192))
  8884. (t (cons 0 0)))
  8885. (cons 6 128))))
  8886. (if (>= val 192) (setq eat (car shift-xor)))
  8887. (setq val (logxor val (cdr shift-xor)))
  8888. (setq sum (+ (lsh sum (car shift-xor)) val))
  8889. (if (> eat 0) (setq eat (- eat 1)))
  8890. (cond
  8891. ((= 0 eat) ;multi byte
  8892. (setq ret (concat ret (org-char-to-string sum)))
  8893. (setq sum 0))
  8894. ((not bytes) ; single byte(s)
  8895. (setq ret (org-link-unescape-single-byte-sequence hex))))
  8896. )) ;; end (while bytes
  8897. ret )))
  8898. (defun org-link-unescape-single-byte-sequence (hex)
  8899. "Unhexify hex-encoded single byte character sequences."
  8900. (mapconcat (lambda (byte)
  8901. (char-to-string (string-to-number byte 16)))
  8902. (cdr (split-string hex "%")) ""))
  8903. (defun org-xor (a b)
  8904. "Exclusive or."
  8905. (if a (not b) b))
  8906. (defun org-fixup-message-id-for-http (s)
  8907. "Replace special characters in a message id, so it can be used in an http query."
  8908. (when (string-match "%" s)
  8909. (setq s (mapconcat (lambda (c)
  8910. (if (eq c ?%)
  8911. "%25"
  8912. (char-to-string c)))
  8913. s "")))
  8914. (while (string-match "<" s)
  8915. (setq s (replace-match "%3C" t t s)))
  8916. (while (string-match ">" s)
  8917. (setq s (replace-match "%3E" t t s)))
  8918. (while (string-match "@" s)
  8919. (setq s (replace-match "%40" t t s)))
  8920. s)
  8921. (defun org-link-prettify (link)
  8922. "Return a human-readable representation of LINK.
  8923. The car of LINK must be a raw link the cdr of LINK must be either
  8924. a link description or nil."
  8925. (let ((desc (or (cadr link) "<no description>")))
  8926. (concat (format "%-45s" (substring desc 0 (min (length desc) 40)))
  8927. "<" (car link) ">")))
  8928. ;;;###autoload
  8929. (defun org-insert-link-global ()
  8930. "Insert a link like Org-mode does.
  8931. This command can be called in any mode to insert a link in Org-mode syntax."
  8932. (interactive)
  8933. (org-load-modules-maybe)
  8934. (org-run-like-in-org-mode 'org-insert-link))
  8935. (defun org-insert-all-links (&optional keep)
  8936. "Insert all links in `org-stored-links'."
  8937. (interactive "P")
  8938. (let ((links (copy-sequence org-stored-links)) l)
  8939. (while (setq l (if keep (pop links) (pop org-stored-links)))
  8940. (insert "- ")
  8941. (org-insert-link nil (car l) (or (cadr l) "<no description>"))
  8942. (insert "\n"))))
  8943. (defun org-link-fontify-links-to-this-file ()
  8944. "Fontify links to the current file in `org-stored-links'."
  8945. (let ((f (buffer-file-name)) a b)
  8946. (setq a (mapcar (lambda(l)
  8947. (let ((ll (car l)))
  8948. (when (and (string-match "^file:\\(.+\\)::" ll)
  8949. (equal f (expand-file-name (match-string 1 ll))))
  8950. ll)))
  8951. org-stored-links))
  8952. (when (featurep 'org-id)
  8953. (setq b (mapcar (lambda(l)
  8954. (let ((ll (car l)))
  8955. (when (and (string-match "^id:\\(.+\\)$" ll)
  8956. (equal f (expand-file-name
  8957. (or (org-id-find-id-file
  8958. (match-string 1 ll)) ""))))
  8959. ll)))
  8960. org-stored-links)))
  8961. (mapcar (lambda(l)
  8962. (put-text-property 0 (length l) 'face 'font-lock-comment-face l))
  8963. (delq nil (append a b)))))
  8964. (defvar org-link-links-in-this-file nil)
  8965. (defun org-insert-link (&optional complete-file link-location default-description)
  8966. "Insert a link. At the prompt, enter the link.
  8967. Completion can be used to insert any of the link protocol prefixes like
  8968. http or ftp in use.
  8969. The history can be used to select a link previously stored with
  8970. `org-store-link'. When the empty string is entered (i.e. if you just
  8971. press RET at the prompt), the link defaults to the most recently
  8972. stored link. As SPC triggers completion in the minibuffer, you need to
  8973. use M-SPC or C-q SPC to force the insertion of a space character.
  8974. You will also be prompted for a description, and if one is given, it will
  8975. be displayed in the buffer instead of the link.
  8976. If there is already a link at point, this command will allow you to edit link
  8977. and description parts.
  8978. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  8979. be selected using completion. The path to the file will be relative to the
  8980. current directory if the file is in the current directory or a subdirectory.
  8981. Otherwise, the link will be the absolute path as completed in the minibuffer
  8982. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  8983. option `org-link-file-path-type'.
  8984. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  8985. the current directory or below.
  8986. With three \\[universal-argument] prefixes, negate the meaning of
  8987. `org-keep-stored-link-after-insertion'.
  8988. If `org-make-link-description-function' is non-nil, this function will be
  8989. called with the link target, and the result will be the default
  8990. link description.
  8991. If the LINK-LOCATION parameter is non-nil, this value will be
  8992. used as the link location instead of reading one interactively.
  8993. If the DEFAULT-DESCRIPTION parameter is non-nil, this value will
  8994. be used as the default description."
  8995. (interactive "P")
  8996. (let* ((wcf (current-window-configuration))
  8997. (origbuf (current-buffer))
  8998. (region (if (org-region-active-p)
  8999. (buffer-substring (region-beginning) (region-end))))
  9000. (remove (and region (list (region-beginning) (region-end))))
  9001. (desc region)
  9002. tmphist ; byte-compile incorrectly complains about this
  9003. (link link-location)
  9004. (abbrevs org-link-abbrev-alist-local)
  9005. entry file all-prefixes auto-desc)
  9006. (cond
  9007. (link-location) ; specified by arg, just use it.
  9008. ((org-in-regexp org-bracket-link-regexp 1)
  9009. ;; We do have a link at point, and we are going to edit it.
  9010. (setq remove (list (match-beginning 0) (match-end 0)))
  9011. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  9012. (setq link (read-string "Link: "
  9013. (org-link-unescape
  9014. (org-match-string-no-properties 1)))))
  9015. ((or (org-in-regexp org-angle-link-re)
  9016. (org-in-regexp org-plain-link-re))
  9017. ;; Convert to bracket link
  9018. (setq remove (list (match-beginning 0) (match-end 0))
  9019. link (read-string "Link: "
  9020. (org-remove-angle-brackets (match-string 0)))))
  9021. ((member complete-file '((4) (16)))
  9022. ;; Completing read for file names.
  9023. (setq link (org-file-complete-link complete-file)))
  9024. (t
  9025. ;; Read link, with completion for stored links.
  9026. (org-link-fontify-links-to-this-file)
  9027. (org-switch-to-buffer-other-window "*Org Links*")
  9028. (with-current-buffer "*Org Links*"
  9029. (erase-buffer)
  9030. (insert "Insert a link.
  9031. Use TAB to complete link prefixes, then RET for type-specific completion support\n")
  9032. (when org-stored-links
  9033. (insert "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  9034. (insert (mapconcat 'org-link-prettify
  9035. (reverse org-stored-links) "\n")))
  9036. (goto-char (point-min)))
  9037. (let ((cw (selected-window)))
  9038. (select-window (get-buffer-window "*Org Links*" 'visible))
  9039. (with-current-buffer "*Org Links*" (setq truncate-lines t))
  9040. (unless (pos-visible-in-window-p (point-max))
  9041. (org-fit-window-to-buffer))
  9042. (and (window-live-p cw) (select-window cw)))
  9043. ;; Fake a link history, containing the stored links.
  9044. (setq tmphist (append (mapcar 'car org-stored-links)
  9045. org-insert-link-history))
  9046. (setq all-prefixes (append (mapcar 'car abbrevs)
  9047. (mapcar 'car org-link-abbrev-alist)
  9048. org-link-types))
  9049. (unwind-protect
  9050. (progn
  9051. (setq link
  9052. (org-completing-read
  9053. "Link: "
  9054. (append
  9055. (mapcar (lambda (x) (concat x ":"))
  9056. all-prefixes)
  9057. (mapcar 'car org-stored-links))
  9058. nil nil nil
  9059. 'tmphist
  9060. (caar org-stored-links)))
  9061. (if (not (string-match "\\S-" link))
  9062. (user-error "No link selected"))
  9063. (mapc (lambda(l)
  9064. (when (equal link (cadr l)) (setq link (car l) auto-desc t)))
  9065. org-stored-links)
  9066. (if (or (member link all-prefixes)
  9067. (and (equal ":" (substring link -1))
  9068. (member (substring link 0 -1) all-prefixes)
  9069. (setq link (substring link 0 -1))))
  9070. (setq link (with-current-buffer origbuf
  9071. (org-link-try-special-completion link)))))
  9072. (set-window-configuration wcf)
  9073. (kill-buffer "*Org Links*"))
  9074. (setq entry (assoc link org-stored-links))
  9075. (or entry (push link org-insert-link-history))
  9076. (setq desc (or desc (nth 1 entry)))))
  9077. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  9078. (not org-keep-stored-link-after-insertion))
  9079. (setq org-stored-links (delq (assoc link org-stored-links)
  9080. org-stored-links)))
  9081. (if (and (string-match org-plain-link-re link)
  9082. (not (string-match org-ts-regexp link)))
  9083. ;; URL-like link, normalize the use of angular brackets.
  9084. (setq link (org-remove-angle-brackets link)))
  9085. ;; Check if we are linking to the current file with a search
  9086. ;; option If yes, simplify the link by using only the search
  9087. ;; option.
  9088. (when (and buffer-file-name
  9089. (string-match "^file:\\(.+?\\)::\\(.+\\)" link))
  9090. (let* ((path (match-string 1 link))
  9091. (case-fold-search nil)
  9092. (search (match-string 2 link)))
  9093. (save-match-data
  9094. (if (equal (file-truename buffer-file-name) (file-truename path))
  9095. ;; We are linking to this same file, with a search option
  9096. (setq link search)))))
  9097. ;; Check if we can/should use a relative path. If yes, simplify the link
  9098. (when (string-match "^\\(file:\\|docview:\\)\\(.*\\)" link)
  9099. (let* ((type (match-string 1 link))
  9100. (path (match-string 2 link))
  9101. (origpath path)
  9102. (case-fold-search nil))
  9103. (cond
  9104. ((or (eq org-link-file-path-type 'absolute)
  9105. (equal complete-file '(16)))
  9106. (setq path (abbreviate-file-name (expand-file-name path))))
  9107. ((eq org-link-file-path-type 'noabbrev)
  9108. (setq path (expand-file-name path)))
  9109. ((eq org-link-file-path-type 'relative)
  9110. (setq path (file-relative-name path)))
  9111. (t
  9112. (save-match-data
  9113. (if (string-match (concat "^" (regexp-quote
  9114. (expand-file-name
  9115. (file-name-as-directory
  9116. default-directory))))
  9117. (expand-file-name path))
  9118. ;; We are linking a file with relative path name.
  9119. (setq path (substring (expand-file-name path)
  9120. (match-end 0)))
  9121. (setq path (abbreviate-file-name (expand-file-name path)))))))
  9122. (setq link (concat type path))
  9123. (if (equal desc origpath)
  9124. (setq desc path))))
  9125. (if org-make-link-description-function
  9126. (setq desc
  9127. (or (condition-case nil
  9128. (funcall org-make-link-description-function link desc)
  9129. (error (progn (message "Can't get link description from `%s'"
  9130. (symbol-name org-make-link-description-function))
  9131. (sit-for 2) nil)))
  9132. (read-string "Description: " default-description)))
  9133. (if default-description (setq desc default-description)
  9134. (setq desc (or (and auto-desc desc)
  9135. (read-string "Description: " desc)))))
  9136. (unless (string-match "\\S-" desc) (setq desc nil))
  9137. (if remove (apply 'delete-region remove))
  9138. (insert (org-make-link-string link desc))))
  9139. (defun org-link-try-special-completion (type)
  9140. "If there is completion support for link type TYPE, offer it."
  9141. (let ((fun (intern (concat "org-" type "-complete-link"))))
  9142. (if (functionp fun)
  9143. (funcall fun)
  9144. (read-string "Link (no completion support): " (concat type ":")))))
  9145. (defun org-file-complete-link (&optional arg)
  9146. "Create a file link using completion."
  9147. (let (file link)
  9148. (setq file (org-iread-file-name "File: "))
  9149. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  9150. (pwd1 (file-name-as-directory (abbreviate-file-name
  9151. (expand-file-name ".")))))
  9152. (cond
  9153. ((equal arg '(16))
  9154. (setq link (concat
  9155. "file:"
  9156. (abbreviate-file-name (expand-file-name file)))))
  9157. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  9158. (setq link (concat "file:" (match-string 1 file))))
  9159. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  9160. (expand-file-name file))
  9161. (setq link (concat
  9162. "file:" (match-string 1 (expand-file-name file)))))
  9163. (t (setq link (concat "file:" file)))))
  9164. link))
  9165. (defun org-iread-file-name (&rest args)
  9166. "Read-file-name using `ido-mode' speedup if available.
  9167. ARGS are arguments that may be passed to `ido-read-file-name' or `read-file-name'.
  9168. See `read-file-name' for a description of parameters."
  9169. (org-without-partial-completion
  9170. (if (and org-completion-use-ido
  9171. (fboundp 'ido-read-file-name)
  9172. (boundp 'ido-mode) ido-mode
  9173. (listp (second args)))
  9174. (let ((ido-enter-matching-directory nil))
  9175. (apply 'ido-read-file-name args))
  9176. (apply 'read-file-name args))))
  9177. (defun org-completing-read (&rest args)
  9178. "Completing-read with SPACE being a normal character."
  9179. (let ((enable-recursive-minibuffers t)
  9180. (minibuffer-local-completion-map
  9181. (copy-keymap minibuffer-local-completion-map)))
  9182. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  9183. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  9184. (org-defkey minibuffer-local-completion-map (kbd "C-c !") 'org-time-stamp-inactive)
  9185. (apply 'org-icompleting-read args)))
  9186. (defun org-completing-read-no-i (&rest args)
  9187. (let (org-completion-use-ido org-completion-use-iswitchb)
  9188. (apply 'org-completing-read args)))
  9189. (defun org-iswitchb-completing-read (prompt choices &rest args)
  9190. "Use iswitch as a completing-read replacement to choose from choices.
  9191. PROMPT is a string to prompt with. CHOICES is a list of strings to choose
  9192. from."
  9193. (let* ((iswitchb-use-virtual-buffers nil)
  9194. (iswitchb-make-buflist-hook
  9195. (lambda ()
  9196. (setq iswitchb-temp-buflist choices))))
  9197. (iswitchb-read-buffer prompt)))
  9198. (defun org-icompleting-read (&rest args)
  9199. "Completing-read using `ido-mode' or `iswitchb' speedups if available."
  9200. (org-without-partial-completion
  9201. (if (and org-completion-use-ido
  9202. (fboundp 'ido-completing-read)
  9203. (boundp 'ido-mode) ido-mode
  9204. (listp (second args)))
  9205. (let ((ido-enter-matching-directory nil))
  9206. (apply 'ido-completing-read (concat (car args))
  9207. (if (consp (car (nth 1 args)))
  9208. (mapcar 'car (nth 1 args))
  9209. (nth 1 args))
  9210. (cddr args)))
  9211. (if (and org-completion-use-iswitchb
  9212. (boundp 'iswitchb-mode) iswitchb-mode
  9213. (listp (second args)))
  9214. (apply 'org-iswitchb-completing-read (concat (car args))
  9215. (if (consp (car (nth 1 args)))
  9216. (mapcar 'car (nth 1 args))
  9217. (nth 1 args))
  9218. (cddr args))
  9219. (apply 'completing-read args)))))
  9220. (defun org-extract-attributes (s)
  9221. "Extract the attributes cookie from a string and set as text property."
  9222. (let (a attr (start 0) key value)
  9223. (save-match-data
  9224. (when (string-match "{{\\([^}]+\\)}}$" s)
  9225. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  9226. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  9227. (setq key (match-string 1 a) value (match-string 2 a)
  9228. start (match-end 0)
  9229. attr (plist-put attr (intern key) value))))
  9230. (org-add-props s nil 'org-attr attr))
  9231. s))
  9232. ;;; Opening/following a link
  9233. (defvar org-link-search-failed nil)
  9234. (defvar org-open-link-functions nil
  9235. "Hook for functions finding a plain text link.
  9236. These functions must take a single argument, the link content.
  9237. They will be called for links that look like [[link text][description]]
  9238. when LINK TEXT does not have a protocol like \"http:\" and does not look
  9239. like a filename (e.g. \"./blue.png\").
  9240. These functions will be called *before* Org attempts to resolve the
  9241. link by doing text searches in the current buffer - so if you want a
  9242. link \"[[target]]\" to still find \"<<target>>\", your function should
  9243. handle this as a special case.
  9244. When the function does handle the link, it must return a non-nil value.
  9245. If it decides that it is not responsible for this link, it must return
  9246. nil to indicate that that Org-mode can continue with other options
  9247. like exact and fuzzy text search.")
  9248. (defun org-next-link (&optional search-backward)
  9249. "Move forward to the next link.
  9250. If the link is in hidden text, expose it."
  9251. (interactive "P")
  9252. (when (and org-link-search-failed (eq this-command last-command))
  9253. (goto-char (point-min))
  9254. (message "Link search wrapped back to beginning of buffer"))
  9255. (setq org-link-search-failed nil)
  9256. (let* ((pos (point))
  9257. (ct (org-context))
  9258. (a (assoc :link ct))
  9259. (srch-fun (if search-backward 're-search-backward 're-search-forward)))
  9260. (cond (a (goto-char (nth (if search-backward 1 2) a)))
  9261. ((looking-at org-any-link-re)
  9262. ;; Don't stay stuck at link without an org-link face
  9263. (forward-char (if search-backward -1 1))))
  9264. (if (funcall srch-fun org-any-link-re nil t)
  9265. (progn
  9266. (goto-char (match-beginning 0))
  9267. (if (outline-invisible-p) (org-show-context)))
  9268. (goto-char pos)
  9269. (setq org-link-search-failed t)
  9270. (message "No further link found"))))
  9271. (defun org-previous-link ()
  9272. "Move backward to the previous link.
  9273. If the link is in hidden text, expose it."
  9274. (interactive)
  9275. (funcall 'org-next-link t))
  9276. (defun org-translate-link (s)
  9277. "Translate a link string if a translation function has been defined."
  9278. (if (and org-link-translation-function
  9279. (fboundp org-link-translation-function)
  9280. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  9281. (progn
  9282. (setq s (funcall org-link-translation-function
  9283. (match-string 1 s) (match-string 2 s)))
  9284. (concat (car s) ":" (cdr s)))
  9285. s))
  9286. (defun org-translate-link-from-planner (type path)
  9287. "Translate a link from Emacs Planner syntax so that Org can follow it.
  9288. This is still an experimental function, your mileage may vary."
  9289. (cond
  9290. ((member type '("http" "https" "news" "ftp"))
  9291. ;; standard Internet links are the same.
  9292. nil)
  9293. ((and (equal type "irc") (string-match "^//" path))
  9294. ;; Planner has two / at the beginning of an irc link, we have 1.
  9295. ;; We should have zero, actually....
  9296. (setq path (substring path 1)))
  9297. ((and (equal type "lisp") (string-match "^/" path))
  9298. ;; Planner has a slash, we do not.
  9299. (setq type "elisp" path (substring path 1)))
  9300. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  9301. ;; A typical message link. Planner has the id after the final slash,
  9302. ;; we separate it with a hash mark
  9303. (setq path (concat (match-string 1 path) "#"
  9304. (org-remove-angle-brackets (match-string 2 path))))))
  9305. (cons type path))
  9306. (defun org-find-file-at-mouse (ev)
  9307. "Open file link or URL at mouse."
  9308. (interactive "e")
  9309. (mouse-set-point ev)
  9310. (org-open-at-point 'in-emacs))
  9311. (defun org-open-at-mouse (ev)
  9312. "Open file link or URL at mouse.
  9313. See the docstring of `org-open-file' for details."
  9314. (interactive "e")
  9315. (mouse-set-point ev)
  9316. (if (eq major-mode 'org-agenda-mode)
  9317. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  9318. (org-open-at-point))
  9319. (defvar org-window-config-before-follow-link nil
  9320. "The window configuration before following a link.
  9321. This is saved in case the need arises to restore it.")
  9322. (defvar org-open-link-marker (make-marker)
  9323. "Marker pointing to the location where `org-open-at-point' was called.")
  9324. ;;;###autoload
  9325. (defun org-open-at-point-global ()
  9326. "Follow a link like Org-mode does.
  9327. This command can be called in any mode to follow a link that has
  9328. Org-mode syntax."
  9329. (interactive)
  9330. (org-run-like-in-org-mode 'org-open-at-point))
  9331. ;;;###autoload
  9332. (defun org-open-link-from-string (s &optional arg reference-buffer)
  9333. "Open a link in the string S, as if it was in Org-mode."
  9334. (interactive "sLink: \nP")
  9335. (let ((reference-buffer (or reference-buffer (current-buffer))))
  9336. (with-temp-buffer
  9337. (let ((org-inhibit-startup (not reference-buffer)))
  9338. (org-mode)
  9339. (insert s)
  9340. (goto-char (point-min))
  9341. (when reference-buffer
  9342. (setq org-link-abbrev-alist-local
  9343. (with-current-buffer reference-buffer
  9344. org-link-abbrev-alist-local)))
  9345. (org-open-at-point arg reference-buffer)))))
  9346. (defvar org-open-at-point-functions nil
  9347. "Hook that is run when following a link at point.
  9348. Functions in this hook must return t if they identify and follow
  9349. a link at point. If they don't find anything interesting at point,
  9350. they must return nil.")
  9351. (defvar org-link-search-inhibit-query nil) ;; dynamically scoped
  9352. (defvar clean-buffer-list-kill-buffer-names) ; Defined in midnight.el
  9353. (defun org-open-at-point (&optional arg reference-buffer)
  9354. "Open link at or after point.
  9355. If there is no link at point, this function will search forward up to
  9356. the end of the current line.
  9357. Normally, files will be opened by an appropriate application. If the
  9358. optional prefix argument ARG is non-nil, Emacs will visit the file.
  9359. With a double prefix argument, try to open outside of Emacs, in the
  9360. application the system uses for this file type."
  9361. (interactive "P")
  9362. ;; if in a code block, then open the block's results
  9363. (unless (call-interactively #'org-babel-open-src-block-result)
  9364. (org-load-modules-maybe)
  9365. (move-marker org-open-link-marker (point))
  9366. (setq org-window-config-before-follow-link (current-window-configuration))
  9367. (org-remove-occur-highlights nil nil t)
  9368. (cond
  9369. ((and (org-at-heading-p)
  9370. (not (org-at-timestamp-p t))
  9371. (not (org-in-regexp
  9372. (concat org-plain-link-re "\\|"
  9373. org-bracket-link-regexp "\\|"
  9374. org-angle-link-re "\\|"
  9375. "[ \t]:[^ \t\n]+:[ \t]*$")))
  9376. (not (get-text-property (point) 'org-linked-text)))
  9377. (or (let* ((lkall (org-offer-links-in-entry (current-buffer) (point) arg))
  9378. (lk0 (car lkall))
  9379. (lk (if (stringp lk0) (list lk0) lk0))
  9380. (lkend (cdr lkall)))
  9381. (mapcar (lambda(l)
  9382. (search-forward l nil lkend)
  9383. (goto-char (match-beginning 0))
  9384. (org-open-at-point))
  9385. lk))
  9386. (progn (require 'org-attach) (org-attach-reveal 'if-exists))))
  9387. ((run-hook-with-args-until-success 'org-open-at-point-functions))
  9388. ((and (org-at-timestamp-p t)
  9389. (not (org-in-regexp org-bracket-link-regexp)))
  9390. (org-follow-timestamp-link))
  9391. ((and (or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  9392. (not (org-in-regexp org-any-link-re)))
  9393. (org-footnote-action))
  9394. (t
  9395. (let (type path link line search (pos (point)))
  9396. (catch 'match
  9397. (save-excursion
  9398. (or (org-in-regexp org-plain-link-re)
  9399. (skip-chars-forward "^]\n\r"))
  9400. (when (org-in-regexp org-bracket-link-regexp 1)
  9401. (setq link (org-extract-attributes
  9402. (org-link-unescape (org-match-string-no-properties 1))))
  9403. (while (string-match " *\n *" link)
  9404. (setq link (replace-match " " t t link)))
  9405. (setq link (org-link-expand-abbrev link))
  9406. (cond
  9407. ((or (file-name-absolute-p link)
  9408. (string-match "^\\.\\.?/" link))
  9409. (setq type "file" path link))
  9410. ((string-match org-link-re-with-space3 link)
  9411. (setq type (match-string 1 link) path (match-string 2 link)))
  9412. ((string-match "^help:+\\(.+\\)" link)
  9413. (setq type "help" path (match-string 1 link)))
  9414. (t (setq type "thisfile" path link)))
  9415. (throw 'match t)))
  9416. (when (get-text-property (point) 'org-linked-text)
  9417. (setq type "thisfile"
  9418. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  9419. (1+ (point)) (point))
  9420. path (buffer-substring
  9421. (or (previous-single-property-change pos 'org-linked-text)
  9422. (point-min))
  9423. (or (next-single-property-change pos 'org-linked-text)
  9424. (point-max)))
  9425. ;; Ensure we will search for a <<<radio>>> link, not
  9426. ;; a simple reference like <<ref>>
  9427. path (concat "<" path))
  9428. (throw 'match t))
  9429. (save-excursion
  9430. (when (or (org-in-regexp org-angle-link-re)
  9431. (let ((match (org-in-regexp org-plain-link-re)))
  9432. ;; Check a plain link is not within a bracket link
  9433. (and match
  9434. (save-excursion
  9435. (save-match-data
  9436. (progn
  9437. (goto-char (car match))
  9438. (not (org-in-regexp org-bracket-link-regexp)))))))
  9439. (let ((line_ending (save-excursion (end-of-line) (point))))
  9440. ;; We are in a line before a plain or bracket link
  9441. (or (re-search-forward org-plain-link-re line_ending t)
  9442. (re-search-forward org-bracket-link-regexp line_ending t))))
  9443. (setq type (match-string 1)
  9444. path (org-link-unescape (match-string 2)))
  9445. (throw 'match t)))
  9446. (save-excursion
  9447. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@#%:]+\\):[ \t]*$"))
  9448. (setq type "tags"
  9449. path (match-string 1))
  9450. (while (string-match ":" path)
  9451. (setq path (replace-match "+" t t path)))
  9452. (throw 'match t)))
  9453. (when (org-in-regexp "<\\([^><\n]+\\)>")
  9454. (setq type "tree-match"
  9455. path (match-string 1))
  9456. (throw 'match t)))
  9457. (unless path
  9458. (user-error "No link found"))
  9459. ;; switch back to reference buffer
  9460. ;; needed when if called in a temporary buffer through
  9461. ;; org-open-link-from-string
  9462. (with-current-buffer (or reference-buffer (current-buffer))
  9463. ;; Remove any trailing spaces in path
  9464. (if (string-match " +\\'" path)
  9465. (setq path (replace-match "" t t path)))
  9466. (if (and org-link-translation-function
  9467. (fboundp org-link-translation-function))
  9468. ;; Check if we need to translate the link
  9469. (let ((tmp (funcall org-link-translation-function type path)))
  9470. (setq type (car tmp) path (cdr tmp))))
  9471. (cond
  9472. ((assoc type org-link-protocols)
  9473. (funcall (nth 1 (assoc type org-link-protocols)) path))
  9474. ((equal type "help")
  9475. (let ((f-or-v (intern path)))
  9476. (cond ((fboundp f-or-v)
  9477. (describe-function f-or-v))
  9478. ((boundp f-or-v)
  9479. (describe-variable f-or-v))
  9480. (t (error "Not a known function or variable")))))
  9481. ((equal type "mailto")
  9482. (let ((cmd (car org-link-mailto-program))
  9483. (args (cdr org-link-mailto-program)) args1
  9484. (address path) (subject "") a)
  9485. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  9486. (setq address (match-string 1 path)
  9487. subject (org-link-escape (match-string 2 path))))
  9488. (while args
  9489. (cond
  9490. ((not (stringp (car args))) (push (pop args) args1))
  9491. (t (setq a (pop args))
  9492. (if (string-match "%a" a)
  9493. (setq a (replace-match address t t a)))
  9494. (if (string-match "%s" a)
  9495. (setq a (replace-match subject t t a)))
  9496. (push a args1))))
  9497. (apply cmd (nreverse args1))))
  9498. ((member type '("http" "https" "ftp" "news"))
  9499. (browse-url
  9500. (concat type ":"
  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. ((string= type "doi")
  9508. (browse-url
  9509. (concat org-doi-server-url
  9510. (if (org-string-match-p
  9511. (concat "[[:nonascii:]"
  9512. org-link-escape-chars-browser "]")
  9513. path)
  9514. (org-link-escape path org-link-escape-chars-browser)
  9515. path))))
  9516. ((member type '("message"))
  9517. (browse-url (concat type ":" path)))
  9518. ((string= type "tags")
  9519. (org-tags-view arg path))
  9520. ((string= type "tree-match")
  9521. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  9522. ((string= type "file")
  9523. (if (string-match "::\\([0-9]+\\)\\'" path)
  9524. (setq line (string-to-number (match-string 1 path))
  9525. path (substring path 0 (match-beginning 0)))
  9526. (if (string-match "::\\(.+\\)\\'" path)
  9527. (setq search (match-string 1 path)
  9528. path (substring path 0 (match-beginning 0)))))
  9529. (if (string-match "[*?{]" (file-name-nondirectory path))
  9530. (dired path)
  9531. (org-open-file path arg line search)))
  9532. ((string= type "shell")
  9533. (let ((buf (generate-new-buffer "*Org Shell Output"))
  9534. (cmd path))
  9535. (if (or (and (not (string= org-confirm-shell-link-not-regexp ""))
  9536. (string-match org-confirm-shell-link-not-regexp cmd))
  9537. (not org-confirm-shell-link-function)
  9538. (funcall org-confirm-shell-link-function
  9539. (format "Execute \"%s\" in shell? "
  9540. (org-add-props cmd nil
  9541. 'face 'org-warning))))
  9542. (progn
  9543. (message "Executing %s" cmd)
  9544. (shell-command cmd buf)
  9545. (if (featurep 'midnight)
  9546. (setq clean-buffer-list-kill-buffer-names
  9547. (cons buf clean-buffer-list-kill-buffer-names))))
  9548. (error "Abort"))))
  9549. ((string= type "elisp")
  9550. (let ((cmd path))
  9551. (if (or (and (not (string= org-confirm-elisp-link-not-regexp ""))
  9552. (string-match org-confirm-elisp-link-not-regexp cmd))
  9553. (not org-confirm-elisp-link-function)
  9554. (funcall org-confirm-elisp-link-function
  9555. (format "Execute \"%s\" as elisp? "
  9556. (org-add-props cmd nil
  9557. 'face 'org-warning))))
  9558. (message "%s => %s" cmd
  9559. (if (equal (string-to-char cmd) ?\()
  9560. (eval (read cmd))
  9561. (call-interactively (read cmd))))
  9562. (error "Abort"))))
  9563. ((and (string= type "thisfile")
  9564. (or (run-hook-with-args-until-success
  9565. 'org-open-link-functions path)
  9566. (and link
  9567. (string-match "^id:" link)
  9568. (or (featurep 'org-id) (require 'org-id))
  9569. (progn
  9570. (funcall (nth 1 (assoc "id" org-link-protocols))
  9571. (substring path 3))
  9572. t)))))
  9573. ((string= type "thisfile")
  9574. (if arg
  9575. (switch-to-buffer-other-window
  9576. (org-get-buffer-for-internal-link (current-buffer)))
  9577. (org-mark-ring-push))
  9578. (let ((cmd `(org-link-search
  9579. ,path
  9580. ,(cond ((equal arg '(4)) ''occur)
  9581. ((equal arg '(16)) ''org-occur))
  9582. ,pos)))
  9583. (condition-case nil (let ((org-link-search-inhibit-query t))
  9584. (eval cmd))
  9585. (error (progn (widen) (eval cmd))))))
  9586. (t (browse-url-at-point)))))))
  9587. (move-marker org-open-link-marker nil)
  9588. (run-hook-with-args 'org-follow-link-hook)))
  9589. (defsubst org-uniquify (list)
  9590. "Non-destructively remove duplicate elements from LIST."
  9591. (let ((res (copy-sequence list))) (delete-dups res)))
  9592. (defun org-offer-links-in-entry (buffer marker &optional nth zero)
  9593. "Offer links in the current entry and return the selected link.
  9594. If there is only one link, return it.
  9595. If NTH is an integer, return the NTH link found.
  9596. If ZERO is a string, check also this string for a link, and if
  9597. there is one, return it."
  9598. (with-current-buffer buffer
  9599. (save-excursion
  9600. (save-restriction
  9601. (widen)
  9602. (goto-char marker)
  9603. (let ((re (concat "\\(" org-bracket-link-regexp "\\)\\|"
  9604. "\\(" org-angle-link-re "\\)\\|"
  9605. "\\(" org-plain-link-re "\\)"))
  9606. (cnt ?0)
  9607. (in-emacs (if (integerp nth) nil nth))
  9608. have-zero end links link c)
  9609. (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
  9610. (push (match-string 0 zero) links)
  9611. (setq cnt (1- cnt) have-zero t))
  9612. (save-excursion
  9613. (org-back-to-heading t)
  9614. (setq end (save-excursion (outline-next-heading) (point)))
  9615. (while (re-search-forward re end t)
  9616. (push (match-string 0) links))
  9617. (setq links (org-uniquify (reverse links))))
  9618. (cond
  9619. ((null links)
  9620. (message "No links"))
  9621. ((equal (length links) 1)
  9622. (setq link (car links)))
  9623. ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
  9624. (setq link (nth (if have-zero nth (1- nth)) links)))
  9625. (t ; we have to select a link
  9626. (save-excursion
  9627. (save-window-excursion
  9628. (delete-other-windows)
  9629. (with-output-to-temp-buffer "*Select Link*"
  9630. (mapc (lambda (l)
  9631. (if (not (string-match org-bracket-link-regexp l))
  9632. (princ (format "[%c] %s\n" (incf cnt)
  9633. (org-remove-angle-brackets l)))
  9634. (if (match-end 3)
  9635. (princ (format "[%c] %s (%s)\n" (incf cnt)
  9636. (match-string 3 l) (match-string 1 l)))
  9637. (princ (format "[%c] %s\n" (incf cnt)
  9638. (match-string 1 l))))))
  9639. links))
  9640. (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
  9641. (message "Select link to open, RET to open all:")
  9642. (setq c (read-char-exclusive))
  9643. (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
  9644. (when (equal c ?q) (error "Abort"))
  9645. (if (equal c ?\C-m)
  9646. (setq link links)
  9647. (setq nth (- c ?0))
  9648. (if have-zero (setq nth (1+ nth)))
  9649. (unless (and (integerp nth) (>= (length links) nth))
  9650. (user-error "Invalid link selection"))
  9651. (setq link (nth (1- nth) links)))))
  9652. (cons link end))))))
  9653. ;; Add special file links that specify the way of opening
  9654. (org-add-link-type "file+sys" 'org-open-file-with-system)
  9655. (org-add-link-type "file+emacs" 'org-open-file-with-emacs)
  9656. (defun org-open-file-with-system (path)
  9657. "Open file at PATH using the system way of opening it."
  9658. (org-open-file path 'system))
  9659. (defun org-open-file-with-emacs (path)
  9660. "Open file at PATH in Emacs."
  9661. (org-open-file path 'emacs))
  9662. ;;; File search
  9663. (defvar org-create-file-search-functions nil
  9664. "List of functions to construct the right search string for a file link.
  9665. These functions are called in turn with point at the location to
  9666. which the link should point.
  9667. A function in the hook should first test if it would like to
  9668. handle this file type, for example by checking the `major-mode'
  9669. or the file extension. If it decides not to handle this file, it
  9670. should just return nil to give other functions a chance. If it
  9671. does handle the file, it must return the search string to be used
  9672. when following the link. The search string will be part of the
  9673. file link, given after a double colon, and `org-open-at-point'
  9674. will automatically search for it. If special measures must be
  9675. taken to make the search successful, another function should be
  9676. added to the companion hook `org-execute-file-search-functions',
  9677. which see.
  9678. A function in this hook may also use `setq' to set the variable
  9679. `description' to provide a suggestion for the descriptive text to
  9680. be used for this link when it gets inserted into an Org-mode
  9681. buffer with \\[org-insert-link].")
  9682. (defvar org-execute-file-search-functions nil
  9683. "List of functions to execute a file search triggered by a link.
  9684. Functions added to this hook must accept a single argument, the
  9685. search string that was part of the file link, the part after the
  9686. double colon. The function must first check if it would like to
  9687. handle this search, for example by checking the `major-mode' or
  9688. the file extension. If it decides not to handle this search, it
  9689. should just return nil to give other functions a chance. If it
  9690. does handle the search, it must return a non-nil value to keep
  9691. other functions from trying.
  9692. Each function can access the current prefix argument through the
  9693. variable `current-prefix-arg'. Note that a single prefix is used
  9694. to force opening a link in Emacs, so it may be good to only use a
  9695. numeric or double prefix to guide the search function.
  9696. In case this is needed, a function in this hook can also restore
  9697. the window configuration before `org-open-at-point' was called using:
  9698. (set-window-configuration org-window-config-before-follow-link)")
  9699. (defun org-link-search (s &optional type avoid-pos stealth)
  9700. "Search for a link search option.
  9701. If S is surrounded by forward slashes, it is interpreted as a
  9702. regular expression. In org-mode files, this will create an `org-occur'
  9703. sparse tree. In ordinary files, `occur' will be used to list matches.
  9704. If the current buffer is in `dired-mode', grep will be used to search
  9705. in all files. If AVOID-POS is given, ignore matches near that position.
  9706. When optional argument STEALTH is non-nil, do not modify
  9707. visibility around point, thus ignoring
  9708. `org-show-hierarchy-above', `org-show-following-heading' and
  9709. `org-show-siblings' variables."
  9710. (let ((case-fold-search t)
  9711. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  9712. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  9713. (append '(("") (" ") ("\t") ("\n"))
  9714. org-emphasis-alist)
  9715. "\\|") "\\)"))
  9716. (pos (point))
  9717. (pre nil) (post nil)
  9718. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  9719. (cond
  9720. ;; First check if there are any special search functions
  9721. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  9722. ;; Now try the builtin stuff
  9723. ((and (equal (string-to-char s0) ?#)
  9724. (> (length s0) 1)
  9725. (save-excursion
  9726. (goto-char (point-min))
  9727. (and
  9728. (re-search-forward
  9729. (concat "^[ \t]*:CUSTOM_ID:[ \t]+"
  9730. (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
  9731. (setq type 'dedicated
  9732. pos (match-beginning 0))))
  9733. ;; There is an exact target for this
  9734. (goto-char pos)
  9735. (org-back-to-heading t)))
  9736. ((save-excursion
  9737. (goto-char (point-min))
  9738. (and
  9739. (re-search-forward
  9740. (concat "<<" (regexp-quote s0) ">>") nil t)
  9741. (setq type 'dedicated
  9742. pos (match-beginning 0))))
  9743. ;; There is an exact target for this
  9744. (goto-char pos))
  9745. ((save-excursion
  9746. (goto-char (point-min))
  9747. (and
  9748. (re-search-forward
  9749. (format "^[ \t]*#\\+NAME: %s" (regexp-quote s0)) nil t)
  9750. (setq type 'dedicated pos (match-beginning 0))))
  9751. ;; Found an element with a matching #+name affiliated keyword.
  9752. (goto-char pos))
  9753. ((and (string-match "^(\\(.*\\))$" s0)
  9754. (save-excursion
  9755. (goto-char (point-min))
  9756. (and
  9757. (re-search-forward
  9758. (concat "[^[]" (regexp-quote
  9759. (format org-coderef-label-format
  9760. (match-string 1 s0))))
  9761. nil t)
  9762. (setq type 'dedicated
  9763. pos (1+ (match-beginning 0))))))
  9764. ;; There is a coderef target for this
  9765. (goto-char pos))
  9766. ((string-match "^/\\(.*\\)/$" s)
  9767. ;; A regular expression
  9768. (cond
  9769. ((derived-mode-p 'org-mode)
  9770. (org-occur (match-string 1 s)))
  9771. (t (org-do-occur (match-string 1 s)))))
  9772. ((and (derived-mode-p 'org-mode) org-link-search-must-match-exact-headline)
  9773. (and (equal (string-to-char s) ?*) (setq s (substring s 1)))
  9774. (goto-char (point-min))
  9775. (cond
  9776. ((let (case-fold-search)
  9777. (re-search-forward (format org-complex-heading-regexp-format
  9778. (regexp-quote s))
  9779. nil t))
  9780. ;; OK, found a match
  9781. (setq type 'dedicated)
  9782. (goto-char (match-beginning 0)))
  9783. ((and (not org-link-search-inhibit-query)
  9784. (eq org-link-search-must-match-exact-headline 'query-to-create)
  9785. (y-or-n-p "No match - create this as a new heading? "))
  9786. (goto-char (point-max))
  9787. (or (bolp) (newline))
  9788. (insert "* " s "\n")
  9789. (beginning-of-line 0))
  9790. (t
  9791. (goto-char pos)
  9792. (error "No match"))))
  9793. (t
  9794. ;; A normal search string
  9795. (when (equal (string-to-char s) ?*)
  9796. ;; Anchor on headlines, post may include tags.
  9797. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  9798. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@#%:+]:[ \t]*\\)?$")
  9799. s (substring s 1)))
  9800. (remove-text-properties
  9801. 0 (length s)
  9802. '(face nil mouse-face nil keymap nil fontified nil) s)
  9803. ;; Make a series of regular expressions to find a match
  9804. (setq words (org-split-string s "[ \n\r\t]+")
  9805. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  9806. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  9807. "\\)" markers)
  9808. re2a_ (concat "\\(" (mapconcat 'downcase words
  9809. "[ \t\r\n]+") "\\)[ \t\r\n]")
  9810. re2a (concat "[ \t\r\n]" re2a_)
  9811. re4_ (concat "\\(" (mapconcat 'downcase words
  9812. "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  9813. re4 (concat "[^a-zA-Z_]" re4_)
  9814. re1 (concat pre re2 post)
  9815. re3 (concat pre (if pre re4_ re4) post)
  9816. re5 (concat pre ".*" re4)
  9817. re2 (concat pre re2)
  9818. re2a (concat pre (if pre re2a_ re2a))
  9819. re4 (concat pre (if pre re4_ re4))
  9820. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  9821. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  9822. re5 "\\)"))
  9823. (cond
  9824. ((eq type 'org-occur) (org-occur reall))
  9825. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  9826. (t (goto-char (point-min))
  9827. (setq type 'fuzzy)
  9828. (if (or (and (org-search-not-self 1 re0 nil t)
  9829. (setq type 'dedicated))
  9830. (org-search-not-self 1 re1 nil t)
  9831. (org-search-not-self 1 re2 nil t)
  9832. (org-search-not-self 1 re2a nil t)
  9833. (org-search-not-self 1 re3 nil t)
  9834. (org-search-not-self 1 re4 nil t)
  9835. (org-search-not-self 1 re5 nil t))
  9836. (goto-char (match-beginning 1))
  9837. (goto-char pos)
  9838. (error "No match"))))))
  9839. (and (derived-mode-p 'org-mode)
  9840. (not stealth)
  9841. (org-show-context 'link-search))
  9842. type))
  9843. (defun org-search-not-self (group &rest args)
  9844. "Execute `re-search-forward', but only accept matches that do not
  9845. enclose the position of `org-open-link-marker'."
  9846. (let ((m org-open-link-marker))
  9847. (catch 'exit
  9848. (while (apply 're-search-forward args)
  9849. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  9850. (goto-char (match-end group))
  9851. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  9852. (> (match-beginning 0) (marker-position m))
  9853. (< (match-end 0) (marker-position m)))
  9854. (save-match-data
  9855. (or (not (org-in-regexp
  9856. org-bracket-link-analytic-regexp 1))
  9857. (not (match-end 4)) ; no description
  9858. (and (<= (match-beginning 4) (point))
  9859. (>= (match-end 4) (point))))))
  9860. (throw 'exit (point))))))))
  9861. (defun org-get-buffer-for-internal-link (buffer)
  9862. "Return a buffer to be used for displaying the link target of internal links."
  9863. (cond
  9864. ((not org-display-internal-link-with-indirect-buffer)
  9865. buffer)
  9866. ((string-match "(Clone)$" (buffer-name buffer))
  9867. (message "Buffer is already a clone, not making another one")
  9868. ;; we also do not modify visibility in this case
  9869. buffer)
  9870. (t ; make a new indirect buffer for displaying the link
  9871. (let* ((bn (buffer-name buffer))
  9872. (ibn (concat bn "(Clone)"))
  9873. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  9874. (with-current-buffer ib (org-overview))
  9875. ib))))
  9876. (defun org-do-occur (regexp &optional cleanup)
  9877. "Call the Emacs command `occur'.
  9878. If CLEANUP is non-nil, remove the printout of the regular expression
  9879. in the *Occur* buffer. This is useful if the regex is long and not useful
  9880. to read."
  9881. (occur regexp)
  9882. (when cleanup
  9883. (let ((cwin (selected-window)) win beg end)
  9884. (when (setq win (get-buffer-window "*Occur*"))
  9885. (select-window win))
  9886. (goto-char (point-min))
  9887. (when (re-search-forward "match[a-z]+" nil t)
  9888. (setq beg (match-end 0))
  9889. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  9890. (setq end (1- (match-beginning 0)))))
  9891. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  9892. (goto-char (point-min))
  9893. (select-window cwin))))
  9894. ;;; The mark ring for links jumps
  9895. (defvar org-mark-ring nil
  9896. "Mark ring for positions before jumps in Org-mode.")
  9897. (defvar org-mark-ring-last-goto nil
  9898. "Last position in the mark ring used to go back.")
  9899. ;; Fill and close the ring
  9900. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  9901. (loop for i from 1 to org-mark-ring-length do
  9902. (push (make-marker) org-mark-ring))
  9903. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  9904. org-mark-ring)
  9905. (defun org-mark-ring-push (&optional pos buffer)
  9906. "Put the current position or POS into the mark ring and rotate it."
  9907. (interactive)
  9908. (setq pos (or pos (point)))
  9909. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  9910. (move-marker (car org-mark-ring)
  9911. (or pos (point))
  9912. (or buffer (current-buffer)))
  9913. (message "%s"
  9914. (substitute-command-keys
  9915. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  9916. (defun org-mark-ring-goto (&optional n)
  9917. "Jump to the previous position in the mark ring.
  9918. With prefix arg N, jump back that many stored positions. When
  9919. called several times in succession, walk through the entire ring.
  9920. Org-mode commands jumping to a different position in the current file,
  9921. or to another Org-mode file, automatically push the old position
  9922. onto the ring."
  9923. (interactive "p")
  9924. (let (p m)
  9925. (if (eq last-command this-command)
  9926. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  9927. (setq p org-mark-ring))
  9928. (setq org-mark-ring-last-goto p)
  9929. (setq m (car p))
  9930. (org-pop-to-buffer-same-window (marker-buffer m))
  9931. (goto-char m)
  9932. (if (or (outline-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  9933. (defun org-remove-angle-brackets (s)
  9934. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  9935. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  9936. s)
  9937. (defun org-add-angle-brackets (s)
  9938. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  9939. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  9940. s)
  9941. (defun org-remove-double-quotes (s)
  9942. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  9943. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  9944. s)
  9945. ;;; Following specific links
  9946. (defun org-follow-timestamp-link ()
  9947. "Open an agenda view for the time-stamp date/range at point."
  9948. (cond
  9949. ((org-at-date-range-p t)
  9950. (let ((org-agenda-start-on-weekday)
  9951. (t1 (match-string 1))
  9952. (t2 (match-string 2)) tt1 tt2)
  9953. (setq tt1 (time-to-days (org-time-string-to-time t1))
  9954. tt2 (time-to-days (org-time-string-to-time t2)))
  9955. (let ((org-agenda-buffer-tmp-name
  9956. (format "*Org Agenda(a:%s)"
  9957. (concat (substring t1 0 10) "--" (substring t2 0 10)))))
  9958. (org-agenda-list nil tt1 (1+ (- tt2 tt1))))))
  9959. ((org-at-timestamp-p t)
  9960. (let ((org-agenda-buffer-tmp-name
  9961. (format "*Org Agenda(a:%s)" (substring (match-string 1) 0 10))))
  9962. (org-agenda-list nil (time-to-days (org-time-string-to-time
  9963. (substring (match-string 1) 0 10)))
  9964. 1)))
  9965. (t (error "This should not happen"))))
  9966. ;;; Following file links
  9967. (declare-function mailcap-parse-mailcaps "mailcap" (&optional path force))
  9968. (declare-function mailcap-extension-to-mime "mailcap" (extn))
  9969. (declare-function mailcap-mime-info
  9970. "mailcap" (string &optional request no-decode))
  9971. (defvar org-wait nil)
  9972. (defun org-open-file (path &optional in-emacs line search)
  9973. "Open the file at PATH.
  9974. First, this expands any special file name abbreviations. Then the
  9975. configuration variable `org-file-apps' is checked if it contains an
  9976. entry for this file type, and if yes, the corresponding command is launched.
  9977. If no application is found, Emacs simply visits the file.
  9978. With optional prefix argument IN-EMACS, Emacs will visit the file.
  9979. With a double \\[universal-argument] \\[universal-argument] \
  9980. prefix arg, Org tries to avoid opening in Emacs
  9981. and to use an external application to visit the file.
  9982. Optional LINE specifies a line to go to, optional SEARCH a string
  9983. to search for. If LINE or SEARCH is given, the file will be
  9984. opened in Emacs, unless an entry from org-file-apps that makes
  9985. use of groups in a regexp matches.
  9986. If you want to change the way frames are used when following a
  9987. link, please customize `org-link-frame-setup'.
  9988. If the file does not exist, an error is thrown."
  9989. (let* ((file (if (equal path "")
  9990. buffer-file-name
  9991. (substitute-in-file-name (expand-file-name path))))
  9992. (file-apps (append org-file-apps (org-default-apps)))
  9993. (apps (org-remove-if
  9994. 'org-file-apps-entry-match-against-dlink-p file-apps))
  9995. (apps-dlink (org-remove-if-not
  9996. 'org-file-apps-entry-match-against-dlink-p file-apps))
  9997. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  9998. (dirp (if remp nil (file-directory-p file)))
  9999. (file (if (and dirp org-open-directory-means-index-dot-org)
  10000. (concat (file-name-as-directory file) "index.org")
  10001. file))
  10002. (a-m-a-p (assq 'auto-mode apps))
  10003. (dfile (downcase file))
  10004. ;; reconstruct the original file: link from the PATH, LINE and SEARCH args
  10005. (link (cond ((and (eq line nil)
  10006. (eq search nil))
  10007. file)
  10008. (line
  10009. (concat file "::" (number-to-string line)))
  10010. (search
  10011. (concat file "::" search))))
  10012. (dlink (downcase link))
  10013. (old-buffer (current-buffer))
  10014. (old-pos (point))
  10015. (old-mode major-mode)
  10016. ext cmd link-match-data)
  10017. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  10018. (setq ext (match-string 1 dfile))
  10019. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  10020. (setq ext (match-string 1 dfile))))
  10021. (cond
  10022. ((member in-emacs '((16) system))
  10023. (setq cmd (cdr (assoc 'system apps))))
  10024. (in-emacs (setq cmd 'emacs))
  10025. (t
  10026. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  10027. (and dirp (cdr (assoc 'directory apps)))
  10028. ; first, try matching against apps-dlink
  10029. ; if we get a match here, store the match data for later
  10030. (let ((match (assoc-default dlink apps-dlink
  10031. 'string-match)))
  10032. (if match
  10033. (progn (setq link-match-data (match-data))
  10034. match)
  10035. (progn (setq in-emacs (or in-emacs line search))
  10036. nil))) ; if we have no match in apps-dlink,
  10037. ; always open the file in emacs if line or search
  10038. ; is given (for backwards compatibility)
  10039. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  10040. 'string-match)
  10041. (cdr (assoc ext apps))
  10042. (cdr (assoc t apps))))))
  10043. (when (eq cmd 'system)
  10044. (setq cmd (cdr (assoc 'system apps))))
  10045. (when (eq cmd 'default)
  10046. (setq cmd (cdr (assoc t apps))))
  10047. (when (eq cmd 'mailcap)
  10048. (require 'mailcap)
  10049. (mailcap-parse-mailcaps)
  10050. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  10051. (command (mailcap-mime-info mime-type)))
  10052. (if (stringp command)
  10053. (setq cmd command)
  10054. (setq cmd 'emacs))))
  10055. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  10056. (not (file-exists-p file))
  10057. (not org-open-non-existing-files))
  10058. (user-error "No such file: %s" file))
  10059. (cond
  10060. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  10061. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  10062. (while (string-match "['\"]%s['\"]" cmd)
  10063. (setq cmd (replace-match "%s" t t cmd)))
  10064. (while (string-match "%s" cmd)
  10065. (setq cmd (replace-match
  10066. (save-match-data
  10067. (shell-quote-argument
  10068. (convert-standard-filename file)))
  10069. t t cmd)))
  10070. ;; Replace "%1", "%2" etc. in command with group matches from regex
  10071. (save-match-data
  10072. (let ((match-index 1)
  10073. (number-of-groups (- (/ (length link-match-data) 2) 1)))
  10074. (set-match-data link-match-data)
  10075. (while (<= match-index number-of-groups)
  10076. (let ((regex (concat "%" (number-to-string match-index)))
  10077. (replace-with (match-string match-index dlink)))
  10078. (while (string-match regex cmd)
  10079. (setq cmd (replace-match replace-with t t cmd))))
  10080. (setq match-index (+ match-index 1)))))
  10081. (save-window-excursion
  10082. (message "Running %s...done" cmd)
  10083. (start-process-shell-command cmd nil cmd)
  10084. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))))
  10085. ((or (stringp cmd)
  10086. (eq cmd 'emacs))
  10087. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  10088. (widen)
  10089. (if line (org-goto-line line)
  10090. (if search (org-link-search search))))
  10091. ((consp cmd)
  10092. (let ((file (convert-standard-filename file)))
  10093. (save-match-data
  10094. (set-match-data link-match-data)
  10095. (eval cmd))))
  10096. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  10097. (and (derived-mode-p 'org-mode) (eq old-mode 'org-mode)
  10098. (or (not (equal old-buffer (current-buffer)))
  10099. (not (equal old-pos (point))))
  10100. (org-mark-ring-push old-pos old-buffer))))
  10101. (defun org-file-apps-entry-match-against-dlink-p (entry)
  10102. "This function returns non-nil if `entry' uses a regular
  10103. expression which should be matched against the whole link by
  10104. org-open-file.
  10105. It assumes that is the case when the entry uses a regular
  10106. expression which has at least one grouping construct and the
  10107. action is either a lisp form or a command string containing
  10108. '%1', i.e. using at least one subexpression match as a
  10109. parameter."
  10110. (let ((selector (car entry))
  10111. (action (cdr entry)))
  10112. (if (stringp selector)
  10113. (and (> (regexp-opt-depth selector) 0)
  10114. (or (and (stringp action)
  10115. (string-match "%[0-9]" action))
  10116. (consp action)))
  10117. nil)))
  10118. (defun org-default-apps ()
  10119. "Return the default applications for this operating system."
  10120. (cond
  10121. ((eq system-type 'darwin)
  10122. org-file-apps-defaults-macosx)
  10123. ((eq system-type 'windows-nt)
  10124. org-file-apps-defaults-windowsnt)
  10125. (t org-file-apps-defaults-gnu)))
  10126. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  10127. "Convert extensions to regular expressions in the cars of LIST.
  10128. Also, weed out any non-string entries, because the return value is used
  10129. only for regexp matching.
  10130. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  10131. point to the symbol `emacs', indicating that the file should
  10132. be opened in Emacs."
  10133. (append
  10134. (delq nil
  10135. (mapcar (lambda (x)
  10136. (if (not (stringp (car x)))
  10137. nil
  10138. (if (string-match "\\W" (car x))
  10139. x
  10140. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  10141. list))
  10142. (if add-auto-mode
  10143. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  10144. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  10145. (defun org-file-remote-p (file)
  10146. "Test whether FILE specifies a location on a remote system.
  10147. Return non-nil if the location is indeed remote.
  10148. For example, the filename \"/user@host:/foo\" specifies a location
  10149. on the system \"/user@host:\"."
  10150. (cond ((fboundp 'file-remote-p)
  10151. (file-remote-p file))
  10152. ((fboundp 'tramp-handle-file-remote-p)
  10153. (tramp-handle-file-remote-p file))
  10154. ((and (boundp 'ange-ftp-name-format)
  10155. (string-match (car ange-ftp-name-format) file))
  10156. t)))
  10157. ;;;; Refiling
  10158. (defun org-get-org-file ()
  10159. "Read a filename, with default directory `org-directory'."
  10160. (let ((default (or org-default-notes-file remember-data-file)))
  10161. (read-file-name (format "File name [%s]: " default)
  10162. (file-name-as-directory org-directory)
  10163. default)))
  10164. (defun org-notes-order-reversed-p ()
  10165. "Check if the current file should receive notes in reversed order."
  10166. (cond
  10167. ((not org-reverse-note-order) nil)
  10168. ((eq t org-reverse-note-order) t)
  10169. ((not (listp org-reverse-note-order)) nil)
  10170. (t (catch 'exit
  10171. (let ((all org-reverse-note-order)
  10172. entry)
  10173. (while (setq entry (pop all))
  10174. (if (string-match (car entry) buffer-file-name)
  10175. (throw 'exit (cdr entry))))
  10176. nil)))))
  10177. (defvar org-refile-target-table nil
  10178. "The list of refile targets, created by `org-refile'.")
  10179. (defvar org-agenda-new-buffers nil
  10180. "Buffers created to visit agenda files.")
  10181. (defvar org-refile-cache nil
  10182. "Cache for refile targets.")
  10183. (defvar org-refile-markers nil
  10184. "All the markers used for caching refile locations.")
  10185. (defun org-refile-marker (pos)
  10186. "Get a new refile marker, but only if caching is in use."
  10187. (if (not org-refile-use-cache)
  10188. pos
  10189. (let ((m (make-marker)))
  10190. (move-marker m pos)
  10191. (push m org-refile-markers)
  10192. m)))
  10193. (defun org-refile-cache-clear ()
  10194. "Clear the refile cache and disable all the markers."
  10195. (mapc (lambda (m) (move-marker m nil)) org-refile-markers)
  10196. (setq org-refile-markers nil)
  10197. (setq org-refile-cache nil)
  10198. (message "Refile cache has been cleared"))
  10199. (defun org-refile-cache-check-set (set)
  10200. "Check if all the markers in the cache still have live buffers."
  10201. (let (marker)
  10202. (catch 'exit
  10203. (while (and set (setq marker (nth 3 (pop set))))
  10204. ;; If `org-refile-use-outline-path' is 'file, marker may be nil
  10205. (when (and marker (null (marker-buffer marker)))
  10206. (message "Please regenerate the refile cache with `C-0 C-c C-w'")
  10207. (sit-for 3)
  10208. (throw 'exit nil)))
  10209. t)))
  10210. (defun org-refile-cache-put (set &rest identifiers)
  10211. "Push the refile targets SET into the cache, under IDENTIFIERS."
  10212. (let* ((key (sha1 (prin1-to-string identifiers)))
  10213. (entry (assoc key org-refile-cache)))
  10214. (if entry
  10215. (setcdr entry set)
  10216. (push (cons key set) org-refile-cache))))
  10217. (defun org-refile-cache-get (&rest identifiers)
  10218. "Retrieve the cached value for refile targets given by IDENTIFIERS."
  10219. (cond
  10220. ((not org-refile-cache) nil)
  10221. ((not org-refile-use-cache) (org-refile-cache-clear) nil)
  10222. (t
  10223. (let ((set (cdr (assoc (sha1 (prin1-to-string identifiers))
  10224. org-refile-cache))))
  10225. (and set (org-refile-cache-check-set set) set)))))
  10226. (defun org-refile-get-targets (&optional default-buffer excluded-entries)
  10227. "Produce a table with refile targets."
  10228. (let ((case-fold-search nil)
  10229. ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
  10230. (entries (or org-refile-targets '((nil . (:level . 1)))))
  10231. targets tgs txt re files f desc descre fast-path-p level pos0)
  10232. (message "Getting targets...")
  10233. (with-current-buffer (or default-buffer (current-buffer))
  10234. (while (setq entry (pop entries))
  10235. (setq files (car entry) desc (cdr entry))
  10236. (setq fast-path-p nil)
  10237. (cond
  10238. ((null files) (setq files (list (current-buffer))))
  10239. ((eq files 'org-agenda-files)
  10240. (setq files (org-agenda-files 'unrestricted)))
  10241. ((and (symbolp files) (fboundp files))
  10242. (setq files (funcall files)))
  10243. ((and (symbolp files) (boundp files))
  10244. (setq files (symbol-value files))))
  10245. (if (stringp files) (setq files (list files)))
  10246. (cond
  10247. ((eq (car desc) :tag)
  10248. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  10249. ((eq (car desc) :todo)
  10250. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  10251. ((eq (car desc) :regexp)
  10252. (setq descre (cdr desc)))
  10253. ((eq (car desc) :level)
  10254. (setq descre (concat "^\\*\\{" (number-to-string
  10255. (if org-odd-levels-only
  10256. (1- (* 2 (cdr desc)))
  10257. (cdr desc)))
  10258. "\\}[ \t]")))
  10259. ((eq (car desc) :maxlevel)
  10260. (setq fast-path-p t)
  10261. (setq descre (concat "^\\*\\{1," (number-to-string
  10262. (if org-odd-levels-only
  10263. (1- (* 2 (cdr desc)))
  10264. (cdr desc)))
  10265. "\\}[ \t]")))
  10266. (t (error "Bad refiling target description %s" desc)))
  10267. (while (setq f (pop files))
  10268. (with-current-buffer
  10269. (if (bufferp f) f (org-get-agenda-file-buffer f))
  10270. (or
  10271. (setq tgs (org-refile-cache-get (buffer-file-name) descre))
  10272. (progn
  10273. (if (bufferp f) (setq f (buffer-file-name
  10274. (buffer-base-buffer f))))
  10275. (setq f (and f (expand-file-name f)))
  10276. (if (eq org-refile-use-outline-path 'file)
  10277. (push (list (file-name-nondirectory f) f nil nil) tgs))
  10278. (save-excursion
  10279. (save-restriction
  10280. (widen)
  10281. (goto-char (point-min))
  10282. (while (re-search-forward descre nil t)
  10283. (goto-char (setq pos0 (point-at-bol)))
  10284. (catch 'next
  10285. (when org-refile-target-verify-function
  10286. (save-match-data
  10287. (or (funcall org-refile-target-verify-function)
  10288. (throw 'next t))))
  10289. (when (and (looking-at org-complex-heading-regexp)
  10290. (not (member (match-string 4) excluded-entries))
  10291. (match-string 4))
  10292. (setq level (org-reduced-level
  10293. (- (match-end 1) (match-beginning 1)))
  10294. txt (org-link-display-format (match-string 4))
  10295. txt (replace-regexp-in-string "\\( *\[[0-9]+/?[0-9]*%?\]\\)+$" "" txt)
  10296. re (format org-complex-heading-regexp-format
  10297. (regexp-quote (match-string 4))))
  10298. (when org-refile-use-outline-path
  10299. (setq txt (mapconcat
  10300. 'org-protect-slash
  10301. (append
  10302. (if (eq org-refile-use-outline-path
  10303. 'file)
  10304. (list (file-name-nondirectory
  10305. (buffer-file-name
  10306. (buffer-base-buffer))))
  10307. (if (eq org-refile-use-outline-path
  10308. 'full-file-path)
  10309. (list (buffer-file-name
  10310. (buffer-base-buffer)))))
  10311. (org-get-outline-path fast-path-p
  10312. level txt)
  10313. (list txt))
  10314. "/")))
  10315. (push (list txt f re (org-refile-marker (point)))
  10316. tgs)))
  10317. (when (= (point) pos0)
  10318. ;; verification function has not moved point
  10319. (goto-char (point-at-eol))))))))
  10320. (when org-refile-use-cache
  10321. (org-refile-cache-put tgs (buffer-file-name) descre))
  10322. (setq targets (append tgs targets))))))
  10323. (message "Getting targets...done")
  10324. (nreverse targets)))
  10325. (defun org-protect-slash (s)
  10326. (while (string-match "/" s)
  10327. (setq s (replace-match "\\" t t s)))
  10328. s)
  10329. (defvar org-olpa (make-vector 20 nil))
  10330. (defun org-get-outline-path (&optional fastp level heading)
  10331. "Return the outline path to the current entry, as a list.
  10332. The parameters FASTP, LEVEL, and HEADING are for use by a scanner
  10333. routine which makes outline path derivations for an entire file,
  10334. avoiding backtracing. Refile target collection makes use of that."
  10335. (if fastp
  10336. (progn
  10337. (if (> level 19)
  10338. (error "Outline path failure, more than 19 levels"))
  10339. (loop for i from level upto 19 do
  10340. (aset org-olpa i nil))
  10341. (prog1
  10342. (delq nil (append org-olpa nil))
  10343. (aset org-olpa level heading)))
  10344. (let (rtn case-fold-search)
  10345. (save-excursion
  10346. (save-restriction
  10347. (widen)
  10348. (while (org-up-heading-safe)
  10349. (when (looking-at org-complex-heading-regexp)
  10350. (push (org-trim
  10351. (replace-regexp-in-string
  10352. ;; Remove statistical/checkboxes cookies
  10353. "\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" ""
  10354. (org-match-string-no-properties 4)))
  10355. rtn)))
  10356. rtn)))))
  10357. (defun org-format-outline-path (path &optional width prefix separator)
  10358. "Format the outline path PATH for display.
  10359. WIDTH is the maximum number of characters that is available.
  10360. PREFIX is a prefix to be included in the returned string,
  10361. such as the file name.
  10362. SEPARATOR is inserted between the different parts of the path,
  10363. the default is \"/\"."
  10364. (setq width (or width 79))
  10365. (if prefix (setq width (- width (length prefix))))
  10366. (if (not path)
  10367. (or prefix "")
  10368. (let* ((nsteps (length path))
  10369. (total-width (+ nsteps (apply '+ (mapcar 'length path))))
  10370. (maxwidth (if (<= total-width width)
  10371. 10000 ;; everything fits
  10372. ;; we need to shorten the level headings
  10373. (/ (- width nsteps) nsteps)))
  10374. (org-odd-levels-only nil)
  10375. (n 0)
  10376. (total (1+ (length prefix))))
  10377. (setq maxwidth (max maxwidth 10))
  10378. (concat prefix
  10379. (if prefix (or separator "/"))
  10380. (mapconcat
  10381. (lambda (h)
  10382. (setq n (1+ n))
  10383. (if (and (= n nsteps) (< maxwidth 10000))
  10384. (setq maxwidth (- total-width total)))
  10385. (if (< (length h) maxwidth)
  10386. (progn (setq total (+ total (length h) 1)) h)
  10387. (setq h (substring h 0 (- maxwidth 2))
  10388. total (+ total maxwidth 1))
  10389. (if (string-match "[ \t]+\\'" h)
  10390. (setq h (substring h 0 (match-beginning 0))))
  10391. (setq h (concat h "..")))
  10392. (org-add-props h nil 'face
  10393. (nth (% (1- n) org-n-level-faces)
  10394. org-level-faces))
  10395. h)
  10396. path (or separator "/"))))))
  10397. (defun org-display-outline-path (&optional file current separator just-return-string)
  10398. "Display the current outline path in the echo area.
  10399. If FILE is non-nil, prepend the output with the file name.
  10400. If CURRENT is non-nil, append the current heading to the output.
  10401. SEPARATOR is passed through to `org-format-outline-path'. It separates
  10402. the different parts of the path and defaults to \"/\".
  10403. If JUST-RETURN-STRING is non-nil, return a string, don't display a message."
  10404. (interactive "P")
  10405. (let* (case-fold-search
  10406. (bfn (buffer-file-name (buffer-base-buffer)))
  10407. (path (and (derived-mode-p 'org-mode) (org-get-outline-path)))
  10408. res)
  10409. (if current (setq path (append path
  10410. (save-excursion
  10411. (org-back-to-heading t)
  10412. (if (looking-at org-complex-heading-regexp)
  10413. (list (match-string 4)))))))
  10414. (setq res
  10415. (org-format-outline-path
  10416. path
  10417. (1- (frame-width))
  10418. (and file bfn (concat (file-name-nondirectory bfn) separator))
  10419. separator))
  10420. (if just-return-string
  10421. (org-no-properties res)
  10422. (org-unlogged-message "%s" res))))
  10423. (defvar org-refile-history nil
  10424. "History for refiling operations.")
  10425. (defvar org-after-refile-insert-hook nil
  10426. "Hook run after `org-refile' has inserted its stuff at the new location.
  10427. Note that this is still *before* the stuff will be removed from
  10428. the *old* location.")
  10429. (defvar org-capture-last-stored-marker)
  10430. (defvar org-refile-keep nil
  10431. "Non-nil means `org-refile' will copy instead of refile.")
  10432. (defun org-copy ()
  10433. "Like `org-refile', but copy."
  10434. (interactive)
  10435. (let ((org-refile-keep t))
  10436. (funcall 'org-refile nil nil nil "Copy")))
  10437. (defun org-refile (&optional goto default-buffer rfloc msg)
  10438. "Move the entry or entries at point to another heading.
  10439. The list of target headings is compiled using the information in
  10440. `org-refile-targets', which see.
  10441. At the target location, the entry is filed as a subitem of the target
  10442. heading. Depending on `org-reverse-note-order', the new subitem will
  10443. either be the first or the last subitem.
  10444. If there is an active region, all entries in that region will be moved.
  10445. However, the region must fulfill the requirement that the first heading
  10446. is the first one sets the top-level of the moved text - at most siblings
  10447. below it are allowed.
  10448. With prefix arg GOTO, the command will only visit the target location
  10449. and not actually move anything.
  10450. With a double prefix arg \\[universal-argument] \\[universal-argument], \
  10451. go to the location where the last refiling operation has put the subtree.
  10452. With a numeric prefix argument of `2', refile to the running clock.
  10453. With a numeric prefix argument of `3', emulate `org-refile-keep'
  10454. being set to `t' and copy to the target location, don't move it.
  10455. Beware that keeping refiled entries may result in duplicated ID
  10456. properties.
  10457. RFLOC can be a refile location obtained in a different way.
  10458. MSG is a string to replace \"Refile\" in the default prompt with
  10459. another verb. E.g. `org-copy' sets this parameter to \"Copy\".
  10460. See also `org-refile-use-outline-path' and `org-completion-use-ido'.
  10461. If you are using target caching (see `org-refile-use-cache'),
  10462. you have to clear the target cache in order to find new targets.
  10463. This can be done with a 0 prefix (`C-0 C-c C-w') or a triple
  10464. prefix argument (`C-u C-u C-u C-c C-w')."
  10465. (interactive "P")
  10466. (if (member goto '(0 (64)))
  10467. (org-refile-cache-clear)
  10468. (let* ((actionmsg (cond (msg msg)
  10469. ((equal goto 3) "Refile (and keep)")
  10470. (t "Refile")))
  10471. (cbuf (current-buffer))
  10472. (regionp (org-region-active-p))
  10473. (region-start (and regionp (region-beginning)))
  10474. (region-end (and regionp (region-end)))
  10475. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  10476. (org-refile-keep (if (equal goto 3) t org-refile-keep))
  10477. pos it nbuf file re level reversed)
  10478. (setq last-command nil)
  10479. (when regionp
  10480. (goto-char region-start)
  10481. (or (bolp) (goto-char (point-at-bol)))
  10482. (setq region-start (point))
  10483. (unless (or (org-kill-is-subtree-p
  10484. (buffer-substring region-start region-end))
  10485. (prog1 org-refile-active-region-within-subtree
  10486. (let ((s (point-at-eol)))
  10487. (org-toggle-heading)
  10488. (setq region-end (+ (- (point-at-eol) s) region-end)))))
  10489. (user-error "The region is not a (sequence of) subtree(s)")))
  10490. (if (equal goto '(16))
  10491. (org-refile-goto-last-stored)
  10492. (when (or
  10493. (and (equal goto 2)
  10494. org-clock-hd-marker (marker-buffer org-clock-hd-marker)
  10495. (prog1
  10496. (setq it (list (or org-clock-heading "running clock")
  10497. (buffer-file-name
  10498. (marker-buffer org-clock-hd-marker))
  10499. ""
  10500. (marker-position org-clock-hd-marker)))
  10501. (setq goto nil)))
  10502. (setq it (or rfloc
  10503. (let (heading-text)
  10504. (save-excursion
  10505. (unless (and goto (listp goto))
  10506. (org-back-to-heading t)
  10507. (setq heading-text
  10508. (nth 4 (org-heading-components))))
  10509. (org-refile-get-location
  10510. (cond ((and goto (listp goto)) "Goto")
  10511. (regionp (concat actionmsg " region to"))
  10512. (t (concat actionmsg " subtree \""
  10513. heading-text "\" to")))
  10514. default-buffer
  10515. (and (not (equal '(4) goto))
  10516. org-refile-allow-creating-parent-nodes)
  10517. goto))))))
  10518. (setq file (nth 1 it)
  10519. re (nth 2 it)
  10520. pos (nth 3 it))
  10521. (if (and (not goto)
  10522. pos
  10523. (equal (buffer-file-name) file)
  10524. (if regionp
  10525. (and (>= pos region-start)
  10526. (<= pos region-end))
  10527. (and (>= pos (point))
  10528. (< pos (save-excursion
  10529. (org-end-of-subtree t t))))))
  10530. (error "Cannot refile to position inside the tree or region"))
  10531. (setq nbuf (or (find-buffer-visiting file)
  10532. (find-file-noselect file)))
  10533. (if (and goto (not (equal goto 3)))
  10534. (progn
  10535. (org-pop-to-buffer-same-window nbuf)
  10536. (goto-char pos)
  10537. (org-show-context 'org-goto))
  10538. (if regionp
  10539. (progn
  10540. (org-kill-new (buffer-substring region-start region-end))
  10541. (org-save-markers-in-region region-start region-end))
  10542. (org-copy-subtree 1 nil t))
  10543. (with-current-buffer (setq nbuf (or (find-buffer-visiting file)
  10544. (find-file-noselect file)))
  10545. (setq reversed (org-notes-order-reversed-p))
  10546. (save-excursion
  10547. (save-restriction
  10548. (widen)
  10549. (if pos
  10550. (progn
  10551. (goto-char pos)
  10552. (looking-at org-outline-regexp)
  10553. (setq level (org-get-valid-level (funcall outline-level) 1))
  10554. (goto-char
  10555. (if reversed
  10556. (or (outline-next-heading) (point-max))
  10557. (or (save-excursion (org-get-next-sibling))
  10558. (org-end-of-subtree t t)
  10559. (point-max)))))
  10560. (setq level 1)
  10561. (if (not reversed)
  10562. (goto-char (point-max))
  10563. (goto-char (point-min))
  10564. (or (outline-next-heading) (goto-char (point-max)))))
  10565. (if (not (bolp)) (newline))
  10566. (org-paste-subtree level)
  10567. (when org-log-refile
  10568. (org-add-log-setup 'refile nil nil 'findpos org-log-refile)
  10569. (unless (eq org-log-refile 'note)
  10570. (save-excursion (org-add-log-note))))
  10571. (and org-auto-align-tags
  10572. (let ((org-loop-over-headlines-in-active-region nil))
  10573. (org-set-tags nil t)))
  10574. (let ((bookmark-name (plist-get org-bookmark-names-plist
  10575. :last-refile)))
  10576. (when bookmark-name
  10577. (with-demoted-errors
  10578. (bookmark-set bookmark-name))))
  10579. ;; If we are refiling for capture, make sure that the
  10580. ;; last-capture pointers point here
  10581. (when (org-bound-and-true-p org-refile-for-capture)
  10582. (let ((bookmark-name (plist-get org-bookmark-names-plist
  10583. :last-capture-marker)))
  10584. (when bookmark-name
  10585. (with-demoted-errors
  10586. (bookmark-set bookmark-name))))
  10587. (move-marker org-capture-last-stored-marker (point)))
  10588. (if (fboundp 'deactivate-mark) (deactivate-mark))
  10589. (run-hooks 'org-after-refile-insert-hook))))
  10590. (unless org-refile-keep
  10591. (if regionp
  10592. (delete-region (point) (+ (point) (- region-end region-start)))
  10593. (delete-region
  10594. (and (org-back-to-heading t) (point))
  10595. (min (1+ (buffer-size)) (org-end-of-subtree t t) (point)))))
  10596. (when (featurep 'org-inlinetask)
  10597. (org-inlinetask-remove-END-maybe))
  10598. (setq org-markers-to-move nil)
  10599. (message (concat actionmsg " to \"%s\" in file %s: done") (car it) file)))))))
  10600. (defun org-refile-goto-last-stored ()
  10601. "Go to the location where the last refile was stored."
  10602. (interactive)
  10603. (bookmark-jump "org-refile-last-stored")
  10604. (message "This is the location of the last refile"))
  10605. (defun org-refile--get-location (refloc tbl)
  10606. "When user refile to REFLOC, find the associated target in TBL.
  10607. Also check `org-refile-target-table'."
  10608. (car (delq
  10609. nil
  10610. (mapcar
  10611. (lambda (r) (or (assoc r tbl)
  10612. (assoc r org-refile-target-table)))
  10613. (list (replace-regexp-in-string "/$" "" refloc)
  10614. (replace-regexp-in-string "\\([^/]\\)$" "\\1/" refloc))))))
  10615. (defun org-refile-get-location (&optional prompt default-buffer new-nodes
  10616. no-exclude)
  10617. "Prompt the user for a refile location, using PROMPT.
  10618. PROMPT should not be suffixed with a colon and a space, because
  10619. this function appends the default value from
  10620. `org-refile-history' automatically, if that is not empty.
  10621. When NO-EXCLUDE is set, do not exclude headlines in the current subtree,
  10622. this is used for the GOTO interface."
  10623. (let ((org-refile-targets org-refile-targets)
  10624. (org-refile-use-outline-path org-refile-use-outline-path)
  10625. excluded-entries)
  10626. (when (and (derived-mode-p 'org-mode)
  10627. (not org-refile-use-cache)
  10628. (not no-exclude))
  10629. (org-map-tree
  10630. (lambda()
  10631. (setq excluded-entries
  10632. (append excluded-entries (list (org-get-heading t t)))))))
  10633. (setq org-refile-target-table
  10634. (org-refile-get-targets default-buffer excluded-entries)))
  10635. (unless org-refile-target-table
  10636. (user-error "No refile targets"))
  10637. (let* ((cbuf (current-buffer))
  10638. (partial-completion-mode nil)
  10639. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  10640. (cfunc (if (and org-refile-use-outline-path
  10641. org-outline-path-complete-in-steps)
  10642. 'org-olpath-completing-read
  10643. 'org-icompleting-read))
  10644. (extra (if org-refile-use-outline-path "/" ""))
  10645. (cbnex (concat (buffer-name) extra))
  10646. (filename (and cfn (expand-file-name cfn)))
  10647. (tbl (mapcar
  10648. (lambda (x)
  10649. (if (and (not (member org-refile-use-outline-path
  10650. '(file full-file-path)))
  10651. (not (equal filename (nth 1 x))))
  10652. (cons (concat (car x) extra " ("
  10653. (file-name-nondirectory (nth 1 x)) ")")
  10654. (cdr x))
  10655. (cons (concat (car x) extra) (cdr x))))
  10656. org-refile-target-table))
  10657. (completion-ignore-case t)
  10658. cdef
  10659. (prompt (concat prompt
  10660. (or (and (car org-refile-history)
  10661. (concat " (default " (car org-refile-history) ")"))
  10662. (and (assoc cbnex tbl) (setq cdef cbnex)
  10663. (concat " (default " cbnex ")"))) ": "))
  10664. pa answ parent-target child parent old-hist)
  10665. (setq old-hist org-refile-history)
  10666. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  10667. nil 'org-refile-history (or cdef (car org-refile-history))))
  10668. (if (setq pa (org-refile--get-location answ tbl))
  10669. (progn
  10670. (org-refile-check-position pa)
  10671. (when (or (not org-refile-history)
  10672. (not (eq old-hist org-refile-history))
  10673. (not (equal (car pa) (car org-refile-history))))
  10674. (setq org-refile-history
  10675. (cons (car pa) (if (assoc (car org-refile-history) tbl)
  10676. org-refile-history
  10677. (cdr org-refile-history))))
  10678. (if (equal (car org-refile-history) (nth 1 org-refile-history))
  10679. (pop org-refile-history)))
  10680. pa)
  10681. (if (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  10682. (progn
  10683. (setq parent (match-string 1 answ)
  10684. child (match-string 2 answ))
  10685. (setq parent-target (org-refile--get-location parent tbl))
  10686. (when (and parent-target
  10687. (or (eq new-nodes t)
  10688. (and (eq new-nodes 'confirm)
  10689. (y-or-n-p (format "Create new node \"%s\"? "
  10690. child)))))
  10691. (org-refile-new-child parent-target child)))
  10692. (user-error "Invalid target location")))))
  10693. (declare-function org-string-nw-p "org-macs" (s))
  10694. (defun org-refile-check-position (refile-pointer)
  10695. "Check if the refile pointer matches the headline to which it points."
  10696. (let* ((file (nth 1 refile-pointer))
  10697. (re (nth 2 refile-pointer))
  10698. (pos (nth 3 refile-pointer))
  10699. buffer)
  10700. (if (and (not (markerp pos)) (not file))
  10701. (user-error "Please save the buffer to a file before refiling")
  10702. (when (org-string-nw-p re)
  10703. (setq buffer (if (markerp pos)
  10704. (marker-buffer pos)
  10705. (or (find-buffer-visiting file)
  10706. (find-file-noselect file))))
  10707. (with-current-buffer buffer
  10708. (save-excursion
  10709. (save-restriction
  10710. (widen)
  10711. (goto-char pos)
  10712. (beginning-of-line 1)
  10713. (unless (org-looking-at-p re)
  10714. (user-error "Invalid refile position, please clear the cache with `C-0 C-c C-w' before refiling")))))))))
  10715. (defun org-refile-new-child (parent-target child)
  10716. "Use refile target PARENT-TARGET to add new CHILD below it."
  10717. (unless parent-target
  10718. (error "Cannot find parent for new node"))
  10719. (let ((file (nth 1 parent-target))
  10720. (pos (nth 3 parent-target))
  10721. level)
  10722. (with-current-buffer (or (find-buffer-visiting file)
  10723. (find-file-noselect file))
  10724. (save-excursion
  10725. (save-restriction
  10726. (widen)
  10727. (if pos
  10728. (goto-char pos)
  10729. (goto-char (point-max))
  10730. (if (not (bolp)) (newline)))
  10731. (when (looking-at org-outline-regexp)
  10732. (setq level (funcall outline-level))
  10733. (org-end-of-subtree t t))
  10734. (org-back-over-empty-lines)
  10735. (insert "\n" (make-string
  10736. (if pos (org-get-valid-level level 1) 1) ?*)
  10737. " " child "\n")
  10738. (beginning-of-line 0)
  10739. (list (concat (car parent-target) "/" child) file "" (point)))))))
  10740. (defun org-olpath-completing-read (prompt collection &rest args)
  10741. "Read an outline path like a file name."
  10742. (let ((thetable collection)
  10743. (org-completion-use-ido nil) ; does not work with ido.
  10744. (org-completion-use-iswitchb nil)) ; or iswitchb
  10745. (apply
  10746. 'org-icompleting-read prompt
  10747. (lambda (string predicate &optional flag)
  10748. (let (rtn r f (l (length string)))
  10749. (cond
  10750. ((eq flag nil)
  10751. ;; try completion
  10752. (try-completion string thetable))
  10753. ((eq flag t)
  10754. ;; all-completions
  10755. (setq rtn (all-completions string thetable predicate))
  10756. (mapcar
  10757. (lambda (x)
  10758. (setq r (substring x l))
  10759. (if (string-match " ([^)]*)$" x)
  10760. (setq f (match-string 0 x))
  10761. (setq f ""))
  10762. (if (string-match "/" r)
  10763. (concat string (substring r 0 (match-end 0)) f)
  10764. x))
  10765. rtn))
  10766. ((eq flag 'lambda)
  10767. ;; exact match?
  10768. (assoc string thetable)))))
  10769. args)))
  10770. ;;;; Dynamic blocks
  10771. (defun org-find-dblock (name)
  10772. "Find the first dynamic block with name NAME in the buffer.
  10773. If not found, stay at current position and return nil."
  10774. (let ((case-fold-search t) pos)
  10775. (save-excursion
  10776. (goto-char (point-min))
  10777. (setq pos (and (re-search-forward
  10778. (concat "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+" name "\\>") nil t)
  10779. (match-beginning 0))))
  10780. (if pos (goto-char pos))
  10781. pos))
  10782. (defconst org-dblock-start-re
  10783. "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  10784. "Matches the start line of a dynamic block, with parameters.")
  10785. (defconst org-dblock-end-re "^[ \t]*#\\+\\(?:END\\|end\\)\\([: \t\r\n]\\|$\\)"
  10786. "Matches the end of a dynamic block.")
  10787. (defun org-create-dblock (plist)
  10788. "Create a dynamic block section, with parameters taken from PLIST.
  10789. PLIST must contain a :name entry which is used as name of the block."
  10790. (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
  10791. (end-of-line 1)
  10792. (newline))
  10793. (let ((col (current-column))
  10794. (name (plist-get plist :name)))
  10795. (insert "#+BEGIN: " name)
  10796. (while plist
  10797. (if (eq (car plist) :name)
  10798. (setq plist (cddr plist))
  10799. (insert " " (prin1-to-string (pop plist)))))
  10800. (insert "\n\n" (make-string col ?\ ) "#+END:\n")
  10801. (beginning-of-line -2)))
  10802. (defun org-prepare-dblock ()
  10803. "Prepare dynamic block for refresh.
  10804. This empties the block, puts the cursor at the insert position and returns
  10805. the property list including an extra property :name with the block name."
  10806. (unless (looking-at org-dblock-start-re)
  10807. (user-error "Not at a dynamic block"))
  10808. (let* ((begdel (1+ (match-end 0)))
  10809. (name (org-no-properties (match-string 1)))
  10810. (params (append (list :name name)
  10811. (read (concat "(" (match-string 3) ")")))))
  10812. (save-excursion
  10813. (beginning-of-line 1)
  10814. (skip-chars-forward " \t")
  10815. (setq params (plist-put params :indentation-column (current-column))))
  10816. (unless (re-search-forward org-dblock-end-re nil t)
  10817. (error "Dynamic block not terminated"))
  10818. (setq params
  10819. (append params
  10820. (list :content (buffer-substring
  10821. begdel (match-beginning 0)))))
  10822. (delete-region begdel (match-beginning 0))
  10823. (goto-char begdel)
  10824. (open-line 1)
  10825. params))
  10826. (defun org-map-dblocks (&optional command)
  10827. "Apply COMMAND to all dynamic blocks in the current buffer.
  10828. If COMMAND is not given, use `org-update-dblock'."
  10829. (let ((cmd (or command 'org-update-dblock)))
  10830. (save-excursion
  10831. (goto-char (point-min))
  10832. (while (re-search-forward org-dblock-start-re nil t)
  10833. (goto-char (match-beginning 0))
  10834. (save-excursion
  10835. (condition-case nil
  10836. (funcall cmd)
  10837. (error (message "Error during update of dynamic block"))))
  10838. (unless (re-search-forward org-dblock-end-re nil t)
  10839. (error "Dynamic block not terminated"))))))
  10840. (defun org-dblock-update (&optional arg)
  10841. "User command for updating dynamic blocks.
  10842. Update the dynamic block at point. With prefix ARG, update all dynamic
  10843. blocks in the buffer."
  10844. (interactive "P")
  10845. (if arg
  10846. (org-update-all-dblocks)
  10847. (or (looking-at org-dblock-start-re)
  10848. (org-beginning-of-dblock))
  10849. (org-update-dblock)))
  10850. (defun org-update-dblock ()
  10851. "Update the dynamic block at point.
  10852. This means to empty the block, parse for parameters and then call
  10853. the correct writing function."
  10854. (interactive)
  10855. (save-excursion
  10856. (let* ((win (selected-window))
  10857. (pos (point))
  10858. (line (org-current-line))
  10859. (params (org-prepare-dblock))
  10860. (name (plist-get params :name))
  10861. (indent (plist-get params :indentation-column))
  10862. (cmd (intern (concat "org-dblock-write:" name))))
  10863. (message "Updating dynamic block `%s' at line %d..." name line)
  10864. (funcall cmd params)
  10865. (message "Updating dynamic block `%s' at line %d...done" name line)
  10866. (goto-char pos)
  10867. (when (and indent (> indent 0))
  10868. (setq indent (make-string indent ?\ ))
  10869. (save-excursion
  10870. (select-window win)
  10871. (org-beginning-of-dblock)
  10872. (forward-line 1)
  10873. (while (not (looking-at org-dblock-end-re))
  10874. (insert indent)
  10875. (beginning-of-line 2))
  10876. (when (looking-at org-dblock-end-re)
  10877. (and (looking-at "[ \t]+")
  10878. (replace-match ""))
  10879. (insert indent)))))))
  10880. (defun org-beginning-of-dblock ()
  10881. "Find the beginning of the dynamic block at point.
  10882. Error if there is no such block at point."
  10883. (let ((pos (point))
  10884. beg)
  10885. (end-of-line 1)
  10886. (if (and (re-search-backward org-dblock-start-re nil t)
  10887. (setq beg (match-beginning 0))
  10888. (re-search-forward org-dblock-end-re nil t)
  10889. (> (match-end 0) pos))
  10890. (goto-char beg)
  10891. (goto-char pos)
  10892. (error "Not in a dynamic block"))))
  10893. (defun org-update-all-dblocks ()
  10894. "Update all dynamic blocks in the buffer.
  10895. This function can be used in a hook."
  10896. (interactive)
  10897. (when (derived-mode-p 'org-mode)
  10898. (org-map-dblocks 'org-update-dblock)))
  10899. ;;;; Completion
  10900. (declare-function org-export-backend-name "org-export" (cl-x))
  10901. (declare-function org-export-backend-options "org-export" (cl-x))
  10902. (defun org-get-export-keywords ()
  10903. "Return a list of all currently understood export keywords.
  10904. Export keywords include options, block names, attributes and
  10905. keywords relative to each registered export back-end."
  10906. (let (keywords)
  10907. (dolist (backend
  10908. (org-bound-and-true-p org-export--registered-backends)
  10909. (delq nil keywords))
  10910. ;; Back-end name (for keywords, like #+LATEX:)
  10911. (push (upcase (symbol-name (org-export-backend-name backend))) keywords)
  10912. (dolist (option-entry (org-export-backend-options backend))
  10913. ;; Back-end options.
  10914. (push (nth 1 option-entry) keywords)))))
  10915. (defconst org-options-keywords
  10916. '("ARCHIVE:" "AUTHOR:" "BIND:" "CATEGORY:" "COLUMNS:" "CREATOR:" "DATE:"
  10917. "DESCRIPTION:" "DRAWERS:" "EMAIL:" "EXCLUDE_TAGS:" "FILETAGS:" "INCLUDE:"
  10918. "INDEX:" "KEYWORDS:" "LANGUAGE:" "MACRO:" "OPTIONS:" "PROPERTY:"
  10919. "PRIORITIES:" "SELECT_TAGS:" "SEQ_TODO:" "SETUPFILE:" "STARTUP:" "TAGS:"
  10920. "TITLE:" "TODO:" "TYP_TODO:" "SELECT_TAGS:" "EXCLUDE_TAGS:"))
  10921. (defcustom org-structure-template-alist
  10922. '(("s" "#+BEGIN_SRC ?\n\n#+END_SRC" "<src lang=\"?\">\n\n</src>")
  10923. ("e" "#+BEGIN_EXAMPLE\n?\n#+END_EXAMPLE" "<example>\n?\n</example>")
  10924. ("q" "#+BEGIN_QUOTE\n?\n#+END_QUOTE" "<quote>\n?\n</quote>")
  10925. ("v" "#+BEGIN_VERSE\n?\n#+END_VERSE" "<verse>\n?\n</verse>")
  10926. ("V" "#+BEGIN_VERBATIM\n?\n#+END_VERBATIM" "<verbatim>\n?\n</verbatim>")
  10927. ("c" "#+BEGIN_CENTER\n?\n#+END_CENTER" "<center>\n?\n</center>")
  10928. ("l" "#+BEGIN_LaTeX\n?\n#+END_LaTeX"
  10929. "<literal style=\"latex\">\n?\n</literal>")
  10930. ("L" "#+LaTeX: " "<literal style=\"latex\">?</literal>")
  10931. ("h" "#+BEGIN_HTML\n?\n#+END_HTML"
  10932. "<literal style=\"html\">\n?\n</literal>")
  10933. ("H" "#+HTML: " "<literal style=\"html\">?</literal>")
  10934. ("a" "#+BEGIN_ASCII\n?\n#+END_ASCII" "")
  10935. ("A" "#+ASCII: " "")
  10936. ("i" "#+INDEX: ?" "#+INDEX: ?")
  10937. ("I" "#+INCLUDE: %file ?"
  10938. "<include file=%file markup=\"?\">"))
  10939. "Structure completion elements.
  10940. This is a list of abbreviation keys and values. The value gets inserted
  10941. if you type `<' followed by the key and then press the completion key,
  10942. usually `M-TAB'. %file will be replaced by a file name after prompting
  10943. for the file using completion. The cursor will be placed at the position
  10944. of the `?` in the template.
  10945. There are two templates for each key, the first uses the original Org syntax,
  10946. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  10947. the default when the /org-mtags.el/ module has been loaded. See also the
  10948. variable `org-mtags-prefer-muse-templates'."
  10949. :group 'org-completion
  10950. :type '(repeat
  10951. (list
  10952. (string :tag "Key")
  10953. (string :tag "Template")
  10954. (string :tag "Muse Template"))))
  10955. (defun org-try-structure-completion ()
  10956. "Try to complete a structure template before point.
  10957. This looks for strings like \"<e\" on an otherwise empty line and
  10958. expands them."
  10959. (let ((l (buffer-substring (point-at-bol) (point)))
  10960. a)
  10961. (when (and (looking-at "[ \t]*$")
  10962. (string-match "^[ \t]*<\\([a-zA-Z]+\\)$" l)
  10963. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  10964. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  10965. (match-beginning 1)) a)
  10966. t)))
  10967. (defun org-complete-expand-structure-template (start cell)
  10968. "Expand a structure template."
  10969. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  10970. (rpl (nth (if musep 2 1) cell))
  10971. (ind ""))
  10972. (delete-region start (point))
  10973. (when (string-match "\\`#\\+" rpl)
  10974. (cond
  10975. ((bolp))
  10976. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  10977. (setq ind (buffer-substring (point-at-bol) (point))))
  10978. (t (newline))))
  10979. (setq start (point))
  10980. (if (string-match "%file" rpl)
  10981. (setq rpl (replace-match
  10982. (concat
  10983. "\""
  10984. (save-match-data
  10985. (abbreviate-file-name (read-file-name "Include file: ")))
  10986. "\"")
  10987. t t rpl)))
  10988. (setq rpl (mapconcat 'identity (split-string rpl "\n")
  10989. (concat "\n" ind)))
  10990. (insert rpl)
  10991. (if (re-search-backward "\\?" start t) (delete-char 1))))
  10992. ;;;; TODO, DEADLINE, Comments
  10993. (defun org-toggle-comment ()
  10994. "Change the COMMENT state of an entry."
  10995. (interactive)
  10996. (save-excursion
  10997. (org-back-to-heading)
  10998. (let (case-fold-search)
  10999. (cond
  11000. ((looking-at (format org-heading-keyword-regexp-format
  11001. org-comment-string))
  11002. (goto-char (match-end 1))
  11003. (looking-at (concat " +" org-comment-string))
  11004. (replace-match "" t t)
  11005. (when (eolp) (insert " ")))
  11006. ((looking-at org-outline-regexp)
  11007. (goto-char (match-end 0))
  11008. (insert org-comment-string " "))))))
  11009. (defvar org-last-todo-state-is-todo nil
  11010. "This is non-nil when the last TODO state change led to a TODO state.
  11011. If the last change removed the TODO tag or switched to DONE, then
  11012. this is nil.")
  11013. (defvar org-setting-tags nil) ; dynamically skipped
  11014. (defvar org-todo-setup-filter-hook nil
  11015. "Hook for functions that pre-filter todo specs.
  11016. Each function takes a todo spec and returns either nil or the spec
  11017. transformed into canonical form." )
  11018. (defvar org-todo-get-default-hook nil
  11019. "Hook for functions that get a default item for todo.
  11020. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  11021. nil or a string to be used for the todo mark." )
  11022. (defvar org-agenda-headline-snapshot-before-repeat)
  11023. (defun org-current-effective-time ()
  11024. "Return current time adjusted for `org-extend-today-until' variable."
  11025. (let* ((ct (org-current-time))
  11026. (dct (decode-time ct))
  11027. (ct1
  11028. (cond
  11029. (org-use-last-clock-out-time-as-effective-time
  11030. (or (org-clock-get-last-clock-out-time) ct))
  11031. ((and org-use-effective-time (< (nth 2 dct) org-extend-today-until))
  11032. (encode-time 0 59 23 (1- (nth 3 dct)) (nth 4 dct) (nth 5 dct)))
  11033. (t ct))))
  11034. ct1))
  11035. (defun org-todo-yesterday (&optional arg)
  11036. "Like `org-todo' but the time of change will be 23:59 of yesterday."
  11037. (interactive "P")
  11038. (if (eq major-mode 'org-agenda-mode)
  11039. (apply 'org-agenda-todo-yesterday arg)
  11040. (let* ((hour (third (decode-time
  11041. (org-current-time))))
  11042. (org-extend-today-until (1+ hour)))
  11043. (org-todo arg))))
  11044. (defvar org-block-entry-blocking ""
  11045. "First entry preventing the TODO state change.")
  11046. (defun org-todo (&optional arg)
  11047. "Change the TODO state of an item.
  11048. The state of an item is given by a keyword at the start of the heading,
  11049. like
  11050. *** TODO Write paper
  11051. *** DONE Call mom
  11052. The different keywords are specified in the variable `org-todo-keywords'.
  11053. By default the available states are \"TODO\" and \"DONE\".
  11054. So for this example: when the item starts with TODO, it is changed to DONE.
  11055. When it starts with DONE, the DONE is removed. And when neither TODO nor
  11056. DONE are present, add TODO at the beginning of the heading.
  11057. With \\[universal-argument] prefix arg, use completion to determine the new \
  11058. state.
  11059. With numeric prefix arg, switch to that state.
  11060. With a double \\[universal-argument] prefix, switch to the next set of TODO \
  11061. keywords (nextset).
  11062. With a triple \\[universal-argument] prefix, circumvent any state blocking.
  11063. With a numeric prefix arg of 0, inhibit note taking for the change.
  11064. For calling through lisp, arg is also interpreted in the following way:
  11065. 'none -> empty state
  11066. \"\"(empty string) -> switch to empty state
  11067. 'done -> switch to DONE
  11068. 'nextset -> switch to the next set of keywords
  11069. 'previousset -> switch to the previous set of keywords
  11070. \"WAITING\" -> switch to the specified keyword, but only if it
  11071. really is a member of `org-todo-keywords'."
  11072. (interactive "P")
  11073. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  11074. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  11075. 'region-start-level 'region))
  11076. org-loop-over-headlines-in-active-region)
  11077. (org-map-entries
  11078. `(org-todo ,arg)
  11079. org-loop-over-headlines-in-active-region
  11080. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  11081. (if (equal arg '(16)) (setq arg 'nextset))
  11082. (let ((org-blocker-hook org-blocker-hook)
  11083. commentp
  11084. case-fold-search)
  11085. (when (equal arg '(64))
  11086. (setq arg nil org-blocker-hook nil))
  11087. (when (and org-blocker-hook
  11088. (or org-inhibit-blocking
  11089. (org-entry-get nil "NOBLOCKING")))
  11090. (setq org-blocker-hook nil))
  11091. (save-excursion
  11092. (catch 'exit
  11093. (org-back-to-heading t)
  11094. (when (looking-at (concat "^\\*+ " org-comment-string))
  11095. (org-toggle-comment)
  11096. (setq commentp t))
  11097. (if (looking-at org-outline-regexp) (goto-char (1- (match-end 0))))
  11098. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|[ \t]*$\\)"))
  11099. (looking-at "\\(?: *\\|[ \t]*$\\)"))
  11100. (let* ((match-data (match-data))
  11101. (startpos (point-at-bol))
  11102. (logging (save-match-data (org-entry-get nil "LOGGING" t t)))
  11103. (org-log-done org-log-done)
  11104. (org-log-repeat org-log-repeat)
  11105. (org-todo-log-states org-todo-log-states)
  11106. (org-inhibit-logging
  11107. (if (equal arg 0)
  11108. (progn (setq arg nil) 'note) org-inhibit-logging))
  11109. (this (match-string 1))
  11110. (hl-pos (match-beginning 0))
  11111. (head (org-get-todo-sequence-head this))
  11112. (ass (assoc head org-todo-kwd-alist))
  11113. (interpret (nth 1 ass))
  11114. (done-word (nth 3 ass))
  11115. (final-done-word (nth 4 ass))
  11116. (org-last-state (or this ""))
  11117. (completion-ignore-case t)
  11118. (member (member this org-todo-keywords-1))
  11119. (tail (cdr member))
  11120. (org-state (cond
  11121. ((and org-todo-key-trigger
  11122. (or (and (equal arg '(4))
  11123. (eq org-use-fast-todo-selection 'prefix))
  11124. (and (not arg) org-use-fast-todo-selection
  11125. (not (eq org-use-fast-todo-selection
  11126. 'prefix)))))
  11127. ;; Use fast selection
  11128. (org-fast-todo-selection))
  11129. ((and (equal arg '(4))
  11130. (or (not org-use-fast-todo-selection)
  11131. (not org-todo-key-trigger)))
  11132. ;; Read a state with completion
  11133. (org-icompleting-read
  11134. "State: " (mapcar 'list org-todo-keywords-1)
  11135. nil t))
  11136. ((eq arg 'right)
  11137. (if this
  11138. (if tail (car tail) nil)
  11139. (car org-todo-keywords-1)))
  11140. ((eq arg 'left)
  11141. (if (equal member org-todo-keywords-1)
  11142. nil
  11143. (if this
  11144. (nth (- (length org-todo-keywords-1)
  11145. (length tail) 2)
  11146. org-todo-keywords-1)
  11147. (org-last org-todo-keywords-1))))
  11148. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  11149. (setq arg nil))) ; hack to fall back to cycling
  11150. (arg
  11151. ;; user or caller requests a specific state
  11152. (cond
  11153. ((equal arg "") nil)
  11154. ((eq arg 'none) nil)
  11155. ((eq arg 'done) (or done-word (car org-done-keywords)))
  11156. ((eq arg 'nextset)
  11157. (or (car (cdr (member head org-todo-heads)))
  11158. (car org-todo-heads)))
  11159. ((eq arg 'previousset)
  11160. (let ((org-todo-heads (reverse org-todo-heads)))
  11161. (or (car (cdr (member head org-todo-heads)))
  11162. (car org-todo-heads))))
  11163. ((car (member arg org-todo-keywords-1)))
  11164. ((stringp arg)
  11165. (user-error "State `%s' not valid in this file" arg))
  11166. ((nth (1- (prefix-numeric-value arg))
  11167. org-todo-keywords-1))))
  11168. ((null member) (or head (car org-todo-keywords-1)))
  11169. ((equal this final-done-word) nil) ;; -> make empty
  11170. ((null tail) nil) ;; -> first entry
  11171. ((memq interpret '(type priority))
  11172. (if (eq this-command last-command)
  11173. (car tail)
  11174. (if (> (length tail) 0)
  11175. (or done-word (car org-done-keywords))
  11176. nil)))
  11177. (t
  11178. (car tail))))
  11179. (org-state (or
  11180. (run-hook-with-args-until-success
  11181. 'org-todo-get-default-hook org-state org-last-state)
  11182. org-state))
  11183. (next (if org-state (concat " " org-state " ") " "))
  11184. (change-plist (list :type 'todo-state-change :from this :to org-state
  11185. :position startpos))
  11186. dolog now-done-p)
  11187. (when org-blocker-hook
  11188. (setq org-last-todo-state-is-todo
  11189. (not (member this org-done-keywords)))
  11190. (unless (save-excursion
  11191. (save-match-data
  11192. (org-with-wide-buffer
  11193. (run-hook-with-args-until-failure
  11194. 'org-blocker-hook change-plist))))
  11195. (if (org-called-interactively-p 'interactive)
  11196. (user-error "TODO state change from %s to %s blocked (by \"%s\")"
  11197. this org-state org-block-entry-blocking)
  11198. ;; fail silently
  11199. (message "TODO state change from %s to %s blocked (by \"%s\")"
  11200. this org-state org-block-entry-blocking)
  11201. (throw 'exit nil))))
  11202. (store-match-data match-data)
  11203. (replace-match next t t)
  11204. (unless (pos-visible-in-window-p hl-pos)
  11205. (message "TODO state changed to %s" (org-trim next)))
  11206. (unless head
  11207. (setq head (org-get-todo-sequence-head org-state)
  11208. ass (assoc head org-todo-kwd-alist)
  11209. interpret (nth 1 ass)
  11210. done-word (nth 3 ass)
  11211. final-done-word (nth 4 ass)))
  11212. (when (memq arg '(nextset previousset))
  11213. (message "Keyword-Set %d/%d: %s"
  11214. (- (length org-todo-sets) -1
  11215. (length (memq (assoc org-state org-todo-sets) org-todo-sets)))
  11216. (length org-todo-sets)
  11217. (mapconcat 'identity (assoc org-state org-todo-sets) " ")))
  11218. (setq org-last-todo-state-is-todo
  11219. (not (member org-state org-done-keywords)))
  11220. (setq now-done-p (and (member org-state org-done-keywords)
  11221. (not (member this org-done-keywords))))
  11222. (and logging (org-local-logging logging))
  11223. (when (and (or org-todo-log-states org-log-done)
  11224. (not (eq org-inhibit-logging t))
  11225. (not (memq arg '(nextset previousset))))
  11226. ;; we need to look at recording a time and note
  11227. (setq dolog (or (nth 1 (assoc org-state org-todo-log-states))
  11228. (nth 2 (assoc this org-todo-log-states))))
  11229. (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
  11230. (setq dolog 'time))
  11231. (when (or (and (not org-state) (not org-closed-keep-when-no-todo))
  11232. (and org-state
  11233. (member org-state org-not-done-keywords)
  11234. (not (member this org-not-done-keywords))))
  11235. ;; This is now a todo state and was not one before
  11236. ;; If there was a CLOSED time stamp, get rid of it.
  11237. (org-add-planning-info nil nil 'closed))
  11238. (when (and now-done-p org-log-done)
  11239. ;; It is now done, and it was not done before
  11240. (org-add-planning-info 'closed (org-current-effective-time))
  11241. (if (and (not dolog) (eq 'note org-log-done))
  11242. (org-add-log-setup 'done org-state this 'findpos 'note)))
  11243. (when (and org-state dolog)
  11244. ;; This is a non-nil state, and we need to log it
  11245. (org-add-log-setup 'state org-state this 'findpos dolog)))
  11246. ;; Fixup tag positioning
  11247. (org-todo-trigger-tag-changes org-state)
  11248. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  11249. (when org-provide-todo-statistics
  11250. (org-update-parent-todo-statistics))
  11251. (run-hooks 'org-after-todo-state-change-hook)
  11252. (if (and arg (not (member org-state org-done-keywords)))
  11253. (setq head (org-get-todo-sequence-head org-state)))
  11254. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  11255. ;; Do we need to trigger a repeat?
  11256. (when now-done-p
  11257. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  11258. ;; This is for the agenda, take a snapshot of the headline.
  11259. (save-match-data
  11260. (setq org-agenda-headline-snapshot-before-repeat
  11261. (org-get-heading))))
  11262. (org-auto-repeat-maybe org-state))
  11263. ;; Fixup cursor location if close to the keyword
  11264. (if (and (outline-on-heading-p)
  11265. (not (bolp))
  11266. (save-excursion (beginning-of-line 1)
  11267. (looking-at org-todo-line-regexp))
  11268. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  11269. (progn
  11270. (goto-char (or (match-end 2) (match-end 1)))
  11271. (and (looking-at " ") (just-one-space))))
  11272. (when org-trigger-hook
  11273. (save-excursion
  11274. (run-hook-with-args 'org-trigger-hook change-plist)))
  11275. (when commentp (org-toggle-comment))))))))
  11276. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  11277. "Block turning an entry into a TODO, using the hierarchy.
  11278. This checks whether the current task should be blocked from state
  11279. changes. Such blocking occurs when:
  11280. 1. The task has children which are not all in a completed state.
  11281. 2. A task has a parent with the property :ORDERED:, and there
  11282. are siblings prior to the current task with incomplete
  11283. status.
  11284. 3. The parent of the task is blocked because it has siblings that should
  11285. be done first, or is child of a block grandparent TODO entry."
  11286. (if (not org-enforce-todo-dependencies)
  11287. t ; if locally turned off don't block
  11288. (catch 'dont-block
  11289. ;; If this is not a todo state change, or if this entry is already DONE,
  11290. ;; do not block
  11291. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  11292. (member (plist-get change-plist :from)
  11293. (cons 'done org-done-keywords))
  11294. (member (plist-get change-plist :to)
  11295. (cons 'todo org-not-done-keywords))
  11296. (not (plist-get change-plist :to)))
  11297. (throw 'dont-block t))
  11298. ;; If this task has children, and any are undone, it's blocked
  11299. (save-excursion
  11300. (org-back-to-heading t)
  11301. (let ((this-level (funcall outline-level)))
  11302. (outline-next-heading)
  11303. (let ((child-level (funcall outline-level)))
  11304. (while (and (not (eobp))
  11305. (> child-level this-level))
  11306. ;; this todo has children, check whether they are all
  11307. ;; completed
  11308. (if (and (not (org-entry-is-done-p))
  11309. (org-entry-is-todo-p))
  11310. (progn (setq org-block-entry-blocking (org-get-heading))
  11311. (throw 'dont-block nil)))
  11312. (outline-next-heading)
  11313. (setq child-level (funcall outline-level))))))
  11314. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  11315. ;; any previous siblings are undone, it's blocked
  11316. (save-excursion
  11317. (org-back-to-heading t)
  11318. (let* ((pos (point))
  11319. (parent-pos (and (org-up-heading-safe) (point))))
  11320. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  11321. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  11322. (forward-line 1)
  11323. (re-search-forward org-not-done-heading-regexp pos t))
  11324. (setq org-block-entry-blocking (match-string 0))
  11325. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  11326. ;; Search further up the hierarchy, to see if an ancestor is blocked
  11327. (while t
  11328. (goto-char parent-pos)
  11329. (if (not (looking-at org-not-done-heading-regexp))
  11330. (throw 'dont-block t)) ; do not block, parent is not a TODO
  11331. (setq pos (point))
  11332. (setq parent-pos (and (org-up-heading-safe) (point)))
  11333. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  11334. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  11335. (forward-line 1)
  11336. (re-search-forward org-not-done-heading-regexp pos t)
  11337. (setq org-block-entry-blocking (org-get-heading)))
  11338. (throw 'dont-block nil)))))))) ; block, older sibling not done.
  11339. (defcustom org-track-ordered-property-with-tag nil
  11340. "Should the ORDERED property also be shown as a tag?
  11341. The ORDERED property decides if an entry should require subtasks to be
  11342. completed in sequence. Since a property is not very visible, setting
  11343. this option means that toggling the ORDERED property with the command
  11344. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  11345. not relevant for the behavior, but it makes things more visible.
  11346. Note that toggling the tag with tags commands will not change the property
  11347. and therefore not influence behavior!
  11348. This can be t, meaning the tag ORDERED should be used, It can also be a
  11349. string to select a different tag for this task."
  11350. :group 'org-todo
  11351. :type '(choice
  11352. (const :tag "No tracking" nil)
  11353. (const :tag "Track with ORDERED tag" t)
  11354. (string :tag "Use other tag")))
  11355. (defun org-toggle-ordered-property ()
  11356. "Toggle the ORDERED property of the current entry.
  11357. For better visibility, you can track the value of this property with a tag.
  11358. See variable `org-track-ordered-property-with-tag'."
  11359. (interactive)
  11360. (let* ((t1 org-track-ordered-property-with-tag)
  11361. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  11362. (save-excursion
  11363. (org-back-to-heading)
  11364. (if (org-entry-get nil "ORDERED")
  11365. (progn
  11366. (org-delete-property "ORDERED" "PROPERTIES")
  11367. (and tag (org-toggle-tag tag 'off))
  11368. (message "Subtasks can be completed in arbitrary order"))
  11369. (org-entry-put nil "ORDERED" "t")
  11370. (and tag (org-toggle-tag tag 'on))
  11371. (message "Subtasks must be completed in sequence")))))
  11372. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  11373. (defun org-block-todo-from-checkboxes (change-plist)
  11374. "Block turning an entry into a TODO, using checkboxes.
  11375. This checks whether the current task should be blocked from state
  11376. changes because there are unchecked boxes in this entry."
  11377. (if (not org-enforce-todo-checkbox-dependencies)
  11378. t ; if locally turned off don't block
  11379. (catch 'dont-block
  11380. ;; If this is not a todo state change, or if this entry is already DONE,
  11381. ;; do not block
  11382. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  11383. (member (plist-get change-plist :from)
  11384. (cons 'done org-done-keywords))
  11385. (member (plist-get change-plist :to)
  11386. (cons 'todo org-not-done-keywords))
  11387. (not (plist-get change-plist :to)))
  11388. (throw 'dont-block t))
  11389. ;; If this task has checkboxes that are not checked, it's blocked
  11390. (save-excursion
  11391. (org-back-to-heading t)
  11392. (let ((beg (point)) end)
  11393. (outline-next-heading)
  11394. (setq end (point))
  11395. (goto-char beg)
  11396. (if (org-list-search-forward
  11397. (concat (org-item-beginning-re)
  11398. "\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?"
  11399. "\\[[- ]\\]")
  11400. end t)
  11401. (progn
  11402. (if (boundp 'org-blocked-by-checkboxes)
  11403. (setq org-blocked-by-checkboxes t))
  11404. (throw 'dont-block nil)))))
  11405. t))) ; do not block
  11406. (defun org-entry-blocked-p ()
  11407. "Is the current entry blocked?"
  11408. (org-with-silent-modifications
  11409. (if (org-entry-get nil "NOBLOCKING")
  11410. nil ;; Never block this entry
  11411. (not (run-hook-with-args-until-failure
  11412. 'org-blocker-hook
  11413. (list :type 'todo-state-change
  11414. :position (point)
  11415. :from 'todo
  11416. :to 'done))))))
  11417. (defun org-update-statistics-cookies (all)
  11418. "Update the statistics cookie, either from TODO or from checkboxes.
  11419. This should be called with the cursor in a line with a statistics cookie."
  11420. (interactive "P")
  11421. (if all
  11422. (progn
  11423. (org-update-checkbox-count 'all)
  11424. (org-map-entries 'org-update-parent-todo-statistics))
  11425. (if (not (org-at-heading-p))
  11426. (org-update-checkbox-count)
  11427. (let ((pos (point-marker))
  11428. end l1 l2)
  11429. (ignore-errors (org-back-to-heading t))
  11430. (if (not (org-at-heading-p))
  11431. (org-update-checkbox-count)
  11432. (setq l1 (org-outline-level))
  11433. (setq end (save-excursion
  11434. (outline-next-heading)
  11435. (if (org-at-heading-p) (setq l2 (org-outline-level)))
  11436. (point)))
  11437. (if (and (save-excursion
  11438. (re-search-forward
  11439. "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) \\[[- X]\\]" end t))
  11440. (not (save-excursion (re-search-forward
  11441. ":COOKIE_DATA:.*\\<todo\\>" end t))))
  11442. (org-update-checkbox-count)
  11443. (if (and l2 (> l2 l1))
  11444. (progn
  11445. (goto-char end)
  11446. (org-update-parent-todo-statistics))
  11447. (goto-char pos)
  11448. (beginning-of-line 1)
  11449. (while (re-search-forward
  11450. "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)"
  11451. (point-at-eol) t)
  11452. (replace-match (if (match-end 2) "[100%]" "[0/0]") t t)))))
  11453. (goto-char pos)
  11454. (move-marker pos nil)))))
  11455. (defvar org-entry-property-inherited-from) ;; defined below
  11456. (defun org-update-parent-todo-statistics ()
  11457. "Update any statistics cookie in the parent of the current headline.
  11458. When `org-hierarchical-todo-statistics' is nil, statistics will cover
  11459. the entire subtree and this will travel up the hierarchy and update
  11460. statistics everywhere."
  11461. (let* ((prop (save-excursion (org-up-heading-safe)
  11462. (org-entry-get nil "COOKIE_DATA" 'inherit)))
  11463. (recursive (or (not org-hierarchical-todo-statistics)
  11464. (and prop (string-match "\\<recursive\\>" prop))))
  11465. (lim (or (and prop (marker-position org-entry-property-inherited-from))
  11466. 0))
  11467. (first t)
  11468. (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  11469. level ltoggle l1 new ndel
  11470. (cnt-all 0) (cnt-done 0) is-percent kwd
  11471. checkbox-beg ov ovs ove cookie-present)
  11472. (catch 'exit
  11473. (save-excursion
  11474. (beginning-of-line 1)
  11475. (setq ltoggle (funcall outline-level))
  11476. ;; Three situations are to consider:
  11477. ;; 1. if `org-hierarchical-todo-statistics' is nil, repeat up
  11478. ;; to the top-level ancestor on the headline;
  11479. ;; 2. If parent has "recursive" property, repeat up to the
  11480. ;; headline setting that property, taking inheritance into
  11481. ;; account;
  11482. ;; 3. Else, move up to direct parent and proceed only once.
  11483. (while (and (setq level (org-up-heading-safe))
  11484. (or recursive first)
  11485. (>= (point) lim))
  11486. (setq first nil cookie-present nil)
  11487. (unless (and level
  11488. (not (string-match
  11489. "\\<checkbox\\>"
  11490. (downcase (or (org-entry-get nil "COOKIE_DATA")
  11491. "")))))
  11492. (throw 'exit nil))
  11493. (while (re-search-forward box-re (point-at-eol) t)
  11494. (setq cnt-all 0 cnt-done 0 cookie-present t)
  11495. (setq is-percent (match-end 2) checkbox-beg (match-beginning 0))
  11496. (save-match-data
  11497. (unless (outline-next-heading) (throw 'exit nil))
  11498. (while (and (looking-at org-complex-heading-regexp)
  11499. (> (setq l1 (length (match-string 1))) level))
  11500. (setq kwd (and (or recursive (= l1 ltoggle))
  11501. (match-string 2)))
  11502. (if (or (eq org-provide-todo-statistics 'all-headlines)
  11503. (and (listp org-provide-todo-statistics)
  11504. (or (member kwd org-provide-todo-statistics)
  11505. (member kwd org-done-keywords))))
  11506. (setq cnt-all (1+ cnt-all))
  11507. (if (eq org-provide-todo-statistics t)
  11508. (and kwd (setq cnt-all (1+ cnt-all)))))
  11509. (and (member kwd org-done-keywords)
  11510. (setq cnt-done (1+ cnt-done)))
  11511. (outline-next-heading)))
  11512. (setq new
  11513. (if is-percent
  11514. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  11515. (format "[%d/%d]" cnt-done cnt-all))
  11516. ndel (- (match-end 0) checkbox-beg))
  11517. ;; handle overlays when updating cookie from column view
  11518. (when (setq ov (car (overlays-at checkbox-beg)))
  11519. (setq ovs (overlay-start ov) ove (overlay-end ov))
  11520. (delete-overlay ov))
  11521. (goto-char checkbox-beg)
  11522. (insert new)
  11523. (delete-region (point) (+ (point) ndel))
  11524. (when org-auto-align-tags (org-fix-tags-on-the-fly))
  11525. (when ov (move-overlay ov ovs ove)))
  11526. (when cookie-present
  11527. (run-hook-with-args 'org-after-todo-statistics-hook
  11528. cnt-done (- cnt-all cnt-done))))))
  11529. (run-hooks 'org-todo-statistics-hook)))
  11530. (defvar org-after-todo-statistics-hook nil
  11531. "Hook that is called after a TODO statistics cookie has been updated.
  11532. Each function is called with two arguments: the number of not-done entries
  11533. and the number of done entries.
  11534. For example, the following function, when added to this hook, will switch
  11535. an entry to DONE when all children are done, and back to TODO when new
  11536. entries are set to a TODO status. Note that this hook is only called
  11537. when there is a statistics cookie in the headline!
  11538. (defun org-summary-todo (n-done n-not-done)
  11539. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  11540. (let (org-log-done org-log-states) ; turn off logging
  11541. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  11542. ")
  11543. (defvar org-todo-statistics-hook nil
  11544. "Hook that is run whenever Org thinks TODO statistics should be updated.
  11545. This hook runs even if there is no statistics cookie present, in which case
  11546. `org-after-todo-statistics-hook' would not run.")
  11547. (defun org-todo-trigger-tag-changes (state)
  11548. "Apply the changes defined in `org-todo-state-tags-triggers'."
  11549. (let ((l org-todo-state-tags-triggers)
  11550. changes)
  11551. (when (or (not state) (equal state ""))
  11552. (setq changes (append changes (cdr (assoc "" l)))))
  11553. (when (and (stringp state) (> (length state) 0))
  11554. (setq changes (append changes (cdr (assoc state l)))))
  11555. (when (member state org-not-done-keywords)
  11556. (setq changes (append changes (cdr (assoc 'todo l)))))
  11557. (when (member state org-done-keywords)
  11558. (setq changes (append changes (cdr (assoc 'done l)))))
  11559. (dolist (c changes)
  11560. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  11561. (defun org-local-logging (value)
  11562. "Get logging settings from a property VALUE."
  11563. (let* (words w a)
  11564. ;; directly set the variables, they are already local.
  11565. (setq org-log-done nil
  11566. org-log-repeat nil
  11567. org-todo-log-states nil)
  11568. (setq words (org-split-string value))
  11569. (while (setq w (pop words))
  11570. (cond
  11571. ((setq a (assoc w org-startup-options))
  11572. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  11573. (set (nth 1 a) (nth 2 a))))
  11574. ((setq a (org-extract-log-state-settings w))
  11575. (and (member (car a) org-todo-keywords-1)
  11576. (push a org-todo-log-states)))))))
  11577. (defun org-get-todo-sequence-head (kwd)
  11578. "Return the head of the TODO sequence to which KWD belongs.
  11579. If KWD is not set, check if there is a text property remembering the
  11580. right sequence."
  11581. (let (p)
  11582. (cond
  11583. ((not kwd)
  11584. (or (get-text-property (point-at-bol) 'org-todo-head)
  11585. (progn
  11586. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  11587. nil (point-at-eol)))
  11588. (get-text-property p 'org-todo-head))))
  11589. ((not (member kwd org-todo-keywords-1))
  11590. (car org-todo-keywords-1))
  11591. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  11592. (defun org-fast-todo-selection ()
  11593. "Fast TODO keyword selection with single keys.
  11594. Returns the new TODO keyword, or nil if no state change should occur."
  11595. (let* ((fulltable org-todo-key-alist)
  11596. (done-keywords org-done-keywords) ;; needed for the faces.
  11597. (maxlen (apply 'max (mapcar
  11598. (lambda (x)
  11599. (if (stringp (car x)) (string-width (car x)) 0))
  11600. fulltable)))
  11601. (expert nil)
  11602. (fwidth (+ maxlen 3 1 3))
  11603. (ncol (/ (- (window-width) 4) fwidth))
  11604. tg cnt e c tbl
  11605. groups ingroup)
  11606. (save-excursion
  11607. (save-window-excursion
  11608. (if expert
  11609. (set-buffer (get-buffer-create " *Org todo*"))
  11610. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  11611. (erase-buffer)
  11612. (org-set-local 'org-done-keywords done-keywords)
  11613. (setq tbl fulltable cnt 0)
  11614. (while (setq e (pop tbl))
  11615. (cond
  11616. ((equal e '(:startgroup))
  11617. (push '() groups) (setq ingroup t)
  11618. (when (not (= cnt 0))
  11619. (setq cnt 0)
  11620. (insert "\n"))
  11621. (insert "{ "))
  11622. ((equal e '(:endgroup))
  11623. (setq ingroup nil cnt 0)
  11624. (insert "}\n"))
  11625. ((equal e '(:newline))
  11626. (when (not (= cnt 0))
  11627. (setq cnt 0)
  11628. (insert "\n")
  11629. (setq e (car tbl))
  11630. (while (equal (car tbl) '(:newline))
  11631. (insert "\n")
  11632. (setq tbl (cdr tbl)))))
  11633. (t
  11634. (setq tg (car e) c (cdr e))
  11635. (if ingroup (push tg (car groups)))
  11636. (setq tg (org-add-props tg nil 'face
  11637. (org-get-todo-face tg)))
  11638. (if (and (= cnt 0) (not ingroup)) (insert " "))
  11639. (insert "[" c "] " tg (make-string
  11640. (- fwidth 4 (length tg)) ?\ ))
  11641. (when (= (setq cnt (1+ cnt)) ncol)
  11642. (insert "\n")
  11643. (if ingroup (insert " "))
  11644. (setq cnt 0)))))
  11645. (insert "\n")
  11646. (goto-char (point-min))
  11647. (if (not expert) (org-fit-window-to-buffer))
  11648. (message "[a-z..]:Set [SPC]:clear")
  11649. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  11650. (cond
  11651. ((or (= c ?\C-g)
  11652. (and (= c ?q) (not (rassoc c fulltable))))
  11653. (setq quit-flag t))
  11654. ((= c ?\ ) nil)
  11655. ((setq e (rassoc c fulltable) tg (car e))
  11656. tg)
  11657. (t (setq quit-flag t)))))))
  11658. (defun org-entry-is-todo-p ()
  11659. (member (org-get-todo-state) org-not-done-keywords))
  11660. (defun org-entry-is-done-p ()
  11661. (member (org-get-todo-state) org-done-keywords))
  11662. (defun org-get-todo-state ()
  11663. "Return the TODO keyword of the current subtree."
  11664. (save-excursion
  11665. (org-back-to-heading t)
  11666. (and (looking-at org-todo-line-regexp)
  11667. (match-end 2)
  11668. (match-string 2))))
  11669. (defun org-at-date-range-p (&optional inactive-ok)
  11670. "Is the cursor inside a date range?"
  11671. (interactive)
  11672. (save-excursion
  11673. (catch 'exit
  11674. (let ((pos (point)))
  11675. (skip-chars-backward "^[<\r\n")
  11676. (skip-chars-backward "<[")
  11677. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  11678. (>= (match-end 0) pos)
  11679. (throw 'exit t))
  11680. (skip-chars-backward "^<[\r\n")
  11681. (skip-chars-backward "<[")
  11682. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  11683. (>= (match-end 0) pos)
  11684. (throw 'exit t)))
  11685. nil)))
  11686. (defun org-get-repeat (&optional tagline)
  11687. "Check if there is a deadline/schedule with repeater in this entry."
  11688. (save-match-data
  11689. (save-excursion
  11690. (org-back-to-heading t)
  11691. (and (re-search-forward (if tagline
  11692. (concat tagline "\\s-*" org-repeat-re)
  11693. org-repeat-re)
  11694. (org-entry-end-position) t)
  11695. (match-string-no-properties 1)))))
  11696. (defvar org-last-changed-timestamp)
  11697. (defvar org-last-inserted-timestamp)
  11698. (defvar org-log-post-message)
  11699. (defvar org-log-note-purpose)
  11700. (defvar org-log-note-how)
  11701. (defvar org-log-note-extra)
  11702. (defun org-auto-repeat-maybe (done-word)
  11703. "Check if the current headline contains a repeated deadline/schedule.
  11704. If yes, set TODO state back to what it was and change the base date
  11705. of repeating deadline/scheduled time stamps to new date.
  11706. This function is run automatically after each state change to a DONE state."
  11707. ;; last-state is dynamically scoped into this function
  11708. (let* ((repeat (org-get-repeat))
  11709. (aa (assoc org-last-state org-todo-kwd-alist))
  11710. (interpret (nth 1 aa))
  11711. (head (nth 2 aa))
  11712. (whata '(("h" . hour) ("d" . day) ("m" . month) ("y" . year)))
  11713. (msg "Entry repeats: ")
  11714. (org-log-done nil)
  11715. (org-todo-log-states nil)
  11716. re type n what ts time to-state)
  11717. (when repeat
  11718. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  11719. (setq to-state (or (org-entry-get nil "REPEAT_TO_STATE")
  11720. org-todo-repeat-to-state))
  11721. (unless (and to-state (member to-state org-todo-keywords-1))
  11722. (setq to-state (if (eq interpret 'type) org-last-state head)))
  11723. (org-todo to-state)
  11724. (when (or org-log-repeat (org-entry-get nil "CLOCK"))
  11725. (org-entry-put nil "LAST_REPEAT" (format-time-string
  11726. (org-time-stamp-format t t))))
  11727. (when org-log-repeat
  11728. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  11729. (memq 'org-add-log-note post-command-hook))
  11730. ;; OK, we are already setup for some record
  11731. (if (eq org-log-repeat 'note)
  11732. ;; make sure we take a note, not only a time stamp
  11733. (setq org-log-note-how 'note))
  11734. ;; Set up for taking a record
  11735. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  11736. org-last-state
  11737. 'findpos org-log-repeat)))
  11738. (org-back-to-heading t)
  11739. (org-add-planning-info nil nil 'closed)
  11740. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  11741. org-deadline-time-regexp "\\)\\|\\("
  11742. org-ts-regexp "\\)"))
  11743. (while (re-search-forward
  11744. re (save-excursion (outline-next-heading) (point)) t)
  11745. (setq type (if (match-end 1) org-scheduled-string
  11746. (if (match-end 3) org-deadline-string "Plain:"))
  11747. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  11748. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts)
  11749. (setq n (string-to-number (match-string 2 ts))
  11750. what (match-string 3 ts))
  11751. (if (equal what "w") (setq n (* n 7) what "d"))
  11752. (if (and (equal what "h") (not (string-match "[0-9]\\{1,2\\}:[0-9]\\{2\\}" ts)))
  11753. (user-error "Cannot repeat in Repeat in %d hour(s) because no hour has been set" n))
  11754. ;; Preparation, see if we need to modify the start date for the change
  11755. (when (match-end 1)
  11756. (setq time (save-match-data (org-time-string-to-time ts)))
  11757. (cond
  11758. ((equal (match-string 1 ts) ".")
  11759. ;; Shift starting date to today
  11760. (org-timestamp-change
  11761. (- (org-today) (time-to-days time))
  11762. 'day))
  11763. ((equal (match-string 1 ts) "+")
  11764. (let ((nshiftmax 10) (nshift 0))
  11765. (while (or (= nshift 0)
  11766. (<= (time-to-days time)
  11767. (time-to-days (current-time))))
  11768. (when (= (incf nshift) nshiftmax)
  11769. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  11770. (error "Abort")))
  11771. (org-timestamp-change n (cdr (assoc what whata)))
  11772. (org-at-timestamp-p t)
  11773. (setq ts (match-string 1))
  11774. (setq time (save-match-data (org-time-string-to-time ts)))))
  11775. (org-timestamp-change (- n) (cdr (assoc what whata)))
  11776. ;; rematch, so that we have everything in place for the real shift
  11777. (org-at-timestamp-p t)
  11778. (setq ts (match-string 1))
  11779. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts))))
  11780. (save-excursion (org-timestamp-change n (cdr (assoc what whata)) nil t))
  11781. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  11782. (setq org-log-post-message msg)
  11783. (message "%s" msg))))
  11784. (defun org-show-todo-tree (arg)
  11785. "Make a compact tree which shows all headlines marked with TODO.
  11786. The tree will show the lines where the regexp matches, and all higher
  11787. headlines above the match.
  11788. With a \\[universal-argument] prefix, prompt for a regexp to match.
  11789. With a numeric prefix N, construct a sparse tree for the Nth element
  11790. of `org-todo-keywords-1'."
  11791. (interactive "P")
  11792. (let ((case-fold-search nil)
  11793. (kwd-re
  11794. (cond ((null arg) org-not-done-regexp)
  11795. ((equal arg '(4))
  11796. (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
  11797. (mapcar 'list org-todo-keywords-1))))
  11798. (concat "\\("
  11799. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  11800. "\\)\\>")))
  11801. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  11802. (regexp-quote (nth (1- (prefix-numeric-value arg))
  11803. org-todo-keywords-1)))
  11804. (t (user-error "Invalid prefix argument: %s" arg)))))
  11805. (message "%d TODO entries found"
  11806. (org-occur (concat "^" org-outline-regexp " *" kwd-re )))))
  11807. (defun org-deadline (arg &optional time)
  11808. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  11809. With one universal prefix argument, remove any deadline from the item.
  11810. With two universal prefix arguments, prompt for a warning delay.
  11811. With argument TIME, set the deadline at the corresponding date. TIME
  11812. can either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  11813. (interactive "P")
  11814. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  11815. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  11816. 'region-start-level 'region))
  11817. org-loop-over-headlines-in-active-region)
  11818. (org-map-entries
  11819. `(org-deadline ',arg ,time)
  11820. org-loop-over-headlines-in-active-region
  11821. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  11822. (let* ((old-date (org-entry-get nil "DEADLINE"))
  11823. (old-date-time (if old-date (org-time-string-to-time old-date)))
  11824. (repeater (and old-date
  11825. (string-match
  11826. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  11827. old-date)
  11828. (match-string 1 old-date))))
  11829. (cond
  11830. ((equal arg '(4))
  11831. (when (and old-date org-log-redeadline)
  11832. (org-add-log-setup 'deldeadline nil old-date 'findpos
  11833. org-log-redeadline))
  11834. (org-remove-timestamp-with-keyword org-deadline-string)
  11835. (message "Item no longer has a deadline."))
  11836. ((equal arg '(16))
  11837. (save-excursion
  11838. (org-back-to-heading t)
  11839. (if (re-search-forward
  11840. org-deadline-time-regexp
  11841. (save-excursion (outline-next-heading) (point)) t)
  11842. (let* ((rpl0 (match-string 1))
  11843. (rpl (replace-regexp-in-string " -[0-9]+[hdwmy]" "" rpl0)))
  11844. (replace-match
  11845. (concat org-deadline-string
  11846. " <" rpl
  11847. (format " -%dd"
  11848. (abs
  11849. (- (time-to-days
  11850. (save-match-data
  11851. (org-read-date nil t nil "Warn starting from" old-date-time)))
  11852. (time-to-days old-date-time))))
  11853. ">") t t))
  11854. (user-error "No deadline information to update"))))
  11855. (t
  11856. (org-add-planning-info 'deadline time 'closed)
  11857. (when (and old-date org-log-redeadline
  11858. (not (equal old-date
  11859. (substring org-last-inserted-timestamp 1 -1))))
  11860. (org-add-log-setup 'redeadline nil old-date 'findpos
  11861. org-log-redeadline))
  11862. (when repeater
  11863. (save-excursion
  11864. (org-back-to-heading t)
  11865. (when (re-search-forward (concat org-deadline-string " "
  11866. org-last-inserted-timestamp)
  11867. (save-excursion
  11868. (outline-next-heading) (point)) t)
  11869. (goto-char (1- (match-end 0)))
  11870. (insert " " repeater)
  11871. (setq org-last-inserted-timestamp
  11872. (concat (substring org-last-inserted-timestamp 0 -1)
  11873. " " repeater
  11874. (substring org-last-inserted-timestamp -1))))))
  11875. (message "Deadline on %s" org-last-inserted-timestamp))))))
  11876. (defun org-schedule (arg &optional time)
  11877. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  11878. With one universal prefix argument, remove any scheduling date from the item.
  11879. With two universal prefix arguments, prompt for a delay cookie.
  11880. With argument TIME, scheduled at the corresponding date. TIME can
  11881. either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  11882. (interactive "P")
  11883. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  11884. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  11885. 'region-start-level 'region))
  11886. org-loop-over-headlines-in-active-region)
  11887. (org-map-entries
  11888. `(org-schedule ',arg ,time)
  11889. org-loop-over-headlines-in-active-region
  11890. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  11891. (let* ((old-date (org-entry-get nil "SCHEDULED"))
  11892. (old-date-time (if old-date (org-time-string-to-time old-date)))
  11893. (repeater (and old-date
  11894. (string-match
  11895. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  11896. old-date)
  11897. (match-string 1 old-date))))
  11898. (cond
  11899. ((equal arg '(4))
  11900. (progn
  11901. (when (and old-date org-log-reschedule)
  11902. (org-add-log-setup 'delschedule nil old-date 'findpos
  11903. org-log-reschedule))
  11904. (org-remove-timestamp-with-keyword org-scheduled-string)
  11905. (message "Item is no longer scheduled.")))
  11906. ((equal arg '(16))
  11907. (save-excursion
  11908. (org-back-to-heading t)
  11909. (if (re-search-forward
  11910. org-scheduled-time-regexp
  11911. (save-excursion (outline-next-heading) (point)) t)
  11912. (let* ((rpl0 (match-string 1))
  11913. (rpl (replace-regexp-in-string " -[0-9]+[hdwmy]" "" rpl0)))
  11914. (replace-match
  11915. (concat org-scheduled-string
  11916. " <" rpl
  11917. (format " -%dd"
  11918. (abs
  11919. (- (time-to-days
  11920. (save-match-data
  11921. (org-read-date nil t nil "Delay until" old-date-time)))
  11922. (time-to-days old-date-time))))
  11923. ">") t t))
  11924. (user-error "No scheduled information to update"))))
  11925. (t
  11926. (org-add-planning-info 'scheduled time 'closed)
  11927. (when (and old-date org-log-reschedule
  11928. (not (equal old-date
  11929. (substring org-last-inserted-timestamp 1 -1))))
  11930. (org-add-log-setup 'reschedule nil old-date 'findpos
  11931. org-log-reschedule))
  11932. (when repeater
  11933. (save-excursion
  11934. (org-back-to-heading t)
  11935. (when (re-search-forward (concat org-scheduled-string " "
  11936. org-last-inserted-timestamp)
  11937. (save-excursion
  11938. (outline-next-heading) (point)) t)
  11939. (goto-char (1- (match-end 0)))
  11940. (insert " " repeater)
  11941. (setq org-last-inserted-timestamp
  11942. (concat (substring org-last-inserted-timestamp 0 -1)
  11943. " " repeater
  11944. (substring org-last-inserted-timestamp -1))))))
  11945. (message "Scheduled to %s" org-last-inserted-timestamp))))))
  11946. (defun org-get-scheduled-time (pom &optional inherit)
  11947. "Get the scheduled time as a time tuple, of a format suitable
  11948. for calling org-schedule with, or if there is no scheduling,
  11949. returns nil."
  11950. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  11951. (when time
  11952. (apply 'encode-time (org-parse-time-string time)))))
  11953. (defun org-get-deadline-time (pom &optional inherit)
  11954. "Get the deadline as a time tuple, of a format suitable for
  11955. calling org-deadline with, or if there is no scheduling, returns
  11956. nil."
  11957. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  11958. (when time
  11959. (apply 'encode-time (org-parse-time-string time)))))
  11960. (defun org-remove-timestamp-with-keyword (keyword)
  11961. "Remove all time stamps with KEYWORD in the current entry."
  11962. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  11963. beg)
  11964. (save-excursion
  11965. (org-back-to-heading t)
  11966. (setq beg (point))
  11967. (outline-next-heading)
  11968. (while (re-search-backward re beg t)
  11969. (replace-match "")
  11970. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  11971. (equal (char-before) ?\ ))
  11972. (backward-delete-char 1)
  11973. (if (string-match "^[ \t]*$" (buffer-substring
  11974. (point-at-bol) (point-at-eol)))
  11975. (delete-region (point-at-bol)
  11976. (min (point-max) (1+ (point-at-eol))))))))))
  11977. (defvar org-time-was-given) ; dynamically scoped parameter
  11978. (defvar org-end-time-was-given) ; dynamically scoped parameter
  11979. (defun org-add-planning-info (what &optional time &rest remove)
  11980. "Insert new timestamp with keyword in the line directly after the headline.
  11981. WHAT indicates what kind of time stamp to add. TIME indicates the time to use.
  11982. If non is given, the user is prompted for a date.
  11983. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  11984. be removed."
  11985. (interactive)
  11986. (let (org-time-was-given org-end-time-was-given ts
  11987. end default-time default-input)
  11988. (catch 'exit
  11989. (when (and (memq what '(scheduled deadline))
  11990. (or (not time)
  11991. (and (stringp time)
  11992. (string-match "^[-+]+[0-9]" time))))
  11993. ;; Try to get a default date/time from existing timestamp
  11994. (save-excursion
  11995. (org-back-to-heading t)
  11996. (setq end (save-excursion (outline-next-heading) (point)))
  11997. (when (re-search-forward (if (eq what 'scheduled)
  11998. org-scheduled-time-regexp
  11999. org-deadline-time-regexp)
  12000. end t)
  12001. (setq ts (match-string 1)
  12002. default-time
  12003. (apply 'encode-time (org-parse-time-string ts))
  12004. default-input (and ts (org-get-compact-tod ts))))))
  12005. (when what
  12006. (setq time
  12007. (if (stringp time)
  12008. ;; This is a string (relative or absolute), set proper date
  12009. (apply 'encode-time
  12010. (org-read-date-analyze
  12011. time default-time (decode-time default-time)))
  12012. ;; If necessary, get the time from the user
  12013. (or time (org-read-date nil 'to-time nil nil
  12014. default-time default-input)))))
  12015. (when (and org-insert-labeled-timestamps-at-point
  12016. (member what '(scheduled deadline)))
  12017. (insert
  12018. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  12019. (org-insert-time-stamp time org-time-was-given
  12020. nil nil nil (list org-end-time-was-given))
  12021. (setq what nil))
  12022. (save-excursion
  12023. (save-restriction
  12024. (let (col list elt ts buffer-invisibility-spec)
  12025. (org-back-to-heading t)
  12026. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"))
  12027. (goto-char (match-end 1))
  12028. (setq col (current-column))
  12029. (goto-char (match-end 0))
  12030. (if (eobp) (insert "\n") (forward-char 1))
  12031. (when (and (not what)
  12032. (not (looking-at
  12033. (concat "[ \t]*"
  12034. org-keyword-time-not-clock-regexp))))
  12035. ;; Nothing to add, nothing to remove...... :-)
  12036. (throw 'exit nil))
  12037. (if (and (not (looking-at org-outline-regexp))
  12038. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  12039. "[^\r\n]*"))
  12040. (not (equal (match-string 1) org-clock-string)))
  12041. (narrow-to-region (match-beginning 0) (match-end 0))
  12042. (insert-before-markers "\n")
  12043. (backward-char 1)
  12044. (narrow-to-region (point) (point))
  12045. (and org-adapt-indentation (org-indent-to-column col)))
  12046. ;; Check if we have to remove something.
  12047. (setq list (cons what remove))
  12048. (while list
  12049. (setq elt (pop list))
  12050. (when (or (and (eq elt 'scheduled)
  12051. (re-search-forward org-scheduled-time-regexp nil t))
  12052. (and (eq elt 'deadline)
  12053. (re-search-forward org-deadline-time-regexp nil t))
  12054. (and (eq elt 'closed)
  12055. (re-search-forward org-closed-time-regexp nil t)))
  12056. (replace-match "")
  12057. (if (looking-at "--+<[^>]+>") (replace-match ""))))
  12058. (and (looking-at "[ \t]+") (replace-match ""))
  12059. (and org-adapt-indentation (bolp) (org-indent-to-column col))
  12060. (when what
  12061. (insert
  12062. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  12063. (cond ((eq what 'scheduled) org-scheduled-string)
  12064. ((eq what 'deadline) org-deadline-string)
  12065. ((eq what 'closed) org-closed-string))
  12066. " ")
  12067. (setq ts (org-insert-time-stamp
  12068. time
  12069. (or org-time-was-given
  12070. (and (eq what 'closed) org-log-done-with-time))
  12071. (eq what 'closed)
  12072. nil nil (list org-end-time-was-given)))
  12073. (insert
  12074. (if (not (or (bolp) (eq (char-before) ?\ )
  12075. (memq (char-after) '(32 10))
  12076. (eobp))) " " ""))
  12077. (end-of-line 1))
  12078. (goto-char (point-min))
  12079. (widen)
  12080. (if (and (looking-at "[ \t]*\n")
  12081. (equal (char-before) ?\n))
  12082. (delete-region (1- (point)) (point-at-eol)))
  12083. ts))))))
  12084. (defvar org-log-note-marker (make-marker))
  12085. (defvar org-log-note-purpose nil)
  12086. (defvar org-log-note-state nil)
  12087. (defvar org-log-note-previous-state nil)
  12088. (defvar org-log-note-how nil)
  12089. (defvar org-log-note-extra nil)
  12090. (defvar org-log-note-window-configuration nil)
  12091. (defvar org-log-note-return-to (make-marker))
  12092. (defvar org-log-note-effective-time nil
  12093. "Remembered current time so that dynamically scoped
  12094. `org-extend-today-until' affects tha timestamps in state change
  12095. log")
  12096. (defvar org-log-post-message nil
  12097. "Message to be displayed after a log note has been stored.
  12098. The auto-repeater uses this.")
  12099. (defun org-add-note ()
  12100. "Add a note to the current entry.
  12101. This is done in the same way as adding a state change note."
  12102. (interactive)
  12103. (org-add-log-setup 'note nil nil 'findpos nil))
  12104. (defvar org-property-end-re)
  12105. (defun org-add-log-setup (&optional purpose state prev-state
  12106. findpos how extra)
  12107. "Set up the post command hook to take a note.
  12108. If this is about to TODO state change, the new state is expected in STATE.
  12109. When FINDPOS is non-nil, find the correct position for the note in
  12110. the current entry. If not, assume that it can be inserted at point.
  12111. HOW is an indicator what kind of note should be created.
  12112. EXTRA is additional text that will be inserted into the notes buffer."
  12113. (let* ((org-log-into-drawer (org-log-into-drawer))
  12114. (drawer (cond ((stringp org-log-into-drawer)
  12115. org-log-into-drawer)
  12116. (org-log-into-drawer "LOGBOOK"))))
  12117. (save-restriction
  12118. (save-excursion
  12119. (when findpos
  12120. (org-back-to-heading t)
  12121. (narrow-to-region (point) (save-excursion
  12122. (outline-next-heading) (point)))
  12123. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"
  12124. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  12125. "[^\r\n]*\\)?"))
  12126. (goto-char (match-end 0))
  12127. (cond
  12128. (drawer
  12129. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  12130. nil t)
  12131. (progn
  12132. (goto-char (match-end 0))
  12133. (or org-log-states-order-reversed
  12134. (and (re-search-forward org-property-end-re nil t)
  12135. (goto-char (1- (match-beginning 0))))))
  12136. (insert "\n:" drawer ":\n:END:")
  12137. (beginning-of-line 0)
  12138. (org-indent-line)
  12139. (beginning-of-line 2)
  12140. (org-indent-line)
  12141. (end-of-line 0)))
  12142. ((and org-log-state-notes-insert-after-drawers
  12143. (save-excursion
  12144. (forward-line) (looking-at org-drawer-regexp)))
  12145. (forward-line)
  12146. (while (looking-at org-drawer-regexp)
  12147. (goto-char (match-end 0))
  12148. (re-search-forward org-property-end-re (point-max) t)
  12149. (forward-line))
  12150. (forward-line -1)))
  12151. (unless org-log-states-order-reversed
  12152. (and (= (char-after) ?\n) (forward-char 1))
  12153. (org-skip-over-state-notes)
  12154. (skip-chars-backward " \t\n\r")))
  12155. (move-marker org-log-note-marker (point))
  12156. (setq org-log-note-purpose purpose
  12157. org-log-note-state state
  12158. org-log-note-previous-state prev-state
  12159. org-log-note-how how
  12160. org-log-note-extra extra
  12161. org-log-note-effective-time (org-current-effective-time))
  12162. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  12163. (defun org-skip-over-state-notes ()
  12164. "Skip past the list of State notes in an entry."
  12165. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  12166. (when (ignore-errors (goto-char (org-in-item-p)))
  12167. (let* ((struct (org-list-struct))
  12168. (prevs (org-list-prevs-alist struct)))
  12169. (while (looking-at "[ \t]*- State")
  12170. (goto-char (or (org-list-get-next-item (point) struct prevs)
  12171. (org-list-get-item-end (point) struct)))))))
  12172. (defun org-add-log-note (&optional purpose)
  12173. "Pop up a window for taking a note, and add this note later at point."
  12174. (remove-hook 'post-command-hook 'org-add-log-note)
  12175. (setq org-log-note-window-configuration (current-window-configuration))
  12176. (delete-other-windows)
  12177. (move-marker org-log-note-return-to (point))
  12178. (org-pop-to-buffer-same-window (marker-buffer org-log-note-marker))
  12179. (goto-char org-log-note-marker)
  12180. (org-switch-to-buffer-other-window "*Org Note*")
  12181. (erase-buffer)
  12182. (if (memq org-log-note-how '(time state))
  12183. (let (current-prefix-arg) (org-store-log-note))
  12184. (let ((org-inhibit-startup t)) (org-mode))
  12185. (insert (format "# Insert note for %s.
  12186. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  12187. (cond
  12188. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  12189. ((eq org-log-note-purpose 'done) "closed todo item")
  12190. ((eq org-log-note-purpose 'state)
  12191. (format "state change from \"%s\" to \"%s\""
  12192. (or org-log-note-previous-state "")
  12193. (or org-log-note-state "")))
  12194. ((eq org-log-note-purpose 'reschedule)
  12195. "rescheduling")
  12196. ((eq org-log-note-purpose 'delschedule)
  12197. "no longer scheduled")
  12198. ((eq org-log-note-purpose 'redeadline)
  12199. "changing deadline")
  12200. ((eq org-log-note-purpose 'deldeadline)
  12201. "removing deadline")
  12202. ((eq org-log-note-purpose 'refile)
  12203. "refiling")
  12204. ((eq org-log-note-purpose 'note)
  12205. "this entry")
  12206. (t (error "This should not happen")))))
  12207. (if org-log-note-extra (insert org-log-note-extra))
  12208. (org-set-local 'org-finish-function 'org-store-log-note)
  12209. (run-hooks 'org-log-buffer-setup-hook)))
  12210. (defvar org-note-abort nil) ; dynamically scoped
  12211. (defun org-store-log-note ()
  12212. "Finish taking a log note, and insert it to where it belongs."
  12213. (let ((txt (buffer-string)))
  12214. (kill-buffer (current-buffer))
  12215. (let ((note (cdr (assq org-log-note-purpose org-log-note-headings)))
  12216. lines ind bul)
  12217. (while (string-match "\\`# .*\n[ \t\n]*" txt)
  12218. (setq txt (replace-match "" t t txt)))
  12219. (if (string-match "\\s-+\\'" txt)
  12220. (setq txt (replace-match "" t t txt)))
  12221. (setq lines (org-split-string txt "\n"))
  12222. (when (and note (string-match "\\S-" note))
  12223. (setq note
  12224. (org-replace-escapes
  12225. note
  12226. (list (cons "%u" (user-login-name))
  12227. (cons "%U" user-full-name)
  12228. (cons "%t" (format-time-string
  12229. (org-time-stamp-format 'long 'inactive)
  12230. org-log-note-effective-time))
  12231. (cons "%T" (format-time-string
  12232. (org-time-stamp-format 'long nil)
  12233. org-log-note-effective-time))
  12234. (cons "%d" (format-time-string
  12235. (org-time-stamp-format nil 'inactive)
  12236. org-log-note-effective-time))
  12237. (cons "%D" (format-time-string
  12238. (org-time-stamp-format nil nil)
  12239. org-log-note-effective-time))
  12240. (cons "%s" (if org-log-note-state
  12241. (concat "\"" org-log-note-state "\"")
  12242. ""))
  12243. (cons "%S" (if org-log-note-previous-state
  12244. (concat "\"" org-log-note-previous-state "\"")
  12245. "\"\"")))))
  12246. (if lines (setq note (concat note " \\\\")))
  12247. (push note lines))
  12248. (when (or current-prefix-arg org-note-abort)
  12249. (when org-log-into-drawer
  12250. (org-remove-empty-drawer-at
  12251. (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
  12252. org-log-note-marker))
  12253. (setq lines nil))
  12254. (when lines
  12255. (with-current-buffer (marker-buffer org-log-note-marker)
  12256. (save-excursion
  12257. (goto-char org-log-note-marker)
  12258. (move-marker org-log-note-marker nil)
  12259. (end-of-line 1)
  12260. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  12261. (setq ind (save-excursion
  12262. (if (ignore-errors (goto-char (org-in-item-p)))
  12263. (let ((struct (org-list-struct)))
  12264. (org-list-get-ind
  12265. (org-list-get-top-point struct) struct))
  12266. (skip-chars-backward " \r\t\n")
  12267. (cond
  12268. ((and (org-at-heading-p)
  12269. org-adapt-indentation)
  12270. (1+ (org-current-level)))
  12271. ((org-at-heading-p) 0)
  12272. (t (org-get-indentation))))))
  12273. (setq bul (org-list-bullet-string "-"))
  12274. (org-indent-line-to ind)
  12275. (insert bul (pop lines))
  12276. (let ((ind-body (+ (length bul) ind)))
  12277. (while lines
  12278. (insert "\n")
  12279. (org-indent-line-to ind-body)
  12280. (insert (pop lines))))
  12281. (message "Note stored")
  12282. (org-back-to-heading t)
  12283. (org-cycle-hide-drawers 'children))
  12284. ;; Fix `buffer-undo-list' when `org-store-log-note' is called
  12285. ;; from within `org-add-log-note' because `buffer-undo-list'
  12286. ;; is then modified outside of `org-with-remote-undo'.
  12287. (when (eq this-command 'org-agenda-todo)
  12288. (setcdr buffer-undo-list (cddr buffer-undo-list)))))))
  12289. ;; Don't add undo information when called from `org-agenda-todo'
  12290. (let ((buffer-undo-list (eq this-command 'org-agenda-todo)))
  12291. (set-window-configuration org-log-note-window-configuration)
  12292. (with-current-buffer (marker-buffer org-log-note-return-to)
  12293. (goto-char org-log-note-return-to))
  12294. (move-marker org-log-note-return-to nil)
  12295. (and org-log-post-message (message "%s" org-log-post-message))))
  12296. (defun org-remove-empty-drawer-at (drawer pos)
  12297. "Remove an empty drawer DRAWER at position POS.
  12298. POS may also be a marker."
  12299. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  12300. (save-excursion
  12301. (save-restriction
  12302. (widen)
  12303. (goto-char pos)
  12304. (if (org-in-regexp
  12305. (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
  12306. (replace-match ""))))))
  12307. (defvar org-ts-type nil)
  12308. (defun org-sparse-tree (&optional arg type)
  12309. "Create a sparse tree, prompt for the details.
  12310. This command can create sparse trees. You first need to select the type
  12311. of match used to create the tree:
  12312. t Show all TODO entries.
  12313. T Show entries with a specific TODO keyword.
  12314. m Show entries selected by a tags/property match.
  12315. p Enter a property name and its value (both with completion on existing
  12316. names/values) and show entries with that property.
  12317. r Show entries matching a regular expression (`/' can be used as well).
  12318. b Show deadlines and scheduled items before a date.
  12319. a Show deadlines and scheduled items after a date.
  12320. d Show deadlines due within `org-deadline-warning-days'.
  12321. D Show deadlines and scheduled items between a date range."
  12322. (interactive "P")
  12323. (setq type (or type org-sparse-tree-default-date-type))
  12324. (setq org-ts-type type)
  12325. (message "Sparse tree: [/]regexp [t]odo [T]odo-kwd [m]atch [p]roperty
  12326. [d]eadlines [b]efore-date [a]fter-date [D]ates range
  12327. [c]ycle through date types: %s"
  12328. (case type
  12329. (all "all timestamps")
  12330. (scheduled "only scheduled")
  12331. (deadline "only deadline")
  12332. (active "only active timestamps")
  12333. (inactive "only inactive timestamps")
  12334. (scheduled-or-deadline "scheduled/deadline")
  12335. (closed "with a closed time-stamp")
  12336. (otherwise "scheduled/deadline")))
  12337. (let ((answer (read-char-exclusive)))
  12338. (case answer
  12339. (?c
  12340. (org-sparse-tree
  12341. arg
  12342. (cadr (memq type '(scheduled-or-deadline all scheduled deadline active
  12343. inactive closed)))))
  12344. (?d (call-interactively #'org-check-deadlines))
  12345. (?b (call-interactively #'org-check-before-date))
  12346. (?a (call-interactively #'org-check-after-date))
  12347. (?D (call-interactively #'org-check-dates-range))
  12348. (?t (call-interactively #'org-show-todo-tree))
  12349. (?T (org-show-todo-tree '(4)))
  12350. (?m (call-interactively #'org-match-sparse-tree))
  12351. ((?p ?P)
  12352. (let* ((kwd (org-icompleting-read
  12353. "Property: " (mapcar #'list (org-buffer-property-keys))))
  12354. (value (org-icompleting-read
  12355. "Value: " (mapcar #'list (org-property-values kwd)))))
  12356. (unless (string-match "\\`{.*}\\'" value)
  12357. (setq value (concat "\"" value "\"")))
  12358. (org-match-sparse-tree arg (concat kwd "=" value))))
  12359. ((?r ?R ?/) (call-interactively #'org-occur))
  12360. (otherwise (user-error "No such sparse tree command \"%c\"" answer)))))
  12361. (defvar org-occur-highlights nil
  12362. "List of overlays used for occur matches.")
  12363. (make-variable-buffer-local 'org-occur-highlights)
  12364. (defvar org-occur-parameters nil
  12365. "Parameters of the active org-occur calls.
  12366. This is a list, each call to org-occur pushes as cons cell,
  12367. containing the regular expression and the callback, onto the list.
  12368. The list can contain several entries if `org-occur' has been called
  12369. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  12370. will only contain one set of parameters. When the highlights are
  12371. removed (for example with `C-c C-c', or with the next edit (depending
  12372. on `org-remove-highlights-with-change'), this variable is emptied
  12373. as well.")
  12374. (make-variable-buffer-local 'org-occur-parameters)
  12375. (defun org-occur (regexp &optional keep-previous callback)
  12376. "Make a compact tree which shows all matches of REGEXP.
  12377. The tree will show the lines where the regexp matches, and all higher
  12378. headlines above the match. It will also show the heading after the match,
  12379. to make sure editing the matching entry is easy.
  12380. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  12381. call to `org-occur' will be kept, to allow stacking of calls to this
  12382. command.
  12383. If CALLBACK is non-nil, it is a function which is called to confirm
  12384. that the match should indeed be shown."
  12385. (interactive "sRegexp: \nP")
  12386. (when (equal regexp "")
  12387. (user-error "Regexp cannot be empty"))
  12388. (unless keep-previous
  12389. (org-remove-occur-highlights nil nil t))
  12390. (push (cons regexp callback) org-occur-parameters)
  12391. (let ((cnt 0))
  12392. (save-excursion
  12393. (goto-char (point-min))
  12394. (if (or (not keep-previous) ; do not want to keep
  12395. (not org-occur-highlights)) ; no previous matches
  12396. ;; hide everything
  12397. (org-overview))
  12398. (while (re-search-forward regexp nil t)
  12399. (when (or (not callback)
  12400. (save-match-data (funcall callback)))
  12401. (setq cnt (1+ cnt))
  12402. (when org-highlight-sparse-tree-matches
  12403. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  12404. (org-show-context 'occur-tree))))
  12405. (when org-remove-highlights-with-change
  12406. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  12407. nil 'local))
  12408. (unless org-sparse-tree-open-archived-trees
  12409. (org-hide-archived-subtrees (point-min) (point-max)))
  12410. (run-hooks 'org-occur-hook)
  12411. (if (org-called-interactively-p 'interactive)
  12412. (message "%d match(es) for regexp %s" cnt regexp))
  12413. cnt))
  12414. (defun org-occur-next-match (&optional n reset)
  12415. "Function for `next-error-function' to find sparse tree matches.
  12416. N is the number of matches to move, when negative move backwards.
  12417. RESET is entirely ignored - this function always goes back to the
  12418. starting point when no match is found."
  12419. (let* ((limit (if (< n 0) (point-min) (point-max)))
  12420. (search-func (if (< n 0)
  12421. 'previous-single-char-property-change
  12422. 'next-single-char-property-change))
  12423. (n (abs n))
  12424. (pos (point))
  12425. p1)
  12426. (catch 'exit
  12427. (while (setq p1 (funcall search-func (point) 'org-type))
  12428. (when (equal p1 limit)
  12429. (goto-char pos)
  12430. (error "No more matches"))
  12431. (when (equal (get-char-property p1 'org-type) 'org-occur)
  12432. (setq n (1- n))
  12433. (when (= n 0)
  12434. (goto-char p1)
  12435. (throw 'exit (point))))
  12436. (goto-char p1))
  12437. (goto-char p1)
  12438. (error "No more matches"))))
  12439. (defun org-show-context (&optional key)
  12440. "Make sure point and context are visible.
  12441. How much context is shown depends upon the variables
  12442. `org-show-hierarchy-above', `org-show-following-heading',
  12443. `org-show-entry-below' and `org-show-siblings'."
  12444. (let ((heading-p (org-at-heading-p t))
  12445. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  12446. (following-p (org-get-alist-option org-show-following-heading key))
  12447. (entry-p (org-get-alist-option org-show-entry-below key))
  12448. (siblings-p (org-get-alist-option org-show-siblings key)))
  12449. ;; Show heading or entry text
  12450. (if (and heading-p (not entry-p))
  12451. (org-flag-heading nil) ; only show the heading
  12452. (and (or entry-p (outline-invisible-p) (org-invisible-p2))
  12453. (org-show-hidden-entry))) ; show entire entry
  12454. (when following-p
  12455. ;; Show next sibling, or heading below text
  12456. (save-excursion
  12457. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  12458. (org-flag-heading nil))))
  12459. (when siblings-p (org-show-siblings))
  12460. (when hierarchy-p
  12461. ;; show all higher headings, possibly with siblings
  12462. (save-excursion
  12463. (while (and (condition-case nil
  12464. (progn (org-up-heading-all 1) t)
  12465. (error nil))
  12466. (not (bobp)))
  12467. (org-flag-heading nil)
  12468. (when siblings-p (org-show-siblings)))))))
  12469. (defvar org-reveal-start-hook nil
  12470. "Hook run before revealing a location.")
  12471. (defun org-reveal (&optional siblings)
  12472. "Show current entry, hierarchy above it, and the following headline.
  12473. This can be used to show a consistent set of context around locations
  12474. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  12475. not t for the search context.
  12476. With optional argument SIBLINGS, on each level of the hierarchy all
  12477. siblings are shown. This repairs the tree structure to what it would
  12478. look like when opened with hierarchical calls to `org-cycle'.
  12479. With double optional argument \\[universal-argument] \\[universal-argument], \
  12480. go to the parent and show the
  12481. entire tree."
  12482. (interactive "P")
  12483. (run-hooks 'org-reveal-start-hook)
  12484. (let ((org-show-hierarchy-above t)
  12485. (org-show-following-heading t)
  12486. (org-show-siblings (if siblings t org-show-siblings)))
  12487. (org-show-context nil))
  12488. (when (equal siblings '(16))
  12489. (save-excursion
  12490. (when (org-up-heading-safe)
  12491. (org-show-subtree)
  12492. (run-hook-with-args 'org-cycle-hook 'subtree)))))
  12493. (defun org-highlight-new-match (beg end)
  12494. "Highlight from BEG to END and mark the highlight is an occur headline."
  12495. (let ((ov (make-overlay beg end)))
  12496. (overlay-put ov 'face 'secondary-selection)
  12497. (overlay-put ov 'org-type 'org-occur)
  12498. (push ov org-occur-highlights)))
  12499. (defun org-remove-occur-highlights (&optional beg end noremove)
  12500. "Remove the occur highlights from the buffer.
  12501. BEG and END are ignored. If NOREMOVE is nil, remove this function
  12502. from the `before-change-functions' in the current buffer."
  12503. (interactive)
  12504. (unless org-inhibit-highlight-removal
  12505. (mapc 'delete-overlay org-occur-highlights)
  12506. (setq org-occur-highlights nil)
  12507. (setq org-occur-parameters nil)
  12508. (unless noremove
  12509. (remove-hook 'before-change-functions
  12510. 'org-remove-occur-highlights 'local))))
  12511. ;;;; Priorities
  12512. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  12513. "Regular expression matching the priority indicator.")
  12514. (defvar org-remove-priority-next-time nil)
  12515. (defun org-priority-up ()
  12516. "Increase the priority of the current item."
  12517. (interactive)
  12518. (org-priority 'up))
  12519. (defun org-priority-down ()
  12520. "Decrease the priority of the current item."
  12521. (interactive)
  12522. (org-priority 'down))
  12523. (defun org-priority (&optional action show)
  12524. "Change the priority of an item.
  12525. ACTION can be `set', `up', `down', or a character."
  12526. (interactive "P")
  12527. (if (equal action '(4))
  12528. (org-show-priority)
  12529. (unless org-enable-priority-commands
  12530. (user-error "Priority commands are disabled"))
  12531. (setq action (or action 'set))
  12532. (let (current new news have remove)
  12533. (save-excursion
  12534. (org-back-to-heading t)
  12535. (if (looking-at org-priority-regexp)
  12536. (setq current (string-to-char (match-string 2))
  12537. have t))
  12538. (cond
  12539. ((eq action 'remove)
  12540. (setq remove t new ?\ ))
  12541. ((or (eq action 'set)
  12542. (if (featurep 'xemacs) (characterp action) (integerp action)))
  12543. (if (not (eq action 'set))
  12544. (setq new action)
  12545. (message "Priority %c-%c, SPC to remove: "
  12546. org-highest-priority org-lowest-priority)
  12547. (save-match-data
  12548. (setq new (read-char-exclusive))))
  12549. (if (and (= (upcase org-highest-priority) org-highest-priority)
  12550. (= (upcase org-lowest-priority) org-lowest-priority))
  12551. (setq new (upcase new)))
  12552. (cond ((equal new ?\ ) (setq remove t))
  12553. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  12554. (user-error "Priority must be between `%c' and `%c'"
  12555. org-highest-priority org-lowest-priority))))
  12556. ((eq action 'up)
  12557. (setq new (if have
  12558. (1- current) ; normal cycling
  12559. ;; last priority was empty
  12560. (if (eq last-command this-command)
  12561. org-lowest-priority ; wrap around empty to lowest
  12562. ;; default
  12563. (if org-priority-start-cycle-with-default
  12564. org-default-priority
  12565. (1- org-default-priority))))))
  12566. ((eq action 'down)
  12567. (setq new (if have
  12568. (1+ current) ; normal cycling
  12569. ;; last priority was empty
  12570. (if (eq last-command this-command)
  12571. org-highest-priority ; wrap around empty to highest
  12572. ;; default
  12573. (if org-priority-start-cycle-with-default
  12574. org-default-priority
  12575. (1+ org-default-priority))))))
  12576. (t (user-error "Invalid action")))
  12577. (if (or (< (upcase new) org-highest-priority)
  12578. (> (upcase new) org-lowest-priority))
  12579. (if (and (memq action '(up down))
  12580. (not have) (not (eq last-command this-command)))
  12581. ;; `new' is from default priority
  12582. (error
  12583. "The default can not be set, see `org-default-priority' why")
  12584. ;; normal cycling: `new' is beyond highest/lowest priority
  12585. ;; and is wrapped around to the empty priority
  12586. (setq remove t)))
  12587. (setq news (format "%c" new))
  12588. (if have
  12589. (if remove
  12590. (replace-match "" t t nil 1)
  12591. (replace-match news t t nil 2))
  12592. (if remove
  12593. (user-error "No priority cookie found in line")
  12594. (let ((case-fold-search nil))
  12595. (looking-at org-todo-line-regexp))
  12596. (if (match-end 2)
  12597. (progn
  12598. (goto-char (match-end 2))
  12599. (insert " [#" news "]"))
  12600. (goto-char (match-beginning 3))
  12601. (insert "[#" news "] "))))
  12602. (org-preserve-lc (org-set-tags nil 'align)))
  12603. (if remove
  12604. (message "Priority removed")
  12605. (message "Priority of current item set to %s" news)))))
  12606. (defun org-show-priority ()
  12607. "Show the priority of the current item.
  12608. This priority is composed of the main priority given with the [#A] cookies,
  12609. and by additional input from the age of a schedules or deadline entry."
  12610. (interactive)
  12611. (let ((pri (if (eq major-mode 'org-agenda-mode)
  12612. (org-get-at-bol 'priority)
  12613. (save-excursion
  12614. (save-match-data
  12615. (beginning-of-line)
  12616. (and (looking-at org-heading-regexp)
  12617. (org-get-priority (match-string 0))))))))
  12618. (message "Priority is %d" (if pri pri -1000))))
  12619. (defun org-get-priority (s)
  12620. "Find priority cookie and return priority."
  12621. (save-match-data
  12622. (if (functionp org-get-priority-function)
  12623. (funcall org-get-priority-function)
  12624. (if (not (string-match org-priority-regexp s))
  12625. (* 1000 (- org-lowest-priority org-default-priority))
  12626. (* 1000 (- org-lowest-priority
  12627. (string-to-char (match-string 2 s))))))))
  12628. ;;;; Tags
  12629. (defvar org-agenda-archives-mode)
  12630. (defvar org-map-continue-from nil
  12631. "Position from where mapping should continue.
  12632. Can be set by the action argument to `org-scan-tags' and `org-map-entries'.")
  12633. (defvar org-scanner-tags nil
  12634. "The current tag list while the tags scanner is running.")
  12635. (defvar org-trust-scanner-tags nil
  12636. "Should `org-get-tags-at' use the tags for the scanner.
  12637. This is for internal dynamical scoping only.
  12638. When this is non-nil, the function `org-get-tags-at' will return the value
  12639. of `org-scanner-tags' instead of building the list by itself. This
  12640. can lead to large speed-ups when the tags scanner is used in a file with
  12641. many entries, and when the list of tags is retrieved, for example to
  12642. obtain a list of properties. Building the tags list for each entry in such
  12643. a file becomes an N^2 operation - but with this variable set, it scales
  12644. as N.")
  12645. (defun org-scan-tags (action matcher todo-only &optional start-level)
  12646. "Scan headline tags with inheritance and produce output ACTION.
  12647. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  12648. or `agenda' to produce an entry list for an agenda view. It can also be
  12649. a Lisp form or a function that should be called at each matched headline, in
  12650. this case the return value is a list of all return values from these calls.
  12651. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  12652. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  12653. only lines with a not-done TODO keyword are included in the output.
  12654. This should be the same variable that was scoped into
  12655. and set by `org-make-tags-matcher' when it constructed MATCHER.
  12656. START-LEVEL can be a string with asterisks, reducing the scope to
  12657. headlines matching this string."
  12658. (require 'org-agenda)
  12659. (let* ((re (concat "^"
  12660. (if start-level
  12661. ;; Get the correct level to match
  12662. (concat "\\*\\{" (number-to-string start-level) "\\} ")
  12663. org-outline-regexp)
  12664. " *\\(\\<\\("
  12665. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  12666. (org-re "\\)\\>\\)? *\\(.*?\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*$")))
  12667. (props (list 'face 'default
  12668. 'done-face 'org-agenda-done
  12669. 'undone-face 'default
  12670. 'mouse-face 'highlight
  12671. 'org-not-done-regexp org-not-done-regexp
  12672. 'org-todo-regexp org-todo-regexp
  12673. 'org-complex-heading-regexp org-complex-heading-regexp
  12674. 'help-echo
  12675. (format "mouse-2 or RET jump to org file %s"
  12676. (abbreviate-file-name
  12677. (or (buffer-file-name (buffer-base-buffer))
  12678. (buffer-name (buffer-base-buffer)))))))
  12679. (org-map-continue-from nil)
  12680. lspos tags tags-list
  12681. (tags-alist (list (cons 0 org-file-tags)))
  12682. (llast 0) rtn rtn1 level category i txt
  12683. todo marker entry priority)
  12684. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  12685. (setq action (list 'lambda nil action)))
  12686. (save-excursion
  12687. (goto-char (point-min))
  12688. (when (eq action 'sparse-tree)
  12689. (org-overview)
  12690. (org-remove-occur-highlights))
  12691. (while (let (case-fold-search)
  12692. (re-search-forward re nil t))
  12693. (setq org-map-continue-from nil)
  12694. (catch :skip
  12695. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  12696. tags (if (match-end 4) (org-match-string-no-properties 4)))
  12697. (goto-char (setq lspos (match-beginning 0)))
  12698. (setq level (org-reduced-level (org-outline-level))
  12699. category (org-get-category))
  12700. (setq i llast llast level)
  12701. ;; remove tag lists from same and sublevels
  12702. (while (>= i level)
  12703. (when (setq entry (assoc i tags-alist))
  12704. (setq tags-alist (delete entry tags-alist)))
  12705. (setq i (1- i)))
  12706. ;; add the next tags
  12707. (when tags
  12708. (setq tags (org-split-string tags ":")
  12709. tags-alist
  12710. (cons (cons level tags) tags-alist)))
  12711. ;; compile tags for current headline
  12712. (setq tags-list
  12713. (if org-use-tag-inheritance
  12714. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  12715. tags)
  12716. org-scanner-tags tags-list)
  12717. (when org-use-tag-inheritance
  12718. (setcdr (car tags-alist)
  12719. (mapcar (lambda (x)
  12720. (setq x (copy-sequence x))
  12721. (org-add-prop-inherited x))
  12722. (cdar tags-alist))))
  12723. (when (and tags org-use-tag-inheritance
  12724. (or (not (eq t org-use-tag-inheritance))
  12725. org-tags-exclude-from-inheritance))
  12726. ;; selective inheritance, remove uninherited ones
  12727. (setcdr (car tags-alist)
  12728. (org-remove-uninherited-tags (cdar tags-alist))))
  12729. (when (and
  12730. ;; eval matcher only when the todo condition is OK
  12731. (and (or (not todo-only) (member todo org-not-done-keywords))
  12732. (let ((case-fold-search t) (org-trust-scanner-tags t))
  12733. (eval matcher)))
  12734. ;; Call the skipper, but return t if it does not skip,
  12735. ;; so that the `and' form continues evaluating
  12736. (progn
  12737. (unless (eq action 'sparse-tree) (org-agenda-skip))
  12738. t)
  12739. ;; Check if timestamps are deselecting this entry
  12740. (or (not todo-only)
  12741. (and (member todo org-not-done-keywords)
  12742. (or (not org-agenda-tags-todo-honor-ignore-options)
  12743. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item))))))
  12744. ;; select this headline
  12745. (cond
  12746. ((eq action 'sparse-tree)
  12747. (and org-highlight-sparse-tree-matches
  12748. (org-get-heading) (match-end 0)
  12749. (org-highlight-new-match
  12750. (match-beginning 1) (match-end 1)))
  12751. (org-show-context 'tags-tree))
  12752. ((eq action 'agenda)
  12753. (setq txt (org-agenda-format-item
  12754. ""
  12755. (concat
  12756. (if (eq org-tags-match-list-sublevels 'indented)
  12757. (make-string (1- level) ?.) "")
  12758. (org-get-heading))
  12759. level category
  12760. tags-list)
  12761. priority (org-get-priority txt))
  12762. (goto-char lspos)
  12763. (setq marker (org-agenda-new-marker))
  12764. (org-add-props txt props
  12765. 'org-marker marker 'org-hd-marker marker 'org-category category
  12766. 'todo-state todo
  12767. 'priority priority 'type "tagsmatch")
  12768. (push txt rtn))
  12769. ((functionp action)
  12770. (setq org-map-continue-from nil)
  12771. (save-excursion
  12772. (setq rtn1 (funcall action))
  12773. (push rtn1 rtn)))
  12774. (t (user-error "Invalid action")))
  12775. ;; if we are to skip sublevels, jump to end of subtree
  12776. (unless org-tags-match-list-sublevels
  12777. (org-end-of-subtree t)
  12778. (backward-char 1))))
  12779. ;; Get the correct position from where to continue
  12780. (if org-map-continue-from
  12781. (goto-char org-map-continue-from)
  12782. (and (= (point) lspos) (end-of-line 1)))))
  12783. (when (and (eq action 'sparse-tree)
  12784. (not org-sparse-tree-open-archived-trees))
  12785. (org-hide-archived-subtrees (point-min) (point-max)))
  12786. (nreverse rtn)))
  12787. (defun org-remove-uninherited-tags (tags)
  12788. "Remove all tags that are not inherited from the list TAGS."
  12789. (cond
  12790. ((eq org-use-tag-inheritance t)
  12791. (if org-tags-exclude-from-inheritance
  12792. (org-delete-all org-tags-exclude-from-inheritance tags)
  12793. tags))
  12794. ((not org-use-tag-inheritance) nil)
  12795. ((stringp org-use-tag-inheritance)
  12796. (delq nil (mapcar
  12797. (lambda (x)
  12798. (if (and (string-match org-use-tag-inheritance x)
  12799. (not (member x org-tags-exclude-from-inheritance)))
  12800. x nil))
  12801. tags)))
  12802. ((listp org-use-tag-inheritance)
  12803. (delq nil (mapcar
  12804. (lambda (x)
  12805. (if (member x org-use-tag-inheritance) x nil))
  12806. tags)))))
  12807. (defun org-match-sparse-tree (&optional todo-only match)
  12808. "Create a sparse tree according to tags string MATCH.
  12809. MATCH can contain positive and negative selection of tags, like
  12810. \"+WORK+URGENT-WITHBOSS\".
  12811. If optional argument TODO-ONLY is non-nil, only select lines that are
  12812. also TODO lines."
  12813. (interactive "P")
  12814. (org-agenda-prepare-buffers (list (current-buffer)))
  12815. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  12816. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  12817. (defvar org-cached-props nil)
  12818. (defun org-cached-entry-get (pom property)
  12819. (if (or (eq t org-use-property-inheritance)
  12820. (and (stringp org-use-property-inheritance)
  12821. (string-match org-use-property-inheritance property))
  12822. (and (listp org-use-property-inheritance)
  12823. (member property org-use-property-inheritance)))
  12824. ;; Caching is not possible, check it directly
  12825. (org-entry-get pom property 'inherit)
  12826. ;; Get all properties, so that we can do complicated checks easily
  12827. (cdr (assoc property (or org-cached-props
  12828. (setq org-cached-props
  12829. (org-entry-properties pom)))))))
  12830. (defun org-global-tags-completion-table (&optional files)
  12831. "Return the list of all tags in all agenda buffer/files.
  12832. Optional FILES argument is a list of files which can be used
  12833. instead of the agenda files."
  12834. (save-excursion
  12835. (org-uniquify
  12836. (delq nil
  12837. (apply 'append
  12838. (mapcar
  12839. (lambda (file)
  12840. (set-buffer (find-file-noselect file))
  12841. (append (org-get-buffer-tags)
  12842. (mapcar (lambda (x) (if (stringp (car-safe x))
  12843. (list (car-safe x)) nil))
  12844. org-tag-alist)))
  12845. (if (and files (car files))
  12846. files
  12847. (org-agenda-files))))))))
  12848. (defun org-make-tags-matcher (match)
  12849. "Create the TAGS/TODO matcher form for the selection string MATCH.
  12850. The variable `todo-only' is scoped dynamically into this function.
  12851. It will be set to t if the matcher restricts matching to TODO entries,
  12852. otherwise will not be touched.
  12853. Returns a cons of the selection string MATCH and the constructed
  12854. lisp form implementing the matcher. The matcher is to be evaluated
  12855. at an Org entry, with point on the headline, and returns t if the
  12856. entry matches the selection string MATCH. The returned lisp form
  12857. references two variables with information about the entry, which
  12858. must be bound around the form's evaluation: todo, the TODO keyword
  12859. at the entry (or nil of none); and tags-list, the list of all tags
  12860. at the entry including inherited ones. Additionally, the category
  12861. of the entry (if any) must be specified as the text property
  12862. 'org-category on the headline.
  12863. See also `org-scan-tags'.
  12864. "
  12865. (declare (special todo-only))
  12866. (unless (boundp 'todo-only)
  12867. (error "`org-make-tags-matcher' expects todo-only to be scoped in"))
  12868. (unless match
  12869. ;; Get a new match request, with completion against the global
  12870. ;; tags table and the local tags in current buffer
  12871. (let ((org-last-tags-completion-table
  12872. (org-uniquify
  12873. (delq nil (append (org-get-buffer-tags)
  12874. (org-global-tags-completion-table))))))
  12875. (setq match (org-completing-read-no-i
  12876. "Match: " 'org-tags-completion-function nil nil nil
  12877. 'org-tags-history))))
  12878. ;; Parse the string and create a lisp form
  12879. (let ((match0 match)
  12880. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\(\\(?:[[:alnum:]_]+\\(?:\\\\-\\)*\\)+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@#%]+\\)"))
  12881. minus tag mm
  12882. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  12883. orterms term orlist re-p str-p level-p level-op time-p
  12884. prop-p pn pv po gv rest (start 0) (ss 0))
  12885. ;; Expand group tags
  12886. (setq match (org-tags-expand match))
  12887. ;; Check if there is a TODO part of this match, which would be the
  12888. ;; part after a "/". TO make sure that this slash is not part of
  12889. ;; a property value to be matched against, we also check that there
  12890. ;; is no " after that slash.
  12891. ;; First, find the last slash
  12892. (while (string-match "/+" match ss)
  12893. (setq start (match-beginning 0) ss (match-end 0)))
  12894. (if (and (string-match "/+" match start)
  12895. (not (save-match-data (string-match "\"" match start))))
  12896. ;; match contains also a todo-matching request
  12897. (progn
  12898. (setq tagsmatch (substring match 0 (match-beginning 0))
  12899. todomatch (substring match (match-end 0)))
  12900. (if (string-match "^!" todomatch)
  12901. (setq todo-only t todomatch (substring todomatch 1)))
  12902. (if (string-match "^\\s-*$" todomatch)
  12903. (setq todomatch nil)))
  12904. ;; only matching tags
  12905. (setq tagsmatch match todomatch nil))
  12906. ;; Make the tags matcher
  12907. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  12908. (setq tagsmatcher t)
  12909. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  12910. (while (setq term (pop orterms))
  12911. (while (and (equal (substring term -1) "\\") orterms)
  12912. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  12913. (while (string-match re term)
  12914. (setq rest (substring term (match-end 0))
  12915. minus (and (match-end 1)
  12916. (equal (match-string 1 term) "-"))
  12917. tag (save-match-data (replace-regexp-in-string
  12918. "\\\\-" "-"
  12919. (match-string 2 term)))
  12920. re-p (equal (string-to-char tag) ?{)
  12921. level-p (match-end 4)
  12922. prop-p (match-end 5)
  12923. mm (cond
  12924. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  12925. (level-p
  12926. (setq level-op (org-op-to-function (match-string 3 term)))
  12927. `(,level-op level ,(string-to-number
  12928. (match-string 4 term))))
  12929. (prop-p
  12930. (setq pn (match-string 5 term)
  12931. po (match-string 6 term)
  12932. pv (match-string 7 term)
  12933. re-p (equal (string-to-char pv) ?{)
  12934. str-p (equal (string-to-char pv) ?\")
  12935. time-p (save-match-data
  12936. (string-match "^\"[[<].*[]>]\"$" pv))
  12937. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  12938. (if time-p (setq pv (org-matcher-time pv)))
  12939. (setq po (org-op-to-function po (if time-p 'time str-p)))
  12940. (cond
  12941. ((equal pn "CATEGORY")
  12942. (setq gv '(get-text-property (point) 'org-category)))
  12943. ((equal pn "TODO")
  12944. (setq gv 'todo))
  12945. (t
  12946. (setq gv `(org-cached-entry-get nil ,pn))))
  12947. (if re-p
  12948. (if (eq po 'org<>)
  12949. `(not (string-match ,pv (or ,gv "")))
  12950. `(string-match ,pv (or ,gv "")))
  12951. (if str-p
  12952. `(,po (or ,gv "") ,pv)
  12953. `(,po (string-to-number (or ,gv ""))
  12954. ,(string-to-number pv) ))))
  12955. (t `(member ,tag tags-list)))
  12956. mm (if minus (list 'not mm) mm)
  12957. term rest)
  12958. (push mm tagsmatcher))
  12959. (push (if (> (length tagsmatcher) 1)
  12960. (cons 'and tagsmatcher)
  12961. (car tagsmatcher))
  12962. orlist)
  12963. (setq tagsmatcher nil))
  12964. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  12965. (setq tagsmatcher
  12966. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  12967. ;; Make the todo matcher
  12968. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  12969. (setq todomatcher t)
  12970. (setq orterms (org-split-string todomatch "|") orlist nil)
  12971. (while (setq term (pop orterms))
  12972. (while (string-match re term)
  12973. (setq minus (and (match-end 1)
  12974. (equal (match-string 1 term) "-"))
  12975. kwd (match-string 2 term)
  12976. re-p (equal (string-to-char kwd) ?{)
  12977. term (substring term (match-end 0))
  12978. mm (if re-p
  12979. `(string-match ,(substring kwd 1 -1) todo)
  12980. (list 'equal 'todo kwd))
  12981. mm (if minus (list 'not mm) mm))
  12982. (push mm todomatcher))
  12983. (push (if (> (length todomatcher) 1)
  12984. (cons 'and todomatcher)
  12985. (car todomatcher))
  12986. orlist)
  12987. (setq todomatcher nil))
  12988. (setq todomatcher (if (> (length orlist) 1)
  12989. (cons 'or orlist) (car orlist))))
  12990. ;; Return the string and lisp forms of the matcher
  12991. (setq matcher (if todomatcher
  12992. (list 'and tagsmatcher todomatcher)
  12993. tagsmatcher))
  12994. (when todo-only
  12995. (setq matcher (list 'and '(member todo org-not-done-keywords)
  12996. matcher)))
  12997. (cons match0 matcher)))
  12998. (defun org-tags-expand (match &optional single-as-list downcased)
  12999. "Expand group tags in MATCH.
  13000. This replaces every group tag in MATCH with a regexp tag search.
  13001. For example, a group tag \"Work\" defined as { Work : Lab Conf }
  13002. will be replaced like this:
  13003. Work => {\\(?:Work\\|Lab\\|Conf\\)}
  13004. +Work => +{\\(?:Work\\|Lab\\|Conf\\)}
  13005. -Work => -{\\(?:Work\\|Lab\\|Conf\\)}
  13006. Replacing by a regexp preserves the structure of the match.
  13007. E.g., this expansion
  13008. Work|Home => {\\(?:Work\\|Lab\\|Conf\\}|Home
  13009. will match anything tagged with \"Lab\" and \"Home\", or tagged
  13010. with \"Conf\" and \"Home\" or tagged with \"Work\" and \"home\".
  13011. When the optional argument SINGLE-AS-LIST is non-nil, MATCH is
  13012. assumed to be a single group tag, and the function will return
  13013. the list of tags in this group.
  13014. When DOWNCASE is non-nil, expand downcased TAGS."
  13015. (if org-group-tags
  13016. (let* ((case-fold-search t)
  13017. (stable org-mode-syntax-table)
  13018. (tal (or org-tag-groups-alist-for-agenda
  13019. org-tag-groups-alist))
  13020. (tal (if downcased
  13021. (mapcar (lambda(tg) (mapcar 'downcase tg)) tal) tal))
  13022. (tml (mapcar 'car tal))
  13023. (rtnmatch match) rpl)
  13024. ;; @ and _ are allowed as word-components in tags
  13025. (modify-syntax-entry ?@ "w" stable)
  13026. (modify-syntax-entry ?_ "w" stable)
  13027. (while (and tml
  13028. (with-syntax-table stable
  13029. (string-match
  13030. (concat "\\(?1:[+-]?\\)\\(?2:\\<"
  13031. (regexp-opt tml) "\\>\\)") rtnmatch)))
  13032. (let* ((dir (match-string 1 rtnmatch))
  13033. (tag (match-string 2 rtnmatch))
  13034. (tag (if downcased (downcase tag) tag)))
  13035. (setq tml (delete tag tml))
  13036. (when (not (get-text-property 0 'grouptag (match-string 2 rtnmatch)))
  13037. (setq rpl (append (org-uniquify rpl) (assoc tag tal)))
  13038. (setq rpl (concat dir "{\\<" (regexp-opt rpl) "\\>}"))
  13039. (if (stringp rpl) (org-add-props rpl '(grouptag t)))
  13040. (setq rtnmatch (replace-match rpl t t rtnmatch)))))
  13041. (if single-as-list
  13042. (or (reverse rpl) (list rtnmatch))
  13043. rtnmatch))
  13044. (if single-as-list (list (if downcased (downcase match) match))
  13045. match)))
  13046. (defun org-op-to-function (op &optional stringp)
  13047. "Turn an operator into the appropriate function."
  13048. (setq op
  13049. (cond
  13050. ((equal op "<" ) '(< string< org-time<))
  13051. ((equal op ">" ) '(> org-string> org-time>))
  13052. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  13053. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  13054. ((member op '("=" "==")) '(= string= org-time=))
  13055. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  13056. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  13057. (defun org<> (a b) (not (= a b)))
  13058. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  13059. (defun org-string>= (a b) (not (string< a b)))
  13060. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  13061. (defun org-string<> (a b) (not (string= a b)))
  13062. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  13063. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  13064. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  13065. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  13066. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  13067. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  13068. (defun org-2ft (s)
  13069. "Convert S to a floating point time.
  13070. If S is already a number, just return it. If it is a string, parse
  13071. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  13072. (cond
  13073. ((numberp s) s)
  13074. ((stringp s)
  13075. (condition-case nil
  13076. (float-time (apply 'encode-time (org-parse-time-string s)))
  13077. (error 0.)))
  13078. (t 0.)))
  13079. (defun org-time-today ()
  13080. "Time in seconds today at 0:00.
  13081. Returns the float number of seconds since the beginning of the
  13082. epoch to the beginning of today (00:00)."
  13083. (float-time (apply 'encode-time
  13084. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  13085. (defun org-matcher-time (s)
  13086. "Interpret a time comparison value."
  13087. (save-match-data
  13088. (cond
  13089. ((string= s "<now>") (float-time))
  13090. ((string= s "<today>") (org-time-today))
  13091. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  13092. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  13093. ((string-match "^<\\([-+][0-9]+\\)\\([hdwmy]\\)>$" s)
  13094. (+ (org-time-today)
  13095. (* (string-to-number (match-string 1 s))
  13096. (cdr (assoc (match-string 2 s)
  13097. '(("d" . 86400.0) ("w" . 604800.0)
  13098. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  13099. (t (org-2ft s)))))
  13100. (defun org-match-any-p (re list)
  13101. "Does re match any element of list?"
  13102. (setq list (mapcar (lambda (x) (string-match re x)) list))
  13103. (delq nil list))
  13104. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  13105. (defvar org-tags-overlay (make-overlay 1 1))
  13106. (org-detach-overlay org-tags-overlay)
  13107. (defun org-get-local-tags-at (&optional pos)
  13108. "Get a list of tags defined in the current headline."
  13109. (org-get-tags-at pos 'local))
  13110. (defun org-get-local-tags ()
  13111. "Get a list of tags defined in the current headline."
  13112. (org-get-tags-at nil 'local))
  13113. (defun org-get-tags-at (&optional pos local)
  13114. "Get a list of all headline tags applicable at POS.
  13115. POS defaults to point. If tags are inherited, the list contains
  13116. the targets in the same sequence as the headlines appear, i.e.
  13117. the tags of the current headline come last.
  13118. When LOCAL is non-nil, only return tags from the current headline,
  13119. ignore inherited ones."
  13120. (interactive)
  13121. (if (and org-trust-scanner-tags
  13122. (or (not pos) (equal pos (point)))
  13123. (not local))
  13124. org-scanner-tags
  13125. (let (tags ltags lastpos parent)
  13126. (save-excursion
  13127. (save-restriction
  13128. (widen)
  13129. (goto-char (or pos (point)))
  13130. (save-match-data
  13131. (catch 'done
  13132. (condition-case nil
  13133. (progn
  13134. (org-back-to-heading t)
  13135. (while (not (equal lastpos (point)))
  13136. (setq lastpos (point))
  13137. (when (looking-at
  13138. (org-re "[^\r\n]+?:\\([[:alnum:]_@#%:]+\\):[ \t]*$"))
  13139. (setq ltags (org-split-string
  13140. (org-match-string-no-properties 1) ":"))
  13141. (when parent
  13142. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  13143. (setq tags (append
  13144. (if parent
  13145. (org-remove-uninherited-tags ltags)
  13146. ltags)
  13147. tags)))
  13148. (or org-use-tag-inheritance (throw 'done t))
  13149. (if local (throw 'done t))
  13150. (or (org-up-heading-safe) (error nil))
  13151. (setq parent t)))
  13152. (error nil)))))
  13153. (if local
  13154. tags
  13155. (reverse (delete-dups
  13156. (reverse (append
  13157. (org-remove-uninherited-tags
  13158. org-file-tags) tags)))))))))
  13159. (defun org-add-prop-inherited (s)
  13160. (add-text-properties 0 (length s) '(inherited t) s)
  13161. s)
  13162. (defun org-toggle-tag (tag &optional onoff)
  13163. "Toggle the tag TAG for the current line.
  13164. If ONOFF is `on' or `off', don't toggle but set to this state."
  13165. (let (res current)
  13166. (save-excursion
  13167. (org-back-to-heading t)
  13168. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t]*$")
  13169. (point-at-eol) t)
  13170. (progn
  13171. (setq current (match-string 1))
  13172. (replace-match ""))
  13173. (setq current ""))
  13174. (setq current (nreverse (org-split-string current ":")))
  13175. (cond
  13176. ((eq onoff 'on)
  13177. (setq res t)
  13178. (or (member tag current) (push tag current)))
  13179. ((eq onoff 'off)
  13180. (or (not (member tag current)) (setq current (delete tag current))))
  13181. (t (if (member tag current)
  13182. (setq current (delete tag current))
  13183. (setq res t)
  13184. (push tag current))))
  13185. (end-of-line 1)
  13186. (if current
  13187. (progn
  13188. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  13189. (org-set-tags nil t))
  13190. (delete-horizontal-space))
  13191. (run-hooks 'org-after-tags-change-hook))
  13192. res))
  13193. (defun org-align-tags-here (to-col)
  13194. ;; Assumes that this is a headline
  13195. "Align tags on the current headline to TO-COL."
  13196. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  13197. (beginning-of-line 1)
  13198. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13199. (< pos (match-beginning 2)))
  13200. (progn
  13201. (setq tags-l (- (match-end 2) (match-beginning 2)))
  13202. (goto-char (match-beginning 1))
  13203. (insert " ")
  13204. (delete-region (point) (1+ (match-beginning 2)))
  13205. (setq ncol (max (current-column)
  13206. (1+ col)
  13207. (if (> to-col 0)
  13208. to-col
  13209. (- (abs to-col) tags-l))))
  13210. (setq p (point))
  13211. (insert (make-string (- ncol (current-column)) ?\ ))
  13212. (setq ncol (current-column))
  13213. (when indent-tabs-mode (tabify p (point-at-eol)))
  13214. (org-move-to-column (min ncol col)))
  13215. (goto-char pos))))
  13216. (defun org-set-tags-command (&optional arg just-align)
  13217. "Call the set-tags command for the current entry."
  13218. (interactive "P")
  13219. (if (or (org-at-heading-p) (and arg (org-before-first-heading-p)))
  13220. (org-set-tags arg just-align)
  13221. (save-excursion
  13222. (unless (and (org-region-active-p)
  13223. org-loop-over-headlines-in-active-region)
  13224. (org-back-to-heading t))
  13225. (org-set-tags arg just-align))))
  13226. (defun org-set-tags-to (data)
  13227. "Set the tags of the current entry to DATA, replacing the current tags.
  13228. DATA may be a tags string like :aa:bb:cc:, or a list of tags.
  13229. If DATA is nil or the empty string, any tags will be removed."
  13230. (interactive "sTags: ")
  13231. (setq data
  13232. (cond
  13233. ((eq data nil) "")
  13234. ((equal data "") "")
  13235. ((stringp data)
  13236. (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
  13237. ":"))
  13238. ((listp data)
  13239. (concat ":" (mapconcat 'identity data ":") ":"))))
  13240. (when data
  13241. (save-excursion
  13242. (org-back-to-heading t)
  13243. (when (looking-at org-complex-heading-regexp)
  13244. (if (match-end 5)
  13245. (progn
  13246. (goto-char (match-beginning 5))
  13247. (insert data)
  13248. (delete-region (point) (point-at-eol))
  13249. (org-set-tags nil 'align))
  13250. (goto-char (point-at-eol))
  13251. (insert " " data)
  13252. (org-set-tags nil 'align)))
  13253. (beginning-of-line 1)
  13254. (if (looking-at ".*?\\([ \t]+\\)$")
  13255. (delete-region (match-beginning 1) (match-end 1))))))
  13256. (defun org-align-all-tags ()
  13257. "Align the tags i all headings."
  13258. (interactive)
  13259. (save-excursion
  13260. (or (ignore-errors (org-back-to-heading t))
  13261. (outline-next-heading))
  13262. (if (org-at-heading-p)
  13263. (org-set-tags t)
  13264. (message "No headings"))))
  13265. (defvar org-indent-indentation-per-level)
  13266. (defun org-set-tags (&optional arg just-align)
  13267. "Set the tags for the current headline.
  13268. With prefix ARG, realign all tags in headings in the current buffer.
  13269. When JUST-ALIGN is non-nil, only align tags."
  13270. (interactive "P")
  13271. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  13272. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  13273. 'region-start-level 'region))
  13274. org-loop-over-headlines-in-active-region)
  13275. (org-map-entries
  13276. ;; We don't use ARG and JUST-ALIGN here because these args
  13277. ;; are not useful when looping over headlines.
  13278. `(org-set-tags)
  13279. org-loop-over-headlines-in-active-region
  13280. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  13281. (let* ((re org-outline-regexp-bol)
  13282. (current (unless arg (org-get-tags-string)))
  13283. (col (current-column))
  13284. (org-setting-tags t)
  13285. table current-tags inherited-tags ; computed below when needed
  13286. tags p0 c0 c1 rpl di tc level)
  13287. (if arg
  13288. (save-excursion
  13289. (goto-char (point-min))
  13290. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  13291. (while (re-search-forward re nil t)
  13292. (org-set-tags nil t)
  13293. (end-of-line 1)))
  13294. (message "All tags realigned to column %d" org-tags-column))
  13295. (if just-align
  13296. (setq tags current)
  13297. ;; Get a new set of tags from the user
  13298. (save-excursion
  13299. (setq table (append org-tag-persistent-alist
  13300. (or org-tag-alist (org-get-buffer-tags))
  13301. (and
  13302. org-complete-tags-always-offer-all-agenda-tags
  13303. (org-global-tags-completion-table
  13304. (org-agenda-files))))
  13305. org-last-tags-completion-table table
  13306. current-tags (org-split-string current ":")
  13307. inherited-tags (nreverse
  13308. (nthcdr (length current-tags)
  13309. (nreverse (org-get-tags-at))))
  13310. tags
  13311. (if (or (eq t org-use-fast-tag-selection)
  13312. (and org-use-fast-tag-selection
  13313. (delq nil (mapcar 'cdr table))))
  13314. (org-fast-tag-selection
  13315. current-tags inherited-tags table
  13316. (if org-fast-tag-selection-include-todo
  13317. org-todo-key-alist))
  13318. (let ((org-add-colon-after-tag-completion (< 1 (length table))))
  13319. (org-trim
  13320. (org-icompleting-read "Tags: "
  13321. 'org-tags-completion-function
  13322. nil nil current 'org-tags-history))))))
  13323. (while (string-match "[-+&]+" tags)
  13324. ;; No boolean logic, just a list
  13325. (setq tags (replace-match ":" t t tags))))
  13326. (setq tags (replace-regexp-in-string "[,]" ":" tags))
  13327. (if org-tags-sort-function
  13328. (setq tags (mapconcat 'identity
  13329. (sort (org-split-string
  13330. tags (org-re "[^[:alnum:]_@#%]+"))
  13331. org-tags-sort-function) ":")))
  13332. (if (string-match "\\`[\t ]*\\'" tags)
  13333. (setq tags "")
  13334. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  13335. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  13336. ;; Insert new tags at the correct column
  13337. (beginning-of-line 1)
  13338. (setq level (or (and (looking-at org-outline-regexp)
  13339. (- (match-end 0) (point) 1))
  13340. 1))
  13341. (cond
  13342. ((and (equal current "") (equal tags "")))
  13343. ((re-search-forward
  13344. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  13345. (point-at-eol) t)
  13346. (if (equal tags "")
  13347. (setq rpl "")
  13348. (goto-char (match-beginning 0))
  13349. (setq c0 (current-column)
  13350. ;; compute offset for the case of org-indent-mode active
  13351. di (if (org-bound-and-true-p org-indent-mode)
  13352. (* (1- org-indent-indentation-per-level) (1- level))
  13353. 0)
  13354. p0 (if (equal (char-before) ?*) (1+ (point)) (point))
  13355. tc (+ org-tags-column (if (> org-tags-column 0) (- di) di))
  13356. c1 (max (1+ c0) (if (> tc 0) tc (- (- tc) (length tags))))
  13357. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  13358. (replace-match rpl t t)
  13359. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  13360. tags)
  13361. (t (error "Tags alignment failed")))
  13362. (org-move-to-column col)
  13363. (unless just-align
  13364. (run-hooks 'org-after-tags-change-hook))))))
  13365. (defun org-change-tag-in-region (beg end tag off)
  13366. "Add or remove TAG for each entry in the region.
  13367. This works in the agenda, and also in an org-mode buffer."
  13368. (interactive
  13369. (list (region-beginning) (region-end)
  13370. (let ((org-last-tags-completion-table
  13371. (if (derived-mode-p 'org-mode)
  13372. (org-uniquify
  13373. (delq nil (append (org-get-buffer-tags)
  13374. (org-global-tags-completion-table))))
  13375. (org-global-tags-completion-table))))
  13376. (org-icompleting-read
  13377. "Tag: " 'org-tags-completion-function nil nil nil
  13378. 'org-tags-history))
  13379. (progn
  13380. (message "[s]et or [r]emove? ")
  13381. (equal (read-char-exclusive) ?r))))
  13382. (if (fboundp 'deactivate-mark) (deactivate-mark))
  13383. (let ((agendap (equal major-mode 'org-agenda-mode))
  13384. l1 l2 m buf pos newhead (cnt 0))
  13385. (goto-char end)
  13386. (setq l2 (1- (org-current-line)))
  13387. (goto-char beg)
  13388. (setq l1 (org-current-line))
  13389. (loop for l from l1 to l2 do
  13390. (org-goto-line l)
  13391. (setq m (get-text-property (point) 'org-hd-marker))
  13392. (when (or (and (derived-mode-p 'org-mode) (org-at-heading-p))
  13393. (and agendap m))
  13394. (setq buf (if agendap (marker-buffer m) (current-buffer))
  13395. pos (if agendap m (point)))
  13396. (with-current-buffer buf
  13397. (save-excursion
  13398. (save-restriction
  13399. (goto-char pos)
  13400. (setq cnt (1+ cnt))
  13401. (org-toggle-tag tag (if off 'off 'on))
  13402. (setq newhead (org-get-heading)))))
  13403. (and agendap (org-agenda-change-all-lines newhead m))))
  13404. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  13405. (defun org-tags-completion-function (string predicate &optional flag)
  13406. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  13407. (confirm (lambda (x) (stringp (car x)))))
  13408. (if (string-match "^\\(.*[-+:&,|]\\)\\([^-+:&,|]*\\)$" string)
  13409. (setq s1 (match-string 1 string)
  13410. s2 (match-string 2 string))
  13411. (setq s1 "" s2 string))
  13412. (cond
  13413. ((eq flag nil)
  13414. ;; try completion
  13415. (setq rtn (try-completion s2 ctable confirm))
  13416. (if (stringp rtn)
  13417. (setq rtn
  13418. (concat s1 s2 (substring rtn (length s2))
  13419. (if (and org-add-colon-after-tag-completion
  13420. (assoc rtn ctable))
  13421. ":" ""))))
  13422. rtn)
  13423. ((eq flag t)
  13424. ;; all-completions
  13425. (all-completions s2 ctable confirm))
  13426. ((eq flag 'lambda)
  13427. ;; exact match?
  13428. (assoc s2 ctable)))))
  13429. (defun org-fast-tag-insert (kwd tags face &optional end)
  13430. "Insert KDW, and the TAGS, the latter with face FACE.
  13431. Also insert END."
  13432. (insert (format "%-12s" (concat kwd ":"))
  13433. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  13434. (or end "")))
  13435. (defun org-fast-tag-show-exit (flag)
  13436. (save-excursion
  13437. (org-goto-line 3)
  13438. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  13439. (replace-match ""))
  13440. (when flag
  13441. (end-of-line 1)
  13442. (org-move-to-column (- (window-width) 19) t)
  13443. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  13444. (defun org-set-current-tags-overlay (current prefix)
  13445. "Add an overlay to CURRENT tag with PREFIX."
  13446. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  13447. (if (featurep 'xemacs)
  13448. (org-overlay-display org-tags-overlay (concat prefix s)
  13449. 'secondary-selection)
  13450. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  13451. (org-overlay-display org-tags-overlay (concat prefix s)))))
  13452. (defvar org-last-tag-selection-key nil)
  13453. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  13454. "Fast tag selection with single keys.
  13455. CURRENT is the current list of tags in the headline, INHERITED is the
  13456. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  13457. possibly with grouping information. TODO-TABLE is a similar table with
  13458. TODO keywords, should these have keys assigned to them.
  13459. If the keys are nil, a-z are automatically assigned.
  13460. Returns the new tags string, or nil to not change the current settings."
  13461. (let* ((fulltable (append table todo-table))
  13462. (maxlen (apply 'max (mapcar
  13463. (lambda (x)
  13464. (if (stringp (car x)) (string-width (car x)) 0))
  13465. fulltable)))
  13466. (buf (current-buffer))
  13467. (expert (eq org-fast-tag-selection-single-key 'expert))
  13468. (buffer-tags nil)
  13469. (fwidth (+ maxlen 3 1 3))
  13470. (ncol (/ (- (window-width) 4) fwidth))
  13471. (i-face 'org-done)
  13472. (c-face 'org-todo)
  13473. tg cnt e c char c1 c2 ntable tbl rtn
  13474. ov-start ov-end ov-prefix
  13475. (exit-after-next org-fast-tag-selection-single-key)
  13476. (done-keywords org-done-keywords)
  13477. groups ingroup)
  13478. (save-excursion
  13479. (beginning-of-line 1)
  13480. (if (looking-at
  13481. (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13482. (setq ov-start (match-beginning 1)
  13483. ov-end (match-end 1)
  13484. ov-prefix "")
  13485. (setq ov-start (1- (point-at-eol))
  13486. ov-end (1+ ov-start))
  13487. (skip-chars-forward "^\n\r")
  13488. (setq ov-prefix
  13489. (concat
  13490. (buffer-substring (1- (point)) (point))
  13491. (if (> (current-column) org-tags-column)
  13492. " "
  13493. (make-string (- org-tags-column (current-column)) ?\ ))))))
  13494. (move-overlay org-tags-overlay ov-start ov-end)
  13495. (save-window-excursion
  13496. (if expert
  13497. (set-buffer (get-buffer-create " *Org tags*"))
  13498. (delete-other-windows)
  13499. (set-window-buffer (split-window-vertically) (get-buffer-create " *Org tags*"))
  13500. (org-switch-to-buffer-other-window " *Org tags*"))
  13501. (erase-buffer)
  13502. (org-set-local 'org-done-keywords done-keywords)
  13503. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  13504. (org-fast-tag-insert "Current" current c-face "\n\n")
  13505. (org-fast-tag-show-exit exit-after-next)
  13506. (org-set-current-tags-overlay current ov-prefix)
  13507. (setq tbl fulltable char ?a cnt 0)
  13508. (while (setq e (pop tbl))
  13509. (cond
  13510. ((equal (car e) :startgroup)
  13511. (push '() groups) (setq ingroup t)
  13512. (when (not (= cnt 0))
  13513. (setq cnt 0)
  13514. (insert "\n"))
  13515. (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
  13516. ((equal (car e) :endgroup)
  13517. (setq ingroup nil cnt 0)
  13518. (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
  13519. ((equal e '(:newline))
  13520. (when (not (= cnt 0))
  13521. (setq cnt 0)
  13522. (insert "\n")
  13523. (setq e (car tbl))
  13524. (while (equal (car tbl) '(:newline))
  13525. (insert "\n")
  13526. (setq tbl (cdr tbl)))))
  13527. ((equal e '(:grouptags)) nil)
  13528. (t
  13529. (setq tg (copy-sequence (car e)) c2 nil)
  13530. (if (cdr e)
  13531. (setq c (cdr e))
  13532. ;; automatically assign a character.
  13533. (setq c1 (string-to-char
  13534. (downcase (substring
  13535. tg (if (= (string-to-char tg) ?@) 1 0)))))
  13536. (if (or (rassoc c1 ntable) (rassoc c1 table))
  13537. (while (or (rassoc char ntable) (rassoc char table))
  13538. (setq char (1+ char)))
  13539. (setq c2 c1))
  13540. (setq c (or c2 char)))
  13541. (if ingroup (push tg (car groups)))
  13542. (setq tg (org-add-props tg nil 'face
  13543. (cond
  13544. ((not (assoc tg table))
  13545. (org-get-todo-face tg))
  13546. ((member tg current) c-face)
  13547. ((member tg inherited) i-face))))
  13548. (if (equal (caar tbl) :grouptags)
  13549. (org-add-props tg nil 'face 'org-tag-group))
  13550. (if (and (= cnt 0) (not ingroup)) (insert " "))
  13551. (insert "[" c "] " tg (make-string
  13552. (- fwidth 4 (length tg)) ?\ ))
  13553. (push (cons tg c) ntable)
  13554. (when (= (setq cnt (1+ cnt)) ncol)
  13555. (insert "\n")
  13556. (if ingroup (insert " "))
  13557. (setq cnt 0)))))
  13558. (setq ntable (nreverse ntable))
  13559. (insert "\n")
  13560. (goto-char (point-min))
  13561. (if (not expert) (org-fit-window-to-buffer))
  13562. (setq rtn
  13563. (catch 'exit
  13564. (while t
  13565. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
  13566. (if (not groups) "no " "")
  13567. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  13568. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  13569. (setq org-last-tag-selection-key c)
  13570. (cond
  13571. ((= c ?\r) (throw 'exit t))
  13572. ((= c ?!)
  13573. (setq groups (not groups))
  13574. (goto-char (point-min))
  13575. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  13576. ((= c ?\C-c)
  13577. (if (not expert)
  13578. (org-fast-tag-show-exit
  13579. (setq exit-after-next (not exit-after-next)))
  13580. (setq expert nil)
  13581. (delete-other-windows)
  13582. (set-window-buffer (split-window-vertically) " *Org tags*")
  13583. (org-switch-to-buffer-other-window " *Org tags*")
  13584. (org-fit-window-to-buffer)))
  13585. ((or (= c ?\C-g)
  13586. (and (= c ?q) (not (rassoc c ntable))))
  13587. (org-detach-overlay org-tags-overlay)
  13588. (setq quit-flag t))
  13589. ((= c ?\ )
  13590. (setq current nil)
  13591. (if exit-after-next (setq exit-after-next 'now)))
  13592. ((= c ?\t)
  13593. (condition-case nil
  13594. (setq tg (org-icompleting-read
  13595. "Tag: "
  13596. (or buffer-tags
  13597. (with-current-buffer buf
  13598. (org-get-buffer-tags)))))
  13599. (quit (setq tg "")))
  13600. (when (string-match "\\S-" tg)
  13601. (add-to-list 'buffer-tags (list tg))
  13602. (if (member tg current)
  13603. (setq current (delete tg current))
  13604. (push tg current)))
  13605. (if exit-after-next (setq exit-after-next 'now)))
  13606. ((setq e (rassoc c todo-table) tg (car e))
  13607. (with-current-buffer buf
  13608. (save-excursion (org-todo tg)))
  13609. (if exit-after-next (setq exit-after-next 'now)))
  13610. ((setq e (rassoc c ntable) tg (car e))
  13611. (if (member tg current)
  13612. (setq current (delete tg current))
  13613. (loop for g in groups do
  13614. (if (member tg g)
  13615. (mapc (lambda (x)
  13616. (setq current (delete x current)))
  13617. g)))
  13618. (push tg current))
  13619. (if exit-after-next (setq exit-after-next 'now))))
  13620. ;; Create a sorted list
  13621. (setq current
  13622. (sort current
  13623. (lambda (a b)
  13624. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  13625. (if (eq exit-after-next 'now) (throw 'exit t))
  13626. (goto-char (point-min))
  13627. (beginning-of-line 2)
  13628. (delete-region (point) (point-at-eol))
  13629. (org-fast-tag-insert "Current" current c-face)
  13630. (org-set-current-tags-overlay current ov-prefix)
  13631. (while (re-search-forward
  13632. (org-re "\\[.\\] \\([[:alnum:]_@#%]+\\)") nil t)
  13633. (setq tg (match-string 1))
  13634. (add-text-properties
  13635. (match-beginning 1) (match-end 1)
  13636. (list 'face
  13637. (cond
  13638. ((member tg current) c-face)
  13639. ((member tg inherited) i-face)
  13640. (t (get-text-property (match-beginning 1) 'face))))))
  13641. (goto-char (point-min)))))
  13642. (org-detach-overlay org-tags-overlay)
  13643. (if rtn
  13644. (mapconcat 'identity current ":")
  13645. nil))))
  13646. (defun org-get-tags-string ()
  13647. "Get the TAGS string in the current headline."
  13648. (unless (org-at-heading-p t)
  13649. (user-error "Not on a heading"))
  13650. (save-excursion
  13651. (beginning-of-line 1)
  13652. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13653. (org-match-string-no-properties 1)
  13654. "")))
  13655. (defun org-get-tags ()
  13656. "Get the list of tags specified in the current headline."
  13657. (org-split-string (org-get-tags-string) ":"))
  13658. (defun org-get-buffer-tags ()
  13659. "Get a table of all tags used in the buffer, for completion."
  13660. (let (tags)
  13661. (save-excursion
  13662. (goto-char (point-min))
  13663. (while (re-search-forward
  13664. (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t\r\n]") nil t)
  13665. (when (equal (char-after (point-at-bol 0)) ?*)
  13666. (mapc (lambda (x) (add-to-list 'tags x))
  13667. (org-split-string (org-match-string-no-properties 1) ":")))))
  13668. (mapc (lambda (s) (add-to-list 'tags s)) org-file-tags)
  13669. (mapcar 'list tags)))
  13670. ;;;; The mapping API
  13671. (defun org-map-entries (func &optional match scope &rest skip)
  13672. "Call FUNC at each headline selected by MATCH in SCOPE.
  13673. FUNC is a function or a lisp form. The function will be called without
  13674. arguments, with the cursor positioned at the beginning of the headline.
  13675. The return values of all calls to the function will be collected and
  13676. returned as a list.
  13677. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  13678. does not need to preserve point. After evaluation, the cursor will be
  13679. moved to the end of the line (presumably of the headline of the
  13680. processed entry) and search continues from there. Under some
  13681. circumstances, this may not produce the wanted results. For example,
  13682. if you have removed (e.g. archived) the current (sub)tree it could
  13683. mean that the next entry will be skipped entirely. In such cases, you
  13684. can specify the position from where search should continue by making
  13685. FUNC set the variable `org-map-continue-from' to the desired buffer
  13686. position.
  13687. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  13688. Only headlines that are matched by this query will be considered during
  13689. the iteration. When MATCH is nil or t, all headlines will be
  13690. visited by the iteration.
  13691. SCOPE determines the scope of this command. It can be any of:
  13692. nil The current buffer, respecting the restriction if any
  13693. tree The subtree started with the entry at point
  13694. region The entries within the active region, if any
  13695. region-start-level
  13696. The entries within the active region, but only those at
  13697. the same level than the first one.
  13698. file The current buffer, without restriction
  13699. file-with-archives
  13700. The current buffer, and any archives associated with it
  13701. agenda All agenda files
  13702. agenda-with-archives
  13703. All agenda files with any archive files associated with them
  13704. \(file1 file2 ...)
  13705. If this is a list, all files in the list will be scanned
  13706. The remaining args are treated as settings for the skipping facilities of
  13707. the scanner. The following items can be given here:
  13708. archive skip trees with the archive tag
  13709. comment skip trees with the COMMENT keyword
  13710. function or Emacs Lisp form:
  13711. will be used as value for `org-agenda-skip-function', so
  13712. whenever the function returns a position, FUNC will not be
  13713. called for that entry and search will continue from the
  13714. position returned
  13715. If your function needs to retrieve the tags including inherited tags
  13716. at the *current* entry, you can use the value of the variable
  13717. `org-scanner-tags' which will be much faster than getting the value
  13718. with `org-get-tags-at'. If your function gets properties with
  13719. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  13720. to t around the call to `org-entry-properties' to get the same speedup.
  13721. Note that if your function moves around to retrieve tags and properties at
  13722. a *different* entry, you cannot use these techniques."
  13723. (unless (and (or (eq scope 'region) (eq scope 'region-start-level))
  13724. (not (org-region-active-p)))
  13725. (let* ((org-agenda-archives-mode nil) ; just to make sure
  13726. (org-agenda-skip-archived-trees (memq 'archive skip))
  13727. (org-agenda-skip-comment-trees (memq 'comment skip))
  13728. (org-agenda-skip-function
  13729. (car (org-delete-all '(comment archive) skip)))
  13730. (org-tags-match-list-sublevels t)
  13731. (start-level (eq scope 'region-start-level))
  13732. matcher file res
  13733. org-todo-keywords-for-agenda
  13734. org-done-keywords-for-agenda
  13735. org-todo-keyword-alist-for-agenda
  13736. org-drawers-for-agenda
  13737. org-tag-alist-for-agenda
  13738. todo-only)
  13739. (cond
  13740. ((eq match t) (setq matcher t))
  13741. ((eq match nil) (setq matcher t))
  13742. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  13743. (save-excursion
  13744. (save-restriction
  13745. (cond ((eq scope 'tree)
  13746. (org-back-to-heading t)
  13747. (org-narrow-to-subtree)
  13748. (setq scope nil))
  13749. ((and (or (eq scope 'region) (eq scope 'region-start-level))
  13750. (org-region-active-p))
  13751. ;; If needed, set start-level to a string like "2"
  13752. (when start-level
  13753. (save-excursion
  13754. (goto-char (region-beginning))
  13755. (unless (org-at-heading-p) (outline-next-heading))
  13756. (setq start-level (org-current-level))))
  13757. (narrow-to-region (region-beginning)
  13758. (save-excursion
  13759. (goto-char (region-end))
  13760. (unless (and (bolp) (org-at-heading-p))
  13761. (outline-next-heading))
  13762. (point)))
  13763. (setq scope nil)))
  13764. (if (not scope)
  13765. (progn
  13766. (org-agenda-prepare-buffers
  13767. (list (buffer-file-name (current-buffer))))
  13768. (setq res (org-scan-tags func matcher todo-only start-level)))
  13769. ;; Get the right scope
  13770. (cond
  13771. ((and scope (listp scope) (symbolp (car scope)))
  13772. (setq scope (eval scope)))
  13773. ((eq scope 'agenda)
  13774. (setq scope (org-agenda-files t)))
  13775. ((eq scope 'agenda-with-archives)
  13776. (setq scope (org-agenda-files t))
  13777. (setq scope (org-add-archive-files scope)))
  13778. ((eq scope 'file)
  13779. (setq scope (list (buffer-file-name))))
  13780. ((eq scope 'file-with-archives)
  13781. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  13782. (org-agenda-prepare-buffers scope)
  13783. (while (setq file (pop scope))
  13784. (with-current-buffer (org-find-base-buffer-visiting file)
  13785. (save-excursion
  13786. (save-restriction
  13787. (widen)
  13788. (goto-char (point-min))
  13789. (setq res (append res (org-scan-tags func matcher todo-only))))))))))
  13790. res)))
  13791. ;;;; Properties
  13792. ;;; Setting and retrieving properties
  13793. (defconst org-special-properties
  13794. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  13795. "TIMESTAMP" "TIMESTAMP_IA" "BLOCKED" "FILE" "CLOCKSUM" "CLOCKSUM_T")
  13796. "The special properties valid in Org-mode.
  13797. These are properties that are not defined in the property drawer,
  13798. but in some other way.")
  13799. (defconst org-default-properties
  13800. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  13801. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  13802. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  13803. "EXPORT_OPTIONS" "EXPORT_TEXT" "EXPORT_FILE_NAME"
  13804. "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
  13805. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER" "REPEAT_TO_STATE"
  13806. "CLOCK_MODELINE_TOTAL" "STYLE" "HTML_CONTAINER_CLASS")
  13807. "Some properties that are used by Org-mode for various purposes.
  13808. Being in this list makes sure that they are offered for completion.")
  13809. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  13810. "Regular expression matching the first line of a property drawer.")
  13811. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  13812. "Regular expression matching the last line of a property drawer.")
  13813. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  13814. "Regular expression matching the first line of a property drawer.")
  13815. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  13816. "Regular expression matching the first line of a property drawer.")
  13817. (defconst org-property-drawer-re
  13818. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  13819. org-property-end-re "\\)\n?")
  13820. "Matches an entire property drawer.")
  13821. (defconst org-clock-drawer-re
  13822. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  13823. org-property-end-re "\\)\n?")
  13824. "Matches an entire clock drawer.")
  13825. (defun org-property-action ()
  13826. "Do an action on properties."
  13827. (interactive)
  13828. (let (c)
  13829. (org-at-property-p)
  13830. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  13831. (setq c (read-char-exclusive))
  13832. (cond
  13833. ((equal c ?s)
  13834. (call-interactively 'org-set-property))
  13835. ((equal c ?d)
  13836. (call-interactively 'org-delete-property))
  13837. ((equal c ?D)
  13838. (call-interactively 'org-delete-property-globally))
  13839. ((equal c ?c)
  13840. (call-interactively 'org-compute-property-at-point))
  13841. (t (user-error "No such property action %c" c)))))
  13842. (defun org-inc-effort ()
  13843. "Increment the value of the effort property in the current entry."
  13844. (interactive)
  13845. (org-set-effort nil t))
  13846. (defvar org-clock-effort) ;; Defined in org-clock.el
  13847. (defvar org-clock-current-task) ;; Defined in org-clock.el
  13848. (defun org-set-effort (&optional value increment)
  13849. "Set the effort property of the current entry.
  13850. With numerical prefix arg, use the nth allowed value, 0 stands for the
  13851. 10th allowed value.
  13852. When INCREMENT is non-nil, set the property to the next allowed value."
  13853. (interactive "P")
  13854. (if (equal value 0) (setq value 10))
  13855. (let* ((completion-ignore-case t)
  13856. (prop org-effort-property)
  13857. (cur (org-entry-get nil prop))
  13858. (allowed (org-property-get-allowed-values nil prop 'table))
  13859. (existing (mapcar 'list (org-property-values prop)))
  13860. (heading (nth 4 (org-heading-components)))
  13861. rpl
  13862. (val (cond
  13863. ((stringp value) value)
  13864. ((and allowed (integerp value))
  13865. (or (car (nth (1- value) allowed))
  13866. (car (org-last allowed))))
  13867. ((and allowed increment)
  13868. (or (caadr (member (list cur) allowed))
  13869. (user-error "Allowed effort values are not set")))
  13870. (allowed
  13871. (message "Select 1-9,0, [RET%s]: %s"
  13872. (if cur (concat "=" cur) "")
  13873. (mapconcat 'car allowed " "))
  13874. (setq rpl (read-char-exclusive))
  13875. (if (equal rpl ?\r)
  13876. cur
  13877. (setq rpl (- rpl ?0))
  13878. (if (equal rpl 0) (setq rpl 10))
  13879. (if (and (> rpl 0) (<= rpl (length allowed)))
  13880. (car (nth (1- rpl) allowed))
  13881. (org-completing-read "Effort: " allowed nil))))
  13882. (t
  13883. (let (org-completion-use-ido org-completion-use-iswitchb)
  13884. (org-completing-read
  13885. (concat "Effort " (if (and cur (string-match "\\S-" cur))
  13886. (concat "[" cur "]") "")
  13887. ": ")
  13888. existing nil nil "" nil cur))))))
  13889. (unless (equal (org-entry-get nil prop) val)
  13890. (org-entry-put nil prop val))
  13891. (save-excursion
  13892. (org-back-to-heading t)
  13893. (put-text-property (point-at-bol) (point-at-eol) 'org-effort val))
  13894. (when (string= heading org-clock-current-task)
  13895. (setq org-clock-effort (get-text-property (point-at-bol) 'org-effort))
  13896. (org-clock-update-mode-line))
  13897. (message "%s is now %s" prop val)))
  13898. (defun org-at-property-p ()
  13899. "Is cursor inside a property drawer?"
  13900. (save-excursion
  13901. (when (equal 'node-property (car (org-element-at-point)))
  13902. (beginning-of-line 1)
  13903. (looking-at org-property-re))))
  13904. (defun org-get-property-block (&optional beg end force)
  13905. "Return the (beg . end) range of the body of the property drawer.
  13906. BEG and END are the beginning and end of the current subtree, or of
  13907. the part before the first headline. If they are not given, they will
  13908. be found. If the drawer does not exist and FORCE is non-nil, create
  13909. the drawer."
  13910. (catch 'exit
  13911. (save-excursion
  13912. (let* ((beg (or beg (and (org-before-first-heading-p) (point-min))
  13913. (progn (org-back-to-heading t) (point))))
  13914. (end (or end (and (not (outline-next-heading)) (point-max))
  13915. (point))))
  13916. (goto-char beg)
  13917. (if (re-search-forward org-property-start-re end t)
  13918. (setq beg (1+ (match-end 0)))
  13919. (if force
  13920. (save-excursion
  13921. (org-insert-property-drawer)
  13922. (setq end (progn (outline-next-heading) (point))))
  13923. (throw 'exit nil))
  13924. (goto-char beg)
  13925. (if (re-search-forward org-property-start-re end t)
  13926. (setq beg (1+ (match-end 0)))))
  13927. (if (re-search-forward org-property-end-re end t)
  13928. (setq end (match-beginning 0))
  13929. (or force (throw 'exit nil))
  13930. (goto-char beg)
  13931. (setq end beg)
  13932. (org-indent-line)
  13933. (insert ":END:\n"))
  13934. (cons beg end)))))
  13935. (defun org-entry-properties (&optional pom which specific)
  13936. "Get all properties of the entry at point-or-marker POM.
  13937. This includes the TODO keyword, the tags, time strings for deadline,
  13938. scheduled, and clocking, and any additional properties defined in the
  13939. entry. The return value is an alist, keys may occur multiple times
  13940. if the property key was used several times.
  13941. POM may also be nil, in which case the current entry is used.
  13942. If WHICH is nil or `all', get all properties. If WHICH is
  13943. `special' or `standard', only get that subclass. If WHICH
  13944. is a string only get exactly this property. SPECIFIC can be a string, the
  13945. specific property we are interested in. Specifying it can speed
  13946. things up because then unnecessary parsing is avoided."
  13947. (setq which (or which 'all))
  13948. (org-with-wide-buffer
  13949. (org-with-point-at pom
  13950. (let ((clockstr (substring org-clock-string 0 -1))
  13951. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY" "BLOCKED"))
  13952. (case-fold-search nil)
  13953. beg end range props sum-props key key1 value string clocksum clocksumt)
  13954. (when (and (derived-mode-p 'org-mode)
  13955. (ignore-errors (org-back-to-heading t)))
  13956. (setq beg (point))
  13957. (setq sum-props (get-text-property (point) 'org-summaries))
  13958. (setq clocksum (get-text-property (point) :org-clock-minutes)
  13959. clocksumt (get-text-property (point) :org-clock-minutes-today))
  13960. (outline-next-heading)
  13961. (setq end (point))
  13962. (when (memq which '(all special))
  13963. ;; Get the special properties, like TODO and tags
  13964. (goto-char beg)
  13965. (when (and (or (not specific) (string= specific "TODO"))
  13966. (looking-at org-todo-line-regexp) (match-end 2))
  13967. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  13968. (when (and (or (not specific) (string= specific "PRIORITY"))
  13969. (looking-at org-priority-regexp))
  13970. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  13971. (when (or (not specific) (string= specific "FILE"))
  13972. (push (cons "FILE" buffer-file-name) props))
  13973. (when (and (or (not specific) (string= specific "TAGS"))
  13974. (setq value (org-get-tags-string))
  13975. (string-match "\\S-" value))
  13976. (push (cons "TAGS" value) props))
  13977. (when (and (or (not specific) (string= specific "ALLTAGS"))
  13978. (setq value (org-get-tags-at)))
  13979. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":")
  13980. ":"))
  13981. props))
  13982. (when (or (not specific) (string= specific "BLOCKED"))
  13983. (push (cons "BLOCKED" (if (org-entry-blocked-p) "t" "")) props))
  13984. (when (or (not specific)
  13985. (member specific
  13986. '("SCHEDULED" "DEADLINE" "CLOCK" "CLOSED"
  13987. "TIMESTAMP" "TIMESTAMP_IA")))
  13988. (catch 'match
  13989. (while (and (re-search-forward org-maybe-keyword-time-regexp end t)
  13990. (not (text-property-any 0 (length (match-string 0))
  13991. 'face 'font-lock-comment-face
  13992. (match-string 0))))
  13993. (setq key (if (match-end 1)
  13994. (substring (org-match-string-no-properties 1)
  13995. 0 -1))
  13996. string (if (equal key clockstr)
  13997. (org-trim
  13998. (buffer-substring-no-properties
  13999. (match-beginning 3) (goto-char
  14000. (point-at-eol))))
  14001. (substring (org-match-string-no-properties 3)
  14002. 1 -1)))
  14003. ;; Get the correct property name from the key. This is
  14004. ;; necessary if the user has configured time keywords.
  14005. (setq key1 (concat key ":"))
  14006. (cond
  14007. ((not key)
  14008. (setq key
  14009. (if (= (char-after (match-beginning 3)) ?\[)
  14010. "TIMESTAMP_IA" "TIMESTAMP")))
  14011. ((equal key1 org-scheduled-string) (setq key "SCHEDULED"))
  14012. ((equal key1 org-deadline-string) (setq key "DEADLINE"))
  14013. ((equal key1 org-closed-string) (setq key "CLOSED"))
  14014. ((equal key1 org-clock-string) (setq key "CLOCK")))
  14015. (if (and specific (equal key specific) (not (equal key "CLOCK")))
  14016. (progn
  14017. (push (cons key string) props)
  14018. ;; no need to search further if match is found
  14019. (throw 'match t))
  14020. (when (or (equal key "CLOCK") (not (assoc key props)))
  14021. (push (cons key string) props)))))))
  14022. (when (memq which '(all standard))
  14023. ;; Get the standard properties, like :PROP: ...
  14024. (setq range (org-get-property-block beg end))
  14025. (when range
  14026. (goto-char (car range))
  14027. (while (re-search-forward org-property-re
  14028. (cdr range) t)
  14029. (setq key (org-match-string-no-properties 2)
  14030. value (org-trim (or (org-match-string-no-properties 3) "")))
  14031. (unless (member key excluded)
  14032. (push (cons key (or value "")) props)))))
  14033. (if clocksum
  14034. (push (cons "CLOCKSUM"
  14035. (org-columns-number-to-string (/ (float clocksum) 60.)
  14036. 'add_times))
  14037. props))
  14038. (if clocksumt
  14039. (push (cons "CLOCKSUM_T"
  14040. (org-columns-number-to-string (/ (float clocksumt) 60.)
  14041. 'add_times))
  14042. props))
  14043. (unless (assoc "CATEGORY" props)
  14044. (push (cons "CATEGORY" (org-get-category)) props))
  14045. (append sum-props (nreverse props)))))))
  14046. (defun org-entry-get (pom property &optional inherit literal-nil)
  14047. "Get value of PROPERTY for entry or content at point-or-marker POM.
  14048. If INHERIT is non-nil and the entry does not have the property,
  14049. then also check higher levels of the hierarchy.
  14050. If INHERIT is the symbol `selective', use inheritance only if the setting
  14051. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  14052. If the property is present but empty, the return value is the empty string.
  14053. If the property is not present at all, nil is returned.
  14054. Return the value as a string.
  14055. If LITERAL-NIL is set, return the string value \"nil\" as a string,
  14056. do not interpret it as the list atom nil. This is used for inheritance
  14057. when a \"nil\" value can supersede a non-nil value higher up the hierarchy."
  14058. (org-with-point-at pom
  14059. (if (and inherit (if (eq inherit 'selective)
  14060. (org-property-inherit-p property)
  14061. t))
  14062. (org-entry-get-with-inheritance property literal-nil)
  14063. (if (member property org-special-properties)
  14064. ;; We need a special property. Use `org-entry-properties'
  14065. ;; to retrieve it, but specify the wanted property
  14066. (cdr (assoc property (org-entry-properties nil 'special property)))
  14067. (org-with-wide-buffer
  14068. (let ((range (org-get-property-block)))
  14069. (when (and range (not (eq (car range) (cdr range)))
  14070. (save-excursion
  14071. (goto-char (car range))
  14072. (re-search-forward
  14073. (concat (org-re-property property) "\\|"
  14074. (org-re-property (concat property "+")))
  14075. (cdr range) t)))
  14076. (let* ((props
  14077. (list (or (assoc property org-file-properties)
  14078. (assoc property org-global-properties)
  14079. (assoc property org-global-properties-fixed))))
  14080. (ap (lambda (key)
  14081. (when (re-search-forward
  14082. (org-re-property key) (cdr range) t)
  14083. (setq props
  14084. (org-update-property-plist
  14085. key
  14086. (if (match-end 3)
  14087. (org-match-string-no-properties 3) "")
  14088. props)))))
  14089. val)
  14090. (goto-char (car range))
  14091. (funcall ap property)
  14092. (goto-char (car range))
  14093. (while (funcall ap (concat property "+")))
  14094. (setq val (cdr (assoc property props)))
  14095. (when val (if literal-nil val (org-not-nil val)))))))))))
  14096. (defun org-property-or-variable-value (var &optional inherit)
  14097. "Check if there is a property fixing the value of VAR.
  14098. If yes, return this value. If not, return the current value of the variable."
  14099. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  14100. (if (and prop (stringp prop) (string-match "\\S-" prop))
  14101. (read prop)
  14102. (symbol-value var))))
  14103. (defun org-entry-delete (pom property &optional delete-empty-drawer)
  14104. "Delete the property PROPERTY from entry at point-or-marker POM.
  14105. When optional argument DELETE-EMPTY-DRAWER is a string, it defines
  14106. an empty drawer to delete."
  14107. (org-with-point-at pom
  14108. (if (member property org-special-properties)
  14109. nil ; cannot delete these properties.
  14110. (let ((range (org-get-property-block)))
  14111. (if (and range
  14112. (goto-char (car range))
  14113. (re-search-forward
  14114. (org-re-property property nil t)
  14115. (cdr range) t))
  14116. (progn
  14117. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  14118. (and delete-empty-drawer
  14119. (org-remove-empty-drawer-at
  14120. delete-empty-drawer (car range)))
  14121. t)
  14122. nil)))))
  14123. ;; Multi-values properties are properties that contain multiple values
  14124. ;; These values are assumed to be single words, separated by whitespace.
  14125. (defun org-entry-add-to-multivalued-property (pom property value)
  14126. "Add VALUE to the 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. (unless (member value values)
  14131. (setq values (append values (list value)))
  14132. (org-entry-put pom property
  14133. (mapconcat 'identity values " ")))))
  14134. (defun org-entry-remove-from-multivalued-property (pom property value)
  14135. "Remove VALUE from 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. (when (member value values)
  14140. (setq values (delete value values))
  14141. (org-entry-put pom property
  14142. (mapconcat 'identity values " ")))))
  14143. (defun org-entry-member-in-multivalued-property (pom property value)
  14144. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  14145. (let* ((old (org-entry-get pom property))
  14146. (values (and old (org-split-string old "[ \t]"))))
  14147. (setq value (org-entry-protect-space value))
  14148. (member value values)))
  14149. (defun org-entry-get-multivalued-property (pom property)
  14150. "Return a list of values in a multivalued property."
  14151. (let* ((value (org-entry-get pom property))
  14152. (values (and value (org-split-string value "[ \t]"))))
  14153. (mapcar 'org-entry-restore-space values)))
  14154. (defun org-entry-put-multivalued-property (pom property &rest values)
  14155. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  14156. VALUES should be a list of strings. Spaces will be protected."
  14157. (org-entry-put pom property
  14158. (mapconcat 'org-entry-protect-space values " "))
  14159. (let* ((value (org-entry-get pom property))
  14160. (values (and value (org-split-string value "[ \t]"))))
  14161. (mapcar 'org-entry-restore-space values)))
  14162. (defun org-entry-protect-space (s)
  14163. "Protect spaces and newline in string S."
  14164. (while (string-match " " s)
  14165. (setq s (replace-match "%20" t t s)))
  14166. (while (string-match "\n" s)
  14167. (setq s (replace-match "%0A" t t s)))
  14168. s)
  14169. (defun org-entry-restore-space (s)
  14170. "Restore spaces and newline in string S."
  14171. (while (string-match "%20" s)
  14172. (setq s (replace-match " " t t s)))
  14173. (while (string-match "%0A" s)
  14174. (setq s (replace-match "\n" t t s)))
  14175. s)
  14176. (defvar org-entry-property-inherited-from (make-marker)
  14177. "Marker pointing to the entry from where a property was inherited.
  14178. Each call to `org-entry-get-with-inheritance' will set this marker to the
  14179. location of the entry where the inheritance search matched. If there was
  14180. no match, the marker will point nowhere.
  14181. Note that also `org-entry-get' calls this function, if the INHERIT flag
  14182. is set.")
  14183. (defun org-entry-get-with-inheritance (property &optional literal-nil)
  14184. "Get PROPERTY of entry or content at point, search higher levels if needed.
  14185. The search will stop at the first ancestor which has the property defined.
  14186. If the value found is \"nil\", return nil to show that the property
  14187. should be considered as undefined (this is the meaning of nil here).
  14188. However, if LITERAL-NIL is set, return the string value \"nil\" instead."
  14189. (move-marker org-entry-property-inherited-from nil)
  14190. (let (tmp)
  14191. (save-excursion
  14192. (save-restriction
  14193. (widen)
  14194. (catch 'ex
  14195. (while t
  14196. (when (setq tmp (org-entry-get nil property nil literal-nil))
  14197. (or (ignore-errors (org-back-to-heading t))
  14198. (goto-char (point-min)))
  14199. (move-marker org-entry-property-inherited-from (point))
  14200. (throw 'ex tmp))
  14201. (or (ignore-errors (org-up-heading-safe))
  14202. (throw 'ex nil))))))
  14203. (setq tmp (or tmp
  14204. (cdr (assoc property org-file-properties))
  14205. (cdr (assoc property org-global-properties))
  14206. (cdr (assoc property org-global-properties-fixed))))
  14207. (if literal-nil tmp (org-not-nil tmp))))
  14208. (defvar org-property-changed-functions nil
  14209. "Hook called when the value of a property has changed.
  14210. Each hook function should accept two arguments, the name of the property
  14211. and the new value.")
  14212. (defun org-entry-put (pom property value)
  14213. "Set PROPERTY to VALUE for entry at point-or-marker POM.
  14214. If the value is `nil', it is converted to the empty string.
  14215. If it is not a string, an error is raised."
  14216. (cond ((null value) (setq value ""))
  14217. ((not (stringp value))
  14218. (error "Properties values should be strings.")))
  14219. (org-with-point-at pom
  14220. (org-back-to-heading t)
  14221. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  14222. range)
  14223. (cond
  14224. ((equal property "TODO")
  14225. (when (and (string-match "\\S-" value)
  14226. (not (member value org-todo-keywords-1)))
  14227. (user-error "\"%s\" is not a valid TODO state" value))
  14228. (if (or (not value)
  14229. (not (string-match "\\S-" value)))
  14230. (setq value 'none))
  14231. (org-todo value)
  14232. (org-set-tags nil 'align))
  14233. ((equal property "PRIORITY")
  14234. (org-priority (if (and value (string-match "\\S-" value))
  14235. (string-to-char value) ?\ ))
  14236. (org-set-tags nil 'align))
  14237. ((equal property "CLOCKSUM")
  14238. (if (not (re-search-forward
  14239. (concat org-clock-string ".*\\]--\\(\\[[^]]+\\]\\)") nil t))
  14240. (error "Cannot find a clock log")
  14241. (goto-char (- (match-end 1) 2))
  14242. (cond
  14243. ((eq value 'earlier) (org-timestamp-down))
  14244. ((eq value 'later) (org-timestamp-up)))
  14245. (org-clock-sum-current-item)))
  14246. ((equal property "SCHEDULED")
  14247. (if (re-search-forward org-scheduled-time-regexp end t)
  14248. (cond
  14249. ((eq value 'earlier) (org-timestamp-change -1 'day))
  14250. ((eq value 'later) (org-timestamp-change 1 'day))
  14251. (t (call-interactively 'org-schedule)))
  14252. (call-interactively 'org-schedule)))
  14253. ((equal property "DEADLINE")
  14254. (if (re-search-forward org-deadline-time-regexp end t)
  14255. (cond
  14256. ((eq value 'earlier) (org-timestamp-change -1 'day))
  14257. ((eq value 'later) (org-timestamp-change 1 'day))
  14258. (t (call-interactively 'org-deadline)))
  14259. (call-interactively 'org-deadline)))
  14260. ((member property org-special-properties)
  14261. (error "The %s property can not yet be set with `org-entry-put'"
  14262. property))
  14263. (t ; a non-special property
  14264. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  14265. (setq range (org-get-property-block beg end 'force))
  14266. (goto-char (car range))
  14267. (if (re-search-forward
  14268. (org-re-property property nil t) (cdr range) t)
  14269. (progn
  14270. (delete-region (match-beginning 0) (match-end 0))
  14271. (goto-char (match-beginning 0)))
  14272. (goto-char (cdr range))
  14273. (insert "\n")
  14274. (backward-char 1)
  14275. (org-indent-line))
  14276. (insert ":" property ":")
  14277. (and value (insert " " value))
  14278. (org-indent-line)))))
  14279. (run-hook-with-args 'org-property-changed-functions property value)))
  14280. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  14281. "Get all property keys in the current buffer.
  14282. With INCLUDE-SPECIALS, also list the special properties that reflect things
  14283. like tags and TODO state.
  14284. With INCLUDE-DEFAULTS, also include properties that has special meaning
  14285. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING
  14286. and others.
  14287. With INCLUDE-COLUMNS, also include property names given in COLUMN
  14288. formats in the current buffer."
  14289. (let (rtn range cfmt s p)
  14290. (save-excursion
  14291. (save-restriction
  14292. (widen)
  14293. (goto-char (point-min))
  14294. (while (re-search-forward org-property-start-re nil t)
  14295. (setq range (org-get-property-block))
  14296. (goto-char (car range))
  14297. (while (re-search-forward org-property-re
  14298. (cdr range) t)
  14299. (add-to-list 'rtn (org-match-string-no-properties 2)))
  14300. (outline-next-heading))))
  14301. (when include-specials
  14302. (setq rtn (append org-special-properties rtn)))
  14303. (when include-defaults
  14304. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties)
  14305. (add-to-list 'rtn org-effort-property))
  14306. (when include-columns
  14307. (save-excursion
  14308. (save-restriction
  14309. (widen)
  14310. (goto-char (point-min))
  14311. (while (re-search-forward
  14312. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  14313. nil t)
  14314. (setq cfmt (match-string 2) s 0)
  14315. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  14316. cfmt s)
  14317. (setq s (match-end 0)
  14318. p (match-string 1 cfmt))
  14319. (unless (or (equal p "ITEM")
  14320. (member p org-special-properties))
  14321. (add-to-list 'rtn (match-string 1 cfmt))))))))
  14322. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  14323. (defun org-property-values (key)
  14324. "Return a list of all values of property KEY in the current buffer."
  14325. (save-excursion
  14326. (save-restriction
  14327. (widen)
  14328. (goto-char (point-min))
  14329. (let ((re (org-re-property key))
  14330. values)
  14331. (while (re-search-forward re nil t)
  14332. (add-to-list 'values (org-trim (match-string 3))))
  14333. (delete "" values)))))
  14334. (defun org-insert-property-drawer ()
  14335. "Insert a property drawer into the current entry."
  14336. (org-back-to-heading t)
  14337. (looking-at org-outline-regexp)
  14338. (let ((indent (if org-adapt-indentation
  14339. (- (match-end 0) (match-beginning 0))
  14340. 0))
  14341. (beg (point))
  14342. (re (concat "^[ \t]*" org-keyword-time-regexp))
  14343. end hiddenp)
  14344. (outline-next-heading)
  14345. (setq end (point))
  14346. (goto-char beg)
  14347. (while (re-search-forward re end t))
  14348. (setq hiddenp (outline-invisible-p))
  14349. (end-of-line 1)
  14350. (and (equal (char-after) ?\n) (forward-char 1))
  14351. (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
  14352. (if (member (match-string 1) '("CLOCK:" ":END:"))
  14353. ;; just skip this line
  14354. (beginning-of-line 2)
  14355. ;; Drawer start, find the end
  14356. (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
  14357. (beginning-of-line 1)))
  14358. (org-skip-over-state-notes)
  14359. (skip-chars-backward " \t\n\r")
  14360. (if (and (eq (char-before) ?*) (not (eq (char-after) ?\n)))
  14361. (forward-char 1))
  14362. (goto-char (point-at-eol))
  14363. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  14364. (beginning-of-line 0)
  14365. (org-indent-to-column indent)
  14366. (beginning-of-line 2)
  14367. (org-indent-to-column indent)
  14368. (beginning-of-line 0)
  14369. (if hiddenp
  14370. (save-excursion
  14371. (org-back-to-heading t)
  14372. (hide-entry))
  14373. (org-flag-drawer t))))
  14374. (defun org-insert-drawer (&optional arg drawer)
  14375. "Insert a drawer at point.
  14376. Optional argument DRAWER, when non-nil, is a string representing
  14377. drawer's name. Otherwise, the user is prompted for a name.
  14378. If a region is active, insert the drawer around that region
  14379. instead.
  14380. Point is left between drawer's boundaries."
  14381. (interactive "P")
  14382. (let* ((logbook (if (stringp org-log-into-drawer) org-log-into-drawer
  14383. "LOGBOOK"))
  14384. ;; SYSTEM-DRAWERS is a list of drawer names that are used
  14385. ;; internally by Org. They are meant to be inserted
  14386. ;; automatically.
  14387. (system-drawers `("CLOCK" ,logbook "PROPERTIES"))
  14388. ;; Remove system drawers from list. Note: For some reason,
  14389. ;; `org-completing-read' ignores the predicate while
  14390. ;; `completing-read' handles it fine.
  14391. (drawer (if arg "PROPERTIES"
  14392. (or drawer
  14393. (completing-read
  14394. "Drawer: " org-drawers
  14395. (lambda (d) (not (member d system-drawers))))))))
  14396. (cond
  14397. ;; With C-u, fall back on `org-insert-property-drawer'
  14398. (arg (org-insert-property-drawer))
  14399. ;; With an active region, insert a drawer at point.
  14400. ((not (org-region-active-p))
  14401. (progn
  14402. (unless (bolp) (insert "\n"))
  14403. (insert (format ":%s:\n\n:END:\n" drawer))
  14404. (forward-line -2)))
  14405. ;; Otherwise, insert the drawer at point
  14406. (t
  14407. (let ((rbeg (region-beginning))
  14408. (rend (copy-marker (region-end))))
  14409. (unwind-protect
  14410. (progn
  14411. (goto-char rbeg)
  14412. (beginning-of-line)
  14413. (when (save-excursion
  14414. (re-search-forward org-outline-regexp-bol rend t))
  14415. (user-error "Drawers cannot contain headlines"))
  14416. ;; Position point at the beginning of the first
  14417. ;; non-blank line in region. Insert drawer's opening
  14418. ;; there, then indent it.
  14419. (org-skip-whitespace)
  14420. (beginning-of-line)
  14421. (insert ":" drawer ":\n")
  14422. (forward-line -1)
  14423. (indent-for-tab-command)
  14424. ;; Move point to the beginning of the first blank line
  14425. ;; after the last non-blank line in region. Insert
  14426. ;; drawer's closing, then indent it.
  14427. (goto-char rend)
  14428. (skip-chars-backward " \r\t\n")
  14429. (insert "\n:END:")
  14430. (deactivate-mark t)
  14431. (indent-for-tab-command)
  14432. (unless (eolp) (insert "\n")))
  14433. ;; Clear marker, whatever the outcome of insertion is.
  14434. (set-marker rend nil)))))))
  14435. (defvar org-property-set-functions-alist nil
  14436. "Property set function alist.
  14437. Each entry should have the following format:
  14438. (PROPERTY . READ-FUNCTION)
  14439. The read function will be called with the same argument as
  14440. `org-completing-read'.")
  14441. (defun org-set-property-function (property)
  14442. "Get the function that should be used to set PROPERTY.
  14443. This is computed according to `org-property-set-functions-alist'."
  14444. (or (cdr (assoc property org-property-set-functions-alist))
  14445. 'org-completing-read))
  14446. (defun org-read-property-value (property)
  14447. "Read PROPERTY value from user."
  14448. (let* ((completion-ignore-case t)
  14449. (allowed (org-property-get-allowed-values nil property 'table))
  14450. (cur (org-entry-get nil property))
  14451. (prompt (concat property " value"
  14452. (if (and cur (string-match "\\S-" cur))
  14453. (concat " [" cur "]") "") ": "))
  14454. (set-function (org-set-property-function property))
  14455. (val (if allowed
  14456. (funcall set-function prompt allowed nil
  14457. (not (get-text-property 0 'org-unrestricted
  14458. (caar allowed))))
  14459. (let (org-completion-use-ido org-completion-use-iswitchb)
  14460. (funcall set-function prompt
  14461. (mapcar 'list (org-property-values property))
  14462. nil nil "" nil cur)))))
  14463. (org-trim val)))
  14464. (defvar org-last-set-property nil)
  14465. (defvar org-last-set-property-value nil)
  14466. (defun org-read-property-name ()
  14467. "Read a property name."
  14468. (let* ((completion-ignore-case t)
  14469. (keys (org-buffer-property-keys nil t t))
  14470. (default-prop (or (save-excursion
  14471. (save-match-data
  14472. (beginning-of-line)
  14473. (and (looking-at "^\\s-*:\\([^:\n]+\\):")
  14474. (null (string= (match-string 1) "END"))
  14475. (match-string 1))))
  14476. org-last-set-property))
  14477. (property (org-icompleting-read
  14478. (concat "Property"
  14479. (if default-prop (concat " [" default-prop "]") "")
  14480. ": ")
  14481. (mapcar 'list keys)
  14482. nil nil nil nil
  14483. default-prop)))
  14484. (if (member property keys)
  14485. property
  14486. (or (cdr (assoc (downcase property)
  14487. (mapcar (lambda (x) (cons (downcase x) x))
  14488. keys)))
  14489. property))))
  14490. (defun org-set-property-and-value (use-last)
  14491. "Allow to set [PROPERTY]: [value] direction from prompt.
  14492. When use-default, don't even ask, just use the last
  14493. \"[PROPERTY]: [value]\" string from the history."
  14494. (interactive "P")
  14495. (let* ((completion-ignore-case t)
  14496. (pv (or (and use-last org-last-set-property-value)
  14497. (org-completing-read
  14498. "Enter a \"[Property]: [value]\" pair: "
  14499. nil nil nil nil nil
  14500. org-last-set-property-value)))
  14501. prop val)
  14502. (when (string-match "^[ \t]*\\([^:]+\\):[ \t]*\\(.*\\)[ \t]*$" pv)
  14503. (setq prop (match-string 1 pv)
  14504. val (match-string 2 pv))
  14505. (org-set-property prop val))))
  14506. (defun org-set-property (property value)
  14507. "In the current entry, set PROPERTY to VALUE.
  14508. When called interactively, this will prompt for a property name, offering
  14509. completion on existing and default properties. And then it will prompt
  14510. for a value, offering completion either on allowed values (via an inherited
  14511. xxx_ALL property) or on existing values in other instances of this property
  14512. in the current file."
  14513. (interactive (list nil nil))
  14514. (let* ((property (or property (org-read-property-name)))
  14515. (value (or value (org-read-property-value property)))
  14516. (fn (cdr (assoc property org-properties-postprocess-alist))))
  14517. (setq org-last-set-property property)
  14518. (setq org-last-set-property-value (concat property ": " value))
  14519. ;; Possibly postprocess the inserted value:
  14520. (when fn (setq value (funcall fn value)))
  14521. (unless (equal (org-entry-get nil property) value)
  14522. (org-entry-put nil property value))))
  14523. (defun org-delete-property (property &optional delete-empty-drawer)
  14524. "In the current entry, delete PROPERTY.
  14525. When optional argument DELETE-EMPTY-DRAWER is a string, it defines
  14526. an empty drawer to delete."
  14527. (interactive
  14528. (let* ((completion-ignore-case t)
  14529. (cat (org-entry-get (point) "CATEGORY"))
  14530. (props0 (org-entry-properties nil 'standard))
  14531. (props (if cat props0
  14532. (delete `("CATEGORY" . ,(org-get-category)) props0)))
  14533. (prop (if (< 1 (length props))
  14534. (org-icompleting-read "Property: " props nil t)
  14535. (caar props))))
  14536. (list prop)))
  14537. (if (not property)
  14538. (message "No property to delete in this entry")
  14539. (org-entry-delete nil property delete-empty-drawer)
  14540. (message "Property \"%s\" deleted" property)))
  14541. (defun org-delete-property-globally (property)
  14542. "Remove PROPERTY globally, from all entries."
  14543. (interactive
  14544. (let* ((completion-ignore-case t)
  14545. (prop (org-icompleting-read
  14546. "Globally remove property: "
  14547. (mapcar 'list (org-buffer-property-keys)))))
  14548. (list prop)))
  14549. (save-excursion
  14550. (save-restriction
  14551. (widen)
  14552. (goto-char (point-min))
  14553. (let ((cnt 0))
  14554. (while (re-search-forward
  14555. (org-re-property property)
  14556. nil t)
  14557. (setq cnt (1+ cnt))
  14558. (delete-region (match-beginning 0) (1+ (point-at-eol))))
  14559. (message "Property \"%s\" removed from %d entries" property cnt)))))
  14560. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  14561. (defun org-compute-property-at-point ()
  14562. "Compute the property at point.
  14563. This looks for an enclosing column format, extracts the operator and
  14564. then applies it to the property in the column format's scope."
  14565. (interactive)
  14566. (unless (org-at-property-p)
  14567. (user-error "Not at a property"))
  14568. (let ((prop (org-match-string-no-properties 2)))
  14569. (org-columns-get-format-and-top-level)
  14570. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  14571. (user-error "No operator defined for property %s" prop))
  14572. (org-columns-compute prop)))
  14573. (defvar org-property-allowed-value-functions nil
  14574. "Hook for functions supplying allowed values for a specific property.
  14575. The functions must take a single argument, the name of the property, and
  14576. return a flat list of allowed values. If \":ETC\" is one of
  14577. the values, this means that these values are intended as defaults for
  14578. completion, but that other values should be allowed too.
  14579. The functions must return nil if they are not responsible for this
  14580. property.")
  14581. (defun org-property-get-allowed-values (pom property &optional table)
  14582. "Get allowed values for the property PROPERTY.
  14583. When TABLE is non-nil, return an alist that can directly be used for
  14584. completion."
  14585. (let (vals)
  14586. (cond
  14587. ((equal property "TODO")
  14588. (setq vals (org-with-point-at pom
  14589. (append org-todo-keywords-1 '("")))))
  14590. ((equal property "PRIORITY")
  14591. (let ((n org-lowest-priority))
  14592. (while (>= n org-highest-priority)
  14593. (push (char-to-string n) vals)
  14594. (setq n (1- n)))))
  14595. ((member property org-special-properties))
  14596. ((setq vals (run-hook-with-args-until-success
  14597. 'org-property-allowed-value-functions property)))
  14598. (t
  14599. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  14600. (when (and vals (string-match "\\S-" vals))
  14601. (setq vals (car (read-from-string (concat "(" vals ")"))))
  14602. (setq vals (mapcar (lambda (x)
  14603. (cond ((stringp x) x)
  14604. ((numberp x) (number-to-string x))
  14605. ((symbolp x) (symbol-name x))
  14606. (t "???")))
  14607. vals)))))
  14608. (when (member ":ETC" vals)
  14609. (setq vals (remove ":ETC" vals))
  14610. (org-add-props (car vals) '(org-unrestricted t)))
  14611. (if table (mapcar 'list vals) vals)))
  14612. (defun org-property-previous-allowed-value (&optional previous)
  14613. "Switch to the next allowed value for this property."
  14614. (interactive)
  14615. (org-property-next-allowed-value t))
  14616. (defun org-property-next-allowed-value (&optional previous)
  14617. "Switch to the next allowed value for this property."
  14618. (interactive)
  14619. (unless (org-at-property-p)
  14620. (user-error "Not at a property"))
  14621. (let* ((prop (car (save-match-data (org-split-string (match-string 1) ":"))))
  14622. (key (match-string 2))
  14623. (value (match-string 3))
  14624. (allowed (or (org-property-get-allowed-values (point) key)
  14625. (and (member value '("[ ]" "[-]" "[X]"))
  14626. '("[ ]" "[X]"))))
  14627. (heading (save-match-data (nth 4 (org-heading-components))))
  14628. nval)
  14629. (unless allowed
  14630. (user-error "Allowed values for this property have not been defined"))
  14631. (if previous (setq allowed (reverse allowed)))
  14632. (if (member value allowed)
  14633. (setq nval (car (cdr (member value allowed)))))
  14634. (setq nval (or nval (car allowed)))
  14635. (if (equal nval value)
  14636. (user-error "Only one allowed value for this property"))
  14637. (org-at-property-p)
  14638. (replace-match (concat " :" key ": " nval) t t)
  14639. (org-indent-line)
  14640. (beginning-of-line 1)
  14641. (skip-chars-forward " \t")
  14642. (when (equal prop org-effort-property)
  14643. (save-excursion
  14644. (org-back-to-heading t)
  14645. (put-text-property (point-at-bol) (point-at-eol) 'org-effort nval))
  14646. (when (string= org-clock-current-task heading)
  14647. (setq org-clock-effort nval)
  14648. (org-clock-update-mode-line)))
  14649. (run-hook-with-args 'org-property-changed-functions key nval)))
  14650. (defun org-find-olp (path &optional this-buffer)
  14651. "Return a marker pointing to the entry at outline path OLP.
  14652. If anything goes wrong, throw an error.
  14653. You can wrap this call to catch the error like this:
  14654. (condition-case msg
  14655. (org-mobile-locate-entry (match-string 4))
  14656. (error (nth 1 msg)))
  14657. The return value will then be either a string with the error message,
  14658. or a marker if everything is OK.
  14659. If THIS-BUFFER is set, the outline path does not contain a file,
  14660. only headings."
  14661. (let* ((file (if this-buffer buffer-file-name (pop path)))
  14662. (buffer (if this-buffer (current-buffer) (find-file-noselect file)))
  14663. (level 1)
  14664. (lmin 1)
  14665. (lmax 1)
  14666. limit re end found pos heading cnt flevel)
  14667. (unless buffer (error "File not found :%s" file))
  14668. (with-current-buffer buffer
  14669. (save-excursion
  14670. (save-restriction
  14671. (widen)
  14672. (setq limit (point-max))
  14673. (goto-char (point-min))
  14674. (while (setq heading (pop path))
  14675. (setq re (format org-complex-heading-regexp-format
  14676. (regexp-quote heading)))
  14677. (setq cnt 0 pos (point))
  14678. (while (re-search-forward re end t)
  14679. (setq level (- (match-end 1) (match-beginning 1)))
  14680. (if (and (>= level lmin) (<= level lmax))
  14681. (setq found (match-beginning 0) flevel level cnt (1+ cnt))))
  14682. (when (= cnt 0) (error "Heading not found on level %d: %s"
  14683. lmax heading))
  14684. (when (> cnt 1) (error "Heading not unique on level %d: %s"
  14685. lmax heading))
  14686. (goto-char found)
  14687. (setq lmin (1+ flevel) lmax (+ lmin (if org-odd-levels-only 1 0)))
  14688. (setq end (save-excursion (org-end-of-subtree t t))))
  14689. (when (org-at-heading-p)
  14690. (point-marker)))))))
  14691. (defun org-find-exact-headline-in-buffer (heading &optional buffer pos-only)
  14692. "Find node HEADING in BUFFER.
  14693. Return a marker to the heading if it was found, or nil if not.
  14694. If POS-ONLY is set, return just the position instead of a marker.
  14695. The heading text must match exact, but it may have a TODO keyword,
  14696. a priority cookie and tags in the standard locations."
  14697. (with-current-buffer (or buffer (current-buffer))
  14698. (save-excursion
  14699. (save-restriction
  14700. (widen)
  14701. (goto-char (point-min))
  14702. (let (case-fold-search)
  14703. (if (re-search-forward
  14704. (format org-complex-heading-regexp-format
  14705. (regexp-quote heading)) nil t)
  14706. (if pos-only
  14707. (match-beginning 0)
  14708. (move-marker (make-marker) (match-beginning 0)))))))))
  14709. (defun org-find-exact-heading-in-directory (heading &optional dir)
  14710. "Find Org node headline HEADING in all .org files in directory DIR.
  14711. When the target headline is found, return a marker to this location."
  14712. (let ((files (directory-files (or dir default-directory)
  14713. nil "\\`[^.#].*\\.org\\'"))
  14714. file visiting m buffer)
  14715. (catch 'found
  14716. (while (setq file (pop files))
  14717. (message "trying %s" file)
  14718. (setq visiting (org-find-base-buffer-visiting file))
  14719. (setq buffer (or visiting (find-file-noselect file)))
  14720. (setq m (org-find-exact-headline-in-buffer
  14721. heading buffer))
  14722. (when (and (not m) (not visiting)) (kill-buffer buffer))
  14723. (and m (throw 'found m))))))
  14724. (defun org-find-entry-with-id (ident)
  14725. "Locate the entry that contains the ID property with exact value IDENT.
  14726. IDENT can be a string, a symbol or a number, this function will search for
  14727. the string representation of it.
  14728. Return the position where this entry starts, or nil if there is no such entry."
  14729. (interactive "sID: ")
  14730. (let ((id (cond
  14731. ((stringp ident) ident)
  14732. ((symbol-name ident) (symbol-name ident))
  14733. ((numberp ident) (number-to-string ident))
  14734. (t (error "IDENT %s must be a string, symbol or number" ident))))
  14735. (case-fold-search nil))
  14736. (save-excursion
  14737. (save-restriction
  14738. (widen)
  14739. (goto-char (point-min))
  14740. (when (re-search-forward
  14741. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  14742. nil t)
  14743. (org-back-to-heading t)
  14744. (point))))))
  14745. ;;;; Timestamps
  14746. (defvar org-last-changed-timestamp nil)
  14747. (defvar org-last-inserted-timestamp nil
  14748. "The last time stamp inserted with `org-insert-time-stamp'.")
  14749. (defvar org-ts-what) ; dynamically scoped parameter
  14750. (defun org-time-stamp (arg &optional inactive)
  14751. "Prompt for a date/time and insert a time stamp.
  14752. If the user specifies a time like HH:MM or if this command is
  14753. called with at least one prefix argument, the time stamp contains
  14754. the date and the time. Otherwise, only the date is be included.
  14755. All parts of a date not specified by the user is filled in from
  14756. the current date/time. So if you just press return without
  14757. typing anything, the time stamp will represent the current
  14758. date/time.
  14759. If there is already a timestamp at the cursor, it will be
  14760. modified.
  14761. With two universal prefix arguments, insert an active timestamp
  14762. with the current time without prompting the user.
  14763. When called from lisp, the timestamp is inactive if INACTIVE is
  14764. non-nil."
  14765. (interactive "P")
  14766. (let* ((ts nil)
  14767. (default-time
  14768. ;; Default time is either today, or, when entering a range,
  14769. ;; the range start.
  14770. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  14771. (save-excursion
  14772. (re-search-backward
  14773. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  14774. (- (point) 20) t)))
  14775. (apply 'encode-time (org-parse-time-string (match-string 1)))
  14776. (current-time)))
  14777. (default-input (and ts (org-get-compact-tod ts)))
  14778. (repeater (save-excursion
  14779. (save-match-data
  14780. (beginning-of-line)
  14781. (when (re-search-forward
  14782. "\\([.+-]+[0-9]+[hdwmy] ?\\)+" ;;\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  14783. (save-excursion (progn (end-of-line) (point))) t)
  14784. (match-string 0)))))
  14785. org-time-was-given org-end-time-was-given time)
  14786. (cond
  14787. ((and (org-at-timestamp-p t)
  14788. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  14789. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  14790. (insert "--")
  14791. (setq time (let ((this-command this-command))
  14792. (org-read-date arg 'totime nil nil
  14793. default-time default-input inactive)))
  14794. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  14795. ((org-at-timestamp-p t)
  14796. (setq time (let ((this-command this-command))
  14797. (org-read-date arg 'totime nil nil default-time default-input inactive)))
  14798. (when (org-at-timestamp-p t) ; just to get the match data
  14799. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  14800. (replace-match "")
  14801. (setq org-last-changed-timestamp
  14802. (org-insert-time-stamp
  14803. time (or org-time-was-given arg)
  14804. inactive nil nil (list org-end-time-was-given)))
  14805. (when repeater (goto-char (1- (point))) (insert " " repeater)
  14806. (setq org-last-changed-timestamp
  14807. (concat (substring org-last-inserted-timestamp 0 -1)
  14808. " " repeater ">"))))
  14809. (message "Timestamp updated"))
  14810. ((equal arg '(16))
  14811. (org-insert-time-stamp (current-time) t inactive))
  14812. (t
  14813. (setq time (let ((this-command this-command))
  14814. (org-read-date arg 'totime nil nil default-time default-input inactive)))
  14815. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  14816. nil nil (list org-end-time-was-given))))))
  14817. ;; FIXME: can we use this for something else, like computing time differences?
  14818. (defun org-get-compact-tod (s)
  14819. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  14820. (let* ((t1 (match-string 1 s))
  14821. (h1 (string-to-number (match-string 2 s)))
  14822. (m1 (string-to-number (match-string 3 s)))
  14823. (t2 (and (match-end 4) (match-string 5 s)))
  14824. (h2 (and t2 (string-to-number (match-string 6 s))))
  14825. (m2 (and t2 (string-to-number (match-string 7 s))))
  14826. dh dm)
  14827. (if (not t2)
  14828. t1
  14829. (setq dh (- h2 h1) dm (- m2 m1))
  14830. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  14831. (concat t1 "+" (number-to-string dh)
  14832. (and (/= 0 dm) (format ":%02d" dm)))))))
  14833. (defun org-time-stamp-inactive (&optional arg)
  14834. "Insert an inactive time stamp.
  14835. An inactive time stamp is enclosed in square brackets instead of angle
  14836. brackets. It is inactive in the sense that it does not trigger agenda entries,
  14837. does not link to the calendar and cannot be changed with the S-cursor keys.
  14838. So these are more for recording a certain time/date."
  14839. (interactive "P")
  14840. (org-time-stamp arg 'inactive))
  14841. (defvar org-date-ovl (make-overlay 1 1))
  14842. (overlay-put org-date-ovl 'face 'org-date-selected)
  14843. (org-detach-overlay org-date-ovl)
  14844. (defvar org-ans1) ; dynamically scoped parameter
  14845. (defvar org-ans2) ; dynamically scoped parameter
  14846. (defvar org-plain-time-of-day-regexp) ; defined below
  14847. (defvar org-overriding-default-time nil) ; dynamically scoped
  14848. (defvar org-read-date-overlay nil)
  14849. (defvar org-dcst nil) ; dynamically scoped
  14850. (defvar org-read-date-history nil)
  14851. (defvar org-read-date-final-answer nil)
  14852. (defvar org-read-date-analyze-futurep nil)
  14853. (defvar org-read-date-analyze-forced-year nil)
  14854. (defvar org-read-date-inactive)
  14855. (defvar org-read-date-minibuffer-local-map
  14856. (let* ((org-replace-disputed-keys nil)
  14857. (map (make-sparse-keymap)))
  14858. (set-keymap-parent map minibuffer-local-map)
  14859. (org-defkey map (kbd ".")
  14860. (lambda () (interactive)
  14861. ;; Are we at the beginning of the prompt?
  14862. (if (looking-back "^[^:]+: ")
  14863. (org-eval-in-calendar '(calendar-goto-today))
  14864. (insert "."))))
  14865. (org-defkey map (kbd "C-.")
  14866. (lambda () (interactive)
  14867. (org-eval-in-calendar '(calendar-goto-today))))
  14868. (org-defkey map [(meta shift left)]
  14869. (lambda () (interactive)
  14870. (org-eval-in-calendar '(calendar-backward-month 1))))
  14871. (org-defkey map [(meta shift right)]
  14872. (lambda () (interactive)
  14873. (org-eval-in-calendar '(calendar-forward-month 1))))
  14874. (org-defkey map [(meta shift up)]
  14875. (lambda () (interactive)
  14876. (org-eval-in-calendar '(calendar-backward-year 1))))
  14877. (org-defkey map [(meta shift down)]
  14878. (lambda () (interactive)
  14879. (org-eval-in-calendar '(calendar-forward-year 1))))
  14880. (org-defkey map [?\e (shift left)]
  14881. (lambda () (interactive)
  14882. (org-eval-in-calendar '(calendar-backward-month 1))))
  14883. (org-defkey map [?\e (shift right)]
  14884. (lambda () (interactive)
  14885. (org-eval-in-calendar '(calendar-forward-month 1))))
  14886. (org-defkey map [?\e (shift up)]
  14887. (lambda () (interactive)
  14888. (org-eval-in-calendar '(calendar-backward-year 1))))
  14889. (org-defkey map [?\e (shift down)]
  14890. (lambda () (interactive)
  14891. (org-eval-in-calendar '(calendar-forward-year 1))))
  14892. (org-defkey map [(shift up)]
  14893. (lambda () (interactive)
  14894. (org-eval-in-calendar '(calendar-backward-week 1))))
  14895. (org-defkey map [(shift down)]
  14896. (lambda () (interactive)
  14897. (org-eval-in-calendar '(calendar-forward-week 1))))
  14898. (org-defkey map [(shift left)]
  14899. (lambda () (interactive)
  14900. (org-eval-in-calendar '(calendar-backward-day 1))))
  14901. (org-defkey map [(shift right)]
  14902. (lambda () (interactive)
  14903. (org-eval-in-calendar '(calendar-forward-day 1))))
  14904. (org-defkey map "!"
  14905. (lambda () (interactive)
  14906. (org-eval-in-calendar '(diary-view-entries))
  14907. (message "")))
  14908. (org-defkey map ">"
  14909. (lambda () (interactive)
  14910. (org-eval-in-calendar '(scroll-calendar-left 1))))
  14911. (org-defkey map "<"
  14912. (lambda () (interactive)
  14913. (org-eval-in-calendar '(scroll-calendar-right 1))))
  14914. (org-defkey map "\C-v"
  14915. (lambda () (interactive)
  14916. (org-eval-in-calendar
  14917. '(calendar-scroll-left-three-months 1))))
  14918. (org-defkey map "\M-v"
  14919. (lambda () (interactive)
  14920. (org-eval-in-calendar
  14921. '(calendar-scroll-right-three-months 1))))
  14922. map)
  14923. "Keymap for minibuffer commands when using `org-read-date'.")
  14924. (defvar org-def)
  14925. (defvar org-defdecode)
  14926. (defvar org-with-time)
  14927. (defun org-read-date (&optional org-with-time to-time from-string prompt
  14928. default-time default-input inactive)
  14929. "Read a date, possibly a time, and make things smooth for the user.
  14930. The prompt will suggest to enter an ISO date, but you can also enter anything
  14931. which will at least partially be understood by `parse-time-string'.
  14932. Unrecognized parts of the date will default to the current day, month, year,
  14933. hour and minute. If this command is called to replace a timestamp at point,
  14934. or to enter the second timestamp of a range, the default time is taken
  14935. from the existing stamp. Furthermore, the command prefers the future,
  14936. so if you are giving a date where the year is not given, and the day-month
  14937. combination is already past in the current year, it will assume you
  14938. mean next year. For details, see the manual. A few examples:
  14939. 3-2-5 --> 2003-02-05
  14940. feb 15 --> currentyear-02-15
  14941. 2/15 --> currentyear-02-15
  14942. sep 12 9 --> 2009-09-12
  14943. 12:45 --> today 12:45
  14944. 22 sept 0:34 --> currentyear-09-22 0:34
  14945. 12 --> currentyear-currentmonth-12
  14946. Fri --> nearest Friday after today
  14947. -Tue --> last Tuesday
  14948. etc.
  14949. Furthermore you can specify a relative date by giving, as the *first* thing
  14950. in the input: a plus/minus sign, a number and a letter [hdwmy] to indicate
  14951. change in days weeks, months, years.
  14952. With a single plus or minus, the date is relative to today. With a double
  14953. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  14954. +4d --> four days from today
  14955. +4 --> same as above
  14956. +2w --> two weeks from today
  14957. ++5 --> five days from default date
  14958. The function understands only English month and weekday abbreviations.
  14959. While prompting, a calendar is popped up - you can also select the
  14960. date with the mouse (button 1). The calendar shows a period of three
  14961. months. To scroll it to other months, use the keys `>' and `<'.
  14962. If you don't like the calendar, turn it off with
  14963. \(setq org-read-date-popup-calendar nil)
  14964. With optional argument TO-TIME, the date will immediately be converted
  14965. to an internal time.
  14966. With an optional argument ORG-WITH-TIME, the prompt will suggest to
  14967. also insert a time. Note that when ORG-WITH-TIME is not set, you can
  14968. still enter a time, and this function will inform the calling routine
  14969. about this change. The calling routine may then choose to change the
  14970. format used to insert the time stamp into the buffer to include the time.
  14971. With optional argument FROM-STRING, read from this string instead from
  14972. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  14973. the time/date that is used for everything that is not specified by the
  14974. user."
  14975. (require 'parse-time)
  14976. (let* ((org-time-stamp-rounding-minutes
  14977. (if (equal org-with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  14978. (org-dcst org-display-custom-times)
  14979. (ct (org-current-time))
  14980. (org-def (or org-overriding-default-time default-time ct))
  14981. (org-defdecode (decode-time org-def))
  14982. (dummy (progn
  14983. (when (< (nth 2 org-defdecode) org-extend-today-until)
  14984. (setcar (nthcdr 2 org-defdecode) -1)
  14985. (setcar (nthcdr 1 org-defdecode) 59)
  14986. (setq org-def (apply 'encode-time org-defdecode)
  14987. org-defdecode (decode-time org-def)))))
  14988. (mouse-autoselect-window nil) ; Don't let the mouse jump
  14989. (calendar-frame-setup nil)
  14990. (calendar-setup nil)
  14991. (calendar-move-hook nil)
  14992. (calendar-view-diary-initially-flag nil)
  14993. (calendar-view-holidays-initially-flag nil)
  14994. (timestr (format-time-string
  14995. (if org-with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") org-def))
  14996. (prompt (concat (if prompt (concat prompt " ") "")
  14997. (format "Date+time [%s]: " timestr)))
  14998. ans (org-ans0 "") org-ans1 org-ans2 final)
  14999. (cond
  15000. (from-string (setq ans from-string))
  15001. (org-read-date-popup-calendar
  15002. (save-excursion
  15003. (save-window-excursion
  15004. (calendar)
  15005. (org-eval-in-calendar '(setq cursor-type nil) t)
  15006. (unwind-protect
  15007. (progn
  15008. (calendar-forward-day (- (time-to-days org-def)
  15009. (calendar-absolute-from-gregorian
  15010. (calendar-current-date))))
  15011. (org-eval-in-calendar nil t)
  15012. (let* ((old-map (current-local-map))
  15013. (map (copy-keymap calendar-mode-map))
  15014. (minibuffer-local-map
  15015. (copy-keymap org-read-date-minibuffer-local-map)))
  15016. (org-defkey map (kbd "RET") 'org-calendar-select)
  15017. (org-defkey map [mouse-1] 'org-calendar-select-mouse)
  15018. (org-defkey map [mouse-2] 'org-calendar-select-mouse)
  15019. (unwind-protect
  15020. (progn
  15021. (use-local-map map)
  15022. (setq org-read-date-inactive inactive)
  15023. (add-hook 'post-command-hook 'org-read-date-display)
  15024. (setq org-ans0 (read-string prompt default-input
  15025. 'org-read-date-history nil))
  15026. ;; org-ans0: from prompt
  15027. ;; org-ans1: from mouse click
  15028. ;; org-ans2: from calendar motion
  15029. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  15030. (remove-hook 'post-command-hook 'org-read-date-display)
  15031. (use-local-map old-map)
  15032. (when org-read-date-overlay
  15033. (delete-overlay org-read-date-overlay)
  15034. (setq org-read-date-overlay nil)))))
  15035. (bury-buffer "*Calendar*")))))
  15036. (t ; Naked prompt only
  15037. (unwind-protect
  15038. (setq ans (read-string prompt default-input
  15039. 'org-read-date-history timestr))
  15040. (when org-read-date-overlay
  15041. (delete-overlay org-read-date-overlay)
  15042. (setq org-read-date-overlay nil)))))
  15043. (setq final (org-read-date-analyze ans org-def org-defdecode))
  15044. (when org-read-date-analyze-forced-year
  15045. (message "Year was forced into %s"
  15046. (if org-read-date-force-compatible-dates
  15047. "compatible range (1970-2037)"
  15048. "range representable on this machine"))
  15049. (ding))
  15050. ;; One round trip to get rid of 34th of August and stuff like that....
  15051. (setq final (decode-time (apply 'encode-time final)))
  15052. (setq org-read-date-final-answer ans)
  15053. (if to-time
  15054. (apply 'encode-time final)
  15055. (if (and (boundp 'org-time-was-given) org-time-was-given)
  15056. (format "%04d-%02d-%02d %02d:%02d"
  15057. (nth 5 final) (nth 4 final) (nth 3 final)
  15058. (nth 2 final) (nth 1 final))
  15059. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  15060. (defun org-read-date-display ()
  15061. "Display the current date prompt interpretation in the minibuffer."
  15062. (when org-read-date-display-live
  15063. (when org-read-date-overlay
  15064. (delete-overlay org-read-date-overlay))
  15065. (when (minibufferp (current-buffer))
  15066. (save-excursion
  15067. (end-of-line 1)
  15068. (while (not (equal (buffer-substring
  15069. (max (point-min) (- (point) 4)) (point))
  15070. " "))
  15071. (insert " ")))
  15072. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  15073. " " (or org-ans1 org-ans2)))
  15074. (org-end-time-was-given nil)
  15075. (f (org-read-date-analyze ans org-def org-defdecode))
  15076. (fmts (if org-dcst
  15077. org-time-stamp-custom-formats
  15078. org-time-stamp-formats))
  15079. (fmt (if (or org-with-time
  15080. (and (boundp 'org-time-was-given) org-time-was-given))
  15081. (cdr fmts)
  15082. (car fmts)))
  15083. (txt (format-time-string fmt (apply 'encode-time f)))
  15084. (txt (if org-read-date-inactive (concat "[" (substring txt 1 -1) "]") txt))
  15085. (txt (concat "=> " txt)))
  15086. (when (and org-end-time-was-given
  15087. (string-match org-plain-time-of-day-regexp txt))
  15088. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  15089. org-end-time-was-given
  15090. (substring txt (match-end 0)))))
  15091. (when org-read-date-analyze-futurep
  15092. (setq txt (concat txt " (=>F)")))
  15093. (setq org-read-date-overlay
  15094. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  15095. (org-overlay-display org-read-date-overlay txt 'secondary-selection)))))
  15096. (defun org-read-date-analyze (ans org-def org-defdecode)
  15097. "Analyze the combined answer of the date prompt."
  15098. ;; FIXME: cleanup and comment
  15099. (let ((nowdecode (decode-time (current-time)))
  15100. delta deltan deltaw deltadef year month day
  15101. hour minute second wday pm h2 m2 tl wday1
  15102. iso-year iso-weekday iso-week iso-year iso-date futurep kill-year)
  15103. (setq org-read-date-analyze-futurep nil
  15104. org-read-date-analyze-forced-year nil)
  15105. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  15106. (setq ans "+0"))
  15107. (when (setq delta (org-read-date-get-relative ans (current-time) org-def))
  15108. (setq ans (replace-match "" t t ans)
  15109. deltan (car delta)
  15110. deltaw (nth 1 delta)
  15111. deltadef (nth 2 delta)))
  15112. ;; Check if there is an iso week date in there. If yes, store the
  15113. ;; info and postpone interpreting it until the rest of the parsing
  15114. ;; is done.
  15115. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  15116. (setq iso-year (if (match-end 1)
  15117. (org-small-year-to-year
  15118. (string-to-number (match-string 1 ans))))
  15119. iso-weekday (if (match-end 3)
  15120. (string-to-number (match-string 3 ans)))
  15121. iso-week (string-to-number (match-string 2 ans)))
  15122. (setq ans (replace-match "" t t ans)))
  15123. ;; Help matching ISO dates with single digit month or day, like 2006-8-11.
  15124. (when (string-match
  15125. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  15126. (setq year (if (match-end 2)
  15127. (string-to-number (match-string 2 ans))
  15128. (progn (setq kill-year t)
  15129. (string-to-number (format-time-string "%Y"))))
  15130. month (string-to-number (match-string 3 ans))
  15131. day (string-to-number (match-string 4 ans)))
  15132. (if (< year 100) (setq year (+ 2000 year)))
  15133. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  15134. t nil ans)))
  15135. ;; Help matching dotted european dates
  15136. (when (string-match
  15137. "^ *\\(3[01]\\|0?[1-9]\\|[12][0-9]\\)\\. ?\\(0?[1-9]\\|1[012]\\)\\.\\( ?[1-9][0-9]\\{3\\}\\)?" ans)
  15138. (setq year (if (match-end 3) (string-to-number (match-string 3 ans))
  15139. (setq kill-year t)
  15140. (string-to-number (format-time-string "%Y")))
  15141. day (string-to-number (match-string 1 ans))
  15142. month (string-to-number (match-string 2 ans))
  15143. ans (replace-match (format "%04d-%02d-%02d" year month day)
  15144. t nil ans)))
  15145. ;; Help matching american dates, like 5/30 or 5/30/7
  15146. (when (string-match
  15147. "^ *\\(0?[1-9]\\|1[012]\\)/\\(0?[1-9]\\|[12][0-9]\\|3[01]\\)\\(/\\([0-9]+\\)\\)?\\([^/0-9]\\|$\\)" ans)
  15148. (setq year (if (match-end 4)
  15149. (string-to-number (match-string 4 ans))
  15150. (progn (setq kill-year t)
  15151. (string-to-number (format-time-string "%Y"))))
  15152. month (string-to-number (match-string 1 ans))
  15153. day (string-to-number (match-string 2 ans)))
  15154. (if (< year 100) (setq year (+ 2000 year)))
  15155. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  15156. t nil ans)))
  15157. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  15158. ;; If there is a time with am/pm, and *no* time without it, we convert
  15159. ;; so that matching will be successful.
  15160. (loop for i from 1 to 2 do ; twice, for end time as well
  15161. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  15162. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  15163. (setq hour (string-to-number (match-string 1 ans))
  15164. minute (if (match-end 3)
  15165. (string-to-number (match-string 3 ans))
  15166. 0)
  15167. pm (equal ?p
  15168. (string-to-char (downcase (match-string 4 ans)))))
  15169. (if (and (= hour 12) (not pm))
  15170. (setq hour 0)
  15171. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  15172. (setq ans (replace-match (format "%02d:%02d" hour minute)
  15173. t t ans))))
  15174. ;; Check if a time range is given as a duration
  15175. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  15176. (setq hour (string-to-number (match-string 1 ans))
  15177. h2 (+ hour (string-to-number (match-string 3 ans)))
  15178. minute (string-to-number (match-string 2 ans))
  15179. m2 (+ minute (if (match-end 5) (string-to-number
  15180. (match-string 5 ans))0)))
  15181. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  15182. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  15183. t t ans)))
  15184. ;; Check if there is a time range
  15185. (when (boundp 'org-end-time-was-given)
  15186. (setq org-time-was-given nil)
  15187. (when (and (string-match org-plain-time-of-day-regexp ans)
  15188. (match-end 8))
  15189. (setq org-end-time-was-given (match-string 8 ans))
  15190. (setq ans (concat (substring ans 0 (match-beginning 7))
  15191. (substring ans (match-end 7))))))
  15192. (setq tl (parse-time-string ans)
  15193. day (or (nth 3 tl) (nth 3 org-defdecode))
  15194. month (or (nth 4 tl)
  15195. (if (and org-read-date-prefer-future
  15196. (nth 3 tl) (< (nth 3 tl) (nth 3 nowdecode)))
  15197. (prog1 (1+ (nth 4 nowdecode)) (setq futurep t))
  15198. (nth 4 org-defdecode)))
  15199. year (or (and (not kill-year) (nth 5 tl))
  15200. (if (and org-read-date-prefer-future
  15201. (nth 4 tl) (< (nth 4 tl) (nth 4 nowdecode)))
  15202. (prog1 (1+ (nth 5 nowdecode)) (setq futurep t))
  15203. (nth 5 org-defdecode)))
  15204. hour (or (nth 2 tl) (nth 2 org-defdecode))
  15205. minute (or (nth 1 tl) (nth 1 org-defdecode))
  15206. second (or (nth 0 tl) 0)
  15207. wday (nth 6 tl))
  15208. (when (and (eq org-read-date-prefer-future 'time)
  15209. (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
  15210. (equal day (nth 3 nowdecode))
  15211. (equal month (nth 4 nowdecode))
  15212. (equal year (nth 5 nowdecode))
  15213. (nth 2 tl)
  15214. (or (< (nth 2 tl) (nth 2 nowdecode))
  15215. (and (= (nth 2 tl) (nth 2 nowdecode))
  15216. (nth 1 tl)
  15217. (< (nth 1 tl) (nth 1 nowdecode)))))
  15218. (setq day (1+ day)
  15219. futurep t))
  15220. ;; Special date definitions below
  15221. (cond
  15222. (iso-week
  15223. ;; There was an iso week
  15224. (require 'cal-iso)
  15225. (setq futurep nil)
  15226. (setq year (or iso-year year)
  15227. day (or iso-weekday wday 1)
  15228. wday nil ; to make sure that the trigger below does not match
  15229. iso-date (calendar-gregorian-from-absolute
  15230. (calendar-absolute-from-iso
  15231. (list iso-week day year))))
  15232. ; FIXME: Should we also push ISO weeks into the future?
  15233. ; (when (and org-read-date-prefer-future
  15234. ; (not iso-year)
  15235. ; (< (calendar-absolute-from-gregorian iso-date)
  15236. ; (time-to-days (current-time))))
  15237. ; (setq year (1+ year)
  15238. ; iso-date (calendar-gregorian-from-absolute
  15239. ; (calendar-absolute-from-iso
  15240. ; (list iso-week day year)))))
  15241. (setq month (car iso-date)
  15242. year (nth 2 iso-date)
  15243. day (nth 1 iso-date)))
  15244. (deltan
  15245. (setq futurep nil)
  15246. (unless deltadef
  15247. (let ((now (decode-time (current-time))))
  15248. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  15249. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  15250. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  15251. ((equal deltaw "m") (setq month (+ month deltan)))
  15252. ((equal deltaw "y") (setq year (+ year deltan)))))
  15253. ((and wday (not (nth 3 tl)))
  15254. ;; Weekday was given, but no day, so pick that day in the week
  15255. ;; on or after the derived date.
  15256. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  15257. (unless (equal wday wday1)
  15258. (setq day (+ day (% (- wday wday1 -7) 7))))))
  15259. (if (and (boundp 'org-time-was-given)
  15260. (nth 2 tl))
  15261. (setq org-time-was-given t))
  15262. (if (< year 100) (setq year (+ 2000 year)))
  15263. ;; Check of the date is representable
  15264. (if org-read-date-force-compatible-dates
  15265. (progn
  15266. (if (< year 1970)
  15267. (setq year 1970 org-read-date-analyze-forced-year t))
  15268. (if (> year 2037)
  15269. (setq year 2037 org-read-date-analyze-forced-year t)))
  15270. (condition-case nil
  15271. (ignore (encode-time second minute hour day month year))
  15272. (error
  15273. (setq year (nth 5 org-defdecode))
  15274. (setq org-read-date-analyze-forced-year t))))
  15275. (setq org-read-date-analyze-futurep futurep)
  15276. (list second minute hour day month year)))
  15277. (defvar parse-time-weekdays)
  15278. (defun org-read-date-get-relative (s today default)
  15279. "Check string S for special relative date string.
  15280. TODAY and DEFAULT are internal times, for today and for a default.
  15281. Return shift list (N what def-flag)
  15282. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  15283. N is the number of WHATs to shift.
  15284. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  15285. the DEFAULT date rather than TODAY."
  15286. (require 'parse-time)
  15287. (when (and
  15288. (string-match
  15289. (concat
  15290. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  15291. "\\([0-9]+\\)?"
  15292. "\\([hdwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  15293. "\\([ \t]\\|$\\)") s)
  15294. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  15295. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  15296. (string-to-char (substring (match-string 1 s) -1))
  15297. ?+))
  15298. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  15299. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  15300. (what (if (match-end 3) (match-string 3 s) "d"))
  15301. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  15302. (date (if rel default today))
  15303. (wday (nth 6 (decode-time date)))
  15304. delta)
  15305. (if wday1
  15306. (progn
  15307. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  15308. (if (= delta 0) (setq delta 7))
  15309. (if (= dir ?-)
  15310. (progn
  15311. (setq delta (- delta 7))
  15312. (if (= delta 0) (setq delta -7))))
  15313. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  15314. (list delta "d" rel))
  15315. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  15316. (defun org-order-calendar-date-args (arg1 arg2 arg3)
  15317. "Turn a user-specified date into the internal representation.
  15318. The internal representation needed by the calendar is (month day year).
  15319. This is a wrapper to handle the brain-dead convention in calendar that
  15320. user function argument order change dependent on argument order."
  15321. (if (boundp 'calendar-date-style)
  15322. (cond
  15323. ((eq calendar-date-style 'american)
  15324. (list arg1 arg2 arg3))
  15325. ((eq calendar-date-style 'european)
  15326. (list arg2 arg1 arg3))
  15327. ((eq calendar-date-style 'iso)
  15328. (list arg2 arg3 arg1)))
  15329. (org-no-warnings ;; european-calendar-style is obsolete as of version 23.1
  15330. (if (org-bound-and-true-p european-calendar-style)
  15331. (list arg2 arg1 arg3)
  15332. (list arg1 arg2 arg3)))))
  15333. (defun org-eval-in-calendar (form &optional keepdate)
  15334. "Eval FORM in the calendar window and return to current window.
  15335. When KEEPDATE is non-nil, update `org-ans2' from the cursor date,
  15336. otherwise stick to the current value of `org-ans2'."
  15337. (let ((sf (selected-frame))
  15338. (sw (selected-window)))
  15339. (select-window (get-buffer-window "*Calendar*" t))
  15340. (eval form)
  15341. (when (and (not keepdate) (calendar-cursor-to-date))
  15342. (let* ((date (calendar-cursor-to-date))
  15343. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15344. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  15345. (move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  15346. (select-window sw)
  15347. (org-select-frame-set-input-focus sf)))
  15348. (defun org-calendar-select ()
  15349. "Return to `org-read-date' with the date currently selected.
  15350. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  15351. (interactive)
  15352. (when (calendar-cursor-to-date)
  15353. (let* ((date (calendar-cursor-to-date))
  15354. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15355. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  15356. (if (active-minibuffer-window) (exit-minibuffer))))
  15357. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  15358. "Insert a date stamp for the date given by the internal TIME.
  15359. WITH-HM means use the stamp format that includes the time of the day.
  15360. INACTIVE means use square brackets instead of angular ones, so that the
  15361. stamp will not contribute to the agenda.
  15362. PRE and POST are optional strings to be inserted before and after the
  15363. stamp.
  15364. The command returns the inserted time stamp."
  15365. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  15366. stamp)
  15367. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  15368. (insert-before-markers (or pre ""))
  15369. (when (listp extra)
  15370. (setq extra (car extra))
  15371. (if (and (stringp extra)
  15372. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  15373. (setq extra (format "-%02d:%02d"
  15374. (string-to-number (match-string 1 extra))
  15375. (string-to-number (match-string 2 extra))))
  15376. (setq extra nil)))
  15377. (when extra
  15378. (setq fmt (concat (substring fmt 0 -1) extra (substring fmt -1))))
  15379. (insert-before-markers (setq stamp (format-time-string fmt time)))
  15380. (insert-before-markers (or post ""))
  15381. (setq org-last-inserted-timestamp stamp)))
  15382. (defun org-toggle-time-stamp-overlays ()
  15383. "Toggle the use of custom time stamp formats."
  15384. (interactive)
  15385. (setq org-display-custom-times (not org-display-custom-times))
  15386. (unless org-display-custom-times
  15387. (let ((p (point-min)) (bmp (buffer-modified-p)))
  15388. (while (setq p (next-single-property-change p 'display))
  15389. (if (and (get-text-property p 'display)
  15390. (eq (get-text-property p 'face) 'org-date))
  15391. (remove-text-properties
  15392. p (setq p (next-single-property-change p 'display))
  15393. '(display t))))
  15394. (set-buffer-modified-p bmp)))
  15395. (if (featurep 'xemacs)
  15396. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  15397. (org-restart-font-lock)
  15398. (setq org-table-may-need-update t)
  15399. (if org-display-custom-times
  15400. (message "Time stamps are overlaid with custom format")
  15401. (message "Time stamp overlays removed")))
  15402. (defun org-display-custom-time (beg end)
  15403. "Overlay modified time stamp format over timestamp between BEG and END."
  15404. (let* ((ts (buffer-substring beg end))
  15405. t1 w1 with-hm tf time str w2 (off 0))
  15406. (save-match-data
  15407. (setq t1 (org-parse-time-string ts t))
  15408. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)?\\'" ts)
  15409. (setq off (- (match-end 0) (match-beginning 0)))))
  15410. (setq end (- end off))
  15411. (setq w1 (- end beg)
  15412. with-hm (and (nth 1 t1) (nth 2 t1))
  15413. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  15414. time (org-fix-decoded-time t1)
  15415. str (org-add-props
  15416. (format-time-string
  15417. (substring tf 1 -1) (apply 'encode-time time))
  15418. nil 'mouse-face 'highlight)
  15419. w2 (length str))
  15420. (if (not (= w2 w1))
  15421. (add-text-properties (1+ beg) (+ 2 beg)
  15422. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  15423. (if (featurep 'xemacs)
  15424. (progn
  15425. (put-text-property beg end 'invisible t)
  15426. (put-text-property beg end 'end-glyph (make-glyph str)))
  15427. (put-text-property beg end 'display str))))
  15428. (defun org-translate-time (string)
  15429. "Translate all timestamps in STRING to custom format.
  15430. But do this only if the variable `org-display-custom-times' is set."
  15431. (when org-display-custom-times
  15432. (save-match-data
  15433. (let* ((start 0)
  15434. (re org-ts-regexp-both)
  15435. t1 with-hm inactive tf time str beg end)
  15436. (while (setq start (string-match re string start))
  15437. (setq beg (match-beginning 0)
  15438. end (match-end 0)
  15439. t1 (save-match-data
  15440. (org-parse-time-string (substring string beg end) t))
  15441. with-hm (and (nth 1 t1) (nth 2 t1))
  15442. inactive (equal (substring string beg (1+ beg)) "[")
  15443. tf (funcall (if with-hm 'cdr 'car)
  15444. org-time-stamp-custom-formats)
  15445. time (org-fix-decoded-time t1)
  15446. str (format-time-string
  15447. (concat
  15448. (if inactive "[" "<") (substring tf 1 -1)
  15449. (if inactive "]" ">"))
  15450. (apply 'encode-time time))
  15451. string (replace-match str t t string)
  15452. start (+ start (length str)))))))
  15453. string)
  15454. (defun org-fix-decoded-time (time)
  15455. "Set 0 instead of nil for the first 6 elements of time.
  15456. Don't touch the rest."
  15457. (let ((n 0))
  15458. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  15459. (define-obsolete-function-alias 'org-days-to-time 'org-time-stamp-to-now "24.4")
  15460. (defun org-time-stamp-to-now (timestamp-string &optional seconds)
  15461. "Difference between TIMESTAMP-STRING and now in days.
  15462. If SECONDS is non-nil, return the difference in seconds."
  15463. (let ((fdiff (if seconds 'org-float-time 'time-to-days)))
  15464. (- (funcall fdiff (org-time-string-to-time timestamp-string))
  15465. (funcall fdiff (current-time)))))
  15466. (defun org-deadline-close (timestamp-string &optional ndays)
  15467. "Is the time in TIMESTAMP-STRING close to the current date?"
  15468. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  15469. (and (< (org-time-stamp-to-now timestamp-string) ndays)
  15470. (not (org-entry-is-done-p))))
  15471. (defun org-get-wdays (ts &optional delay zero-delay)
  15472. "Get the deadline lead time appropriate for timestring TS.
  15473. When DELAY is non-nil, get the delay time for scheduled items
  15474. instead of the deadline lead time. When ZERO-DELAY is non-nil
  15475. and `org-scheduled-delay-days' is 0, enforce 0 as the delay,
  15476. don't try to find the delay cookie in the scheduled timestamp."
  15477. (let ((tv (if delay org-scheduled-delay-days
  15478. org-deadline-warning-days)))
  15479. (cond
  15480. ((or (and delay (< tv 0))
  15481. (and delay zero-delay (<= tv 0))
  15482. (and (not delay) (<= tv 0)))
  15483. ;; Enforce this value no matter what
  15484. (- tv))
  15485. ((string-match "-\\([0-9]+\\)\\([hdwmy]\\)\\(\\'\\|>\\| \\)" ts)
  15486. ;; lead time is specified.
  15487. (floor (* (string-to-number (match-string 1 ts))
  15488. (cdr (assoc (match-string 2 ts)
  15489. '(("d" . 1) ("w" . 7)
  15490. ("m" . 30.4) ("y" . 365.25)
  15491. ("h" . 0.041667)))))))
  15492. ;; go for the default.
  15493. (t tv))))
  15494. (defun org-calendar-select-mouse (ev)
  15495. "Return to `org-read-date' with the date currently selected.
  15496. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  15497. (interactive "e")
  15498. (mouse-set-point ev)
  15499. (when (calendar-cursor-to-date)
  15500. (let* ((date (calendar-cursor-to-date))
  15501. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15502. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  15503. (if (active-minibuffer-window) (exit-minibuffer))))
  15504. (defun org-check-deadlines (ndays)
  15505. "Check if there are any deadlines due or past due.
  15506. A deadline is considered due if it happens within `org-deadline-warning-days'
  15507. days from today's date. If the deadline appears in an entry marked DONE,
  15508. it is not shown. The prefix arg NDAYS can be used to test that many
  15509. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  15510. (interactive "P")
  15511. (let* ((org-warn-days
  15512. (cond
  15513. ((equal ndays '(4)) 100000)
  15514. (ndays (prefix-numeric-value ndays))
  15515. (t (abs org-deadline-warning-days))))
  15516. (case-fold-search nil)
  15517. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  15518. (callback
  15519. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  15520. (message "%d deadlines past-due or due within %d days"
  15521. (org-occur regexp nil callback)
  15522. org-warn-days)))
  15523. (defsubst org-re-timestamp (type)
  15524. "Return a regexp for timestamp TYPE.
  15525. Allowed values for TYPE are:
  15526. all: all timestamps
  15527. active: only active timestamps (<...>)
  15528. inactive: only inactive timestamps ([...])
  15529. scheduled: only scheduled timestamps
  15530. deadline: only deadline timestamps
  15531. closed: only closed time-stamps
  15532. When TYPE is nil, fall back on returning a regexp that matches
  15533. both scheduled and deadline timestamps."
  15534. (cond ((eq type 'all) "\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}\\(?: +[^]+0-9> \n -]+\\)?\\(?: +[0-9]\\{1,2\\}:[0-9]\\{2\\}\\)?\\)")
  15535. ((eq type 'active) org-ts-regexp)
  15536. ((eq type 'inactive) "\\[\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^ \n>]*?\\)\\]")
  15537. ((eq type 'scheduled) (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>"))
  15538. ((eq type 'deadline) (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  15539. ((eq type 'closed) (concat org-closed-string " \\[\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^ \n>]*?\\)\\]"))
  15540. ((eq type 'scheduled-or-deadline)
  15541. (concat "\\<\\(?:" org-deadline-string "\\|" org-scheduled-string "\\) *<\\([^>]+\\)>"))))
  15542. (defun org-check-before-date (date)
  15543. "Check if there are deadlines or scheduled entries before DATE."
  15544. (interactive (list (org-read-date)))
  15545. (let ((case-fold-search nil)
  15546. (regexp (org-re-timestamp org-ts-type))
  15547. (callback
  15548. (lambda () (time-less-p
  15549. (org-time-string-to-time (match-string 1))
  15550. (org-time-string-to-time date)))))
  15551. (message "%d entries before %s"
  15552. (org-occur regexp nil callback) date)))
  15553. (defun org-check-after-date (date)
  15554. "Check if there are deadlines or scheduled entries after DATE."
  15555. (interactive (list (org-read-date)))
  15556. (let ((case-fold-search nil)
  15557. (regexp (org-re-timestamp org-ts-type))
  15558. (callback
  15559. (lambda () (not
  15560. (time-less-p
  15561. (org-time-string-to-time (match-string 1))
  15562. (org-time-string-to-time date))))))
  15563. (message "%d entries after %s"
  15564. (org-occur regexp nil callback) date)))
  15565. (defun org-check-dates-range (start-date end-date)
  15566. "Check for deadlines/scheduled entries between START-DATE and END-DATE."
  15567. (interactive (list (org-read-date nil nil nil "Range starts")
  15568. (org-read-date nil nil nil "Range end")))
  15569. (let ((case-fold-search nil)
  15570. (regexp (org-re-timestamp org-ts-type))
  15571. (callback
  15572. (lambda ()
  15573. (let ((match (match-string 1)))
  15574. (and
  15575. (not (time-less-p
  15576. (org-time-string-to-time match)
  15577. (org-time-string-to-time start-date)))
  15578. (time-less-p
  15579. (org-time-string-to-time match)
  15580. (org-time-string-to-time end-date)))))))
  15581. (message "%d entries between %s and %s"
  15582. (org-occur regexp nil callback) start-date end-date)))
  15583. (defun org-evaluate-time-range (&optional to-buffer)
  15584. "Evaluate a time range by computing the difference between start and end.
  15585. Normally the result is just printed in the echo area, but with prefix arg
  15586. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  15587. If the time range is actually in a table, the result is inserted into the
  15588. next column.
  15589. For time difference computation, a year is assumed to be exactly 365
  15590. days in order to avoid rounding problems."
  15591. (interactive "P")
  15592. (or
  15593. (org-clock-update-time-maybe)
  15594. (save-excursion
  15595. (unless (org-at-date-range-p t)
  15596. (goto-char (point-at-bol))
  15597. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  15598. (if (not (org-at-date-range-p t))
  15599. (user-error "Not at a time-stamp range, and none found in current line")))
  15600. (let* ((ts1 (match-string 1))
  15601. (ts2 (match-string 2))
  15602. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  15603. (match-end (match-end 0))
  15604. (time1 (org-time-string-to-time ts1))
  15605. (time2 (org-time-string-to-time ts2))
  15606. (t1 (org-float-time time1))
  15607. (t2 (org-float-time time2))
  15608. (diff (abs (- t2 t1)))
  15609. (negative (< (- t2 t1) 0))
  15610. ;; (ys (floor (* 365 24 60 60)))
  15611. (ds (* 24 60 60))
  15612. (hs (* 60 60))
  15613. (fy "%dy %dd %02d:%02d")
  15614. (fy1 "%dy %dd")
  15615. (fd "%dd %02d:%02d")
  15616. (fd1 "%dd")
  15617. (fh "%02d:%02d")
  15618. y d h m align)
  15619. (if havetime
  15620. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  15621. y 0
  15622. d (floor (/ diff ds)) diff (mod diff ds)
  15623. h (floor (/ diff hs)) diff (mod diff hs)
  15624. m (floor (/ diff 60)))
  15625. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  15626. y 0
  15627. d (floor (+ (/ diff ds) 0.5))
  15628. h 0 m 0))
  15629. (if (not to-buffer)
  15630. (message "%s" (org-make-tdiff-string y d h m))
  15631. (if (org-at-table-p)
  15632. (progn
  15633. (goto-char match-end)
  15634. (setq align t)
  15635. (and (looking-at " *|") (goto-char (match-end 0))))
  15636. (goto-char match-end))
  15637. (if (looking-at
  15638. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  15639. (replace-match ""))
  15640. (if negative (insert " -"))
  15641. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  15642. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  15643. (insert " " (format fh h m))))
  15644. (if align (org-table-align))
  15645. (message "Time difference inserted")))))
  15646. (defun org-make-tdiff-string (y d h m)
  15647. (let ((fmt "")
  15648. (l nil))
  15649. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  15650. l (push y l)))
  15651. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  15652. l (push d l)))
  15653. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  15654. l (push h l)))
  15655. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  15656. l (push m l)))
  15657. (apply 'format fmt (nreverse l))))
  15658. (defun org-time-string-to-time (s &optional buffer pos)
  15659. "Convert a timestamp string into internal time."
  15660. (condition-case errdata
  15661. (apply 'encode-time (org-parse-time-string s))
  15662. (error (error "Bad timestamp `%s'%s\nError was: %s"
  15663. s (if (not (and buffer pos))
  15664. ""
  15665. (format " at %d in buffer `%s'" pos buffer))
  15666. (cdr errdata)))))
  15667. (defun org-time-string-to-seconds (s)
  15668. "Convert a timestamp string to a number of seconds."
  15669. (org-float-time (org-time-string-to-time s)))
  15670. (defun org-time-string-to-absolute (s &optional daynr prefer show-all buffer pos)
  15671. "Convert a time stamp to an absolute day number.
  15672. If there is a specifier for a cyclic time stamp, get the closest
  15673. date to DAYNR.
  15674. PREFER and SHOW-ALL are passed through to `org-closest-date'.
  15675. The variable `date' is bound by the calendar when this is called."
  15676. (cond
  15677. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  15678. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  15679. daynr
  15680. (+ daynr 1000)))
  15681. ((and daynr (string-match "\\+[0-9]+[hdwmy]" s))
  15682. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  15683. (time-to-days (current-time))) (match-string 0 s)
  15684. prefer show-all))
  15685. (t (time-to-days
  15686. (condition-case errdata
  15687. (apply 'encode-time (org-parse-time-string s))
  15688. (error (error "Bad timestamp `%s'%s\nError was: %s"
  15689. s (if (not (and buffer pos))
  15690. ""
  15691. (format " at %d in buffer `%s'" pos buffer))
  15692. (cdr errdata))))))))
  15693. (defun org-days-to-iso-week (days)
  15694. "Return the iso week number."
  15695. (require 'cal-iso)
  15696. (car (calendar-iso-from-absolute days)))
  15697. (defun org-small-year-to-year (year)
  15698. "Convert 2-digit years into 4-digit years.
  15699. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2037.
  15700. The year 2000 cannot be abbreviated. Any year larger than 99
  15701. is returned unchanged."
  15702. (if (< year 38)
  15703. (setq year (+ 2000 year))
  15704. (if (< year 100)
  15705. (setq year (+ 1900 year))))
  15706. year)
  15707. (defun org-time-from-absolute (d)
  15708. "Return the time corresponding to date D.
  15709. D may be an absolute day number, or a calendar-type list (month day year)."
  15710. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  15711. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  15712. (defun org-calendar-holiday ()
  15713. "List of holidays, for Diary display in Org-mode."
  15714. (require 'holidays)
  15715. (let ((hl (funcall
  15716. (if (fboundp 'calendar-check-holidays)
  15717. 'calendar-check-holidays 'check-calendar-holidays) date)))
  15718. (if hl (mapconcat 'identity hl "; "))))
  15719. (defun org-diary-sexp-entry (sexp entry date)
  15720. "Process a SEXP diary ENTRY for DATE."
  15721. (require 'diary-lib)
  15722. (let ((result (if calendar-debug-sexp
  15723. (let ((stack-trace-on-error t))
  15724. (eval (car (read-from-string sexp))))
  15725. (condition-case nil
  15726. (eval (car (read-from-string sexp)))
  15727. (error
  15728. (beep)
  15729. (message "Bad sexp at line %d in %s: %s"
  15730. (org-current-line)
  15731. (buffer-file-name) sexp)
  15732. (sleep-for 2))))))
  15733. (cond ((stringp result) (split-string result "; "))
  15734. ((and (consp result)
  15735. (not (consp (cdr result)))
  15736. (stringp (cdr result))) (cdr result))
  15737. ((and (consp result)
  15738. (stringp (car result))) result)
  15739. (result entry))))
  15740. (defun org-diary-to-ical-string (frombuf)
  15741. "Get iCalendar entries from diary entries in buffer FROMBUF.
  15742. This uses the icalendar.el library."
  15743. (let* ((tmpdir (if (featurep 'xemacs)
  15744. (temp-directory)
  15745. temporary-file-directory))
  15746. (tmpfile (make-temp-name
  15747. (expand-file-name "orgics" tmpdir)))
  15748. buf rtn b e)
  15749. (with-current-buffer frombuf
  15750. (icalendar-export-region (point-min) (point-max) tmpfile)
  15751. (setq buf (find-buffer-visiting tmpfile))
  15752. (set-buffer buf)
  15753. (goto-char (point-min))
  15754. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  15755. (setq b (match-beginning 0)))
  15756. (goto-char (point-max))
  15757. (if (re-search-backward "^END:VEVENT" nil t)
  15758. (setq e (match-end 0)))
  15759. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  15760. (kill-buffer buf)
  15761. (delete-file tmpfile)
  15762. rtn))
  15763. (defun org-closest-date (start current change prefer show-all)
  15764. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  15765. When PREFER is `past', return a date that is either CURRENT or past.
  15766. When PREFER is `future', return a date that is either CURRENT or future.
  15767. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  15768. ;; Make the proper lists from the dates
  15769. (catch 'exit
  15770. (let ((a1 '(("h" . hour)
  15771. ("d" . day)
  15772. ("w" . week)
  15773. ("m" . month)
  15774. ("y" . year)))
  15775. (shour (nth 2 (org-parse-time-string start)))
  15776. dn dw sday cday n1 n2 n0
  15777. d m y y1 y2 date1 date2 nmonths nm ny m2)
  15778. (setq start (org-date-to-gregorian start)
  15779. current (org-date-to-gregorian
  15780. (if show-all
  15781. current
  15782. (time-to-days (current-time))))
  15783. sday (calendar-absolute-from-gregorian start)
  15784. cday (calendar-absolute-from-gregorian current))
  15785. (if (<= cday sday) (throw 'exit sday))
  15786. (if (string-match "\\(\\+[0-9]+\\)\\([hdwmy]\\)" change)
  15787. (setq dn (string-to-number (match-string 1 change))
  15788. dw (cdr (assoc (match-string 2 change) a1)))
  15789. (user-error "Invalid change specifier: %s" change))
  15790. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  15791. (cond
  15792. ((eq dw 'hour)
  15793. (let ((missing-hours
  15794. (mod (+ (- (* 24 (- cday sday)) shour) org-extend-today-until)
  15795. dn)))
  15796. (setq n1 (if (zerop missing-hours) cday
  15797. (- cday (1+ (floor (/ missing-hours 24)))))
  15798. n2 (+ cday (floor (/ (- dn missing-hours) 24))))))
  15799. ((eq dw 'day)
  15800. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  15801. n2 (+ n1 dn)))
  15802. ((eq dw 'year)
  15803. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  15804. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  15805. (setq date1 (list m d y1)
  15806. n1 (calendar-absolute-from-gregorian date1)
  15807. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  15808. n2 (calendar-absolute-from-gregorian date2)))
  15809. ((eq dw 'month)
  15810. ;; approx number of month between the two dates
  15811. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  15812. ;; How often does dn fit in there?
  15813. (setq d (nth 1 start) m (car start) y (nth 2 start)
  15814. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  15815. m (+ m nm)
  15816. ny (floor (/ m 12))
  15817. y (+ y ny)
  15818. m (- m (* ny 12)))
  15819. (while (> m 12) (setq m (- m 12) y (1+ y)))
  15820. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  15821. (setq m2 (+ m dn) y2 y)
  15822. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  15823. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  15824. (while (<= n2 cday)
  15825. (setq n1 n2 m m2 y y2)
  15826. (setq m2 (+ m dn) y2 y)
  15827. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  15828. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  15829. ;; Make sure n1 is the earlier date
  15830. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  15831. (if show-all
  15832. (cond
  15833. ((eq prefer 'past) (if (= cday n2) n2 n1))
  15834. ((eq prefer 'future) (if (= cday n1) n1 n2))
  15835. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  15836. (cond
  15837. ((eq prefer 'past) (if (= cday n2) n2 n1))
  15838. ((eq prefer 'future) (if (= cday n1) n1 n2))
  15839. (t (if (= cday n1) n1 n2)))))))
  15840. (defun org-date-to-gregorian (date)
  15841. "Turn any specification of DATE into a Gregorian date for the calendar."
  15842. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  15843. ((and (listp date) (= (length date) 3)) date)
  15844. ((stringp date)
  15845. (setq date (org-parse-time-string date))
  15846. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  15847. ((listp date)
  15848. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  15849. (defun org-parse-time-string (s &optional nodefault)
  15850. "Parse the standard Org-mode time string.
  15851. This should be a lot faster than the normal `parse-time-string'.
  15852. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  15853. hour and minute fields will be nil if not given."
  15854. (cond ((string-match org-ts-regexp0 s)
  15855. (list 0
  15856. (if (or (match-beginning 8) (not nodefault))
  15857. (string-to-number (or (match-string 8 s) "0")))
  15858. (if (or (match-beginning 7) (not nodefault))
  15859. (string-to-number (or (match-string 7 s) "0")))
  15860. (string-to-number (match-string 4 s))
  15861. (string-to-number (match-string 3 s))
  15862. (string-to-number (match-string 2 s))
  15863. nil nil nil))
  15864. ((string-match "^<[^>]+>$" s)
  15865. (decode-time (seconds-to-time (org-matcher-time s))))
  15866. (t (error "Not a standard Org-mode time string: %s" s))))
  15867. (defun org-timestamp-up (&optional arg)
  15868. "Increase the date item at the cursor by one.
  15869. If the cursor is on the year, change the year. If it is on the month,
  15870. the day or the time, change that.
  15871. With prefix ARG, change by that many units."
  15872. (interactive "p")
  15873. (org-timestamp-change (prefix-numeric-value arg) nil 'updown))
  15874. (defun org-timestamp-down (&optional arg)
  15875. "Decrease the date item at the cursor by one.
  15876. If the cursor is on the year, change the year. If it is on the month,
  15877. the day or the time, change that.
  15878. With prefix ARG, change by that many units."
  15879. (interactive "p")
  15880. (org-timestamp-change (- (prefix-numeric-value arg)) nil 'updown))
  15881. (defun org-timestamp-up-day (&optional arg)
  15882. "Increase the date in the time stamp by one day.
  15883. With prefix ARG, change that many days."
  15884. (interactive "p")
  15885. (if (and (not (org-at-timestamp-p t))
  15886. (org-at-heading-p))
  15887. (org-todo 'up)
  15888. (org-timestamp-change (prefix-numeric-value arg) 'day 'updown)))
  15889. (defun org-timestamp-down-day (&optional arg)
  15890. "Decrease the date in the time stamp by one day.
  15891. With prefix ARG, change that many days."
  15892. (interactive "p")
  15893. (if (and (not (org-at-timestamp-p t))
  15894. (org-at-heading-p))
  15895. (org-todo 'down)
  15896. (org-timestamp-change (- (prefix-numeric-value arg)) 'day) 'updown))
  15897. (defun org-at-timestamp-p (&optional inactive-ok)
  15898. "Determine if the cursor is in or at a timestamp."
  15899. (interactive)
  15900. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  15901. (pos (point))
  15902. (ans (or (looking-at tsr)
  15903. (save-excursion
  15904. (skip-chars-backward "^[<\n\r\t")
  15905. (if (> (point) (point-min)) (backward-char 1))
  15906. (and (looking-at tsr)
  15907. (> (- (match-end 0) pos) -1))))))
  15908. (and ans
  15909. (boundp 'org-ts-what)
  15910. (setq org-ts-what
  15911. (cond
  15912. ((= pos (match-beginning 0)) 'bracket)
  15913. ;; Point is considered to be "on the bracket" whether
  15914. ;; it's really on it or right after it.
  15915. ((= pos (1- (match-end 0))) 'bracket)
  15916. ((= pos (match-end 0)) 'after)
  15917. ((org-pos-in-match-range pos 2) 'year)
  15918. ((org-pos-in-match-range pos 3) 'month)
  15919. ((org-pos-in-match-range pos 7) 'hour)
  15920. ((org-pos-in-match-range pos 8) 'minute)
  15921. ((or (org-pos-in-match-range pos 4)
  15922. (org-pos-in-match-range pos 5)) 'day)
  15923. ((and (> pos (or (match-end 8) (match-end 5)))
  15924. (< pos (match-end 0)))
  15925. (- pos (or (match-end 8) (match-end 5))))
  15926. (t 'day))))
  15927. ans))
  15928. (defun org-toggle-timestamp-type ()
  15929. "Toggle the type (<active> or [inactive]) of a time stamp."
  15930. (interactive)
  15931. (when (org-at-timestamp-p t)
  15932. (let ((beg (match-beginning 0)) (end (match-end 0))
  15933. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  15934. (save-excursion
  15935. (goto-char beg)
  15936. (while (re-search-forward "[][<>]" end t)
  15937. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  15938. t t)))
  15939. (message "Timestamp is now %sactive"
  15940. (if (equal (char-after beg) ?<) "" "in")))))
  15941. (defun org-at-clock-log-p nil
  15942. "Is the cursor on the clock log line?"
  15943. (save-excursion
  15944. (move-beginning-of-line 1)
  15945. (looking-at "^[ \t]*CLOCK:")))
  15946. (defvar org-clock-history) ; defined in org-clock.el
  15947. (defvar org-clock-adjust-closest nil) ; defined in org-clock.el
  15948. (defun org-timestamp-change (n &optional what updown suppress-tmp-delay)
  15949. "Change the date in the time stamp at point.
  15950. The date will be changed by N times WHAT. WHAT can be `day', `month',
  15951. `year', `minute', `second'. If WHAT is not given, the cursor position
  15952. in the timestamp determines what will be changed.
  15953. When SUPPRESS-TMP-DELAY is non-nil, suppress delays like \"--2d\"."
  15954. (let ((origin (point)) origin-cat
  15955. with-hm inactive
  15956. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  15957. org-ts-what
  15958. extra rem
  15959. ts time time0 fixnext clrgx)
  15960. (if (not (org-at-timestamp-p t))
  15961. (user-error "Not at a timestamp"))
  15962. (if (and (not what) (eq org-ts-what 'bracket))
  15963. (org-toggle-timestamp-type)
  15964. ;; Point isn't on brackets. Remember the part of the time-stamp
  15965. ;; the point was in. Indeed, size of time-stamps may change,
  15966. ;; but point must be kept in the same category nonetheless.
  15967. (setq origin-cat org-ts-what)
  15968. (if (and (not what) (not (eq org-ts-what 'day))
  15969. org-display-custom-times
  15970. (get-text-property (point) 'display)
  15971. (not (get-text-property (1- (point)) 'display)))
  15972. (setq org-ts-what 'day))
  15973. (setq org-ts-what (or what org-ts-what)
  15974. inactive (= (char-after (match-beginning 0)) ?\[)
  15975. ts (match-string 0))
  15976. (replace-match "")
  15977. (when (string-match
  15978. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?-?[-+][0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)*\\)[]>]"
  15979. ts)
  15980. (setq extra (match-string 1 ts))
  15981. (if suppress-tmp-delay
  15982. (setq extra (replace-regexp-in-string " --[0-9]+[hdwmy]" "" extra))))
  15983. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  15984. (setq with-hm t))
  15985. (setq time0 (org-parse-time-string ts))
  15986. (when (and updown
  15987. (eq org-ts-what 'minute)
  15988. (not current-prefix-arg))
  15989. ;; This looks like s-up and s-down. Change by one rounding step.
  15990. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  15991. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  15992. (setcar (cdr time0) (+ (nth 1 time0)
  15993. (if (> n 0) (- rem) (- dm rem))))))
  15994. (setq time
  15995. (encode-time (or (car time0) 0)
  15996. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  15997. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  15998. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  15999. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  16000. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  16001. (nthcdr 6 time0)))
  16002. (when (and (member org-ts-what '(hour minute))
  16003. extra
  16004. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  16005. (setq extra (org-modify-ts-extra
  16006. extra
  16007. (if (eq org-ts-what 'hour) 2 5)
  16008. n dm)))
  16009. (when (integerp org-ts-what)
  16010. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  16011. (if (eq what 'calendar)
  16012. (let ((cal-date (org-get-date-from-calendar)))
  16013. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  16014. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  16015. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  16016. (setcar time0 (or (car time0) 0))
  16017. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  16018. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  16019. (setq time (apply 'encode-time time0))))
  16020. ;; Insert the new time-stamp, and ensure point stays in the same
  16021. ;; category as before (i.e. not after the last position in that
  16022. ;; category).
  16023. (let ((pos (point)))
  16024. ;; Stay before inserted string. `save-excursion' is of no use.
  16025. (setq org-last-changed-timestamp
  16026. (org-insert-time-stamp time with-hm inactive nil nil extra))
  16027. (goto-char pos))
  16028. (save-match-data
  16029. (looking-at org-ts-regexp3)
  16030. (goto-char (cond
  16031. ;; `day' category ends before `hour' if any, or at
  16032. ;; the end of the day name.
  16033. ((eq origin-cat 'day)
  16034. (min (or (match-beginning 7) (1- (match-end 5))) origin))
  16035. ((eq origin-cat 'hour) (min (match-end 7) origin))
  16036. ((eq origin-cat 'minute) (min (1- (match-end 8)) origin))
  16037. ((integerp origin-cat) (min (1- (match-end 0)) origin))
  16038. ;; `year' and `month' have both fixed size: point
  16039. ;; couldn't have moved into another part.
  16040. (t origin))))
  16041. ;; Update clock if on a CLOCK line.
  16042. (org-clock-update-time-maybe)
  16043. ;; Maybe adjust the closest clock in `org-clock-history'
  16044. (when org-clock-adjust-closest
  16045. (if (not (and (org-at-clock-log-p)
  16046. (< 1 (length (delq nil (mapcar 'marker-position
  16047. org-clock-history))))))
  16048. (message "No clock to adjust")
  16049. (cond ((save-excursion ; fix previous clock?
  16050. (re-search-backward org-ts-regexp0 nil t)
  16051. (org-looking-back (concat org-clock-string " \\[")))
  16052. (setq fixnext 1 clrgx (concat org-ts-regexp0 "\\] =>.*$")))
  16053. ((save-excursion ; fix next clock?
  16054. (re-search-backward org-ts-regexp0 nil t)
  16055. (looking-at (concat org-ts-regexp0 "\\] =>")))
  16056. (setq fixnext -1 clrgx (concat org-clock-string " \\[" org-ts-regexp0))))
  16057. (save-window-excursion
  16058. ;; Find closest clock to point, adjust the previous/next one in history
  16059. (let* ((p (save-excursion (org-back-to-heading t)))
  16060. (cl (mapcar (lambda(c) (abs (- (marker-position c) p))) org-clock-history))
  16061. (clfixnth
  16062. (+ fixnext (- (length cl) (or (length (member (apply #'min cl) cl)) 100))))
  16063. (clfixpos (if (> 0 clfixnth) nil (nth clfixnth org-clock-history))))
  16064. (if (not clfixpos)
  16065. (message "No clock to adjust")
  16066. (save-excursion
  16067. (org-goto-marker-or-bmk clfixpos)
  16068. (org-show-subtree)
  16069. (when (re-search-forward clrgx nil t)
  16070. (goto-char (match-beginning 1))
  16071. (let (org-clock-adjust-closest)
  16072. (org-timestamp-change n org-ts-what updown))
  16073. (message "Clock adjusted in %s for heading: %s"
  16074. (file-name-nondirectory (buffer-file-name))
  16075. (org-get-heading t t)))))))))
  16076. ;; Try to recenter the calendar window, if any.
  16077. (if (and org-calendar-follow-timestamp-change
  16078. (get-buffer-window "*Calendar*" t)
  16079. (memq org-ts-what '(day month year)))
  16080. (org-recenter-calendar (time-to-days time))))))
  16081. (defun org-modify-ts-extra (s pos n dm)
  16082. "Change the different parts of the lead-time and repeat fields in timestamp."
  16083. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  16084. ng h m new rem)
  16085. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  16086. (cond
  16087. ((or (org-pos-in-match-range pos 2)
  16088. (org-pos-in-match-range pos 3))
  16089. (setq m (string-to-number (match-string 3 s))
  16090. h (string-to-number (match-string 2 s)))
  16091. (if (org-pos-in-match-range pos 2)
  16092. (setq h (+ h n))
  16093. (setq n (* dm (org-no-warnings (signum n))))
  16094. (when (not (= 0 (setq rem (% m dm))))
  16095. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  16096. (setq m (+ m n)))
  16097. (if (< m 0) (setq m (+ m 60) h (1- h)))
  16098. (if (> m 59) (setq m (- m 60) h (1+ h)))
  16099. (setq h (min 24 (max 0 h)))
  16100. (setq ng 1 new (format "-%02d:%02d" h m)))
  16101. ((org-pos-in-match-range pos 6)
  16102. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  16103. ((org-pos-in-match-range pos 5)
  16104. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  16105. ((org-pos-in-match-range pos 9)
  16106. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  16107. ((org-pos-in-match-range pos 8)
  16108. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  16109. (when ng
  16110. (setq s (concat
  16111. (substring s 0 (match-beginning ng))
  16112. new
  16113. (substring s (match-end ng))))))
  16114. s))
  16115. (defun org-recenter-calendar (date)
  16116. "If the calendar is visible, recenter it to DATE."
  16117. (let ((cwin (get-buffer-window "*Calendar*" t)))
  16118. (when cwin
  16119. (let ((calendar-move-hook nil))
  16120. (with-selected-window cwin
  16121. (calendar-goto-date (if (listp date) date
  16122. (calendar-gregorian-from-absolute date))))))))
  16123. (defun org-goto-calendar (&optional arg)
  16124. "Go to the Emacs calendar at the current date.
  16125. If there is a time stamp in the current line, go to that date.
  16126. A prefix ARG can be used to force the current date."
  16127. (interactive "P")
  16128. (let ((tsr org-ts-regexp) diff
  16129. (calendar-move-hook nil)
  16130. (calendar-view-holidays-initially-flag nil)
  16131. (calendar-view-diary-initially-flag nil))
  16132. (if (or (org-at-timestamp-p)
  16133. (save-excursion
  16134. (beginning-of-line 1)
  16135. (looking-at (concat ".*" tsr))))
  16136. (let ((d1 (time-to-days (current-time)))
  16137. (d2 (time-to-days
  16138. (org-time-string-to-time (match-string 1)))))
  16139. (setq diff (- d2 d1))))
  16140. (calendar)
  16141. (calendar-goto-today)
  16142. (if (and diff (not arg)) (calendar-forward-day diff))))
  16143. (defun org-get-date-from-calendar ()
  16144. "Return a list (month day year) of date at point in calendar."
  16145. (with-current-buffer "*Calendar*"
  16146. (save-match-data
  16147. (calendar-cursor-to-date))))
  16148. (defun org-date-from-calendar ()
  16149. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  16150. If there is already a time stamp at the cursor position, update it."
  16151. (interactive)
  16152. (if (org-at-timestamp-p t)
  16153. (org-timestamp-change 0 'calendar)
  16154. (let ((cal-date (org-get-date-from-calendar)))
  16155. (org-insert-time-stamp
  16156. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  16157. (defcustom org-effort-durations
  16158. `(("h" . 60)
  16159. ("d" . ,(* 60 8))
  16160. ("w" . ,(* 60 8 5))
  16161. ("m" . ,(* 60 8 5 4))
  16162. ("y" . ,(* 60 8 5 40)))
  16163. "Conversion factor to minutes for an effort modifier.
  16164. Each entry has the form (MODIFIER . MINUTES).
  16165. In an effort string, a number followed by MODIFIER is multiplied
  16166. by the specified number of MINUTES to obtain an effort in
  16167. minutes.
  16168. For example, if the value of this variable is ((\"hours\" . 60)), then an
  16169. effort string \"2hours\" is equivalent to 120 minutes."
  16170. :group 'org-agenda
  16171. :version "24.1"
  16172. :type '(alist :key-type (string :tag "Modifier")
  16173. :value-type (number :tag "Minutes")))
  16174. (defun org-minutes-to-clocksum-string (m)
  16175. "Format number of minutes as a clocksum string.
  16176. The format is determined by `org-time-clocksum-format',
  16177. `org-time-clocksum-use-fractional' and
  16178. `org-time-clocksum-fractional-format' and
  16179. `org-time-clocksum-use-effort-durations'."
  16180. (let ((clocksum "")
  16181. (m (round m)) ; Don't allow fractions of minutes
  16182. h d w mo y fmt n)
  16183. (setq h (if org-time-clocksum-use-effort-durations
  16184. (cdr (assoc "h" org-effort-durations)) 60)
  16185. d (if org-time-clocksum-use-effort-durations
  16186. (/ (cdr (assoc "d" org-effort-durations)) h) 24)
  16187. w (if org-time-clocksum-use-effort-durations
  16188. (/ (cdr (assoc "w" org-effort-durations)) (* d h)) 7)
  16189. mo (if org-time-clocksum-use-effort-durations
  16190. (/ (cdr (assoc "m" org-effort-durations)) (* d h)) 30)
  16191. y (if org-time-clocksum-use-effort-durations
  16192. (/ (cdr (assoc "y" org-effort-durations)) (* d h)) 365))
  16193. ;; fractional format
  16194. (if org-time-clocksum-use-fractional
  16195. (cond
  16196. ;; single format string
  16197. ((stringp org-time-clocksum-fractional-format)
  16198. (format org-time-clocksum-fractional-format (/ m (float h))))
  16199. ;; choice of fractional formats for different time units
  16200. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :years))
  16201. (> (/ (truncate m) (* y d h)) 0))
  16202. (format fmt (/ m (* y d (float h)))))
  16203. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :months))
  16204. (> (/ (truncate m) (* mo d h)) 0))
  16205. (format fmt (/ m (* mo d (float h)))))
  16206. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :weeks))
  16207. (> (/ (truncate m) (* w d h)) 0))
  16208. (format fmt (/ m (* w d (float h)))))
  16209. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :days))
  16210. (> (/ (truncate m) (* d h)) 0))
  16211. (format fmt (/ m (* d (float h)))))
  16212. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :hours))
  16213. (> (/ (truncate m) h) 0))
  16214. (format fmt (/ m (float h))))
  16215. ((setq fmt (plist-get org-time-clocksum-fractional-format :minutes))
  16216. (format fmt m))
  16217. ;; fall back to smallest time unit with a format
  16218. ((setq fmt (plist-get org-time-clocksum-fractional-format :hours))
  16219. (format fmt (/ m (float h))))
  16220. ((setq fmt (plist-get org-time-clocksum-fractional-format :days))
  16221. (format fmt (/ m (* d (float h)))))
  16222. ((setq fmt (plist-get org-time-clocksum-fractional-format :weeks))
  16223. (format fmt (/ m (* w d (float h)))))
  16224. ((setq fmt (plist-get org-time-clocksum-fractional-format :months))
  16225. (format fmt (/ m (* mo d (float h)))))
  16226. ((setq fmt (plist-get org-time-clocksum-fractional-format :years))
  16227. (format fmt (/ m (* y d (float h))))))
  16228. ;; standard (non-fractional) format, with single format string
  16229. (if (stringp org-time-clocksum-format)
  16230. (format org-time-clocksum-format (setq n (/ m h)) (- m (* h n)))
  16231. ;; separate formats components
  16232. (and (setq fmt (plist-get org-time-clocksum-format :years))
  16233. (or (> (setq n (/ (truncate m) (* y d h))) 0)
  16234. (plist-get org-time-clocksum-format :require-years))
  16235. (setq clocksum (concat clocksum (format fmt n))
  16236. m (- m (* n y d h))))
  16237. (and (setq fmt (plist-get org-time-clocksum-format :months))
  16238. (or (> (setq n (/ (truncate m) (* mo d h))) 0)
  16239. (plist-get org-time-clocksum-format :require-months))
  16240. (setq clocksum (concat clocksum (format fmt n))
  16241. m (- m (* n mo d h))))
  16242. (and (setq fmt (plist-get org-time-clocksum-format :weeks))
  16243. (or (> (setq n (/ (truncate m) (* w d h))) 0)
  16244. (plist-get org-time-clocksum-format :require-weeks))
  16245. (setq clocksum (concat clocksum (format fmt n))
  16246. m (- m (* n w d h))))
  16247. (and (setq fmt (plist-get org-time-clocksum-format :days))
  16248. (or (> (setq n (/ (truncate m) (* d h))) 0)
  16249. (plist-get org-time-clocksum-format :require-days))
  16250. (setq clocksum (concat clocksum (format fmt n))
  16251. m (- m (* n d h))))
  16252. (and (setq fmt (plist-get org-time-clocksum-format :hours))
  16253. (or (> (setq n (/ (truncate m) h)) 0)
  16254. (plist-get org-time-clocksum-format :require-hours))
  16255. (setq clocksum (concat clocksum (format fmt n))
  16256. m (- m (* n h))))
  16257. (and (setq fmt (plist-get org-time-clocksum-format :minutes))
  16258. (or (> m 0) (plist-get org-time-clocksum-format :require-minutes))
  16259. (setq clocksum (concat clocksum (format fmt m))))
  16260. ;; return formatted time duration
  16261. clocksum))))
  16262. (defalias 'org-minutes-to-hh:mm-string 'org-minutes-to-clocksum-string)
  16263. (make-obsolete 'org-minutes-to-hh:mm-string 'org-minutes-to-clocksum-string
  16264. "Org mode version 8.0")
  16265. (defun org-hours-to-clocksum-string (n)
  16266. (org-minutes-to-clocksum-string (* n 60)))
  16267. (defun org-hh:mm-string-to-minutes (s)
  16268. "Convert a string H:MM to a number of minutes.
  16269. If the string is just a number, interpret it as minutes.
  16270. In fact, the first hh:mm or number in the string will be taken,
  16271. there can be extra stuff in the string.
  16272. If no number is found, the return value is 0."
  16273. (cond
  16274. ((integerp s) s)
  16275. ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  16276. (+ (* (string-to-number (match-string 1 s)) 60)
  16277. (string-to-number (match-string 2 s))))
  16278. ((string-match "\\([0-9]+\\)" s)
  16279. (string-to-number (match-string 1 s)))
  16280. (t 0)))
  16281. (defcustom org-image-actual-width t
  16282. "Should we use the actual width of images when inlining them?
  16283. When set to `t', always use the image width.
  16284. When set to a number, use imagemagick (when available) to set
  16285. the image's width to this value.
  16286. When set to a number in a list, try to get the width from any
  16287. #+ATTR.* keyword if it matches a width specification like
  16288. #+ATTR_HTML: :width 300px
  16289. and fall back on that number if none is found.
  16290. When set to nil, try to get the width from an #+ATTR.* keyword
  16291. and fall back on the original width if none is found.
  16292. This requires Emacs >= 24.1, build with imagemagick support."
  16293. :group 'org-appearance
  16294. :version "24.4"
  16295. :package-version '(Org . "8.0")
  16296. :type '(choice
  16297. (const :tag "Use the image width" t)
  16298. (integer :tag "Use a number of pixels")
  16299. (list :tag "Use #+ATTR* or a number of pixels" (integer))
  16300. (const :tag "Use #+ATTR* or don't resize" nil)))
  16301. (defcustom org-agenda-inhibit-startup nil
  16302. "Inhibit startup when preparing agenda buffers.
  16303. When this variable is `t', the initialization of the Org agenda
  16304. buffers is inhibited: e.g. the visibility state is not set, the
  16305. tables are not re-aligned, etc."
  16306. :type 'boolean
  16307. :version "24.3"
  16308. :group 'org-agenda)
  16309. (defcustom org-agenda-ignore-drawer-properties nil
  16310. "Avoid updating text properties when building the agenda.
  16311. Properties are used to prepare buffers for effort estimates, appointments,
  16312. and subtree-local categories.
  16313. If you don't use these in the agenda, you can add them to this list and
  16314. agenda building will be a bit faster.
  16315. The value is a list, with zero or more of the symbols `effort', `appt',
  16316. or `category'."
  16317. :type '(set :greedy t
  16318. (const effort)
  16319. (const appt)
  16320. (const category))
  16321. :version "24.3"
  16322. :group 'org-agenda)
  16323. (defun org-duration-string-to-minutes (s &optional output-to-string)
  16324. "Convert a duration string S to minutes.
  16325. A bare number is interpreted as minutes, modifiers can be set by
  16326. customizing `org-effort-durations' (which see).
  16327. Entries containing a colon are interpreted as H:MM by
  16328. `org-hh:mm-string-to-minutes'."
  16329. (let ((result 0)
  16330. (re (concat "\\([0-9.]+\\) *\\("
  16331. (regexp-opt (mapcar 'car org-effort-durations))
  16332. "\\)")))
  16333. (while (string-match re s)
  16334. (incf result (* (cdr (assoc (match-string 2 s) org-effort-durations))
  16335. (string-to-number (match-string 1 s))))
  16336. (setq s (replace-match "" nil t s)))
  16337. (setq result (floor result))
  16338. (incf result (org-hh:mm-string-to-minutes s))
  16339. (if output-to-string (number-to-string result) result)))
  16340. ;;;; Files
  16341. (defun org-save-all-org-buffers ()
  16342. "Save all Org-mode buffers without user confirmation."
  16343. (interactive)
  16344. (message "Saving all Org-mode buffers...")
  16345. (save-some-buffers t (lambda () (derived-mode-p 'org-mode)))
  16346. (when (featurep 'org-id) (org-id-locations-save))
  16347. (message "Saving all Org-mode buffers... done"))
  16348. (defun org-revert-all-org-buffers ()
  16349. "Revert all Org-mode buffers.
  16350. Prompt for confirmation when there are unsaved changes.
  16351. Be sure you know what you are doing before letting this function
  16352. overwrite your changes.
  16353. This function is useful in a setup where one tracks org files
  16354. with a version control system, to revert on one machine after pulling
  16355. changes from another. I believe the procedure must be like this:
  16356. 1. M-x org-save-all-org-buffers
  16357. 2. Pull changes from the other machine, resolve conflicts
  16358. 3. M-x org-revert-all-org-buffers"
  16359. (interactive)
  16360. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  16361. (user-error "Abort"))
  16362. (save-excursion
  16363. (save-window-excursion
  16364. (mapc
  16365. (lambda (b)
  16366. (when (and (with-current-buffer b (derived-mode-p 'org-mode))
  16367. (with-current-buffer b buffer-file-name))
  16368. (org-pop-to-buffer-same-window b)
  16369. (revert-buffer t 'no-confirm)))
  16370. (buffer-list))
  16371. (when (and (featurep 'org-id) org-id-track-globally)
  16372. (org-id-locations-load)))))
  16373. ;;;; Agenda files
  16374. ;;;###autoload
  16375. (defun org-switchb (&optional arg)
  16376. "Switch between Org buffers.
  16377. With one prefix argument, restrict available buffers to files.
  16378. With two prefix arguments, restrict available buffers to agenda files.
  16379. Defaults to `iswitchb' for buffer name completion.
  16380. Set `org-completion-use-ido' to make it use ido instead."
  16381. (interactive "P")
  16382. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  16383. ((equal arg '(16)) (org-buffer-list 'agenda))
  16384. (t (org-buffer-list))))
  16385. (org-completion-use-iswitchb org-completion-use-iswitchb)
  16386. (org-completion-use-ido org-completion-use-ido))
  16387. (unless (or org-completion-use-ido org-completion-use-iswitchb)
  16388. (setq org-completion-use-iswitchb t))
  16389. (org-pop-to-buffer-same-window
  16390. (org-icompleting-read "Org buffer: "
  16391. (mapcar 'list (mapcar 'buffer-name blist))
  16392. nil t))))
  16393. ;;; Define some older names previously used for this functionality
  16394. ;;;###autoload
  16395. (defalias 'org-ido-switchb 'org-switchb)
  16396. ;;;###autoload
  16397. (defalias 'org-iswitchb 'org-switchb)
  16398. (defun org-buffer-list (&optional predicate exclude-tmp)
  16399. "Return a list of Org buffers.
  16400. PREDICATE can be `export', `files' or `agenda'.
  16401. export restrict the list to Export buffers.
  16402. files restrict the list to buffers visiting Org files.
  16403. agenda restrict the list to buffers visiting agenda files.
  16404. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  16405. (let* ((bfn nil)
  16406. (agenda-files (and (eq predicate 'agenda)
  16407. (mapcar 'file-truename (org-agenda-files t))))
  16408. (filter
  16409. (cond
  16410. ((eq predicate 'files)
  16411. (lambda (b) (with-current-buffer b (derived-mode-p 'org-mode))))
  16412. ((eq predicate 'export)
  16413. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  16414. ((eq predicate 'agenda)
  16415. (lambda (b)
  16416. (with-current-buffer b
  16417. (and (derived-mode-p 'org-mode)
  16418. (setq bfn (buffer-file-name b))
  16419. (member (file-truename bfn) agenda-files)))))
  16420. (t (lambda (b) (with-current-buffer b
  16421. (or (derived-mode-p 'org-mode)
  16422. (string-match "\*Org .*Export"
  16423. (buffer-name b)))))))))
  16424. (delq nil
  16425. (mapcar
  16426. (lambda(b)
  16427. (if (and (funcall filter b)
  16428. (or (not exclude-tmp)
  16429. (not (string-match "tmp" (buffer-name b)))))
  16430. b
  16431. nil))
  16432. (buffer-list)))))
  16433. (defun org-agenda-files (&optional unrestricted archives)
  16434. "Get the list of agenda files.
  16435. Optional UNRESTRICTED means return the full list even if a restriction
  16436. is currently in place.
  16437. When ARCHIVES is t, include all archive files that are really being
  16438. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  16439. `org-agenda-archives-mode' is t."
  16440. (let ((files
  16441. (cond
  16442. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  16443. ((stringp org-agenda-files) (org-read-agenda-file-list))
  16444. ((listp org-agenda-files) org-agenda-files)
  16445. (t (error "Invalid value of `org-agenda-files'")))))
  16446. (setq files (apply 'append
  16447. (mapcar (lambda (f)
  16448. (if (file-directory-p f)
  16449. (directory-files
  16450. f t org-agenda-file-regexp)
  16451. (list f)))
  16452. files)))
  16453. (when org-agenda-skip-unavailable-files
  16454. (setq files (delq nil
  16455. (mapcar (function
  16456. (lambda (file)
  16457. (and (file-readable-p file) file)))
  16458. files))))
  16459. (when (or (eq archives t)
  16460. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  16461. (setq files (org-add-archive-files files)))
  16462. files))
  16463. (defun org-agenda-file-p (&optional file)
  16464. "Return non-nil, if FILE is an agenda file.
  16465. If FILE is omitted, use the file associated with the current
  16466. buffer."
  16467. (member (or file (buffer-file-name))
  16468. (org-agenda-files t)))
  16469. (defun org-edit-agenda-file-list ()
  16470. "Edit the list of agenda files.
  16471. Depending on setup, this either uses customize to edit the variable
  16472. `org-agenda-files', or it visits the file that is holding the list. In the
  16473. latter case, the buffer is set up in a way that saving it automatically kills
  16474. the buffer and restores the previous window configuration."
  16475. (interactive)
  16476. (if (stringp org-agenda-files)
  16477. (let ((cw (current-window-configuration)))
  16478. (find-file org-agenda-files)
  16479. (org-set-local 'org-window-configuration cw)
  16480. (org-add-hook 'after-save-hook
  16481. (lambda ()
  16482. (set-window-configuration
  16483. (prog1 org-window-configuration
  16484. (kill-buffer (current-buffer))))
  16485. (org-install-agenda-files-menu)
  16486. (message "New agenda file list installed"))
  16487. nil 'local)
  16488. (message "%s" (substitute-command-keys
  16489. "Edit list and finish with \\[save-buffer]")))
  16490. (customize-variable 'org-agenda-files)))
  16491. (defun org-store-new-agenda-file-list (list)
  16492. "Set new value for the agenda file list and save it correctly."
  16493. (if (stringp org-agenda-files)
  16494. (let ((fe (org-read-agenda-file-list t)) b u)
  16495. (while (setq b (find-buffer-visiting org-agenda-files))
  16496. (kill-buffer b))
  16497. (with-temp-file org-agenda-files
  16498. (insert
  16499. (mapconcat
  16500. (lambda (f) ;; Keep un-expanded entries.
  16501. (if (setq u (assoc f fe))
  16502. (cdr u)
  16503. f))
  16504. list "\n")
  16505. "\n")))
  16506. (let ((org-mode-hook nil) (org-inhibit-startup t)
  16507. (org-insert-mode-line-in-empty-file nil))
  16508. (setq org-agenda-files list)
  16509. (customize-save-variable 'org-agenda-files org-agenda-files))))
  16510. (defun org-read-agenda-file-list (&optional pair-with-expansion)
  16511. "Read the list of agenda files from a file.
  16512. If PAIR-WITH-EXPANSION is t return pairs with un-expanded
  16513. filenames, used by `org-store-new-agenda-file-list' to write back
  16514. un-expanded file names."
  16515. (when (file-directory-p org-agenda-files)
  16516. (error "`org-agenda-files' cannot be a single directory"))
  16517. (when (stringp org-agenda-files)
  16518. (with-temp-buffer
  16519. (insert-file-contents org-agenda-files)
  16520. (mapcar
  16521. (lambda (f)
  16522. (let ((e (expand-file-name (substitute-in-file-name f)
  16523. org-directory)))
  16524. (if pair-with-expansion
  16525. (cons e f)
  16526. e)))
  16527. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*")))))
  16528. ;;;###autoload
  16529. (defun org-cycle-agenda-files ()
  16530. "Cycle through the files in `org-agenda-files'.
  16531. If the current buffer visits an agenda file, find the next one in the list.
  16532. If the current buffer does not, find the first agenda file."
  16533. (interactive)
  16534. (let* ((fs (org-agenda-files t))
  16535. (files (append fs (list (car fs))))
  16536. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  16537. file)
  16538. (unless files (user-error "No agenda files"))
  16539. (catch 'exit
  16540. (while (setq file (pop files))
  16541. (if (equal (file-truename file) tcf)
  16542. (when (car files)
  16543. (find-file (car files))
  16544. (throw 'exit t))))
  16545. (find-file (car fs)))
  16546. (if (buffer-base-buffer) (org-pop-to-buffer-same-window (buffer-base-buffer)))))
  16547. (defun org-agenda-file-to-front (&optional to-end)
  16548. "Move/add the current file to the top of the agenda file list.
  16549. If the file is not present in the list, it is added to the front. If it is
  16550. present, it is moved there. With optional argument TO-END, add/move to the
  16551. end of the list."
  16552. (interactive "P")
  16553. (let ((org-agenda-skip-unavailable-files nil)
  16554. (file-alist (mapcar (lambda (x)
  16555. (cons (file-truename x) x))
  16556. (org-agenda-files t)))
  16557. (ctf (file-truename
  16558. (or buffer-file-name
  16559. (user-error "Please save the current buffer to a file"))))
  16560. x had)
  16561. (setq x (assoc ctf file-alist) had x)
  16562. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  16563. (if to-end
  16564. (setq file-alist (append (delq x file-alist) (list x)))
  16565. (setq file-alist (cons x (delq x file-alist))))
  16566. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  16567. (org-install-agenda-files-menu)
  16568. (message "File %s to %s of agenda file list"
  16569. (if had "moved" "added") (if to-end "end" "front"))))
  16570. (defun org-remove-file (&optional file)
  16571. "Remove current file from the list of files in variable `org-agenda-files'.
  16572. These are the files which are being checked for agenda entries.
  16573. Optional argument FILE means use this file instead of the current."
  16574. (interactive)
  16575. (let* ((org-agenda-skip-unavailable-files nil)
  16576. (file (or file buffer-file-name
  16577. (user-error "Current buffer does not visit a file")))
  16578. (true-file (file-truename file))
  16579. (afile (abbreviate-file-name file))
  16580. (files (delq nil (mapcar
  16581. (lambda (x)
  16582. (if (equal true-file
  16583. (file-truename x))
  16584. nil x))
  16585. (org-agenda-files t)))))
  16586. (if (not (= (length files) (length (org-agenda-files t))))
  16587. (progn
  16588. (org-store-new-agenda-file-list files)
  16589. (org-install-agenda-files-menu)
  16590. (message "Removed file: %s" afile))
  16591. (message "File was not in list: %s (not removed)" afile))))
  16592. (defun org-file-menu-entry (file)
  16593. (vector file (list 'find-file file) t))
  16594. (defun org-check-agenda-file (file)
  16595. "Make sure FILE exists. If not, ask user what to do."
  16596. (when (not (file-exists-p file))
  16597. (message "Non-existent agenda file %s. [R]emove from list or [A]bort?"
  16598. (abbreviate-file-name file))
  16599. (let ((r (downcase (read-char-exclusive))))
  16600. (cond
  16601. ((equal r ?r)
  16602. (org-remove-file file)
  16603. (throw 'nextfile t))
  16604. (t (error "Abort"))))))
  16605. (defun org-get-agenda-file-buffer (file)
  16606. "Get a buffer visiting FILE. If the buffer needs to be created, add
  16607. it to the list of buffers which might be released later."
  16608. (let ((buf (org-find-base-buffer-visiting file)))
  16609. (if buf
  16610. buf ; just return it
  16611. ;; Make a new buffer and remember it
  16612. (setq buf (find-file-noselect file))
  16613. (if buf (push buf org-agenda-new-buffers))
  16614. buf)))
  16615. (defun org-release-buffers (blist)
  16616. "Release all buffers in list, asking the user for confirmation when needed.
  16617. When a buffer is unmodified, it is just killed. When modified, it is saved
  16618. \(if the user agrees) and then killed."
  16619. (let (buf file)
  16620. (while (setq buf (pop blist))
  16621. (setq file (buffer-file-name buf))
  16622. (when (and (buffer-modified-p buf)
  16623. file
  16624. (y-or-n-p (format "Save file %s? " file)))
  16625. (with-current-buffer buf (save-buffer)))
  16626. (kill-buffer buf))))
  16627. (defun org-agenda-prepare-buffers (files)
  16628. "Create buffers for all agenda files, protect archived trees and comments."
  16629. (interactive)
  16630. (let ((pa '(:org-archived t))
  16631. (pc '(:org-comment t))
  16632. (pall '(:org-archived t :org-comment t))
  16633. (inhibit-read-only t)
  16634. (org-inhibit-startup org-agenda-inhibit-startup)
  16635. (rea (concat ":" org-archive-tag ":"))
  16636. file re pos)
  16637. (setq org-tag-alist-for-agenda nil
  16638. org-tag-groups-alist-for-agenda nil)
  16639. (save-excursion
  16640. (save-restriction
  16641. (while (setq file (pop files))
  16642. (catch 'nextfile
  16643. (if (bufferp file)
  16644. (set-buffer file)
  16645. (org-check-agenda-file file)
  16646. (set-buffer (org-get-agenda-file-buffer file)))
  16647. (widen)
  16648. (org-set-regexps-and-options-for-tags)
  16649. (setq pos (point))
  16650. (goto-char (point-min))
  16651. (let ((case-fold-search t))
  16652. (when (search-forward "#+setupfile" nil t)
  16653. ;; Don't set all regexps and options systematically as
  16654. ;; this is only run for setting agenda tags from setup
  16655. ;; file
  16656. (org-set-regexps-and-options)))
  16657. (or (memq 'category org-agenda-ignore-drawer-properties)
  16658. (org-refresh-category-properties))
  16659. (or (memq 'effort org-agenda-ignore-drawer-properties)
  16660. (org-refresh-properties org-effort-property 'org-effort))
  16661. (or (memq 'appt org-agenda-ignore-drawer-properties)
  16662. (org-refresh-properties "APPT_WARNTIME" 'org-appt-warntime))
  16663. (setq org-todo-keywords-for-agenda
  16664. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  16665. (setq org-done-keywords-for-agenda
  16666. (append org-done-keywords-for-agenda org-done-keywords))
  16667. (setq org-todo-keyword-alist-for-agenda
  16668. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  16669. (setq org-drawers-for-agenda
  16670. (append org-drawers-for-agenda org-drawers))
  16671. (setq org-tag-alist-for-agenda
  16672. (org-uniquify
  16673. (append org-tag-alist-for-agenda
  16674. org-tag-alist
  16675. org-tag-persistent-alist)))
  16676. (if org-group-tags
  16677. (setq org-tag-groups-alist-for-agenda
  16678. (org-uniquify-alist
  16679. (append org-tag-groups-alist-for-agenda org-tag-groups-alist))))
  16680. (org-with-silent-modifications
  16681. (save-excursion
  16682. (remove-text-properties (point-min) (point-max) pall)
  16683. (when org-agenda-skip-archived-trees
  16684. (goto-char (point-min))
  16685. (while (re-search-forward rea nil t)
  16686. (if (org-at-heading-p t)
  16687. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  16688. (goto-char (point-min))
  16689. (setq re (format org-heading-keyword-regexp-format
  16690. org-comment-string))
  16691. (while (re-search-forward re nil t)
  16692. (add-text-properties
  16693. (match-beginning 0) (org-end-of-subtree t) pc))))
  16694. (goto-char pos)))))
  16695. (setq org-todo-keywords-for-agenda
  16696. (org-uniquify org-todo-keywords-for-agenda))
  16697. (setq org-todo-keyword-alist-for-agenda
  16698. (org-uniquify org-todo-keyword-alist-for-agenda))))
  16699. ;;;; CDLaTeX minor mode
  16700. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  16701. "Keymap for the minor `org-cdlatex-mode'.")
  16702. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  16703. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  16704. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  16705. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  16706. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  16707. (defvar org-cdlatex-texmathp-advice-is-done nil
  16708. "Flag remembering if we have applied the advice to texmathp already.")
  16709. (define-minor-mode org-cdlatex-mode
  16710. "Toggle the minor `org-cdlatex-mode'.
  16711. This mode supports entering LaTeX environment and math in LaTeX fragments
  16712. in Org-mode.
  16713. \\{org-cdlatex-mode-map}"
  16714. nil " OCDL" nil
  16715. (when org-cdlatex-mode
  16716. (require 'cdlatex)
  16717. (run-hooks 'cdlatex-mode-hook)
  16718. (cdlatex-compute-tables))
  16719. (unless org-cdlatex-texmathp-advice-is-done
  16720. (setq org-cdlatex-texmathp-advice-is-done t)
  16721. (defadvice texmathp (around org-math-always-on activate)
  16722. "Always return t in org-mode buffers.
  16723. This is because we want to insert math symbols without dollars even outside
  16724. the LaTeX math segments. If Orgmode thinks that point is actually inside
  16725. an embedded LaTeX fragment, let texmathp do its job.
  16726. \\[org-cdlatex-mode-map]"
  16727. (interactive)
  16728. (let (p)
  16729. (cond
  16730. ((not (derived-mode-p 'org-mode)) ad-do-it)
  16731. ((eq this-command 'cdlatex-math-symbol)
  16732. (setq ad-return-value t
  16733. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  16734. (t
  16735. (let ((p (org-inside-LaTeX-fragment-p)))
  16736. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  16737. (setq ad-return-value t
  16738. texmathp-why '("Org-mode embedded math" . 0))
  16739. (if p ad-do-it)))))))))
  16740. (defun turn-on-org-cdlatex ()
  16741. "Unconditionally turn on `org-cdlatex-mode'."
  16742. (org-cdlatex-mode 1))
  16743. (defun org-try-cdlatex-tab ()
  16744. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  16745. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  16746. - inside a LaTeX fragment, or
  16747. - after the first word in a line, where an abbreviation expansion could
  16748. insert a LaTeX environment."
  16749. (when org-cdlatex-mode
  16750. (cond
  16751. ;; Before any word on the line: No expansion possible.
  16752. ((save-excursion (skip-chars-backward " \t") (bolp)) nil)
  16753. ;; Just after first word on the line: Expand it. Make sure it
  16754. ;; cannot happen on headlines, though.
  16755. ((save-excursion
  16756. (skip-chars-backward "a-zA-Z0-9*")
  16757. (skip-chars-backward " \t")
  16758. (and (bolp) (not (org-at-heading-p))))
  16759. (cdlatex-tab) t)
  16760. ((org-inside-LaTeX-fragment-p) (cdlatex-tab) t))))
  16761. (defun org-cdlatex-underscore-caret (&optional arg)
  16762. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  16763. Revert to the normal definition outside of these fragments."
  16764. (interactive "P")
  16765. (if (org-inside-LaTeX-fragment-p)
  16766. (call-interactively 'cdlatex-sub-superscript)
  16767. (let (org-cdlatex-mode)
  16768. (call-interactively (key-binding (vector last-input-event))))))
  16769. (defun org-cdlatex-math-modify (&optional arg)
  16770. "Execute `cdlatex-math-modify' in LaTeX fragments.
  16771. Revert to the normal definition outside of these fragments."
  16772. (interactive "P")
  16773. (if (org-inside-LaTeX-fragment-p)
  16774. (call-interactively 'cdlatex-math-modify)
  16775. (let (org-cdlatex-mode)
  16776. (call-interactively (key-binding (vector last-input-event))))))
  16777. ;;;; LaTeX fragments
  16778. (defvar org-latex-regexps
  16779. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  16780. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  16781. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  16782. ("$1" "\\([^$]\\|^\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  16783. ("$" "\\([^$]\\|^\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  16784. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  16785. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
  16786. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
  16787. "Regular expressions for matching embedded LaTeX.")
  16788. (defun org-inside-LaTeX-fragment-p ()
  16789. "Test if point is inside a LaTeX fragment.
  16790. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  16791. sequence appearing also before point.
  16792. Even though the matchers for math are configurable, this function assumes
  16793. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  16794. delimiters are skipped when they have been removed by customization.
  16795. The return value is nil, or a cons cell with the delimiter and the
  16796. position of this delimiter.
  16797. This function does a reasonably good job, but can locally be fooled by
  16798. for example currency specifications. For example it will assume being in
  16799. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  16800. fragments that are properly closed, but during editing, we have to live
  16801. with the uncertainty caused by missing closing delimiters. This function
  16802. looks only before point, not after."
  16803. (catch 'exit
  16804. (let ((pos (point))
  16805. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  16806. (lim (progn
  16807. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  16808. (point)))
  16809. dd-on str (start 0) m re)
  16810. (goto-char pos)
  16811. (when dodollar
  16812. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  16813. re (nth 1 (assoc "$" org-latex-regexps)))
  16814. (while (string-match re str start)
  16815. (cond
  16816. ((= (match-end 0) (length str))
  16817. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  16818. ((= (match-end 0) (- (length str) 5))
  16819. (throw 'exit nil))
  16820. (t (setq start (match-end 0))))))
  16821. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  16822. (goto-char pos)
  16823. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  16824. (and (match-beginning 2) (throw 'exit nil))
  16825. ;; count $$
  16826. (while (re-search-backward "\\$\\$" lim t)
  16827. (setq dd-on (not dd-on)))
  16828. (goto-char pos)
  16829. (if dd-on (cons "$$" m))))))
  16830. (defun org-inside-latex-macro-p ()
  16831. "Is point inside a LaTeX macro or its arguments?"
  16832. (save-match-data
  16833. (org-in-regexp
  16834. "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")))
  16835. (defvar org-latex-fragment-image-overlays nil
  16836. "List of overlays carrying the images of latex fragments.")
  16837. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  16838. (defun org-remove-latex-fragment-image-overlays ()
  16839. "Remove all overlays with LaTeX fragment images in current buffer."
  16840. (mapc 'delete-overlay org-latex-fragment-image-overlays)
  16841. (setq org-latex-fragment-image-overlays nil))
  16842. (defun org-preview-latex-fragment (&optional subtree)
  16843. "Preview the LaTeX fragment at point, or all locally or globally.
  16844. If the cursor is in a LaTeX fragment, create the image and overlay
  16845. it over the source code. If there is no fragment at point, display
  16846. all fragments in the current text, from one headline to the next. With
  16847. prefix SUBTREE, display all fragments in the current subtree. With a
  16848. double prefix arg \\[universal-argument] \\[universal-argument], or when \
  16849. the cursor is before the first headline,
  16850. display all fragments in the buffer.
  16851. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  16852. (interactive "P")
  16853. (unless buffer-file-name
  16854. (user-error "Can't preview LaTeX fragment in a non-file buffer"))
  16855. (when (display-graphic-p)
  16856. (org-remove-latex-fragment-image-overlays)
  16857. (save-excursion
  16858. (save-restriction
  16859. (let (beg end at msg)
  16860. (cond
  16861. ((or (equal subtree '(16))
  16862. (not (save-excursion
  16863. (re-search-backward org-outline-regexp-bol nil t))))
  16864. (setq beg (point-min) end (point-max)
  16865. msg "Creating images for buffer...%s"))
  16866. ((equal subtree '(4))
  16867. (org-back-to-heading)
  16868. (setq beg (point) end (org-end-of-subtree t)
  16869. msg "Creating images for subtree...%s"))
  16870. (t
  16871. (if (setq at (org-inside-LaTeX-fragment-p))
  16872. (goto-char (max (point-min) (- (cdr at) 2)))
  16873. (org-back-to-heading))
  16874. (setq beg (point) end (progn (outline-next-heading) (point))
  16875. msg (if at "Creating image...%s"
  16876. "Creating images for entry...%s"))))
  16877. (message msg "")
  16878. (narrow-to-region beg end)
  16879. (goto-char beg)
  16880. (org-format-latex
  16881. (concat org-latex-preview-ltxpng-directory (file-name-sans-extension
  16882. (file-name-nondirectory
  16883. buffer-file-name)))
  16884. default-directory 'overlays msg at 'forbuffer
  16885. org-latex-create-formula-image-program)
  16886. (message msg "done. Use `C-c C-c' to remove images."))))))
  16887. (defun org-format-latex (prefix &optional dir overlays msg at
  16888. forbuffer processing-type)
  16889. "Replace LaTeX fragments with links to an image, and produce images.
  16890. Some of the options can be changed using the variable
  16891. `org-format-latex-options'."
  16892. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  16893. (let* ((prefixnodir (file-name-nondirectory prefix))
  16894. (absprefix (expand-file-name prefix dir))
  16895. (todir (file-name-directory absprefix))
  16896. (opt org-format-latex-options)
  16897. (optnew org-format-latex-options)
  16898. (matchers (plist-get opt :matchers))
  16899. (re-list org-latex-regexps)
  16900. (cnt 0) txt hash link beg end re e checkdir
  16901. string
  16902. m n block-type block linkfile movefile ov)
  16903. ;; Check the different regular expressions
  16904. (while (setq e (pop re-list))
  16905. (setq m (car e) re (nth 1 e) n (nth 2 e) block-type (nth 3 e)
  16906. block (if block-type "\n\n" ""))
  16907. (when (member m matchers)
  16908. (goto-char (point-min))
  16909. (while (re-search-forward re nil t)
  16910. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  16911. (or (not overlays)
  16912. (not (eq (get-char-property (match-beginning n)
  16913. 'org-overlay-type)
  16914. 'org-latex-overlay))))
  16915. (cond
  16916. ((eq processing-type 'verbatim))
  16917. ((eq processing-type 'mathjax)
  16918. ;; Prepare for MathJax processing.
  16919. (setq string (match-string n))
  16920. (when (member m '("$" "$1"))
  16921. (save-excursion
  16922. (delete-region (match-beginning n) (match-end n))
  16923. (goto-char (match-beginning n))
  16924. (insert (concat "\\(" (substring string 1 -1) "\\)")))))
  16925. ((or (eq processing-type 'dvipng)
  16926. (eq processing-type 'imagemagick))
  16927. ;; Process to an image.
  16928. (setq txt (match-string n)
  16929. beg (match-beginning n) end (match-end n)
  16930. cnt (1+ cnt))
  16931. (let ((face (face-at-point))
  16932. (fg (plist-get opt :foreground))
  16933. (bg (plist-get opt :background))
  16934. ;; Ensure full list is printed.
  16935. print-length print-level)
  16936. (when forbuffer
  16937. ;; Get the colors from the face at point.
  16938. (goto-char beg)
  16939. (when (eq fg 'auto)
  16940. (setq fg (face-attribute face :foreground nil 'default)))
  16941. (when (eq bg 'auto)
  16942. (setq bg (face-attribute face :background nil 'default)))
  16943. (setq optnew (copy-sequence opt))
  16944. (plist-put optnew :foreground fg)
  16945. (plist-put optnew :background bg))
  16946. (setq hash (sha1 (prin1-to-string
  16947. (list org-format-latex-header
  16948. org-latex-default-packages-alist
  16949. org-latex-packages-alist
  16950. org-format-latex-options
  16951. forbuffer txt fg bg)))
  16952. linkfile (format "%s_%s.png" prefix hash)
  16953. movefile (format "%s_%s.png" absprefix hash)))
  16954. (setq link (concat block "[[file:" linkfile "]]" block))
  16955. (if msg (message msg cnt))
  16956. (goto-char beg)
  16957. (unless checkdir ; Ensure the directory exists.
  16958. (setq checkdir t)
  16959. (or (file-directory-p todir) (make-directory todir t)))
  16960. (unless (file-exists-p movefile)
  16961. (org-create-formula-image
  16962. txt movefile optnew forbuffer processing-type))
  16963. (if overlays
  16964. (progn
  16965. (mapc (lambda (o)
  16966. (if (eq (overlay-get o 'org-overlay-type)
  16967. 'org-latex-overlay)
  16968. (delete-overlay o)))
  16969. (overlays-in beg end))
  16970. (setq ov (make-overlay beg end))
  16971. (overlay-put ov 'org-overlay-type 'org-latex-overlay)
  16972. (if (featurep 'xemacs)
  16973. (progn
  16974. (overlay-put ov 'invisible t)
  16975. (overlay-put
  16976. ov 'end-glyph
  16977. (make-glyph (vector 'png :file movefile))))
  16978. (overlay-put
  16979. ov 'display
  16980. (list 'image :type 'png :file movefile :ascent 'center)))
  16981. (push ov org-latex-fragment-image-overlays)
  16982. (goto-char end))
  16983. (delete-region beg end)
  16984. (insert (org-add-props link
  16985. (list 'org-latex-src
  16986. (replace-regexp-in-string
  16987. "\"" "" txt)
  16988. 'org-latex-src-embed-type
  16989. (if block-type 'paragraph 'character))))))
  16990. ((eq processing-type 'mathml)
  16991. ;; Process to MathML
  16992. (unless (save-match-data (org-format-latex-mathml-available-p))
  16993. (user-error "LaTeX to MathML converter not configured"))
  16994. (setq txt (match-string n)
  16995. beg (match-beginning n) end (match-end n)
  16996. cnt (1+ cnt))
  16997. (if msg (message msg cnt))
  16998. (goto-char beg)
  16999. (delete-region beg end)
  17000. (insert (org-format-latex-as-mathml
  17001. txt block-type prefix dir)))
  17002. (t
  17003. (error "Unknown conversion type %s for LaTeX fragments"
  17004. processing-type)))))))))
  17005. (defun org-create-math-formula (latex-frag &optional mathml-file)
  17006. "Convert LATEX-FRAG to MathML and store it in MATHML-FILE.
  17007. Use `org-latex-to-mathml-convert-command'. If the conversion is
  17008. sucessful, return the portion between \"<math...> </math>\"
  17009. elements otherwise return nil. When MATHML-FILE is specified,
  17010. write the results in to that file. When invoked as an
  17011. interactive command, prompt for LATEX-FRAG, with initial value
  17012. set to the current active region and echo the results for user
  17013. inspection."
  17014. (interactive (list (let ((frag (when (org-region-active-p)
  17015. (buffer-substring-no-properties
  17016. (region-beginning) (region-end)))))
  17017. (read-string "LaTeX Fragment: " frag nil frag))))
  17018. (unless latex-frag (error "Invalid LaTeX fragment"))
  17019. (let* ((tmp-in-file (file-relative-name
  17020. (make-temp-name (expand-file-name "ltxmathml-in"))))
  17021. (ignore (write-region latex-frag nil tmp-in-file))
  17022. (tmp-out-file (file-relative-name
  17023. (make-temp-name (expand-file-name "ltxmathml-out"))))
  17024. (cmd (format-spec
  17025. org-latex-to-mathml-convert-command
  17026. `((?j . ,(shell-quote-argument
  17027. (expand-file-name org-latex-to-mathml-jar-file)))
  17028. (?I . ,(shell-quote-argument tmp-in-file))
  17029. (?o . ,(shell-quote-argument tmp-out-file)))))
  17030. mathml shell-command-output)
  17031. (when (org-called-interactively-p 'any)
  17032. (unless (org-format-latex-mathml-available-p)
  17033. (user-error "LaTeX to MathML converter not configured")))
  17034. (message "Running %s" cmd)
  17035. (setq shell-command-output (shell-command-to-string cmd))
  17036. (setq mathml
  17037. (when (file-readable-p tmp-out-file)
  17038. (with-current-buffer (find-file-noselect tmp-out-file t)
  17039. (goto-char (point-min))
  17040. (when (re-search-forward
  17041. (concat
  17042. (regexp-quote
  17043. "<math xmlns=\"http://www.w3.org/1998/Math/MathML\">")
  17044. "\\(.\\|\n\\)*"
  17045. (regexp-quote "</math>")) nil t)
  17046. (prog1 (match-string 0) (kill-buffer))))))
  17047. (cond
  17048. (mathml
  17049. (setq mathml
  17050. (concat "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" mathml))
  17051. (when mathml-file
  17052. (write-region mathml nil mathml-file))
  17053. (when (org-called-interactively-p 'any)
  17054. (message mathml)))
  17055. ((message "LaTeX to MathML conversion failed")
  17056. (message shell-command-output)))
  17057. (delete-file tmp-in-file)
  17058. (when (file-exists-p tmp-out-file)
  17059. (delete-file tmp-out-file))
  17060. mathml))
  17061. (defun org-format-latex-as-mathml (latex-frag latex-frag-type
  17062. prefix &optional dir)
  17063. "Use `org-create-math-formula' but check local cache first."
  17064. (let* ((absprefix (expand-file-name prefix dir))
  17065. (print-length nil) (print-level nil)
  17066. (formula-id (concat
  17067. "formula-"
  17068. (sha1
  17069. (prin1-to-string
  17070. (list latex-frag
  17071. org-latex-to-mathml-convert-command)))))
  17072. (formula-cache (format "%s-%s.mathml" absprefix formula-id))
  17073. (formula-cache-dir (file-name-directory formula-cache)))
  17074. (unless (file-directory-p formula-cache-dir)
  17075. (make-directory formula-cache-dir t))
  17076. (unless (file-exists-p formula-cache)
  17077. (org-create-math-formula latex-frag formula-cache))
  17078. (if (file-exists-p formula-cache)
  17079. ;; Successful conversion. Return the link to MathML file.
  17080. (org-add-props
  17081. (format "[[file:%s]]" (file-relative-name formula-cache dir))
  17082. (list 'org-latex-src (replace-regexp-in-string "\"" "" latex-frag)
  17083. 'org-latex-src-embed-type (if latex-frag-type
  17084. 'paragraph 'character)))
  17085. ;; Failed conversion. Return the LaTeX fragment verbatim
  17086. latex-frag)))
  17087. (defun org-create-formula-image (string tofile options buffer &optional type)
  17088. "Create an image from LaTeX source using dvipng or convert.
  17089. This function calls either `org-create-formula-image-with-dvipng'
  17090. or `org-create-formula-image-with-imagemagick' depending on the
  17091. value of `org-latex-create-formula-image-program' or on the value
  17092. of the optional TYPE variable.
  17093. Note: ultimately these two function should be combined as they
  17094. share a good deal of logic."
  17095. (org-check-external-command
  17096. "latex" "needed to convert LaTeX fragments to images")
  17097. (funcall
  17098. (case (or type org-latex-create-formula-image-program)
  17099. ('dvipng
  17100. (org-check-external-command
  17101. "dvipng" "needed to convert LaTeX fragments to images")
  17102. #'org-create-formula-image-with-dvipng)
  17103. ('imagemagick
  17104. (org-check-external-command
  17105. "convert" "you need to install imagemagick")
  17106. #'org-create-formula-image-with-imagemagick)
  17107. (t (error
  17108. "Invalid value of `org-latex-create-formula-image-program'")))
  17109. string tofile options buffer))
  17110. (declare-function org-export-get-backend "ox" (name))
  17111. (declare-function org-export--get-global-options "ox" (&optional backend))
  17112. (declare-function org-export--get-inbuffer-options "ox" (&optional backend))
  17113. (declare-function org-latex-guess-inputenc "ox-latex" (header))
  17114. (declare-function org-latex-guess-babel-language "ox-latex" (header info))
  17115. (defun org-create-formula--latex-header ()
  17116. "Return LaTeX header appropriate for previewing a LaTeX snippet."
  17117. (let ((info (org-combine-plists (org-export--get-global-options
  17118. (org-export-get-backend 'latex))
  17119. (org-export--get-inbuffer-options
  17120. (org-export-get-backend 'latex)))))
  17121. (org-latex-guess-babel-language
  17122. (org-latex-guess-inputenc
  17123. (org-splice-latex-header
  17124. org-format-latex-header
  17125. org-latex-default-packages-alist
  17126. org-latex-packages-alist t
  17127. (plist-get info :latex-header)))
  17128. info)))
  17129. ;; This function borrows from Ganesh Swami's latex2png.el
  17130. (defun org-create-formula-image-with-dvipng (string tofile options buffer)
  17131. "This calls dvipng."
  17132. (require 'ox-latex)
  17133. (let* ((tmpdir (if (featurep 'xemacs)
  17134. (temp-directory)
  17135. temporary-file-directory))
  17136. (texfilebase (make-temp-name
  17137. (expand-file-name "orgtex" tmpdir)))
  17138. (texfile (concat texfilebase ".tex"))
  17139. (dvifile (concat texfilebase ".dvi"))
  17140. (pngfile (concat texfilebase ".png"))
  17141. (fnh (if (featurep 'xemacs)
  17142. (font-height (face-font 'default))
  17143. (face-attribute 'default :height nil)))
  17144. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  17145. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  17146. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  17147. "Black"))
  17148. (bg (or (plist-get options (if buffer :background :html-background))
  17149. "Transparent")))
  17150. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground))
  17151. (unless (string= fg "Transparent") (setq fg (org-dvipng-color-format fg))))
  17152. (if (eq bg 'default) (setq bg (org-dvipng-color :background))
  17153. (unless (string= bg "Transparent") (setq bg (org-dvipng-color-format bg))))
  17154. (let ((latex-header (org-create-formula--latex-header)))
  17155. (with-temp-file texfile
  17156. (insert latex-header)
  17157. (insert "\n\\begin{document}\n" string "\n\\end{document}\n")))
  17158. (let ((dir default-directory))
  17159. (condition-case nil
  17160. (progn
  17161. (cd tmpdir)
  17162. (call-process "latex" nil nil nil texfile))
  17163. (error nil))
  17164. (cd dir))
  17165. (if (not (file-exists-p dvifile))
  17166. (progn (message "Failed to create dvi file from %s" texfile) nil)
  17167. (condition-case nil
  17168. (if (featurep 'xemacs)
  17169. (call-process "dvipng" nil nil nil
  17170. "-fg" fg "-bg" bg
  17171. "-T" "tight"
  17172. "-o" pngfile
  17173. dvifile)
  17174. (call-process "dvipng" nil nil nil
  17175. "-fg" fg "-bg" bg
  17176. "-D" dpi
  17177. ;;"-x" scale "-y" scale
  17178. "-T" "tight"
  17179. "-o" pngfile
  17180. dvifile))
  17181. (error nil))
  17182. (if (not (file-exists-p pngfile))
  17183. (if org-format-latex-signal-error
  17184. (error "Failed to create png file from %s" texfile)
  17185. (message "Failed to create png file from %s" texfile)
  17186. nil)
  17187. ;; Use the requested file name and clean up
  17188. (copy-file pngfile tofile 'replace)
  17189. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png" ".out") do
  17190. (if (file-exists-p (concat texfilebase e))
  17191. (delete-file (concat texfilebase e))))
  17192. pngfile))))
  17193. (declare-function org-latex-compile "ox-latex" (texfile &optional snippet))
  17194. (defun org-create-formula-image-with-imagemagick (string tofile options buffer)
  17195. "This calls convert, which is included into imagemagick."
  17196. (require 'ox-latex)
  17197. (let* ((tmpdir (if (featurep 'xemacs)
  17198. (temp-directory)
  17199. temporary-file-directory))
  17200. (texfilebase (make-temp-name
  17201. (expand-file-name "orgtex" tmpdir)))
  17202. (texfile (concat texfilebase ".tex"))
  17203. (pdffile (concat texfilebase ".pdf"))
  17204. (pngfile (concat texfilebase ".png"))
  17205. (fnh (if (featurep 'xemacs)
  17206. (font-height (face-font 'default))
  17207. (face-attribute 'default :height nil)))
  17208. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  17209. (dpi (number-to-string (* scale (floor (if buffer fnh 120.)))))
  17210. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  17211. "black"))
  17212. (bg (or (plist-get options (if buffer :background :html-background))
  17213. "white")))
  17214. (if (eq fg 'default) (setq fg (org-latex-color :foreground))
  17215. (setq fg (org-latex-color-format fg)))
  17216. (if (eq bg 'default) (setq bg (org-latex-color :background))
  17217. (setq bg (org-latex-color-format
  17218. (if (string= bg "Transparent") "white" bg))))
  17219. (let ((latex-header (org-create-formula--latex-header)))
  17220. (with-temp-file texfile
  17221. (insert latex-header)
  17222. (insert "\n\\begin{document}\n"
  17223. "\\definecolor{fg}{rgb}{" fg "}\n"
  17224. "\\definecolor{bg}{rgb}{" bg "}\n"
  17225. "\n\\pagecolor{bg}\n"
  17226. "\n{\\color{fg}\n"
  17227. string
  17228. "\n}\n"
  17229. "\n\\end{document}\n")))
  17230. (org-latex-compile texfile t)
  17231. (if (not (file-exists-p pdffile))
  17232. (progn (message "Failed to create pdf file from %s" texfile) nil)
  17233. (condition-case nil
  17234. (if (featurep 'xemacs)
  17235. (call-process "convert" nil nil nil
  17236. "-density" "96"
  17237. "-trim"
  17238. "-antialias"
  17239. pdffile
  17240. "-quality" "100"
  17241. ;; "-sharpen" "0x1.0"
  17242. pngfile)
  17243. (call-process "convert" nil nil nil
  17244. "-density" dpi
  17245. "-trim"
  17246. "-antialias"
  17247. pdffile
  17248. "-quality" "100"
  17249. ;; "-sharpen" "0x1.0"
  17250. pngfile))
  17251. (error nil))
  17252. (if (not (file-exists-p pngfile))
  17253. (if org-format-latex-signal-error
  17254. (error "Failed to create png file from %s" texfile)
  17255. (message "Failed to create png file from %s" texfile)
  17256. nil)
  17257. ;; Use the requested file name and clean up
  17258. (copy-file pngfile tofile 'replace)
  17259. (loop for e in '(".pdf" ".tex" ".aux" ".log" ".png") do
  17260. (if (file-exists-p (concat texfilebase e))
  17261. (delete-file (concat texfilebase e))))
  17262. pngfile))))
  17263. (defun org-splice-latex-header (tpl def-pkg pkg snippets-p &optional extra)
  17264. "Fill a LaTeX header template TPL.
  17265. In the template, the following place holders will be recognized:
  17266. [DEFAULT-PACKAGES] \\usepackage statements for DEF-PKG
  17267. [NO-DEFAULT-PACKAGES] do not include DEF-PKG
  17268. [PACKAGES] \\usepackage statements for PKG
  17269. [NO-PACKAGES] do not include PKG
  17270. [EXTRA] the string EXTRA
  17271. [NO-EXTRA] do not include EXTRA
  17272. For backward compatibility, if both the positive and the negative place
  17273. holder is missing, the positive one (without the \"NO-\") will be
  17274. assumed to be present at the end of the template.
  17275. DEF-PKG and PKG are assumed to be alists of options/packagename lists.
  17276. EXTRA is a string.
  17277. SNIPPETS-P indicates if this is run to create snippet images for HTML."
  17278. (let (rpl (end ""))
  17279. (if (string-match "^[ \t]*\\[\\(NO-\\)?DEFAULT-PACKAGES\\][ \t]*\n?" tpl)
  17280. (setq rpl (if (or (match-end 1) (not def-pkg))
  17281. "" (org-latex-packages-to-string def-pkg snippets-p t))
  17282. tpl (replace-match rpl t t tpl))
  17283. (if def-pkg (setq end (org-latex-packages-to-string def-pkg snippets-p))))
  17284. (if (string-match "\\[\\(NO-\\)?PACKAGES\\][ \t]*\n?" tpl)
  17285. (setq rpl (if (or (match-end 1) (not pkg))
  17286. "" (org-latex-packages-to-string pkg snippets-p t))
  17287. tpl (replace-match rpl t t tpl))
  17288. (if pkg (setq end
  17289. (concat end "\n"
  17290. (org-latex-packages-to-string pkg snippets-p)))))
  17291. (if (string-match "\\[\\(NO-\\)?EXTRA\\][ \t]*\n?" tpl)
  17292. (setq rpl (if (or (match-end 1) (not extra))
  17293. "" (concat extra "\n"))
  17294. tpl (replace-match rpl t t tpl))
  17295. (if (and extra (string-match "\\S-" extra))
  17296. (setq end (concat end "\n" extra))))
  17297. (if (string-match "\\S-" end)
  17298. (concat tpl "\n" end)
  17299. tpl)))
  17300. (defun org-latex-packages-to-string (pkg &optional snippets-p newline)
  17301. "Turn an alist of packages into a string with the \\usepackage macros."
  17302. (setq pkg (mapconcat (lambda(p)
  17303. (cond
  17304. ((stringp p) p)
  17305. ((and snippets-p (>= (length p) 3) (not (nth 2 p)))
  17306. (format "%% Package %s omitted" (cadr p)))
  17307. ((equal "" (car p))
  17308. (format "\\usepackage{%s}" (cadr p)))
  17309. (t
  17310. (format "\\usepackage[%s]{%s}"
  17311. (car p) (cadr p)))))
  17312. pkg
  17313. "\n"))
  17314. (if newline (concat pkg "\n") pkg))
  17315. (defun org-dvipng-color (attr)
  17316. "Return a RGB color specification for dvipng."
  17317. (apply 'format "rgb %s %s %s"
  17318. (mapcar 'org-normalize-color
  17319. (if (featurep 'xemacs)
  17320. (color-rgb-components
  17321. (face-property 'default
  17322. (cond ((eq attr :foreground) 'foreground)
  17323. ((eq attr :background) 'background))))
  17324. (color-values (face-attribute 'default attr nil))))))
  17325. (defun org-dvipng-color-format (color-name)
  17326. "Convert COLOR-NAME to a RGB color value for dvipng."
  17327. (apply 'format "rgb %s %s %s"
  17328. (mapcar 'org-normalize-color
  17329. (color-values color-name))))
  17330. (defun org-latex-color (attr)
  17331. "Return a RGB color for the LaTeX color package."
  17332. (apply 'format "%s,%s,%s"
  17333. (mapcar 'org-normalize-color
  17334. (if (featurep 'xemacs)
  17335. (color-rgb-components
  17336. (face-property 'default
  17337. (cond ((eq attr :foreground) 'foreground)
  17338. ((eq attr :background) 'background))))
  17339. (color-values (face-attribute 'default attr nil))))))
  17340. (defun org-latex-color-format (color-name)
  17341. "Convert COLOR-NAME to a RGB color value."
  17342. (apply 'format "%s,%s,%s"
  17343. (mapcar 'org-normalize-color
  17344. (color-values color-name))))
  17345. (defun org-normalize-color (value)
  17346. "Return string to be used as color value for an RGB component."
  17347. (format "%g" (/ value 65535.0)))
  17348. ;; Image display
  17349. (defvar org-inline-image-overlays nil)
  17350. (make-variable-buffer-local 'org-inline-image-overlays)
  17351. (defun org-toggle-inline-images (&optional include-linked)
  17352. "Toggle the display of inline images.
  17353. INCLUDE-LINKED is passed to `org-display-inline-images'."
  17354. (interactive "P")
  17355. (if org-inline-image-overlays
  17356. (progn
  17357. (org-remove-inline-images)
  17358. (message "Inline image display turned off"))
  17359. (org-display-inline-images include-linked)
  17360. (if (and (org-called-interactively-p)
  17361. org-inline-image-overlays)
  17362. (message "%d images displayed inline"
  17363. (length org-inline-image-overlays))
  17364. (message "No images to display inline"))))
  17365. (defun org-redisplay-inline-images ()
  17366. "Refresh the display of inline images."
  17367. (interactive)
  17368. (if (not org-inline-image-overlays)
  17369. (org-toggle-inline-images)
  17370. (org-toggle-inline-images)
  17371. (org-toggle-inline-images)))
  17372. (defun org-display-inline-images (&optional include-linked refresh beg end)
  17373. "Display inline images.
  17374. Normally only links without a description part are inlined, because this
  17375. is how it will work for export. When INCLUDE-LINKED is set, also links
  17376. with a description part will be inlined. This can be nice for a quick
  17377. look at those images, but it does not reflect what exported files will look
  17378. like.
  17379. When REFRESH is set, refresh existing images between BEG and END.
  17380. This will create new image displays only if necessary.
  17381. BEG and END default to the buffer boundaries."
  17382. (interactive "P")
  17383. (when (display-graphic-p)
  17384. (unless refresh
  17385. (org-remove-inline-images)
  17386. (if (fboundp 'clear-image-cache) (clear-image-cache)))
  17387. (save-excursion
  17388. (save-restriction
  17389. (widen)
  17390. (setq beg (or beg (point-min)) end (or end (point-max)))
  17391. (goto-char beg)
  17392. (let ((re (concat "\\[\\[\\(\\(file:\\)\\|\\([./~]\\)\\)\\([^]\n]+?"
  17393. (substring (org-image-file-name-regexp) 0 -2)
  17394. "\\)\\]" (if include-linked "" "\\]")))
  17395. (case-fold-search t)
  17396. old file ov img type attrwidth width)
  17397. (while (re-search-forward re end t)
  17398. (setq old (get-char-property-and-overlay (match-beginning 1)
  17399. 'org-image-overlay)
  17400. file (expand-file-name
  17401. (concat (or (match-string 3) "") (match-string 4))))
  17402. (when (image-type-available-p 'imagemagick)
  17403. (setq attrwidth (if (or (listp org-image-actual-width)
  17404. (null org-image-actual-width))
  17405. (save-excursion
  17406. (save-match-data
  17407. (when (re-search-backward
  17408. "#\\+attr.*:width[ \t]+\\([^ ]+\\)"
  17409. (save-excursion
  17410. (re-search-backward "^[ \t]*$\\|\\`" nil t)) t)
  17411. (string-to-number (match-string 1))))))
  17412. width (cond ((eq org-image-actual-width t) nil)
  17413. ((null org-image-actual-width) attrwidth)
  17414. ((numberp org-image-actual-width)
  17415. org-image-actual-width)
  17416. ((listp org-image-actual-width)
  17417. (or attrwidth (car org-image-actual-width))))
  17418. type (if width 'imagemagick)))
  17419. (when (file-exists-p file)
  17420. (if (and (car-safe old) refresh)
  17421. (image-refresh (overlay-get (cdr old) 'display))
  17422. (setq img (save-match-data (create-image file type nil :width width)))
  17423. (when img
  17424. (setq ov (make-overlay (match-beginning 0) (match-end 0)))
  17425. (overlay-put ov 'display img)
  17426. (overlay-put ov 'face 'default)
  17427. (overlay-put ov 'org-image-overlay t)
  17428. (overlay-put ov 'modification-hooks
  17429. (list 'org-display-inline-remove-overlay))
  17430. (push ov org-inline-image-overlays))))))))))
  17431. (define-obsolete-function-alias
  17432. 'org-display-inline-modification-hook 'org-display-inline-remove-overlay "24.3")
  17433. (defun org-display-inline-remove-overlay (ov after beg end &optional len)
  17434. "Remove inline-display overlay if a corresponding region is modified."
  17435. (let ((inhibit-modification-hooks t))
  17436. (when (and ov after)
  17437. (delete ov org-inline-image-overlays)
  17438. (delete-overlay ov))))
  17439. (defun org-remove-inline-images ()
  17440. "Remove inline display of images."
  17441. (interactive)
  17442. (mapc 'delete-overlay org-inline-image-overlays)
  17443. (setq org-inline-image-overlays nil))
  17444. ;;;; Key bindings
  17445. ;; Outline functions from `outline-mode-prefix-map'
  17446. ;; that can be remapped in Org:
  17447. (define-key org-mode-map [remap outline-mark-subtree] 'org-mark-subtree)
  17448. (define-key org-mode-map [remap show-subtree] 'org-show-subtree)
  17449. (define-key org-mode-map [remap outline-forward-same-level]
  17450. 'org-forward-heading-same-level)
  17451. (define-key org-mode-map [remap outline-backward-same-level]
  17452. 'org-backward-heading-same-level)
  17453. (define-key org-mode-map [remap show-branches]
  17454. 'org-kill-note-or-show-branches)
  17455. (define-key org-mode-map [remap outline-promote] 'org-promote-subtree)
  17456. (define-key org-mode-map [remap outline-demote] 'org-demote-subtree)
  17457. (define-key org-mode-map [remap outline-insert-heading] 'org-ctrl-c-ret)
  17458. ;; Outline functions from `outline-mode-prefix-map' that can not
  17459. ;; be remapped in Org:
  17460. ;;
  17461. ;; - the column "key binding" shows whether the Outline function is still
  17462. ;; available in Org mode on the same key that it has been bound to in
  17463. ;; Outline mode:
  17464. ;; - "overridden": key used for a different functionality in Org mode
  17465. ;; - else: key still bound to the same Outline function in Org mode
  17466. ;;
  17467. ;; | Outline function | key binding | Org replacement |
  17468. ;; |------------------------------------+-------------+-----------------------|
  17469. ;; | `outline-next-visible-heading' | `C-c C-n' | still same function |
  17470. ;; | `outline-previous-visible-heading' | `C-c C-p' | still same function |
  17471. ;; | `outline-up-heading' | `C-c C-u' | still same function |
  17472. ;; | `outline-move-subtree-up' | overridden | better: org-shiftup |
  17473. ;; | `outline-move-subtree-down' | overridden | better: org-shiftdown |
  17474. ;; | `show-entry' | overridden | no replacement |
  17475. ;; | `show-children' | `C-c C-i' | visibility cycling |
  17476. ;; | `show-branches' | `C-c C-k' | still same function |
  17477. ;; | `show-subtree' | overridden | visibility cycling |
  17478. ;; | `show-all' | overridden | no replacement |
  17479. ;; | `hide-subtree' | overridden | visibility cycling |
  17480. ;; | `hide-body' | overridden | no replacement |
  17481. ;; | `hide-entry' | overridden | visibility cycling |
  17482. ;; | `hide-leaves' | overridden | no replacement |
  17483. ;; | `hide-sublevels' | overridden | no replacement |
  17484. ;; | `hide-other' | overridden | no replacement |
  17485. ;; Make `C-c C-x' a prefix key
  17486. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  17487. ;; TAB key with modifiers
  17488. (org-defkey org-mode-map "\C-i" 'org-cycle)
  17489. (org-defkey org-mode-map [(tab)] 'org-cycle)
  17490. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  17491. (org-defkey org-mode-map "\M-\t" 'pcomplete)
  17492. ;; The following line is necessary under Suse GNU/Linux
  17493. (unless (featurep 'xemacs)
  17494. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  17495. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  17496. (define-key org-mode-map [backtab] 'org-shifttab)
  17497. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  17498. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  17499. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  17500. ;; Cursor keys with modifiers
  17501. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  17502. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  17503. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  17504. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  17505. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  17506. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  17507. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  17508. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  17509. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  17510. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  17511. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  17512. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  17513. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  17514. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  17515. (org-defkey org-mode-map [(control shift up)] 'org-shiftcontrolup)
  17516. (org-defkey org-mode-map [(control shift down)] 'org-shiftcontroldown)
  17517. ;; Babel keys
  17518. (define-key org-mode-map org-babel-key-prefix org-babel-map)
  17519. (mapc (lambda (pair)
  17520. (define-key org-babel-map (car pair) (cdr pair)))
  17521. org-babel-key-bindings)
  17522. ;;; Extra keys for tty access.
  17523. ;; We only set them when really needed because otherwise the
  17524. ;; menus don't show the simple keys
  17525. (when (or org-use-extra-keys
  17526. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  17527. (not window-system))
  17528. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  17529. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  17530. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  17531. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  17532. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  17533. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  17534. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  17535. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  17536. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  17537. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  17538. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  17539. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  17540. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  17541. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  17542. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  17543. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  17544. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  17545. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  17546. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  17547. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  17548. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  17549. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
  17550. (org-defkey org-mode-map [?\e (tab)] 'pcomplete)
  17551. (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
  17552. (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
  17553. (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
  17554. (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
  17555. (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
  17556. ;; All the other keys
  17557. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  17558. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  17559. (if (boundp 'narrow-map)
  17560. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  17561. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  17562. (if (boundp 'narrow-map)
  17563. (org-defkey narrow-map "b" 'org-narrow-to-block)
  17564. (org-defkey org-mode-map "\C-xnb" 'org-narrow-to-block))
  17565. (if (boundp 'narrow-map)
  17566. (org-defkey narrow-map "e" 'org-narrow-to-element)
  17567. (org-defkey org-mode-map "\C-xne" 'org-narrow-to-element))
  17568. (org-defkey org-mode-map "\C-\M-t" 'org-transpose-element)
  17569. (org-defkey org-mode-map "\M-}" 'org-forward-element)
  17570. (org-defkey org-mode-map "\M-{" 'org-backward-element)
  17571. (org-defkey org-mode-map "\C-c\C-^" 'org-up-element)
  17572. (org-defkey org-mode-map "\C-c\C-_" 'org-down-element)
  17573. (org-defkey org-mode-map "\C-c\C-f" 'org-forward-heading-same-level)
  17574. (org-defkey org-mode-map "\C-c\C-b" 'org-backward-heading-same-level)
  17575. (org-defkey org-mode-map "\C-c\M-f" 'org-next-block)
  17576. (org-defkey org-mode-map "\C-c\M-b" 'org-previous-block)
  17577. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  17578. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  17579. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
  17580. (org-defkey org-mode-map "\C-c\C-xd" 'org-insert-drawer)
  17581. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  17582. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  17583. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  17584. (org-defkey org-mode-map "\C-c\C-xq" 'org-toggle-tags-groups)
  17585. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  17586. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  17587. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  17588. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  17589. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  17590. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  17591. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  17592. (org-defkey org-mode-map "\C-c\M-w" 'org-copy)
  17593. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  17594. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  17595. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  17596. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  17597. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  17598. (org-defkey org-mode-map "\C-c\C-xv" 'org-copy-visible)
  17599. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  17600. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  17601. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  17602. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  17603. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  17604. (org-defkey org-mode-map "\C-c\C-\M-l" 'org-insert-all-links)
  17605. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  17606. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  17607. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  17608. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  17609. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  17610. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  17611. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  17612. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  17613. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  17614. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  17615. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  17616. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  17617. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  17618. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  17619. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  17620. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  17621. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  17622. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  17623. (org-defkey org-mode-map "\C-c^" 'org-sort)
  17624. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  17625. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  17626. (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
  17627. (org-defkey org-mode-map [remap open-line] 'org-open-line)
  17628. (org-defkey org-mode-map [remap forward-paragraph] 'org-forward-paragraph)
  17629. (org-defkey org-mode-map [remap backward-paragraph] 'org-backward-paragraph)
  17630. (org-defkey org-mode-map "\C-m" 'org-return)
  17631. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  17632. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  17633. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  17634. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  17635. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  17636. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  17637. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  17638. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  17639. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  17640. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  17641. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  17642. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  17643. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  17644. (org-defkey org-mode-map "\C-c\C-e" 'org-export-dispatch)
  17645. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  17646. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  17647. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  17648. (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
  17649. (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
  17650. (org-defkey org-mode-map "\C-c@" 'org-mark-subtree)
  17651. (org-defkey org-mode-map "\M-h" 'org-mark-element)
  17652. (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
  17653. ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
  17654. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  17655. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  17656. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  17657. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  17658. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  17659. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-in-last)
  17660. (org-defkey org-mode-map "\C-c\C-x\C-z" 'org-resolve-clocks)
  17661. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  17662. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  17663. (org-defkey org-mode-map "\C-c\C-x\C-q" 'org-clock-cancel)
  17664. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  17665. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  17666. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  17667. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  17668. (org-defkey org-mode-map "\C-c\C-x\C-v" 'org-toggle-inline-images)
  17669. (org-defkey org-mode-map "\C-c\C-x\C-\M-v" 'org-redisplay-inline-images)
  17670. (org-defkey org-mode-map "\C-c\C-x\\" 'org-toggle-pretty-entities)
  17671. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  17672. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  17673. (org-defkey org-mode-map "\C-c\C-xP" 'org-set-property-and-value)
  17674. (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
  17675. (org-defkey org-mode-map "\C-c\C-xE" 'org-inc-effort)
  17676. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  17677. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  17678. (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
  17679. (org-defkey org-mode-map [(control ?c) (control ?x) ?\:] 'org-timer-cancel-timer)
  17680. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  17681. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  17682. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  17683. (org-defkey org-mode-map "\C-c\C-x_" 'org-timer-stop)
  17684. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  17685. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  17686. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  17687. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  17688. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  17689. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  17690. (when (featurep 'xemacs)
  17691. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  17692. (defconst org-speed-commands-default
  17693. '(
  17694. ("Outline Navigation")
  17695. ("n" . (org-speed-move-safe 'outline-next-visible-heading))
  17696. ("p" . (org-speed-move-safe 'outline-previous-visible-heading))
  17697. ("f" . (org-speed-move-safe 'org-forward-heading-same-level))
  17698. ("b" . (org-speed-move-safe 'org-backward-heading-same-level))
  17699. ("F" . org-next-block)
  17700. ("B" . org-previous-block)
  17701. ("u" . (org-speed-move-safe 'outline-up-heading))
  17702. ("j" . org-goto)
  17703. ("g" . (org-refile t))
  17704. ("Outline Visibility")
  17705. ("c" . org-cycle)
  17706. ("C" . org-shifttab)
  17707. (" " . org-display-outline-path)
  17708. ("s" . org-narrow-to-subtree)
  17709. ("=" . org-columns)
  17710. ("Outline Structure Editing")
  17711. ("U" . org-shiftmetaup)
  17712. ("D" . org-shiftmetadown)
  17713. ("r" . org-metaright)
  17714. ("l" . org-metaleft)
  17715. ("R" . org-shiftmetaright)
  17716. ("L" . org-shiftmetaleft)
  17717. ("i" . (progn (forward-char 1) (call-interactively
  17718. 'org-insert-heading-respect-content)))
  17719. ("^" . org-sort)
  17720. ("w" . org-refile)
  17721. ("a" . org-archive-subtree-default-with-confirmation)
  17722. ("@" . org-mark-subtree)
  17723. ("#" . org-toggle-comment)
  17724. ("Clock Commands")
  17725. ("I" . org-clock-in)
  17726. ("O" . org-clock-out)
  17727. ("Meta Data Editing")
  17728. ("t" . org-todo)
  17729. ("," . (org-priority))
  17730. ("0" . (org-priority ?\ ))
  17731. ("1" . (org-priority ?A))
  17732. ("2" . (org-priority ?B))
  17733. ("3" . (org-priority ?C))
  17734. (":" . org-set-tags-command)
  17735. ("e" . org-set-effort)
  17736. ("E" . org-inc-effort)
  17737. ("W" . (lambda(m) (interactive "sMinutes before warning: ")
  17738. (org-entry-put (point) "APPT_WARNTIME" m)))
  17739. ("Agenda Views etc")
  17740. ("v" . org-agenda)
  17741. ("/" . org-sparse-tree)
  17742. ("Misc")
  17743. ("o" . org-open-at-point)
  17744. ("?" . org-speed-command-help)
  17745. ("<" . (org-agenda-set-restriction-lock 'subtree))
  17746. (">" . (org-agenda-remove-restriction-lock))
  17747. )
  17748. "The default speed commands.")
  17749. (defun org-print-speed-command (e)
  17750. (if (> (length (car e)) 1)
  17751. (progn
  17752. (princ "\n")
  17753. (princ (car e))
  17754. (princ "\n")
  17755. (princ (make-string (length (car e)) ?-))
  17756. (princ "\n"))
  17757. (princ (car e))
  17758. (princ " ")
  17759. (if (symbolp (cdr e))
  17760. (princ (symbol-name (cdr e)))
  17761. (prin1 (cdr e)))
  17762. (princ "\n")))
  17763. (defun org-speed-command-help ()
  17764. "Show the available speed commands."
  17765. (interactive)
  17766. (if (not org-use-speed-commands)
  17767. (user-error "Speed commands are not activated, customize `org-use-speed-commands'")
  17768. (with-output-to-temp-buffer "*Help*"
  17769. (princ "User-defined Speed commands\n===========================\n")
  17770. (mapc 'org-print-speed-command org-speed-commands-user)
  17771. (princ "\n")
  17772. (princ "Built-in Speed commands\n=======================\n")
  17773. (mapc 'org-print-speed-command org-speed-commands-default))
  17774. (with-current-buffer "*Help*"
  17775. (setq truncate-lines t))))
  17776. (defun org-speed-move-safe (cmd)
  17777. "Execute CMD, but make sure that the cursor always ends up in a headline.
  17778. If not, return to the original position and throw an error."
  17779. (interactive)
  17780. (let ((pos (point)))
  17781. (call-interactively cmd)
  17782. (unless (and (bolp) (org-at-heading-p))
  17783. (goto-char pos)
  17784. (error "Boundary reached while executing %s" cmd))))
  17785. (defvar org-self-insert-command-undo-counter 0)
  17786. (defvar org-table-auto-blank-field) ; defined in org-table.el
  17787. (defvar org-speed-command nil)
  17788. (define-obsolete-function-alias
  17789. 'org-speed-command-default-hook 'org-speed-command-activate "24.3")
  17790. (defun org-speed-command-activate (keys)
  17791. "Hook for activating single-letter speed commands.
  17792. `org-speed-commands-default' specifies a minimal command set.
  17793. Use `org-speed-commands-user' for further customization."
  17794. (when (or (and (bolp) (looking-at org-outline-regexp))
  17795. (and (functionp org-use-speed-commands)
  17796. (funcall org-use-speed-commands)))
  17797. (cdr (assoc keys (append org-speed-commands-user
  17798. org-speed-commands-default)))))
  17799. (define-obsolete-function-alias
  17800. 'org-babel-speed-command-hook 'org-babel-speed-command-activate "24.3")
  17801. (defun org-babel-speed-command-activate (keys)
  17802. "Hook for activating single-letter code block commands."
  17803. (when (and (bolp) (looking-at org-babel-src-block-regexp))
  17804. (cdr (assoc keys org-babel-key-bindings))))
  17805. (defcustom org-speed-command-hook
  17806. '(org-speed-command-default-hook org-babel-speed-command-hook)
  17807. "Hook for activating speed commands at strategic locations.
  17808. Hook functions are called in sequence until a valid handler is
  17809. found.
  17810. Each hook takes a single argument, a user-pressed command key
  17811. which is also a `self-insert-command' from the global map.
  17812. Within the hook, examine the cursor position and the command key
  17813. and return nil or a valid handler as appropriate. Handler could
  17814. be one of an interactive command, a function, or a form.
  17815. Set `org-use-speed-commands' to non-nil value to enable this
  17816. hook. The default setting is `org-speed-command-activate'."
  17817. :group 'org-structure
  17818. :version "24.1"
  17819. :type 'hook)
  17820. (defun org-self-insert-command (N)
  17821. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  17822. If the cursor is in a table looking at whitespace, the whitespace is
  17823. overwritten, and the table is not marked as requiring realignment."
  17824. (interactive "p")
  17825. (org-check-before-invisible-edit 'insert)
  17826. (cond
  17827. ((and org-use-speed-commands
  17828. (setq org-speed-command
  17829. (run-hook-with-args-until-success
  17830. 'org-speed-command-hook (this-command-keys))))
  17831. (cond
  17832. ((commandp org-speed-command)
  17833. (setq this-command org-speed-command)
  17834. (call-interactively org-speed-command))
  17835. ((functionp org-speed-command)
  17836. (funcall org-speed-command))
  17837. ((and org-speed-command (listp org-speed-command))
  17838. (eval org-speed-command))
  17839. (t (let (org-use-speed-commands)
  17840. (call-interactively 'org-self-insert-command)))))
  17841. ((and
  17842. (org-table-p)
  17843. (progn
  17844. ;; check if we blank the field, and if that triggers align
  17845. (and (featurep 'org-table) org-table-auto-blank-field
  17846. (member last-command
  17847. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c yas/expand))
  17848. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  17849. ;; got extra space, this field does not determine column width
  17850. (let (org-table-may-need-update) (org-table-blank-field))
  17851. ;; no extra space, this field may determine column width
  17852. (org-table-blank-field)))
  17853. t)
  17854. (eq N 1)
  17855. (looking-at "[^|\n]* |"))
  17856. (let (org-table-may-need-update)
  17857. (goto-char (1- (match-end 0)))
  17858. (backward-delete-char 1)
  17859. (goto-char (match-beginning 0))
  17860. (self-insert-command N)))
  17861. (t
  17862. (setq org-table-may-need-update t)
  17863. (self-insert-command N)
  17864. (org-fix-tags-on-the-fly)
  17865. (if org-self-insert-cluster-for-undo
  17866. (if (not (eq last-command 'org-self-insert-command))
  17867. (setq org-self-insert-command-undo-counter 1)
  17868. (if (>= org-self-insert-command-undo-counter 20)
  17869. (setq org-self-insert-command-undo-counter 1)
  17870. (and (> org-self-insert-command-undo-counter 0)
  17871. buffer-undo-list (listp buffer-undo-list)
  17872. (not (cadr buffer-undo-list)) ; remove nil entry
  17873. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  17874. (setq org-self-insert-command-undo-counter
  17875. (1+ org-self-insert-command-undo-counter))))))))
  17876. (defun org-check-before-invisible-edit (kind)
  17877. "Check is editing if kind KIND would be dangerous with invisible text around.
  17878. The detailed reaction depends on the user option `org-catch-invisible-edits'."
  17879. ;; First, try to get out of here as quickly as possible, to reduce overhead
  17880. (if (and org-catch-invisible-edits
  17881. (or (not (boundp 'visible-mode)) (not visible-mode))
  17882. (or (get-char-property (point) 'invisible)
  17883. (get-char-property (max (point-min) (1- (point))) 'invisible)))
  17884. ;; OK, we need to take a closer look
  17885. (let* ((invisible-at-point (get-char-property (point) 'invisible))
  17886. (invisible-before-point (if (bobp) nil (get-char-property
  17887. (1- (point)) 'invisible)))
  17888. (border-and-ok-direction
  17889. (or
  17890. ;; Check if we are acting predictably before invisible text
  17891. (and invisible-at-point (not invisible-before-point)
  17892. (memq kind '(insert delete-backward)))
  17893. ;; Check if we are acting predictably after invisible text
  17894. ;; This works not well, and I have turned it off. It seems
  17895. ;; better to always show and stop after invisible text.
  17896. ;; (and (not invisible-at-point) invisible-before-point
  17897. ;; (memq kind '(insert delete)))
  17898. )))
  17899. (when (or (memq invisible-at-point '(outline org-hide-block t))
  17900. (memq invisible-before-point '(outline org-hide-block t)))
  17901. (if (eq org-catch-invisible-edits 'error)
  17902. (user-error "Editing in invisible areas is prohibited, make them visible first"))
  17903. (if (and org-custom-properties-overlays
  17904. (y-or-n-p "Display invisible properties in this buffer? "))
  17905. (org-toggle-custom-properties-visibility)
  17906. ;; Make the area visible
  17907. (save-excursion
  17908. (if invisible-before-point
  17909. (goto-char (previous-single-char-property-change
  17910. (point) 'invisible)))
  17911. (show-subtree))
  17912. (cond
  17913. ((eq org-catch-invisible-edits 'show)
  17914. ;; That's it, we do the edit after showing
  17915. (message
  17916. "Unfolding invisible region around point before editing")
  17917. (sit-for 1))
  17918. ((and (eq org-catch-invisible-edits 'smart)
  17919. border-and-ok-direction)
  17920. (message "Unfolding invisible region around point before editing"))
  17921. (t
  17922. ;; Don't do the edit, make the user repeat it in full visibility
  17923. (user-error "Edit in invisible region aborted, repeat to confirm with text visible"))))))))
  17924. (defun org-fix-tags-on-the-fly ()
  17925. (when (and (equal (char-after (point-at-bol)) ?*)
  17926. (org-at-heading-p))
  17927. (org-align-tags-here org-tags-column)))
  17928. (defun org-delete-backward-char (N)
  17929. "Like `delete-backward-char', insert whitespace at field end in tables.
  17930. When deleting backwards, in tables this function will insert whitespace in
  17931. front of the next \"|\" separator, to keep the table aligned. The table will
  17932. still be marked for re-alignment if the field did fill the entire column,
  17933. because, in this case the deletion might narrow the column."
  17934. (interactive "p")
  17935. (save-match-data
  17936. (org-check-before-invisible-edit 'delete-backward)
  17937. (if (and (org-table-p)
  17938. (eq N 1)
  17939. (string-match "|" (buffer-substring (point-at-bol) (point)))
  17940. (looking-at ".*?|"))
  17941. (let ((pos (point))
  17942. (noalign (looking-at "[^|\n\r]* |"))
  17943. (c org-table-may-need-update))
  17944. (backward-delete-char N)
  17945. (if (not overwrite-mode)
  17946. (progn
  17947. (skip-chars-forward "^|")
  17948. (insert " ")
  17949. (goto-char (1- pos))))
  17950. ;; noalign: if there were two spaces at the end, this field
  17951. ;; does not determine the width of the column.
  17952. (if noalign (setq org-table-may-need-update c)))
  17953. (backward-delete-char N)
  17954. (org-fix-tags-on-the-fly))))
  17955. (defun org-delete-char (N)
  17956. "Like `delete-char', but insert whitespace at field end in tables.
  17957. When deleting characters, in tables this function will insert whitespace in
  17958. front of the next \"|\" separator, to keep the table aligned. The table will
  17959. still be marked for re-alignment if the field did fill the entire column,
  17960. because, in this case the deletion might narrow the column."
  17961. (interactive "p")
  17962. (save-match-data
  17963. (org-check-before-invisible-edit 'delete)
  17964. (if (and (org-table-p)
  17965. (not (bolp))
  17966. (not (= (char-after) ?|))
  17967. (eq N 1))
  17968. (if (looking-at ".*?|")
  17969. (let ((pos (point))
  17970. (noalign (looking-at "[^|\n\r]* |"))
  17971. (c org-table-may-need-update))
  17972. (replace-match
  17973. (concat (substring (match-string 0) 1 -1) " |") nil t)
  17974. (goto-char pos)
  17975. ;; noalign: if there were two spaces at the end, this field
  17976. ;; does not determine the width of the column.
  17977. (if noalign (setq org-table-may-need-update c)))
  17978. (delete-char N))
  17979. (delete-char N)
  17980. (org-fix-tags-on-the-fly))))
  17981. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  17982. (put 'org-self-insert-command 'delete-selection
  17983. (lambda ()
  17984. (not (run-hook-with-args-until-success
  17985. 'self-insert-uses-region-functions))))
  17986. (put 'orgtbl-self-insert-command 'delete-selection
  17987. (lambda ()
  17988. (not (run-hook-with-args-until-success
  17989. 'self-insert-uses-region-functions))))
  17990. (put 'org-delete-char 'delete-selection 'supersede)
  17991. (put 'org-delete-backward-char 'delete-selection 'supersede)
  17992. (put 'org-yank 'delete-selection 'yank)
  17993. ;; Make `flyspell-mode' delay after some commands
  17994. (put 'org-self-insert-command 'flyspell-delayed t)
  17995. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  17996. (put 'org-delete-char 'flyspell-delayed t)
  17997. (put 'org-delete-backward-char 'flyspell-delayed t)
  17998. ;; Make pabbrev-mode expand after org-mode commands
  17999. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  18000. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  18001. (defun org-remap (map &rest commands)
  18002. "In MAP, remap the functions given in COMMANDS.
  18003. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  18004. (let (new old)
  18005. (while commands
  18006. (setq old (pop commands) new (pop commands))
  18007. (if (fboundp 'command-remapping)
  18008. (org-defkey map (vector 'remap old) new)
  18009. (substitute-key-definition old new map global-map)))))
  18010. (defun org-transpose-words ()
  18011. "Transpose words for Org.
  18012. This uses the `org-mode-transpose-word-syntax-table' syntax
  18013. table, which interprets characters in `org-emphasis-alist' as
  18014. word constituents."
  18015. (interactive)
  18016. (with-syntax-table org-mode-transpose-word-syntax-table
  18017. (call-interactively 'transpose-words)))
  18018. (org-remap org-mode-map 'transpose-words 'org-transpose-words)
  18019. (when (eq org-enable-table-editor 'optimized)
  18020. ;; If the user wants maximum table support, we need to hijack
  18021. ;; some standard editing functions
  18022. (org-remap org-mode-map
  18023. 'self-insert-command 'org-self-insert-command
  18024. 'delete-char 'org-delete-char
  18025. 'delete-backward-char 'org-delete-backward-char)
  18026. (org-defkey org-mode-map "|" 'org-force-self-insert))
  18027. (defvar org-ctrl-c-ctrl-c-hook nil
  18028. "Hook for functions attaching themselves to `C-c C-c'.
  18029. This can be used to add additional functionality to the C-c C-c
  18030. key which executes context-dependent commands. This hook is run
  18031. before any other test, while `org-ctrl-c-ctrl-c-final-hook' is
  18032. run after the last test.
  18033. Each function will be called with no arguments. The function
  18034. must check if the context is appropriate for it to act. If yes,
  18035. it should do its thing and then return a non-nil value. If the
  18036. context is wrong, just do nothing and return nil.")
  18037. (defvar org-ctrl-c-ctrl-c-final-hook nil
  18038. "Hook for functions attaching themselves to `C-c C-c'.
  18039. This can be used to add additional functionality to the C-c C-c
  18040. key which executes context-dependent commands. This hook is run
  18041. after any other test, while `org-ctrl-c-ctrl-c-hook' is run
  18042. before the first test.
  18043. Each function will be called with no arguments. The function
  18044. must check if the context is appropriate for it to act. If yes,
  18045. it should do its thing and then return a non-nil value. If the
  18046. context is wrong, just do nothing and return nil.")
  18047. (defvar org-tab-first-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 as the first action when TAB is pressed, even before
  18051. `org-cycle' messes around with the `outline-regexp' to cater for
  18052. inline tasks and plain list item folding.
  18053. If any function in this hook returns t, any other actions that
  18054. would have been caused by TAB (such as table field motion or visibility
  18055. cycling) will not occur.")
  18056. (defvar org-tab-after-check-for-table-hook nil
  18057. "Hook for functions to attach themselves to TAB.
  18058. See `org-ctrl-c-ctrl-c-hook' for more information.
  18059. This hook runs after it has been established that the cursor is not in a
  18060. table, but before checking if the cursor is in a headline or if global cycling
  18061. should be done.
  18062. If any function in this hook returns t, not other actions like visibility
  18063. cycling will be done.")
  18064. (defvar org-tab-after-check-for-cycling-hook nil
  18065. "Hook for functions to attach themselves to TAB.
  18066. See `org-ctrl-c-ctrl-c-hook' for more information.
  18067. This hook runs after it has been established that not table field motion and
  18068. not visibility should be done because of current context. This is probably
  18069. the place where a package like yasnippets can hook in.")
  18070. (defvar org-tab-before-tab-emulation-hook nil
  18071. "Hook for functions to attach themselves to TAB.
  18072. See `org-ctrl-c-ctrl-c-hook' for more information.
  18073. This hook runs after every other options for TAB have been exhausted, but
  18074. before indentation and \t insertion takes place.")
  18075. (defvar org-metaleft-hook nil
  18076. "Hook for functions attaching themselves to `M-left'.
  18077. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18078. (defvar org-metaright-hook nil
  18079. "Hook for functions attaching themselves to `M-right'.
  18080. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18081. (defvar org-metaup-hook nil
  18082. "Hook for functions attaching themselves to `M-up'.
  18083. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18084. (defvar org-metadown-hook nil
  18085. "Hook for functions attaching themselves to `M-down'.
  18086. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18087. (defvar org-shiftmetaleft-hook nil
  18088. "Hook for functions attaching themselves to `M-S-left'.
  18089. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18090. (defvar org-shiftmetaright-hook nil
  18091. "Hook for functions attaching themselves to `M-S-right'.
  18092. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18093. (defvar org-shiftmetaup-hook nil
  18094. "Hook for functions attaching themselves to `M-S-up'.
  18095. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18096. (defvar org-shiftmetadown-hook nil
  18097. "Hook for functions attaching themselves to `M-S-down'.
  18098. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18099. (defvar org-metareturn-hook nil
  18100. "Hook for functions attaching themselves to `M-RET'.
  18101. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18102. (defvar org-shiftup-hook nil
  18103. "Hook for functions attaching themselves to `S-up'.
  18104. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18105. (defvar org-shiftup-final-hook nil
  18106. "Hook for functions attaching themselves to `S-up'.
  18107. This one runs after all other options except shift-select have been excluded.
  18108. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18109. (defvar org-shiftdown-hook nil
  18110. "Hook for functions attaching themselves to `S-down'.
  18111. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18112. (defvar org-shiftdown-final-hook nil
  18113. "Hook for functions attaching themselves to `S-down'.
  18114. This one runs after all other options except shift-select have been excluded.
  18115. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18116. (defvar org-shiftleft-hook nil
  18117. "Hook for functions attaching themselves to `S-left'.
  18118. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18119. (defvar org-shiftleft-final-hook nil
  18120. "Hook for functions attaching themselves to `S-left'.
  18121. This one runs after all other options except shift-select have been excluded.
  18122. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18123. (defvar org-shiftright-hook nil
  18124. "Hook for functions attaching themselves to `S-right'.
  18125. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18126. (defvar org-shiftright-final-hook nil
  18127. "Hook for functions attaching themselves to `S-right'.
  18128. This one runs after all other options except shift-select have been excluded.
  18129. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18130. (defun org-modifier-cursor-error ()
  18131. "Throw an error, a modified cursor command was applied in wrong context."
  18132. (user-error "This command is active in special context like tables, headlines or items"))
  18133. (defun org-shiftselect-error ()
  18134. "Throw an error because Shift-Cursor command was applied in wrong context."
  18135. (if (and (boundp 'shift-select-mode) shift-select-mode)
  18136. (user-error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
  18137. (user-error "This command works only in special context like headlines or timestamps")))
  18138. (defun org-call-for-shift-select (cmd)
  18139. (let ((this-command-keys-shift-translated t))
  18140. (call-interactively cmd)))
  18141. (defun org-shifttab (&optional arg)
  18142. "Global visibility cycling or move to previous table field.
  18143. Call `org-table-previous-field' within a table.
  18144. When ARG is nil, cycle globally through visibility states.
  18145. When ARG is a numeric prefix, show contents of this level."
  18146. (interactive "P")
  18147. (cond
  18148. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  18149. ((integerp arg)
  18150. (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
  18151. (message "Content view to level: %d" arg)
  18152. (org-content (prefix-numeric-value arg2))
  18153. (org-cycle-show-empty-lines t)
  18154. (setq org-cycle-global-status 'overview)))
  18155. (t (call-interactively 'org-global-cycle))))
  18156. (defun org-shiftmetaleft ()
  18157. "Promote subtree or delete table column.
  18158. Calls `org-promote-subtree', `org-outdent-item-tree', or
  18159. `org-table-delete-column', depending on context. See the
  18160. individual commands for more information."
  18161. (interactive)
  18162. (cond
  18163. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  18164. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  18165. ((org-at-heading-p) (call-interactively 'org-promote-subtree))
  18166. ((if (not (org-region-active-p)) (org-at-item-p)
  18167. (save-excursion (goto-char (region-beginning))
  18168. (org-at-item-p)))
  18169. (call-interactively 'org-outdent-item-tree))
  18170. (t (org-modifier-cursor-error))))
  18171. (defun org-shiftmetaright ()
  18172. "Demote subtree or insert table column.
  18173. Calls `org-demote-subtree', `org-indent-item-tree', or
  18174. `org-table-insert-column', depending on context. See the
  18175. individual commands for more information."
  18176. (interactive)
  18177. (cond
  18178. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  18179. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  18180. ((org-at-heading-p) (call-interactively 'org-demote-subtree))
  18181. ((if (not (org-region-active-p)) (org-at-item-p)
  18182. (save-excursion (goto-char (region-beginning))
  18183. (org-at-item-p)))
  18184. (call-interactively 'org-indent-item-tree))
  18185. (t (org-modifier-cursor-error))))
  18186. (defun org-shiftmetaup (&optional arg)
  18187. "Move subtree up or kill table row.
  18188. Calls `org-move-subtree-up' or `org-table-kill-row' or
  18189. `org-move-item-up' or `org-timestamp-up', depending on context.
  18190. See the individual commands for more information."
  18191. (interactive "P")
  18192. (cond
  18193. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  18194. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  18195. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  18196. ((org-at-item-p) (call-interactively 'org-move-item-up))
  18197. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  18198. (call-interactively 'org-timestamp-up)))
  18199. (t (call-interactively 'org-drag-line-backward))))
  18200. (defun org-shiftmetadown (&optional arg)
  18201. "Move subtree down or insert table row.
  18202. Calls `org-move-subtree-down' or `org-table-insert-row' or
  18203. `org-move-item-down' or `org-timestamp-up', depending on context.
  18204. See the individual commands for more information."
  18205. (interactive "P")
  18206. (cond
  18207. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  18208. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  18209. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  18210. ((org-at-item-p) (call-interactively 'org-move-item-down))
  18211. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  18212. (call-interactively 'org-timestamp-down)))
  18213. (t (call-interactively 'org-drag-line-forward))))
  18214. (defsubst org-hidden-tree-error ()
  18215. (user-error
  18216. "Hidden subtree, open with TAB or use subtree command M-S-<left>/<right>"))
  18217. (defun org-metaleft (&optional arg)
  18218. "Promote heading or move table column to left.
  18219. Calls `org-do-promote' or `org-table-move-column', depending on context.
  18220. With no specific context, calls the Emacs default `backward-word'.
  18221. See the individual commands for more information."
  18222. (interactive "P")
  18223. (cond
  18224. ((run-hook-with-args-until-success 'org-metaleft-hook))
  18225. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  18226. ((org-with-limited-levels
  18227. (or (org-at-heading-p)
  18228. (and (org-region-active-p)
  18229. (save-excursion
  18230. (goto-char (region-beginning))
  18231. (org-at-heading-p)))))
  18232. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  18233. (call-interactively 'org-do-promote))
  18234. ;; At an inline task.
  18235. ((org-at-heading-p)
  18236. (call-interactively 'org-inlinetask-promote))
  18237. ((or (org-at-item-p)
  18238. (and (org-region-active-p)
  18239. (save-excursion
  18240. (goto-char (region-beginning))
  18241. (org-at-item-p))))
  18242. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  18243. (call-interactively 'org-outdent-item))
  18244. (t (call-interactively 'backward-word))))
  18245. (defun org-metaright (&optional arg)
  18246. "Demote a subtree, a list item or move table column to right.
  18247. In front of a drawer or a block keyword, indent it correctly.
  18248. With no specific context, calls the Emacs default `forward-word'.
  18249. See the individual commands for more information."
  18250. (interactive "P")
  18251. (cond
  18252. ((run-hook-with-args-until-success 'org-metaright-hook))
  18253. ((org-at-table-p) (call-interactively 'org-table-move-column))
  18254. ((org-at-drawer-p) (call-interactively 'org-indent-drawer))
  18255. ((org-at-block-p) (call-interactively 'org-indent-block))
  18256. ((org-with-limited-levels
  18257. (or (org-at-heading-p)
  18258. (and (org-region-active-p)
  18259. (save-excursion
  18260. (goto-char (region-beginning))
  18261. (org-at-heading-p)))))
  18262. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  18263. (call-interactively 'org-do-demote))
  18264. ;; At an inline task.
  18265. ((org-at-heading-p)
  18266. (call-interactively 'org-inlinetask-demote))
  18267. ((or (org-at-item-p)
  18268. (and (org-region-active-p)
  18269. (save-excursion
  18270. (goto-char (region-beginning))
  18271. (org-at-item-p))))
  18272. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  18273. (call-interactively 'org-indent-item))
  18274. (t (call-interactively 'forward-word))))
  18275. (defun org-check-for-hidden (what)
  18276. "Check if there are hidden headlines/items in the current visual line.
  18277. WHAT can be either `headlines' or `items'. If the current line is
  18278. an outline or item heading and it has a folded subtree below it,
  18279. this function returns t, nil otherwise."
  18280. (let ((re (cond
  18281. ((eq what 'headlines) org-outline-regexp-bol)
  18282. ((eq what 'items) (org-item-beginning-re))
  18283. (t (error "This should not happen"))))
  18284. beg end)
  18285. (save-excursion
  18286. (catch 'exit
  18287. (unless (org-region-active-p)
  18288. (setq beg (point-at-bol))
  18289. (beginning-of-line 2)
  18290. (while (and (not (eobp)) ;; this is like `next-line'
  18291. (get-char-property (1- (point)) 'invisible))
  18292. (beginning-of-line 2))
  18293. (setq end (point))
  18294. (goto-char beg)
  18295. (goto-char (point-at-eol))
  18296. (setq end (max end (point)))
  18297. (while (re-search-forward re end t)
  18298. (if (get-char-property (match-beginning 0) 'invisible)
  18299. (throw 'exit t))))
  18300. nil))))
  18301. (defun org-metaup (&optional arg)
  18302. "Move subtree up or move table row up.
  18303. Calls `org-move-subtree-up' or `org-table-move-row' or
  18304. `org-move-item-up', depending on context. See the individual commands
  18305. for more information."
  18306. (interactive "P")
  18307. (cond
  18308. ((run-hook-with-args-until-success 'org-metaup-hook))
  18309. ((org-region-active-p)
  18310. (let* ((a (min (region-beginning) (region-end)))
  18311. (b (1- (max (region-beginning) (region-end))))
  18312. (c (save-excursion (goto-char a)
  18313. (move-beginning-of-line 0)))
  18314. (d (save-excursion (goto-char a)
  18315. (move-end-of-line 0) (point))))
  18316. (transpose-regions a b c d)
  18317. (goto-char c)))
  18318. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  18319. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  18320. ((org-at-item-p) (call-interactively 'org-move-item-up))
  18321. (t (org-drag-element-backward))))
  18322. (defun org-metadown (&optional arg)
  18323. "Move subtree down or move table row down.
  18324. Calls `org-move-subtree-down' or `org-table-move-row' or
  18325. `org-move-item-down', depending on context. See the individual
  18326. commands for more information."
  18327. (interactive "P")
  18328. (cond
  18329. ((run-hook-with-args-until-success 'org-metadown-hook))
  18330. ((org-region-active-p)
  18331. (let* ((a (min (region-beginning) (region-end)))
  18332. (b (max (region-beginning) (region-end)))
  18333. (c (save-excursion (goto-char b)
  18334. (move-beginning-of-line 1)))
  18335. (d (save-excursion (goto-char b)
  18336. (move-end-of-line 1) (1+ (point)))))
  18337. (transpose-regions a b c d)
  18338. (goto-char d)))
  18339. ((org-at-table-p) (call-interactively 'org-table-move-row))
  18340. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  18341. ((org-at-item-p) (call-interactively 'org-move-item-down))
  18342. (t (org-drag-element-forward))))
  18343. (defun org-shiftup (&optional arg)
  18344. "Increase item in timestamp or increase priority of current headline.
  18345. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  18346. depending on context. See the individual commands for more information."
  18347. (interactive "P")
  18348. (cond
  18349. ((run-hook-with-args-until-success 'org-shiftup-hook))
  18350. ((and org-support-shift-select (org-region-active-p))
  18351. (org-call-for-shift-select 'previous-line))
  18352. ((org-at-timestamp-p t)
  18353. (call-interactively (if org-edit-timestamp-down-means-later
  18354. 'org-timestamp-down 'org-timestamp-up)))
  18355. ((and (not (eq org-support-shift-select 'always))
  18356. org-enable-priority-commands
  18357. (org-at-heading-p))
  18358. (call-interactively 'org-priority-up))
  18359. ((and (not org-support-shift-select) (org-at-item-p))
  18360. (call-interactively 'org-previous-item))
  18361. ((org-clocktable-try-shift 'up arg))
  18362. ((run-hook-with-args-until-success 'org-shiftup-final-hook))
  18363. (org-support-shift-select
  18364. (org-call-for-shift-select 'previous-line))
  18365. (t (org-shiftselect-error))))
  18366. (defun org-shiftdown (&optional arg)
  18367. "Decrease item in timestamp or decrease priority of current headline.
  18368. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  18369. depending on context. See the individual commands for more information."
  18370. (interactive "P")
  18371. (cond
  18372. ((run-hook-with-args-until-success 'org-shiftdown-hook))
  18373. ((and org-support-shift-select (org-region-active-p))
  18374. (org-call-for-shift-select 'next-line))
  18375. ((org-at-timestamp-p t)
  18376. (call-interactively (if org-edit-timestamp-down-means-later
  18377. 'org-timestamp-up 'org-timestamp-down)))
  18378. ((and (not (eq org-support-shift-select 'always))
  18379. org-enable-priority-commands
  18380. (org-at-heading-p))
  18381. (call-interactively 'org-priority-down))
  18382. ((and (not org-support-shift-select) (org-at-item-p))
  18383. (call-interactively 'org-next-item))
  18384. ((org-clocktable-try-shift 'down arg))
  18385. ((run-hook-with-args-until-success 'org-shiftdown-final-hook))
  18386. (org-support-shift-select
  18387. (org-call-for-shift-select 'next-line))
  18388. (t (org-shiftselect-error))))
  18389. (defun org-shiftright (&optional arg)
  18390. "Cycle the thing at point or in the current line, depending on context.
  18391. Depending on context, this does one of the following:
  18392. - switch a timestamp at point one day into the future
  18393. - on a headline, switch to the next TODO keyword.
  18394. - on an item, switch entire list to the next bullet type
  18395. - on a property line, switch to the next allowed value
  18396. - on a clocktable definition line, move time block into the future"
  18397. (interactive "P")
  18398. (cond
  18399. ((run-hook-with-args-until-success 'org-shiftright-hook))
  18400. ((and org-support-shift-select (org-region-active-p))
  18401. (org-call-for-shift-select 'forward-char))
  18402. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  18403. ((and (not (eq org-support-shift-select 'always))
  18404. (org-at-heading-p))
  18405. (let ((org-inhibit-logging
  18406. (not org-treat-S-cursor-todo-selection-as-state-change))
  18407. (org-inhibit-blocking
  18408. (not org-treat-S-cursor-todo-selection-as-state-change)))
  18409. (org-call-with-arg 'org-todo 'right)))
  18410. ((or (and org-support-shift-select
  18411. (not (eq org-support-shift-select 'always))
  18412. (org-at-item-bullet-p))
  18413. (and (not org-support-shift-select) (org-at-item-p)))
  18414. (org-call-with-arg 'org-cycle-list-bullet nil))
  18415. ((and (not (eq org-support-shift-select 'always))
  18416. (org-at-property-p))
  18417. (call-interactively 'org-property-next-allowed-value))
  18418. ((org-clocktable-try-shift 'right arg))
  18419. ((run-hook-with-args-until-success 'org-shiftright-final-hook))
  18420. (org-support-shift-select
  18421. (org-call-for-shift-select 'forward-char))
  18422. (t (org-shiftselect-error))))
  18423. (defun org-shiftleft (&optional arg)
  18424. "Cycle the thing at point or in the current line, depending on context.
  18425. Depending on context, this does one of the following:
  18426. - switch a timestamp at point one day into the past
  18427. - on a headline, switch to the previous TODO keyword.
  18428. - on an item, switch entire list to the previous bullet type
  18429. - on a property line, switch to the previous allowed value
  18430. - on a clocktable definition line, move time block into the past"
  18431. (interactive "P")
  18432. (cond
  18433. ((run-hook-with-args-until-success 'org-shiftleft-hook))
  18434. ((and org-support-shift-select (org-region-active-p))
  18435. (org-call-for-shift-select 'backward-char))
  18436. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  18437. ((and (not (eq org-support-shift-select 'always))
  18438. (org-at-heading-p))
  18439. (let ((org-inhibit-logging
  18440. (not org-treat-S-cursor-todo-selection-as-state-change))
  18441. (org-inhibit-blocking
  18442. (not org-treat-S-cursor-todo-selection-as-state-change)))
  18443. (org-call-with-arg 'org-todo 'left)))
  18444. ((or (and org-support-shift-select
  18445. (not (eq org-support-shift-select 'always))
  18446. (org-at-item-bullet-p))
  18447. (and (not org-support-shift-select) (org-at-item-p)))
  18448. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  18449. ((and (not (eq org-support-shift-select 'always))
  18450. (org-at-property-p))
  18451. (call-interactively 'org-property-previous-allowed-value))
  18452. ((org-clocktable-try-shift 'left arg))
  18453. ((run-hook-with-args-until-success 'org-shiftleft-final-hook))
  18454. (org-support-shift-select
  18455. (org-call-for-shift-select 'backward-char))
  18456. (t (org-shiftselect-error))))
  18457. (defun org-shiftcontrolright ()
  18458. "Switch to next TODO set."
  18459. (interactive)
  18460. (cond
  18461. ((and org-support-shift-select (org-region-active-p))
  18462. (org-call-for-shift-select 'forward-word))
  18463. ((and (not (eq org-support-shift-select 'always))
  18464. (org-at-heading-p))
  18465. (org-call-with-arg 'org-todo 'nextset))
  18466. (org-support-shift-select
  18467. (org-call-for-shift-select 'forward-word))
  18468. (t (org-shiftselect-error))))
  18469. (defun org-shiftcontrolleft ()
  18470. "Switch to previous TODO set."
  18471. (interactive)
  18472. (cond
  18473. ((and org-support-shift-select (org-region-active-p))
  18474. (org-call-for-shift-select 'backward-word))
  18475. ((and (not (eq org-support-shift-select 'always))
  18476. (org-at-heading-p))
  18477. (org-call-with-arg 'org-todo 'previousset))
  18478. (org-support-shift-select
  18479. (org-call-for-shift-select 'backward-word))
  18480. (t (org-shiftselect-error))))
  18481. (defun org-shiftcontrolup (&optional n)
  18482. "Change timestamps synchronously up in CLOCK log lines.
  18483. Optional argument N tells to change by that many units."
  18484. (interactive "P")
  18485. (if (and (org-at-clock-log-p) (org-at-timestamp-p t))
  18486. (let (org-support-shift-select)
  18487. (org-clock-timestamps-up n))
  18488. (user-error "Not at a clock log")))
  18489. (defun org-shiftcontroldown (&optional n)
  18490. "Change timestamps synchronously down in CLOCK log lines.
  18491. Optional argument N tells to change by that many units."
  18492. (interactive "P")
  18493. (if (and (org-at-clock-log-p) (org-at-timestamp-p t))
  18494. (let (org-support-shift-select)
  18495. (org-clock-timestamps-down n))
  18496. (user-error "Not at a clock log")))
  18497. (defun org-ctrl-c-ret ()
  18498. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  18499. (interactive)
  18500. (cond
  18501. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  18502. (t (call-interactively 'org-insert-heading))))
  18503. (defun org-find-visible ()
  18504. (let ((s (point)))
  18505. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  18506. (get-char-property s 'invisible)))
  18507. s))
  18508. (defun org-find-invisible ()
  18509. (let ((s (point)))
  18510. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  18511. (not (get-char-property s 'invisible))))
  18512. s))
  18513. (defun org-copy-visible (beg end)
  18514. "Copy the visible parts of the region."
  18515. (interactive "r")
  18516. (let (snippets s)
  18517. (save-excursion
  18518. (save-restriction
  18519. (narrow-to-region beg end)
  18520. (setq s (goto-char (point-min)))
  18521. (while (not (= (point) (point-max)))
  18522. (goto-char (org-find-invisible))
  18523. (push (buffer-substring s (point)) snippets)
  18524. (setq s (goto-char (org-find-visible))))))
  18525. (kill-new (apply 'concat (nreverse snippets)))))
  18526. (defun org-copy-special ()
  18527. "Copy region in table or copy current subtree.
  18528. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  18529. See the individual commands for more information."
  18530. (interactive)
  18531. (call-interactively
  18532. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  18533. (defun org-cut-special ()
  18534. "Cut region in table or cut current subtree.
  18535. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  18536. See the individual commands for more information."
  18537. (interactive)
  18538. (call-interactively
  18539. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  18540. (defun org-paste-special (arg)
  18541. "Paste rectangular region into table, or past subtree relative to level.
  18542. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  18543. See the individual commands for more information."
  18544. (interactive "P")
  18545. (if (org-at-table-p)
  18546. (org-table-paste-rectangle)
  18547. (org-paste-subtree arg)))
  18548. (defsubst org-in-fixed-width-region-p ()
  18549. "Is point in a fixed-width region?"
  18550. (save-match-data
  18551. (eq 'fixed-width (org-element-type (org-element-at-point)))))
  18552. (defun org-edit-special (&optional arg)
  18553. "Call a special editor for the element at point.
  18554. When at a table, call the formula editor with `org-table-edit-formulas'.
  18555. When in a source code block, call `org-edit-src-code'.
  18556. When in a fixed-width region, call `org-edit-fixed-width-region'.
  18557. When at an #+INCLUDE keyword, visit the included file.
  18558. On a link, call `ffap' to visit the link at point.
  18559. Otherwise, return a user error."
  18560. (interactive "P")
  18561. (let ((element (org-element-at-point)))
  18562. (assert (not buffer-read-only) nil
  18563. "Buffer is read-only: %s" (buffer-name))
  18564. (case (org-element-type element)
  18565. (src-block
  18566. (if (not arg) (org-edit-src-code)
  18567. (let* ((info (org-babel-get-src-block-info))
  18568. (lang (nth 0 info))
  18569. (params (nth 2 info))
  18570. (session (cdr (assq :session params))))
  18571. (if (not session) (org-edit-src-code)
  18572. ;; At a src-block with a session and function called with
  18573. ;; an ARG: switch to the buffer related to the inferior
  18574. ;; process.
  18575. (switch-to-buffer
  18576. (funcall (intern (concat "org-babel-prep-session:" lang))
  18577. session params))))))
  18578. (keyword
  18579. (if (member (org-element-property :key element) '("INCLUDE" "SETUPFILE"))
  18580. (find-file
  18581. (org-remove-double-quotes
  18582. (car (org-split-string (org-element-property :value element)))))
  18583. (user-error "No special environment to edit here")))
  18584. (table
  18585. (if (eq (org-element-property :type element) 'table.el)
  18586. (org-edit-src-code)
  18587. (call-interactively 'org-table-edit-formulas)))
  18588. ;; Only Org tables contain `table-row' type elements.
  18589. (table-row (call-interactively 'org-table-edit-formulas))
  18590. ((example-block export-block) (org-edit-src-code))
  18591. (fixed-width (org-edit-fixed-width-region))
  18592. (otherwise
  18593. ;; No notable element at point. Though, we may be at a link,
  18594. ;; which is an object. Thus, scan deeper.
  18595. (if (eq (org-element-type (org-element-context element)) 'link)
  18596. (call-interactively 'ffap)
  18597. (user-error "No special environment to edit here"))))))
  18598. (defvar org-table-coordinate-overlays) ; defined in org-table.el
  18599. (defun org-ctrl-c-ctrl-c (&optional arg)
  18600. "Set tags in headline, or update according to changed information at point.
  18601. This command does many different things, depending on context:
  18602. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  18603. this is what we do.
  18604. - If the cursor is on a statistics cookie, update it.
  18605. - If the cursor is in a headline, prompt for tags and insert them
  18606. into the current line, aligned to `org-tags-column'. When called
  18607. with prefix arg, realign all tags in the current buffer.
  18608. - If the cursor is in one of the special #+KEYWORD lines, this
  18609. triggers scanning the buffer for these lines and updating the
  18610. information.
  18611. - If the cursor is inside a table, realign the table. This command
  18612. works even if the automatic table editor has been turned off.
  18613. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  18614. the entire table.
  18615. - If the cursor is at a footnote reference or definition, jump to
  18616. the corresponding definition or references, respectively.
  18617. - If the cursor is a the beginning of a dynamic block, update it.
  18618. - If the current buffer is a capture buffer, close note and file it.
  18619. - If the cursor is on a <<<target>>>, update radio targets and
  18620. corresponding links in this buffer.
  18621. - If the cursor is on a numbered item in a plain list, renumber the
  18622. ordered list.
  18623. - If the cursor is on a checkbox, toggle it.
  18624. - If the cursor is on a code block, evaluate it. The variable
  18625. `org-confirm-babel-evaluate' can be used to control prompting
  18626. before code block evaluation, by default every code block
  18627. evaluation requires confirmation. Code block evaluation can be
  18628. inhibited by setting `org-babel-no-eval-on-ctrl-c-ctrl-c'."
  18629. (interactive "P")
  18630. (cond
  18631. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  18632. org-occur-highlights
  18633. org-latex-fragment-image-overlays)
  18634. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  18635. (org-remove-occur-highlights)
  18636. (org-remove-latex-fragment-image-overlays)
  18637. (message "Temporary highlights/overlays removed from current buffer"))
  18638. ((and (local-variable-p 'org-finish-function (current-buffer))
  18639. (fboundp org-finish-function))
  18640. (funcall org-finish-function))
  18641. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  18642. (t
  18643. (let* ((context (org-element-context)) (type (org-element-type context)))
  18644. ;; Test if point is within a blank line.
  18645. (if (save-excursion (beginning-of-line) (looking-at "[ \t]*$"))
  18646. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  18647. (user-error "C-c C-c can do nothing useful at this location"))
  18648. (case type
  18649. ;; When at a link, act according to the parent instead.
  18650. (link (setq context (org-element-property :parent context))
  18651. (setq type (org-element-type context)))
  18652. ;; Unsupported object types: refer to the first supported
  18653. ;; element or object containing it.
  18654. ((bold code entity export-snippet inline-babel-call inline-src-block
  18655. italic latex-fragment line-break macro strike-through subscript
  18656. superscript underline verbatim)
  18657. (while (and (setq context (org-element-property :parent context))
  18658. (not (memq (setq type (org-element-type context))
  18659. '(radio-target paragraph verse-block
  18660. table-cell)))))))
  18661. ;; For convenience: at the first line of a paragraph on the
  18662. ;; same line as an item, apply function on that item instead.
  18663. (when (eq type 'paragraph)
  18664. (let ((parent (org-element-property :parent context)))
  18665. (when (and (eq (org-element-type parent) 'item)
  18666. (= (point-at-bol) (org-element-property :begin parent)))
  18667. (setq context parent type 'item))))
  18668. ;; Act according to type of element or object at point.
  18669. (case type
  18670. (clock (org-clock-update-time-maybe))
  18671. (dynamic-block
  18672. (save-excursion
  18673. (goto-char (org-element-property :post-affiliated context))
  18674. (org-update-dblock)))
  18675. (footnote-definition
  18676. (goto-char (org-element-property :post-affiliated context))
  18677. (call-interactively 'org-footnote-action))
  18678. (footnote-reference (call-interactively 'org-footnote-action))
  18679. ((headline inlinetask)
  18680. (save-excursion (goto-char (org-element-property :begin context))
  18681. (call-interactively 'org-set-tags)))
  18682. (item
  18683. ;; At an item: a double C-u set checkbox to "[-]"
  18684. ;; unconditionally, whereas a single one will toggle its
  18685. ;; presence. Without an universal argument, if the item
  18686. ;; has a checkbox, toggle it. Otherwise repair the list.
  18687. (let* ((box (org-element-property :checkbox context))
  18688. (struct (org-element-property :structure context))
  18689. (old-struct (copy-tree struct))
  18690. (parents (org-list-parents-alist struct))
  18691. (prevs (org-list-prevs-alist struct))
  18692. (orderedp (org-not-nil (org-entry-get nil "ORDERED"))))
  18693. (org-list-set-checkbox
  18694. (org-element-property :begin context) struct
  18695. (cond ((equal arg '(16)) "[-]")
  18696. ((and (not box) (equal arg '(4))) "[ ]")
  18697. ((or (not box) (equal arg '(4))) nil)
  18698. ((eq box 'on) "[ ]")
  18699. (t "[X]")))
  18700. ;; Mimic `org-list-write-struct' but with grabbing
  18701. ;; a return value from `org-list-struct-fix-box'.
  18702. (org-list-struct-fix-ind struct parents 2)
  18703. (org-list-struct-fix-item-end struct)
  18704. (org-list-struct-fix-bul struct prevs)
  18705. (org-list-struct-fix-ind struct parents)
  18706. (let ((block-item
  18707. (org-list-struct-fix-box struct parents prevs orderedp)))
  18708. (if (and box (equal struct old-struct))
  18709. (if (equal arg '(16))
  18710. (message "Checkboxes already reset")
  18711. (user-error "Cannot toggle this checkbox: %s"
  18712. (if (eq box 'on)
  18713. "all subitems checked"
  18714. "unchecked subitems")))
  18715. (org-list-struct-apply-struct struct old-struct)
  18716. (org-update-checkbox-count-maybe))
  18717. (when block-item
  18718. (message "Checkboxes were removed due to empty box at line %d"
  18719. (org-current-line block-item))))))
  18720. (keyword
  18721. (let ((org-inhibit-startup-visibility-stuff t)
  18722. (org-startup-align-all-tables nil))
  18723. (when (boundp 'org-table-coordinate-overlays)
  18724. (mapc 'delete-overlay org-table-coordinate-overlays)
  18725. (setq org-table-coordinate-overlays nil))
  18726. (org-save-outline-visibility 'use-markers (org-mode-restart)))
  18727. (message "Local setup has been refreshed"))
  18728. (plain-list
  18729. ;; At a plain list, with a double C-u argument, set
  18730. ;; checkboxes of each item to "[-]", whereas a single one
  18731. ;; will toggle their presence according to the state of the
  18732. ;; first item in the list. Without an argument, repair the
  18733. ;; list.
  18734. (let* ((begin (org-element-property :contents-begin context))
  18735. (beginm (move-marker (make-marker) begin))
  18736. (struct (org-element-property :structure context))
  18737. (old-struct (copy-tree struct))
  18738. (first-box (save-excursion
  18739. (goto-char begin)
  18740. (looking-at org-list-full-item-re)
  18741. (match-string-no-properties 3)))
  18742. (new-box (cond ((equal arg '(16)) "[-]")
  18743. ((equal arg '(4)) (unless first-box "[ ]"))
  18744. ((equal first-box "[X]") "[ ]")
  18745. (t "[X]"))))
  18746. (cond
  18747. (arg
  18748. (mapc (lambda (pos) (org-list-set-checkbox pos struct new-box))
  18749. (org-list-get-all-items
  18750. begin struct (org-list-prevs-alist struct))))
  18751. ((and first-box (eq (point) begin))
  18752. ;; For convenience, when point is at bol on the first
  18753. ;; item of the list and no argument is provided, simply
  18754. ;; toggle checkbox of that item, if any.
  18755. (org-list-set-checkbox begin struct new-box)))
  18756. (org-list-write-struct
  18757. struct (org-list-parents-alist struct) old-struct)
  18758. (org-update-checkbox-count-maybe)
  18759. (save-excursion (goto-char beginm) (org-list-send-list 'maybe))))
  18760. ((property-drawer node-property)
  18761. (call-interactively 'org-property-action))
  18762. ((radio-target target)
  18763. (call-interactively 'org-update-radio-target-regexp))
  18764. (statistics-cookie
  18765. (call-interactively 'org-update-statistics-cookies))
  18766. ((table table-cell table-row)
  18767. ;; At a table, recalculate every field and align it. Also
  18768. ;; send the table if necessary. If the table has
  18769. ;; a `table.el' type, just give up. At a table row or
  18770. ;; cell, maybe recalculate line but always align table.
  18771. (if (eq (org-element-property :type context) 'table.el)
  18772. (message "Use C-c ' to edit table.el tables")
  18773. (let ((org-enable-table-editor t))
  18774. (if (or (eq type 'table)
  18775. ;; Check if point is at a TBLFM line.
  18776. (and (eq type 'table-row)
  18777. (= (point) (org-element-property :end context))))
  18778. (save-excursion
  18779. (if (org-at-TBLFM-p)
  18780. (progn (require 'org-table)
  18781. (org-table-calc-current-TBLFM))
  18782. (goto-char (org-element-property :contents-begin context))
  18783. (org-call-with-arg 'org-table-recalculate (or arg t))
  18784. (orgtbl-send-table 'maybe)))
  18785. (org-table-maybe-eval-formula)
  18786. (cond (arg (call-interactively 'org-table-recalculate))
  18787. ((org-table-maybe-recalculate-line))
  18788. (t (org-table-align)))))))
  18789. (timestamp (org-timestamp-change 0 'day))
  18790. (otherwise
  18791. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  18792. (user-error
  18793. "C-c C-c can do nothing useful at this location")))))))))
  18794. (defun org-mode-restart ()
  18795. (interactive)
  18796. (let ((indent-status (org-bound-and-true-p org-indent-mode)))
  18797. (funcall major-mode)
  18798. (hack-local-variables)
  18799. (when (and indent-status (not (org-bound-and-true-p org-indent-mode)))
  18800. (org-indent-mode -1)))
  18801. (message "%s restarted" major-mode))
  18802. (defun org-kill-note-or-show-branches ()
  18803. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  18804. (interactive)
  18805. (if (not org-finish-function)
  18806. (progn
  18807. (hide-subtree)
  18808. (call-interactively 'show-branches))
  18809. (let ((org-note-abort t))
  18810. (funcall org-finish-function))))
  18811. (defun org-open-line (n)
  18812. "Insert a new row in tables, call `open-line' elsewhere.
  18813. If `org-special-ctrl-o' is nil, just call `open-line' everywhere."
  18814. (interactive "*p")
  18815. (cond
  18816. ((not org-special-ctrl-o)
  18817. (open-line n))
  18818. ((org-at-table-p)
  18819. (org-table-insert-row))
  18820. (t
  18821. (open-line n))))
  18822. (defun org-return (&optional indent)
  18823. "Goto next table row or insert a newline.
  18824. Calls `org-table-next-row' or `newline', depending on context.
  18825. See the individual commands for more information."
  18826. (interactive)
  18827. (let (org-ts-what)
  18828. (cond
  18829. ((or (bobp) (org-in-src-block-p))
  18830. (if indent (newline-and-indent) (newline)))
  18831. ((org-at-table-p)
  18832. (org-table-justify-field-maybe)
  18833. (call-interactively 'org-table-next-row))
  18834. ;; when `newline-and-indent' is called within a list, make sure
  18835. ;; text moved stays inside the item.
  18836. ((and (org-in-item-p) indent)
  18837. (if (and (org-at-item-p) (>= (point) (match-end 0)))
  18838. (progn
  18839. (save-match-data (newline))
  18840. (org-indent-line-to (length (match-string 0))))
  18841. (let ((ind (org-get-indentation)))
  18842. (newline)
  18843. (if (org-looking-back org-list-end-re)
  18844. (org-indent-line)
  18845. (org-indent-line-to ind)))))
  18846. ((and org-return-follows-link
  18847. (org-at-timestamp-p t)
  18848. (not (eq org-ts-what 'after)))
  18849. (org-follow-timestamp-link))
  18850. ((and org-return-follows-link
  18851. (let ((tprop (get-text-property (point) 'face)))
  18852. (or (eq tprop 'org-link)
  18853. (and (listp tprop) (memq 'org-link tprop)))))
  18854. (call-interactively 'org-open-at-point))
  18855. ((and (org-at-heading-p)
  18856. (looking-at
  18857. (org-re "\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$")))
  18858. (org-show-entry)
  18859. (end-of-line 1)
  18860. (newline))
  18861. (t (if indent (newline-and-indent) (newline))))))
  18862. (defun org-return-indent ()
  18863. "Goto next table row or insert a newline and indent.
  18864. Calls `org-table-next-row' or `newline-and-indent', depending on
  18865. context. See the individual commands for more information."
  18866. (interactive)
  18867. (org-return t))
  18868. (defun org-ctrl-c-star ()
  18869. "Compute table, or change heading status of lines.
  18870. Calls `org-table-recalculate' or `org-toggle-heading',
  18871. depending on context."
  18872. (interactive)
  18873. (cond
  18874. ((org-at-table-p)
  18875. (call-interactively 'org-table-recalculate))
  18876. (t
  18877. ;; Convert all lines in region to list items
  18878. (call-interactively 'org-toggle-heading))))
  18879. (defun org-ctrl-c-minus ()
  18880. "Insert separator line in table or modify bullet status of line.
  18881. Also turns a plain line or a region of lines into list items.
  18882. Calls `org-table-insert-hline', `org-toggle-item', or
  18883. `org-cycle-list-bullet', depending on context."
  18884. (interactive)
  18885. (cond
  18886. ((org-at-table-p)
  18887. (call-interactively 'org-table-insert-hline))
  18888. ((org-region-active-p)
  18889. (call-interactively 'org-toggle-item))
  18890. ((org-in-item-p)
  18891. (call-interactively 'org-cycle-list-bullet))
  18892. (t
  18893. (call-interactively 'org-toggle-item))))
  18894. (defun org-toggle-item (arg)
  18895. "Convert headings or normal lines to items, items to normal lines.
  18896. If there is no active region, only the current line is considered.
  18897. If the first non blank line in the region is a headline, convert
  18898. all headlines to items, shifting text accordingly.
  18899. If it is an item, convert all items to normal lines.
  18900. If it is normal text, change region into a list of items.
  18901. With a prefix argument ARG, change the region in a single item."
  18902. (interactive "P")
  18903. (let ((shift-text
  18904. (function
  18905. ;; Shift text in current section to IND, from point to END.
  18906. ;; The function leaves point to END line.
  18907. (lambda (ind end)
  18908. (let ((min-i 1000) (end (copy-marker end)))
  18909. ;; First determine the minimum indentation (MIN-I) of
  18910. ;; the text.
  18911. (save-excursion
  18912. (catch 'exit
  18913. (while (< (point) end)
  18914. (let ((i (org-get-indentation)))
  18915. (cond
  18916. ;; Skip blank lines and inline tasks.
  18917. ((looking-at "^[ \t]*$"))
  18918. ((looking-at org-outline-regexp-bol))
  18919. ;; We can't find less than 0 indentation.
  18920. ((zerop i) (throw 'exit (setq min-i 0)))
  18921. ((< i min-i) (setq min-i i))))
  18922. (forward-line))))
  18923. ;; Then indent each line so that a line indented to
  18924. ;; MIN-I becomes indented to IND. Ignore blank lines
  18925. ;; and inline tasks in the process.
  18926. (let ((delta (- ind min-i)))
  18927. (while (< (point) end)
  18928. (unless (or (looking-at "^[ \t]*$")
  18929. (looking-at org-outline-regexp-bol))
  18930. (org-indent-line-to (+ (org-get-indentation) delta)))
  18931. (forward-line)))))))
  18932. (skip-blanks
  18933. (function
  18934. ;; Return beginning of first non-blank line, starting from
  18935. ;; line at POS.
  18936. (lambda (pos)
  18937. (save-excursion
  18938. (goto-char pos)
  18939. (skip-chars-forward " \r\t\n")
  18940. (point-at-bol)))))
  18941. beg end)
  18942. ;; Determine boundaries of changes.
  18943. (if (org-region-active-p)
  18944. (setq beg (funcall skip-blanks (region-beginning))
  18945. end (copy-marker (region-end)))
  18946. (setq beg (funcall skip-blanks (point-at-bol))
  18947. end (copy-marker (point-at-eol))))
  18948. ;; Depending on the starting line, choose an action on the text
  18949. ;; between BEG and END.
  18950. (org-with-limited-levels
  18951. (save-excursion
  18952. (goto-char beg)
  18953. (cond
  18954. ;; Case 1. Start at an item: de-itemize. Note that it only
  18955. ;; happens when a region is active: `org-ctrl-c-minus'
  18956. ;; would call `org-cycle-list-bullet' otherwise.
  18957. ((org-at-item-p)
  18958. (while (< (point) end)
  18959. (when (org-at-item-p)
  18960. (skip-chars-forward " \t")
  18961. (delete-region (point) (match-end 0)))
  18962. (forward-line)))
  18963. ;; Case 2. Start at an heading: convert to items.
  18964. ((org-at-heading-p)
  18965. (let* ((bul (org-list-bullet-string "-"))
  18966. (bul-len (length bul))
  18967. ;; Indentation of the first heading. It should be
  18968. ;; relative to the indentation of its parent, if any.
  18969. (start-ind (save-excursion
  18970. (cond
  18971. ((not org-adapt-indentation) 0)
  18972. ((not (outline-previous-heading)) 0)
  18973. (t (length (match-string 0))))))
  18974. ;; Level of first heading. Further headings will be
  18975. ;; compared to it to determine hierarchy in the list.
  18976. (ref-level (org-reduced-level (org-outline-level))))
  18977. (while (< (point) end)
  18978. (let* ((level (org-reduced-level (org-outline-level)))
  18979. (delta (max 0 (- level ref-level))))
  18980. ;; If current headline is less indented than the first
  18981. ;; one, set it as reference, in order to preserve
  18982. ;; subtrees.
  18983. (when (< level ref-level) (setq ref-level level))
  18984. (replace-match bul t t)
  18985. (org-indent-line-to (+ start-ind (* delta bul-len)))
  18986. ;; Ensure all text down to END (or SECTION-END) belongs
  18987. ;; to the newly created item.
  18988. (let ((section-end (save-excursion
  18989. (or (outline-next-heading) (point)))))
  18990. (forward-line)
  18991. (funcall shift-text
  18992. (+ start-ind (* (1+ delta) bul-len))
  18993. (min end section-end)))))))
  18994. ;; Case 3. Normal line with ARG: make the first line of region
  18995. ;; an item, and shift indentation of others lines to
  18996. ;; set them as item's body.
  18997. (arg (let* ((bul (org-list-bullet-string "-"))
  18998. (bul-len (length bul))
  18999. (ref-ind (org-get-indentation)))
  19000. (skip-chars-forward " \t")
  19001. (insert bul)
  19002. (forward-line)
  19003. (while (< (point) end)
  19004. ;; Ensure that lines less indented than first one
  19005. ;; still get included in item body.
  19006. (funcall shift-text
  19007. (+ ref-ind bul-len)
  19008. (min end (save-excursion (or (outline-next-heading)
  19009. (point)))))
  19010. (forward-line))))
  19011. ;; Case 4. Normal line without ARG: turn each non-item line
  19012. ;; into an item.
  19013. (t
  19014. (while (< (point) end)
  19015. (unless (or (org-at-heading-p) (org-at-item-p))
  19016. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  19017. (replace-match
  19018. (concat "\\1" (org-list-bullet-string "-") "\\2"))))
  19019. (forward-line))))))))
  19020. (defun org-toggle-heading (&optional nstars)
  19021. "Convert headings to normal text, or items or text to headings.
  19022. If there is no active region, only convert the current line.
  19023. With a \\[universal-argument] prefix, convert the whole list at
  19024. point into heading.
  19025. In a region:
  19026. - If the first non blank line is a headline, remove the stars
  19027. from all headlines in the region.
  19028. - If it is a normal line, turn each and every normal line (i.e.,
  19029. not an heading or an item) in the region into headings. If you
  19030. want to convert only the first line of this region, use one
  19031. universal prefix argument.
  19032. - If it is a plain list item, turn all plain list items into headings.
  19033. When converting a line into a heading, the number of stars is chosen
  19034. such that the lines become children of the current entry. However,
  19035. when a numeric prefix argument is given, its value determines the
  19036. number of stars to add."
  19037. (interactive "P")
  19038. (let ((skip-blanks
  19039. (function
  19040. ;; Return beginning of first non-blank line, starting from
  19041. ;; line at POS.
  19042. (lambda (pos)
  19043. (save-excursion
  19044. (goto-char pos)
  19045. (while (org-at-comment-p) (forward-line))
  19046. (skip-chars-forward " \r\t\n")
  19047. (point-at-bol)))))
  19048. beg end toggled)
  19049. ;; Determine boundaries of changes. If a universal prefix has
  19050. ;; been given, put the list in a region. If region ends at a bol,
  19051. ;; do not consider the last line to be in the region.
  19052. (when (and current-prefix-arg (org-at-item-p))
  19053. (if (listp current-prefix-arg) (setq current-prefix-arg 1))
  19054. (org-mark-element))
  19055. (if (org-region-active-p)
  19056. (setq beg (funcall skip-blanks (region-beginning))
  19057. end (copy-marker (save-excursion
  19058. (goto-char (region-end))
  19059. (if (bolp) (point) (point-at-eol)))))
  19060. (setq beg (funcall skip-blanks (point-at-bol))
  19061. end (copy-marker (point-at-eol))))
  19062. ;; Ensure inline tasks don't count as headings.
  19063. (org-with-limited-levels
  19064. (save-excursion
  19065. (goto-char beg)
  19066. (cond
  19067. ;; Case 1. Started at an heading: de-star headings.
  19068. ((org-at-heading-p)
  19069. (while (< (point) end)
  19070. (when (org-at-heading-p t)
  19071. (looking-at org-outline-regexp) (replace-match "")
  19072. (setq toggled t))
  19073. (forward-line)))
  19074. ;; Case 2. Started at an item: change items into headlines.
  19075. ;; One star will be added by `org-list-to-subtree'.
  19076. ((org-at-item-p)
  19077. (let* ((stars (make-string
  19078. ;; subtract the star that will be added again by
  19079. ;; `org-list-to-subtree'
  19080. (if (numberp nstars) (1- nstars)
  19081. (or (org-current-level) 0))
  19082. ?*))
  19083. (add-stars
  19084. (cond (nstars "") ; stars from prefix only
  19085. ((equal stars "") "") ; before first heading
  19086. (org-odd-levels-only "*") ; inside heading, odd
  19087. (t "")))) ; inside heading, oddeven
  19088. (while (< (point) end)
  19089. (when (org-at-item-p)
  19090. ;; Pay attention to cases when region ends before list.
  19091. (let* ((struct (org-list-struct))
  19092. (list-end (min (org-list-get-bottom-point struct) (1+ end))))
  19093. (save-restriction
  19094. (narrow-to-region (point) list-end)
  19095. (insert
  19096. (org-list-to-subtree
  19097. (org-list-parse-list t)
  19098. '(:istart (concat stars add-stars (funcall get-stars depth))
  19099. :icount (concat stars add-stars (funcall get-stars depth)))))))
  19100. (setq toggled t))
  19101. (forward-line))))
  19102. ;; Case 3. Started at normal text: make every line an heading,
  19103. ;; skipping headlines and items.
  19104. (t (let* ((stars
  19105. (make-string
  19106. (if (numberp nstars) nstars (or (org-current-level) 0)) ?*))
  19107. (add-stars
  19108. (cond (nstars "") ; stars from prefix only
  19109. ((equal stars "") "*") ; before first heading
  19110. (org-odd-levels-only "**") ; inside heading, odd
  19111. (t "*"))) ; inside heading, oddeven
  19112. (rpl (concat stars add-stars " "))
  19113. (lend (if (listp nstars) (save-excursion (end-of-line) (point)))))
  19114. (while (< (point) (if (equal nstars '(4)) lend end))
  19115. (when (and (not (or (org-at-heading-p) (org-at-item-p) (org-at-comment-p)))
  19116. (looking-at "\\([ \t]*\\)\\(\\S-\\)"))
  19117. (replace-match (concat rpl (match-string 2))) (setq toggled t))
  19118. (forward-line)))))))
  19119. (unless toggled (message "Cannot toggle heading from here"))))
  19120. (defun org-meta-return (&optional arg)
  19121. "Insert a new heading or wrap a region in a table.
  19122. Calls `org-insert-heading' or `org-table-wrap-region', depending
  19123. on context. See the individual commands for more information."
  19124. (interactive "P")
  19125. (org-check-before-invisible-edit 'insert)
  19126. (or (run-hook-with-args-until-success 'org-metareturn-hook)
  19127. (let* ((element (org-element-at-point))
  19128. (type (org-element-type element)))
  19129. (when (eq type 'table-row)
  19130. (setq element (org-element-property :parent element))
  19131. (setq type 'table))
  19132. (if (and (eq type 'table)
  19133. (eq (org-element-property :type element) 'org)
  19134. (>= (point) (org-element-property :contents-begin element))
  19135. (< (point) (org-element-property :contents-end element)))
  19136. (call-interactively 'org-table-wrap-region)
  19137. (call-interactively 'org-insert-heading)))))
  19138. ;;; Menu entries
  19139. (defsubst org-in-subtree-not-table-p ()
  19140. "Are we in a subtree and not in a table?"
  19141. (and (not (org-before-first-heading-p))
  19142. (not (org-at-table-p))))
  19143. ;; Define the Org-mode menus
  19144. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  19145. '("Tbl"
  19146. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  19147. ["Next Field" org-cycle (org-at-table-p)]
  19148. ["Previous Field" org-shifttab (org-at-table-p)]
  19149. ["Next Row" org-return (org-at-table-p)]
  19150. "--"
  19151. ["Blank Field" org-table-blank-field (org-at-table-p)]
  19152. ["Edit Field" org-table-edit-field (org-at-table-p)]
  19153. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  19154. "--"
  19155. ("Column"
  19156. ["Move Column Left" org-metaleft (org-at-table-p)]
  19157. ["Move Column Right" org-metaright (org-at-table-p)]
  19158. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  19159. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  19160. ("Row"
  19161. ["Move Row Up" org-metaup (org-at-table-p)]
  19162. ["Move Row Down" org-metadown (org-at-table-p)]
  19163. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  19164. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  19165. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  19166. "--"
  19167. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  19168. ("Rectangle"
  19169. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  19170. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  19171. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  19172. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  19173. "--"
  19174. ("Calculate"
  19175. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  19176. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  19177. ["Edit Formulas" org-edit-special (org-at-table-p)]
  19178. "--"
  19179. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  19180. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  19181. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  19182. "--"
  19183. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  19184. "--"
  19185. ["Sum Column/Rectangle" org-table-sum
  19186. (or (org-at-table-p) (org-region-active-p))]
  19187. ["Which Column?" org-table-current-column (org-at-table-p)])
  19188. ["Debug Formulas"
  19189. org-table-toggle-formula-debugger
  19190. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  19191. ["Show Col/Row Numbers"
  19192. org-table-toggle-coordinate-overlays
  19193. :style toggle
  19194. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  19195. "--"
  19196. ["Create" org-table-create (and (not (org-at-table-p))
  19197. org-enable-table-editor)]
  19198. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  19199. ["Import from File" org-table-import (not (org-at-table-p))]
  19200. ["Export to File" org-table-export (org-at-table-p)]
  19201. "--"
  19202. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  19203. (easy-menu-define org-org-menu org-mode-map "Org menu"
  19204. '("Org"
  19205. ("Show/Hide"
  19206. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  19207. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  19208. ["Sparse Tree..." org-sparse-tree t]
  19209. ["Reveal Context" org-reveal t]
  19210. ["Show All" show-all t]
  19211. "--"
  19212. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  19213. "--"
  19214. ["New Heading" org-insert-heading t]
  19215. ("Navigate Headings"
  19216. ["Up" outline-up-heading t]
  19217. ["Next" outline-next-visible-heading t]
  19218. ["Previous" outline-previous-visible-heading t]
  19219. ["Next Same Level" outline-forward-same-level t]
  19220. ["Previous Same Level" outline-backward-same-level t]
  19221. "--"
  19222. ["Jump" org-goto t])
  19223. ("Edit Structure"
  19224. ["Refile Subtree" org-refile (org-in-subtree-not-table-p)]
  19225. "--"
  19226. ["Move Subtree Up" org-shiftmetaup (org-in-subtree-not-table-p)]
  19227. ["Move Subtree Down" org-shiftmetadown (org-in-subtree-not-table-p)]
  19228. "--"
  19229. ["Copy Subtree" org-copy-special (org-in-subtree-not-table-p)]
  19230. ["Cut Subtree" org-cut-special (org-in-subtree-not-table-p)]
  19231. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  19232. "--"
  19233. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  19234. "--"
  19235. ["Copy visible text" org-copy-visible t]
  19236. "--"
  19237. ["Promote Heading" org-metaleft (org-in-subtree-not-table-p)]
  19238. ["Promote Subtree" org-shiftmetaleft (org-in-subtree-not-table-p)]
  19239. ["Demote Heading" org-metaright (org-in-subtree-not-table-p)]
  19240. ["Demote Subtree" org-shiftmetaright (org-in-subtree-not-table-p)]
  19241. "--"
  19242. ["Sort Region/Children" org-sort t]
  19243. "--"
  19244. ["Convert to odd levels" org-convert-to-odd-levels t]
  19245. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  19246. ("Editing"
  19247. ["Emphasis..." org-emphasize t]
  19248. ["Edit Source Example" org-edit-special t]
  19249. "--"
  19250. ["Footnote new/jump" org-footnote-action t]
  19251. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  19252. ("Archive"
  19253. ["Archive (default method)" org-archive-subtree-default (org-in-subtree-not-table-p)]
  19254. "--"
  19255. ["Move Subtree to Archive file" org-advertized-archive-subtree (org-in-subtree-not-table-p)]
  19256. ["Toggle ARCHIVE tag" org-toggle-archive-tag (org-in-subtree-not-table-p)]
  19257. ["Move subtree to Archive sibling" org-archive-to-archive-sibling (org-in-subtree-not-table-p)]
  19258. )
  19259. "--"
  19260. ("Hyperlinks"
  19261. ["Store Link (Global)" org-store-link t]
  19262. ["Find existing link to here" org-occur-link-in-agenda-files t]
  19263. ["Insert Link" org-insert-link t]
  19264. ["Follow Link" org-open-at-point t]
  19265. "--"
  19266. ["Next link" org-next-link t]
  19267. ["Previous link" org-previous-link t]
  19268. "--"
  19269. ["Descriptive Links"
  19270. org-toggle-link-display
  19271. :style radio
  19272. :selected org-descriptive-links
  19273. ]
  19274. ["Literal Links"
  19275. org-toggle-link-display
  19276. :style radio
  19277. :selected (not org-descriptive-links)])
  19278. "--"
  19279. ("TODO Lists"
  19280. ["TODO/DONE/-" org-todo t]
  19281. ("Select keyword"
  19282. ["Next keyword" org-shiftright (org-at-heading-p)]
  19283. ["Previous keyword" org-shiftleft (org-at-heading-p)]
  19284. ["Complete Keyword" pcomplete (assq :todo-keyword (org-context))]
  19285. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))]
  19286. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))])
  19287. ["Show TODO Tree" org-show-todo-tree :active t :keys "C-c / t"]
  19288. ["Global TODO list" org-todo-list :active t :keys "C-c a t"]
  19289. "--"
  19290. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  19291. :selected org-enforce-todo-dependencies :style toggle :active t]
  19292. "Settings for tree at point"
  19293. ["Do Children sequentially" org-toggle-ordered-property :style radio
  19294. :selected (org-entry-get nil "ORDERED")
  19295. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  19296. ["Do Children parallel" org-toggle-ordered-property :style radio
  19297. :selected (not (org-entry-get nil "ORDERED"))
  19298. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  19299. "--"
  19300. ["Set Priority" org-priority t]
  19301. ["Priority Up" org-shiftup t]
  19302. ["Priority Down" org-shiftdown t]
  19303. "--"
  19304. ["Get news from all feeds" org-feed-update-all t]
  19305. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  19306. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  19307. ("TAGS and Properties"
  19308. ["Set Tags" org-set-tags-command (not (org-before-first-heading-p))]
  19309. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  19310. "--"
  19311. ["Set property" org-set-property (not (org-before-first-heading-p))]
  19312. ["Column view of properties" org-columns t]
  19313. ["Insert Column View DBlock" org-insert-columns-dblock t])
  19314. ("Dates and Scheduling"
  19315. ["Timestamp" org-time-stamp (not (org-before-first-heading-p))]
  19316. ["Timestamp (inactive)" org-time-stamp-inactive (not (org-before-first-heading-p))]
  19317. ("Change Date"
  19318. ["1 Day Later" org-shiftright (org-at-timestamp-p)]
  19319. ["1 Day Earlier" org-shiftleft (org-at-timestamp-p)]
  19320. ["1 ... Later" org-shiftup (org-at-timestamp-p)]
  19321. ["1 ... Earlier" org-shiftdown (org-at-timestamp-p)])
  19322. ["Compute Time Range" org-evaluate-time-range t]
  19323. ["Schedule Item" org-schedule (not (org-before-first-heading-p))]
  19324. ["Deadline" org-deadline (not (org-before-first-heading-p))]
  19325. "--"
  19326. ["Custom time format" org-toggle-time-stamp-overlays
  19327. :style radio :selected org-display-custom-times]
  19328. "--"
  19329. ["Goto Calendar" org-goto-calendar t]
  19330. ["Date from Calendar" org-date-from-calendar t]
  19331. "--"
  19332. ["Start/Restart Timer" org-timer-start t]
  19333. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  19334. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  19335. ["Insert Timer String" org-timer t]
  19336. ["Insert Timer Item" org-timer-item t])
  19337. ("Logging work"
  19338. ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
  19339. ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
  19340. ["Clock out" org-clock-out t]
  19341. ["Clock cancel" org-clock-cancel t]
  19342. "--"
  19343. ["Mark as default task" org-clock-mark-default-task t]
  19344. ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
  19345. ["Goto running clock" org-clock-goto t]
  19346. "--"
  19347. ["Display times" org-clock-display t]
  19348. ["Create clock table" org-clock-report t]
  19349. "--"
  19350. ["Record DONE time"
  19351. (progn (setq org-log-done (not org-log-done))
  19352. (message "Switching to %s will %s record a timestamp"
  19353. (car org-done-keywords)
  19354. (if org-log-done "automatically" "not")))
  19355. :style toggle :selected org-log-done])
  19356. "--"
  19357. ["Agenda Command..." org-agenda t]
  19358. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  19359. ("File List for Agenda")
  19360. ("Special views current file"
  19361. ["TODO Tree" org-show-todo-tree t]
  19362. ["Check Deadlines" org-check-deadlines t]
  19363. ["Timeline" org-timeline t]
  19364. ["Tags/Property tree" org-match-sparse-tree t])
  19365. "--"
  19366. ["Export/Publish..." org-export-dispatch t]
  19367. ("LaTeX"
  19368. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  19369. :selected org-cdlatex-mode]
  19370. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  19371. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  19372. ["Modify math symbol" org-cdlatex-math-modify
  19373. (org-inside-LaTeX-fragment-p)]
  19374. ["Insert citation" org-reftex-citation t]
  19375. "--"
  19376. ["Template for BEAMER" (org-beamer-insert-options-template) t])
  19377. "--"
  19378. ("MobileOrg"
  19379. ["Push Files and Views" org-mobile-push t]
  19380. ["Get Captured and Flagged" org-mobile-pull t]
  19381. ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
  19382. "--"
  19383. ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
  19384. "--"
  19385. ("Documentation"
  19386. ["Show Version" org-version t]
  19387. ["Info Documentation" org-info t])
  19388. ("Customize"
  19389. ["Browse Org Group" org-customize t]
  19390. "--"
  19391. ["Expand This Menu" org-create-customize-menu
  19392. (fboundp 'customize-menu-create)])
  19393. ["Send bug report" org-submit-bug-report t]
  19394. "--"
  19395. ("Refresh/Reload"
  19396. ["Refresh setup current buffer" org-mode-restart t]
  19397. ["Reload Org (after update)" org-reload t]
  19398. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x !"])
  19399. ))
  19400. (defun org-info (&optional node)
  19401. "Read documentation for Org-mode in the info system.
  19402. With optional NODE, go directly to that node."
  19403. (interactive)
  19404. (info (format "(org)%s" (or node ""))))
  19405. ;;;###autoload
  19406. (defun org-submit-bug-report ()
  19407. "Submit a bug report on Org-mode via mail.
  19408. Don't hesitate to report any problems or inaccurate documentation.
  19409. If you don't have setup sending mail from (X)Emacs, please copy the
  19410. output buffer into your mail program, as it gives us important
  19411. information about your Org-mode version and configuration."
  19412. (interactive)
  19413. (require 'reporter)
  19414. (org-load-modules-maybe)
  19415. (org-require-autoloaded-modules)
  19416. (let ((reporter-prompt-for-summary-p "Bug report subject: "))
  19417. (reporter-submit-bug-report
  19418. "emacs-orgmode@gnu.org"
  19419. (org-version nil 'full)
  19420. (let (list)
  19421. (save-window-excursion
  19422. (org-pop-to-buffer-same-window (get-buffer-create "*Warn about privacy*"))
  19423. (delete-other-windows)
  19424. (erase-buffer)
  19425. (insert "You are about to submit a bug report to the Org-mode mailing list.
  19426. We would like to add your full Org-mode and Outline configuration to the
  19427. bug report. This greatly simplifies the work of the maintainer and
  19428. other experts on the mailing list.
  19429. HOWEVER, some variables you have customized may contain private
  19430. information. The names of customers, colleagues, or friends, might
  19431. appear in the form of file names, tags, todo states, or search strings.
  19432. If you answer yes to the prompt, you might want to check and remove
  19433. such private information before sending the email.")
  19434. (add-text-properties (point-min) (point-max) '(face org-warning))
  19435. (when (yes-or-no-p "Include your Org-mode configuration ")
  19436. (mapatoms
  19437. (lambda (v)
  19438. (and (boundp v)
  19439. (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
  19440. (or (and (symbol-value v)
  19441. (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
  19442. (and
  19443. (get v 'custom-type) (get v 'standard-value)
  19444. (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
  19445. (push v list)))))
  19446. (kill-buffer (get-buffer "*Warn about privacy*"))
  19447. list))
  19448. nil nil
  19449. "Remember to cover the basics, that is, what you expected to happen and
  19450. what in fact did happen. You don't know how to make a good report? See
  19451. http://orgmode.org/manual/Feedback.html#Feedback
  19452. Your bug report will be posted to the Org-mode mailing list.
  19453. ------------------------------------------------------------------------")
  19454. (save-excursion
  19455. (if (re-search-backward "^\\(Subject: \\)Org-mode version \\(.*?\\);[ \t]*\\(.*\\)" nil t)
  19456. (replace-match "\\1Bug: \\3 [\\2]")))))
  19457. (defun org-install-agenda-files-menu ()
  19458. (let ((bl (buffer-list)))
  19459. (save-excursion
  19460. (while bl
  19461. (set-buffer (pop bl))
  19462. (if (derived-mode-p 'org-mode) (setq bl nil)))
  19463. (when (derived-mode-p 'org-mode)
  19464. (easy-menu-change
  19465. '("Org") "File List for Agenda"
  19466. (append
  19467. (list
  19468. ["Edit File List" (org-edit-agenda-file-list) t]
  19469. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  19470. ["Remove Current File from List" org-remove-file t]
  19471. ["Cycle through agenda files" org-cycle-agenda-files t]
  19472. ["Occur in all agenda files" org-occur-in-agenda-files t]
  19473. "--")
  19474. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  19475. ;;;; Documentation
  19476. (defun org-require-autoloaded-modules ()
  19477. (interactive)
  19478. (mapc 'require
  19479. '(org-agenda org-archive org-attach org-clock org-colview org-id
  19480. org-table org-timer)))
  19481. ;;;###autoload
  19482. (defun org-reload (&optional uncompiled)
  19483. "Reload all org lisp files.
  19484. With prefix arg UNCOMPILED, load the uncompiled versions."
  19485. (interactive "P")
  19486. (require 'loadhist)
  19487. (let* ((org-dir (org-find-library-dir "org"))
  19488. (contrib-dir (or (org-find-library-dir "org-contribdir") org-dir))
  19489. (feature-re "^\\(org\\|ob\\|ox\\)\\(-.*\\)?")
  19490. (remove-re (mapconcat 'identity
  19491. (mapcar (lambda (f) (concat "^" f "$"))
  19492. (list (if (featurep 'xemacs)
  19493. "org-colview"
  19494. "org-colview-xemacs")
  19495. "org" "org-loaddefs" "org-version"))
  19496. "\\|"))
  19497. (feats (delete-dups
  19498. (mapcar 'file-name-sans-extension
  19499. (mapcar 'file-name-nondirectory
  19500. (delq nil
  19501. (mapcar 'feature-file
  19502. features))))))
  19503. (lfeat (append
  19504. (sort
  19505. (setq feats
  19506. (delq nil (mapcar
  19507. (lambda (f)
  19508. (if (and (string-match feature-re f)
  19509. (not (string-match remove-re f)))
  19510. f nil))
  19511. feats)))
  19512. 'string-lessp)
  19513. (list "org-version" "org")))
  19514. (load-suffixes (when (boundp 'load-suffixes) load-suffixes))
  19515. (load-suffixes (if uncompiled (reverse load-suffixes) load-suffixes))
  19516. load-uncore load-misses)
  19517. (setq load-misses
  19518. (delq 't
  19519. (mapcar (lambda (f)
  19520. (or (org-load-noerror-mustsuffix (concat org-dir f))
  19521. (and (string= org-dir contrib-dir)
  19522. (org-load-noerror-mustsuffix (concat contrib-dir f)))
  19523. (and (org-load-noerror-mustsuffix (concat (org-find-library-dir f) f))
  19524. (add-to-list 'load-uncore f 'append)
  19525. 't)
  19526. f))
  19527. lfeat)))
  19528. (if load-uncore
  19529. (message "The following feature%s found in load-path, please check if that's correct:\n%s"
  19530. (if (> (length load-uncore) 1) "s were" " was") load-uncore))
  19531. (if load-misses
  19532. (message "Some error occurred while reloading Org feature%s\n%s\nPlease check *Messages*!\n%s"
  19533. (if (> (length load-misses) 1) "s" "") load-misses (org-version nil 'full))
  19534. (message "Successfully reloaded Org\n%s" (org-version nil 'full)))))
  19535. ;;;###autoload
  19536. (defun org-customize ()
  19537. "Call the customize function with org as argument."
  19538. (interactive)
  19539. (org-load-modules-maybe)
  19540. (org-require-autoloaded-modules)
  19541. (customize-browse 'org))
  19542. (defun org-create-customize-menu ()
  19543. "Create a full customization menu for Org-mode, insert it into the menu."
  19544. (interactive)
  19545. (org-load-modules-maybe)
  19546. (org-require-autoloaded-modules)
  19547. (if (fboundp 'customize-menu-create)
  19548. (progn
  19549. (easy-menu-change
  19550. '("Org") "Customize"
  19551. `(["Browse Org group" org-customize t]
  19552. "--"
  19553. ,(customize-menu-create 'org)
  19554. ["Set" Custom-set t]
  19555. ["Save" Custom-save t]
  19556. ["Reset to Current" Custom-reset-current t]
  19557. ["Reset to Saved" Custom-reset-saved t]
  19558. ["Reset to Standard Settings" Custom-reset-standard t]))
  19559. (message "\"Org\"-menu now contains full customization menu"))
  19560. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  19561. ;;;; Miscellaneous stuff
  19562. ;;; Generally useful functions
  19563. (defun org-get-at-bol (property)
  19564. "Get text property PROPERTY at beginning of line."
  19565. (get-text-property (point-at-bol) property))
  19566. (defun org-find-text-property-in-string (prop s)
  19567. "Return the first non-nil value of property PROP in string S."
  19568. (or (get-text-property 0 prop s)
  19569. (get-text-property (or (next-single-property-change 0 prop s) 0)
  19570. prop s)))
  19571. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  19572. "Display the given MESSAGE as a warning."
  19573. (if (fboundp 'display-warning)
  19574. (display-warning 'org message
  19575. (if (featurep 'xemacs) 'warning :warning))
  19576. (let ((buf (get-buffer-create "*Org warnings*")))
  19577. (with-current-buffer buf
  19578. (goto-char (point-max))
  19579. (insert "Warning (Org): " message)
  19580. (unless (bolp)
  19581. (newline)))
  19582. (display-buffer buf)
  19583. (sit-for 0))))
  19584. (defun org-eval (form)
  19585. "Eval FORM and return result."
  19586. (condition-case error
  19587. (eval form)
  19588. (error (format "%%![Error: %s]" error))))
  19589. (defun org-in-clocktable-p ()
  19590. "Check if the cursor is in a clocktable."
  19591. (let ((pos (point)) start)
  19592. (save-excursion
  19593. (end-of-line 1)
  19594. (and (re-search-backward "^[ \t]*#\\+BEGIN:[ \t]+clocktable" nil t)
  19595. (setq start (match-beginning 0))
  19596. (re-search-forward "^[ \t]*#\\+END:.*" nil t)
  19597. (>= (match-end 0) pos)
  19598. start))))
  19599. (defun org-in-commented-line ()
  19600. "Is point in a line starting with `#'?"
  19601. (equal (char-after (point-at-bol)) ?#))
  19602. (defun org-in-indented-comment-line ()
  19603. "Is point in a line starting with `#' after some white space?"
  19604. (save-excursion
  19605. (save-match-data
  19606. (goto-char (point-at-bol))
  19607. (looking-at "[ \t]*#"))))
  19608. (defun org-in-verbatim-emphasis ()
  19609. (save-match-data
  19610. (and (org-in-regexp org-emph-re 2)
  19611. (>= (point) (match-beginning 3))
  19612. (<= (point) (match-end 4))
  19613. (member (match-string 3) '("=" "~")))))
  19614. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  19615. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  19616. (if (and marker (marker-buffer marker)
  19617. (buffer-live-p (marker-buffer marker)))
  19618. (progn
  19619. (org-pop-to-buffer-same-window (marker-buffer marker))
  19620. (if (or (> marker (point-max)) (< marker (point-min)))
  19621. (widen))
  19622. (goto-char marker)
  19623. (org-show-context 'org-goto))
  19624. (if bookmark
  19625. (bookmark-jump bookmark)
  19626. (error "Cannot find location"))))
  19627. (defun org-quote-csv-field (s)
  19628. "Quote field for inclusion in CSV material."
  19629. (if (string-match "[\",]" s)
  19630. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  19631. s))
  19632. (defun org-force-self-insert (N)
  19633. "Needed to enforce self-insert under remapping."
  19634. (interactive "p")
  19635. (self-insert-command N))
  19636. (defun org-string-width (s)
  19637. "Compute width of string, ignoring invisible characters.
  19638. This ignores character with invisibility property `org-link', and also
  19639. characters with property `org-cwidth', because these will become invisible
  19640. upon the next fontification round."
  19641. (let (b l)
  19642. (when (or (eq t buffer-invisibility-spec)
  19643. (assq 'org-link buffer-invisibility-spec))
  19644. (while (setq b (text-property-any 0 (length s)
  19645. 'invisible 'org-link s))
  19646. (setq s (concat (substring s 0 b)
  19647. (substring s (or (next-single-property-change
  19648. b 'invisible s) (length s)))))))
  19649. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  19650. (setq s (concat (substring s 0 b)
  19651. (substring s (or (next-single-property-change
  19652. b 'org-cwidth s) (length s))))))
  19653. (setq l (string-width s) b -1)
  19654. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  19655. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  19656. l))
  19657. (defun org-shorten-string (s maxlength)
  19658. "Shorten string S so tht it is no longer than MAXLENGTH characters.
  19659. If the string is shorter or has length MAXLENGTH, just return the
  19660. original string. If it is longer, the functions finds a space in the
  19661. string, breaks this string off at that locations and adds three dots
  19662. as ellipsis. Including the ellipsis, the string will not be longer
  19663. than MAXLENGTH. If finding a good breaking point in the string does
  19664. not work, the string is just chopped off in the middle of a word
  19665. if necessary."
  19666. (if (<= (length s) maxlength)
  19667. s
  19668. (let* ((n (max (- maxlength 4) 1))
  19669. (re (concat "\\`\\(.\\{1," (int-to-string n) "\\}[^ ]\\)\\([ ]\\|\\'\\)")))
  19670. (if (string-match re s)
  19671. (concat (match-string 1 s) "...")
  19672. (concat (substring s 0 (max (- maxlength 3) 0)) "...")))))
  19673. (defun org-get-indentation (&optional line)
  19674. "Get the indentation of the current line, interpreting tabs.
  19675. When LINE is given, assume it represents a line and compute its indentation."
  19676. (if line
  19677. (if (string-match "^ *" (org-remove-tabs line))
  19678. (match-end 0))
  19679. (save-excursion
  19680. (beginning-of-line 1)
  19681. (skip-chars-forward " \t")
  19682. (current-column))))
  19683. (defun org-get-string-indentation (s)
  19684. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  19685. (let ((n -1) (i 0) (w tab-width) c)
  19686. (catch 'exit
  19687. (while (< (setq n (1+ n)) (length s))
  19688. (setq c (aref s n))
  19689. (cond ((= c ?\ ) (setq i (1+ i)))
  19690. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  19691. (t (throw 'exit t)))))
  19692. i))
  19693. (defun org-remove-tabs (s &optional width)
  19694. "Replace tabulators in S with spaces.
  19695. Assumes that s is a single line, starting in column 0."
  19696. (setq width (or width tab-width))
  19697. (while (string-match "\t" s)
  19698. (setq s (replace-match
  19699. (make-string
  19700. (- (* width (/ (+ (match-beginning 0) width) width))
  19701. (match-beginning 0)) ?\ )
  19702. t t s)))
  19703. s)
  19704. (defun org-fix-indentation (line ind)
  19705. "Fix indentation in LINE.
  19706. IND is a cons cell with target and minimum indentation.
  19707. If the current indentation in LINE is smaller than the minimum,
  19708. leave it alone. If it is larger than ind, set it to the target."
  19709. (let* ((l (org-remove-tabs line))
  19710. (i (org-get-indentation l))
  19711. (i1 (car ind)) (i2 (cdr ind)))
  19712. (if (>= i i2) (setq l (substring line i2)))
  19713. (if (> i1 0)
  19714. (concat (make-string i1 ?\ ) l)
  19715. l)))
  19716. (defun org-remove-indentation (code &optional n)
  19717. "Remove the maximum common indentation from the lines in CODE.
  19718. N may optionally be the number of spaces to remove."
  19719. (with-temp-buffer
  19720. (insert code)
  19721. (org-do-remove-indentation n)
  19722. (buffer-string)))
  19723. (defun org-do-remove-indentation (&optional n)
  19724. "Remove the maximum common indentation from the buffer."
  19725. (untabify (point-min) (point-max))
  19726. (let ((min 10000) re)
  19727. (if n
  19728. (setq min n)
  19729. (goto-char (point-min))
  19730. (while (re-search-forward "^ *[^ \n]" nil t)
  19731. (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
  19732. (unless (or (= min 0) (= min 10000))
  19733. (setq re (format "^ \\{%d\\}" min))
  19734. (goto-char (point-min))
  19735. (while (re-search-forward re nil t)
  19736. (replace-match "")
  19737. (end-of-line 1))
  19738. min)))
  19739. (defun org-fill-template (template alist)
  19740. "Find each %key of ALIST in TEMPLATE and replace it."
  19741. (let ((case-fold-search nil)
  19742. entry key value)
  19743. (setq alist (sort (copy-sequence alist)
  19744. (lambda (a b) (< (length (car a)) (length (car b))))))
  19745. (while (setq entry (pop alist))
  19746. (setq template
  19747. (replace-regexp-in-string
  19748. (concat "%" (regexp-quote (car entry)))
  19749. (or (cdr entry) "") template t t)))
  19750. template))
  19751. (defun org-base-buffer (buffer)
  19752. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  19753. (if (not buffer)
  19754. buffer
  19755. (or (buffer-base-buffer buffer)
  19756. buffer)))
  19757. (defun org-trim (s)
  19758. "Remove whitespace at beginning and end of string."
  19759. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  19760. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  19761. s)
  19762. (defun org-wrap (string &optional width lines)
  19763. "Wrap string to either a number of lines, or a width in characters.
  19764. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  19765. that costs. If there is a word longer than WIDTH, the text is actually
  19766. wrapped to the length of that word.
  19767. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  19768. many lines, whatever width that takes.
  19769. The return value is a list of lines, without newlines at the end."
  19770. (let* ((words (org-split-string string "[ \t\n]+"))
  19771. (maxword (apply 'max (mapcar 'org-string-width words)))
  19772. w ll)
  19773. (cond (width
  19774. (org-do-wrap words (max maxword width)))
  19775. (lines
  19776. (setq w maxword)
  19777. (setq ll (org-do-wrap words maxword))
  19778. (if (<= (length ll) lines)
  19779. ll
  19780. (setq ll words)
  19781. (while (> (length ll) lines)
  19782. (setq w (1+ w))
  19783. (setq ll (org-do-wrap words w)))
  19784. ll))
  19785. (t (error "Cannot wrap this")))))
  19786. (defun org-do-wrap (words width)
  19787. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  19788. (let (lines line)
  19789. (while words
  19790. (setq line (pop words))
  19791. (while (and words (< (+ (length line) (length (car words))) width))
  19792. (setq line (concat line " " (pop words))))
  19793. (setq lines (push line lines)))
  19794. (nreverse lines)))
  19795. (defun org-split-string (string &optional separators)
  19796. "Splits STRING into substrings at SEPARATORS.
  19797. No empty strings are returned if there are matches at the beginning
  19798. and end of string."
  19799. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  19800. (start 0)
  19801. notfirst
  19802. (list nil))
  19803. (while (and (string-match rexp string
  19804. (if (and notfirst
  19805. (= start (match-beginning 0))
  19806. (< start (length string)))
  19807. (1+ start) start))
  19808. (< (match-beginning 0) (length string)))
  19809. (setq notfirst t)
  19810. (or (eq (match-beginning 0) 0)
  19811. (and (eq (match-beginning 0) (match-end 0))
  19812. (eq (match-beginning 0) start))
  19813. (setq list
  19814. (cons (substring string start (match-beginning 0))
  19815. list)))
  19816. (setq start (match-end 0)))
  19817. (or (eq start (length string))
  19818. (setq list
  19819. (cons (substring string start)
  19820. list)))
  19821. (nreverse list)))
  19822. (defun org-quote-vert (s)
  19823. "Replace \"|\" with \"\\vert\"."
  19824. (while (string-match "|" s)
  19825. (setq s (replace-match "\\vert" t t s)))
  19826. s)
  19827. (defun org-uuidgen-p (s)
  19828. "Is S an ID created by UUIDGEN?"
  19829. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  19830. (defun org-in-src-block-p (&optional inside)
  19831. "Whether point is in a code source block.
  19832. When INSIDE is non-nil, don't consider we are within a src block
  19833. when point is at #+BEGIN_SRC or #+END_SRC."
  19834. (let ((case-fold-search t) ov)
  19835. (or (and (setq ov (overlays-at (point)))
  19836. (memq 'org-block-background
  19837. (overlay-properties (car ov))))
  19838. (and (not inside)
  19839. (save-match-data
  19840. (save-excursion
  19841. (beginning-of-line)
  19842. (looking-at ".*#\\+\\(begin\\|end\\)_src")))))))
  19843. (defun org-context ()
  19844. "Return a list of contexts of the current cursor position.
  19845. If several contexts apply, all are returned.
  19846. Each context entry is a list with a symbol naming the context, and
  19847. two positions indicating start and end of the context. Possible
  19848. contexts are:
  19849. :headline anywhere in a headline
  19850. :headline-stars on the leading stars in a headline
  19851. :todo-keyword on a TODO keyword (including DONE) in a headline
  19852. :tags on the TAGS in a headline
  19853. :priority on the priority cookie in a headline
  19854. :item on the first line of a plain list item
  19855. :item-bullet on the bullet/number of a plain list item
  19856. :checkbox on the checkbox in a plain list item
  19857. :table in an org-mode table
  19858. :table-special on a special filed in a table
  19859. :table-table in a table.el table
  19860. :clocktable in a clocktable
  19861. :src-block in a source block
  19862. :link on a hyperlink
  19863. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE, COMMENT, QUOTE.
  19864. :target on a <<target>>
  19865. :radio-target on a <<<radio-target>>>
  19866. :latex-fragment on a LaTeX fragment
  19867. :latex-preview on a LaTeX fragment with overlaid preview image
  19868. This function expects the position to be visible because it uses font-lock
  19869. faces as a help to recognize the following contexts: :table-special, :link,
  19870. and :keyword."
  19871. (let* ((f (get-text-property (point) 'face))
  19872. (faces (if (listp f) f (list f)))
  19873. (case-fold-search t)
  19874. (p (point)) clist o)
  19875. ;; First the large context
  19876. (cond
  19877. ((org-at-heading-p t)
  19878. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  19879. (when (progn
  19880. (beginning-of-line 1)
  19881. (looking-at org-todo-line-tags-regexp))
  19882. (push (org-point-in-group p 1 :headline-stars) clist)
  19883. (push (org-point-in-group p 2 :todo-keyword) clist)
  19884. (push (org-point-in-group p 4 :tags) clist))
  19885. (goto-char p)
  19886. (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
  19887. (if (looking-at "\\[#[A-Z0-9]\\]")
  19888. (push (org-point-in-group p 0 :priority) clist)))
  19889. ((org-at-item-p)
  19890. (push (org-point-in-group p 2 :item-bullet) clist)
  19891. (push (list :item (point-at-bol)
  19892. (save-excursion (org-end-of-item) (point)))
  19893. clist)
  19894. (and (org-at-item-checkbox-p)
  19895. (push (org-point-in-group p 0 :checkbox) clist)))
  19896. ((org-at-table-p)
  19897. (push (list :table (org-table-begin) (org-table-end)) clist)
  19898. (if (memq 'org-formula faces)
  19899. (push (list :table-special
  19900. (previous-single-property-change p 'face)
  19901. (next-single-property-change p 'face)) clist)))
  19902. ((org-at-table-p 'any)
  19903. (push (list :table-table) clist)))
  19904. (goto-char p)
  19905. (let ((case-fold-search t))
  19906. ;; New the "medium" contexts: clocktables, source blocks
  19907. (cond ((org-in-clocktable-p)
  19908. (push (list :clocktable
  19909. (and (or (looking-at "#\\+BEGIN: clocktable")
  19910. (search-backward "#+BEGIN: clocktable" nil t))
  19911. (match-beginning 0))
  19912. (and (re-search-forward "#\\+END:?" nil t)
  19913. (match-end 0))) clist))
  19914. ((org-in-src-block-p)
  19915. (push (list :src-block
  19916. (and (or (looking-at "#\\+BEGIN_SRC")
  19917. (search-backward "#+BEGIN_SRC" nil t))
  19918. (match-beginning 0))
  19919. (and (search-forward "#+END_SRC" nil t)
  19920. (match-beginning 0))) clist))))
  19921. (goto-char p)
  19922. ;; Now the small context
  19923. (cond
  19924. ((org-at-timestamp-p)
  19925. (push (org-point-in-group p 0 :timestamp) clist))
  19926. ((memq 'org-link faces)
  19927. (push (list :link
  19928. (previous-single-property-change p 'face)
  19929. (next-single-property-change p 'face)) clist))
  19930. ((memq 'org-special-keyword faces)
  19931. (push (list :keyword
  19932. (previous-single-property-change p 'face)
  19933. (next-single-property-change p 'face)) clist))
  19934. ((org-at-target-p)
  19935. (push (org-point-in-group p 0 :target) clist)
  19936. (goto-char (1- (match-beginning 0)))
  19937. (if (looking-at org-radio-target-regexp)
  19938. (push (org-point-in-group p 0 :radio-target) clist))
  19939. (goto-char p))
  19940. ((setq o (car (delq nil
  19941. (mapcar
  19942. (lambda (x)
  19943. (if (memq x org-latex-fragment-image-overlays) x))
  19944. (overlays-at (point))))))
  19945. (push (list :latex-fragment
  19946. (overlay-start o) (overlay-end o)) clist)
  19947. (push (list :latex-preview
  19948. (overlay-start o) (overlay-end o)) clist))
  19949. ((org-inside-LaTeX-fragment-p)
  19950. ;; FIXME: positions wrong.
  19951. (push (list :latex-fragment (point) (point)) clist)))
  19952. (setq clist (nreverse (delq nil clist)))
  19953. clist))
  19954. ;; FIXME: Compare with at-regexp-p Do we need both?
  19955. (defun org-in-regexp (re &optional nlines visually)
  19956. "Check if point is inside a match of regexp.
  19957. Normally only the current line is checked, but you can include NLINES extra
  19958. lines both before and after point into the search.
  19959. If VISUALLY is set, require that the cursor is not after the match but
  19960. really on, so that the block visually is on the match."
  19961. (catch 'exit
  19962. (let ((pos (point))
  19963. (eol (point-at-eol (+ 1 (or nlines 0))))
  19964. (inc (if visually 1 0)))
  19965. (save-excursion
  19966. (beginning-of-line (- 1 (or nlines 0)))
  19967. (while (re-search-forward re eol t)
  19968. (if (and (<= (match-beginning 0) pos)
  19969. (>= (+ inc (match-end 0)) pos))
  19970. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  19971. (defun org-at-regexp-p (regexp)
  19972. "Is point inside a match of REGEXP in the current line?"
  19973. (catch 'exit
  19974. (save-excursion
  19975. (let ((pos (point)) (end (point-at-eol)))
  19976. (beginning-of-line 1)
  19977. (while (re-search-forward regexp end t)
  19978. (if (and (<= (match-beginning 0) pos)
  19979. (>= (match-end 0) pos))
  19980. (throw 'exit t)))
  19981. nil))))
  19982. (defun org-between-regexps-p (start-re end-re &optional lim-up lim-down)
  19983. "Non-nil when point is between matches of START-RE and END-RE.
  19984. Also return a non-nil value when point is on one of the matches.
  19985. Optional arguments LIM-UP and LIM-DOWN bound the search; they are
  19986. buffer positions. Default values are the positions of headlines
  19987. surrounding the point.
  19988. The functions returns a cons cell whose car (resp. cdr) is the
  19989. position before START-RE (resp. after END-RE)."
  19990. (save-match-data
  19991. (let ((pos (point))
  19992. (limit-up (or lim-up (save-excursion (outline-previous-heading))))
  19993. (limit-down (or lim-down (save-excursion (outline-next-heading))))
  19994. beg end)
  19995. (save-excursion
  19996. ;; Point is on a block when on START-RE or if START-RE can be
  19997. ;; found before it...
  19998. (and (or (org-at-regexp-p start-re)
  19999. (re-search-backward start-re limit-up t))
  20000. (setq beg (match-beginning 0))
  20001. ;; ... and END-RE after it...
  20002. (goto-char (match-end 0))
  20003. (re-search-forward end-re limit-down t)
  20004. (> (setq end (match-end 0)) pos)
  20005. ;; ... without another START-RE in-between.
  20006. (goto-char (match-beginning 0))
  20007. (not (re-search-backward start-re (1+ beg) t))
  20008. ;; Return value.
  20009. (cons beg end))))))
  20010. (defun org-in-block-p (names)
  20011. "Non-nil when point belongs to a block whose name belongs to NAMES.
  20012. NAMES is a list of strings containing names of blocks.
  20013. Return first block name matched, or nil. Beware that in case of
  20014. nested blocks, the returned name may not belong to the closest
  20015. block from point."
  20016. (save-match-data
  20017. (catch 'exit
  20018. (let ((case-fold-search t)
  20019. (lim-up (save-excursion (outline-previous-heading)))
  20020. (lim-down (save-excursion (outline-next-heading))))
  20021. (mapc (lambda (name)
  20022. (let ((n (regexp-quote name)))
  20023. (when (org-between-regexps-p
  20024. (concat "^[ \t]*#\\+begin_" n)
  20025. (concat "^[ \t]*#\\+end_" n)
  20026. lim-up lim-down)
  20027. (throw 'exit n))))
  20028. names))
  20029. nil)))
  20030. (defun org-in-drawer-p ()
  20031. "Is point within a drawer?"
  20032. (save-match-data
  20033. (let ((case-fold-search t)
  20034. (lim-up (save-excursion (outline-previous-heading)))
  20035. (lim-down (save-excursion (outline-next-heading))))
  20036. (org-between-regexps-p
  20037. (concat "^[ \t]*:" (regexp-opt org-drawers) ":")
  20038. "^[ \t]*:end:.*$"
  20039. lim-up lim-down))))
  20040. (defun org-occur-in-agenda-files (regexp &optional nlines)
  20041. "Call `multi-occur' with buffers for all agenda files."
  20042. (interactive "sOrg-files matching: \np")
  20043. (let* ((files (org-agenda-files))
  20044. (tnames (mapcar 'file-truename files))
  20045. (extra org-agenda-text-search-extra-files)
  20046. f)
  20047. (when (eq (car extra) 'agenda-archives)
  20048. (setq extra (cdr extra))
  20049. (setq files (org-add-archive-files files)))
  20050. (while (setq f (pop extra))
  20051. (unless (member (file-truename f) tnames)
  20052. (add-to-list 'files f 'append)
  20053. (add-to-list 'tnames (file-truename f) 'append)))
  20054. (multi-occur
  20055. (mapcar (lambda (x)
  20056. (with-current-buffer
  20057. (or (get-file-buffer x) (find-file-noselect x))
  20058. (widen)
  20059. (current-buffer)))
  20060. files)
  20061. regexp)))
  20062. (if (boundp 'occur-mode-find-occurrence-hook)
  20063. ;; Emacs 23
  20064. (add-hook 'occur-mode-find-occurrence-hook
  20065. (lambda ()
  20066. (when (derived-mode-p 'org-mode)
  20067. (org-reveal))))
  20068. ;; Emacs 22
  20069. (defadvice occur-mode-goto-occurrence
  20070. (after org-occur-reveal activate)
  20071. (and (derived-mode-p 'org-mode) (org-reveal)))
  20072. (defadvice occur-mode-goto-occurrence-other-window
  20073. (after org-occur-reveal activate)
  20074. (and (derived-mode-p 'org-mode) (org-reveal)))
  20075. (defadvice occur-mode-display-occurrence
  20076. (after org-occur-reveal activate)
  20077. (when (derived-mode-p 'org-mode)
  20078. (let ((pos (occur-mode-find-occurrence)))
  20079. (with-current-buffer (marker-buffer pos)
  20080. (save-excursion
  20081. (goto-char pos)
  20082. (org-reveal)))))))
  20083. (defun org-occur-link-in-agenda-files ()
  20084. "Create a link and search for it in the agendas.
  20085. The link is not stored in `org-stored-links', it is just created
  20086. for the search purpose."
  20087. (interactive)
  20088. (let ((link (condition-case nil
  20089. (org-store-link nil)
  20090. (error "Unable to create a link to here"))))
  20091. (org-occur-in-agenda-files (regexp-quote link))))
  20092. (defun org-reverse-string (string)
  20093. "Return the reverse of STRING."
  20094. (apply 'string (reverse (string-to-list string))))
  20095. ;; defsubst org-uniquify must be defined before first use
  20096. (defun org-uniquify-alist (alist)
  20097. "Merge elements of ALIST with the same key.
  20098. For example, in this alist:
  20099. \(org-uniquify-alist '((a 1) (b 2) (a 3)))
  20100. => '((a 1 3) (b 2))
  20101. merge (a 1) and (a 3) into (a 1 3).
  20102. The function returns the new ALIST."
  20103. (let (rtn)
  20104. (mapc
  20105. (lambda (e)
  20106. (let (n)
  20107. (if (not (assoc (car e) rtn))
  20108. (push e rtn)
  20109. (setq n (cons (car e) (append (cdr (assoc (car e) rtn)) (cdr e))))
  20110. (setq rtn (assq-delete-all (car e) rtn))
  20111. (push n rtn))))
  20112. alist)
  20113. rtn))
  20114. (defun org-delete-all (elts list)
  20115. "Remove all elements in ELTS from LIST."
  20116. (while elts
  20117. (setq list (delete (pop elts) list)))
  20118. list)
  20119. (defun org-count (cl-item cl-seq)
  20120. "Count the number of occurrences of ITEM in SEQ.
  20121. Taken from `count' in cl-seq.el with all keyword arguments removed."
  20122. (let ((cl-end (length cl-seq)) (cl-start 0) (cl-count 0) cl-x)
  20123. (when (consp cl-seq) (setq cl-seq (nthcdr cl-start cl-seq)))
  20124. (while (< cl-start cl-end)
  20125. (setq cl-x (if (consp cl-seq) (pop cl-seq) (aref cl-seq cl-start)))
  20126. (if (equal cl-item cl-x) (setq cl-count (1+ cl-count)))
  20127. (setq cl-start (1+ cl-start)))
  20128. cl-count))
  20129. (defun org-remove-if (predicate seq)
  20130. "Remove everything from SEQ that fulfills PREDICATE."
  20131. (let (res e)
  20132. (while seq
  20133. (setq e (pop seq))
  20134. (if (not (funcall predicate e)) (push e res)))
  20135. (nreverse res)))
  20136. (defun org-remove-if-not (predicate seq)
  20137. "Remove everything from SEQ that does not fulfill PREDICATE."
  20138. (let (res e)
  20139. (while seq
  20140. (setq e (pop seq))
  20141. (if (funcall predicate e) (push e res)))
  20142. (nreverse res)))
  20143. (defun org-reduce (cl-func cl-seq &rest cl-keys)
  20144. "Reduce two-argument FUNCTION across SEQ.
  20145. Taken from `reduce' in cl-seq.el with all keyword arguments but
  20146. \":initial-value\" removed."
  20147. (let ((cl-accum (cond ((memq :initial-value cl-keys)
  20148. (cadr (memq :initial-value cl-keys)))
  20149. (cl-seq (pop cl-seq))
  20150. (t (funcall cl-func)))))
  20151. (while cl-seq
  20152. (setq cl-accum (funcall cl-func cl-accum (pop cl-seq))))
  20153. cl-accum))
  20154. (defun org-every (pred seq)
  20155. "Return true if PREDICATE is true of every element of SEQ.
  20156. Adapted from `every' in cl.el."
  20157. (catch 'org-every
  20158. (mapc (lambda (e) (unless (funcall pred e) (throw 'org-every nil))) seq)
  20159. t))
  20160. (defun org-some (pred seq)
  20161. "Return true if PREDICATE is true of any element of SEQ.
  20162. Adapted from `some' in cl.el."
  20163. (catch 'org-some
  20164. (mapc (lambda (e) (when (funcall pred e) (throw 'org-some t))) seq)
  20165. nil))
  20166. (defun org-back-over-empty-lines ()
  20167. "Move backwards over whitespace, to the beginning of the first empty line.
  20168. Returns the number of empty lines passed."
  20169. (let ((pos (point)))
  20170. (if (cdr (assoc 'heading org-blank-before-new-entry))
  20171. (skip-chars-backward " \t\n\r")
  20172. (unless (eobp)
  20173. (forward-line -1)))
  20174. (beginning-of-line 2)
  20175. (goto-char (min (point) pos))
  20176. (count-lines (point) pos)))
  20177. (defun org-skip-whitespace ()
  20178. (skip-chars-forward " \t\n\r"))
  20179. (defun org-point-in-group (point group &optional context)
  20180. "Check if POINT is in match-group GROUP.
  20181. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  20182. match. If the match group does not exist or point is not inside it,
  20183. return nil."
  20184. (and (match-beginning group)
  20185. (>= point (match-beginning group))
  20186. (<= point (match-end group))
  20187. (if context
  20188. (list context (match-beginning group) (match-end group))
  20189. t)))
  20190. (defun org-switch-to-buffer-other-window (&rest args)
  20191. "Switch to buffer in a second window on the current frame.
  20192. In particular, do not allow pop-up frames.
  20193. Returns the newly created buffer."
  20194. (org-no-popups
  20195. (apply 'switch-to-buffer-other-window args)))
  20196. (defun org-combine-plists (&rest plists)
  20197. "Create a single property list from all plists in PLISTS.
  20198. The process starts by copying the first list, and then setting properties
  20199. from the other lists. Settings in the last list are the most significant
  20200. ones and overrule settings in the other lists."
  20201. (let ((rtn (copy-sequence (pop plists)))
  20202. p v ls)
  20203. (while plists
  20204. (setq ls (pop plists))
  20205. (while ls
  20206. (setq p (pop ls) v (pop ls))
  20207. (setq rtn (plist-put rtn p v))))
  20208. rtn))
  20209. (defun org-replace-escapes (string table)
  20210. "Replace %-escapes in STRING with values in TABLE.
  20211. TABLE is an association list with keys like \"%a\" and string values.
  20212. The sequences in STRING may contain normal field width and padding information,
  20213. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  20214. so values can contain further %-escapes if they are define later in TABLE."
  20215. (let ((tbl (copy-alist table))
  20216. (case-fold-search nil)
  20217. (pchg 0)
  20218. e re rpl)
  20219. (while (setq e (pop tbl))
  20220. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  20221. (when (and (cdr e) (string-match re (cdr e)))
  20222. (let ((sref (substring (cdr e) (match-beginning 0) (match-end 0)))
  20223. (safe "SREF"))
  20224. (add-text-properties 0 3 (list 'sref sref) safe)
  20225. (setcdr e (replace-match safe t t (cdr e)))))
  20226. (while (string-match re string)
  20227. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  20228. (cdr e)))
  20229. (setq string (replace-match rpl t t string))))
  20230. (while (setq pchg (next-property-change pchg string))
  20231. (let ((sref (get-text-property pchg 'sref string)))
  20232. (when (and sref (string-match "SREF" string pchg))
  20233. (setq string (replace-match sref t t string)))))
  20234. string))
  20235. (defun org-sublist (list start end)
  20236. "Return a section of LIST, from START to END.
  20237. Counting starts at 1."
  20238. (let (rtn (c start))
  20239. (setq list (nthcdr (1- start) list))
  20240. (while (and list (<= c end))
  20241. (push (pop list) rtn)
  20242. (setq c (1+ c)))
  20243. (nreverse rtn)))
  20244. (defun org-find-base-buffer-visiting (file)
  20245. "Like `find-buffer-visiting' but always return the base buffer and
  20246. not an indirect buffer."
  20247. (let ((buf (or (get-file-buffer file)
  20248. (find-buffer-visiting file))))
  20249. (if buf
  20250. (or (buffer-base-buffer buf) buf)
  20251. nil)))
  20252. (defun org-image-file-name-regexp (&optional extensions)
  20253. "Return regexp matching the file names of images.
  20254. If EXTENSIONS is given, only match these."
  20255. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  20256. (image-file-name-regexp)
  20257. (let ((image-file-name-extensions
  20258. (or extensions
  20259. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  20260. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  20261. (concat "\\."
  20262. (regexp-opt (nconc (mapcar 'upcase
  20263. image-file-name-extensions)
  20264. image-file-name-extensions)
  20265. t)
  20266. "\\'"))))
  20267. (defun org-file-image-p (file &optional extensions)
  20268. "Return non-nil if FILE is an image."
  20269. (save-match-data
  20270. (string-match (org-image-file-name-regexp extensions) file)))
  20271. (defun org-get-cursor-date (&optional with-time)
  20272. "Return the date at cursor in as a time.
  20273. This works in the calendar and in the agenda, anywhere else it just
  20274. returns the current time.
  20275. If WITH-TIME is non-nil, returns the time of the event at point (in
  20276. the agenda) or the current time of the day."
  20277. (let (date day defd tp tm hod mod)
  20278. (when with-time
  20279. (setq tp (get-text-property (point) 'time))
  20280. (when (and tp (string-match "\\([0-9][0-9]\\):\\([0-9][0-9]\\)" tp))
  20281. (setq hod (string-to-number (match-string 1 tp))
  20282. mod (string-to-number (match-string 2 tp))))
  20283. (or tp (setq hod (nth 2 (decode-time (current-time)))
  20284. mod (nth 1 (decode-time (current-time))))))
  20285. (cond
  20286. ((eq major-mode 'calendar-mode)
  20287. (setq date (calendar-cursor-to-date)
  20288. defd (encode-time 0 (or mod 0) (or hod 0)
  20289. (nth 1 date) (nth 0 date) (nth 2 date))))
  20290. ((eq major-mode 'org-agenda-mode)
  20291. (setq day (get-text-property (point) 'day))
  20292. (if day
  20293. (setq date (calendar-gregorian-from-absolute day)
  20294. defd (encode-time 0 (or mod 0) (or hod 0)
  20295. (nth 1 date) (nth 0 date) (nth 2 date))))))
  20296. (or defd (current-time))))
  20297. (defun org-mark-subtree (&optional up)
  20298. "Mark the current subtree.
  20299. This puts point at the start of the current subtree, and mark at
  20300. the end. If a numeric prefix UP is given, move up into the
  20301. hierarchy of headlines by UP levels before marking the subtree."
  20302. (interactive "P")
  20303. (org-with-limited-levels
  20304. (cond ((org-at-heading-p) (beginning-of-line))
  20305. ((org-before-first-heading-p) (user-error "Not in a subtree"))
  20306. (t (outline-previous-visible-heading 1))))
  20307. (when up (while (and (> up 0) (org-up-heading-safe)) (decf up)))
  20308. (if (org-called-interactively-p 'any)
  20309. (call-interactively 'org-mark-element)
  20310. (org-mark-element)))
  20311. ;;; Indentation
  20312. (defun org-indent-line ()
  20313. "Indent line depending on context."
  20314. (interactive)
  20315. (let* ((pos (point))
  20316. (itemp (org-at-item-p))
  20317. (case-fold-search t)
  20318. (org-drawer-regexp (or org-drawer-regexp "\000"))
  20319. (inline-task-p (and (featurep 'org-inlinetask)
  20320. (org-inlinetask-in-task-p)))
  20321. (inline-re (and inline-task-p
  20322. (org-inlinetask-outline-regexp)))
  20323. column)
  20324. (if (and orgstruct-is-++ (eq pos (point)))
  20325. (let ((indent-line-function (cadadr (assoc 'indent-line-function org-fb-vars))))
  20326. (indent-according-to-mode))
  20327. (beginning-of-line 1)
  20328. (cond
  20329. ;; Headings
  20330. ((looking-at org-outline-regexp) (setq column 0))
  20331. ;; Footnote definition
  20332. ((looking-at org-footnote-definition-re) (setq column 0))
  20333. ;; Literal examples
  20334. ((looking-at "[ \t]*:\\( \\|$\\)")
  20335. (setq column (org-get-indentation))) ; do nothing
  20336. ;; Lists
  20337. ((ignore-errors (goto-char (org-in-item-p)))
  20338. (setq column (if itemp
  20339. (org-get-indentation)
  20340. (org-list-item-body-column (point))))
  20341. (goto-char pos))
  20342. ;; Drawers
  20343. ((and (looking-at "[ \t]*:END:")
  20344. (save-excursion (re-search-backward org-drawer-regexp nil t)))
  20345. (save-excursion
  20346. (goto-char (1- (match-beginning 1)))
  20347. (setq column (current-column))))
  20348. ;; Special blocks
  20349. ((and (looking-at "[ \t]*#\\+end_\\([a-z]+\\)")
  20350. (save-excursion
  20351. (re-search-backward
  20352. (concat "^[ \t]*#\\+begin_" (downcase (match-string 1))) nil t)))
  20353. (setq column (org-get-indentation (match-string 0))))
  20354. ((and (not (looking-at "[ \t]*#\\+begin_"))
  20355. (org-between-regexps-p "^[ \t]*#\\+begin_" "[ \t]*#\\+end_"))
  20356. (save-excursion
  20357. (re-search-backward "^[ \t]*#\\+begin_\\([a-z]+\\)" nil t))
  20358. (setq column
  20359. (cond ((equal (downcase (match-string 1)) "src")
  20360. ;; src blocks: let `org-edit-src-exit' handle them
  20361. (org-get-indentation))
  20362. ((equal (downcase (match-string 1)) "example")
  20363. (max (org-get-indentation)
  20364. (org-get-indentation (match-string 0))))
  20365. (t
  20366. (org-get-indentation (match-string 0))))))
  20367. ;; This line has nothing special, look at the previous relevant
  20368. ;; line to compute indentation
  20369. (t
  20370. (beginning-of-line 0)
  20371. (while (and (not (bobp))
  20372. (not (looking-at org-table-line-regexp))
  20373. (not (looking-at org-drawer-regexp))
  20374. ;; When point started in an inline task, do not move
  20375. ;; above task starting line.
  20376. (not (and inline-task-p (looking-at inline-re)))
  20377. ;; Skip drawers, blocks, empty lines, verbatim,
  20378. ;; comments, tables, footnotes definitions, lists,
  20379. ;; inline tasks.
  20380. (or (and (looking-at "[ \t]*:END:")
  20381. (re-search-backward org-drawer-regexp nil t))
  20382. (and (looking-at "[ \t]*#\\+end_")
  20383. (re-search-backward "[ \t]*#\\+begin_"nil t))
  20384. (looking-at "[ \t]*[\n:#|]")
  20385. (looking-at org-footnote-definition-re)
  20386. (and (not inline-task-p)
  20387. (featurep 'org-inlinetask)
  20388. (org-inlinetask-in-task-p)
  20389. (or (org-inlinetask-goto-beginning) t))))
  20390. (beginning-of-line 0))
  20391. (cond
  20392. ;; There was a list item above.
  20393. ((ignore-errors (goto-char (org-in-item-p)))
  20394. (goto-char (org-list-get-top-point (org-list-struct)))
  20395. (setq column (org-get-indentation)))
  20396. ;; There was an heading above.
  20397. ((looking-at "\\*+[ \t]+")
  20398. (if (not org-adapt-indentation)
  20399. (setq column 0)
  20400. (goto-char (match-end 0))
  20401. (setq column (current-column))))
  20402. ;; A drawer had started and is unfinished
  20403. ((looking-at org-drawer-regexp)
  20404. (goto-char (1- (match-beginning 1)))
  20405. (setq column (current-column)))
  20406. ;; Else, nothing noticeable found: get indentation and go on.
  20407. (t (setq column (org-get-indentation))))))
  20408. ;; Now apply indentation and move cursor accordingly
  20409. (goto-char pos)
  20410. (if (<= (current-column) (current-indentation))
  20411. (org-indent-line-to column)
  20412. (save-excursion (org-indent-line-to column)))
  20413. ;; Special polishing for properties, see `org-property-format'
  20414. (setq column (current-column))
  20415. (beginning-of-line 1)
  20416. (if (looking-at org-property-re)
  20417. (replace-match (concat (match-string 4)
  20418. (format org-property-format
  20419. (match-string 1) (match-string 3)))
  20420. t t))
  20421. (org-move-to-column column))))
  20422. (defun org-indent-drawer ()
  20423. "Indent the drawer at point."
  20424. (interactive)
  20425. (let ((p (point))
  20426. (e (and (save-excursion (re-search-forward ":END:" nil t))
  20427. (match-end 0)))
  20428. (folded
  20429. (save-excursion
  20430. (end-of-line)
  20431. (when (overlays-at (point))
  20432. (member 'invisible (overlay-properties
  20433. (car (overlays-at (point)))))))))
  20434. (when folded (org-cycle))
  20435. (indent-for-tab-command)
  20436. (while (and (move-beginning-of-line 2) (< (point) e))
  20437. (indent-for-tab-command))
  20438. (goto-char p)
  20439. (when folded (org-cycle)))
  20440. (message "Drawer at point indented"))
  20441. (defun org-indent-block ()
  20442. "Indent the block at point."
  20443. (interactive)
  20444. (let ((p (point))
  20445. (case-fold-search t)
  20446. (e (and (save-excursion (re-search-forward "#\\+end_?\\(?:[a-z]+\\)?" nil t))
  20447. (match-end 0)))
  20448. (folded
  20449. (save-excursion
  20450. (end-of-line)
  20451. (when (overlays-at (point))
  20452. (member 'invisible (overlay-properties
  20453. (car (overlays-at (point)))))))))
  20454. (when folded (org-cycle))
  20455. (indent-for-tab-command)
  20456. (while (and (move-beginning-of-line 2) (< (point) e))
  20457. (indent-for-tab-command))
  20458. (goto-char p)
  20459. (when folded (org-cycle)))
  20460. (message "Block at point indented"))
  20461. (defun org-indent-region (start end)
  20462. "Indent region."
  20463. (interactive "r")
  20464. (save-excursion
  20465. (let ((line-end (org-current-line end)))
  20466. (goto-char start)
  20467. (while (< (org-current-line) line-end)
  20468. (cond ((org-in-src-block-p t) (org-src-native-tab-command-maybe))
  20469. (t (call-interactively 'org-indent-line)))
  20470. (move-beginning-of-line 2)))))
  20471. ;;; Filling
  20472. ;; We use our own fill-paragraph and auto-fill functions.
  20473. ;; `org-fill-paragraph' relies on adaptive filling and context
  20474. ;; checking. Appropriate `fill-prefix' is computed with
  20475. ;; `org-adaptive-fill-function'.
  20476. ;; `org-auto-fill-function' takes care of auto-filling. It calls
  20477. ;; `do-auto-fill' only on valid areas with `fill-prefix' shadowed with
  20478. ;; `org-adaptive-fill-function' value. Internally,
  20479. ;; `org-comment-line-break-function' breaks the line.
  20480. ;; `org-setup-filling' installs filling and auto-filling related
  20481. ;; variables during `org-mode' initialization.
  20482. (defvar org-element-paragraph-separate) ; org-element.el
  20483. (defun org-setup-filling ()
  20484. (require 'org-element)
  20485. ;; Prevent auto-fill from inserting unwanted new items.
  20486. (when (boundp 'fill-nobreak-predicate)
  20487. (org-set-local
  20488. 'fill-nobreak-predicate
  20489. (org-uniquify
  20490. (append fill-nobreak-predicate
  20491. '(org-fill-line-break-nobreak-p
  20492. org-fill-paragraph-with-timestamp-nobreak-p)))))
  20493. (let ((paragraph-ending (substring org-element-paragraph-separate 1)))
  20494. (org-set-local 'paragraph-start paragraph-ending)
  20495. (org-set-local 'paragraph-separate paragraph-ending))
  20496. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  20497. (org-set-local 'auto-fill-inhibit-regexp nil)
  20498. (org-set-local 'adaptive-fill-function 'org-adaptive-fill-function)
  20499. (org-set-local 'normal-auto-fill-function 'org-auto-fill-function)
  20500. (org-set-local 'comment-line-break-function 'org-comment-line-break-function))
  20501. (defun org-fill-line-break-nobreak-p ()
  20502. "Non-nil when a new line at point would create an Org line break."
  20503. (save-excursion
  20504. (skip-chars-backward "[ \t]")
  20505. (skip-chars-backward "\\\\")
  20506. (looking-at "\\\\\\\\\\($\\|[^\\\\]\\)")))
  20507. (defun org-fill-paragraph-with-timestamp-nobreak-p ()
  20508. "Non-nil when a new line at point would split a timestamp."
  20509. (and (org-at-timestamp-p t)
  20510. (not (looking-at org-ts-regexp-both))))
  20511. (declare-function message-in-body-p "message" ())
  20512. (defvar orgtbl-line-start-regexp) ; From org-table.el
  20513. (defun org-adaptive-fill-function ()
  20514. "Compute a fill prefix for the current line.
  20515. Return fill prefix, as a string, or nil if current line isn't
  20516. meant to be filled. For convenience, if `adaptive-fill-regexp'
  20517. matches in paragraphs or comments, use it."
  20518. (catch 'exit
  20519. (when (derived-mode-p 'message-mode)
  20520. (save-excursion
  20521. (beginning-of-line)
  20522. (cond ((or (not (message-in-body-p))
  20523. (looking-at orgtbl-line-start-regexp))
  20524. (throw 'exit nil))
  20525. ((looking-at message-cite-prefix-regexp)
  20526. (throw 'exit (match-string-no-properties 0)))
  20527. ((looking-at org-outline-regexp)
  20528. (throw 'exit (make-string (length (match-string 0)) ? ))))))
  20529. (org-with-wide-buffer
  20530. (let* ((p (line-beginning-position))
  20531. (element (save-excursion
  20532. (beginning-of-line)
  20533. (or (ignore-errors (org-element-at-point))
  20534. (user-error "An element cannot be parsed line %d"
  20535. (line-number-at-pos (point))))))
  20536. (type (org-element-type element))
  20537. (post-affiliated (org-element-property :post-affiliated element)))
  20538. (unless (and post-affiliated (< p post-affiliated))
  20539. (case type
  20540. (comment
  20541. (save-excursion
  20542. (beginning-of-line)
  20543. (looking-at "[ \t]*")
  20544. (concat (match-string 0) "# ")))
  20545. (footnote-definition "")
  20546. ((item plain-list)
  20547. (make-string (org-list-item-body-column
  20548. (or post-affiliated
  20549. (org-element-property :begin element)))
  20550. ? ))
  20551. (paragraph
  20552. ;; Fill prefix is usually the same as the current line,
  20553. ;; unless the paragraph is at the beginning of an item.
  20554. (let ((parent (org-element-property :parent element)))
  20555. (save-excursion
  20556. (beginning-of-line)
  20557. (cond ((eq (org-element-type parent) 'item)
  20558. (make-string (org-list-item-body-column
  20559. (org-element-property :begin parent))
  20560. ? ))
  20561. ((and adaptive-fill-regexp
  20562. ;; Locally disable
  20563. ;; `adaptive-fill-function' to let
  20564. ;; `fill-context-prefix' handle
  20565. ;; `adaptive-fill-regexp' variable.
  20566. (let (adaptive-fill-function)
  20567. (fill-context-prefix
  20568. post-affiliated
  20569. (org-element-property :end element)))))
  20570. ((looking-at "[ \t]+") (match-string 0))
  20571. (t "")))))
  20572. (comment-block
  20573. ;; Only fill contents if P is within block boundaries.
  20574. (let* ((cbeg (save-excursion (goto-char post-affiliated)
  20575. (forward-line)
  20576. (point)))
  20577. (cend (save-excursion
  20578. (goto-char (org-element-property :end element))
  20579. (skip-chars-backward " \r\t\n")
  20580. (line-beginning-position))))
  20581. (when (and (>= p cbeg) (< p cend))
  20582. (if (save-excursion (beginning-of-line) (looking-at "[ \t]+"))
  20583. (match-string 0)
  20584. ""))))))))))
  20585. (declare-function message-goto-body "message" ())
  20586. (defvar message-cite-prefix-regexp) ; From message.el
  20587. (defun org-fill-paragraph (&optional justify)
  20588. "Fill element at point, when applicable.
  20589. This function only applies to comment blocks, comments, example
  20590. blocks and paragraphs. Also, as a special case, re-align table
  20591. when point is at one.
  20592. If JUSTIFY is non-nil (interactively, with prefix argument),
  20593. justify as well. If `sentence-end-double-space' is non-nil, then
  20594. period followed by one space does not end a sentence, so don't
  20595. break a line there. The variable `fill-column' controls the
  20596. width for filling.
  20597. For convenience, when point is at a plain list, an item or
  20598. a footnote definition, try to fill the first paragraph within."
  20599. (interactive)
  20600. (if (and (derived-mode-p 'message-mode)
  20601. (or (not (message-in-body-p))
  20602. (save-excursion (move-beginning-of-line 1)
  20603. (looking-at message-cite-prefix-regexp))))
  20604. ;; First ensure filling is correct in message-mode.
  20605. (let ((fill-paragraph-function
  20606. (cadadr (assoc 'fill-paragraph-function org-fb-vars)))
  20607. (fill-prefix (cadadr (assoc 'fill-prefix org-fb-vars)))
  20608. (paragraph-start (cadadr (assoc 'paragraph-start org-fb-vars)))
  20609. (paragraph-separate
  20610. (cadadr (assoc 'paragraph-separate org-fb-vars))))
  20611. (fill-paragraph nil))
  20612. (with-syntax-table org-mode-transpose-word-syntax-table
  20613. ;; Move to end of line in order to get the first paragraph
  20614. ;; within a plain list or a footnote definition.
  20615. (let ((element (save-excursion
  20616. (end-of-line)
  20617. (or (ignore-errors (org-element-at-point))
  20618. (user-error "An element cannot be parsed line %d"
  20619. (line-number-at-pos (point)))))))
  20620. ;; First check if point is in a blank line at the beginning of
  20621. ;; the buffer. In that case, ignore filling.
  20622. (case (org-element-type element)
  20623. ;; Use major mode filling function is src blocks.
  20624. (src-block (org-babel-do-key-sequence-in-edit-buffer (kbd "M-q")))
  20625. ;; Align Org tables, leave table.el tables as-is.
  20626. (table-row (org-table-align) t)
  20627. (table
  20628. (when (eq (org-element-property :type element) 'org)
  20629. (save-excursion
  20630. (goto-char (org-element-property :post-affiliated element))
  20631. (org-table-align)))
  20632. t)
  20633. (paragraph
  20634. ;; Paragraphs may contain `line-break' type objects.
  20635. (let ((beg (max (point-min)
  20636. (org-element-property :contents-begin element)))
  20637. (end (min (point-max)
  20638. (org-element-property :contents-end element))))
  20639. ;; Do nothing if point is at an affiliated keyword.
  20640. (if (< (line-end-position) beg) t
  20641. (when (derived-mode-p 'message-mode)
  20642. ;; In `message-mode', do not fill following citation
  20643. ;; in current paragraph nor text before message body.
  20644. (let ((body-start (save-excursion (message-goto-body))))
  20645. (when body-start (setq beg (max body-start beg))))
  20646. (when (save-excursion
  20647. (re-search-forward
  20648. (concat "^" message-cite-prefix-regexp) end t))
  20649. (setq end (match-beginning 0))))
  20650. ;; Fill paragraph, taking line breaks into account.
  20651. ;; For that, slice the paragraph using line breaks as
  20652. ;; separators, and fill the parts in reverse order to
  20653. ;; avoid messing with markers.
  20654. (save-excursion
  20655. (goto-char end)
  20656. (mapc
  20657. (lambda (pos)
  20658. (fill-region-as-paragraph pos (point) justify)
  20659. (goto-char pos))
  20660. ;; Find the list of ending positions for line breaks
  20661. ;; in the current paragraph. Add paragraph
  20662. ;; beginning to include first slice.
  20663. (nreverse
  20664. (cons beg
  20665. (org-element-map
  20666. (org-element--parse-objects
  20667. beg end nil (org-element-restriction 'paragraph))
  20668. 'line-break
  20669. (lambda (lb) (org-element-property :end lb)))))))
  20670. t)))
  20671. ;; Contents of `comment-block' type elements should be
  20672. ;; filled as plain text, but only if point is within block
  20673. ;; markers.
  20674. (comment-block
  20675. (let* ((case-fold-search t)
  20676. (beg (save-excursion
  20677. (goto-char (org-element-property :begin element))
  20678. (re-search-forward "^[ \t]*#\\+begin_comment" nil t)
  20679. (forward-line)
  20680. (point)))
  20681. (end (save-excursion
  20682. (goto-char (org-element-property :end element))
  20683. (re-search-backward "^[ \t]*#\\+end_comment" nil t)
  20684. (line-beginning-position))))
  20685. (if (or (< (point) beg) (> (point) end)) t
  20686. (fill-region-as-paragraph
  20687. (save-excursion (end-of-line)
  20688. (re-search-backward "^[ \t]*$" beg 'move)
  20689. (line-beginning-position))
  20690. (save-excursion (beginning-of-line)
  20691. (re-search-forward "^[ \t]*$" end 'move)
  20692. (line-beginning-position))
  20693. justify))))
  20694. ;; Fill comments.
  20695. (comment
  20696. (let ((begin (org-element-property :post-affiliated element))
  20697. (end (org-element-property :end element)))
  20698. (when (and (>= (point) begin) (<= (point) end))
  20699. (let ((begin (save-excursion
  20700. (end-of-line)
  20701. (if (re-search-backward "^[ \t]*#[ \t]*$" begin t)
  20702. (progn (forward-line) (point))
  20703. begin)))
  20704. (end (save-excursion
  20705. (end-of-line)
  20706. (if (re-search-forward "^[ \t]*#[ \t]*$" end 'move)
  20707. (1- (line-beginning-position))
  20708. (skip-chars-backward " \r\t\n")
  20709. (line-end-position)))))
  20710. ;; Do not fill comments when at a blank line.
  20711. (when (> end begin)
  20712. (let ((fill-prefix
  20713. (save-excursion
  20714. (beginning-of-line)
  20715. (looking-at "[ \t]*#")
  20716. (let ((comment-prefix (match-string 0)))
  20717. (goto-char (match-end 0))
  20718. (if (looking-at adaptive-fill-regexp)
  20719. (concat comment-prefix (match-string 0))
  20720. (concat comment-prefix " "))))))
  20721. (save-excursion
  20722. (fill-region-as-paragraph begin end justify))))))
  20723. t))
  20724. ;; Ignore every other element.
  20725. (otherwise t))))))
  20726. (defun org-auto-fill-function ()
  20727. "Auto-fill function."
  20728. ;; Check if auto-filling is meaningful.
  20729. (let ((fc (current-fill-column)))
  20730. (when (and fc (> (current-column) fc))
  20731. (let* ((fill-prefix (org-adaptive-fill-function))
  20732. ;; Enforce empty fill prefix, if required. Otherwise, it
  20733. ;; will be computed again.
  20734. (adaptive-fill-mode (not (equal fill-prefix ""))))
  20735. (when fill-prefix (do-auto-fill))))))
  20736. (defun org-comment-line-break-function (&optional soft)
  20737. "Break line at point and indent, continuing comment if within one.
  20738. The inserted newline is marked hard if variable
  20739. `use-hard-newlines' is true, unless optional argument SOFT is
  20740. non-nil."
  20741. (if soft (insert-and-inherit ?\n) (newline 1))
  20742. (save-excursion (forward-char -1) (delete-horizontal-space))
  20743. (delete-horizontal-space)
  20744. (indent-to-left-margin)
  20745. (insert-before-markers-and-inherit fill-prefix))
  20746. ;;; Comments
  20747. ;; Org comments syntax is quite complex. It requires the entire line
  20748. ;; to be just a comment. Also, even with the right syntax at the
  20749. ;; beginning of line, some some elements (i.e. verse-block or
  20750. ;; example-block) don't accept comments. Usual Emacs comment commands
  20751. ;; cannot cope with those requirements. Therefore, Org replaces them.
  20752. ;; Org still relies on `comment-dwim', but cannot trust
  20753. ;; `comment-only-p'. So, `comment-region-function' and
  20754. ;; `uncomment-region-function' both point
  20755. ;; to`org-comment-or-uncomment-region'. Eventually,
  20756. ;; `org-insert-comment' takes care of insertion of comments at the
  20757. ;; beginning of line.
  20758. ;; `org-setup-comments-handling' install comments related variables
  20759. ;; during `org-mode' initialization.
  20760. (defun org-setup-comments-handling ()
  20761. (interactive)
  20762. (org-set-local 'comment-use-syntax nil)
  20763. (org-set-local 'comment-start "# ")
  20764. (org-set-local 'comment-start-skip "^\\s-*#\\(?: \\|$\\)")
  20765. (org-set-local 'comment-insert-comment-function 'org-insert-comment)
  20766. (org-set-local 'comment-region-function 'org-comment-or-uncomment-region)
  20767. (org-set-local 'uncomment-region-function 'org-comment-or-uncomment-region))
  20768. (defun org-insert-comment ()
  20769. "Insert an empty comment above current line.
  20770. If the line is empty, insert comment at its beginning."
  20771. (beginning-of-line)
  20772. (if (looking-at "\\s-*$") (replace-match "") (open-line 1))
  20773. (org-indent-line)
  20774. (insert "# "))
  20775. (defvar comment-empty-lines) ; From newcomment.el.
  20776. (defun org-comment-or-uncomment-region (beg end &rest ignore)
  20777. "Comment or uncomment each non-blank line in the region.
  20778. Uncomment each non-blank line between BEG and END if it only
  20779. contains commented lines. Otherwise, comment them."
  20780. (save-restriction
  20781. ;; Restrict region
  20782. (narrow-to-region (save-excursion (goto-char beg)
  20783. (skip-chars-forward " \r\t\n" end)
  20784. (line-beginning-position))
  20785. (save-excursion (goto-char end)
  20786. (skip-chars-backward " \r\t\n" beg)
  20787. (line-end-position)))
  20788. (let ((uncommentp
  20789. ;; UNCOMMENTP is non-nil when every non blank line between
  20790. ;; BEG and END is a comment.
  20791. (save-excursion
  20792. (goto-char (point-min))
  20793. (while (and (not (eobp))
  20794. (let ((element (org-element-at-point)))
  20795. (and (eq (org-element-type element) 'comment)
  20796. (goto-char (min (point-max)
  20797. (org-element-property
  20798. :end element)))))))
  20799. (eobp))))
  20800. (if uncommentp
  20801. ;; Only blank lines and comments in region: uncomment it.
  20802. (save-excursion
  20803. (goto-char (point-min))
  20804. (while (not (eobp))
  20805. (when (looking-at "[ \t]*\\(#\\(?: \\|$\\)\\)")
  20806. (replace-match "" nil nil nil 1))
  20807. (forward-line)))
  20808. ;; Comment each line in region.
  20809. (let ((min-indent (point-max)))
  20810. ;; First find the minimum indentation across all lines.
  20811. (save-excursion
  20812. (goto-char (point-min))
  20813. (while (and (not (eobp)) (not (zerop min-indent)))
  20814. (unless (looking-at "[ \t]*$")
  20815. (setq min-indent (min min-indent (current-indentation))))
  20816. (forward-line)))
  20817. ;; Then loop over all lines.
  20818. (save-excursion
  20819. (goto-char (point-min))
  20820. (while (not (eobp))
  20821. (unless (and (not comment-empty-lines) (looking-at "[ \t]*$"))
  20822. ;; Don't get fooled by invisible text (e.g. link path)
  20823. ;; when moving to column MIN-INDENT.
  20824. (let ((buffer-invisibility-spec nil))
  20825. (org-move-to-column min-indent t))
  20826. (insert comment-start))
  20827. (forward-line))))))))
  20828. ;;; Planning
  20829. ;; This section contains tools to operate on timestamp objects, as
  20830. ;; returned by, e.g. `org-element-context'.
  20831. (defun org-timestamp-has-time-p (timestamp)
  20832. "Non-nil when TIMESTAMP has a time specified."
  20833. (org-element-property :hour-start timestamp))
  20834. (defun org-timestamp-format (timestamp format &optional end utc)
  20835. "Format a TIMESTAMP element into a string.
  20836. FORMAT is a format specifier to be passed to
  20837. `format-time-string'.
  20838. When optional argument END is non-nil, use end of date-range or
  20839. time-range, if possible.
  20840. When optional argument UTC is non-nil, time will be expressed as
  20841. Universal Time."
  20842. (format-time-string
  20843. format
  20844. (apply 'encode-time
  20845. (cons 0
  20846. (mapcar
  20847. (lambda (prop) (or (org-element-property prop timestamp) 0))
  20848. (if end '(:minute-end :hour-end :day-end :month-end :year-end)
  20849. '(:minute-start :hour-start :day-start :month-start
  20850. :year-start)))))
  20851. utc))
  20852. (defun org-timestamp-split-range (timestamp &optional end)
  20853. "Extract a timestamp object from a date or time range.
  20854. TIMESTAMP is a timestamp object. END, when non-nil, means extract
  20855. the end of the range. Otherwise, extract its start.
  20856. Return a new timestamp object sharing the same parent as
  20857. TIMESTAMP."
  20858. (let ((type (org-element-property :type timestamp)))
  20859. (if (memq type '(active inactive diary)) timestamp
  20860. (let ((split-ts (list 'timestamp (copy-sequence (nth 1 timestamp)))))
  20861. ;; Set new type.
  20862. (org-element-put-property
  20863. split-ts :type (if (eq type 'active-range) 'active 'inactive))
  20864. ;; Copy start properties over end properties if END is
  20865. ;; non-nil. Otherwise, copy end properties over `start' ones.
  20866. (let ((p-alist '((:minute-start . :minute-end)
  20867. (:hour-start . :hour-end)
  20868. (:day-start . :day-end)
  20869. (:month-start . :month-end)
  20870. (:year-start . :year-end))))
  20871. (dolist (p-cell p-alist)
  20872. (org-element-put-property
  20873. split-ts
  20874. (funcall (if end 'car 'cdr) p-cell)
  20875. (org-element-property
  20876. (funcall (if end 'cdr 'car) p-cell) split-ts)))
  20877. ;; Eventually refresh `:raw-value'.
  20878. (org-element-put-property split-ts :raw-value nil)
  20879. (org-element-put-property
  20880. split-ts :raw-value (org-element-interpret-data split-ts)))))))
  20881. (defun org-timestamp-translate (timestamp &optional boundary)
  20882. "Apply `org-translate-time' on a TIMESTAMP object.
  20883. When optional argument BOUNDARY is non-nil, it is either the
  20884. symbol `start' or `end'. In this case, only translate the
  20885. starting or ending part of TIMESTAMP if it is a date or time
  20886. range. Otherwise, translate both parts."
  20887. (if (and (not boundary)
  20888. (memq (org-element-property :type timestamp)
  20889. '(active-range inactive-range)))
  20890. (concat
  20891. (org-translate-time
  20892. (org-element-property :raw-value
  20893. (org-timestamp-split-range timestamp)))
  20894. "--"
  20895. (org-translate-time
  20896. (org-element-property :raw-value
  20897. (org-timestamp-split-range timestamp t))))
  20898. (org-translate-time
  20899. (org-element-property
  20900. :raw-value
  20901. (if (not boundary) timestamp
  20902. (org-timestamp-split-range timestamp (eq boundary 'end)))))))
  20903. ;;; Other stuff.
  20904. (defun org-toggle-fixed-width-section (arg)
  20905. "Toggle the fixed-width export.
  20906. If there is no active region, the QUOTE keyword at the current headline is
  20907. inserted or removed. When present, it causes the text between this headline
  20908. and the next to be exported as fixed-width text, and unmodified.
  20909. If there is an active region, this command adds or removes a colon as the
  20910. first character of this line. If the first character of a line is a colon,
  20911. this line is also exported in fixed-width font."
  20912. (interactive "P")
  20913. (let* ((cc 0)
  20914. (regionp (org-region-active-p))
  20915. (beg (if regionp (region-beginning) (point)))
  20916. (end (if regionp (region-end)))
  20917. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  20918. (case-fold-search nil)
  20919. (re "[ \t]*\\(:\\(?: \\|$\\)\\)")
  20920. off)
  20921. (if regionp
  20922. (save-excursion
  20923. (goto-char beg)
  20924. (setq cc (current-column))
  20925. (beginning-of-line 1)
  20926. (setq off (looking-at re))
  20927. (while (> nlines 0)
  20928. (setq nlines (1- nlines))
  20929. (beginning-of-line 1)
  20930. (cond
  20931. (arg
  20932. (org-move-to-column cc t)
  20933. (insert ": \n")
  20934. (forward-line -1))
  20935. ((and off (looking-at re))
  20936. (replace-match "" t t nil 1))
  20937. ((not off) (org-move-to-column cc t) (insert ": ")))
  20938. (forward-line 1)))
  20939. (save-excursion
  20940. (org-back-to-heading)
  20941. (cond
  20942. ((looking-at (format org-heading-keyword-regexp-format
  20943. org-quote-string))
  20944. (goto-char (match-end 1))
  20945. (looking-at (concat " +" org-quote-string))
  20946. (replace-match "" t t)
  20947. (when (eolp) (insert " ")))
  20948. ((looking-at org-outline-regexp)
  20949. (goto-char (match-end 0))
  20950. (insert org-quote-string " ")))))))
  20951. (defun org-reftex-citation ()
  20952. "Use reftex-citation to insert a citation into the buffer.
  20953. This looks for a line like
  20954. #+BIBLIOGRAPHY: foo plain option:-d
  20955. and derives from it that foo.bib is the bibliography file relevant
  20956. for this document. It then installs the necessary environment for RefTeX
  20957. to work in this buffer and calls `reftex-citation' to insert a citation
  20958. into the buffer.
  20959. Export of such citations to both LaTeX and HTML is handled by the contributed
  20960. package ox-bibtex by Taru Karttunen."
  20961. (interactive)
  20962. (let ((reftex-docstruct-symbol 'rds)
  20963. (reftex-cite-format "\\cite{%l}")
  20964. rds bib)
  20965. (save-excursion
  20966. (save-restriction
  20967. (widen)
  20968. (let ((case-fold-search t)
  20969. (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
  20970. (if (not (save-excursion
  20971. (or (re-search-forward re nil t)
  20972. (re-search-backward re nil t))))
  20973. (error "No bibliography defined in file")
  20974. (setq bib (concat (match-string 1) ".bib")
  20975. rds (list (list 'bib bib)))))))
  20976. (call-interactively 'reftex-citation)))
  20977. ;;;; Functions extending outline functionality
  20978. (defun org-beginning-of-line (&optional arg)
  20979. "Go to the beginning of the current line. If that is invisible, continue
  20980. to a visible line beginning. This makes the function of C-a more intuitive.
  20981. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  20982. first attempt, and only move to after the tags when the cursor is already
  20983. beyond the end of the headline."
  20984. (interactive "P")
  20985. (let ((pos (point))
  20986. (special (if (consp org-special-ctrl-a/e)
  20987. (car org-special-ctrl-a/e)
  20988. org-special-ctrl-a/e))
  20989. deactivate-mark refpos)
  20990. (if (org-bound-and-true-p visual-line-mode)
  20991. (beginning-of-visual-line 1)
  20992. (beginning-of-line 1))
  20993. (if (and arg (fboundp 'move-beginning-of-line))
  20994. (call-interactively 'move-beginning-of-line)
  20995. (if (bobp)
  20996. nil
  20997. (backward-char 1)
  20998. (if (org-truely-invisible-p)
  20999. (while (and (not (bobp)) (org-truely-invisible-p))
  21000. (backward-char 1)
  21001. (beginning-of-line 1))
  21002. (forward-char 1))))
  21003. (when special
  21004. (cond
  21005. ((and (looking-at org-complex-heading-regexp)
  21006. (= (char-after (match-end 1)) ?\ ))
  21007. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  21008. (point-at-eol)))
  21009. (goto-char
  21010. (if (eq special t)
  21011. (cond ((> pos refpos) refpos)
  21012. ((= pos (point)) refpos)
  21013. (t (point)))
  21014. (cond ((> pos (point)) (point))
  21015. ((not (eq last-command this-command)) (point))
  21016. (t refpos)))))
  21017. ((org-at-item-p)
  21018. ;; Being at an item and not looking at an the item means point
  21019. ;; was previously moved to beginning of a visual line, which
  21020. ;; doesn't contain the item. Therefore, do nothing special,
  21021. ;; just stay here.
  21022. (when (looking-at org-list-full-item-re)
  21023. ;; Set special position at first white space character after
  21024. ;; bullet, and check-box, if any.
  21025. (let ((after-bullet
  21026. (let ((box (match-end 3)))
  21027. (if (not box) (match-end 1)
  21028. (let ((after (char-after box)))
  21029. (if (and after (= after ? )) (1+ box) box))))))
  21030. ;; Special case: Move point to special position when
  21031. ;; currently after it or at beginning of line.
  21032. (if (eq special t)
  21033. (when (or (> pos after-bullet) (= (point) pos))
  21034. (goto-char after-bullet))
  21035. ;; Reversed case: Move point to special position when
  21036. ;; point was already at beginning of line and command is
  21037. ;; repeated.
  21038. (when (and (= (point) pos) (eq last-command this-command))
  21039. (goto-char after-bullet))))))))
  21040. (org-no-warnings
  21041. (and (featurep 'xemacs) (setq zmacs-region-stays t))))
  21042. (setq disable-point-adjustment
  21043. (or (not (invisible-p (point)))
  21044. (not (invisible-p (max (point-min) (1- (point))))))))
  21045. (defun org-end-of-line (&optional arg)
  21046. "Go to the end of the line.
  21047. If this is a headline, and `org-special-ctrl-a/e' is set, ignore
  21048. tags on the first attempt, and only move to after the tags when
  21049. the cursor is already beyond the end of the headline."
  21050. (interactive "P")
  21051. (let ((special (if (consp org-special-ctrl-a/e) (cdr org-special-ctrl-a/e)
  21052. org-special-ctrl-a/e))
  21053. (move-fun (cond ((org-bound-and-true-p visual-line-mode)
  21054. 'end-of-visual-line)
  21055. ((fboundp 'move-end-of-line) 'move-end-of-line)
  21056. (t 'end-of-line)))
  21057. deactivate-mark)
  21058. (if (or (not special) arg) (call-interactively move-fun)
  21059. (let* ((element (save-excursion (beginning-of-line)
  21060. (org-element-at-point)))
  21061. (type (org-element-type element)))
  21062. (cond
  21063. ((memq type '(headline inlinetask))
  21064. (let ((pos (point)))
  21065. (beginning-of-line 1)
  21066. (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*\\)?$"))
  21067. (if (eq special t)
  21068. (if (or (< pos (match-beginning 1)) (= pos (match-end 0)))
  21069. (goto-char (match-beginning 1))
  21070. (goto-char (match-end 0)))
  21071. (if (or (< pos (match-end 0))
  21072. (not (eq this-command last-command)))
  21073. (goto-char (match-end 0))
  21074. (goto-char (match-beginning 1))))
  21075. (call-interactively move-fun))))
  21076. ((org-element-property :hiddenp element)
  21077. ;; If element is hidden, `move-end-of-line' would put point
  21078. ;; after it. Use `end-of-line' to stay on current line.
  21079. (call-interactively 'end-of-line))
  21080. (t (call-interactively move-fun)))))
  21081. (org-no-warnings (and (featurep 'xemacs) (setq zmacs-region-stays t))))
  21082. (setq disable-point-adjustment
  21083. (or (not (invisible-p (point)))
  21084. (not (invisible-p (max (point-min) (1- (point))))))))
  21085. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  21086. (define-key org-mode-map "\C-e" 'org-end-of-line)
  21087. (defun org-backward-sentence (&optional arg)
  21088. "Go to beginning of sentence, or beginning of table field.
  21089. This will call `backward-sentence' or `org-table-beginning-of-field',
  21090. depending on context."
  21091. (interactive "P")
  21092. (cond
  21093. ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
  21094. (t (call-interactively 'backward-sentence))))
  21095. (defun org-forward-sentence (&optional arg)
  21096. "Go to end of sentence, or end of table field.
  21097. This will call `forward-sentence' or `org-table-end-of-field',
  21098. depending on context."
  21099. (interactive "P")
  21100. (cond
  21101. ((org-at-table-p) (call-interactively 'org-table-end-of-field))
  21102. (t (call-interactively 'forward-sentence))))
  21103. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  21104. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  21105. (defun org-kill-line (&optional arg)
  21106. "Kill line, to tags or end of line."
  21107. (interactive "P")
  21108. (cond
  21109. ((or (not org-special-ctrl-k)
  21110. (bolp)
  21111. (not (org-at-heading-p)))
  21112. (if (and (get-char-property (min (point-max) (point-at-eol)) 'invisible)
  21113. org-ctrl-k-protect-subtree)
  21114. (if (or (eq org-ctrl-k-protect-subtree 'error)
  21115. (not (y-or-n-p "Kill hidden subtree along with headline? ")))
  21116. (user-error "C-k aborted as it would kill a hidden subtree")))
  21117. (call-interactively
  21118. (if (org-bound-and-true-p visual-line-mode) 'kill-visual-line 'kill-line)))
  21119. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$"))
  21120. (kill-region (point) (match-beginning 1))
  21121. (org-set-tags nil t))
  21122. (t (kill-region (point) (point-at-eol)))))
  21123. (define-key org-mode-map "\C-k" 'org-kill-line)
  21124. (defun org-yank (&optional arg)
  21125. "Yank. If the kill is a subtree, treat it specially.
  21126. This command will look at the current kill and check if is a single
  21127. subtree, or a series of subtrees[1]. If it passes the test, and if the
  21128. cursor is at the beginning of a line or after the stars of a currently
  21129. empty headline, then the yank is handled specially. How exactly depends
  21130. on the value of the following variables, both set by default.
  21131. org-yank-folded-subtrees
  21132. When set, the subtree(s) will be folded after insertion, but only
  21133. if doing so would now swallow text after the yanked text.
  21134. org-yank-adjusted-subtrees
  21135. When set, the subtree will be promoted or demoted in order to
  21136. fit into the local outline tree structure, which means that the level
  21137. will be adjusted so that it becomes the smaller one of the two
  21138. *visible* surrounding headings.
  21139. Any prefix to this command will cause `yank' to be called directly with
  21140. no special treatment. In particular, a simple \\[universal-argument] prefix \
  21141. will just
  21142. plainly yank the text as it is.
  21143. \[1] The test checks if the first non-white line is a heading
  21144. and if there are no other headings with fewer stars."
  21145. (interactive "P")
  21146. (org-yank-generic 'yank arg))
  21147. (defun org-yank-generic (command arg)
  21148. "Perform some yank-like command.
  21149. This function implements the behavior described in the `org-yank'
  21150. documentation. However, it has been generalized to work for any
  21151. interactive command with similar behavior."
  21152. ;; pretend to be command COMMAND
  21153. (setq this-command command)
  21154. (if arg
  21155. (call-interactively command)
  21156. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  21157. (and (org-kill-is-subtree-p)
  21158. (or (bolp)
  21159. (and (looking-at "[ \t]*$")
  21160. (string-match
  21161. "\\`\\*+\\'"
  21162. (buffer-substring (point-at-bol) (point)))))))
  21163. swallowp)
  21164. (cond
  21165. ((and subtreep org-yank-folded-subtrees)
  21166. (let ((beg (point))
  21167. end)
  21168. (if (and subtreep org-yank-adjusted-subtrees)
  21169. (org-paste-subtree nil nil 'for-yank)
  21170. (call-interactively command))
  21171. (setq end (point))
  21172. (goto-char beg)
  21173. (when (and (bolp) subtreep
  21174. (not (setq swallowp
  21175. (org-yank-folding-would-swallow-text beg end))))
  21176. (org-with-limited-levels
  21177. (or (looking-at org-outline-regexp)
  21178. (re-search-forward org-outline-regexp-bol end t))
  21179. (while (and (< (point) end) (looking-at org-outline-regexp))
  21180. (hide-subtree)
  21181. (org-cycle-show-empty-lines 'folded)
  21182. (condition-case nil
  21183. (outline-forward-same-level 1)
  21184. (error (goto-char end))))))
  21185. (when swallowp
  21186. (message
  21187. "Inserted text not folded because that would swallow text"))
  21188. (goto-char end)
  21189. (skip-chars-forward " \t\n\r")
  21190. (beginning-of-line 1)
  21191. (push-mark beg 'nomsg)))
  21192. ((and subtreep org-yank-adjusted-subtrees)
  21193. (let ((beg (point-at-bol)))
  21194. (org-paste-subtree nil nil 'for-yank)
  21195. (push-mark beg 'nomsg)))
  21196. (t
  21197. (call-interactively command))))))
  21198. (defun org-yank-folding-would-swallow-text (beg end)
  21199. "Would hide-subtree at BEG swallow any text after END?"
  21200. (let (level)
  21201. (org-with-limited-levels
  21202. (save-excursion
  21203. (goto-char beg)
  21204. (when (or (looking-at org-outline-regexp)
  21205. (re-search-forward org-outline-regexp-bol end t))
  21206. (setq level (org-outline-level)))
  21207. (goto-char end)
  21208. (skip-chars-forward " \t\r\n\v\f")
  21209. (if (or (eobp)
  21210. (and (bolp) (looking-at org-outline-regexp)
  21211. (<= (org-outline-level) level)))
  21212. nil ; Nothing would be swallowed
  21213. t))))) ; something would swallow
  21214. (define-key org-mode-map "\C-y" 'org-yank)
  21215. (defun org-truely-invisible-p ()
  21216. "Check if point is at a character currently not visible.
  21217. This version does not only check the character property, but also
  21218. `visible-mode'."
  21219. ;; Early versions of noutline don't have `outline-invisible-p'.
  21220. (if (org-bound-and-true-p visible-mode)
  21221. nil
  21222. (outline-invisible-p)))
  21223. (defun org-invisible-p2 ()
  21224. "Check if point is at a character currently not visible."
  21225. (save-excursion
  21226. (if (and (eolp) (not (bobp))) (backward-char 1))
  21227. ;; Early versions of noutline don't have `outline-invisible-p'.
  21228. (outline-invisible-p)))
  21229. (defun org-back-to-heading (&optional invisible-ok)
  21230. "Call `outline-back-to-heading', but provide a better error message."
  21231. (condition-case nil
  21232. (outline-back-to-heading invisible-ok)
  21233. (error (error "Before first headline at position %d in buffer %s"
  21234. (point) (current-buffer)))))
  21235. (defun org-before-first-heading-p ()
  21236. "Before first heading?"
  21237. (save-excursion
  21238. (end-of-line)
  21239. (null (re-search-backward org-outline-regexp-bol nil t))))
  21240. (defun org-at-heading-p (&optional ignored)
  21241. (outline-on-heading-p t))
  21242. ;; Compatibility alias with Org versions < 7.8.03
  21243. (defalias 'org-on-heading-p 'org-at-heading-p)
  21244. (defun org-at-comment-p nil
  21245. "Is cursor in a line starting with a # character?"
  21246. (save-excursion
  21247. (beginning-of-line)
  21248. (looking-at "^#")))
  21249. (defun org-at-drawer-p nil
  21250. "Is cursor at a drawer keyword?"
  21251. (save-excursion
  21252. (move-beginning-of-line 1)
  21253. (looking-at org-drawer-regexp)))
  21254. (defun org-at-block-p nil
  21255. "Is cursor at a block keyword?"
  21256. (save-excursion
  21257. (move-beginning-of-line 1)
  21258. (looking-at org-block-regexp)))
  21259. (defun org-point-at-end-of-empty-headline ()
  21260. "If point is at the end of an empty headline, return t, else nil.
  21261. If the heading only contains a TODO keyword, it is still still considered
  21262. empty."
  21263. (and (looking-at "[ \t]*$")
  21264. (when org-todo-line-regexp
  21265. (save-excursion
  21266. (beginning-of-line 1)
  21267. (let ((case-fold-search nil))
  21268. (looking-at org-todo-line-regexp)
  21269. (string= (match-string 3) ""))))))
  21270. (defun org-at-heading-or-item-p ()
  21271. (or (org-at-heading-p) (org-at-item-p)))
  21272. (defun org-at-target-p ()
  21273. (or (org-in-regexp org-radio-target-regexp)
  21274. (org-in-regexp org-target-regexp)))
  21275. ;; Compatibility alias with Org versions < 7.8.03
  21276. (defalias 'org-on-target-p 'org-at-target-p)
  21277. (defun org-up-heading-all (arg)
  21278. "Move to the heading line of which the present line is a subheading.
  21279. This function considers both visible and invisible heading lines.
  21280. With argument, move up ARG levels."
  21281. (if (fboundp 'outline-up-heading-all)
  21282. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  21283. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  21284. (defun org-up-heading-safe ()
  21285. "Move to the heading line of which the present line is a subheading.
  21286. This version will not throw an error. It will return the level of the
  21287. headline found, or nil if no higher level is found.
  21288. Also, this function will be a lot faster than `outline-up-heading',
  21289. because it relies on stars being the outline starters. This can really
  21290. make a significant difference in outlines with very many siblings."
  21291. (let (start-level re)
  21292. (org-back-to-heading t)
  21293. (setq start-level (funcall outline-level))
  21294. (if (equal start-level 1)
  21295. nil
  21296. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  21297. (if (re-search-backward re nil t)
  21298. (funcall outline-level)))))
  21299. (defun org-first-sibling-p ()
  21300. "Is this heading the first child of its parents?"
  21301. (interactive)
  21302. (let ((re org-outline-regexp-bol)
  21303. level l)
  21304. (unless (org-at-heading-p t)
  21305. (user-error "Not at a heading"))
  21306. (setq level (funcall outline-level))
  21307. (save-excursion
  21308. (if (not (re-search-backward re nil t))
  21309. t
  21310. (setq l (funcall outline-level))
  21311. (< l level)))))
  21312. (defun org-goto-sibling (&optional previous)
  21313. "Goto the next sibling, even if it is invisible.
  21314. When PREVIOUS is set, go to the previous sibling instead. Returns t
  21315. when a sibling was found. When none is found, return nil and don't
  21316. move point."
  21317. (let ((fun (if previous 're-search-backward 're-search-forward))
  21318. (pos (point))
  21319. (re org-outline-regexp-bol)
  21320. level l)
  21321. (when (condition-case nil (org-back-to-heading t) (error nil))
  21322. (setq level (funcall outline-level))
  21323. (catch 'exit
  21324. (or previous (forward-char 1))
  21325. (while (funcall fun re nil t)
  21326. (setq l (funcall outline-level))
  21327. (when (< l level) (goto-char pos) (throw 'exit nil))
  21328. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  21329. (goto-char pos)
  21330. nil))))
  21331. (defun org-show-siblings ()
  21332. "Show all siblings of the current headline."
  21333. (save-excursion
  21334. (while (org-goto-sibling) (org-flag-heading nil)))
  21335. (save-excursion
  21336. (while (org-goto-sibling 'previous)
  21337. (org-flag-heading nil))))
  21338. (defun org-goto-first-child ()
  21339. "Goto the first child, even if it is invisible.
  21340. Return t when a child was found. Otherwise don't move point and
  21341. return nil."
  21342. (let (level (pos (point)) (re org-outline-regexp-bol))
  21343. (when (condition-case nil (org-back-to-heading t) (error nil))
  21344. (setq level (outline-level))
  21345. (forward-char 1)
  21346. (if (and (re-search-forward re nil t) (> (outline-level) level))
  21347. (progn (goto-char (match-beginning 0)) t)
  21348. (goto-char pos) nil))))
  21349. (defun org-show-hidden-entry ()
  21350. "Show an entry where even the heading is hidden."
  21351. (save-excursion
  21352. (org-show-entry)))
  21353. (defun org-flag-heading (flag &optional entry)
  21354. "Flag the current heading. FLAG non-nil means make invisible.
  21355. When ENTRY is non-nil, show the entire entry."
  21356. (save-excursion
  21357. (org-back-to-heading t)
  21358. ;; Check if we should show the entire entry
  21359. (if entry
  21360. (progn
  21361. (org-show-entry)
  21362. (save-excursion
  21363. (and (outline-next-heading)
  21364. (org-flag-heading nil))))
  21365. (outline-flag-region (max (point-min) (1- (point)))
  21366. (save-excursion (outline-end-of-heading) (point))
  21367. flag))))
  21368. (defun org-get-next-sibling ()
  21369. "Move to next heading of the same level, and return point.
  21370. If there is no such heading, return nil.
  21371. This is like outline-next-sibling, but invisible headings are ok."
  21372. (let ((level (funcall outline-level)))
  21373. (outline-next-heading)
  21374. (while (and (not (eobp)) (> (funcall outline-level) level))
  21375. (outline-next-heading))
  21376. (if (or (eobp) (< (funcall outline-level) level))
  21377. nil
  21378. (point))))
  21379. (defun org-get-last-sibling ()
  21380. "Move to previous heading of the same level, and return point.
  21381. If there is no such heading, return nil."
  21382. (let ((opoint (point))
  21383. (level (funcall outline-level)))
  21384. (outline-previous-heading)
  21385. (when (and (/= (point) opoint) (outline-on-heading-p t))
  21386. (while (and (> (funcall outline-level) level)
  21387. (not (bobp)))
  21388. (outline-previous-heading))
  21389. (if (< (funcall outline-level) level)
  21390. nil
  21391. (point)))))
  21392. (defun org-end-of-subtree (&optional invisible-ok to-heading)
  21393. "Goto to the end of a subtree."
  21394. ;; This contains an exact copy of the original function, but it uses
  21395. ;; `org-back-to-heading', to make it work also in invisible
  21396. ;; trees. And is uses an invisible-ok argument.
  21397. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  21398. ;; Furthermore, when used inside Org, finding the end of a large subtree
  21399. ;; with many children and grandchildren etc, this can be much faster
  21400. ;; than the outline version.
  21401. (org-back-to-heading invisible-ok)
  21402. (let ((first t)
  21403. (level (funcall outline-level)))
  21404. (if (and (derived-mode-p 'org-mode) (< level 1000))
  21405. ;; A true heading (not a plain list item), in Org-mode
  21406. ;; This means we can easily find the end by looking
  21407. ;; only for the right number of stars. Using a regexp to do
  21408. ;; this is so much faster than using a Lisp loop.
  21409. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  21410. (forward-char 1)
  21411. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  21412. ;; something else, do it the slow way
  21413. (while (and (not (eobp))
  21414. (or first (> (funcall outline-level) level)))
  21415. (setq first nil)
  21416. (outline-next-heading)))
  21417. (unless to-heading
  21418. (if (memq (preceding-char) '(?\n ?\^M))
  21419. (progn
  21420. ;; Go to end of line before heading
  21421. (forward-char -1)
  21422. (if (memq (preceding-char) '(?\n ?\^M))
  21423. ;; leave blank line before heading
  21424. (forward-char -1))))))
  21425. (point))
  21426. (defun org-end-of-meta-data-and-drawers ()
  21427. "Jump to the first text after meta data and drawers in the current entry.
  21428. This will move over empty lines, lines with planning time stamps,
  21429. clocking lines, and drawers."
  21430. (org-back-to-heading t)
  21431. (let ((end (save-excursion (outline-next-heading) (point)))
  21432. (re (concat "\\(" org-drawer-regexp "\\)"
  21433. "\\|" "[ \t]*" org-keyword-time-regexp)))
  21434. (forward-line 1)
  21435. (while (re-search-forward re end t)
  21436. (if (not (match-end 1))
  21437. ;; empty or planning line
  21438. (forward-line 1)
  21439. ;; a drawer, find the end
  21440. (re-search-forward "^[ \t]*:END:" end 'move)
  21441. (forward-line 1)))
  21442. (and (re-search-forward "[^\n]" nil t) (backward-char 1))
  21443. (point)))
  21444. (defun org-forward-heading-same-level (arg &optional invisible-ok)
  21445. "Move forward to the ARG'th subheading at same level as this one.
  21446. Stop at the first and last subheadings of a superior heading.
  21447. Normally this only looks at visible headings, but when INVISIBLE-OK is
  21448. non-nil it will also look at invisible ones."
  21449. (interactive "p")
  21450. (if (not (ignore-errors (org-back-to-heading invisible-ok)))
  21451. (if (and arg (< arg 0))
  21452. (goto-char (point-min))
  21453. (outline-next-heading))
  21454. (org-at-heading-p)
  21455. (let ((level (- (match-end 0) (match-beginning 0) 1))
  21456. (f (if (and arg (< arg 0))
  21457. 're-search-backward
  21458. 're-search-forward))
  21459. (count (if arg (abs arg) 1))
  21460. (result (point)))
  21461. (while (and (prog1 (> count 0)
  21462. (forward-char (if (and arg (< arg 0)) -1 1)))
  21463. (funcall f org-outline-regexp-bol nil 'move))
  21464. (let ((l (- (match-end 0) (match-beginning 0) 1)))
  21465. (cond ((< l level) (setq count 0))
  21466. ((and (= l level)
  21467. (or invisible-ok
  21468. (progn
  21469. (goto-char (line-beginning-position))
  21470. (not (outline-invisible-p)))))
  21471. (setq count (1- count))
  21472. (when (eq l level)
  21473. (setq result (point)))))))
  21474. (goto-char result))
  21475. (beginning-of-line 1)))
  21476. (defun org-backward-heading-same-level (arg &optional invisible-ok)
  21477. "Move backward to the ARG'th subheading at same level as this one.
  21478. Stop at the first and last subheadings of a superior heading."
  21479. (interactive "p")
  21480. (org-forward-heading-same-level (if arg (- arg) -1) invisible-ok))
  21481. (defun org-next-block (arg &optional backward block-regexp)
  21482. "Jump to the next block.
  21483. With a prefix argument ARG, jump forward ARG many source blocks.
  21484. When BACKWARD is non-nil, jump to the previous block.
  21485. When BLOCK-REGEXP is non-nil, use this regexp to find blocks."
  21486. (interactive "p")
  21487. (let ((re (or block-regexp org-block-regexp))
  21488. (re-search-fn (or (and backward 're-search-backward)
  21489. 're-search-forward)))
  21490. (if (looking-at re) (forward-char 1))
  21491. (condition-case nil
  21492. (funcall re-search-fn re nil nil arg)
  21493. (error (error "No %s code blocks" (if backward "previous" "further" ))))
  21494. (goto-char (match-beginning 0)) (org-show-context)))
  21495. (defun org-previous-block (arg &optional block-regexp)
  21496. "Jump to the previous block.
  21497. With a prefix argument ARG, jump backward ARG many source blocks.
  21498. When BLOCK-REGEXP is non-nil, use this regexp to find blocks."
  21499. (interactive "p")
  21500. (org-next-block arg t block-regexp))
  21501. (defun org-forward-paragraph ()
  21502. "Move forward to beginning of next paragraph or equivalent.
  21503. The function moves point to the beginning of the next visible
  21504. structural element, which can be a paragraph, a table, a list
  21505. item, etc. It also provides some special moves for convenience:
  21506. - On an affiliated keyword, jump to the beginning of the
  21507. relative element.
  21508. - On an item or a footnote definition, move to the second
  21509. element inside, if any.
  21510. - On a table or a property drawer, jump after it.
  21511. - On a verse or source block, stop after blank lines."
  21512. (interactive)
  21513. (when (eobp) (user-error "Cannot move further down"))
  21514. (let* ((deactivate-mark nil)
  21515. (element (org-element-at-point))
  21516. (type (org-element-type element))
  21517. (post-affiliated (org-element-property :post-affiliated element))
  21518. (contents-begin (org-element-property :contents-begin element))
  21519. (contents-end (org-element-property :contents-end element))
  21520. (end (let ((end (org-element-property :end element)) (parent element))
  21521. (while (and (setq parent (org-element-property :parent parent))
  21522. (= (org-element-property :contents-end parent) end))
  21523. (setq end (org-element-property :end parent)))
  21524. end)))
  21525. (cond ((not element)
  21526. (skip-chars-forward " \r\t\n")
  21527. (or (eobp) (beginning-of-line)))
  21528. ;; On affiliated keywords, move to element's beginning.
  21529. ((and post-affiliated (< (point) post-affiliated))
  21530. (goto-char post-affiliated))
  21531. ;; At a table row, move to the end of the table. Similarly,
  21532. ;; at a node property, move to the end of the property
  21533. ;; drawer.
  21534. ((memq type '(node-property table-row))
  21535. (goto-char (org-element-property
  21536. :end (org-element-property :parent element))))
  21537. ((memq type '(property-drawer table)) (goto-char end))
  21538. ;; Consider blank lines as separators in verse and source
  21539. ;; blocks to ease editing.
  21540. ((memq type '(src-block verse-block))
  21541. (when (eq type 'src-block)
  21542. (setq contents-end
  21543. (save-excursion (goto-char end)
  21544. (skip-chars-backward " \r\t\n")
  21545. (line-beginning-position))))
  21546. (beginning-of-line)
  21547. (when (looking-at "[ \t]*$") (skip-chars-forward " \r\t\n"))
  21548. (if (not (re-search-forward "^[ \t]*$" contents-end t))
  21549. (goto-char end)
  21550. (skip-chars-forward " \r\t\n")
  21551. (if (= (point) contents-end) (goto-char end)
  21552. (beginning-of-line))))
  21553. ;; With no contents, just skip element.
  21554. ((not contents-begin) (goto-char end))
  21555. ;; If contents are invisible, skip the element altogether.
  21556. ((outline-invisible-p (line-end-position))
  21557. (case type
  21558. (headline
  21559. (org-with-limited-levels (outline-next-visible-heading 1)))
  21560. ;; At a plain list, make sure we move to the next item
  21561. ;; instead of skipping the whole list.
  21562. (plain-list (forward-char)
  21563. (org-forward-paragraph))
  21564. (otherwise (goto-char end))))
  21565. ((>= (point) contents-end) (goto-char end))
  21566. ((>= (point) contents-begin)
  21567. ;; This can only happen on paragraphs and plain lists.
  21568. (case type
  21569. (paragraph (goto-char end))
  21570. ;; At a plain list, try to move to second element in
  21571. ;; first item, if possible.
  21572. (plain-list (end-of-line)
  21573. (org-forward-paragraph))))
  21574. ;; When contents start on the middle of a line (e.g. in
  21575. ;; items and footnote definitions), try to reach first
  21576. ;; element starting after current line.
  21577. ((> (line-end-position) contents-begin)
  21578. (end-of-line)
  21579. (org-forward-paragraph))
  21580. (t (goto-char contents-begin)))))
  21581. (defun org-backward-paragraph ()
  21582. "Move backward to start of previous paragraph or equivalent.
  21583. The function moves point to the beginning of the current
  21584. structural element, which can be a paragraph, a table, a list
  21585. item, etc., or to the beginning of the previous visible one if
  21586. point is already there. It also provides some special moves for
  21587. convenience:
  21588. - On an affiliated keyword, jump to the first one.
  21589. - On a table or a property drawer, move to its beginning.
  21590. - On a verse or source block, stop before blank lines."
  21591. (interactive)
  21592. (when (bobp) (user-error "Cannot move further up"))
  21593. (let* ((deactivate-mark nil)
  21594. (element (org-element-at-point))
  21595. (type (org-element-type element))
  21596. (contents-begin (org-element-property :contents-begin element))
  21597. (contents-end (org-element-property :contents-end element))
  21598. (post-affiliated (org-element-property :post-affiliated element))
  21599. (begin (org-element-property :begin element)))
  21600. (cond
  21601. ((not element) (goto-char (point-min)))
  21602. ((= (point) begin)
  21603. (backward-char)
  21604. (org-backward-paragraph))
  21605. ((and post-affiliated (<= (point) post-affiliated)) (goto-char begin))
  21606. ((memq type '(node-property table-row))
  21607. (goto-char (org-element-property
  21608. :post-affiliated (org-element-property :parent element))))
  21609. ((memq type '(property-drawer table)) (goto-char begin))
  21610. ((memq type '(src-block verse-block))
  21611. (when (eq type 'src-block)
  21612. (setq contents-begin
  21613. (save-excursion (goto-char begin) (forward-line) (point))))
  21614. (if (= (point) contents-begin) (goto-char post-affiliated)
  21615. ;; Inside a verse block, see blank lines as paragraph
  21616. ;; separators.
  21617. (let ((origin (point)))
  21618. (skip-chars-backward " \r\t\n" contents-begin)
  21619. (when (re-search-backward "^[ \t]*$" contents-begin 'move)
  21620. (skip-chars-forward " \r\t\n" origin)
  21621. (if (= (point) origin) (goto-char contents-begin)
  21622. (beginning-of-line))))))
  21623. ((not contents-begin) (goto-char (or post-affiliated begin)))
  21624. ((eq type 'paragraph)
  21625. (goto-char contents-begin)
  21626. ;; When at first paragraph in an item or a footnote definition,
  21627. ;; move directly to beginning of line.
  21628. (let ((parent-contents
  21629. (org-element-property
  21630. :contents-begin (org-element-property :parent element))))
  21631. (when (and parent-contents (= parent-contents contents-begin))
  21632. (beginning-of-line))))
  21633. ;; At the end of a greater element, move to the beginning of the
  21634. ;; last element within.
  21635. ((>= (point) contents-end)
  21636. (goto-char (1- contents-end))
  21637. (org-backward-paragraph))
  21638. (t (goto-char (or post-affiliated begin))))
  21639. ;; Ensure we never leave point invisible.
  21640. (when (outline-invisible-p (point)) (beginning-of-visual-line))))
  21641. (defun org-forward-element ()
  21642. "Move forward by one element.
  21643. Move to the next element at the same level, when possible."
  21644. (interactive)
  21645. (cond ((eobp) (user-error "Cannot move further down"))
  21646. ((org-with-limited-levels (org-at-heading-p))
  21647. (let ((origin (point)))
  21648. (goto-char (org-end-of-subtree nil t))
  21649. (unless (org-with-limited-levels (org-at-heading-p))
  21650. (goto-char origin)
  21651. (user-error "Cannot move further down"))))
  21652. (t
  21653. (let* ((elem (org-element-at-point))
  21654. (end (org-element-property :end elem))
  21655. (parent (org-element-property :parent elem)))
  21656. (cond ((and parent (= (org-element-property :contents-end parent) end))
  21657. (goto-char (org-element-property :end parent)))
  21658. ((integer-or-marker-p end) (goto-char end))
  21659. (t (message "No element at point")))))))
  21660. (defun org-backward-element ()
  21661. "Move backward by one element.
  21662. Move to the previous element at the same level, when possible."
  21663. (interactive)
  21664. (cond ((bobp) (user-error "Cannot move further up"))
  21665. ((org-with-limited-levels (org-at-heading-p))
  21666. ;; At a headline, move to the previous one, if any, or stay
  21667. ;; here.
  21668. (let ((origin (point)))
  21669. (org-with-limited-levels (org-backward-heading-same-level 1))
  21670. ;; When current headline has no sibling above, move to its
  21671. ;; parent.
  21672. (when (= (point) origin)
  21673. (or (org-with-limited-levels (org-up-heading-safe))
  21674. (progn (goto-char origin)
  21675. (user-error "Cannot move further up"))))))
  21676. (t
  21677. (let* ((trail (org-element-at-point 'keep-trail))
  21678. (elem (car trail))
  21679. (prev-elem (nth 1 trail))
  21680. (beg (org-element-property :begin elem)))
  21681. (cond
  21682. ;; Move to beginning of current element if point isn't
  21683. ;; there already.
  21684. ((null beg) (message "No element at point"))
  21685. ((/= (point) beg) (goto-char beg))
  21686. (prev-elem (goto-char (org-element-property :begin prev-elem)))
  21687. ((org-before-first-heading-p) (goto-char (point-min)))
  21688. (t (org-back-to-heading)))))))
  21689. (defun org-up-element ()
  21690. "Move to upper element."
  21691. (interactive)
  21692. (if (org-with-limited-levels (org-at-heading-p))
  21693. (unless (org-up-heading-safe) (user-error "No surrounding element"))
  21694. (let* ((elem (org-element-at-point))
  21695. (parent (org-element-property :parent elem)))
  21696. (if parent (goto-char (org-element-property :begin parent))
  21697. (if (org-with-limited-levels (org-before-first-heading-p))
  21698. (user-error "No surrounding element")
  21699. (org-with-limited-levels (org-back-to-heading)))))))
  21700. (defvar org-element-greater-elements)
  21701. (defun org-down-element ()
  21702. "Move to inner element."
  21703. (interactive)
  21704. (let ((element (org-element-at-point)))
  21705. (cond
  21706. ((memq (org-element-type element) '(plain-list table))
  21707. (goto-char (org-element-property :contents-begin element))
  21708. (forward-char))
  21709. ((memq (org-element-type element) org-element-greater-elements)
  21710. ;; If contents are hidden, first disclose them.
  21711. (when (org-element-property :hiddenp element) (org-cycle))
  21712. (goto-char (or (org-element-property :contents-begin element)
  21713. (user-error "No content for this element"))))
  21714. (t (user-error "No inner element")))))
  21715. (defun org-drag-element-backward ()
  21716. "Move backward element at point."
  21717. (interactive)
  21718. (if (org-with-limited-levels (org-at-heading-p)) (org-move-subtree-up)
  21719. (let* ((trail (org-element-at-point 'keep-trail))
  21720. (elem (car trail))
  21721. (prev-elem (nth 1 trail)))
  21722. ;; Error out if no previous element or previous element is
  21723. ;; a parent of the current one.
  21724. (if (or (not prev-elem) (org-element-nested-p elem prev-elem))
  21725. (user-error "Cannot drag element backward")
  21726. (let ((pos (point)))
  21727. (org-element-swap-A-B prev-elem elem)
  21728. (goto-char (+ (org-element-property :begin prev-elem)
  21729. (- pos (org-element-property :begin elem)))))))))
  21730. (defun org-drag-element-forward ()
  21731. "Move forward element at point."
  21732. (interactive)
  21733. (let* ((pos (point))
  21734. (elem (org-element-at-point)))
  21735. (when (= (point-max) (org-element-property :end elem))
  21736. (user-error "Cannot drag element forward"))
  21737. (goto-char (org-element-property :end elem))
  21738. (let ((next-elem (org-element-at-point)))
  21739. (when (or (org-element-nested-p elem next-elem)
  21740. (and (eq (org-element-type next-elem) 'headline)
  21741. (not (eq (org-element-type elem) 'headline))))
  21742. (goto-char pos)
  21743. (user-error "Cannot drag element forward"))
  21744. ;; Compute new position of point: it's shifted by NEXT-ELEM
  21745. ;; body's length (without final blanks) and by the length of
  21746. ;; blanks between ELEM and NEXT-ELEM.
  21747. (let ((size-next (- (save-excursion
  21748. (goto-char (org-element-property :end next-elem))
  21749. (skip-chars-backward " \r\t\n")
  21750. (forward-line)
  21751. ;; Small correction if buffer doesn't end
  21752. ;; with a newline character.
  21753. (if (and (eolp) (not (bolp))) (1+ (point)) (point)))
  21754. (org-element-property :begin next-elem)))
  21755. (size-blank (- (org-element-property :end elem)
  21756. (save-excursion
  21757. (goto-char (org-element-property :end elem))
  21758. (skip-chars-backward " \r\t\n")
  21759. (forward-line)
  21760. (point)))))
  21761. (org-element-swap-A-B elem next-elem)
  21762. (goto-char (+ pos size-next size-blank))))))
  21763. (defun org-drag-line-forward (arg)
  21764. "Drag the line at point ARG lines forward."
  21765. (interactive "p")
  21766. (dotimes (n (abs arg))
  21767. (let ((c (current-column)))
  21768. (if (< 0 arg)
  21769. (progn
  21770. (beginning-of-line 2)
  21771. (transpose-lines 1)
  21772. (beginning-of-line 0))
  21773. (transpose-lines 1)
  21774. (beginning-of-line -1))
  21775. (org-move-to-column c))))
  21776. (defun org-drag-line-backward (arg)
  21777. "Drag the line at point ARG lines backward."
  21778. (interactive "p")
  21779. (org-drag-line-forward (- arg)))
  21780. (defun org-mark-element ()
  21781. "Put point at beginning of this element, mark at end.
  21782. Interactively, if this command is repeated or (in Transient Mark
  21783. mode) if the mark is active, it marks the next element after the
  21784. ones already marked."
  21785. (interactive)
  21786. (let (deactivate-mark)
  21787. (if (and (org-called-interactively-p 'any)
  21788. (or (and (eq last-command this-command) (mark t))
  21789. (and transient-mark-mode mark-active)))
  21790. (set-mark
  21791. (save-excursion
  21792. (goto-char (mark))
  21793. (goto-char (org-element-property :end (org-element-at-point)))))
  21794. (let ((element (org-element-at-point)))
  21795. (end-of-line)
  21796. (push-mark (org-element-property :end element) t t)
  21797. (goto-char (org-element-property :begin element))))))
  21798. (defun org-narrow-to-element ()
  21799. "Narrow buffer to current element."
  21800. (interactive)
  21801. (let ((elem (org-element-at-point)))
  21802. (cond
  21803. ((eq (car elem) 'headline)
  21804. (narrow-to-region
  21805. (org-element-property :begin elem)
  21806. (org-element-property :end elem)))
  21807. ((memq (car elem) org-element-greater-elements)
  21808. (narrow-to-region
  21809. (org-element-property :contents-begin elem)
  21810. (org-element-property :contents-end elem)))
  21811. (t
  21812. (narrow-to-region
  21813. (org-element-property :begin elem)
  21814. (org-element-property :end elem))))))
  21815. (defun org-transpose-element ()
  21816. "Transpose current and previous elements, keeping blank lines between.
  21817. Point is moved after both elements."
  21818. (interactive)
  21819. (org-skip-whitespace)
  21820. (let ((end (org-element-property :end (org-element-at-point))))
  21821. (org-drag-element-backward)
  21822. (goto-char end)))
  21823. (defun org-unindent-buffer ()
  21824. "Un-indent the visible part of the buffer.
  21825. Relative indentation (between items, inside blocks, etc.) isn't
  21826. modified."
  21827. (interactive)
  21828. (unless (eq major-mode 'org-mode)
  21829. (user-error "Cannot un-indent a buffer not in Org mode"))
  21830. (let* ((parse-tree (org-element-parse-buffer 'greater-element))
  21831. unindent-tree ; For byte-compiler.
  21832. (unindent-tree
  21833. (function
  21834. (lambda (contents)
  21835. (mapc
  21836. (lambda (element)
  21837. (if (memq (org-element-type element) '(headline section))
  21838. (funcall unindent-tree (org-element-contents element))
  21839. (save-excursion
  21840. (save-restriction
  21841. (narrow-to-region
  21842. (org-element-property :begin element)
  21843. (org-element-property :end element))
  21844. (org-do-remove-indentation)))))
  21845. (reverse contents))))))
  21846. (funcall unindent-tree (org-element-contents parse-tree))))
  21847. (defun org-show-subtree ()
  21848. "Show everything after this heading at deeper levels."
  21849. (interactive)
  21850. (outline-flag-region
  21851. (point)
  21852. (save-excursion
  21853. (org-end-of-subtree t t))
  21854. nil))
  21855. (defun org-show-entry ()
  21856. "Show the body directly following this heading.
  21857. Show the heading too, if it is currently invisible."
  21858. (interactive)
  21859. (save-excursion
  21860. (condition-case nil
  21861. (progn
  21862. (org-back-to-heading t)
  21863. (outline-flag-region
  21864. (max (point-min) (1- (point)))
  21865. (save-excursion
  21866. (if (re-search-forward
  21867. (concat "[\r\n]\\(" org-outline-regexp "\\)") nil t)
  21868. (match-beginning 1)
  21869. (point-max)))
  21870. nil)
  21871. (org-cycle-hide-drawers 'children))
  21872. (error nil))))
  21873. (defun org-make-options-regexp (kwds &optional extra)
  21874. "Make a regular expression for keyword lines."
  21875. (concat
  21876. "^#\\+\\("
  21877. (mapconcat 'regexp-quote kwds "\\|")
  21878. (if extra (concat "\\|" extra))
  21879. "\\):[ \t]*\\(.*\\)"))
  21880. ;; Make isearch reveal the necessary context
  21881. (defun org-isearch-end ()
  21882. "Reveal context after isearch exits."
  21883. (when isearch-success ; only if search was successful
  21884. (if (featurep 'xemacs)
  21885. ;; Under XEmacs, the hook is run in the correct place,
  21886. ;; we directly show the context.
  21887. (org-show-context 'isearch)
  21888. ;; In Emacs the hook runs *before* restoring the overlays.
  21889. ;; So we have to use a one-time post-command-hook to do this.
  21890. ;; (Emacs 22 has a special variable, see function `org-mode')
  21891. (unless (and (boundp 'isearch-mode-end-hook-quit)
  21892. isearch-mode-end-hook-quit)
  21893. ;; Only when the isearch was not quitted.
  21894. (org-add-hook 'post-command-hook 'org-isearch-post-command
  21895. 'append 'local)))))
  21896. (defun org-isearch-post-command ()
  21897. "Remove self from hook, and show context."
  21898. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  21899. (org-show-context 'isearch))
  21900. ;;;; Integration with and fixes for other packages
  21901. ;;; Imenu support
  21902. (defvar org-imenu-markers nil
  21903. "All markers currently used by Imenu.")
  21904. (make-variable-buffer-local 'org-imenu-markers)
  21905. (defun org-imenu-new-marker (&optional pos)
  21906. "Return a new marker for use by Imenu, and remember the marker."
  21907. (let ((m (make-marker)))
  21908. (move-marker m (or pos (point)))
  21909. (push m org-imenu-markers)
  21910. m))
  21911. (defun org-imenu-get-tree ()
  21912. "Produce the index for Imenu."
  21913. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  21914. (setq org-imenu-markers nil)
  21915. (let* ((n org-imenu-depth)
  21916. (re (concat "^" (org-get-limited-outline-regexp)))
  21917. (subs (make-vector (1+ n) nil))
  21918. (last-level 0)
  21919. m level head0 head)
  21920. (save-excursion
  21921. (save-restriction
  21922. (widen)
  21923. (goto-char (point-max))
  21924. (while (re-search-backward re nil t)
  21925. (setq level (org-reduced-level (funcall outline-level)))
  21926. (when (and (<= level n)
  21927. (looking-at org-complex-heading-regexp)
  21928. (setq head0 (org-match-string-no-properties 4)))
  21929. (setq head (org-link-display-format head0)
  21930. m (org-imenu-new-marker))
  21931. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  21932. (if (>= level last-level)
  21933. (push (cons head m) (aref subs level))
  21934. (push (cons head (aref subs (1+ level))) (aref subs level))
  21935. (loop for i from (1+ level) to n do (aset subs i nil)))
  21936. (setq last-level level)))))
  21937. (aref subs 1)))
  21938. (eval-after-load "imenu"
  21939. '(progn
  21940. (add-hook 'imenu-after-jump-hook
  21941. (lambda ()
  21942. (if (derived-mode-p 'org-mode)
  21943. (org-show-context 'org-goto))))))
  21944. (defun org-link-display-format (link)
  21945. "Replace a link with its the description.
  21946. If there is no description, use the link target."
  21947. (save-match-data
  21948. (if (string-match org-bracket-link-analytic-regexp link)
  21949. (replace-match (if (match-end 5)
  21950. (match-string 5 link)
  21951. (concat (match-string 1 link)
  21952. (match-string 3 link)))
  21953. nil t link)
  21954. link)))
  21955. (defun org-toggle-link-display ()
  21956. "Toggle the literal or descriptive display of links."
  21957. (interactive)
  21958. (if org-descriptive-links
  21959. (progn (org-remove-from-invisibility-spec '(org-link))
  21960. (org-restart-font-lock)
  21961. (setq org-descriptive-links nil))
  21962. (progn (add-to-invisibility-spec '(org-link))
  21963. (org-restart-font-lock)
  21964. (setq org-descriptive-links t))))
  21965. ;; Speedbar support
  21966. (defvar org-speedbar-restriction-lock-overlay (make-overlay 1 1)
  21967. "Overlay marking the agenda restriction line in speedbar.")
  21968. (overlay-put org-speedbar-restriction-lock-overlay
  21969. 'face 'org-agenda-restriction-lock)
  21970. (overlay-put org-speedbar-restriction-lock-overlay
  21971. 'help-echo "Agendas are currently limited to this item.")
  21972. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  21973. (defun org-speedbar-set-agenda-restriction ()
  21974. "Restrict future agenda commands to the location at point in speedbar.
  21975. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  21976. (interactive)
  21977. (require 'org-agenda)
  21978. (let (p m tp np dir txt)
  21979. (cond
  21980. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  21981. 'org-imenu t))
  21982. (setq m (get-text-property p 'org-imenu-marker))
  21983. (with-current-buffer (marker-buffer m)
  21984. (goto-char m)
  21985. (org-agenda-set-restriction-lock 'subtree)))
  21986. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  21987. 'speedbar-function 'speedbar-find-file))
  21988. (setq tp (previous-single-property-change
  21989. (1+ p) 'speedbar-function)
  21990. np (next-single-property-change
  21991. tp 'speedbar-function)
  21992. dir (speedbar-line-directory)
  21993. txt (buffer-substring-no-properties (or tp (point-min))
  21994. (or np (point-max))))
  21995. (with-current-buffer (find-file-noselect
  21996. (let ((default-directory dir))
  21997. (expand-file-name txt)))
  21998. (unless (derived-mode-p 'org-mode)
  21999. (user-error "Cannot restrict to non-Org-mode file"))
  22000. (org-agenda-set-restriction-lock 'file)))
  22001. (t (user-error "Don't know how to restrict Org-mode's agenda")))
  22002. (move-overlay org-speedbar-restriction-lock-overlay
  22003. (point-at-bol) (point-at-eol))
  22004. (setq current-prefix-arg nil)
  22005. (org-agenda-maybe-redo)))
  22006. (defvar speedbar-file-key-map)
  22007. (declare-function speedbar-add-supported-extension "speedbar" (extension))
  22008. (eval-after-load "speedbar"
  22009. '(progn
  22010. (speedbar-add-supported-extension ".org")
  22011. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  22012. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  22013. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  22014. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  22015. (add-hook 'speedbar-visiting-tag-hook
  22016. (lambda () (and (derived-mode-p 'org-mode) (org-show-context 'org-goto))))))
  22017. ;;; Fixes and Hacks for problems with other packages
  22018. ;; Make flyspell not check words in links, to not mess up our keymap
  22019. (defvar org-element-affiliated-keywords) ; From org-element.el
  22020. (defvar org-element-block-name-alist) ; From org-element.el
  22021. (defun org-mode-flyspell-verify ()
  22022. "Don't let flyspell put overlays at active buttons, or on
  22023. {todo,all-time,additional-option-like}-keywords."
  22024. (require 'org-element) ; For `org-element-affiliated-keywords'
  22025. (let ((pos (max (1- (point)) (point-min)))
  22026. (word (thing-at-point 'word)))
  22027. (and (not (get-text-property pos 'keymap))
  22028. (not (get-text-property pos 'org-no-flyspell))
  22029. (not (member word org-todo-keywords-1))
  22030. (not (member word org-all-time-keywords))
  22031. (not (member word org-options-keywords))
  22032. (not (member word (mapcar 'car org-startup-options)))
  22033. (not (member-ignore-case word org-element-affiliated-keywords))
  22034. (not (member-ignore-case word (org-get-export-keywords)))
  22035. (not (member-ignore-case
  22036. word (mapcar 'car org-element-block-name-alist)))
  22037. (not (member-ignore-case word '("BEGIN" "END" "ATTR")))
  22038. (not (org-in-src-block-p)))))
  22039. (defun org-remove-flyspell-overlays-in (beg end)
  22040. "Remove flyspell overlays in region."
  22041. (and (org-bound-and-true-p flyspell-mode)
  22042. (fboundp 'flyspell-delete-region-overlays)
  22043. (flyspell-delete-region-overlays beg end))
  22044. (add-text-properties beg end '(org-no-flyspell t)))
  22045. ;; Make `bookmark-jump' shows the jump location if it was hidden.
  22046. (eval-after-load "bookmark"
  22047. '(if (boundp 'bookmark-after-jump-hook)
  22048. ;; We can use the hook
  22049. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  22050. ;; Hook not available, use advice
  22051. (defadvice bookmark-jump (after org-make-visible activate)
  22052. "Make the position visible."
  22053. (org-bookmark-jump-unhide))))
  22054. ;; Make sure saveplace shows the location if it was hidden
  22055. (eval-after-load "saveplace"
  22056. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  22057. "Make the position visible."
  22058. (org-bookmark-jump-unhide)))
  22059. ;; Make sure ecb shows the location if it was hidden
  22060. (eval-after-load "ecb"
  22061. '(defadvice ecb-method-clicked (after esf/org-show-context activate)
  22062. "Make hierarchy visible when jumping into location from ECB tree buffer."
  22063. (if (derived-mode-p 'org-mode)
  22064. (org-show-context))))
  22065. (defun org-bookmark-jump-unhide ()
  22066. "Unhide the current position, to show the bookmark location."
  22067. (and (derived-mode-p 'org-mode)
  22068. (or (outline-invisible-p)
  22069. (save-excursion (goto-char (max (point-min) (1- (point))))
  22070. (outline-invisible-p)))
  22071. (org-show-context 'bookmark-jump)))
  22072. ;; Make session.el ignore our circular variable
  22073. (defvar session-globals-exclude)
  22074. (eval-after-load "session"
  22075. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  22076. ;;;; Finish up
  22077. (provide 'org)
  22078. (run-hooks 'org-load-hook)
  22079. ;;; org.el ends here