org.el 894 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515175161751717518175191752017521175221752317524175251752617527175281752917530175311753217533175341753517536175371753817539175401754117542175431754417545175461754717548175491755017551175521755317554175551755617557175581755917560175611756217563175641756517566175671756817569175701757117572175731757417575175761757717578175791758017581175821758317584175851758617587175881758917590175911759217593175941759517596175971759817599176001760117602176031760417605176061760717608176091761017611176121761317614176151761617617176181761917620176211762217623176241762517626176271762817629176301763117632176331763417635176361763717638176391764017641176421764317644176451764617647176481764917650176511765217653176541765517656176571765817659176601766117662176631766417665176661766717668176691767017671176721767317674176751767617677176781767917680176811768217683176841768517686176871768817689176901769117692176931769417695176961769717698176991770017701177021770317704177051770617707177081770917710177111771217713177141771517716177171771817719177201772117722177231772417725177261772717728177291773017731177321773317734177351773617737177381773917740177411774217743177441774517746177471774817749177501775117752177531775417755177561775717758177591776017761177621776317764177651776617767177681776917770177711777217773177741777517776177771777817779177801778117782177831778417785177861778717788177891779017791177921779317794177951779617797177981779917800178011780217803178041780517806178071780817809178101781117812178131781417815178161781717818178191782017821178221782317824178251782617827178281782917830178311783217833178341783517836178371783817839178401784117842178431784417845178461784717848178491785017851178521785317854178551785617857178581785917860178611786217863178641786517866178671786817869178701787117872178731787417875178761787717878178791788017881178821788317884178851788617887178881788917890178911789217893178941789517896178971789817899179001790117902179031790417905179061790717908179091791017911179121791317914179151791617917179181791917920179211792217923179241792517926179271792817929179301793117932179331793417935179361793717938179391794017941179421794317944179451794617947179481794917950179511795217953179541795517956179571795817959179601796117962179631796417965179661796717968179691797017971179721797317974179751797617977179781797917980179811798217983179841798517986179871798817989179901799117992179931799417995179961799717998179991800018001180021800318004180051800618007180081800918010180111801218013180141801518016180171801818019180201802118022180231802418025180261802718028180291803018031180321803318034180351803618037180381803918040180411804218043180441804518046180471804818049180501805118052180531805418055180561805718058180591806018061180621806318064180651806618067180681806918070180711807218073180741807518076180771807818079180801808118082180831808418085180861808718088180891809018091180921809318094180951809618097180981809918100181011810218103181041810518106181071810818109181101811118112181131811418115181161811718118181191812018121181221812318124181251812618127181281812918130181311813218133181341813518136181371813818139181401814118142181431814418145181461814718148181491815018151181521815318154181551815618157181581815918160181611816218163181641816518166181671816818169181701817118172181731817418175181761817718178181791818018181181821818318184181851818618187181881818918190181911819218193181941819518196181971819818199182001820118202182031820418205182061820718208182091821018211182121821318214182151821618217182181821918220182211822218223182241822518226182271822818229182301823118232182331823418235182361823718238182391824018241182421824318244182451824618247182481824918250182511825218253182541825518256182571825818259182601826118262182631826418265182661826718268182691827018271182721827318274182751827618277182781827918280182811828218283182841828518286182871828818289182901829118292182931829418295182961829718298182991830018301183021830318304183051830618307183081830918310183111831218313183141831518316183171831818319183201832118322183231832418325183261832718328183291833018331183321833318334183351833618337183381833918340183411834218343183441834518346183471834818349183501835118352183531835418355183561835718358183591836018361183621836318364183651836618367183681836918370183711837218373183741837518376183771837818379183801838118382183831838418385183861838718388183891839018391183921839318394183951839618397183981839918400184011840218403184041840518406184071840818409184101841118412184131841418415184161841718418184191842018421184221842318424184251842618427184281842918430184311843218433184341843518436184371843818439184401844118442184431844418445184461844718448184491845018451184521845318454184551845618457184581845918460184611846218463184641846518466184671846818469184701847118472184731847418475184761847718478184791848018481184821848318484184851848618487184881848918490184911849218493184941849518496184971849818499185001850118502185031850418505185061850718508185091851018511185121851318514185151851618517185181851918520185211852218523185241852518526185271852818529185301853118532185331853418535185361853718538185391854018541185421854318544185451854618547185481854918550185511855218553185541855518556185571855818559185601856118562185631856418565185661856718568185691857018571185721857318574185751857618577185781857918580185811858218583185841858518586185871858818589185901859118592185931859418595185961859718598185991860018601186021860318604186051860618607186081860918610186111861218613186141861518616186171861818619186201862118622186231862418625186261862718628186291863018631186321863318634186351863618637186381863918640186411864218643186441864518646186471864818649186501865118652186531865418655186561865718658186591866018661186621866318664186651866618667186681866918670186711867218673186741867518676186771867818679186801868118682186831868418685186861868718688186891869018691186921869318694186951869618697186981869918700187011870218703187041870518706187071870818709187101871118712187131871418715187161871718718187191872018721187221872318724187251872618727187281872918730187311873218733187341873518736187371873818739187401874118742187431874418745187461874718748187491875018751187521875318754187551875618757187581875918760187611876218763187641876518766187671876818769187701877118772187731877418775187761877718778187791878018781187821878318784187851878618787187881878918790187911879218793187941879518796187971879818799188001880118802188031880418805188061880718808188091881018811188121881318814188151881618817188181881918820188211882218823188241882518826188271882818829188301883118832188331883418835188361883718838188391884018841188421884318844188451884618847188481884918850188511885218853188541885518856188571885818859188601886118862188631886418865188661886718868188691887018871188721887318874188751887618877188781887918880188811888218883188841888518886188871888818889188901889118892188931889418895188961889718898188991890018901189021890318904189051890618907189081890918910189111891218913189141891518916189171891818919189201892118922189231892418925189261892718928189291893018931189321893318934189351893618937189381893918940189411894218943189441894518946189471894818949189501895118952189531895418955189561895718958189591896018961189621896318964189651896618967189681896918970189711897218973189741897518976189771897818979189801898118982189831898418985189861898718988189891899018991189921899318994189951899618997189981899919000190011900219003190041900519006190071900819009190101901119012190131901419015190161901719018190191902019021190221902319024190251902619027190281902919030190311903219033190341903519036190371903819039190401904119042190431904419045190461904719048190491905019051190521905319054190551905619057190581905919060190611906219063190641906519066190671906819069190701907119072190731907419075190761907719078190791908019081190821908319084190851908619087190881908919090190911909219093190941909519096190971909819099191001910119102191031910419105191061910719108191091911019111191121911319114191151911619117191181911919120191211912219123191241912519126191271912819129191301913119132191331913419135191361913719138191391914019141191421914319144191451914619147191481914919150191511915219153191541915519156191571915819159191601916119162191631916419165191661916719168191691917019171191721917319174191751917619177191781917919180191811918219183191841918519186191871918819189191901919119192191931919419195191961919719198191991920019201192021920319204192051920619207192081920919210192111921219213192141921519216192171921819219192201922119222192231922419225192261922719228192291923019231192321923319234192351923619237192381923919240192411924219243192441924519246192471924819249192501925119252192531925419255192561925719258192591926019261192621926319264192651926619267192681926919270192711927219273192741927519276192771927819279192801928119282192831928419285192861928719288192891929019291192921929319294192951929619297192981929919300193011930219303193041930519306193071930819309193101931119312193131931419315193161931719318193191932019321193221932319324193251932619327193281932919330193311933219333193341933519336193371933819339193401934119342193431934419345193461934719348193491935019351193521935319354193551935619357193581935919360193611936219363193641936519366193671936819369193701937119372193731937419375193761937719378193791938019381193821938319384193851938619387193881938919390193911939219393193941939519396193971939819399194001940119402194031940419405194061940719408194091941019411194121941319414194151941619417194181941919420194211942219423194241942519426194271942819429194301943119432194331943419435194361943719438194391944019441194421944319444194451944619447194481944919450194511945219453194541945519456194571945819459194601946119462194631946419465194661946719468194691947019471194721947319474194751947619477194781947919480194811948219483194841948519486194871948819489194901949119492194931949419495194961949719498194991950019501195021950319504195051950619507195081950919510195111951219513195141951519516195171951819519195201952119522195231952419525195261952719528195291953019531195321953319534195351953619537195381953919540195411954219543195441954519546195471954819549195501955119552195531955419555195561955719558195591956019561195621956319564195651956619567195681956919570195711957219573195741957519576195771957819579195801958119582195831958419585195861958719588195891959019591195921959319594195951959619597195981959919600196011960219603196041960519606196071960819609196101961119612196131961419615196161961719618196191962019621196221962319624196251962619627196281962919630196311963219633196341963519636196371963819639196401964119642196431964419645196461964719648196491965019651196521965319654196551965619657196581965919660196611966219663196641966519666196671966819669196701967119672196731967419675196761967719678196791968019681196821968319684196851968619687196881968919690196911969219693196941969519696196971969819699197001970119702197031970419705197061970719708197091971019711197121971319714197151971619717197181971919720197211972219723197241972519726197271972819729197301973119732197331973419735197361973719738197391974019741197421974319744197451974619747197481974919750197511975219753197541975519756197571975819759197601976119762197631976419765197661976719768197691977019771197721977319774197751977619777197781977919780197811978219783197841978519786197871978819789197901979119792197931979419795197961979719798197991980019801198021980319804198051980619807198081980919810198111981219813198141981519816198171981819819198201982119822198231982419825198261982719828198291983019831198321983319834198351983619837198381983919840198411984219843198441984519846198471984819849198501985119852198531985419855198561985719858198591986019861198621986319864198651986619867198681986919870198711987219873198741987519876198771987819879198801988119882198831988419885198861988719888198891989019891198921989319894198951989619897198981989919900199011990219903199041990519906199071990819909199101991119912199131991419915199161991719918199191992019921199221992319924199251992619927199281992919930199311993219933199341993519936199371993819939199401994119942199431994419945199461994719948199491995019951199521995319954199551995619957199581995919960199611996219963199641996519966199671996819969199701997119972199731997419975199761997719978199791998019981199821998319984199851998619987199881998919990199911999219993199941999519996199971999819999200002000120002200032000420005200062000720008200092001020011200122001320014200152001620017200182001920020200212002220023200242002520026200272002820029200302003120032200332003420035200362003720038200392004020041200422004320044200452004620047200482004920050200512005220053200542005520056200572005820059200602006120062200632006420065200662006720068200692007020071200722007320074200752007620077200782007920080200812008220083200842008520086200872008820089200902009120092200932009420095200962009720098200992010020101201022010320104201052010620107201082010920110201112011220113201142011520116201172011820119201202012120122201232012420125201262012720128201292013020131201322013320134201352013620137201382013920140201412014220143201442014520146201472014820149201502015120152201532015420155201562015720158201592016020161201622016320164201652016620167201682016920170201712017220173201742017520176201772017820179201802018120182201832018420185201862018720188201892019020191201922019320194201952019620197201982019920200202012020220203202042020520206202072020820209202102021120212202132021420215202162021720218202192022020221202222022320224202252022620227202282022920230202312023220233202342023520236202372023820239202402024120242202432024420245202462024720248202492025020251202522025320254202552025620257202582025920260202612026220263202642026520266202672026820269202702027120272202732027420275202762027720278202792028020281202822028320284202852028620287202882028920290202912029220293202942029520296202972029820299203002030120302203032030420305203062030720308203092031020311203122031320314203152031620317203182031920320203212032220323203242032520326203272032820329203302033120332203332033420335203362033720338203392034020341203422034320344203452034620347203482034920350203512035220353203542035520356203572035820359203602036120362203632036420365203662036720368203692037020371203722037320374203752037620377203782037920380203812038220383203842038520386203872038820389203902039120392203932039420395203962039720398203992040020401204022040320404204052040620407204082040920410204112041220413204142041520416204172041820419204202042120422204232042420425204262042720428204292043020431204322043320434204352043620437204382043920440204412044220443204442044520446204472044820449204502045120452204532045420455204562045720458204592046020461204622046320464204652046620467204682046920470204712047220473204742047520476204772047820479204802048120482204832048420485204862048720488204892049020491204922049320494204952049620497204982049920500205012050220503205042050520506205072050820509205102051120512205132051420515205162051720518205192052020521205222052320524205252052620527205282052920530205312053220533205342053520536205372053820539205402054120542205432054420545205462054720548205492055020551205522055320554205552055620557205582055920560205612056220563205642056520566205672056820569205702057120572205732057420575205762057720578205792058020581205822058320584205852058620587205882058920590205912059220593205942059520596205972059820599206002060120602206032060420605206062060720608206092061020611206122061320614206152061620617206182061920620206212062220623206242062520626206272062820629206302063120632206332063420635206362063720638206392064020641206422064320644206452064620647206482064920650206512065220653206542065520656206572065820659206602066120662206632066420665206662066720668206692067020671206722067320674206752067620677206782067920680206812068220683206842068520686206872068820689206902069120692206932069420695206962069720698206992070020701207022070320704207052070620707207082070920710207112071220713207142071520716207172071820719207202072120722207232072420725207262072720728207292073020731207322073320734207352073620737207382073920740207412074220743207442074520746207472074820749207502075120752207532075420755207562075720758207592076020761207622076320764207652076620767207682076920770207712077220773207742077520776207772077820779207802078120782207832078420785207862078720788207892079020791207922079320794207952079620797207982079920800208012080220803208042080520806208072080820809208102081120812208132081420815208162081720818208192082020821208222082320824208252082620827208282082920830208312083220833208342083520836208372083820839208402084120842208432084420845208462084720848208492085020851208522085320854208552085620857208582085920860208612086220863208642086520866208672086820869208702087120872208732087420875208762087720878208792088020881208822088320884208852088620887208882088920890208912089220893208942089520896208972089820899209002090120902209032090420905209062090720908209092091020911209122091320914209152091620917209182091920920209212092220923209242092520926209272092820929209302093120932209332093420935209362093720938209392094020941209422094320944209452094620947209482094920950209512095220953209542095520956209572095820959209602096120962209632096420965209662096720968209692097020971209722097320974209752097620977209782097920980209812098220983209842098520986209872098820989209902099120992209932099420995209962099720998209992100021001210022100321004210052100621007210082100921010210112101221013210142101521016210172101821019210202102121022210232102421025210262102721028210292103021031210322103321034210352103621037210382103921040210412104221043210442104521046210472104821049210502105121052210532105421055210562105721058210592106021061210622106321064210652106621067210682106921070210712107221073210742107521076210772107821079210802108121082210832108421085210862108721088210892109021091210922109321094210952109621097210982109921100211012110221103211042110521106211072110821109211102111121112211132111421115211162111721118211192112021121211222112321124211252112621127211282112921130211312113221133211342113521136211372113821139211402114121142211432114421145211462114721148211492115021151211522115321154211552115621157211582115921160211612116221163211642116521166211672116821169211702117121172211732117421175211762117721178211792118021181211822118321184211852118621187211882118921190211912119221193211942119521196211972119821199212002120121202212032120421205212062120721208212092121021211212122121321214212152121621217212182121921220212212122221223212242122521226212272122821229212302123121232212332123421235212362123721238212392124021241212422124321244212452124621247212482124921250212512125221253212542125521256212572125821259212602126121262212632126421265212662126721268212692127021271212722127321274212752127621277212782127921280212812128221283212842128521286212872128821289212902129121292212932129421295212962129721298212992130021301213022130321304213052130621307213082130921310213112131221313213142131521316213172131821319213202132121322213232132421325213262132721328213292133021331213322133321334213352133621337213382133921340213412134221343213442134521346213472134821349213502135121352213532135421355213562135721358213592136021361213622136321364213652136621367213682136921370213712137221373213742137521376213772137821379213802138121382213832138421385213862138721388213892139021391213922139321394213952139621397213982139921400214012140221403214042140521406214072140821409214102141121412214132141421415214162141721418214192142021421214222142321424214252142621427214282142921430214312143221433214342143521436214372143821439214402144121442214432144421445214462144721448214492145021451214522145321454214552145621457214582145921460214612146221463214642146521466214672146821469214702147121472214732147421475214762147721478214792148021481214822148321484214852148621487214882148921490214912149221493214942149521496214972149821499215002150121502215032150421505215062150721508215092151021511215122151321514215152151621517215182151921520215212152221523215242152521526215272152821529215302153121532215332153421535215362153721538215392154021541215422154321544215452154621547215482154921550215512155221553215542155521556215572155821559215602156121562215632156421565215662156721568215692157021571215722157321574215752157621577215782157921580215812158221583215842158521586215872158821589215902159121592215932159421595215962159721598215992160021601216022160321604216052160621607216082160921610216112161221613216142161521616216172161821619216202162121622216232162421625216262162721628216292163021631216322163321634216352163621637216382163921640216412164221643216442164521646216472164821649216502165121652216532165421655216562165721658216592166021661216622166321664216652166621667216682166921670216712167221673216742167521676216772167821679216802168121682216832168421685216862168721688216892169021691216922169321694216952169621697216982169921700217012170221703217042170521706217072170821709217102171121712217132171421715217162171721718217192172021721217222172321724217252172621727217282172921730217312173221733217342173521736217372173821739217402174121742217432174421745217462174721748217492175021751217522175321754217552175621757217582175921760217612176221763217642176521766217672176821769217702177121772217732177421775217762177721778217792178021781217822178321784217852178621787217882178921790217912179221793217942179521796217972179821799218002180121802218032180421805218062180721808218092181021811218122181321814218152181621817218182181921820218212182221823218242182521826218272182821829218302183121832218332183421835218362183721838218392184021841218422184321844218452184621847218482184921850218512185221853218542185521856218572185821859218602186121862218632186421865218662186721868218692187021871218722187321874218752187621877218782187921880218812188221883218842188521886218872188821889218902189121892218932189421895218962189721898218992190021901219022190321904219052190621907219082190921910219112191221913219142191521916219172191821919219202192121922219232192421925219262192721928219292193021931219322193321934219352193621937219382193921940219412194221943219442194521946219472194821949219502195121952219532195421955219562195721958219592196021961219622196321964219652196621967219682196921970219712197221973219742197521976219772197821979219802198121982219832198421985219862198721988219892199021991219922199321994219952199621997219982199922000220012200222003220042200522006220072200822009220102201122012220132201422015220162201722018220192202022021220222202322024220252202622027220282202922030220312203222033220342203522036220372203822039220402204122042220432204422045220462204722048220492205022051220522205322054220552205622057220582205922060220612206222063220642206522066220672206822069220702207122072220732207422075220762207722078220792208022081220822208322084220852208622087220882208922090220912209222093220942209522096220972209822099221002210122102221032210422105221062210722108221092211022111221122211322114221152211622117221182211922120221212212222123221242212522126221272212822129221302213122132221332213422135221362213722138221392214022141221422214322144221452214622147221482214922150221512215222153221542215522156221572215822159221602216122162221632216422165221662216722168221692217022171221722217322174221752217622177221782217922180221812218222183221842218522186221872218822189221902219122192221932219422195221962219722198221992220022201222022220322204222052220622207222082220922210222112221222213222142221522216222172221822219222202222122222222232222422225222262222722228222292223022231222322223322234222352223622237222382223922240222412224222243222442224522246222472224822249222502225122252222532225422255222562225722258222592226022261222622226322264222652226622267222682226922270222712227222273222742227522276222772227822279222802228122282222832228422285222862228722288222892229022291222922229322294222952229622297222982229922300223012230222303223042230522306223072230822309223102231122312223132231422315223162231722318223192232022321223222232322324223252232622327223282232922330223312233222333223342233522336223372233822339223402234122342223432234422345223462234722348223492235022351223522235322354223552235622357223582235922360223612236222363223642236522366223672236822369223702237122372223732237422375223762237722378223792238022381223822238322384223852238622387223882238922390223912239222393223942239522396223972239822399224002240122402224032240422405224062240722408224092241022411224122241322414224152241622417224182241922420224212242222423224242242522426224272242822429224302243122432224332243422435224362243722438224392244022441224422244322444224452244622447224482244922450224512245222453224542245522456224572245822459224602246122462224632246422465224662246722468224692247022471224722247322474224752247622477224782247922480224812248222483224842248522486224872248822489224902249122492224932249422495224962249722498224992250022501225022250322504225052250622507225082250922510225112251222513225142251522516225172251822519225202252122522225232252422525225262252722528225292253022531225322253322534225352253622537225382253922540225412254222543225442254522546225472254822549225502255122552225532255422555225562255722558225592256022561225622256322564225652256622567225682256922570225712257222573225742257522576225772257822579225802258122582225832258422585225862258722588225892259022591225922259322594225952259622597225982259922600226012260222603226042260522606226072260822609226102261122612226132261422615226162261722618226192262022621226222262322624226252262622627226282262922630226312263222633226342263522636226372263822639226402264122642226432264422645226462264722648226492265022651226522265322654226552265622657226582265922660226612266222663226642266522666226672266822669226702267122672226732267422675226762267722678226792268022681226822268322684226852268622687226882268922690226912269222693226942269522696226972269822699227002270122702227032270422705227062270722708227092271022711227122271322714227152271622717227182271922720227212272222723227242272522726227272272822729227302273122732227332273422735227362273722738227392274022741227422274322744227452274622747227482274922750227512275222753227542275522756227572275822759227602276122762227632276422765227662276722768227692277022771227722277322774227752277622777227782277922780227812278222783227842278522786227872278822789227902279122792227932279422795227962279722798227992280022801228022280322804228052280622807228082280922810228112281222813228142281522816228172281822819228202282122822228232282422825228262282722828228292283022831228322283322834228352283622837228382283922840228412284222843228442284522846228472284822849228502285122852228532285422855228562285722858228592286022861228622286322864228652286622867228682286922870228712287222873228742287522876228772287822879228802288122882228832288422885228862288722888228892289022891228922289322894228952289622897228982289922900229012290222903229042290522906229072290822909229102291122912229132291422915229162291722918229192292022921229222292322924229252292622927229282292922930229312293222933229342293522936229372293822939229402294122942229432294422945229462294722948229492295022951229522295322954229552295622957229582295922960229612296222963229642296522966229672296822969229702297122972229732297422975229762297722978229792298022981229822298322984229852298622987229882298922990229912299222993229942299522996229972299822999230002300123002230032300423005230062300723008230092301023011230122301323014230152301623017230182301923020230212302223023230242302523026230272302823029230302303123032230332303423035230362303723038230392304023041230422304323044230452304623047230482304923050230512305223053230542305523056230572305823059230602306123062230632306423065230662306723068230692307023071230722307323074230752307623077230782307923080230812308223083230842308523086230872308823089230902309123092230932309423095230962309723098230992310023101231022310323104231052310623107231082310923110231112311223113231142311523116231172311823119231202312123122231232312423125231262312723128231292313023131231322313323134231352313623137231382313923140231412314223143231442314523146231472314823149231502315123152231532315423155231562315723158231592316023161231622316323164231652316623167231682316923170231712317223173231742317523176231772317823179231802318123182231832318423185231862318723188231892319023191231922319323194231952319623197231982319923200232012320223203232042320523206232072320823209232102321123212232132321423215232162321723218232192322023221232222322323224232252322623227232282322923230232312323223233232342323523236232372323823239232402324123242232432324423245232462324723248232492325023251232522325323254232552325623257232582325923260232612326223263232642326523266232672326823269232702327123272232732327423275232762327723278232792328023281232822328323284232852328623287232882328923290232912329223293232942329523296232972329823299233002330123302233032330423305233062330723308233092331023311233122331323314233152331623317233182331923320233212332223323233242332523326233272332823329233302333123332233332333423335233362333723338233392334023341233422334323344233452334623347233482334923350233512335223353233542335523356233572335823359233602336123362233632336423365233662336723368233692337023371233722337323374233752337623377233782337923380233812338223383233842338523386233872338823389233902339123392233932339423395233962339723398233992340023401234022340323404234052340623407234082340923410234112341223413234142341523416234172341823419234202342123422234232342423425234262342723428234292343023431234322343323434234352343623437234382343923440234412344223443234442344523446234472344823449234502345123452234532345423455234562345723458234592346023461234622346323464234652346623467234682346923470234712347223473234742347523476234772347823479234802348123482234832348423485234862348723488234892349023491234922349323494234952349623497234982349923500235012350223503235042350523506235072350823509235102351123512235132351423515235162351723518235192352023521235222352323524235252352623527235282352923530235312353223533235342353523536235372353823539235402354123542235432354423545235462354723548235492355023551235522355323554235552355623557235582355923560235612356223563235642356523566235672356823569235702357123572235732357423575235762357723578235792358023581235822358323584235852358623587235882358923590235912359223593235942359523596235972359823599236002360123602236032360423605236062360723608236092361023611236122361323614236152361623617236182361923620236212362223623236242362523626236272362823629236302363123632236332363423635236362363723638236392364023641236422364323644236452364623647
  1. ;;; org.el --- Outline-based notes management and organizer
  2. ;; Carstens outline-mode for keeping track of everything.
  3. ;; Copyright (C) 2004-2013 Free Software Foundation, Inc.
  4. ;;
  5. ;; Author: Carsten Dominik <carsten at orgmode dot org>
  6. ;; Maintainer: Carsten Dominik <carsten at orgmode dot org>
  7. ;; Keywords: outlines, hypermedia, calendar, wp
  8. ;; Homepage: http://orgmode.org
  9. ;;
  10. ;; This file is part of GNU Emacs.
  11. ;;
  12. ;; GNU Emacs is free software: you can redistribute it and/or modify
  13. ;; it under the terms of the GNU General Public License as published by
  14. ;; the Free Software Foundation, either version 3 of the License, or
  15. ;; (at your option) any later version.
  16. ;; GNU Emacs is distributed in the hope that it will be useful,
  17. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. ;; GNU General Public License for more details.
  20. ;; You should have received a copy of the GNU General Public License
  21. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  22. ;;
  23. ;;; Commentary:
  24. ;;
  25. ;; Org-mode is a mode for keeping notes, maintaining ToDo lists, and doing
  26. ;; project planning with a fast and effective plain-text system.
  27. ;;
  28. ;; Org-mode develops organizational tasks around NOTES files that contain
  29. ;; information about projects as plain text. Org-mode is implemented on
  30. ;; top of outline-mode, which makes it possible to keep the content of
  31. ;; large files well structured. Visibility cycling and structure editing
  32. ;; help to work with the tree. Tables are easily created with a built-in
  33. ;; table editor. Org-mode supports ToDo items, deadlines, time stamps,
  34. ;; and scheduling. It dynamically compiles entries into an agenda that
  35. ;; utilizes and smoothly integrates much of the Emacs calendar and diary.
  36. ;; Plain text URL-like links connect to websites, emails, Usenet
  37. ;; messages, BBDB entries, and any files related to the projects. For
  38. ;; printing and sharing of notes, an Org-mode file can be exported as a
  39. ;; structured ASCII file, as HTML, or (todo and agenda items only) as an
  40. ;; iCalendar file. It can also serve as a publishing tool for a set of
  41. ;; linked webpages.
  42. ;;
  43. ;; Installation and Activation
  44. ;; ---------------------------
  45. ;; See the corresponding sections in the manual at
  46. ;;
  47. ;; http://orgmode.org/org.html#Installation
  48. ;;
  49. ;; Documentation
  50. ;; -------------
  51. ;; The documentation of Org-mode can be found in the TeXInfo file. The
  52. ;; distribution also contains a PDF version of it. At the homepage of
  53. ;; Org-mode, you can read the same text online as HTML. There is also an
  54. ;; excellent reference card made by Philip Rooke. This card can be found
  55. ;; in the etc/ directory of Emacs 22.
  56. ;;
  57. ;; A list of recent changes can be found at
  58. ;; http://orgmode.org/Changes.html
  59. ;;
  60. ;;; Code:
  61. (defvar org-inhibit-highlight-removal nil) ; dynamically scoped param
  62. (defvar org-table-formula-constants-local nil
  63. "Local version of `org-table-formula-constants'.")
  64. (make-variable-buffer-local 'org-table-formula-constants-local)
  65. ;;;; Require other packages
  66. (eval-when-compile
  67. (require 'cl)
  68. (require 'gnus-sum))
  69. (require 'calendar)
  70. (require 'find-func)
  71. (require 'format-spec)
  72. (load "org-loaddefs.el" t t t)
  73. (require 'org-macs)
  74. (require 'org-compat)
  75. ;; `org-outline-regexp' ought to be a defconst but is let-binding in
  76. ;; some places -- e.g. see the macro `org-with-limited-levels'.
  77. ;;
  78. ;; In Org buffers, the value of `outline-regexp' is that of
  79. ;; `org-outline-regexp'. The only function still directly relying on
  80. ;; `outline-regexp' is `org-overview' so that `org-cycle' can do its
  81. ;; job when `orgstruct-mode' is active.
  82. (defvar org-outline-regexp "\\*+ "
  83. "Regexp to match Org headlines.")
  84. (defvar org-outline-regexp-bol "^\\*+ "
  85. "Regexp to match Org headlines.
  86. This is similar to `org-outline-regexp' but additionally makes
  87. sure that we are at the beginning of the line.")
  88. (defvar org-heading-regexp "^\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  89. "Matches a headline, putting stars and text into groups.
  90. Stars are put in group 1 and the trimmed body in group 2.")
  91. ;; Emacs 22 calendar compatibility: Make sure the new variables are available
  92. (unless (boundp 'calendar-view-holidays-initially-flag)
  93. (org-defvaralias 'calendar-view-holidays-initially-flag
  94. 'view-calendar-holidays-initially))
  95. (unless (boundp 'calendar-view-diary-initially-flag)
  96. (org-defvaralias 'calendar-view-diary-initially-flag
  97. 'view-diary-entries-initially))
  98. (unless (boundp 'diary-fancy-buffer)
  99. (org-defvaralias 'diary-fancy-buffer 'fancy-diary-buffer))
  100. (declare-function org-inlinetask-at-task-p "org-inlinetask" ())
  101. (declare-function org-inlinetask-outline-regexp "org-inlinetask" ())
  102. (declare-function org-inlinetask-toggle-visibility "org-inlinetask" ())
  103. (declare-function org-pop-to-buffer-same-window "org-compat" (&optional buffer-or-name norecord label))
  104. (declare-function org-clock-get-last-clock-out-time "org-clock" ())
  105. (declare-function org-clock-timestamps-up "org-clock" (&optional n))
  106. (declare-function org-clock-timestamps-down "org-clock" (&optional n))
  107. (declare-function org-clock-sum-current-item "org-clock" (&optional tstart))
  108. (declare-function orgtbl-mode "org-table" (&optional arg))
  109. (declare-function org-clock-out "org-clock" (&optional switch-to-state fail-quietly at-time))
  110. (declare-function org-beamer-mode "ox-beamer" ())
  111. (declare-function org-table-edit-field "org-table" (arg))
  112. (declare-function org-table-justify-field-maybe "org-table" (&optional new))
  113. (declare-function org-table-set-constants "org-table" ())
  114. (declare-function org-table-calc-current-TBLFM "org-table" (&optional arg))
  115. (declare-function org-id-get-create "org-id" (&optional force))
  116. (declare-function org-id-find-id-file "org-id" (id))
  117. (declare-function org-tags-view "org-agenda" (&optional todo-only match))
  118. (declare-function org-agenda-list "org-agenda" (&optional arg start-day span))
  119. (declare-function org-agenda-redo "org-agenda" (&optional all))
  120. (declare-function org-table-align "org-table" ())
  121. (declare-function org-table-paste-rectangle "org-table" ())
  122. (declare-function org-table-maybe-eval-formula "org-table" ())
  123. (declare-function org-table-maybe-recalculate-line "org-table" ())
  124. (declare-function org-element--parse-objects "org-element"
  125. (beg end acc restriction))
  126. (declare-function org-element-at-point "org-element" (&optional keep-trail))
  127. (declare-function org-element-contents "org-element" (element))
  128. (declare-function org-element-context "org-element" (&optional element))
  129. (declare-function org-element-interpret-data "org-element"
  130. (data &optional parent))
  131. (declare-function org-element-map "org-element"
  132. (data types fun &optional info first-match no-recursion))
  133. (declare-function org-element-nested-p "org-element" (elem-a elem-b))
  134. (declare-function org-element-parse-buffer "org-element"
  135. (&optional granularity visible-only))
  136. (declare-function org-element-property "org-element" (property element))
  137. (declare-function org-element-put-property "org-element"
  138. (element property value))
  139. (declare-function org-element-swap-A-B "org-element" (elem-a elem-b))
  140. (declare-function org-element--parse-objects "org-element"
  141. (beg end acc restriction))
  142. (declare-function org-element-parse-buffer "org-element"
  143. (&optional granularity visible-only))
  144. (declare-function org-element-restriction "org-element" (element))
  145. (declare-function org-element-type "org-element" (element))
  146. ;; load languages based on value of `org-babel-load-languages'
  147. (defvar org-babel-load-languages)
  148. ;;;###autoload
  149. (defun org-babel-do-load-languages (sym value)
  150. "Load the languages defined in `org-babel-load-languages'."
  151. (set-default sym value)
  152. (mapc (lambda (pair)
  153. (let ((active (cdr pair)) (lang (symbol-name (car pair))))
  154. (if active
  155. (progn
  156. (require (intern (concat "ob-" lang))))
  157. (progn
  158. (funcall 'fmakunbound
  159. (intern (concat "org-babel-execute:" lang)))
  160. (funcall 'fmakunbound
  161. (intern (concat "org-babel-expand-body:" lang)))))))
  162. org-babel-load-languages))
  163. ;;;###autoload
  164. (defun org-babel-load-file (file &optional compile)
  165. "Load Emacs Lisp source code blocks in the Org-mode FILE.
  166. This function exports the source code using `org-babel-tangle'
  167. and then loads the resulting file using `load-file'. With prefix
  168. arg (noninteractively: 2nd arg) COMPILE the tangled Emacs Lisp
  169. file to byte-code before it is loaded."
  170. (interactive "fFile to load: \nP")
  171. (require 'ob-core)
  172. (let* ((age (lambda (file)
  173. (float-time
  174. (time-subtract (current-time)
  175. (nth 5 (or (file-attributes (file-truename file))
  176. (file-attributes file)))))))
  177. (base-name (file-name-sans-extension file))
  178. (exported-file (concat base-name ".el")))
  179. ;; tangle if the org-mode file is newer than the elisp file
  180. (unless (and (file-exists-p exported-file)
  181. (> (funcall age file) (funcall age exported-file)))
  182. (org-babel-tangle-file file exported-file "emacs-lisp"))
  183. (message "%s %s"
  184. (if compile
  185. (progn (byte-compile-file exported-file 'load)
  186. "Compiled and loaded")
  187. (progn (load-file exported-file) "Loaded"))
  188. exported-file)))
  189. (defcustom org-babel-load-languages '((emacs-lisp . t))
  190. "Languages which can be evaluated in Org-mode buffers.
  191. This list can be used to load support for any of the languages
  192. below, note that each language will depend on a different set of
  193. system executables and/or Emacs modes. When a language is
  194. \"loaded\", then code blocks in that language can be evaluated
  195. with `org-babel-execute-src-block' bound by default to C-c
  196. C-c (note the `org-babel-no-eval-on-ctrl-c-ctrl-c' variable can
  197. be set to remove code block evaluation from the C-c C-c
  198. keybinding. By default only Emacs Lisp (which has no
  199. requirements) is loaded."
  200. :group 'org-babel
  201. :set 'org-babel-do-load-languages
  202. :version "24.1"
  203. :type '(alist :tag "Babel Languages"
  204. :key-type
  205. (choice
  206. (const :tag "Awk" awk)
  207. (const :tag "C" C)
  208. (const :tag "R" R)
  209. (const :tag "Asymptote" asymptote)
  210. (const :tag "Calc" calc)
  211. (const :tag "Clojure" clojure)
  212. (const :tag "CSS" css)
  213. (const :tag "Ditaa" ditaa)
  214. (const :tag "Dot" dot)
  215. (const :tag "Emacs Lisp" emacs-lisp)
  216. (const :tag "Fortran" fortran)
  217. (const :tag "Gnuplot" gnuplot)
  218. (const :tag "Haskell" haskell)
  219. (const :tag "IO" io)
  220. (const :tag "Java" java)
  221. (const :tag "Javascript" js)
  222. (const :tag "LaTeX" latex)
  223. (const :tag "Ledger" ledger)
  224. (const :tag "Lilypond" lilypond)
  225. (const :tag "Lisp" lisp)
  226. (const :tag "Makefile" makefile)
  227. (const :tag "Maxima" maxima)
  228. (const :tag "Matlab" matlab)
  229. (const :tag "Mscgen" mscgen)
  230. (const :tag "Ocaml" ocaml)
  231. (const :tag "Octave" octave)
  232. (const :tag "Org" org)
  233. (const :tag "Perl" perl)
  234. (const :tag "Pico Lisp" picolisp)
  235. (const :tag "PlantUML" plantuml)
  236. (const :tag "Python" python)
  237. (const :tag "Ruby" ruby)
  238. (const :tag "Sass" sass)
  239. (const :tag "Scala" scala)
  240. (const :tag "Scheme" scheme)
  241. (const :tag "Screen" screen)
  242. (const :tag "Shell Script" sh)
  243. (const :tag "Shen" shen)
  244. (const :tag "Sql" sql)
  245. (const :tag "Sqlite" sqlite))
  246. :value-type (boolean :tag "Activate" :value t)))
  247. ;;;; Customization variables
  248. (defcustom org-clone-delete-id nil
  249. "Remove ID property of clones of a subtree.
  250. When non-nil, clones of a subtree don't inherit the ID property.
  251. Otherwise they inherit the ID property with a new unique
  252. identifier."
  253. :type 'boolean
  254. :version "24.1"
  255. :group 'org-id)
  256. ;;; Version
  257. (org-check-version)
  258. ;;;###autoload
  259. (defun org-version (&optional here full message)
  260. "Show the org-mode version in the echo area.
  261. With prefix argument HERE, insert it at point.
  262. When FULL is non-nil, use a verbose version string.
  263. When MESSAGE is non-nil, display a message with the version."
  264. (interactive "P")
  265. (let* ((org-dir (ignore-errors (org-find-library-dir "org")))
  266. (save-load-suffixes (when (boundp 'load-suffixes) load-suffixes))
  267. (load-suffixes (list ".el"))
  268. (org-install-dir (ignore-errors (org-find-library-dir "org-loaddefs")))
  269. (org-trash (or
  270. (and (fboundp 'org-release) (fboundp 'org-git-version))
  271. (org-load-noerror-mustsuffix (concat org-dir "org-version"))))
  272. (load-suffixes save-load-suffixes)
  273. (org-version (org-release))
  274. (git-version (org-git-version))
  275. (version (format "Org-mode version %s (%s @ %s)"
  276. org-version
  277. git-version
  278. (if org-install-dir
  279. (if (string= org-dir org-install-dir)
  280. org-install-dir
  281. (concat "mixed installation! " org-install-dir " and " org-dir))
  282. "org-loaddefs.el can not be found!")))
  283. (_version (if full version org-version)))
  284. (if (org-called-interactively-p 'interactive)
  285. (if here
  286. (insert version)
  287. (message version))
  288. (if message (message _version))
  289. _version)))
  290. (defconst org-version (org-version))
  291. ;;; Compatibility constants
  292. ;;; The custom variables
  293. (defgroup org nil
  294. "Outline-based notes management and organizer."
  295. :tag "Org"
  296. :group 'outlines
  297. :group 'calendar)
  298. (defcustom org-mode-hook nil
  299. "Mode hook for Org-mode, run after the mode was turned on."
  300. :group 'org
  301. :type 'hook)
  302. (defcustom org-load-hook nil
  303. "Hook that is run after org.el has been loaded."
  304. :group 'org
  305. :type 'hook)
  306. (defcustom org-log-buffer-setup-hook nil
  307. "Hook that is run after an Org log buffer is created."
  308. :group 'org
  309. :version "24.1"
  310. :type 'hook)
  311. (defvar org-modules) ; defined below
  312. (defvar org-modules-loaded nil
  313. "Have the modules been loaded already?")
  314. (defun org-load-modules-maybe (&optional force)
  315. "Load all extensions listed in `org-modules'."
  316. (when (or force (not org-modules-loaded))
  317. (mapc (lambda (ext)
  318. (condition-case nil (require ext)
  319. (error (message "Problems while trying to load feature `%s'" ext))))
  320. org-modules)
  321. (setq org-modules-loaded t)))
  322. (defun org-set-modules (var value)
  323. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  324. (set var value)
  325. (when (featurep 'org)
  326. (org-load-modules-maybe 'force)))
  327. (defcustom org-modules '(org-w3m org-bbdb org-bibtex org-docview org-gnus org-info org-irc org-mhe org-rmail)
  328. "Modules that should always be loaded together with org.el.
  329. If a description starts with <C>, the file is not part of Emacs
  330. and loading it will require that you have downloaded and properly
  331. installed the Org mode distribution.
  332. You can also use this system to load external packages (i.e. neither Org
  333. core modules, nor modules from the CONTRIB directory). Just add symbols
  334. to the end of the list. If the package is called org-xyz.el, then you need
  335. to add the symbol `xyz', and the package must have a call to:
  336. \(provide 'org-xyz)
  337. For export specific modules, see also `org-export-backends'."
  338. :group 'org
  339. :set 'org-set-modules
  340. :version "24.4"
  341. :package-version '(Org . "8.0")
  342. :type
  343. '(set :greedy t
  344. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  345. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  346. (const :tag " crypt: Encryption of subtrees" org-crypt)
  347. (const :tag " ctags: Access to Emacs tags with links" org-ctags)
  348. (const :tag " docview: Links to doc-view buffers" org-docview)
  349. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  350. (const :tag " habit: Track your consistency with habits" org-habit)
  351. (const :tag " id: Global IDs for identifying entries" org-id)
  352. (const :tag " info: Links to Info nodes" org-info)
  353. (const :tag " inlinetask: Tasks independent of outline hierarchy" org-inlinetask)
  354. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  355. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  356. (const :tag " mouse: Additional mouse support" org-mouse)
  357. (const :tag " protocol: Intercept calls from emacsclient" org-protocol)
  358. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  359. (const :tag " w3m: Special cut/paste from w3m to Org-mode." org-w3m)
  360. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  361. (const :tag "C bookmark: Org-mode links to bookmarks" org-bookmark)
  362. (const :tag "C bullets: Add overlays to headlines stars" org-bullets)
  363. (const :tag "C checklist: Extra functions for checklists in repeated tasks" org-checklist)
  364. (const :tag "C choose: Use TODO keywords to mark decisions states" org-choose)
  365. (const :tag "C collector: Collect properties into tables" org-collector)
  366. (const :tag "C depend: TODO dependencies for Org-mode\n\t\t\t(PARTIALLY OBSOLETE, see built-in dependency support))" org-depend)
  367. (const :tag "C drill: Flashcards and spaced repetition for Org-mode" org-drill)
  368. (const :tag "C elisp-symbol: Org-mode links to emacs-lisp symbols" org-elisp-symbol)
  369. (const :tag "C eshell Support for links to working directories in eshell" org-eshell)
  370. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  371. (const :tag "C eval: Include command output as text" org-eval)
  372. (const :tag "C expiry: Expiry mechanism for Org-mode entries" org-expiry)
  373. (const :tag "C favtable: Lookup table of favorite references and links" org-favtable)
  374. (const :tag "C git-link: Provide org links to specific file version" org-git-link)
  375. (const :tag "C interactive-query: Interactive modification of tags query\n\t\t\t(PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query)
  376. (const :tag "C invoice: Help manage client invoices in Org-mode" org-invoice)
  377. (const :tag "C jira: Add a jira:ticket protocol to Org-mode" org-jira)
  378. (const :tag "C learn: SuperMemo's incremental learning algorithm" org-learn)
  379. (const :tag "C mac-iCal Imports events from iCal.app to the Emacs diary" org-mac-iCal)
  380. (const :tag "C mac-link-grabber Grab links and URLs from various Mac applications" org-mac-link-grabber)
  381. (const :tag "C mac-message: Links to messages in Apple Mail" org-mac-message)
  382. (const :tag "C mairix: Hook mairix search into Org-mode for different MUAs" org-mairix)
  383. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  384. (const :tag "C mew: Links to Mew folders/messages" org-mew)
  385. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  386. (const :tag "C notmuch: Provide org links to notmuch searches or messages" org-notmuch)
  387. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  388. (const :tag "C registry: A registry for Org-mode links" org-registry)
  389. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  390. (const :tag "C secretary: Team management with org-mode" org-secretary)
  391. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  392. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  393. (const :tag "C track: Keep up with Org-mode development" org-track)
  394. (const :tag "C velocity Something like Notational Velocity for Org" org-velocity)
  395. (const :tag "C vm: Links to VM folders/messages" org-vm)
  396. (const :tag "C wikinodes: CamelCase wiki-like links" org-wikinodes)
  397. (const :tag "C wl: Links to Wanderlust folders/messages" org-wl)
  398. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  399. (defvar org-export-registered-backends) ; From ox.el
  400. (declare-function org-export-derived-backend-p "ox" (backend &rest backends))
  401. (defcustom org-export-backends '(ascii html icalendar latex)
  402. "List of export back-ends that should be always available.
  403. If a description starts with <C>, the file is not part of Emacs
  404. and loading it will require that you have downloaded and properly
  405. installed the Org mode distribution.
  406. Unlike to `org-modules', libraries in this list will not be
  407. loaded along with Org, but only once the export framework is
  408. needed.
  409. This variable needs to be set before org.el is loaded. If you
  410. need to make a change while Emacs is running, use the customize
  411. interface or run the following code, where VALUE stands for the
  412. new value of the variable, after updating it:
  413. \(progn
  414. \(setq org-export-registered-backends
  415. \(org-remove-if-not
  416. \(lambda (backend)
  417. \(or (memq backend val)
  418. \(catch 'parentp
  419. \(mapc
  420. \(lambda (b)
  421. \(and (org-export-derived-backend-p b (car backend))
  422. \(throw 'parentp t)))
  423. val)
  424. nil)))
  425. org-export-registered-backends))
  426. \(let ((new-list (mapcar 'car org-export-registered-backends)))
  427. \(dolist (backend val)
  428. \(cond
  429. \((not (load (format \"ox-%s\" backend) t t))
  430. \(message \"Problems while trying to load export back-end `%s'\"
  431. backend))
  432. \((not (memq backend new-list)) (push backend new-list))))
  433. \(set-default var new-list)))
  434. Adding a back-end to this list will also pull the back-end it
  435. depends on, if any."
  436. :group 'org
  437. :group 'org-export
  438. :version "24.4"
  439. :package-version '(Org . "8.0")
  440. :initialize 'custom-initialize-set
  441. :set (lambda (var val)
  442. (if (not (featurep 'ox)) (set-default var val)
  443. ;; Any back-end not required anymore (not present in VAL and not
  444. ;; a parent of any back-end in the new value) is removed from the
  445. ;; list of registered back-ends.
  446. (setq org-export-registered-backends
  447. (org-remove-if-not
  448. (lambda (backend)
  449. (or (memq backend val)
  450. (catch 'parentp
  451. (mapc
  452. (lambda (b)
  453. (and (org-export-derived-backend-p b (car backend))
  454. (throw 'parentp t)))
  455. val)
  456. nil)))
  457. org-export-registered-backends))
  458. ;; Now build NEW-LIST of both new back-ends and required
  459. ;; parents.
  460. (let ((new-list (mapcar 'car org-export-registered-backends)))
  461. (dolist (backend val)
  462. (cond
  463. ((not (load (format "ox-%s" backend) t t))
  464. (message "Problems while trying to load export back-end `%s'"
  465. backend))
  466. ((not (memq backend new-list)) (push backend new-list))))
  467. ;; Set VAR to that list with fixed dependencies.
  468. (set-default var new-list))))
  469. :type '(set :greedy t
  470. (const :tag " ascii Export buffer to ASCII format" ascii)
  471. (const :tag " beamer Export buffer to Beamer presentation" beamer)
  472. (const :tag " html Export buffer to HTML format" html)
  473. (const :tag " icalendar Export buffer to iCalendar format" icalendar)
  474. (const :tag " latex Export buffer to LaTeX format" latex)
  475. (const :tag " man Export buffer to MAN format" man)
  476. (const :tag " md Export buffer to Markdown format" md)
  477. (const :tag " odt Export buffer to ODT format" odt)
  478. (const :tag " org Export buffer to Org format" org)
  479. (const :tag " texinfo Export buffer to Texinfo format" texinfo)
  480. (const :tag "C confluence Export buffer to Confluence Wiki format" confluence)
  481. (const :tag "C deck Export buffer to deck.js presentations" deck)
  482. (const :tag "C freemind Export buffer to Freemind mindmap format" freemind)
  483. (const :tag "C groff Export buffer to Groff format" groff)
  484. (const :tag "C koma-letter Export buffer to KOMA Scrlttrl2 format" koma-letter)
  485. (const :tag "C RSS 2.0 Export buffer to RSS 2.0 format" rss)
  486. (const :tag "C s5 Export buffer to s5 presentations" s5)
  487. (const :tag "C taskjuggler Export buffer to TaskJuggler format" taskjuggler)))
  488. (eval-after-load 'ox
  489. '(mapc
  490. (lambda (backend)
  491. (condition-case nil (require (intern (format "ox-%s" backend)))
  492. (error (message "Problems while trying to load export back-end `%s'"
  493. backend))))
  494. org-export-backends))
  495. (defcustom org-support-shift-select nil
  496. "Non-nil means make shift-cursor commands select text when possible.
  497. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys
  498. start selecting a region, or enlarge regions started in this way.
  499. In Org-mode, in special contexts, these same keys are used for
  500. other purposes, important enough to compete with shift selection.
  501. Org tries to balance these needs by supporting `shift-select-mode'
  502. outside these special contexts, under control of this variable.
  503. The default of this variable is nil, to avoid confusing behavior. Shifted
  504. cursor keys will then execute Org commands in the following contexts:
  505. - on a headline, changing TODO state (left/right) and priority (up/down)
  506. - on a time stamp, changing the time
  507. - in a plain list item, changing the bullet type
  508. - in a property definition line, switching between allowed values
  509. - in the BEGIN line of a clock table (changing the time block).
  510. Outside these contexts, the commands will throw an error.
  511. When this variable is t and the cursor is not in a special
  512. context, Org-mode will support shift-selection for making and
  513. enlarging regions. To make this more effective, the bullet
  514. cycling will no longer happen anywhere in an item line, but only
  515. if the cursor is exactly on the bullet.
  516. If you set this variable to the symbol `always', then the keys
  517. will not be special in headlines, property lines, and item lines,
  518. to make shift selection work there as well. If this is what you
  519. want, you can use the following alternative commands: `C-c C-t'
  520. and `C-c ,' to change TODO state and priority, `C-u C-u C-c C-t'
  521. can be used to switch TODO sets, `C-c -' to cycle item bullet
  522. types, and properties can be edited by hand or in column view.
  523. However, when the cursor is on a timestamp, shift-cursor commands
  524. will still edit the time stamp - this is just too good to give up.
  525. XEmacs user should have this variable set to nil, because
  526. `shift-select-mode' is in Emacs 23 or later only."
  527. :group 'org
  528. :type '(choice
  529. (const :tag "Never" nil)
  530. (const :tag "When outside special context" t)
  531. (const :tag "Everywhere except timestamps" always)))
  532. (defcustom org-loop-over-headlines-in-active-region nil
  533. "Shall some commands act upon headlines in the active region?
  534. When set to `t', some commands will be performed in all headlines
  535. within the active region.
  536. When set to `start-level', some commands will be performed in all
  537. headlines within the active region, provided that these headlines
  538. are of the same level than the first one.
  539. When set to a string, those commands will be performed on the
  540. matching headlines within the active region. Such string must be
  541. a tags/property/todo match as it is used in the agenda tags view.
  542. The list of commands is: `org-schedule', `org-deadline',
  543. `org-todo', `org-archive-subtree', `org-archive-set-tag' and
  544. `org-archive-to-archive-sibling'. The archiving commands skip
  545. already archived entries."
  546. :type '(choice (const :tag "Don't loop" nil)
  547. (const :tag "All headlines in active region" t)
  548. (const :tag "In active region, headlines at the same level than the first one" 'start-level)
  549. (string :tag "Tags/Property/Todo matcher"))
  550. :version "24.1"
  551. :group 'org-todo
  552. :group 'org-archive)
  553. (defgroup org-startup nil
  554. "Options concerning startup of Org-mode."
  555. :tag "Org Startup"
  556. :group 'org)
  557. (defcustom org-startup-folded t
  558. "Non-nil means entering Org-mode will switch to OVERVIEW.
  559. This can also be configured on a per-file basis by adding one of
  560. the following lines anywhere in the buffer:
  561. #+STARTUP: fold (or `overview', this is equivalent)
  562. #+STARTUP: nofold (or `showall', this is equivalent)
  563. #+STARTUP: content
  564. #+STARTUP: showeverything
  565. By default, this option is ignored when Org opens agenda files
  566. for the first time. If you want the agenda to honor the startup
  567. option, set `org-agenda-inhibit-startup' to nil."
  568. :group 'org-startup
  569. :type '(choice
  570. (const :tag "nofold: show all" nil)
  571. (const :tag "fold: overview" t)
  572. (const :tag "content: all headlines" content)
  573. (const :tag "show everything, even drawers" showeverything)))
  574. (defcustom org-startup-truncated t
  575. "Non-nil means entering Org-mode will set `truncate-lines'.
  576. This is useful since some lines containing links can be very long and
  577. uninteresting. Also tables look terrible when wrapped."
  578. :group 'org-startup
  579. :type 'boolean)
  580. (defcustom org-startup-indented nil
  581. "Non-nil means turn on `org-indent-mode' on startup.
  582. This can also be configured on a per-file basis by adding one of
  583. the following lines anywhere in the buffer:
  584. #+STARTUP: indent
  585. #+STARTUP: noindent"
  586. :group 'org-structure
  587. :type '(choice
  588. (const :tag "Not" nil)
  589. (const :tag "Globally (slow on startup in large files)" t)))
  590. (defcustom org-use-sub-superscripts t
  591. "Non-nil means interpret \"_\" and \"^\" for display.
  592. When this option is turned on, you can use TeX-like syntax for sub- and
  593. superscripts. Several characters after \"_\" or \"^\" will be
  594. considered as a single item - so grouping with {} is normally not
  595. needed. For example, the following things will be parsed as single
  596. sub- or superscripts.
  597. 10^24 or 10^tau several digits will be considered 1 item.
  598. 10^-12 or 10^-tau a leading sign with digits or a word
  599. x^2-y^3 will be read as x^2 - y^3, because items are
  600. terminated by almost any nonword/nondigit char.
  601. x_{i^2} or x^(2-i) braces or parenthesis do grouping.
  602. Still, ambiguity is possible - so when in doubt use {} to enclose
  603. the sub/superscript. If you set this variable to the symbol
  604. `{}', the braces are *required* in order to trigger
  605. interpretations as sub/superscript. This can be helpful in
  606. documents that need \"_\" frequently in plain text."
  607. :group 'org-startup
  608. :version "24.1"
  609. :type '(choice
  610. (const :tag "Always interpret" t)
  611. (const :tag "Only with braces" {})
  612. (const :tag "Never interpret" nil)))
  613. (defcustom org-startup-with-beamer-mode nil
  614. "Non-nil means turn on `org-beamer-mode' on startup.
  615. This can also be configured on a per-file basis by adding one of
  616. the following lines anywhere in the buffer:
  617. #+STARTUP: beamer"
  618. :group 'org-startup
  619. :version "24.1"
  620. :type 'boolean)
  621. (defcustom org-startup-align-all-tables nil
  622. "Non-nil means align all tables when visiting a file.
  623. This is useful when the column width in tables is forced with <N> cookies
  624. in table fields. Such tables will look correct only after the first re-align.
  625. This can also be configured on a per-file basis by adding one of
  626. the following lines anywhere in the buffer:
  627. #+STARTUP: align
  628. #+STARTUP: noalign"
  629. :group 'org-startup
  630. :type 'boolean)
  631. (defcustom org-startup-with-inline-images nil
  632. "Non-nil means show inline images when loading a new Org file.
  633. This can also be configured on a per-file basis by adding one of
  634. the following lines anywhere in the buffer:
  635. #+STARTUP: inlineimages
  636. #+STARTUP: noinlineimages"
  637. :group 'org-startup
  638. :version "24.1"
  639. :type 'boolean)
  640. (defcustom org-startup-with-latex-preview nil
  641. "Non-nil means preview LaTeX fragments when loading a new Org file.
  642. This can also be configured on a per-file basis by adding one of
  643. the followinglines anywhere in the buffer:
  644. #+STARTUP: latexpreview
  645. #+STARTUP: nolatexpreview"
  646. :group 'org-startup
  647. :version "24.4"
  648. :package-version '(Org . "8.0")
  649. :type 'boolean)
  650. (defcustom org-insert-mode-line-in-empty-file nil
  651. "Non-nil means insert the first line setting Org-mode in empty files.
  652. When the function `org-mode' is called interactively in an empty file, this
  653. normally means that the file name does not automatically trigger Org-mode.
  654. To ensure that the file will always be in Org-mode in the future, a
  655. line enforcing Org-mode will be inserted into the buffer, if this option
  656. has been set."
  657. :group 'org-startup
  658. :type 'boolean)
  659. (defcustom org-replace-disputed-keys nil
  660. "Non-nil means use alternative key bindings for some keys.
  661. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  662. These keys are also used by other packages like shift-selection-mode'
  663. \(built into Emacs 23), `CUA-mode' or `windmove.el'.
  664. If you want to use Org-mode together with one of these other modes,
  665. or more generally if you would like to move some Org-mode commands to
  666. other keys, set this variable and configure the keys with the variable
  667. `org-disputed-keys'.
  668. This option is only relevant at load-time of Org-mode, and must be set
  669. *before* org.el is loaded. Changing it requires a restart of Emacs to
  670. become effective."
  671. :group 'org-startup
  672. :type 'boolean)
  673. (defcustom org-use-extra-keys nil
  674. "Non-nil means use extra key sequence definitions for certain commands.
  675. This happens automatically if you run XEmacs or if `window-system'
  676. is nil. This variable lets you do the same manually. You must
  677. set it before loading org.
  678. Example: on Carbon Emacs 22 running graphically, with an external
  679. keyboard on a Powerbook, the default way of setting M-left might
  680. not work for either Alt or ESC. Setting this variable will make
  681. it work for ESC."
  682. :group 'org-startup
  683. :type 'boolean)
  684. (org-defvaralias 'org-CUA-compatible 'org-replace-disputed-keys)
  685. (defcustom org-disputed-keys
  686. '(([(shift up)] . [(meta p)])
  687. ([(shift down)] . [(meta n)])
  688. ([(shift left)] . [(meta -)])
  689. ([(shift right)] . [(meta +)])
  690. ([(control shift right)] . [(meta shift +)])
  691. ([(control shift left)] . [(meta shift -)]))
  692. "Keys for which Org-mode and other modes compete.
  693. This is an alist, cars are the default keys, second element specifies
  694. the alternative to use when `org-replace-disputed-keys' is t.
  695. Keys can be specified in any syntax supported by `define-key'.
  696. The value of this option takes effect only at Org-mode's startup,
  697. therefore you'll have to restart Emacs to apply it after changing."
  698. :group 'org-startup
  699. :type 'alist)
  700. (defun org-key (key)
  701. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  702. Or return the original if not disputed.
  703. Also apply the translations defined in `org-xemacs-key-equivalents'."
  704. (when org-replace-disputed-keys
  705. (let* ((nkey (key-description key))
  706. (x (org-find-if (lambda (x)
  707. (equal (key-description (car x)) nkey))
  708. org-disputed-keys)))
  709. (setq key (if x (cdr x) key))))
  710. (when (featurep 'xemacs)
  711. (setq key (or (cdr (assoc key org-xemacs-key-equivalents)) key)))
  712. key)
  713. (defun org-find-if (predicate seq)
  714. (catch 'exit
  715. (while seq
  716. (if (funcall predicate (car seq))
  717. (throw 'exit (car seq))
  718. (pop seq)))))
  719. (defun org-defkey (keymap key def)
  720. "Define a key, possibly translated, as returned by `org-key'."
  721. (define-key keymap (org-key key) def))
  722. (defcustom org-ellipsis nil
  723. "The ellipsis to use in the Org-mode outline.
  724. When nil, just use the standard three dots. When a string, use that instead,
  725. When a face, use the standard 3 dots, but with the specified face.
  726. The change affects only Org-mode (which will then use its own display table).
  727. Changing this requires executing `M-x org-mode' in a buffer to become
  728. effective."
  729. :group 'org-startup
  730. :type '(choice (const :tag "Default" nil)
  731. (face :tag "Face" :value org-warning)
  732. (string :tag "String" :value "...#")))
  733. (defvar org-display-table nil
  734. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  735. (defgroup org-keywords nil
  736. "Keywords in Org-mode."
  737. :tag "Org Keywords"
  738. :group 'org)
  739. (defcustom org-deadline-string "DEADLINE:"
  740. "String to mark deadline entries.
  741. A deadline is this string, followed by a time stamp. Should be a word,
  742. terminated by a colon. You can insert a schedule keyword and
  743. a timestamp with \\[org-deadline].
  744. Changes become only effective after restarting Emacs."
  745. :group 'org-keywords
  746. :type 'string)
  747. (defcustom org-scheduled-string "SCHEDULED:"
  748. "String to mark scheduled TODO entries.
  749. A schedule is this string, followed by a time stamp. Should be a word,
  750. terminated by a colon. You can insert a schedule keyword and
  751. a timestamp with \\[org-schedule].
  752. Changes become only effective after restarting Emacs."
  753. :group 'org-keywords
  754. :type 'string)
  755. (defcustom org-closed-string "CLOSED:"
  756. "String used as the prefix for timestamps logging closing a TODO entry."
  757. :group 'org-keywords
  758. :type 'string)
  759. (defcustom org-clock-string "CLOCK:"
  760. "String used as prefix for timestamps clocking work hours on an item."
  761. :group 'org-keywords
  762. :type 'string)
  763. (defcustom org-closed-keep-when-no-todo nil
  764. "Remove CLOSED: time-stamp when switching back to a non-todo state?"
  765. :group 'org-todo
  766. :group 'org-keywords
  767. :version "24.4"
  768. :package-version '(Org . "8.0")
  769. :type 'boolean)
  770. (defconst org-planning-or-clock-line-re (concat "^[ \t]*\\("
  771. org-scheduled-string "\\|"
  772. org-deadline-string "\\|"
  773. org-closed-string "\\|"
  774. org-clock-string "\\)")
  775. "Matches a line with planning or clock info.")
  776. (defcustom org-comment-string "COMMENT"
  777. "Entries starting with this keyword will never be exported.
  778. An entry can be toggled between COMMENT and normal with
  779. \\[org-toggle-comment].
  780. Changes become only effective after restarting Emacs."
  781. :group 'org-keywords
  782. :type 'string)
  783. (defcustom org-quote-string "QUOTE"
  784. "Entries starting with this keyword will be exported in fixed-width font.
  785. Quoting applies only to the text in the entry following the headline, and does
  786. not extend beyond the next headline, even if that is lower level.
  787. An entry can be toggled between QUOTE and normal with
  788. \\[org-toggle-fixed-width-section]."
  789. :group 'org-keywords
  790. :type 'string)
  791. (defconst org-repeat-re
  792. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*?\\([.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)"
  793. "Regular expression for specifying repeated events.
  794. After a match, group 1 contains the repeat expression.")
  795. (defgroup org-structure nil
  796. "Options concerning the general structure of Org-mode files."
  797. :tag "Org Structure"
  798. :group 'org)
  799. (defgroup org-reveal-location nil
  800. "Options about how to make context of a location visible."
  801. :tag "Org Reveal Location"
  802. :group 'org-structure)
  803. (defconst org-context-choice
  804. '(choice
  805. (const :tag "Always" t)
  806. (const :tag "Never" nil)
  807. (repeat :greedy t :tag "Individual contexts"
  808. (cons
  809. (choice :tag "Context"
  810. (const agenda)
  811. (const org-goto)
  812. (const occur-tree)
  813. (const tags-tree)
  814. (const link-search)
  815. (const mark-goto)
  816. (const bookmark-jump)
  817. (const isearch)
  818. (const default))
  819. (boolean))))
  820. "Contexts for the reveal options.")
  821. (defcustom org-show-hierarchy-above '((default . t))
  822. "Non-nil means show full hierarchy when revealing a location.
  823. Org-mode often shows locations in an org-mode file which might have
  824. been invisible before. When this is set, the hierarchy of headings
  825. above the exposed location is shown.
  826. Turning this off for example for sparse trees makes them very compact.
  827. Instead of t, this can also be an alist specifying this option for different
  828. contexts. Valid contexts are
  829. agenda when exposing an entry from the agenda
  830. org-goto when using the command `org-goto' on key C-c C-j
  831. occur-tree when using the command `org-occur' on key C-c /
  832. tags-tree when constructing a sparse tree based on tags matches
  833. link-search when exposing search matches associated with a link
  834. mark-goto when exposing the jump goal of a mark
  835. bookmark-jump when exposing a bookmark location
  836. isearch when exiting from an incremental search
  837. default default for all contexts not set explicitly"
  838. :group 'org-reveal-location
  839. :type org-context-choice)
  840. (defcustom org-show-following-heading '((default . nil))
  841. "Non-nil means show following heading when revealing a location.
  842. Org-mode often shows locations in an org-mode file which might have
  843. been invisible before. When this is set, the heading following the
  844. match is shown.
  845. Turning this off for example for sparse trees makes them very compact,
  846. but makes it harder to edit the location of the match. In such a case,
  847. use the command \\[org-reveal] to show more context.
  848. Instead of t, this can also be an alist specifying this option for different
  849. contexts. See `org-show-hierarchy-above' for valid contexts."
  850. :group 'org-reveal-location
  851. :type org-context-choice)
  852. (defcustom org-show-siblings '((default . nil) (isearch t) (bookmark-jump t))
  853. "Non-nil means show all sibling heading when revealing a location.
  854. Org-mode often shows locations in an org-mode file which might have
  855. been invisible before. When this is set, the sibling of the current entry
  856. heading are all made visible. If `org-show-hierarchy-above' is t,
  857. the same happens on each level of the hierarchy above the current entry.
  858. By default this is on for the isearch context, off for all other contexts.
  859. Turning this off for example for sparse trees makes them very compact,
  860. but makes it harder to edit the location of the match. In such a case,
  861. use the command \\[org-reveal] to show more context.
  862. Instead of t, this can also be an alist specifying this option for different
  863. contexts. See `org-show-hierarchy-above' for valid contexts."
  864. :group 'org-reveal-location
  865. :type org-context-choice
  866. :version "24.4"
  867. :package-version '(Org . "8.0"))
  868. (defcustom org-show-entry-below '((default . nil))
  869. "Non-nil means show the entry below a headline when revealing a location.
  870. Org-mode often shows locations in an org-mode file which might have
  871. been invisible before. When this is set, the text below the headline that is
  872. exposed is also shown.
  873. By default this is off for all contexts.
  874. Instead of t, this can also be an alist specifying this option for different
  875. contexts. See `org-show-hierarchy-above' for valid contexts."
  876. :group 'org-reveal-location
  877. :type org-context-choice)
  878. (defcustom org-indirect-buffer-display 'other-window
  879. "How should indirect tree buffers be displayed?
  880. This applies to indirect buffers created with the commands
  881. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  882. Valid values are:
  883. current-window Display in the current window
  884. other-window Just display in another window.
  885. dedicated-frame Create one new frame, and re-use it each time.
  886. new-frame Make a new frame each time. Note that in this case
  887. previously-made indirect buffers are kept, and you need to
  888. kill these buffers yourself."
  889. :group 'org-structure
  890. :group 'org-agenda-windows
  891. :type '(choice
  892. (const :tag "In current window" current-window)
  893. (const :tag "In current frame, other window" other-window)
  894. (const :tag "Each time a new frame" new-frame)
  895. (const :tag "One dedicated frame" dedicated-frame)))
  896. (defcustom org-use-speed-commands nil
  897. "Non-nil means activate single letter commands at beginning of a headline.
  898. This may also be a function to test for appropriate locations where speed
  899. commands should be active."
  900. :group 'org-structure
  901. :type '(choice
  902. (const :tag "Never" nil)
  903. (const :tag "At beginning of headline stars" t)
  904. (function)))
  905. (defcustom org-speed-commands-user nil
  906. "Alist of additional speed commands.
  907. This list will be checked before `org-speed-commands-default'
  908. when the variable `org-use-speed-commands' is non-nil
  909. and when the cursor is at the beginning of a headline.
  910. The car if each entry is a string with a single letter, which must
  911. be assigned to `self-insert-command' in the global map.
  912. The cdr is either a command to be called interactively, a function
  913. to be called, or a form to be evaluated.
  914. An entry that is just a list with a single string will be interpreted
  915. as a descriptive headline that will be added when listing the speed
  916. commands in the Help buffer using the `?' speed command."
  917. :group 'org-structure
  918. :type '(repeat :value ("k" . ignore)
  919. (choice :value ("k" . ignore)
  920. (list :tag "Descriptive Headline" (string :tag "Headline"))
  921. (cons :tag "Letter and Command"
  922. (string :tag "Command letter")
  923. (choice
  924. (function)
  925. (sexp))))))
  926. (defgroup org-cycle nil
  927. "Options concerning visibility cycling in Org-mode."
  928. :tag "Org Cycle"
  929. :group 'org-structure)
  930. (defcustom org-cycle-skip-children-state-if-no-children t
  931. "Non-nil means skip CHILDREN state in entries that don't have any."
  932. :group 'org-cycle
  933. :type 'boolean)
  934. (defcustom org-cycle-max-level nil
  935. "Maximum level which should still be subject to visibility cycling.
  936. Levels higher than this will, for cycling, be treated as text, not a headline.
  937. When `org-odd-levels-only' is set, a value of N in this variable actually
  938. means 2N-1 stars as the limiting headline.
  939. When nil, cycle all levels.
  940. Note that the limiting level of cycling is also influenced by
  941. `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
  942. `org-inlinetask-min-level' is, cycling will be limited to levels one less
  943. than its value."
  944. :group 'org-cycle
  945. :type '(choice
  946. (const :tag "No limit" nil)
  947. (integer :tag "Maximum level")))
  948. (defcustom org-drawers '("PROPERTIES" "CLOCK" "LOGBOOK" "RESULTS")
  949. "Names of drawers. Drawers are not opened by cycling on the headline above.
  950. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  951. this:
  952. :DRAWERNAME:
  953. .....
  954. :END:
  955. The drawer \"PROPERTIES\" is special for capturing properties through
  956. the property API.
  957. Drawers can be defined on the per-file basis with a line like:
  958. #+DRAWERS: HIDDEN STATE PROPERTIES"
  959. :group 'org-structure
  960. :group 'org-cycle
  961. :type '(repeat (string :tag "Drawer Name")))
  962. (defcustom org-hide-block-startup nil
  963. "Non-nil means entering Org-mode will fold all blocks.
  964. This can also be set in on a per-file basis with
  965. #+STARTUP: hideblocks
  966. #+STARTUP: showblocks"
  967. :group 'org-startup
  968. :group 'org-cycle
  969. :type 'boolean)
  970. (defcustom org-cycle-global-at-bob nil
  971. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  972. This makes it possible to do global cycling without having to use S-TAB or
  973. \\[universal-argument] TAB. For this special case to work, the first line
  974. of the buffer must not be a headline -- it may be empty or some other text.
  975. When used in this way, `org-cycle-hook' is disabled temporarily to make
  976. sure the cursor stays at the beginning of the buffer. When this option is
  977. nil, don't do anything special at the beginning of the buffer."
  978. :group 'org-cycle
  979. :type 'boolean)
  980. (defcustom org-cycle-level-after-item/entry-creation t
  981. "Non-nil means cycle entry level or item indentation in new empty entries.
  982. When the cursor is at the end of an empty headline, i.e., with only stars
  983. and maybe a TODO keyword, TAB will then switch the entry to become a child,
  984. and then all possible ancestor states, before returning to the original state.
  985. This makes data entry extremely fast: M-RET to create a new headline,
  986. on TAB to make it a child, two or more tabs to make it a (grand-)uncle.
  987. When the cursor is at the end of an empty plain list item, one TAB will
  988. make it a subitem, two or more tabs will back up to make this an item
  989. higher up in the item hierarchy."
  990. :group 'org-cycle
  991. :type 'boolean)
  992. (defcustom org-cycle-emulate-tab t
  993. "Where should `org-cycle' emulate TAB.
  994. nil Never
  995. white Only in completely white lines
  996. whitestart Only at the beginning of lines, before the first non-white char
  997. t Everywhere except in headlines
  998. exc-hl-bol Everywhere except at the start of a headline
  999. If TAB is used in a place where it does not emulate TAB, the current subtree
  1000. visibility is cycled."
  1001. :group 'org-cycle
  1002. :type '(choice (const :tag "Never" nil)
  1003. (const :tag "Only in completely white lines" white)
  1004. (const :tag "Before first char in a line" whitestart)
  1005. (const :tag "Everywhere except in headlines" t)
  1006. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)))
  1007. (defcustom org-cycle-separator-lines 2
  1008. "Number of empty lines needed to keep an empty line between collapsed trees.
  1009. If you leave an empty line between the end of a subtree and the following
  1010. headline, this empty line is hidden when the subtree is folded.
  1011. Org-mode will leave (exactly) one empty line visible if the number of
  1012. empty lines is equal or larger to the number given in this variable.
  1013. So the default 2 means at least 2 empty lines after the end of a subtree
  1014. are needed to produce free space between a collapsed subtree and the
  1015. following headline.
  1016. If the number is negative, and the number of empty lines is at least -N,
  1017. all empty lines are shown.
  1018. Special case: when 0, never leave empty lines in collapsed view."
  1019. :group 'org-cycle
  1020. :type 'integer)
  1021. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  1022. (defcustom org-pre-cycle-hook nil
  1023. "Hook that is run before visibility cycling is happening.
  1024. The function(s) in this hook must accept a single argument which indicates
  1025. the new state that will be set right after running this hook. The
  1026. argument is a symbol. Before a global state change, it can have the values
  1027. `overview', `content', or `all'. Before a local state change, it can have
  1028. the values `folded', `children', or `subtree'."
  1029. :group 'org-cycle
  1030. :type 'hook)
  1031. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  1032. org-cycle-hide-drawers
  1033. org-cycle-hide-inline-tasks
  1034. org-cycle-show-empty-lines
  1035. org-optimize-window-after-visibility-change)
  1036. "Hook that is run after `org-cycle' has changed the buffer visibility.
  1037. The function(s) in this hook must accept a single argument which indicates
  1038. the new state that was set by the most recent `org-cycle' command. The
  1039. argument is a symbol. After a global state change, it can have the values
  1040. `overview', `contents', or `all'. After a local state change, it can have
  1041. the values `folded', `children', or `subtree'."
  1042. :group 'org-cycle
  1043. :type 'hook)
  1044. (defgroup org-edit-structure nil
  1045. "Options concerning structure editing in Org-mode."
  1046. :tag "Org Edit Structure"
  1047. :group 'org-structure)
  1048. (defcustom org-odd-levels-only nil
  1049. "Non-nil means skip even levels and only use odd levels for the outline.
  1050. This has the effect that two stars are being added/taken away in
  1051. promotion/demotion commands. It also influences how levels are
  1052. handled by the exporters.
  1053. Changing it requires restart of `font-lock-mode' to become effective
  1054. for fontification also in regions already fontified.
  1055. You may also set this on a per-file basis by adding one of the following
  1056. lines to the buffer:
  1057. #+STARTUP: odd
  1058. #+STARTUP: oddeven"
  1059. :group 'org-edit-structure
  1060. :group 'org-appearance
  1061. :type 'boolean)
  1062. (defcustom org-adapt-indentation t
  1063. "Non-nil means adapt indentation to outline node level.
  1064. When this variable is set, Org assumes that you write outlines by
  1065. indenting text in each node to align with the headline (after the stars).
  1066. The following issues are influenced by this variable:
  1067. - When this is set and the *entire* text in an entry is indented, the
  1068. indentation is increased by one space in a demotion command, and
  1069. decreased by one in a promotion command. If any line in the entry
  1070. body starts with text at column 0, indentation is not changed at all.
  1071. - Property drawers and planning information is inserted indented when
  1072. this variable s set. When nil, they will not be indented.
  1073. - TAB indents a line relative to context. The lines below a headline
  1074. will be indented when this variable is set.
  1075. Note that this is all about true indentation, by adding and removing
  1076. space characters. See also `org-indent.el' which does level-dependent
  1077. indentation in a virtual way, i.e. at display time in Emacs."
  1078. :group 'org-edit-structure
  1079. :type 'boolean)
  1080. (defcustom org-special-ctrl-a/e nil
  1081. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  1082. When t, `C-a' will bring back the cursor to the beginning of the
  1083. headline text, i.e. after the stars and after a possible TODO
  1084. keyword. In an item, this will be the position after bullet and
  1085. check-box, if any. When the cursor is already at that position,
  1086. another `C-a' will bring it to the beginning of the line.
  1087. `C-e' will jump to the end of the headline, ignoring the presence
  1088. of tags in the headline. A second `C-e' will then jump to the
  1089. true end of the line, after any tags. This also means that, when
  1090. this variable is non-nil, `C-e' also will never jump beyond the
  1091. end of the heading of a folded section, i.e. not after the
  1092. ellipses.
  1093. When set to the symbol `reversed', the first `C-a' or `C-e' works
  1094. normally, going to the true line boundary first. Only a directly
  1095. following, identical keypress will bring the cursor to the
  1096. special positions.
  1097. This may also be a cons cell where the behavior for `C-a' and
  1098. `C-e' is set separately."
  1099. :group 'org-edit-structure
  1100. :type '(choice
  1101. (const :tag "off" nil)
  1102. (const :tag "on: after stars/bullet and before tags first" t)
  1103. (const :tag "reversed: true line boundary first" reversed)
  1104. (cons :tag "Set C-a and C-e separately"
  1105. (choice :tag "Special C-a"
  1106. (const :tag "off" nil)
  1107. (const :tag "on: after stars/bullet first" t)
  1108. (const :tag "reversed: before stars/bullet first" reversed))
  1109. (choice :tag "Special C-e"
  1110. (const :tag "off" nil)
  1111. (const :tag "on: before tags first" t)
  1112. (const :tag "reversed: after tags first" reversed)))))
  1113. (org-defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e)
  1114. (defcustom org-special-ctrl-k nil
  1115. "Non-nil means `C-k' will behave specially in headlines.
  1116. When nil, `C-k' will call the default `kill-line' command.
  1117. When t, the following will happen while the cursor is in the headline:
  1118. - When the cursor is at the beginning of a headline, kill the entire
  1119. line and possible the folded subtree below the line.
  1120. - When in the middle of the headline text, kill the headline up to the tags.
  1121. - When after the headline text, kill the tags."
  1122. :group 'org-edit-structure
  1123. :type 'boolean)
  1124. (defcustom org-ctrl-k-protect-subtree nil
  1125. "Non-nil means, do not delete a hidden subtree with C-k.
  1126. When set to the symbol `error', simply throw an error when C-k is
  1127. used to kill (part-of) a headline that has hidden text behind it.
  1128. Any other non-nil value will result in a query to the user, if it is
  1129. OK to kill that hidden subtree. When nil, kill without remorse."
  1130. :group 'org-edit-structure
  1131. :version "24.1"
  1132. :type '(choice
  1133. (const :tag "Do not protect hidden subtrees" nil)
  1134. (const :tag "Protect hidden subtrees with a security query" t)
  1135. (const :tag "Never kill a hidden subtree with C-k" error)))
  1136. (defcustom org-catch-invisible-edits nil
  1137. "Check if in invisible region before inserting or deleting a character.
  1138. Valid values are:
  1139. nil Do not check, so just do invisible edits.
  1140. error Throw an error and do nothing.
  1141. show Make point visible, and do the requested edit.
  1142. show-and-error Make point visible, then throw an error and abort the edit.
  1143. smart Make point visible, and do insertion/deletion if it is
  1144. adjacent to visible text and the change feels predictable.
  1145. Never delete a previously invisible character or add in the
  1146. middle or right after an invisible region. Basically, this
  1147. allows insertion and backward-delete right before ellipses.
  1148. FIXME: maybe in this case we should not even show?"
  1149. :group 'org-edit-structure
  1150. :version "24.1"
  1151. :type '(choice
  1152. (const :tag "Do not check" nil)
  1153. (const :tag "Throw error when trying to edit" error)
  1154. (const :tag "Unhide, but do not do the edit" show-and-error)
  1155. (const :tag "Show invisible part and do the edit" show)
  1156. (const :tag "Be smart and do the right thing" smart)))
  1157. (defcustom org-yank-folded-subtrees t
  1158. "Non-nil means when yanking subtrees, fold them.
  1159. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  1160. it starts with a heading and all other headings in it are either children
  1161. or siblings, then fold all the subtrees. However, do this only if no
  1162. text after the yank would be swallowed into a folded tree by this action."
  1163. :group 'org-edit-structure
  1164. :type 'boolean)
  1165. (defcustom org-yank-adjusted-subtrees nil
  1166. "Non-nil means when yanking subtrees, adjust the level.
  1167. With this setting, `org-paste-subtree' is used to insert the subtree, see
  1168. this function for details."
  1169. :group 'org-edit-structure
  1170. :type 'boolean)
  1171. (defcustom org-M-RET-may-split-line '((default . t))
  1172. "Non-nil means M-RET will split the line at the cursor position.
  1173. When nil, it will go to the end of the line before making a
  1174. new line.
  1175. You may also set this option in a different way for different
  1176. contexts. Valid contexts are:
  1177. headline when creating a new headline
  1178. item when creating a new item
  1179. table in a table field
  1180. default the value to be used for all contexts not explicitly
  1181. customized"
  1182. :group 'org-structure
  1183. :group 'org-table
  1184. :type '(choice
  1185. (const :tag "Always" t)
  1186. (const :tag "Never" nil)
  1187. (repeat :greedy t :tag "Individual contexts"
  1188. (cons
  1189. (choice :tag "Context"
  1190. (const headline)
  1191. (const item)
  1192. (const table)
  1193. (const default))
  1194. (boolean)))))
  1195. (defcustom org-insert-heading-respect-content nil
  1196. "Non-nil means insert new headings after the current subtree.
  1197. When nil, the new heading is created directly after the current line.
  1198. The commands \\[org-insert-heading-respect-content] and \\[org-insert-todo-heading-respect-content] turn
  1199. this variable on for the duration of the command."
  1200. :group 'org-structure
  1201. :type 'boolean)
  1202. (defcustom org-blank-before-new-entry '((heading . auto)
  1203. (plain-list-item . auto))
  1204. "Should `org-insert-heading' leave a blank line before new heading/item?
  1205. The value is an alist, with `heading' and `plain-list-item' as CAR,
  1206. and a boolean flag as CDR. The cdr may also be the symbol `auto', in
  1207. which case Org will look at the surrounding headings/items and try to
  1208. make an intelligent decision whether to insert a blank line or not.
  1209. For plain lists, if `org-list-empty-line-terminates-plain-lists' is set,
  1210. the setting here is ignored and no empty line is inserted to avoid breaking
  1211. the list structure."
  1212. :group 'org-edit-structure
  1213. :type '(list
  1214. (cons (const heading)
  1215. (choice (const :tag "Never" nil)
  1216. (const :tag "Always" t)
  1217. (const :tag "Auto" auto)))
  1218. (cons (const plain-list-item)
  1219. (choice (const :tag "Never" nil)
  1220. (const :tag "Always" t)
  1221. (const :tag "Auto" auto)))))
  1222. (defcustom org-insert-heading-hook nil
  1223. "Hook being run after inserting a new heading."
  1224. :group 'org-edit-structure
  1225. :type 'hook)
  1226. (defcustom org-enable-fixed-width-editor t
  1227. "Non-nil means lines starting with \":\" are treated as fixed-width.
  1228. This currently only means they are never auto-wrapped.
  1229. When nil, such lines will be treated like ordinary lines.
  1230. See also the QUOTE keyword."
  1231. :group 'org-edit-structure
  1232. :type 'boolean)
  1233. (defcustom org-goto-auto-isearch t
  1234. "Non-nil means typing characters in `org-goto' starts incremental search.
  1235. When nil, you can use these keybindings to navigate the buffer:
  1236. q Quit the org-goto interface
  1237. n Go to the next visible heading
  1238. p Go to the previous visible heading
  1239. f Go one heading forward on same level
  1240. b Go one heading backward on same level
  1241. u Go one heading up"
  1242. :group 'org-edit-structure
  1243. :type 'boolean)
  1244. (defgroup org-sparse-trees nil
  1245. "Options concerning sparse trees in Org-mode."
  1246. :tag "Org Sparse Trees"
  1247. :group 'org-structure)
  1248. (defcustom org-highlight-sparse-tree-matches t
  1249. "Non-nil means highlight all matches that define a sparse tree.
  1250. The highlights will automatically disappear the next time the buffer is
  1251. changed by an edit command."
  1252. :group 'org-sparse-trees
  1253. :type 'boolean)
  1254. (defcustom org-remove-highlights-with-change t
  1255. "Non-nil means any change to the buffer will remove temporary highlights.
  1256. Such highlights are created by `org-occur' and `org-clock-display'.
  1257. When nil, `C-c C-c needs to be used to get rid of the highlights.
  1258. The highlights created by `org-preview-latex-fragment' always need
  1259. `C-c C-c' to be removed."
  1260. :group 'org-sparse-trees
  1261. :group 'org-time
  1262. :type 'boolean)
  1263. (defcustom org-occur-hook '(org-first-headline-recenter)
  1264. "Hook that is run after `org-occur' has constructed a sparse tree.
  1265. This can be used to recenter the window to show as much of the structure
  1266. as possible."
  1267. :group 'org-sparse-trees
  1268. :type 'hook)
  1269. (defgroup org-imenu-and-speedbar nil
  1270. "Options concerning imenu and speedbar in Org-mode."
  1271. :tag "Org Imenu and Speedbar"
  1272. :group 'org-structure)
  1273. (defcustom org-imenu-depth 2
  1274. "The maximum level for Imenu access to Org-mode headlines.
  1275. This also applied for speedbar access."
  1276. :group 'org-imenu-and-speedbar
  1277. :type 'integer)
  1278. (defgroup org-table nil
  1279. "Options concerning tables in Org-mode."
  1280. :tag "Org Table"
  1281. :group 'org)
  1282. (defcustom org-enable-table-editor 'optimized
  1283. "Non-nil means lines starting with \"|\" are handled by the table editor.
  1284. When nil, such lines will be treated like ordinary lines.
  1285. When equal to the symbol `optimized', the table editor will be optimized to
  1286. do the following:
  1287. - Automatic overwrite mode in front of whitespace in table fields.
  1288. This makes the structure of the table stay in tact as long as the edited
  1289. field does not exceed the column width.
  1290. - Minimize the number of realigns. Normally, the table is aligned each time
  1291. TAB or RET are pressed to move to another field. With optimization this
  1292. happens only if changes to a field might have changed the column width.
  1293. Optimization requires replacing the functions `self-insert-command',
  1294. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  1295. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  1296. very good at guessing when a re-align will be necessary, but you can always
  1297. force one with \\[org-ctrl-c-ctrl-c].
  1298. If you would like to use the optimized version in Org-mode, but the
  1299. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  1300. This variable can be used to turn on and off the table editor during a session,
  1301. but in order to toggle optimization, a restart is required.
  1302. See also the variable `org-table-auto-blank-field'."
  1303. :group 'org-table
  1304. :type '(choice
  1305. (const :tag "off" nil)
  1306. (const :tag "on" t)
  1307. (const :tag "on, optimized" optimized)))
  1308. (defcustom org-self-insert-cluster-for-undo (or (featurep 'xemacs)
  1309. (version<= emacs-version "24.1"))
  1310. "Non-nil means cluster self-insert commands for undo when possible.
  1311. If this is set, then, like in the Emacs command loop, 20 consecutive
  1312. characters will be undone together.
  1313. This is configurable, because there is some impact on typing performance."
  1314. :group 'org-table
  1315. :type 'boolean)
  1316. (defcustom org-table-tab-recognizes-table.el t
  1317. "Non-nil means TAB will automatically notice a table.el table.
  1318. When it sees such a table, it moves point into it and - if necessary -
  1319. calls `table-recognize-table'."
  1320. :group 'org-table-editing
  1321. :type 'boolean)
  1322. (defgroup org-link nil
  1323. "Options concerning links in Org-mode."
  1324. :tag "Org Link"
  1325. :group 'org)
  1326. (defvar org-link-abbrev-alist-local nil
  1327. "Buffer-local version of `org-link-abbrev-alist', which see.
  1328. The value of this is taken from the #+LINK lines.")
  1329. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  1330. (defcustom org-link-abbrev-alist nil
  1331. "Alist of link abbreviations.
  1332. The car of each element is a string, to be replaced at the start of a link.
  1333. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  1334. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  1335. [[linkkey:tag][description]]
  1336. The 'linkkey' must be a word word, starting with a letter, followed
  1337. by letters, numbers, '-' or '_'.
  1338. If REPLACE is a string, the tag will simply be appended to create the link.
  1339. If the string contains \"%s\", the tag will be inserted there. If the string
  1340. contains \"%h\", it will cause a url-encoded version of the tag to be inserted
  1341. at that point (see the function `url-hexify-string'). If the string contains
  1342. the specifier \"%(my-function)\", then the custom function `my-function' will
  1343. be invoked: this function takes the tag as its only argument and must return
  1344. a string.
  1345. REPLACE may also be a function that will be called with the tag as the
  1346. only argument to create the link, which should be returned as a string.
  1347. See the manual for examples."
  1348. :group 'org-link
  1349. :type '(repeat
  1350. (cons
  1351. (string :tag "Protocol")
  1352. (choice
  1353. (string :tag "Format")
  1354. (function)))))
  1355. (defcustom org-descriptive-links t
  1356. "Non-nil means Org will display descriptive links.
  1357. E.g. [[http://orgmode.org][Org website]] will be displayed as
  1358. \"Org Website\", hiding the link itself and just displaying its
  1359. description. When set to `nil', Org will display the full links
  1360. literally.
  1361. You can interactively set the value of this variable by calling
  1362. `org-toggle-link-display' or from the menu Org>Hyperlinks menu."
  1363. :group 'org-link
  1364. :type 'boolean)
  1365. (defcustom org-link-file-path-type 'adaptive
  1366. "How the path name in file links should be stored.
  1367. Valid values are:
  1368. relative Relative to the current directory, i.e. the directory of the file
  1369. into which the link is being inserted.
  1370. absolute Absolute path, if possible with ~ for home directory.
  1371. noabbrev Absolute path, no abbreviation of home directory.
  1372. adaptive Use relative path for files in the current directory and sub-
  1373. directories of it. For other files, use an absolute path."
  1374. :group 'org-link
  1375. :type '(choice
  1376. (const relative)
  1377. (const absolute)
  1378. (const noabbrev)
  1379. (const adaptive)))
  1380. (defcustom org-activate-links '(bracket angle plain radio tag date footnote)
  1381. "Types of links that should be activated in Org-mode files.
  1382. This is a list of symbols, each leading to the activation of a certain link
  1383. type. In principle, it does not hurt to turn on most link types - there may
  1384. be a small gain when turning off unused link types. The types are:
  1385. bracket The recommended [[link][description]] or [[link]] links with hiding.
  1386. angle Links in angular brackets that may contain whitespace like
  1387. <bbdb:Carsten Dominik>.
  1388. plain Plain links in normal text, no whitespace, like http://google.com.
  1389. radio Text that is matched by a radio target, see manual for details.
  1390. tag Tag settings in a headline (link to tag search).
  1391. date Time stamps (link to calendar).
  1392. footnote Footnote labels.
  1393. Changing this variable requires a restart of Emacs to become effective."
  1394. :group 'org-link
  1395. :type '(set :greedy t
  1396. (const :tag "Double bracket links" bracket)
  1397. (const :tag "Angular bracket links" angle)
  1398. (const :tag "Plain text links" plain)
  1399. (const :tag "Radio target matches" radio)
  1400. (const :tag "Tags" tag)
  1401. (const :tag "Timestamps" date)
  1402. (const :tag "Footnotes" footnote)))
  1403. (defcustom org-make-link-description-function nil
  1404. "Function to use for generating link descriptions from links.
  1405. When nil, the link location will be used. This function must take
  1406. two parameters: the first one is the link, the second one is the
  1407. description generated by `org-insert-link'. The function should
  1408. return the description to use."
  1409. :group 'org-link
  1410. :type '(choice (const nil) (function)))
  1411. (defgroup org-link-store nil
  1412. "Options concerning storing links in Org-mode."
  1413. :tag "Org Store Link"
  1414. :group 'org-link)
  1415. (defcustom org-url-hexify-p t
  1416. "When non-nil, hexify URL when creating a link."
  1417. :type 'boolean
  1418. :version "24.3"
  1419. :group 'org-link-store)
  1420. (defcustom org-email-link-description-format "Email %c: %.30s"
  1421. "Format of the description part of a link to an email or usenet message.
  1422. The following %-escapes will be replaced by corresponding information:
  1423. %F full \"From\" field
  1424. %f name, taken from \"From\" field, address if no name
  1425. %T full \"To\" field
  1426. %t first name in \"To\" field, address if no name
  1427. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  1428. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  1429. %s subject
  1430. %d date
  1431. %m message-id.
  1432. You may use normal field width specification between the % and the letter.
  1433. This is for example useful to limit the length of the subject.
  1434. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  1435. :group 'org-link-store
  1436. :type 'string)
  1437. (defcustom org-from-is-user-regexp
  1438. (let (r1 r2)
  1439. (when (and user-mail-address (not (string= user-mail-address "")))
  1440. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  1441. (when (and user-full-name (not (string= user-full-name "")))
  1442. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  1443. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  1444. "Regexp matched against the \"From:\" header of an email or usenet message.
  1445. It should match if the message is from the user him/herself."
  1446. :group 'org-link-store
  1447. :type 'regexp)
  1448. (defcustom org-context-in-file-links t
  1449. "Non-nil means file links from `org-store-link' contain context.
  1450. A search string will be added to the file name with :: as separator and
  1451. used to find the context when the link is activated by the command
  1452. `org-open-at-point'. When this option is t, the entire active region
  1453. will be placed in the search string of the file link. If set to a
  1454. positive integer, only the first n lines of context will be stored.
  1455. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1456. negates this setting for the duration of the command."
  1457. :group 'org-link-store
  1458. :type '(choice boolean integer))
  1459. (defcustom org-keep-stored-link-after-insertion nil
  1460. "Non-nil means keep link in list for entire session.
  1461. The command `org-store-link' adds a link pointing to the current
  1462. location to an internal list. These links accumulate during a session.
  1463. The command `org-insert-link' can be used to insert links into any
  1464. Org-mode file (offering completion for all stored links). When this
  1465. option is nil, every link which has been inserted once using \\[org-insert-link]
  1466. will be removed from the list, to make completing the unused links
  1467. more efficient."
  1468. :group 'org-link-store
  1469. :type 'boolean)
  1470. (defgroup org-link-follow nil
  1471. "Options concerning following links in Org-mode."
  1472. :tag "Org Follow Link"
  1473. :group 'org-link)
  1474. (defcustom org-link-translation-function nil
  1475. "Function to translate links with different syntax to Org syntax.
  1476. This can be used to translate links created for example by the Planner
  1477. or emacs-wiki packages to Org syntax.
  1478. The function must accept two parameters, a TYPE containing the link
  1479. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  1480. which is everything after the link protocol. It should return a cons
  1481. with possibly modified values of type and path.
  1482. Org contains a function for this, so if you set this variable to
  1483. `org-translate-link-from-planner', you should be able follow many
  1484. links created by planner."
  1485. :group 'org-link-follow
  1486. :type '(choice (const nil) (function)))
  1487. (defcustom org-follow-link-hook nil
  1488. "Hook that is run after a link has been followed."
  1489. :group 'org-link-follow
  1490. :type 'hook)
  1491. (defcustom org-tab-follows-link nil
  1492. "Non-nil means on links TAB will follow the link.
  1493. Needs to be set before org.el is loaded.
  1494. This really should not be used, it does not make sense, and the
  1495. implementation is bad."
  1496. :group 'org-link-follow
  1497. :type 'boolean)
  1498. (defcustom org-return-follows-link nil
  1499. "Non-nil means on links RET will follow the link.
  1500. In tables, the special behavior of RET has precedence."
  1501. :group 'org-link-follow
  1502. :type 'boolean)
  1503. (defcustom org-mouse-1-follows-link
  1504. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1505. "Non-nil means mouse-1 on a link will follow the link.
  1506. A longer mouse click will still set point. Does not work on XEmacs.
  1507. Needs to be set before org.el is loaded."
  1508. :group 'org-link-follow
  1509. :type 'boolean)
  1510. (defcustom org-mark-ring-length 4
  1511. "Number of different positions to be recorded in the ring.
  1512. Changing this requires a restart of Emacs to work correctly."
  1513. :group 'org-link-follow
  1514. :type 'integer)
  1515. (defcustom org-link-search-must-match-exact-headline 'query-to-create
  1516. "Non-nil means internal links in Org files must exactly match a headline.
  1517. When nil, the link search tries to match a phrase with all words
  1518. in the search text."
  1519. :group 'org-link-follow
  1520. :version "24.1"
  1521. :type '(choice
  1522. (const :tag "Use fuzzy text search" nil)
  1523. (const :tag "Match only exact headline" t)
  1524. (const :tag "Match exact headline or query to create it"
  1525. query-to-create)))
  1526. (defcustom org-link-frame-setup
  1527. '((vm . vm-visit-folder-other-frame)
  1528. (vm-imap . vm-visit-imap-folder-other-frame)
  1529. (gnus . org-gnus-no-new-news)
  1530. (file . find-file-other-window)
  1531. (wl . wl-other-frame))
  1532. "Setup the frame configuration for following links.
  1533. When following a link with Emacs, it may often be useful to display
  1534. this link in another window or frame. This variable can be used to
  1535. set this up for the different types of links.
  1536. For VM, use any of
  1537. `vm-visit-folder'
  1538. `vm-visit-folder-other-window'
  1539. `vm-visit-folder-other-frame'
  1540. For Gnus, use any of
  1541. `gnus'
  1542. `gnus-other-frame'
  1543. `org-gnus-no-new-news'
  1544. For FILE, use any of
  1545. `find-file'
  1546. `find-file-other-window'
  1547. `find-file-other-frame'
  1548. For Wanderlust use any of
  1549. `wl'
  1550. `wl-other-frame'
  1551. For the calendar, use the variable `calendar-setup'.
  1552. For BBDB, it is currently only possible to display the matches in
  1553. another window."
  1554. :group 'org-link-follow
  1555. :type '(list
  1556. (cons (const vm)
  1557. (choice
  1558. (const vm-visit-folder)
  1559. (const vm-visit-folder-other-window)
  1560. (const vm-visit-folder-other-frame)))
  1561. (cons (const vm-imap)
  1562. (choice
  1563. (const vm-visit-imap-folder)
  1564. (const vm-visit-imap-folder-other-window)
  1565. (const vm-visit-imap-folder-other-frame)))
  1566. (cons (const gnus)
  1567. (choice
  1568. (const gnus)
  1569. (const gnus-other-frame)
  1570. (const org-gnus-no-new-news)))
  1571. (cons (const file)
  1572. (choice
  1573. (const find-file)
  1574. (const find-file-other-window)
  1575. (const find-file-other-frame)))
  1576. (cons (const wl)
  1577. (choice
  1578. (const wl)
  1579. (const wl-other-frame)))))
  1580. (defcustom org-display-internal-link-with-indirect-buffer nil
  1581. "Non-nil means use indirect buffer to display infile links.
  1582. Activating internal links (from one location in a file to another location
  1583. in the same file) normally just jumps to the location. When the link is
  1584. activated with a \\[universal-argument] prefix (or with mouse-3), the link \
  1585. is displayed in
  1586. another window. When this option is set, the other window actually displays
  1587. an indirect buffer clone of the current buffer, to avoid any visibility
  1588. changes to the current buffer."
  1589. :group 'org-link-follow
  1590. :type 'boolean)
  1591. (defcustom org-open-non-existing-files nil
  1592. "Non-nil means `org-open-file' will open non-existing files.
  1593. When nil, an error will be generated.
  1594. This variable applies only to external applications because they
  1595. might choke on non-existing files. If the link is to a file that
  1596. will be opened in Emacs, the variable is ignored."
  1597. :group 'org-link-follow
  1598. :type 'boolean)
  1599. (defcustom org-open-directory-means-index-dot-org nil
  1600. "Non-nil means a link to a directory really means to index.org.
  1601. When nil, following a directory link will run dired or open a finder/explorer
  1602. window on that directory."
  1603. :group 'org-link-follow
  1604. :type 'boolean)
  1605. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1606. "Function and arguments to call for following mailto links.
  1607. This is a list with the first element being a Lisp function, and the
  1608. remaining elements being arguments to the function. In string arguments,
  1609. %a will be replaced by the address, and %s will be replaced by the subject
  1610. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1611. :group 'org-link-follow
  1612. :type '(choice
  1613. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1614. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1615. (const :tag "message-mail" (message-mail "%a" "%s"))
  1616. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1617. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1618. "Non-nil means ask for confirmation before executing shell links.
  1619. Shell links can be dangerous: just think about a link
  1620. [[shell:rm -rf ~/*][Google Search]]
  1621. This link would show up in your Org-mode document as \"Google Search\",
  1622. but really it would remove your entire home directory.
  1623. Therefore we advise against setting this variable to nil.
  1624. Just change it to `y-or-n-p' if you want to confirm with a
  1625. single keystroke rather than having to type \"yes\"."
  1626. :group 'org-link-follow
  1627. :type '(choice
  1628. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1629. (const :tag "with y-or-n (faster)" y-or-n-p)
  1630. (const :tag "no confirmation (dangerous)" nil)))
  1631. (put 'org-confirm-shell-link-function
  1632. 'safe-local-variable
  1633. #'(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1634. (defcustom org-confirm-shell-link-not-regexp ""
  1635. "A regexp to skip confirmation for shell links."
  1636. :group 'org-link-follow
  1637. :version "24.1"
  1638. :type 'regexp)
  1639. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1640. "Non-nil means ask for confirmation before executing Emacs Lisp links.
  1641. Elisp links can be dangerous: just think about a link
  1642. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1643. This link would show up in your Org-mode document as \"Google Search\",
  1644. but really it would remove your entire home directory.
  1645. Therefore we advise against setting this variable to nil.
  1646. Just change it to `y-or-n-p' if you want to confirm with a
  1647. single keystroke rather than having to type \"yes\"."
  1648. :group 'org-link-follow
  1649. :type '(choice
  1650. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1651. (const :tag "with y-or-n (faster)" y-or-n-p)
  1652. (const :tag "no confirmation (dangerous)" nil)))
  1653. (put 'org-confirm-shell-link-function
  1654. 'safe-local-variable
  1655. #'(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1656. (defcustom org-confirm-elisp-link-not-regexp ""
  1657. "A regexp to skip confirmation for Elisp links."
  1658. :group 'org-link-follow
  1659. :version "24.1"
  1660. :type 'regexp)
  1661. (defconst org-file-apps-defaults-gnu
  1662. '((remote . emacs)
  1663. (system . mailcap)
  1664. (t . mailcap))
  1665. "Default file applications on a UNIX or GNU/Linux system.
  1666. See `org-file-apps'.")
  1667. (defconst org-file-apps-defaults-macosx
  1668. '((remote . emacs)
  1669. (t . "open %s")
  1670. (system . "open %s")
  1671. ("ps.gz" . "gv %s")
  1672. ("eps.gz" . "gv %s")
  1673. ("dvi" . "xdvi %s")
  1674. ("fig" . "xfig %s"))
  1675. "Default file applications on a MacOS X system.
  1676. The system \"open\" is known as a default, but we use X11 applications
  1677. for some files for which the OS does not have a good default.
  1678. See `org-file-apps'.")
  1679. (defconst org-file-apps-defaults-windowsnt
  1680. (list
  1681. '(remote . emacs)
  1682. (cons t
  1683. (list (if (featurep 'xemacs)
  1684. 'mswindows-shell-execute
  1685. 'w32-shell-execute)
  1686. "open" 'file))
  1687. (cons 'system
  1688. (list (if (featurep 'xemacs)
  1689. 'mswindows-shell-execute
  1690. 'w32-shell-execute)
  1691. "open" 'file)))
  1692. "Default file applications on a Windows NT system.
  1693. The system \"open\" is used for most files.
  1694. See `org-file-apps'.")
  1695. (defcustom org-file-apps
  1696. '((auto-mode . emacs)
  1697. ("\\.mm\\'" . default)
  1698. ("\\.x?html?\\'" . default)
  1699. ("\\.pdf\\'" . default))
  1700. "External applications for opening `file:path' items in a document.
  1701. Org-mode uses system defaults for different file types, but
  1702. you can use this variable to set the application for a given file
  1703. extension. The entries in this list are cons cells where the car identifies
  1704. files and the cdr the corresponding command. Possible values for the
  1705. file identifier are
  1706. \"string\" A string as a file identifier can be interpreted in different
  1707. ways, depending on its contents:
  1708. - Alphanumeric characters only:
  1709. Match links with this file extension.
  1710. Example: (\"pdf\" . \"evince %s\")
  1711. to open PDFs with evince.
  1712. - Regular expression: Match links where the
  1713. filename matches the regexp. If you want to
  1714. use groups here, use shy groups.
  1715. Example: (\"\\.x?html\\'\" . \"firefox %s\")
  1716. (\"\\(?:xhtml\\|html\\)\" . \"firefox %s\")
  1717. to open *.html and *.xhtml with firefox.
  1718. - Regular expression which contains (non-shy) groups:
  1719. Match links where the whole link, including \"::\", and
  1720. anything after that, matches the regexp.
  1721. In a custom command string, %1, %2, etc. are replaced with
  1722. the parts of the link that were matched by the groups.
  1723. For backwards compatibility, if a command string is given
  1724. that does not use any of the group matches, this case is
  1725. handled identically to the second one (i.e. match against
  1726. file name only).
  1727. In a custom lisp form, you can access the group matches with
  1728. (match-string n link).
  1729. Example: (\"\\.pdf::\\(\\d+\\)\\'\" . \"evince -p %1 %s\")
  1730. to open [[file:document.pdf::5]] with evince at page 5.
  1731. `directory' Matches a directory
  1732. `remote' Matches a remote file, accessible through tramp or efs.
  1733. Remote files most likely should be visited through Emacs
  1734. because external applications cannot handle such paths.
  1735. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1736. so all files Emacs knows how to handle. Using this with
  1737. command `emacs' will open most files in Emacs. Beware that this
  1738. will also open html files inside Emacs, unless you add
  1739. (\"html\" . default) to the list as well.
  1740. t Default for files not matched by any of the other options.
  1741. `system' The system command to open files, like `open' on Windows
  1742. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1743. that will be selected if you call `C-c C-o' with a double
  1744. \\[universal-argument] \\[universal-argument] prefix.
  1745. Possible values for the command are:
  1746. `emacs' The file will be visited by the current Emacs process.
  1747. `default' Use the default application for this file type, which is the
  1748. association for t in the list, most likely in the system-specific
  1749. part.
  1750. This can be used to overrule an unwanted setting in the
  1751. system-specific variable.
  1752. `system' Use the system command for opening files, like \"open\".
  1753. This command is specified by the entry whose car is `system'.
  1754. Most likely, the system-specific version of this variable
  1755. does define this command, but you can overrule/replace it
  1756. here.
  1757. string A command to be executed by a shell; %s will be replaced
  1758. by the path to the file.
  1759. sexp A Lisp form which will be evaluated. The file path will
  1760. be available in the Lisp variable `file'.
  1761. For more examples, see the system specific constants
  1762. `org-file-apps-defaults-macosx'
  1763. `org-file-apps-defaults-windowsnt'
  1764. `org-file-apps-defaults-gnu'."
  1765. :group 'org-link-follow
  1766. :type '(repeat
  1767. (cons (choice :value ""
  1768. (string :tag "Extension")
  1769. (const :tag "System command to open files" system)
  1770. (const :tag "Default for unrecognized files" t)
  1771. (const :tag "Remote file" remote)
  1772. (const :tag "Links to a directory" directory)
  1773. (const :tag "Any files that have Emacs modes"
  1774. auto-mode))
  1775. (choice :value ""
  1776. (const :tag "Visit with Emacs" emacs)
  1777. (const :tag "Use default" default)
  1778. (const :tag "Use the system command" system)
  1779. (string :tag "Command")
  1780. (sexp :tag "Lisp form")))))
  1781. (defcustom org-doi-server-url "http://dx.doi.org/"
  1782. "The URL of the DOI server."
  1783. :type 'string
  1784. :version "24.3"
  1785. :group 'org-link-follow)
  1786. (defgroup org-refile nil
  1787. "Options concerning refiling entries in Org-mode."
  1788. :tag "Org Refile"
  1789. :group 'org)
  1790. (defcustom org-directory "~/org"
  1791. "Directory with org files.
  1792. This is just a default location to look for Org files. There is no need
  1793. at all to put your files into this directory. It is only used in the
  1794. following situations:
  1795. 1. When a capture template specifies a target file that is not an
  1796. absolute path. The path will then be interpreted relative to
  1797. `org-directory'
  1798. 2. When a capture note is filed away in an interactive way (when exiting the
  1799. note buffer with `C-1 C-c C-c'. The user is prompted for an org file,
  1800. with `org-directory' as the default path."
  1801. :group 'org-refile
  1802. :group 'org-remember
  1803. :group 'org-capture
  1804. :type 'directory)
  1805. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1806. "Default target for storing notes.
  1807. Used as a fall back file for org-remember.el and org-capture.el, for
  1808. templates that do not specify a target file."
  1809. :group 'org-refile
  1810. :group 'org-remember
  1811. :group 'org-capture
  1812. :type '(choice
  1813. (const :tag "Default from remember-data-file" nil)
  1814. file))
  1815. (defcustom org-goto-interface 'outline
  1816. "The default interface to be used for `org-goto'.
  1817. Allowed values are:
  1818. outline The interface shows an outline of the relevant file
  1819. and the correct heading is found by moving through
  1820. the outline or by searching with incremental search.
  1821. outline-path-completion Headlines in the current buffer are offered via
  1822. completion. This is the interface also used by
  1823. the refile command."
  1824. :group 'org-refile
  1825. :type '(choice
  1826. (const :tag "Outline" outline)
  1827. (const :tag "Outline-path-completion" outline-path-completion)))
  1828. (defcustom org-goto-max-level 5
  1829. "Maximum target level when running `org-goto' with refile interface."
  1830. :group 'org-refile
  1831. :type 'integer)
  1832. (defcustom org-reverse-note-order nil
  1833. "Non-nil means store new notes at the beginning of a file or entry.
  1834. When nil, new notes will be filed to the end of a file or entry.
  1835. This can also be a list with cons cells of regular expressions that
  1836. are matched against file names, and values."
  1837. :group 'org-remember
  1838. :group 'org-capture
  1839. :group 'org-refile
  1840. :type '(choice
  1841. (const :tag "Reverse always" t)
  1842. (const :tag "Reverse never" nil)
  1843. (repeat :tag "By file name regexp"
  1844. (cons regexp boolean))))
  1845. (defcustom org-log-refile nil
  1846. "Information to record when a task is refiled.
  1847. Possible values are:
  1848. nil Don't add anything
  1849. time Add a time stamp to the task
  1850. note Prompt for a note and add it with template `org-log-note-headings'
  1851. This option can also be set with on a per-file-basis with
  1852. #+STARTUP: nologrefile
  1853. #+STARTUP: logrefile
  1854. #+STARTUP: lognoterefile
  1855. You can have local logging settings for a subtree by setting the LOGGING
  1856. property to one or more of these keywords.
  1857. When bulk-refiling from the agenda, the value `note' is forbidden and
  1858. will temporarily be changed to `time'."
  1859. :group 'org-refile
  1860. :group 'org-progress
  1861. :version "24.1"
  1862. :type '(choice
  1863. (const :tag "No logging" nil)
  1864. (const :tag "Record timestamp" time)
  1865. (const :tag "Record timestamp with note." note)))
  1866. (defcustom org-refile-targets nil
  1867. "Targets for refiling entries with \\[org-refile].
  1868. This is a list of cons cells. Each cell contains:
  1869. - a specification of the files to be considered, either a list of files,
  1870. or a symbol whose function or variable value will be used to retrieve
  1871. a file name or a list of file names. If you use `org-agenda-files' for
  1872. that, all agenda files will be scanned for targets. Nil means consider
  1873. headings in the current buffer.
  1874. - A specification of how to find candidate refile targets. This may be
  1875. any of:
  1876. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1877. This tag has to be present in all target headlines, inheritance will
  1878. not be considered.
  1879. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1880. todo keyword.
  1881. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1882. headlines that are refiling targets.
  1883. - a cons cell (:level . N). Any headline of level N is considered a target.
  1884. Note that, when `org-odd-levels-only' is set, level corresponds to
  1885. order in hierarchy, not to the number of stars.
  1886. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1887. Note that, when `org-odd-levels-only' is set, level corresponds to
  1888. order in hierarchy, not to the number of stars.
  1889. Each element of this list generates a set of possible targets.
  1890. The union of these sets is presented (with completion) to
  1891. the user by `org-refile'.
  1892. You can set the variable `org-refile-target-verify-function' to a function
  1893. to verify each headline found by the simple criteria above.
  1894. When this variable is nil, all top-level headlines in the current buffer
  1895. are used, equivalent to the value `((nil . (:level . 1))'."
  1896. :group 'org-refile
  1897. :type '(repeat
  1898. (cons
  1899. (choice :value org-agenda-files
  1900. (const :tag "All agenda files" org-agenda-files)
  1901. (const :tag "Current buffer" nil)
  1902. (function) (variable) (file))
  1903. (choice :tag "Identify target headline by"
  1904. (cons :tag "Specific tag" (const :value :tag) (string))
  1905. (cons :tag "TODO keyword" (const :value :todo) (string))
  1906. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  1907. (cons :tag "Level number" (const :value :level) (integer))
  1908. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  1909. (defcustom org-refile-target-verify-function nil
  1910. "Function to verify if the headline at point should be a refile target.
  1911. The function will be called without arguments, with point at the
  1912. beginning of the headline. It should return t and leave point
  1913. where it is if the headline is a valid target for refiling.
  1914. If the target should not be selected, the function must return nil.
  1915. In addition to this, it may move point to a place from where the search
  1916. should be continued. For example, the function may decide that the entire
  1917. subtree of the current entry should be excluded and move point to the end
  1918. of the subtree."
  1919. :group 'org-refile
  1920. :type '(choice
  1921. (const nil)
  1922. (function)))
  1923. (defcustom org-refile-use-cache nil
  1924. "Non-nil means cache refile targets to speed up the process.
  1925. The cache for a particular file will be updated automatically when
  1926. the buffer has been killed, or when any of the marker used for flagging
  1927. refile targets no longer points at a live buffer.
  1928. If you have added new entries to a buffer that might themselves be targets,
  1929. you need to clear the cache manually by pressing `C-0 C-c C-w' or, if you
  1930. find that easier, `C-u C-u C-u C-c C-w'."
  1931. :group 'org-refile
  1932. :version "24.1"
  1933. :type 'boolean)
  1934. (defcustom org-refile-use-outline-path nil
  1935. "Non-nil means provide refile targets as paths.
  1936. So a level 3 headline will be available as level1/level2/level3.
  1937. When the value is `file', also include the file name (without directory)
  1938. into the path. In this case, you can also stop the completion after
  1939. the file name, to get entries inserted as top level in the file.
  1940. When `full-file-path', include the full file path."
  1941. :group 'org-refile
  1942. :type '(choice
  1943. (const :tag "Not" nil)
  1944. (const :tag "Yes" t)
  1945. (const :tag "Start with file name" file)
  1946. (const :tag "Start with full file path" full-file-path)))
  1947. (defcustom org-outline-path-complete-in-steps t
  1948. "Non-nil means complete the outline path in hierarchical steps.
  1949. When Org-mode uses the refile interface to select an outline path
  1950. \(see variable `org-refile-use-outline-path'), the completion of
  1951. the path can be done is a single go, or if can be done in steps down
  1952. the headline hierarchy. Going in steps is probably the best if you
  1953. do not use a special completion package like `ido' or `icicles'.
  1954. However, when using these packages, going in one step can be very
  1955. fast, while still showing the whole path to the entry."
  1956. :group 'org-refile
  1957. :type 'boolean)
  1958. (defcustom org-refile-allow-creating-parent-nodes nil
  1959. "Non-nil means allow to create new nodes as refile targets.
  1960. New nodes are then created by adding \"/new node name\" to the completion
  1961. of an existing node. When the value of this variable is `confirm',
  1962. new node creation must be confirmed by the user (recommended)
  1963. When nil, the completion must match an existing entry.
  1964. Note that, if the new heading is not seen by the criteria
  1965. listed in `org-refile-targets', multiple instances of the same
  1966. heading would be created by trying again to file under the new
  1967. heading."
  1968. :group 'org-refile
  1969. :type '(choice
  1970. (const :tag "Never" nil)
  1971. (const :tag "Always" t)
  1972. (const :tag "Prompt for confirmation" confirm)))
  1973. (defcustom org-refile-active-region-within-subtree nil
  1974. "Non-nil means also refile active region within a subtree.
  1975. By default `org-refile' doesn't allow refiling regions if they
  1976. don't contain a set of subtrees, but it might be convenient to
  1977. do so sometimes: in that case, the first line of the region is
  1978. converted to a headline before refiling."
  1979. :group 'org-refile
  1980. :version "24.1"
  1981. :type 'boolean)
  1982. (defgroup org-todo nil
  1983. "Options concerning TODO items in Org-mode."
  1984. :tag "Org TODO"
  1985. :group 'org)
  1986. (defgroup org-progress nil
  1987. "Options concerning Progress logging in Org-mode."
  1988. :tag "Org Progress"
  1989. :group 'org-time)
  1990. (defvar org-todo-interpretation-widgets
  1991. '((:tag "Sequence (cycling hits every state)" sequence)
  1992. (:tag "Type (cycling directly to DONE)" type))
  1993. "The available interpretation symbols for customizing `org-todo-keywords'.
  1994. Interested libraries should add to this list.")
  1995. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1996. "List of TODO entry keyword sequences and their interpretation.
  1997. \\<org-mode-map>This is a list of sequences.
  1998. Each sequence starts with a symbol, either `sequence' or `type',
  1999. indicating if the keywords should be interpreted as a sequence of
  2000. action steps, or as different types of TODO items. The first
  2001. keywords are states requiring action - these states will select a headline
  2002. for inclusion into the global TODO list Org-mode produces. If one of
  2003. the \"keywords\" is the vertical bar, \"|\", the remaining keywords
  2004. signify that no further action is necessary. If \"|\" is not found,
  2005. the last keyword is treated as the only DONE state of the sequence.
  2006. The command \\[org-todo] cycles an entry through these states, and one
  2007. additional state where no keyword is present. For details about this
  2008. cycling, see the manual.
  2009. TODO keywords and interpretation can also be set on a per-file basis with
  2010. the special #+SEQ_TODO and #+TYP_TODO lines.
  2011. Each keyword can optionally specify a character for fast state selection
  2012. \(in combination with the variable `org-use-fast-todo-selection')
  2013. and specifiers for state change logging, using the same syntax that
  2014. is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says that
  2015. the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  2016. indicates to record a time stamp each time this state is selected.
  2017. Each keyword may also specify if a timestamp or a note should be
  2018. recorded when entering or leaving the state, by adding additional
  2019. characters in the parenthesis after the keyword. This looks like this:
  2020. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  2021. record only the time of the state change. With X and Y being either
  2022. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  2023. Y when leaving the state if and only if the *target* state does not
  2024. define X. You may omit any of the fast-selection key or X or /Y,
  2025. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  2026. For backward compatibility, this variable may also be just a list
  2027. of keywords. In this case the interpretation (sequence or type) will be
  2028. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  2029. :group 'org-todo
  2030. :group 'org-keywords
  2031. :type '(choice
  2032. (repeat :tag "Old syntax, just keywords"
  2033. (string :tag "Keyword"))
  2034. (repeat :tag "New syntax"
  2035. (cons
  2036. (choice
  2037. :tag "Interpretation"
  2038. ;;Quick and dirty way to see
  2039. ;;`org-todo-interpretations'. This takes the
  2040. ;;place of item arguments
  2041. :convert-widget
  2042. (lambda (widget)
  2043. (widget-put widget
  2044. :args (mapcar
  2045. #'(lambda (x)
  2046. (widget-convert
  2047. (cons 'const x)))
  2048. org-todo-interpretation-widgets))
  2049. widget))
  2050. (repeat
  2051. (string :tag "Keyword"))))))
  2052. (defvar org-todo-keywords-1 nil
  2053. "All TODO and DONE keywords active in a buffer.")
  2054. (make-variable-buffer-local 'org-todo-keywords-1)
  2055. (defvar org-todo-keywords-for-agenda nil)
  2056. (defvar org-done-keywords-for-agenda nil)
  2057. (defvar org-drawers-for-agenda nil)
  2058. (defvar org-todo-keyword-alist-for-agenda nil)
  2059. (defvar org-tag-alist-for-agenda nil
  2060. "Alist of all tags from all agenda files.")
  2061. (defvar org-tag-groups-alist-for-agenda nil
  2062. "Alist of all groups tags from all current agenda files.")
  2063. (defvar org-tag-groups-alist nil)
  2064. (make-variable-buffer-local 'org-tag-groups-alist)
  2065. (defvar org-agenda-contributing-files nil)
  2066. (defvar org-not-done-keywords nil)
  2067. (make-variable-buffer-local 'org-not-done-keywords)
  2068. (defvar org-done-keywords nil)
  2069. (make-variable-buffer-local 'org-done-keywords)
  2070. (defvar org-todo-heads nil)
  2071. (make-variable-buffer-local 'org-todo-heads)
  2072. (defvar org-todo-sets nil)
  2073. (make-variable-buffer-local 'org-todo-sets)
  2074. (defvar org-todo-log-states nil)
  2075. (make-variable-buffer-local 'org-todo-log-states)
  2076. (defvar org-todo-kwd-alist nil)
  2077. (make-variable-buffer-local 'org-todo-kwd-alist)
  2078. (defvar org-todo-key-alist nil)
  2079. (make-variable-buffer-local 'org-todo-key-alist)
  2080. (defvar org-todo-key-trigger nil)
  2081. (make-variable-buffer-local 'org-todo-key-trigger)
  2082. (defcustom org-todo-interpretation 'sequence
  2083. "Controls how TODO keywords are interpreted.
  2084. This variable is in principle obsolete and is only used for
  2085. backward compatibility, if the interpretation of todo keywords is
  2086. not given already in `org-todo-keywords'. See that variable for
  2087. more information."
  2088. :group 'org-todo
  2089. :group 'org-keywords
  2090. :type '(choice (const sequence)
  2091. (const type)))
  2092. (defcustom org-use-fast-todo-selection t
  2093. "Non-nil means use the fast todo selection scheme with C-c C-t.
  2094. This variable describes if and under what circumstances the cycling
  2095. mechanism for TODO keywords will be replaced by a single-key, direct
  2096. selection scheme.
  2097. When nil, fast selection is never used.
  2098. When the symbol `prefix', it will be used when `org-todo' is called
  2099. with a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and
  2100. `C-u t' in an agenda buffer.
  2101. When t, fast selection is used by default. In this case, the prefix
  2102. argument forces cycling instead.
  2103. In all cases, the special interface is only used if access keys have
  2104. actually been assigned by the user, i.e. if keywords in the configuration
  2105. are followed by a letter in parenthesis, like TODO(t)."
  2106. :group 'org-todo
  2107. :type '(choice
  2108. (const :tag "Never" nil)
  2109. (const :tag "By default" t)
  2110. (const :tag "Only with C-u C-c C-t" prefix)))
  2111. (defcustom org-provide-todo-statistics t
  2112. "Non-nil means update todo statistics after insert and toggle.
  2113. ALL-HEADLINES means update todo statistics by including headlines
  2114. with no TODO keyword as well, counting them as not done.
  2115. A list of TODO keywords means the same, but skip keywords that are
  2116. not in this list.
  2117. When this is set, todo statistics is updated in the parent of the
  2118. current entry each time a todo state is changed."
  2119. :group 'org-todo
  2120. :type '(choice
  2121. (const :tag "Yes, only for TODO entries" t)
  2122. (const :tag "Yes, including all entries" 'all-headlines)
  2123. (repeat :tag "Yes, for TODOs in this list"
  2124. (string :tag "TODO keyword"))
  2125. (other :tag "No TODO statistics" nil)))
  2126. (defcustom org-hierarchical-todo-statistics t
  2127. "Non-nil means TODO statistics covers just direct children.
  2128. When nil, all entries in the subtree are considered.
  2129. This has only an effect if `org-provide-todo-statistics' is set.
  2130. To set this to nil for only a single subtree, use a COOKIE_DATA
  2131. property and include the word \"recursive\" into the value."
  2132. :group 'org-todo
  2133. :type 'boolean)
  2134. (defcustom org-after-todo-state-change-hook nil
  2135. "Hook which is run after the state of a TODO item was changed.
  2136. The new state (a string with a TODO keyword, or nil) is available in the
  2137. Lisp variable `org-state'."
  2138. :group 'org-todo
  2139. :type 'hook)
  2140. (defvar org-blocker-hook nil
  2141. "Hook for functions that are allowed to block a state change.
  2142. Functions in this hook should not modify the buffer.
  2143. Each function gets as its single argument a property list,
  2144. see `org-trigger-hook' for more information about this list.
  2145. If any of the functions in this hook returns nil, the state change
  2146. is blocked.")
  2147. (defvar org-trigger-hook nil
  2148. "Hook for functions that are triggered by a state change.
  2149. Each function gets as its single argument a property list with at
  2150. least the following elements:
  2151. (:type type-of-change :position pos-at-entry-start
  2152. :from old-state :to new-state)
  2153. Depending on the type, more properties may be present.
  2154. This mechanism is currently implemented for:
  2155. TODO state changes
  2156. ------------------
  2157. :type todo-state-change
  2158. :from previous state (keyword as a string), or nil, or a symbol
  2159. 'todo' or 'done', to indicate the general type of state.
  2160. :to new state, like in :from")
  2161. (defcustom org-enforce-todo-dependencies nil
  2162. "Non-nil means undone TODO entries will block switching the parent to DONE.
  2163. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  2164. be blocked if any prior sibling is not yet done.
  2165. Finally, if the parent is blocked because of ordered siblings of its own,
  2166. the child will also be blocked."
  2167. :set (lambda (var val)
  2168. (set var val)
  2169. (if val
  2170. (add-hook 'org-blocker-hook
  2171. 'org-block-todo-from-children-or-siblings-or-parent)
  2172. (remove-hook 'org-blocker-hook
  2173. 'org-block-todo-from-children-or-siblings-or-parent)))
  2174. :group 'org-todo
  2175. :type 'boolean)
  2176. (defcustom org-enforce-todo-checkbox-dependencies nil
  2177. "Non-nil means unchecked boxes will block switching the parent to DONE.
  2178. When this is nil, checkboxes have no influence on switching TODO states.
  2179. When non-nil, you first need to check off all check boxes before the TODO
  2180. entry can be switched to DONE.
  2181. This variable needs to be set before org.el is loaded, and you need to
  2182. restart Emacs after a change to make the change effective. The only way
  2183. to change is while Emacs is running is through the customize interface."
  2184. :set (lambda (var val)
  2185. (set var val)
  2186. (if val
  2187. (add-hook 'org-blocker-hook
  2188. 'org-block-todo-from-checkboxes)
  2189. (remove-hook 'org-blocker-hook
  2190. 'org-block-todo-from-checkboxes)))
  2191. :group 'org-todo
  2192. :type 'boolean)
  2193. (defcustom org-treat-insert-todo-heading-as-state-change nil
  2194. "Non-nil means inserting a TODO heading is treated as state change.
  2195. So when the command \\[org-insert-todo-heading] is used, state change
  2196. logging will apply if appropriate. When nil, the new TODO item will
  2197. be inserted directly, and no logging will take place."
  2198. :group 'org-todo
  2199. :type 'boolean)
  2200. (defcustom org-treat-S-cursor-todo-selection-as-state-change t
  2201. "Non-nil means switching TODO states with S-cursor counts as state change.
  2202. This is the default behavior. However, setting this to nil allows a
  2203. convenient way to select a TODO state and bypass any logging associated
  2204. with that."
  2205. :group 'org-todo
  2206. :type 'boolean)
  2207. (defcustom org-todo-state-tags-triggers nil
  2208. "Tag changes that should be triggered by TODO state changes.
  2209. This is a list. Each entry is
  2210. (state-change (tag . flag) .......)
  2211. State-change can be a string with a state, and empty string to indicate the
  2212. state that has no TODO keyword, or it can be one of the symbols `todo'
  2213. or `done', meaning any not-done or done state, respectively."
  2214. :group 'org-todo
  2215. :group 'org-tags
  2216. :type '(repeat
  2217. (cons (choice :tag "When changing to"
  2218. (const :tag "Not-done state" todo)
  2219. (const :tag "Done state" done)
  2220. (string :tag "State"))
  2221. (repeat
  2222. (cons :tag "Tag action"
  2223. (string :tag "Tag")
  2224. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  2225. (defcustom org-log-done nil
  2226. "Information to record when a task moves to the DONE state.
  2227. Possible values are:
  2228. nil Don't add anything, just change the keyword
  2229. time Add a time stamp to the task
  2230. note Prompt for a note and add it with template `org-log-note-headings'
  2231. This option can also be set with on a per-file-basis with
  2232. #+STARTUP: nologdone
  2233. #+STARTUP: logdone
  2234. #+STARTUP: lognotedone
  2235. You can have local logging settings for a subtree by setting the LOGGING
  2236. property to one or more of these keywords."
  2237. :group 'org-todo
  2238. :group 'org-progress
  2239. :type '(choice
  2240. (const :tag "No logging" nil)
  2241. (const :tag "Record CLOSED timestamp" time)
  2242. (const :tag "Record CLOSED timestamp with note." note)))
  2243. ;; Normalize old uses of org-log-done.
  2244. (cond
  2245. ((eq org-log-done t) (setq org-log-done 'time))
  2246. ((and (listp org-log-done) (memq 'done org-log-done))
  2247. (setq org-log-done 'note)))
  2248. (defcustom org-log-reschedule nil
  2249. "Information to record when the scheduling date of a tasks is modified.
  2250. Possible values are:
  2251. nil Don't add anything, just change the date
  2252. time Add a time stamp to the task
  2253. note Prompt for a note and add it with template `org-log-note-headings'
  2254. This option can also be set with on a per-file-basis with
  2255. #+STARTUP: nologreschedule
  2256. #+STARTUP: logreschedule
  2257. #+STARTUP: lognotereschedule"
  2258. :group 'org-todo
  2259. :group 'org-progress
  2260. :type '(choice
  2261. (const :tag "No logging" nil)
  2262. (const :tag "Record timestamp" time)
  2263. (const :tag "Record timestamp with note." note)))
  2264. (defcustom org-log-redeadline nil
  2265. "Information to record when the deadline date of a tasks is modified.
  2266. Possible values are:
  2267. nil Don't add anything, just change the date
  2268. time Add a time stamp to the task
  2269. note Prompt for a note and add it with template `org-log-note-headings'
  2270. This option can also be set with on a per-file-basis with
  2271. #+STARTUP: nologredeadline
  2272. #+STARTUP: logredeadline
  2273. #+STARTUP: lognoteredeadline
  2274. You can have local logging settings for a subtree by setting the LOGGING
  2275. property to one or more of these keywords."
  2276. :group 'org-todo
  2277. :group 'org-progress
  2278. :type '(choice
  2279. (const :tag "No logging" nil)
  2280. (const :tag "Record timestamp" time)
  2281. (const :tag "Record timestamp with note." note)))
  2282. (defcustom org-log-note-clock-out nil
  2283. "Non-nil means record a note when clocking out of an item.
  2284. This can also be configured on a per-file basis by adding one of
  2285. the following lines anywhere in the buffer:
  2286. #+STARTUP: lognoteclock-out
  2287. #+STARTUP: nolognoteclock-out"
  2288. :group 'org-todo
  2289. :group 'org-progress
  2290. :type 'boolean)
  2291. (defcustom org-log-done-with-time t
  2292. "Non-nil means the CLOSED time stamp will contain date and time.
  2293. When nil, only the date will be recorded."
  2294. :group 'org-progress
  2295. :type 'boolean)
  2296. (defcustom org-log-note-headings
  2297. '((done . "CLOSING NOTE %t")
  2298. (state . "State %-12s from %-12S %t")
  2299. (note . "Note taken on %t")
  2300. (reschedule . "Rescheduled from %S on %t")
  2301. (delschedule . "Not scheduled, was %S on %t")
  2302. (redeadline . "New deadline from %S on %t")
  2303. (deldeadline . "Removed deadline, was %S on %t")
  2304. (refile . "Refiled on %t")
  2305. (clock-out . ""))
  2306. "Headings for notes added to entries.
  2307. The value is an alist, with the car being a symbol indicating the note
  2308. context, and the cdr is the heading to be used. The heading may also be the
  2309. empty string.
  2310. %t in the heading will be replaced by a time stamp.
  2311. %T will be an active time stamp instead the default inactive one
  2312. %d will be replaced by a short-format time stamp.
  2313. %D will be replaced by an active short-format time stamp.
  2314. %s will be replaced by the new TODO state, in double quotes.
  2315. %S will be replaced by the old TODO state, in double quotes.
  2316. %u will be replaced by the user name.
  2317. %U will be replaced by the full user name.
  2318. In fact, it is not a good idea to change the `state' entry, because
  2319. agenda log mode depends on the format of these entries."
  2320. :group 'org-todo
  2321. :group 'org-progress
  2322. :type '(list :greedy t
  2323. (cons (const :tag "Heading when closing an item" done) string)
  2324. (cons (const :tag
  2325. "Heading when changing todo state (todo sequence only)"
  2326. state) string)
  2327. (cons (const :tag "Heading when just taking a note" note) string)
  2328. (cons (const :tag "Heading when clocking out" clock-out) string)
  2329. (cons (const :tag "Heading when an item is no longer scheduled" delschedule) string)
  2330. (cons (const :tag "Heading when rescheduling" reschedule) string)
  2331. (cons (const :tag "Heading when changing deadline" redeadline) string)
  2332. (cons (const :tag "Heading when deleting a deadline" deldeadline) string)
  2333. (cons (const :tag "Heading when refiling" refile) string)))
  2334. (unless (assq 'note org-log-note-headings)
  2335. (push '(note . "%t") org-log-note-headings))
  2336. (defcustom org-log-into-drawer nil
  2337. "Non-nil means insert state change notes and time stamps into a drawer.
  2338. When nil, state changes notes will be inserted after the headline and
  2339. any scheduling and clock lines, but not inside a drawer.
  2340. The value of this variable should be the name of the drawer to use.
  2341. LOGBOOK is proposed as the default drawer for this purpose, you can
  2342. also set this to a string to define the drawer of your choice.
  2343. A value of t is also allowed, representing \"LOGBOOK\".
  2344. A value of t or nil can also be set with on a per-file-basis with
  2345. #+STARTUP: logdrawer
  2346. #+STARTUP: nologdrawer
  2347. If this variable is set, `org-log-state-notes-insert-after-drawers'
  2348. will be ignored.
  2349. You can set the property LOG_INTO_DRAWER to overrule this setting for
  2350. a subtree."
  2351. :group 'org-todo
  2352. :group 'org-progress
  2353. :type '(choice
  2354. (const :tag "Not into a drawer" nil)
  2355. (const :tag "LOGBOOK" t)
  2356. (string :tag "Other")))
  2357. (org-defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer)
  2358. (defun org-log-into-drawer ()
  2359. "Return the value of `org-log-into-drawer', but let properties overrule.
  2360. If the current entry has or inherits a LOG_INTO_DRAWER property, it will be
  2361. used instead of the default value."
  2362. (let ((p (org-entry-get nil "LOG_INTO_DRAWER" 'inherit t)))
  2363. (cond
  2364. ((not p) org-log-into-drawer)
  2365. ((equal p "nil") nil)
  2366. ((equal p "t") "LOGBOOK")
  2367. (t p))))
  2368. (defcustom org-log-state-notes-insert-after-drawers nil
  2369. "Non-nil means insert state change notes after any drawers in entry.
  2370. Only the drawers that *immediately* follow the headline and the
  2371. deadline/scheduled line are skipped.
  2372. When nil, insert notes right after the heading and perhaps the line
  2373. with deadline/scheduling if present.
  2374. This variable will have no effect if `org-log-into-drawer' is
  2375. set."
  2376. :group 'org-todo
  2377. :group 'org-progress
  2378. :type 'boolean)
  2379. (defcustom org-log-states-order-reversed t
  2380. "Non-nil means the latest state note will be directly after heading.
  2381. When nil, the state change notes will be ordered according to time.
  2382. This option can also be set with on a per-file-basis with
  2383. #+STARTUP: logstatesreversed
  2384. #+STARTUP: nologstatesreversed"
  2385. :group 'org-todo
  2386. :group 'org-progress
  2387. :type 'boolean)
  2388. (defcustom org-todo-repeat-to-state nil
  2389. "The TODO state to which a repeater should return the repeating task.
  2390. By default this is the first task in a TODO sequence, or the previous state
  2391. in a TODO_TYP set. But you can specify another task here.
  2392. alternatively, set the :REPEAT_TO_STATE: property of the entry."
  2393. :group 'org-todo
  2394. :version "24.1"
  2395. :type '(choice (const :tag "Head of sequence" nil)
  2396. (string :tag "Specific state")))
  2397. (defcustom org-log-repeat 'time
  2398. "Non-nil means record moving through the DONE state when triggering repeat.
  2399. An auto-repeating task is immediately switched back to TODO when
  2400. marked DONE. If you are not logging state changes (by adding \"@\"
  2401. or \"!\" to the TODO keyword definition), or set `org-log-done' to
  2402. record a closing note, there will be no record of the task moving
  2403. through DONE. This variable forces taking a note anyway.
  2404. nil Don't force a record
  2405. time Record a time stamp
  2406. note Prompt for a note and add it with template `org-log-note-headings'
  2407. This option can also be set with on a per-file-basis with
  2408. #+STARTUP: nologrepeat
  2409. #+STARTUP: logrepeat
  2410. #+STARTUP: lognoterepeat
  2411. You can have local logging settings for a subtree by setting the LOGGING
  2412. property to one or more of these keywords."
  2413. :group 'org-todo
  2414. :group 'org-progress
  2415. :type '(choice
  2416. (const :tag "Don't force a record" nil)
  2417. (const :tag "Force recording the DONE state" time)
  2418. (const :tag "Force recording a note with the DONE state" note)))
  2419. (defgroup org-priorities nil
  2420. "Priorities in Org-mode."
  2421. :tag "Org Priorities"
  2422. :group 'org-todo)
  2423. (defcustom org-enable-priority-commands t
  2424. "Non-nil means priority commands are active.
  2425. When nil, these commands will be disabled, so that you never accidentally
  2426. set a priority."
  2427. :group 'org-priorities
  2428. :type 'boolean)
  2429. (defcustom org-highest-priority ?A
  2430. "The highest priority of TODO items. A character like ?A, ?B etc.
  2431. Must have a smaller ASCII number than `org-lowest-priority'."
  2432. :group 'org-priorities
  2433. :type 'character)
  2434. (defcustom org-lowest-priority ?C
  2435. "The lowest priority of TODO items. A character like ?A, ?B etc.
  2436. Must have a larger ASCII number than `org-highest-priority'."
  2437. :group 'org-priorities
  2438. :type 'character)
  2439. (defcustom org-default-priority ?B
  2440. "The default priority of TODO items.
  2441. This is the priority an item gets if no explicit priority is given.
  2442. When starting to cycle on an empty priority the first step in the cycle
  2443. depends on `org-priority-start-cycle-with-default'. The resulting first
  2444. step priority must not exceed the range from `org-highest-priority' to
  2445. `org-lowest-priority' which means that `org-default-priority' has to be
  2446. in this range exclusive or inclusive the range boundaries. Else the
  2447. first step refuses to set the default and the second will fall back
  2448. to (depending on the command used) the highest or lowest priority."
  2449. :group 'org-priorities
  2450. :type 'character)
  2451. (defcustom org-priority-start-cycle-with-default t
  2452. "Non-nil means start with default priority when starting to cycle.
  2453. When this is nil, the first step in the cycle will be (depending on the
  2454. command used) one higher or lower than the default priority.
  2455. See also `org-default-priority'."
  2456. :group 'org-priorities
  2457. :type 'boolean)
  2458. (defcustom org-get-priority-function nil
  2459. "Function to extract the priority from a string.
  2460. The string is normally the headline. If this is nil Org computes the
  2461. priority from the priority cookie like [#A] in the headline. It returns
  2462. an integer, increasing by 1000 for each priority level.
  2463. The user can set a different function here, which should take a string
  2464. as an argument and return the numeric priority."
  2465. :group 'org-priorities
  2466. :version "24.1"
  2467. :type '(choice
  2468. (const nil)
  2469. (function)))
  2470. (defgroup org-time nil
  2471. "Options concerning time stamps and deadlines in Org-mode."
  2472. :tag "Org Time"
  2473. :group 'org)
  2474. (defcustom org-insert-labeled-timestamps-at-point nil
  2475. "Non-nil means SCHEDULED and DEADLINE timestamps are inserted at point.
  2476. When nil, these labeled time stamps are forces into the second line of an
  2477. entry, just after the headline. When scheduling from the global TODO list,
  2478. the time stamp will always be forced into the second line."
  2479. :group 'org-time
  2480. :type 'boolean)
  2481. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  2482. "Formats for `format-time-string' which are used for time stamps.
  2483. It is not recommended to change this constant.")
  2484. (defcustom org-time-stamp-rounding-minutes '(0 5)
  2485. "Number of minutes to round time stamps to.
  2486. These are two values, the first applies when first creating a time stamp.
  2487. The second applies when changing it with the commands `S-up' and `S-down'.
  2488. When changing the time stamp, this means that it will change in steps
  2489. of N minutes, as given by the second value.
  2490. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  2491. numbers should be factors of 60, so for example 5, 10, 15.
  2492. When this is larger than 1, you can still force an exact time stamp by using
  2493. a double prefix argument to a time stamp command like `C-c .' or `C-c !',
  2494. and by using a prefix arg to `S-up/down' to specify the exact number
  2495. of minutes to shift."
  2496. :group 'org-time
  2497. :get #'(lambda (var) ; Make sure both elements are there
  2498. (if (integerp (default-value var))
  2499. (list (default-value var) 5)
  2500. (default-value var)))
  2501. :type '(list
  2502. (integer :tag "when inserting times")
  2503. (integer :tag "when modifying times")))
  2504. ;; Normalize old customizations of this variable.
  2505. (when (integerp org-time-stamp-rounding-minutes)
  2506. (setq org-time-stamp-rounding-minutes
  2507. (list org-time-stamp-rounding-minutes
  2508. org-time-stamp-rounding-minutes)))
  2509. (defcustom org-display-custom-times nil
  2510. "Non-nil means overlay custom formats over all time stamps.
  2511. The formats are defined through the variable `org-time-stamp-custom-formats'.
  2512. To turn this on on a per-file basis, insert anywhere in the file:
  2513. #+STARTUP: customtime"
  2514. :group 'org-time
  2515. :set 'set-default
  2516. :type 'sexp)
  2517. (make-variable-buffer-local 'org-display-custom-times)
  2518. (defcustom org-time-stamp-custom-formats
  2519. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  2520. "Custom formats for time stamps. See `format-time-string' for the syntax.
  2521. These are overlaid over the default ISO format if the variable
  2522. `org-display-custom-times' is set. Time like %H:%M should be at the
  2523. end of the second format. The custom formats are also honored by export
  2524. commands, if custom time display is turned on at the time of export."
  2525. :group 'org-time
  2526. :type 'sexp)
  2527. (defun org-time-stamp-format (&optional long inactive)
  2528. "Get the right format for a time string."
  2529. (let ((f (if long (cdr org-time-stamp-formats)
  2530. (car org-time-stamp-formats))))
  2531. (if inactive
  2532. (concat "[" (substring f 1 -1) "]")
  2533. f)))
  2534. (defcustom org-time-clocksum-format
  2535. '(:days "%dd " :hours "%d" :require-hours t :minutes ":%02d" :require-minutes t)
  2536. "The format string used when creating CLOCKSUM lines.
  2537. This is also used when Org mode generates a time duration.
  2538. The value can be a single format string containing two
  2539. %-sequences, which will be filled with the number of hours and
  2540. minutes in that order.
  2541. Alternatively, the value can be a plist associating any of the
  2542. keys :years, :months, :weeks, :days, :hours or :minutes with
  2543. format strings. The time duration is formatted using only the
  2544. time components that are needed and concatenating the results.
  2545. If a time unit in absent, it falls back to the next smallest
  2546. unit.
  2547. The keys :require-years, :require-months, :require-days,
  2548. :require-weeks, :require-hours, :require-minutes are also
  2549. meaningful. A non-nil value for these keys indicates that the
  2550. corresponding time component should always be included, even if
  2551. its value is 0.
  2552. For example,
  2553. \(:days \"%dd\" :hours \"%d\" :require-hours t :minutes \":%02d\"
  2554. :require-minutes t)
  2555. means durations longer than a day will be expressed in days,
  2556. hours and minutes, and durations less than a day will always be
  2557. expressed in hours and minutes (even for durations less than an
  2558. hour).
  2559. The value
  2560. \(:days \"%dd\" :minutes \"%dm\")
  2561. means durations longer than a day will be expressed in days and
  2562. minutes, and durations less than a day will be expressed entirely
  2563. in minutes (even for durations longer than an hour)."
  2564. :group 'org-time
  2565. :group 'org-clock
  2566. :version "24.4"
  2567. :package-version '(Org . "8.0")
  2568. :type '(choice (string :tag "Format string")
  2569. (set :tag "Plist"
  2570. (group :inline t (const :tag "Years" :years)
  2571. (string :tag "Format string"))
  2572. (group :inline t
  2573. (const :tag "Always show years" :require-years)
  2574. (const t))
  2575. (group :inline t (const :tag "Months" :months)
  2576. (string :tag "Format string"))
  2577. (group :inline t
  2578. (const :tag "Always show months" :require-months)
  2579. (const t))
  2580. (group :inline t (const :tag "Weeks" :weeks)
  2581. (string :tag "Format string"))
  2582. (group :inline t
  2583. (const :tag "Always show weeks" :require-weeks)
  2584. (const t))
  2585. (group :inline t (const :tag "Days" :days)
  2586. (string :tag "Format string"))
  2587. (group :inline t
  2588. (const :tag "Always show days" :require-days)
  2589. (const t))
  2590. (group :inline t (const :tag "Hours" :hours)
  2591. (string :tag "Format string"))
  2592. (group :inline t
  2593. (const :tag "Always show hours" :require-hours)
  2594. (const t))
  2595. (group :inline t (const :tag "Minutes" :minutes)
  2596. (string :tag "Format string"))
  2597. (group :inline t
  2598. (const :tag "Always show minutes" :require-minutes)
  2599. (const t)))))
  2600. (defcustom org-time-clocksum-use-fractional nil
  2601. "When non-nil, \\[org-clock-display] uses fractional times.
  2602. See `org-time-clocksum-format' for more on time clock formats."
  2603. :group 'org-time
  2604. :group 'org-clock
  2605. :version "24.3"
  2606. :type 'boolean)
  2607. (defcustom org-time-clocksum-use-effort-durations nil
  2608. "When non-nil, \\[org-clock-display] uses effort durations.
  2609. E.g. by default, one day is considered to be a 8 hours effort,
  2610. so a task that has been clocked for 16 hours will be displayed
  2611. as during 2 days in the clock display or in the clocktable.
  2612. See `org-effort-durations' on how to set effort durations
  2613. and `org-time-clocksum-format' for more on time clock formats."
  2614. :group 'org-time
  2615. :group 'org-clock
  2616. :version "24.4"
  2617. :package-version '(Org . "8.0")
  2618. :type 'boolean)
  2619. (defcustom org-time-clocksum-fractional-format "%.2f"
  2620. "The format string used when creating CLOCKSUM lines,
  2621. or when Org mode generates a time duration, if
  2622. `org-time-clocksum-use-fractional' is enabled.
  2623. The value can be a single format string containing one
  2624. %-sequence, which will be filled with the number of hours as
  2625. a float.
  2626. Alternatively, the value can be a plist associating any of the
  2627. keys :years, :months, :weeks, :days, :hours or :minutes with
  2628. a format string. The time duration is formatted using the
  2629. largest time unit which gives a non-zero integer part. If all
  2630. specified formats have zero integer part, the smallest time unit
  2631. is used."
  2632. :group 'org-time
  2633. :type '(choice (string :tag "Format string")
  2634. (set (group :inline t (const :tag "Years" :years)
  2635. (string :tag "Format string"))
  2636. (group :inline t (const :tag "Months" :months)
  2637. (string :tag "Format string"))
  2638. (group :inline t (const :tag "Weeks" :weeks)
  2639. (string :tag "Format string"))
  2640. (group :inline t (const :tag "Days" :days)
  2641. (string :tag "Format string"))
  2642. (group :inline t (const :tag "Hours" :hours)
  2643. (string :tag "Format string"))
  2644. (group :inline t (const :tag "Minutes" :minutes)
  2645. (string :tag "Format string")))))
  2646. (defcustom org-deadline-warning-days 14
  2647. "Number of days before expiration during which a deadline becomes active.
  2648. This variable governs the display in sparse trees and in the agenda.
  2649. When 0 or negative, it means use this number (the absolute value of it)
  2650. even if a deadline has a different individual lead time specified.
  2651. Custom commands can set this variable in the options section."
  2652. :group 'org-time
  2653. :group 'org-agenda-daily/weekly
  2654. :type 'integer)
  2655. (defcustom org-scheduled-delay-days 0
  2656. "Number of days before a scheduled item becomes active.
  2657. This variable governs the display in sparse trees and in the agenda.
  2658. The default value (i.e. 0) means: don't delay scheduled item.
  2659. When negative, it means use this number (the absolute value of it)
  2660. even if a scheduled item has a different individual delay time
  2661. specified.
  2662. Custom commands can set this variable in the options section."
  2663. :group 'org-time
  2664. :group 'org-agenda-daily/weekly
  2665. :version "24.4"
  2666. :package-version '(Org . "8.0")
  2667. :type 'integer)
  2668. (defcustom org-read-date-prefer-future t
  2669. "Non-nil means assume future for incomplete date input from user.
  2670. This affects the following situations:
  2671. 1. The user gives a month but not a year.
  2672. For example, if it is April and you enter \"feb 2\", this will be read
  2673. as Feb 2, *next* year. \"May 5\", however, will be this year.
  2674. 2. The user gives a day, but no month.
  2675. For example, if today is the 15th, and you enter \"3\", Org-mode will
  2676. read this as the third of *next* month. However, if you enter \"17\",
  2677. it will be considered as *this* month.
  2678. If you set this variable to the symbol `time', then also the following
  2679. will work:
  2680. 3. If the user gives a time.
  2681. If the time is before now, it will be interpreted as tomorrow.
  2682. Currently none of this works for ISO week specifications.
  2683. When this option is nil, the current day, month and year will always be
  2684. used as defaults.
  2685. See also `org-agenda-jump-prefer-future'."
  2686. :group 'org-time
  2687. :type '(choice
  2688. (const :tag "Never" nil)
  2689. (const :tag "Check month and day" t)
  2690. (const :tag "Check month, day, and time" time)))
  2691. (defcustom org-agenda-jump-prefer-future 'org-read-date-prefer-future
  2692. "Should the agenda jump command prefer the future for incomplete dates?
  2693. The default is to do the same as configured in `org-read-date-prefer-future'.
  2694. But you can also set a deviating value here.
  2695. This may t or nil, or the symbol `org-read-date-prefer-future'."
  2696. :group 'org-agenda
  2697. :group 'org-time
  2698. :version "24.1"
  2699. :type '(choice
  2700. (const :tag "Use org-read-date-prefer-future"
  2701. org-read-date-prefer-future)
  2702. (const :tag "Never" nil)
  2703. (const :tag "Always" t)))
  2704. (defcustom org-read-date-force-compatible-dates t
  2705. "Should date/time prompt force dates that are guaranteed to work in Emacs?
  2706. Depending on the system Emacs is running on, certain dates cannot
  2707. be represented with the type used internally to represent time.
  2708. Dates between 1970-1-1 and 2038-1-1 can always be represented
  2709. correctly. Some systems allow for earlier dates, some for later,
  2710. some for both. One way to find out it to insert any date into an
  2711. Org buffer, putting the cursor on the year and hitting S-up and
  2712. S-down to test the range.
  2713. When this variable is set to t, the date/time prompt will not let
  2714. you specify dates outside the 1970-2037 range, so it is certain that
  2715. these dates will work in whatever version of Emacs you are
  2716. running, and also that you can move a file from one Emacs implementation
  2717. to another. WHenever Org is forcing the year for you, it will display
  2718. a message and beep.
  2719. When this variable is nil, Org will check if the date is
  2720. representable in the specific Emacs implementation you are using.
  2721. If not, it will force a year, usually the current year, and beep
  2722. to remind you. Currently this setting is not recommended because
  2723. the likelihood that you will open your Org files in an Emacs that
  2724. has limited date range is not negligible.
  2725. A workaround for this problem is to use diary sexp dates for time
  2726. stamps outside of this range."
  2727. :group 'org-time
  2728. :version "24.1"
  2729. :type 'boolean)
  2730. (defcustom org-read-date-display-live t
  2731. "Non-nil means display current interpretation of date prompt live.
  2732. This display will be in an overlay, in the minibuffer."
  2733. :group 'org-time
  2734. :type 'boolean)
  2735. (defcustom org-read-date-popup-calendar t
  2736. "Non-nil means pop up a calendar when prompting for a date.
  2737. In the calendar, the date can be selected with mouse-1. However, the
  2738. minibuffer will also be active, and you can simply enter the date as well.
  2739. When nil, only the minibuffer will be available."
  2740. :group 'org-time
  2741. :type 'boolean)
  2742. (org-defvaralias 'org-popup-calendar-for-date-prompt
  2743. 'org-read-date-popup-calendar)
  2744. (make-obsolete-variable
  2745. 'org-read-date-minibuffer-setup-hook
  2746. "Set `org-read-date-minibuffer-local-map' instead." "24.4")
  2747. (defcustom org-read-date-minibuffer-setup-hook nil
  2748. "Hook to be used to set up keys for the date/time interface.
  2749. Add key definitions to `minibuffer-local-map', which will be a
  2750. temporary copy.
  2751. WARNING: This option is obsolete, you should use
  2752. `org-read-date-minibuffer-local-map' to set up keys."
  2753. :group 'org-time
  2754. :type 'hook)
  2755. (defcustom org-extend-today-until 0
  2756. "The hour when your day really ends. Must be an integer.
  2757. This has influence for the following applications:
  2758. - When switching the agenda to \"today\". It it is still earlier than
  2759. the time given here, the day recognized as TODAY is actually yesterday.
  2760. - When a date is read from the user and it is still before the time given
  2761. here, the current date and time will be assumed to be yesterday, 23:59.
  2762. Also, timestamps inserted in capture templates follow this rule.
  2763. IMPORTANT: This is a feature whose implementation is and likely will
  2764. remain incomplete. Really, it is only here because past midnight seems to
  2765. be the favorite working time of John Wiegley :-)"
  2766. :group 'org-time
  2767. :type 'integer)
  2768. (defcustom org-use-effective-time nil
  2769. "If non-nil, consider `org-extend-today-until' when creating timestamps.
  2770. For example, if `org-extend-today-until' is 8, and it's 4am, then the
  2771. \"effective time\" of any timestamps between midnight and 8am will be
  2772. 23:59 of the previous day."
  2773. :group 'org-time
  2774. :version "24.1"
  2775. :type 'boolean)
  2776. (defcustom org-use-last-clock-out-time-as-effective-time nil
  2777. "When non-nil, use the last clock out time for `org-todo'.
  2778. Note that this option has precedence over the combined use of
  2779. `org-use-effective-time' and `org-extend-today-until'."
  2780. :group 'org-time
  2781. :version "24.4"
  2782. :package-version '(Org . "8.0")
  2783. :type 'boolean)
  2784. (defcustom org-edit-timestamp-down-means-later nil
  2785. "Non-nil means S-down will increase the time in a time stamp.
  2786. When nil, S-up will increase."
  2787. :group 'org-time
  2788. :type 'boolean)
  2789. (defcustom org-calendar-follow-timestamp-change t
  2790. "Non-nil means make the calendar window follow timestamp changes.
  2791. When a timestamp is modified and the calendar window is visible, it will be
  2792. moved to the new date."
  2793. :group 'org-time
  2794. :type 'boolean)
  2795. (defgroup org-tags nil
  2796. "Options concerning tags in Org-mode."
  2797. :tag "Org Tags"
  2798. :group 'org)
  2799. (defcustom org-tag-alist nil
  2800. "List of tags allowed in Org-mode files.
  2801. When this list is nil, Org-mode will base TAG input on what is already in the
  2802. buffer.
  2803. The value of this variable is an alist, the car of each entry must be a
  2804. keyword as a string, the cdr may be a character that is used to select
  2805. that tag through the fast-tag-selection interface.
  2806. See the manual for details."
  2807. :group 'org-tags
  2808. :type '(repeat
  2809. (choice
  2810. (cons (string :tag "Tag name")
  2811. (character :tag "Access char"))
  2812. (list :tag "Start radio group"
  2813. (const :startgroup)
  2814. (option (string :tag "Group description")))
  2815. (list :tag "Group tags delimiter"
  2816. (const :grouptags))
  2817. (list :tag "End radio group"
  2818. (const :endgroup)
  2819. (option (string :tag "Group description")))
  2820. (const :tag "New line" (:newline)))))
  2821. (defcustom org-tag-persistent-alist nil
  2822. "List of tags that will always appear in all Org-mode files.
  2823. This is in addition to any in buffer settings or customizations
  2824. of `org-tag-alist'.
  2825. When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
  2826. The value of this variable is an alist, the car of each entry must be a
  2827. keyword as a string, the cdr may be a character that is used to select
  2828. that tag through the fast-tag-selection interface.
  2829. See the manual for details.
  2830. To disable these tags on a per-file basis, insert anywhere in the file:
  2831. #+STARTUP: noptag"
  2832. :group 'org-tags
  2833. :type '(repeat
  2834. (choice
  2835. (cons (string :tag "Tag name")
  2836. (character :tag "Access char"))
  2837. (const :tag "Start radio group" (:startgroup))
  2838. (const :tag "Group tags delimiter" (:grouptags))
  2839. (const :tag "End radio group" (:endgroup))
  2840. (const :tag "New line" (:newline)))))
  2841. (defcustom org-complete-tags-always-offer-all-agenda-tags nil
  2842. "If non-nil, always offer completion for all tags of all agenda files.
  2843. Instead of customizing this variable directly, you might want to
  2844. set it locally for capture buffers, because there no list of
  2845. tags in that file can be created dynamically (there are none).
  2846. (add-hook 'org-capture-mode-hook
  2847. (lambda ()
  2848. (set (make-local-variable
  2849. 'org-complete-tags-always-offer-all-agenda-tags)
  2850. t)))"
  2851. :group 'org-tags
  2852. :version "24.1"
  2853. :type 'boolean)
  2854. (defvar org-file-tags nil
  2855. "List of tags that can be inherited by all entries in the file.
  2856. The tags will be inherited if the variable `org-use-tag-inheritance'
  2857. says they should be.
  2858. This variable is populated from #+FILETAGS lines.")
  2859. (defcustom org-use-fast-tag-selection 'auto
  2860. "Non-nil means use fast tag selection scheme.
  2861. This is a special interface to select and deselect tags with single keys.
  2862. When nil, fast selection is never used.
  2863. When the symbol `auto', fast selection is used if and only if selection
  2864. characters for tags have been configured, either through the variable
  2865. `org-tag-alist' or through a #+TAGS line in the buffer.
  2866. When t, fast selection is always used and selection keys are assigned
  2867. automatically if necessary."
  2868. :group 'org-tags
  2869. :type '(choice
  2870. (const :tag "Always" t)
  2871. (const :tag "Never" nil)
  2872. (const :tag "When selection characters are configured" auto)))
  2873. (defcustom org-fast-tag-selection-single-key nil
  2874. "Non-nil means fast tag selection exits after first change.
  2875. When nil, you have to press RET to exit it.
  2876. During fast tag selection, you can toggle this flag with `C-c'.
  2877. This variable can also have the value `expert'. In this case, the window
  2878. displaying the tags menu is not even shown, until you press C-c again."
  2879. :group 'org-tags
  2880. :type '(choice
  2881. (const :tag "No" nil)
  2882. (const :tag "Yes" t)
  2883. (const :tag "Expert" expert)))
  2884. (defvar org-fast-tag-selection-include-todo nil
  2885. "Non-nil means fast tags selection interface will also offer TODO states.
  2886. This is an undocumented feature, you should not rely on it.")
  2887. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  2888. "The column to which tags should be indented in a headline.
  2889. If this number is positive, it specifies the column. If it is negative,
  2890. it means that the tags should be flushright to that column. For example,
  2891. -80 works well for a normal 80 character screen.
  2892. When 0, place tags directly after headline text, with only one space in
  2893. between."
  2894. :group 'org-tags
  2895. :type 'integer)
  2896. (defcustom org-auto-align-tags t
  2897. "Non-nil keeps tags aligned when modifying headlines.
  2898. Some operations (i.e. demoting) change the length of a headline and
  2899. therefore shift the tags around. With this option turned on, after
  2900. each such operation the tags are again aligned to `org-tags-column'."
  2901. :group 'org-tags
  2902. :type 'boolean)
  2903. (defcustom org-use-tag-inheritance t
  2904. "Non-nil means tags in levels apply also for sublevels.
  2905. When nil, only the tags directly given in a specific line apply there.
  2906. This may also be a list of tags that should be inherited, or a regexp that
  2907. matches tags that should be inherited. Additional control is possible
  2908. with the variable `org-tags-exclude-from-inheritance' which gives an
  2909. explicit list of tags to be excluded from inheritance, even if the value of
  2910. `org-use-tag-inheritance' would select it for inheritance.
  2911. If this option is t, a match early-on in a tree can lead to a large
  2912. number of matches in the subtree when constructing the agenda or creating
  2913. a sparse tree. If you only want to see the first match in a tree during
  2914. a search, check out the variable `org-tags-match-list-sublevels'."
  2915. :group 'org-tags
  2916. :type '(choice
  2917. (const :tag "Not" nil)
  2918. (const :tag "Always" t)
  2919. (repeat :tag "Specific tags" (string :tag "Tag"))
  2920. (regexp :tag "Tags matched by regexp")))
  2921. (defcustom org-tags-exclude-from-inheritance nil
  2922. "List of tags that should never be inherited.
  2923. This is a way to exclude a few tags from inheritance. For way to do
  2924. the opposite, to actively allow inheritance for selected tags,
  2925. see the variable `org-use-tag-inheritance'."
  2926. :group 'org-tags
  2927. :type '(repeat (string :tag "Tag")))
  2928. (defun org-tag-inherit-p (tag)
  2929. "Check if TAG is one that should be inherited."
  2930. (cond
  2931. ((member tag org-tags-exclude-from-inheritance) nil)
  2932. ((eq org-use-tag-inheritance t) t)
  2933. ((not org-use-tag-inheritance) nil)
  2934. ((stringp org-use-tag-inheritance)
  2935. (string-match org-use-tag-inheritance tag))
  2936. ((listp org-use-tag-inheritance)
  2937. (member tag org-use-tag-inheritance))
  2938. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  2939. (defcustom org-tags-match-list-sublevels t
  2940. "Non-nil means list also sublevels of headlines matching a search.
  2941. This variable applies to tags/property searches, and also to stuck
  2942. projects because this search is based on a tags match as well.
  2943. When set to the symbol `indented', sublevels are indented with
  2944. leading dots.
  2945. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  2946. the sublevels of a headline matching a tag search often also match
  2947. the same search. Listing all of them can create very long lists.
  2948. Setting this variable to nil causes subtrees of a match to be skipped.
  2949. This variable is semi-obsolete and probably should always be true. It
  2950. is better to limit inheritance to certain tags using the variables
  2951. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  2952. :group 'org-tags
  2953. :type '(choice
  2954. (const :tag "No, don't list them" nil)
  2955. (const :tag "Yes, do list them" t)
  2956. (const :tag "List them, indented with leading dots" indented)))
  2957. (defcustom org-tags-sort-function nil
  2958. "When set, tags are sorted using this function as a comparator."
  2959. :group 'org-tags
  2960. :type '(choice
  2961. (const :tag "No sorting" nil)
  2962. (const :tag "Alphabetical" string<)
  2963. (const :tag "Reverse alphabetical" string>)
  2964. (function :tag "Custom function" nil)))
  2965. (defvar org-tags-history nil
  2966. "History of minibuffer reads for tags.")
  2967. (defvar org-last-tags-completion-table nil
  2968. "The last used completion table for tags.")
  2969. (defvar org-after-tags-change-hook nil
  2970. "Hook that is run after the tags in a line have changed.")
  2971. (defgroup org-properties nil
  2972. "Options concerning properties in Org-mode."
  2973. :tag "Org Properties"
  2974. :group 'org)
  2975. (defcustom org-property-format "%-10s %s"
  2976. "How property key/value pairs should be formatted by `indent-line'.
  2977. When `indent-line' hits a property definition, it will format the line
  2978. according to this format, mainly to make sure that the values are
  2979. lined-up with respect to each other."
  2980. :group 'org-properties
  2981. :type 'string)
  2982. (defcustom org-properties-postprocess-alist nil
  2983. "Alist of properties and functions to adjust inserted values.
  2984. Elements of this alist must be of the form
  2985. ([string] [function])
  2986. where [string] must be a property name and [function] must be a
  2987. lambda expression: this lambda expression must take one argument,
  2988. the value to adjust, and return the new value as a string.
  2989. For example, this element will allow the property \"Remaining\"
  2990. to be updated wrt the relation between the \"Effort\" property
  2991. and the clock summary:
  2992. ((\"Remaining\" (lambda(value)
  2993. (let ((clocksum (org-clock-sum-current-item))
  2994. (effort (org-duration-string-to-minutes
  2995. (org-entry-get (point) \"Effort\"))))
  2996. (org-minutes-to-clocksum-string (- effort clocksum))))))"
  2997. :group 'org-properties
  2998. :version "24.1"
  2999. :type '(alist :key-type (string :tag "Property")
  3000. :value-type (function :tag "Function")))
  3001. (defcustom org-use-property-inheritance nil
  3002. "Non-nil means properties apply also for sublevels.
  3003. This setting is chiefly used during property searches. Turning it on can
  3004. cause significant overhead when doing a search, which is why it is not
  3005. on by default.
  3006. When nil, only the properties directly given in the current entry count.
  3007. When t, every property is inherited. The value may also be a list of
  3008. properties that should have inheritance, or a regular expression matching
  3009. properties that should be inherited.
  3010. However, note that some special properties use inheritance under special
  3011. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  3012. and the properties ending in \"_ALL\" when they are used as descriptor
  3013. for valid values of a property.
  3014. Note for programmers:
  3015. When querying an entry with `org-entry-get', you can control if inheritance
  3016. should be used. By default, `org-entry-get' looks only at the local
  3017. properties. You can request inheritance by setting the inherit argument
  3018. to t (to force inheritance) or to `selective' (to respect the setting
  3019. in this variable)."
  3020. :group 'org-properties
  3021. :type '(choice
  3022. (const :tag "Not" nil)
  3023. (const :tag "Always" t)
  3024. (repeat :tag "Specific properties" (string :tag "Property"))
  3025. (regexp :tag "Properties matched by regexp")))
  3026. (defun org-property-inherit-p (property)
  3027. "Check if PROPERTY is one that should be inherited."
  3028. (cond
  3029. ((eq org-use-property-inheritance t) t)
  3030. ((not org-use-property-inheritance) nil)
  3031. ((stringp org-use-property-inheritance)
  3032. (string-match org-use-property-inheritance property))
  3033. ((listp org-use-property-inheritance)
  3034. (member property org-use-property-inheritance))
  3035. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  3036. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  3037. "The default column format, if no other format has been defined.
  3038. This variable can be set on the per-file basis by inserting a line
  3039. #+COLUMNS: %25ITEM ....."
  3040. :group 'org-properties
  3041. :type 'string)
  3042. (defcustom org-columns-ellipses ".."
  3043. "The ellipses to be used when a field in column view is truncated.
  3044. When this is the empty string, as many characters as possible are shown,
  3045. but then there will be no visual indication that the field has been truncated.
  3046. When this is a string of length N, the last N characters of a truncated
  3047. field are replaced by this string. If the column is narrower than the
  3048. ellipses string, only part of the ellipses string will be shown."
  3049. :group 'org-properties
  3050. :type 'string)
  3051. (defcustom org-columns-modify-value-for-display-function nil
  3052. "Function that modifies values for display in column view.
  3053. For example, it can be used to cut out a certain part from a time stamp.
  3054. The function must take 2 arguments:
  3055. column-title The title of the column (*not* the property name)
  3056. value The value that should be modified.
  3057. The function should return the value that should be displayed,
  3058. or nil if the normal value should be used."
  3059. :group 'org-properties
  3060. :type '(choice (const nil) (function)))
  3061. (defcustom org-effort-property "Effort"
  3062. "The property that is being used to keep track of effort estimates.
  3063. Effort estimates given in this property need to have the format H:MM."
  3064. :group 'org-properties
  3065. :group 'org-progress
  3066. :type '(string :tag "Property"))
  3067. (defconst org-global-properties-fixed
  3068. '(("VISIBILITY_ALL" . "folded children content all")
  3069. ("CLOCK_MODELINE_TOTAL_ALL" . "current today repeat all auto"))
  3070. "List of property/value pairs that can be inherited by any entry.
  3071. These are fixed values, for the preset properties. The user variable
  3072. that can be used to add to this list is `org-global-properties'.
  3073. The entries in this list are cons cells where the car is a property
  3074. name and cdr is a string with the value. If the value represents
  3075. multiple items like an \"_ALL\" property, separate the items by
  3076. spaces.")
  3077. (defcustom org-global-properties nil
  3078. "List of property/value pairs that can be inherited by any entry.
  3079. This list will be combined with the constant `org-global-properties-fixed'.
  3080. The entries in this list are cons cells where the car is a property
  3081. name and cdr is a string with the value.
  3082. You can set buffer-local values for the same purpose in the variable
  3083. `org-file-properties' this by adding lines like
  3084. #+PROPERTY: NAME VALUE"
  3085. :group 'org-properties
  3086. :type '(repeat
  3087. (cons (string :tag "Property")
  3088. (string :tag "Value"))))
  3089. (defvar org-file-properties nil
  3090. "List of property/value pairs that can be inherited by any entry.
  3091. Valid for the current buffer.
  3092. This variable is populated from #+PROPERTY lines.")
  3093. (make-variable-buffer-local 'org-file-properties)
  3094. (defgroup org-agenda nil
  3095. "Options concerning agenda views in Org-mode."
  3096. :tag "Org Agenda"
  3097. :group 'org)
  3098. (defvar org-category nil
  3099. "Variable used by org files to set a category for agenda display.
  3100. Such files should use a file variable to set it, for example
  3101. # -*- mode: org; org-category: \"ELisp\"
  3102. or contain a special line
  3103. #+CATEGORY: ELisp
  3104. If the file does not specify a category, then file's base name
  3105. is used instead.")
  3106. (make-variable-buffer-local 'org-category)
  3107. (put 'org-category 'safe-local-variable #'(lambda (x) (or (symbolp x) (stringp x))))
  3108. (defcustom org-agenda-files nil
  3109. "The files to be used for agenda display.
  3110. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  3111. \\[org-remove-file]. You can also use customize to edit the list.
  3112. If an entry is a directory, all files in that directory that are matched by
  3113. `org-agenda-file-regexp' will be part of the file list.
  3114. If the value of the variable is not a list but a single file name, then
  3115. the list of agenda files is actually stored and maintained in that file, one
  3116. agenda file per line. In this file paths can be given relative to
  3117. `org-directory'. Tilde expansion and environment variable substitution
  3118. are also made."
  3119. :group 'org-agenda
  3120. :type '(choice
  3121. (repeat :tag "List of files and directories" file)
  3122. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  3123. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  3124. "Regular expression to match files for `org-agenda-files'.
  3125. If any element in the list in that variable contains a directory instead
  3126. of a normal file, all files in that directory that are matched by this
  3127. regular expression will be included."
  3128. :group 'org-agenda
  3129. :type 'regexp)
  3130. (defcustom org-agenda-text-search-extra-files nil
  3131. "List of extra files to be searched by text search commands.
  3132. These files will be searched in addition to the agenda files by the
  3133. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  3134. Note that these files will only be searched for text search commands,
  3135. not for the other agenda views like todo lists, tag searches or the weekly
  3136. agenda. This variable is intended to list notes and possibly archive files
  3137. that should also be searched by these two commands.
  3138. In fact, if the first element in the list is the symbol `agenda-archives',
  3139. then all archive files of all agenda files will be added to the search
  3140. scope."
  3141. :group 'org-agenda
  3142. :type '(set :greedy t
  3143. (const :tag "Agenda Archives" agenda-archives)
  3144. (repeat :inline t (file))))
  3145. (org-defvaralias 'org-agenda-multi-occur-extra-files
  3146. 'org-agenda-text-search-extra-files)
  3147. (defcustom org-agenda-skip-unavailable-files nil
  3148. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  3149. A nil value means to remove them, after a query, from the list."
  3150. :group 'org-agenda
  3151. :type 'boolean)
  3152. (defcustom org-calendar-to-agenda-key [?c]
  3153. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  3154. The command `org-calendar-goto-agenda' will be bound to this key. The
  3155. default is the character `c' because then `c' can be used to switch back and
  3156. forth between agenda and calendar."
  3157. :group 'org-agenda
  3158. :type 'sexp)
  3159. (defcustom org-calendar-insert-diary-entry-key [?i]
  3160. "The key to be installed in `calendar-mode-map' for adding diary entries.
  3161. This option is irrelevant until `org-agenda-diary-file' has been configured
  3162. to point to an Org-mode file. When that is the case, the command
  3163. `org-agenda-diary-entry' will be bound to the key given here, by default
  3164. `i'. In the calendar, `i' normally adds entries to `diary-file'. So
  3165. if you want to continue doing this, you need to change this to a different
  3166. key."
  3167. :group 'org-agenda
  3168. :type 'sexp)
  3169. (defcustom org-agenda-diary-file 'diary-file
  3170. "File to which to add new entries with the `i' key in agenda and calendar.
  3171. When this is the symbol `diary-file', the functionality in the Emacs
  3172. calendar will be used to add entries to the `diary-file'. But when this
  3173. points to a file, `org-agenda-diary-entry' will be used instead."
  3174. :group 'org-agenda
  3175. :type '(choice
  3176. (const :tag "The standard Emacs diary file" diary-file)
  3177. (file :tag "Special Org file diary entries")))
  3178. (eval-after-load "calendar"
  3179. '(progn
  3180. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  3181. 'org-calendar-goto-agenda)
  3182. (add-hook 'calendar-mode-hook
  3183. (lambda ()
  3184. (unless (eq org-agenda-diary-file 'diary-file)
  3185. (define-key calendar-mode-map
  3186. org-calendar-insert-diary-entry-key
  3187. 'org-agenda-diary-entry))))))
  3188. (defgroup org-latex nil
  3189. "Options for embedding LaTeX code into Org-mode."
  3190. :tag "Org LaTeX"
  3191. :group 'org)
  3192. (defcustom org-format-latex-options
  3193. '(:foreground default :background default :scale 1.0
  3194. :html-foreground "Black" :html-background "Transparent"
  3195. :html-scale 1.0 :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  3196. "Options for creating images from LaTeX fragments.
  3197. This is a property list with the following properties:
  3198. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  3199. `default' means use the foreground of the default face.
  3200. `auto' means use the foreground from the text face.
  3201. :background the background color, or \"Transparent\".
  3202. `default' means use the background of the default face.
  3203. `auto' means use the background from the text face.
  3204. :scale a scaling factor for the size of the images, to get more pixels
  3205. :html-foreground, :html-background, :html-scale
  3206. the same numbers for HTML export.
  3207. :matchers a list indicating which matchers should be used to
  3208. find LaTeX fragments. Valid members of this list are:
  3209. \"begin\" find environments
  3210. \"$1\" find single characters surrounded by $.$
  3211. \"$\" find math expressions surrounded by $...$
  3212. \"$$\" find math expressions surrounded by $$....$$
  3213. \"\\(\" find math expressions surrounded by \\(...\\)
  3214. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  3215. :group 'org-latex
  3216. :type 'plist)
  3217. (defcustom org-format-latex-signal-error t
  3218. "Non-nil means signal an error when image creation of LaTeX snippets fails.
  3219. When nil, just push out a message."
  3220. :group 'org-latex
  3221. :version "24.1"
  3222. :type 'boolean)
  3223. (defcustom org-latex-to-mathml-jar-file nil
  3224. "Value of\"%j\" in `org-latex-to-mathml-convert-command'.
  3225. Use this to specify additional executable file say a jar file.
  3226. When using MathToWeb as the converter, specify the full-path to
  3227. your mathtoweb.jar file."
  3228. :group 'org-latex
  3229. :version "24.1"
  3230. :type '(choice
  3231. (const :tag "None" nil)
  3232. (file :tag "JAR file" :must-match t)))
  3233. (defcustom org-latex-to-mathml-convert-command nil
  3234. "Command to convert LaTeX fragments to MathML.
  3235. Replace format-specifiers in the command as noted below and use
  3236. `shell-command' to convert LaTeX to MathML.
  3237. %j: Executable file in fully expanded form as specified by
  3238. `org-latex-to-mathml-jar-file'.
  3239. %I: Input LaTeX file in fully expanded form
  3240. %o: Output MathML file
  3241. This command is used by `org-create-math-formula'.
  3242. When using MathToWeb as the converter, set this to
  3243. \"java -jar %j -unicode -force -df %o %I\"."
  3244. :group 'org-latex
  3245. :version "24.1"
  3246. :type '(choice
  3247. (const :tag "None" nil)
  3248. (string :tag "\nShell command")))
  3249. (defcustom org-latex-create-formula-image-program 'dvipng
  3250. "Program to convert LaTeX fragments with.
  3251. dvipng Process the LaTeX fragments to dvi file, then convert
  3252. dvi files to png files using dvipng.
  3253. This will also include processing of non-math environments.
  3254. imagemagick Convert the LaTeX fragments to pdf files and use imagemagick
  3255. to convert pdf files to png files"
  3256. :group 'org-latex
  3257. :version "24.1"
  3258. :type '(choice
  3259. (const :tag "dvipng" dvipng)
  3260. (const :tag "imagemagick" imagemagick)))
  3261. (defcustom org-latex-preview-ltxpng-directory "ltxpng/"
  3262. "Path to store latex preview images.
  3263. A relative path here creates many directories relative to the
  3264. processed org files paths. An absolute path puts all preview
  3265. images at the same place."
  3266. :group 'org-latex
  3267. :version "24.3"
  3268. :type 'string)
  3269. (defun org-format-latex-mathml-available-p ()
  3270. "Return t if `org-latex-to-mathml-convert-command' is usable."
  3271. (save-match-data
  3272. (when (and (boundp 'org-latex-to-mathml-convert-command)
  3273. org-latex-to-mathml-convert-command)
  3274. (let ((executable (car (split-string
  3275. org-latex-to-mathml-convert-command))))
  3276. (when (executable-find executable)
  3277. (if (string-match
  3278. "%j" org-latex-to-mathml-convert-command)
  3279. (file-readable-p org-latex-to-mathml-jar-file)
  3280. t))))))
  3281. (defcustom org-format-latex-header "\\documentclass{article}
  3282. \\usepackage[usenames]{color}
  3283. \\usepackage{amsmath}
  3284. \\usepackage[mathscr]{eucal}
  3285. \\pagestyle{empty} % do not remove
  3286. \[PACKAGES]
  3287. \[DEFAULT-PACKAGES]
  3288. % The settings below are copied from fullpage.sty
  3289. \\setlength{\\textwidth}{\\paperwidth}
  3290. \\addtolength{\\textwidth}{-3cm}
  3291. \\setlength{\\oddsidemargin}{1.5cm}
  3292. \\addtolength{\\oddsidemargin}{-2.54cm}
  3293. \\setlength{\\evensidemargin}{\\oddsidemargin}
  3294. \\setlength{\\textheight}{\\paperheight}
  3295. \\addtolength{\\textheight}{-\\headheight}
  3296. \\addtolength{\\textheight}{-\\headsep}
  3297. \\addtolength{\\textheight}{-\\footskip}
  3298. \\addtolength{\\textheight}{-3cm}
  3299. \\setlength{\\topmargin}{1.5cm}
  3300. \\addtolength{\\topmargin}{-2.54cm}"
  3301. "The document header used for processing LaTeX fragments.
  3302. It is imperative that this header make sure that no page number
  3303. appears on the page. The package defined in the variables
  3304. `org-latex-default-packages-alist' and `org-latex-packages-alist'
  3305. will either replace the placeholder \"[PACKAGES]\" in this
  3306. header, or they will be appended."
  3307. :group 'org-latex
  3308. :type 'string)
  3309. (defun org-set-packages-alist (var val)
  3310. "Set the packages alist and make sure it has 3 elements per entry."
  3311. (set var (mapcar (lambda (x)
  3312. (if (and (consp x) (= (length x) 2))
  3313. (list (car x) (nth 1 x) t)
  3314. x))
  3315. val)))
  3316. (defun org-get-packages-alist (var)
  3317. "Get the packages alist and make sure it has 3 elements per entry."
  3318. (mapcar (lambda (x)
  3319. (if (and (consp x) (= (length x) 2))
  3320. (list (car x) (nth 1 x) t)
  3321. x))
  3322. (default-value var)))
  3323. (defcustom org-latex-default-packages-alist
  3324. '(("AUTO" "inputenc" t)
  3325. ("T1" "fontenc" t)
  3326. ("" "fixltx2e" nil)
  3327. ("" "graphicx" t)
  3328. ("" "longtable" nil)
  3329. ("" "float" nil)
  3330. ("" "wrapfig" nil)
  3331. ("" "soul" t)
  3332. ("" "textcomp" t)
  3333. ("" "marvosym" t)
  3334. ("" "wasysym" t)
  3335. ("" "latexsym" t)
  3336. ("" "amssymb" t)
  3337. ("" "amstext" nil)
  3338. ("" "hyperref" nil)
  3339. "\\tolerance=1000")
  3340. "Alist of default packages to be inserted in the header.
  3341. Change this only if one of the packages here causes an
  3342. incompatibility with another package you are using.
  3343. The packages in this list are needed by one part or another of
  3344. Org mode to function properly:
  3345. - inputenc, fontenc: for basic font and character selection
  3346. - amstext: for subscript and superscript
  3347. - textcomp, marvosymb, wasysym, latexsym, amssym: for various
  3348. symbols used for interpreting the entities in `org-entities'.
  3349. You can skip some of these packages if you don't use any of the
  3350. symbols in it.
  3351. - graphicx: for including images
  3352. - float, wrapfig: for figure placement
  3353. - longtable: for long tables
  3354. - hyperref: for cross references
  3355. Therefore you should not modify this variable unless you know
  3356. what you are doing. The one reason to change it anyway is that
  3357. you might be loading some other package that conflicts with one
  3358. of the default packages. Each cell is of the format
  3359. \( \"options\" \"package\" snippet-flag). If SNIPPET-FLAG is t,
  3360. the package also needs to be included when compiling LaTeX
  3361. snippets into images for inclusion into non-LaTeX output."
  3362. :group 'org-latex
  3363. :group 'org-export-latex
  3364. :set 'org-set-packages-alist
  3365. :get 'org-get-packages-alist
  3366. :version "24.1"
  3367. :type '(repeat
  3368. (choice
  3369. (list :tag "options/package pair"
  3370. (string :tag "options")
  3371. (string :tag "package")
  3372. (boolean :tag "Snippet"))
  3373. (string :tag "A line of LaTeX"))))
  3374. (defcustom org-latex-packages-alist nil
  3375. "Alist of packages to be inserted in every LaTeX header.
  3376. These will be inserted after `org-latex-default-packages-alist'.
  3377. Each cell is of the format:
  3378. \(\"options\" \"package\" snippet-flag)
  3379. SNIPPET-FLAG, when t, indicates that this package is also needed
  3380. when turning LaTeX snippets into images for inclusion into
  3381. non-LaTeX output.
  3382. Make sure that you only list packages here which:
  3383. - you want in every file
  3384. - do not conflict with the setup in `org-format-latex-header'.
  3385. - do not conflict with the default packages in
  3386. `org-latex-default-packages-alist'."
  3387. :group 'org-latex
  3388. :group 'org-export-latex
  3389. :set 'org-set-packages-alist
  3390. :get 'org-get-packages-alist
  3391. :type '(repeat
  3392. (choice
  3393. (list :tag "options/package pair"
  3394. (string :tag "options")
  3395. (string :tag "package")
  3396. (boolean :tag "Snippet"))
  3397. (string :tag "A line of LaTeX"))))
  3398. (defgroup org-appearance nil
  3399. "Settings for Org-mode appearance."
  3400. :tag "Org Appearance"
  3401. :group 'org)
  3402. (defcustom org-level-color-stars-only nil
  3403. "Non-nil means fontify only the stars in each headline.
  3404. When nil, the entire headline is fontified.
  3405. Changing it requires restart of `font-lock-mode' to become effective
  3406. also in regions already fontified."
  3407. :group 'org-appearance
  3408. :type 'boolean)
  3409. (defcustom org-hide-leading-stars nil
  3410. "Non-nil means hide the first N-1 stars in a headline.
  3411. This works by using the face `org-hide' for these stars. This
  3412. face is white for a light background, and black for a dark
  3413. background. You may have to customize the face `org-hide' to
  3414. make this work.
  3415. Changing it requires restart of `font-lock-mode' to become effective
  3416. also in regions already fontified.
  3417. You may also set this on a per-file basis by adding one of the following
  3418. lines to the buffer:
  3419. #+STARTUP: hidestars
  3420. #+STARTUP: showstars"
  3421. :group 'org-appearance
  3422. :type 'boolean)
  3423. (defcustom org-hidden-keywords nil
  3424. "List of symbols corresponding to keywords to be hidden the org buffer.
  3425. For example, a value '(title) for this list will make the document's title
  3426. appear in the buffer without the initial #+TITLE: keyword."
  3427. :group 'org-appearance
  3428. :version "24.1"
  3429. :type '(set (const :tag "#+AUTHOR" author)
  3430. (const :tag "#+DATE" date)
  3431. (const :tag "#+EMAIL" email)
  3432. (const :tag "#+TITLE" title)))
  3433. (defcustom org-custom-properties nil
  3434. "List of properties (as strings) with a special meaning.
  3435. The default use of these custom properties is to let the user
  3436. hide them with `org-toggle-custom-properties-visibility'."
  3437. :group 'org-properties
  3438. :group 'org-appearance
  3439. :version "24.3"
  3440. :type '(repeat (string :tag "Property Name")))
  3441. (defcustom org-fontify-done-headline nil
  3442. "Non-nil means change the face of a headline if it is marked DONE.
  3443. Normally, only the TODO/DONE keyword indicates the state of a headline.
  3444. When this is non-nil, the headline after the keyword is set to the
  3445. `org-headline-done' as an additional indication."
  3446. :group 'org-appearance
  3447. :type 'boolean)
  3448. (defcustom org-fontify-emphasized-text t
  3449. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  3450. Changing this variable requires a restart of Emacs to take effect."
  3451. :group 'org-appearance
  3452. :type 'boolean)
  3453. (defcustom org-fontify-whole-heading-line nil
  3454. "Non-nil means fontify the whole line for headings.
  3455. This is useful when setting a background color for the
  3456. org-level-* faces."
  3457. :group 'org-appearance
  3458. :type 'boolean)
  3459. (defcustom org-highlight-latex-and-related nil
  3460. "Non-nil means highlight LaTeX related syntax in the buffer.
  3461. When non nil, the value should be a list containing any of the
  3462. following symbols:
  3463. `latex' Highlight LaTeX snippets and environments.
  3464. `script' Highlight subscript and superscript.
  3465. `entities' Highlight entities."
  3466. :group 'org-appearance
  3467. :version "24.4"
  3468. :package-version '(Org . "8.0")
  3469. :type '(choice
  3470. (const :tag "No highlighting" nil)
  3471. (set :greedy t :tag "Highlight"
  3472. (const :tag "LaTeX snippets and environments" latex)
  3473. (const :tag "Subscript and superscript" script)
  3474. (const :tag "Entities" entities))))
  3475. (defcustom org-hide-emphasis-markers nil
  3476. "Non-nil mean font-lock should hide the emphasis marker characters."
  3477. :group 'org-appearance
  3478. :type 'boolean)
  3479. (defcustom org-pretty-entities nil
  3480. "Non-nil means show entities as UTF8 characters.
  3481. When nil, the \\name form remains in the buffer."
  3482. :group 'org-appearance
  3483. :version "24.1"
  3484. :type 'boolean)
  3485. (defcustom org-pretty-entities-include-sub-superscripts t
  3486. "Non-nil means, pretty entity display includes formatting sub/superscripts."
  3487. :group 'org-appearance
  3488. :version "24.1"
  3489. :type 'boolean)
  3490. (defvar org-emph-re nil
  3491. "Regular expression for matching emphasis.
  3492. After a match, the match groups contain these elements:
  3493. 0 The match of the full regular expression, including the characters
  3494. before and after the proper match
  3495. 1 The character before the proper match, or empty at beginning of line
  3496. 2 The proper match, including the leading and trailing markers
  3497. 3 The leading marker like * or /, indicating the type of highlighting
  3498. 4 The text between the emphasis markers, not including the markers
  3499. 5 The character after the match, empty at the end of a line")
  3500. (defvar org-verbatim-re nil
  3501. "Regular expression for matching verbatim text.")
  3502. (defvar org-emphasis-regexp-components) ; defined just below
  3503. (defvar org-emphasis-alist) ; defined just below
  3504. (defun org-set-emph-re (var val)
  3505. "Set variable and compute the emphasis regular expression."
  3506. (set var val)
  3507. (when (and (boundp 'org-emphasis-alist)
  3508. (boundp 'org-emphasis-regexp-components)
  3509. org-emphasis-alist org-emphasis-regexp-components)
  3510. (let* ((e org-emphasis-regexp-components)
  3511. (pre (car e))
  3512. (post (nth 1 e))
  3513. (border (nth 2 e))
  3514. (body (nth 3 e))
  3515. (nl (nth 4 e))
  3516. (body1 (concat body "*?"))
  3517. (markers (mapconcat 'car org-emphasis-alist ""))
  3518. (vmarkers (mapconcat
  3519. (lambda (x) (if (eq (nth 2 x) 'verbatim) (car x) ""))
  3520. org-emphasis-alist "")))
  3521. ;; make sure special characters appear at the right position in the class
  3522. (if (string-match "\\^" markers)
  3523. (setq markers (concat (replace-match "" t t markers) "^")))
  3524. (if (string-match "-" markers)
  3525. (setq markers (concat (replace-match "" t t markers) "-")))
  3526. (if (string-match "\\^" vmarkers)
  3527. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  3528. (if (string-match "-" vmarkers)
  3529. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  3530. (if (> nl 0)
  3531. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  3532. (int-to-string nl) "\\}")))
  3533. ;; Make the regexp
  3534. (setq org-emph-re
  3535. (concat "\\([" pre "]\\|^\\)"
  3536. "\\("
  3537. "\\([" markers "]\\)"
  3538. "\\("
  3539. "[^" border "]\\|"
  3540. "[^" border "]"
  3541. body1
  3542. "[^" border "]"
  3543. "\\)"
  3544. "\\3\\)"
  3545. "\\([" post "]\\|$\\)"))
  3546. (setq org-verbatim-re
  3547. (concat "\\([" pre "]\\|^\\)"
  3548. "\\("
  3549. "\\([" vmarkers "]\\)"
  3550. "\\("
  3551. "[^" border "]\\|"
  3552. "[^" border "]"
  3553. body1
  3554. "[^" border "]"
  3555. "\\)"
  3556. "\\3\\)"
  3557. "\\([" post "]\\|$\\)")))))
  3558. ;; This used to be a defcustom (Org <8.0) but allowing the users to
  3559. ;; set this option proved cumbersome. See this message/thread:
  3560. ;; http://article.gmane.org/gmane.emacs.orgmode/68681
  3561. (defvar org-emphasis-regexp-components
  3562. '(" \t('\"{" "- \t.,:!?;'\")}\\" " \t\r\n,\"'" "." 1)
  3563. "Components used to build the regular expression for emphasis.
  3564. This is a list with five entries. Terminology: In an emphasis string
  3565. like \" *strong word* \", we call the initial space PREMATCH, the final
  3566. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  3567. and \"trong wor\" is the body. The different components in this variable
  3568. specify what is allowed/forbidden in each part:
  3569. pre Chars allowed as prematch. Beginning of line will be allowed too.
  3570. post Chars allowed as postmatch. End of line will be allowed too.
  3571. border The chars *forbidden* as border characters.
  3572. body-regexp A regexp like \".\" to match a body character. Don't use
  3573. non-shy groups here, and don't allow newline here.
  3574. newline The maximum number of newlines allowed in an emphasis exp.
  3575. You need to reload Org or to restart Emacs after customizing this.")
  3576. (defcustom org-emphasis-alist
  3577. `(("*" bold)
  3578. ("/" italic)
  3579. ("_" underline)
  3580. ("=" org-code verbatim)
  3581. ("~" org-verbatim verbatim)
  3582. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))))
  3583. "Alist of characters and faces to emphasize text.
  3584. Text starting and ending with a special character will be emphasized,
  3585. for example *bold*, _underlined_ and /italic/. This variable sets the
  3586. marker characters and the face to be used by font-lock for highlighting
  3587. in Org-mode Emacs buffers.
  3588. You need to reload Org or to restart Emacs after customizing this."
  3589. :group 'org-appearance
  3590. :set 'org-set-emph-re
  3591. :version "24.4"
  3592. :package-version '(Org . "8.0")
  3593. :type '(repeat
  3594. (list
  3595. (string :tag "Marker character")
  3596. (choice
  3597. (face :tag "Font-lock-face")
  3598. (plist :tag "Face property list"))
  3599. (option (const verbatim)))))
  3600. (defvar org-protecting-blocks
  3601. '("src" "example" "latex" "ascii" "html" "ditaa" "dot" "r" "R")
  3602. "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
  3603. This is needed for font-lock setup.")
  3604. ;;; Miscellaneous options
  3605. (defgroup org-completion nil
  3606. "Completion in Org-mode."
  3607. :tag "Org Completion"
  3608. :group 'org)
  3609. (defcustom org-completion-use-ido nil
  3610. "Non-nil means use ido completion wherever possible.
  3611. Note that `ido-mode' must be active for this variable to be relevant.
  3612. If you decide to turn this variable on, you might well want to turn off
  3613. `org-outline-path-complete-in-steps'.
  3614. See also `org-completion-use-iswitchb'."
  3615. :group 'org-completion
  3616. :type 'boolean)
  3617. (defcustom org-completion-use-iswitchb nil
  3618. "Non-nil means use iswitchb completion wherever possible.
  3619. Note that `iswitchb-mode' must be active for this variable to be relevant.
  3620. If you decide to turn this variable on, you might well want to turn off
  3621. `org-outline-path-complete-in-steps'.
  3622. Note that this variable has only an effect if `org-completion-use-ido' is nil."
  3623. :group 'org-completion
  3624. :type 'boolean)
  3625. (defcustom org-completion-fallback-command 'hippie-expand
  3626. "The expansion command called by \\[pcomplete] in normal context.
  3627. Normal means, no org-mode-specific context."
  3628. :group 'org-completion
  3629. :type 'function)
  3630. ;;; Functions and variables from their packages
  3631. ;; Declared here to avoid compiler warnings
  3632. ;; XEmacs only
  3633. (defvar outline-mode-menu-heading)
  3634. (defvar outline-mode-menu-show)
  3635. (defvar outline-mode-menu-hide)
  3636. (defvar zmacs-regions) ; XEmacs regions
  3637. ;; Emacs only
  3638. (defvar mark-active)
  3639. ;; Various packages
  3640. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  3641. (declare-function calendar-forward-day "cal-move" (arg))
  3642. (declare-function calendar-goto-date "cal-move" (date))
  3643. (declare-function calendar-goto-today "cal-move" ())
  3644. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  3645. (defvar calc-embedded-close-formula)
  3646. (defvar calc-embedded-open-formula)
  3647. (declare-function cdlatex-tab "ext:cdlatex" ())
  3648. (declare-function cdlatex-compute-tables "ext:cdlatex" ())
  3649. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  3650. (defvar font-lock-unfontify-region-function)
  3651. (declare-function iswitchb-read-buffer "iswitchb"
  3652. (prompt &optional default require-match start matches-set))
  3653. (defvar iswitchb-temp-buflist)
  3654. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  3655. (defvar org-agenda-tags-todo-honor-ignore-options)
  3656. (declare-function org-agenda-skip "org-agenda" ())
  3657. (declare-function
  3658. org-agenda-format-item "org-agenda"
  3659. (extra txt &optional level category tags dotime noprefix remove-re habitp))
  3660. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  3661. (declare-function org-agenda-change-all-lines "org-agenda"
  3662. (newhead hdmarker &optional fixface just-this))
  3663. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  3664. (declare-function org-agenda-maybe-redo "org-agenda" ())
  3665. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  3666. (beg end))
  3667. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  3668. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  3669. "org-agenda" (&optional end))
  3670. (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
  3671. (declare-function org-inlinetask-in-task-p "org-inlinetask" ())
  3672. (declare-function org-inlinetask-goto-beginning "org-inlinetask" ())
  3673. (declare-function org-inlinetask-goto-end "org-inlinetask" ())
  3674. (declare-function org-indent-mode "org-indent" (&optional arg))
  3675. (declare-function parse-time-string "parse-time" (string))
  3676. (declare-function org-attach-reveal "org-attach" (&optional if-exists))
  3677. (declare-function orgtbl-send-table "org-table" (&optional maybe))
  3678. (defvar remember-data-file)
  3679. (defvar texmathp-why)
  3680. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  3681. (declare-function table--at-cell-p "table" (position &optional object at-column))
  3682. (defvar org-latex-regexps)
  3683. ;;; Autoload and prepare some org modules
  3684. ;; Some table stuff that needs to be defined here, because it is used
  3685. ;; by the functions setting up org-mode or checking for table context.
  3686. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  3687. "Detect an org-type or table-type table.")
  3688. (defconst org-table-line-regexp "^[ \t]*|"
  3689. "Detect an org-type table line.")
  3690. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  3691. "Detect an org-type table line.")
  3692. (defconst org-table-hline-regexp "^[ \t]*|-"
  3693. "Detect an org-type table hline.")
  3694. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  3695. "Detect a table-type table hline.")
  3696. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  3697. "Detect the first line outside a table when searching from within it.
  3698. This works for both table types.")
  3699. ;; Autoload the functions in org-table.el that are needed by functions here.
  3700. (eval-and-compile
  3701. (org-autoload "org-table"
  3702. '(org-table-begin org-table-blank-field org-table-end)))
  3703. (defconst org-TBLFM-regexp "^[ \t]*#\\+TBLFM: "
  3704. "Detect a #+TBLFM line.")
  3705. ;;;###autoload
  3706. (defun turn-on-orgtbl ()
  3707. "Unconditionally turn on `orgtbl-mode'."
  3708. (require 'org-table)
  3709. (orgtbl-mode 1))
  3710. (defun org-at-table-p (&optional table-type)
  3711. "Return t if the cursor is inside an org-type table.
  3712. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  3713. (if org-enable-table-editor
  3714. (save-excursion
  3715. (beginning-of-line 1)
  3716. (looking-at (if table-type org-table-any-line-regexp
  3717. org-table-line-regexp)))
  3718. nil))
  3719. (defsubst org-table-p () (org-at-table-p))
  3720. (defun org-at-table.el-p ()
  3721. "Return t if and only if we are at a table.el table."
  3722. (and (org-at-table-p 'any)
  3723. (save-excursion
  3724. (goto-char (org-table-begin 'any))
  3725. (looking-at org-table1-hline-regexp))))
  3726. (defun org-table-recognize-table.el ()
  3727. "If there is a table.el table nearby, recognize it and move into it."
  3728. (if org-table-tab-recognizes-table.el
  3729. (if (org-at-table.el-p)
  3730. (progn
  3731. (beginning-of-line 1)
  3732. (if (looking-at org-table-dataline-regexp)
  3733. nil
  3734. (if (looking-at org-table1-hline-regexp)
  3735. (progn
  3736. (beginning-of-line 2)
  3737. (if (looking-at org-table-any-border-regexp)
  3738. (beginning-of-line -1)))))
  3739. (if (re-search-forward "|" (org-table-end t) t)
  3740. (progn
  3741. (require 'table)
  3742. (if (table--at-cell-p (point))
  3743. t
  3744. (message "recognizing table.el table...")
  3745. (table-recognize-table)
  3746. (message "recognizing table.el table...done")))
  3747. (error "This should not happen"))
  3748. t)
  3749. nil)
  3750. nil))
  3751. (defun org-at-table-hline-p ()
  3752. "Return t if the cursor is inside a hline in a table."
  3753. (if org-enable-table-editor
  3754. (save-excursion
  3755. (beginning-of-line 1)
  3756. (looking-at org-table-hline-regexp))
  3757. nil))
  3758. (defun org-table-map-tables (function &optional quietly)
  3759. "Apply FUNCTION to the start of all tables in the buffer."
  3760. (save-excursion
  3761. (save-restriction
  3762. (widen)
  3763. (goto-char (point-min))
  3764. (while (re-search-forward org-table-any-line-regexp nil t)
  3765. (unless quietly
  3766. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size))))
  3767. (beginning-of-line 1)
  3768. (when (and (looking-at org-table-line-regexp)
  3769. ;; Exclude tables in src/example/verbatim/clocktable blocks
  3770. (not (org-in-block-p '("src" "example" "verbatim" "clocktable"))))
  3771. (save-excursion (funcall function))
  3772. (or (looking-at org-table-line-regexp)
  3773. (forward-char 1)))
  3774. (re-search-forward org-table-any-border-regexp nil 1))))
  3775. (unless quietly (message "Mapping tables: done")))
  3776. ;; Declare and autoload functions from ox.el and al.
  3777. (declare-function org-export-get-environment "ox"
  3778. (&optional backend subtreep ext-plist))
  3779. (declare-function org-latex-guess-inputenc "ox-latex" (header))
  3780. ;; Declare and autoload functions from org-agenda.el
  3781. (eval-and-compile
  3782. (org-autoload "org-agenda"
  3783. '(org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
  3784. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock" (beg end))
  3785. (declare-function org-clock-update-mode-line "org-clock" ())
  3786. (declare-function org-resolve-clocks "org-clock"
  3787. (&optional also-non-dangling-p prompt last-valid))
  3788. (defun org-at-TBLFM-p (&optional pos)
  3789. "Return t when point (or POS) is in #+TBLFM line."
  3790. (save-excursion
  3791. (let ((pos pos)))
  3792. (goto-char (or pos (point)))
  3793. (beginning-of-line 1)
  3794. (looking-at org-TBLFM-regexp)))
  3795. (defvar org-clock-start-time)
  3796. (defvar org-clock-marker (make-marker)
  3797. "Marker recording the last clock-in.")
  3798. (defvar org-clock-hd-marker (make-marker)
  3799. "Marker recording the last clock-in, but the headline position.")
  3800. (defvar org-clock-heading ""
  3801. "The heading of the current clock entry.")
  3802. (defun org-clock-is-active ()
  3803. "Return non-nil if clock is currently running.
  3804. The return value is actually the clock marker."
  3805. (marker-buffer org-clock-marker))
  3806. (eval-and-compile
  3807. (org-autoload "org-clock" '(org-clock-remove-overlays
  3808. org-clock-update-time-maybe
  3809. org-clocktable-shift)))
  3810. (defun org-check-running-clock ()
  3811. "Check if the current buffer contains the running clock.
  3812. If yes, offer to stop it and to save the buffer with the changes."
  3813. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3814. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  3815. (buffer-name))))
  3816. (org-clock-out)
  3817. (when (y-or-n-p "Save changed buffer?")
  3818. (save-buffer))))
  3819. (defun org-clocktable-try-shift (dir n)
  3820. "Check if this line starts a clock table, if yes, shift the time block."
  3821. (when (org-match-line "^[ \t]*#\\+BEGIN:[ \t]+clocktable\\>")
  3822. (org-clocktable-shift dir n)))
  3823. ;;;###autoload
  3824. (defun org-clock-persistence-insinuate ()
  3825. "Set up hooks for clock persistence."
  3826. (require 'org-clock)
  3827. (add-hook 'org-mode-hook 'org-clock-load)
  3828. (add-hook 'kill-emacs-hook 'org-clock-save))
  3829. ;; Define the variable already here, to make sure we have it.
  3830. (defvar org-indent-mode nil
  3831. "Non-nil if Org-Indent mode is enabled.
  3832. Use the command `org-indent-mode' to change this variable.")
  3833. ;; Autoload archiving code
  3834. ;; The stuff that is needed for cycling and tags has to be defined here.
  3835. (defgroup org-archive nil
  3836. "Options concerning archiving in Org-mode."
  3837. :tag "Org Archive"
  3838. :group 'org-structure)
  3839. (defcustom org-archive-location "%s_archive::"
  3840. "The location where subtrees should be archived.
  3841. The value of this variable is a string, consisting of two parts,
  3842. separated by a double-colon. The first part is a filename and
  3843. the second part is a headline.
  3844. When the filename is omitted, archiving happens in the same file.
  3845. %s in the filename will be replaced by the current file
  3846. name (without the directory part). Archiving to a different file
  3847. is useful to keep archived entries from contributing to the
  3848. Org-mode Agenda.
  3849. The archived entries will be filed as subtrees of the specified
  3850. headline. When the headline is omitted, the subtrees are simply
  3851. filed away at the end of the file, as top-level entries. Also in
  3852. the heading you can use %s to represent the file name, this can be
  3853. useful when using the same archive for a number of different files.
  3854. Here are a few examples:
  3855. \"%s_archive::\"
  3856. If the current file is Projects.org, archive in file
  3857. Projects.org_archive, as top-level trees. This is the default.
  3858. \"::* Archived Tasks\"
  3859. Archive in the current file, under the top-level headline
  3860. \"* Archived Tasks\".
  3861. \"~/org/archive.org::\"
  3862. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  3863. \"~/org/archive.org::* From %s\"
  3864. Archive in file ~/org/archive.org (absolute path), under headlines
  3865. \"From FILENAME\" where file name is the current file name.
  3866. \"~/org/datetree.org::datetree/* Finished Tasks\"
  3867. The \"datetree/\" string is special, signifying to archive
  3868. items to the datetree. Items are placed in either the CLOSED
  3869. date of the item, or the current date if there is no CLOSED date.
  3870. The heading will be a subentry to the current date. There doesn't
  3871. need to be a heading, but there always needs to be a slash after
  3872. datetree. For example, to store archived items directly in the
  3873. datetree, use \"~/org/datetree.org::datetree/\".
  3874. \"basement::** Finished Tasks\"
  3875. Archive in file ./basement (relative path), as level 3 trees
  3876. below the level 2 heading \"** Finished Tasks\".
  3877. You may set this option on a per-file basis by adding to the buffer a
  3878. line like
  3879. #+ARCHIVE: basement::** Finished Tasks
  3880. You may also define it locally for a subtree by setting an ARCHIVE property
  3881. in the entry. If such a property is found in an entry, or anywhere up
  3882. the hierarchy, it will be used."
  3883. :group 'org-archive
  3884. :type 'string)
  3885. (defcustom org-archive-tag "ARCHIVE"
  3886. "The tag that marks a subtree as archived.
  3887. An archived subtree does not open during visibility cycling, and does
  3888. not contribute to the agenda listings.
  3889. After changing this, font-lock must be restarted in the relevant buffers to
  3890. get the proper fontification."
  3891. :group 'org-archive
  3892. :group 'org-keywords
  3893. :type 'string)
  3894. (defcustom org-agenda-skip-archived-trees t
  3895. "Non-nil means the agenda will skip any items located in archived trees.
  3896. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  3897. variable is no longer recommended, you should leave it at the value t.
  3898. Instead, use the key `v' to cycle the archives-mode in the agenda."
  3899. :group 'org-archive
  3900. :group 'org-agenda-skip
  3901. :type 'boolean)
  3902. (defcustom org-columns-skip-archived-trees t
  3903. "Non-nil means ignore archived trees when creating column view."
  3904. :group 'org-archive
  3905. :group 'org-properties
  3906. :type 'boolean)
  3907. (defcustom org-cycle-open-archived-trees nil
  3908. "Non-nil means `org-cycle' will open archived trees.
  3909. An archived tree is a tree marked with the tag ARCHIVE.
  3910. When nil, archived trees will stay folded. You can still open them with
  3911. normal outline commands like `show-all', but not with the cycling commands."
  3912. :group 'org-archive
  3913. :group 'org-cycle
  3914. :type 'boolean)
  3915. (defcustom org-sparse-tree-open-archived-trees nil
  3916. "Non-nil means sparse tree construction shows matches in archived trees.
  3917. When nil, matches in these trees are highlighted, but the trees are kept in
  3918. collapsed state."
  3919. :group 'org-archive
  3920. :group 'org-sparse-trees
  3921. :type 'boolean)
  3922. (defcustom org-sparse-tree-default-date-type 'scheduled-or-deadline
  3923. "The default date type when building a sparse tree.
  3924. When this is nil, a date is a scheduled or a deadline timestamp.
  3925. Otherwise, these types are allowed:
  3926. all: all timestamps
  3927. active: only active timestamps (<...>)
  3928. inactive: only inactive timestamps (<...)
  3929. scheduled: only scheduled timestamps
  3930. deadline: only deadline timestamps"
  3931. :type '(choice (const :tag "Scheduled or deadline" scheduled-or-deadline)
  3932. (const :tag "All timestamps" all)
  3933. (const :tag "Only active timestamps" active)
  3934. (const :tag "Only inactive timestamps" inactive)
  3935. (const :tag "Only scheduled timestamps" scheduled)
  3936. (const :tag "Only deadline timestamps" deadline)
  3937. (const :tag "Only closed timestamps" closed))
  3938. :version "24.3"
  3939. :group 'org-sparse-trees)
  3940. (defun org-cycle-hide-archived-subtrees (state)
  3941. "Re-hide all archived subtrees after a visibility state change."
  3942. (when (and (not org-cycle-open-archived-trees)
  3943. (not (memq state '(overview folded))))
  3944. (save-excursion
  3945. (let* ((globalp (memq state '(contents all)))
  3946. (beg (if globalp (point-min) (point)))
  3947. (end (if globalp (point-max) (org-end-of-subtree t))))
  3948. (org-hide-archived-subtrees beg end)
  3949. (goto-char beg)
  3950. (if (looking-at (concat ".*:" org-archive-tag ":"))
  3951. (message "%s" (substitute-command-keys
  3952. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  3953. (defun org-force-cycle-archived ()
  3954. "Cycle subtree even if it is archived."
  3955. (interactive)
  3956. (setq this-command 'org-cycle)
  3957. (let ((org-cycle-open-archived-trees t))
  3958. (call-interactively 'org-cycle)))
  3959. (defun org-hide-archived-subtrees (beg end)
  3960. "Re-hide all archived subtrees after a visibility state change."
  3961. (save-excursion
  3962. (let* ((re (concat ":" org-archive-tag ":")))
  3963. (goto-char beg)
  3964. (while (re-search-forward re end t)
  3965. (when (org-at-heading-p)
  3966. (org-flag-subtree t)
  3967. (org-end-of-subtree t))))))
  3968. (declare-function outline-end-of-heading "outline" ())
  3969. (declare-function outline-flag-region "outline" (from to flag))
  3970. (defun org-flag-subtree (flag)
  3971. (save-excursion
  3972. (org-back-to-heading t)
  3973. (outline-end-of-heading)
  3974. (outline-flag-region (point)
  3975. (progn (org-end-of-subtree t) (point))
  3976. flag)))
  3977. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  3978. (eval-and-compile
  3979. (org-autoload "org-archive"
  3980. '(org-add-archive-files)))
  3981. ;; Autoload Column View Code
  3982. (declare-function org-columns-number-to-string "org-colview" (n fmt &optional printf))
  3983. (declare-function org-columns-get-format-and-top-level "org-colview" ())
  3984. (declare-function org-columns-compute "org-colview" (property))
  3985. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  3986. '(org-columns-number-to-string
  3987. org-columns-get-format-and-top-level
  3988. org-columns-compute
  3989. org-columns-remove-overlays))
  3990. ;; Autoload ID code
  3991. (declare-function org-id-store-link "org-id")
  3992. (declare-function org-id-locations-load "org-id")
  3993. (declare-function org-id-locations-save "org-id")
  3994. (defvar org-id-track-globally)
  3995. (org-autoload "org-id"
  3996. '(org-id-new
  3997. org-id-copy
  3998. org-id-get-with-outline-path-completion
  3999. org-id-get-with-outline-drilling))
  4000. ;;; Variables for pre-computed regular expressions, all buffer local
  4001. (defvar org-drawer-regexp "^[ \t]*:PROPERTIES:[ \t]*$"
  4002. "Matches first line of a hidden block.")
  4003. (make-variable-buffer-local 'org-drawer-regexp)
  4004. (defvar org-todo-regexp nil
  4005. "Matches any of the TODO state keywords.")
  4006. (make-variable-buffer-local 'org-todo-regexp)
  4007. (defvar org-not-done-regexp nil
  4008. "Matches any of the TODO state keywords except the last one.")
  4009. (make-variable-buffer-local 'org-not-done-regexp)
  4010. (defvar org-not-done-heading-regexp nil
  4011. "Matches a TODO headline that is not done.")
  4012. (make-variable-buffer-local 'org-not-done-regexp)
  4013. (defvar org-todo-line-regexp nil
  4014. "Matches a headline and puts TODO state into group 2 if present.")
  4015. (make-variable-buffer-local 'org-todo-line-regexp)
  4016. (defvar org-complex-heading-regexp nil
  4017. "Matches a headline and puts everything into groups:
  4018. group 1: the stars
  4019. group 2: The todo keyword, maybe
  4020. group 3: Priority cookie
  4021. group 4: True headline
  4022. group 5: Tags")
  4023. (make-variable-buffer-local 'org-complex-heading-regexp)
  4024. (defvar org-complex-heading-regexp-format nil
  4025. "Printf format to make regexp to match an exact headline.
  4026. This regexp will match the headline of any node which has the
  4027. exact headline text that is put into the format, but may have any
  4028. TODO state, priority and tags.")
  4029. (make-variable-buffer-local 'org-complex-heading-regexp-format)
  4030. (defvar org-todo-line-tags-regexp nil
  4031. "Matches a headline and puts TODO state into group 2 if present.
  4032. Also put tags into group 4 if tags are present.")
  4033. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  4034. (defvar org-ds-keyword-length 12
  4035. "Maximum length of the DEADLINE and SCHEDULED keywords.")
  4036. (make-variable-buffer-local 'org-ds-keyword-length)
  4037. (defvar org-deadline-regexp nil
  4038. "Matches the DEADLINE keyword.")
  4039. (make-variable-buffer-local 'org-deadline-regexp)
  4040. (defvar org-deadline-time-regexp nil
  4041. "Matches the DEADLINE keyword together with a time stamp.")
  4042. (make-variable-buffer-local 'org-deadline-time-regexp)
  4043. (defvar org-deadline-time-hour-regexp nil
  4044. "Matches the DEADLINE keyword together with a time-and-hour stamp.")
  4045. (make-variable-buffer-local 'org-deadline-time-hour-regexp)
  4046. (defvar org-deadline-line-regexp nil
  4047. "Matches the DEADLINE keyword and the rest of the line.")
  4048. (make-variable-buffer-local 'org-deadline-line-regexp)
  4049. (defvar org-scheduled-regexp nil
  4050. "Matches the SCHEDULED keyword.")
  4051. (make-variable-buffer-local 'org-scheduled-regexp)
  4052. (defvar org-scheduled-time-regexp nil
  4053. "Matches the SCHEDULED keyword together with a time stamp.")
  4054. (make-variable-buffer-local 'org-scheduled-time-regexp)
  4055. (defvar org-scheduled-time-hour-regexp nil
  4056. "Matches the SCHEDULED keyword together with a time-and-hour stamp.")
  4057. (make-variable-buffer-local 'org-scheduled-time-hour-regexp)
  4058. (defvar org-closed-time-regexp nil
  4059. "Matches the CLOSED keyword together with a time stamp.")
  4060. (make-variable-buffer-local 'org-closed-time-regexp)
  4061. (defvar org-keyword-time-regexp nil
  4062. "Matches any of the 4 keywords, together with the time stamp.")
  4063. (make-variable-buffer-local 'org-keyword-time-regexp)
  4064. (defvar org-keyword-time-not-clock-regexp nil
  4065. "Matches any of the 3 keywords, together with the time stamp.")
  4066. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  4067. (defvar org-maybe-keyword-time-regexp nil
  4068. "Matches a timestamp, possibly preceded by a keyword.")
  4069. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  4070. (defvar org-all-time-keywords nil
  4071. "List of time keywords.")
  4072. (make-variable-buffer-local 'org-all-time-keywords)
  4073. (defconst org-plain-time-of-day-regexp
  4074. (concat
  4075. "\\(\\<[012]?[0-9]"
  4076. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4077. "\\(--?"
  4078. "\\(\\<[012]?[0-9]"
  4079. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4080. "\\)?")
  4081. "Regular expression to match a plain time or time range.
  4082. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  4083. groups carry important information:
  4084. 0 the full match
  4085. 1 the first time, range or not
  4086. 8 the second time, if it is a range.")
  4087. (defconst org-plain-time-extension-regexp
  4088. (concat
  4089. "\\(\\<[012]?[0-9]"
  4090. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4091. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  4092. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  4093. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  4094. groups carry important information:
  4095. 0 the full match
  4096. 7 hours of duration
  4097. 9 minutes of duration")
  4098. (defconst org-stamp-time-of-day-regexp
  4099. (concat
  4100. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  4101. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  4102. "\\(--?"
  4103. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  4104. "Regular expression to match a timestamp time or time range.
  4105. After a match, the following groups carry important information:
  4106. 0 the full match
  4107. 1 date plus weekday, for back referencing to make sure both times are on the same day
  4108. 2 the first time, range or not
  4109. 4 the second time, if it is a range.")
  4110. (defconst org-startup-options
  4111. '(("fold" org-startup-folded t)
  4112. ("overview" org-startup-folded t)
  4113. ("nofold" org-startup-folded nil)
  4114. ("showall" org-startup-folded nil)
  4115. ("showeverything" org-startup-folded showeverything)
  4116. ("content" org-startup-folded content)
  4117. ("indent" org-startup-indented t)
  4118. ("noindent" org-startup-indented nil)
  4119. ("hidestars" org-hide-leading-stars t)
  4120. ("showstars" org-hide-leading-stars nil)
  4121. ("odd" org-odd-levels-only t)
  4122. ("oddeven" org-odd-levels-only nil)
  4123. ("align" org-startup-align-all-tables t)
  4124. ("noalign" org-startup-align-all-tables nil)
  4125. ("inlineimages" org-startup-with-inline-images t)
  4126. ("noinlineimages" org-startup-with-inline-images nil)
  4127. ("latexpreview" org-startup-with-latex-preview t)
  4128. ("nolatexpreview" org-startup-with-latex-preview nil)
  4129. ("customtime" org-display-custom-times t)
  4130. ("logdone" org-log-done time)
  4131. ("lognotedone" org-log-done note)
  4132. ("nologdone" org-log-done nil)
  4133. ("lognoteclock-out" org-log-note-clock-out t)
  4134. ("nolognoteclock-out" org-log-note-clock-out nil)
  4135. ("logrepeat" org-log-repeat state)
  4136. ("lognoterepeat" org-log-repeat note)
  4137. ("logdrawer" org-log-into-drawer t)
  4138. ("nologdrawer" org-log-into-drawer nil)
  4139. ("logstatesreversed" org-log-states-order-reversed t)
  4140. ("nologstatesreversed" org-log-states-order-reversed nil)
  4141. ("nologrepeat" org-log-repeat nil)
  4142. ("logreschedule" org-log-reschedule time)
  4143. ("lognotereschedule" org-log-reschedule note)
  4144. ("nologreschedule" org-log-reschedule nil)
  4145. ("logredeadline" org-log-redeadline time)
  4146. ("lognoteredeadline" org-log-redeadline note)
  4147. ("nologredeadline" org-log-redeadline nil)
  4148. ("logrefile" org-log-refile time)
  4149. ("lognoterefile" org-log-refile note)
  4150. ("nologrefile" org-log-refile nil)
  4151. ("fninline" org-footnote-define-inline t)
  4152. ("nofninline" org-footnote-define-inline nil)
  4153. ("fnlocal" org-footnote-section nil)
  4154. ("fnauto" org-footnote-auto-label t)
  4155. ("fnprompt" org-footnote-auto-label nil)
  4156. ("fnconfirm" org-footnote-auto-label confirm)
  4157. ("fnplain" org-footnote-auto-label plain)
  4158. ("fnadjust" org-footnote-auto-adjust t)
  4159. ("nofnadjust" org-footnote-auto-adjust nil)
  4160. ("constcgs" constants-unit-system cgs)
  4161. ("constSI" constants-unit-system SI)
  4162. ("noptag" org-tag-persistent-alist nil)
  4163. ("hideblocks" org-hide-block-startup t)
  4164. ("nohideblocks" org-hide-block-startup nil)
  4165. ("beamer" org-startup-with-beamer-mode t)
  4166. ("entitiespretty" org-pretty-entities t)
  4167. ("entitiesplain" org-pretty-entities nil))
  4168. "Variable associated with STARTUP options for org-mode.
  4169. Each element is a list of three items: the startup options (as written
  4170. in the #+STARTUP line), the corresponding variable, and the value to set
  4171. this variable to if the option is found. An optional forth element PUSH
  4172. means to push this value onto the list in the variable.")
  4173. (defun org-update-property-plist (key val props)
  4174. "Update PROPS with KEY and VAL."
  4175. (let* ((appending (string= "+" (substring key (- (length key) 1))))
  4176. (key (if appending (substring key 0 (- (length key) 1)) key))
  4177. (remainder (org-remove-if (lambda (p) (string= (car p) key)) props))
  4178. (previous (cdr (assoc key props))))
  4179. (if appending
  4180. (cons (cons key (if previous (concat previous " " val) val)) remainder)
  4181. (cons (cons key val) remainder))))
  4182. (defconst org-block-regexp
  4183. "^[ \t]*#\\+begin_?\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_?\\1[ \t]*$"
  4184. "Regular expression for hiding blocks.")
  4185. (defconst org-heading-keyword-regexp-format
  4186. "^\\(\\*+\\)\\(?: +%s\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  4187. "Printf format for a regexp matching a headline with some keyword.
  4188. This regexp will match the headline of any node which has the
  4189. exact keyword that is put into the format. The keyword isn't in
  4190. any group by default, but the stars and the body are.")
  4191. (defconst org-heading-keyword-maybe-regexp-format
  4192. "^\\(\\*+\\)\\(?: +%s\\)?\\(?: +\\(.*?\\)\\)?[ \t]*$"
  4193. "Printf format for a regexp matching a headline, possibly with some keyword.
  4194. This regexp can match any headline with the specified keyword, or
  4195. without a keyword. The keyword isn't in any group by default,
  4196. but the stars and the body are.")
  4197. (defcustom org-group-tags t
  4198. "When non-nil (the default), use group tags.
  4199. This can be turned on/off through `org-toggle-tags-groups'."
  4200. :group 'org-tags
  4201. :group 'org-startup
  4202. :type 'boolean)
  4203. (defun org-toggle-tags-groups ()
  4204. "Toggle support for group tags.
  4205. Support for group tags is controlled by the option
  4206. `org-group-tags', which is non-nil by default."
  4207. (interactive)
  4208. (setq org-group-tags (not org-group-tags))
  4209. (cond ((and (derived-mode-p 'org-agenda-mode)
  4210. org-group-tags)
  4211. (org-agenda-redo))
  4212. ((derived-mode-p 'org-mode)
  4213. (let ((org-inhibit-startup t)) (org-mode))))
  4214. (message "Groups tags support has been turned %s"
  4215. (if org-group-tags "on" "off")))
  4216. (defun org-set-regexps-and-options-for-tags ()
  4217. "Precompute regular expressions used for tags in the current buffer."
  4218. (when (derived-mode-p 'org-mode)
  4219. (org-set-local 'org-file-tags nil)
  4220. (let ((re (org-make-options-regexp '("FILETAGS" "TAGS")))
  4221. (splitre "[ \t]+")
  4222. (start 0)
  4223. tags ftags key value)
  4224. (save-excursion
  4225. (save-restriction
  4226. (widen)
  4227. (goto-char (point-min))
  4228. (while (re-search-forward re nil t)
  4229. (setq key (upcase (org-match-string-no-properties 1))
  4230. value (org-match-string-no-properties 2))
  4231. (if (stringp value) (setq value (org-trim value)))
  4232. (cond
  4233. ((equal key "TAGS")
  4234. (setq tags (append tags (if tags '("\\n") nil)
  4235. (org-split-string value splitre))))
  4236. ((equal key "FILETAGS")
  4237. (when (string-match "\\S-" value)
  4238. (setq ftags
  4239. (append
  4240. ftags
  4241. (apply 'append
  4242. (mapcar (lambda (x) (org-split-string x ":"))
  4243. (org-split-string value)))))))))))
  4244. ;; Process the file tags.
  4245. (and ftags (org-set-local 'org-file-tags
  4246. (mapcar 'org-add-prop-inherited ftags)))
  4247. (org-set-local 'org-tag-groups-alist nil)
  4248. ;; Process the tags.
  4249. ;; FIXME
  4250. (when tags
  4251. (let (e tgs g)
  4252. (while (setq e (pop tags))
  4253. (cond
  4254. ((equal e "{")
  4255. (progn (push '(:startgroup) tgs)
  4256. (when (equal (nth 1 tags) ":")
  4257. (push (list (replace-regexp-in-string
  4258. "(.+)$" "" (nth 0 tags)))
  4259. org-tag-groups-alist)
  4260. (setq g 0))))
  4261. ((equal e ":") (push '(:grouptags) tgs))
  4262. ((equal e "}") (push '(:endgroup) tgs) (if g (setq g nil)))
  4263. ((equal e "\\n") (push '(:newline) tgs))
  4264. ((string-match (org-re "^\\([[:alnum:]_@#%]+\\)(\\(.\\))$") e)
  4265. (push (cons (match-string 1 e)
  4266. (string-to-char (match-string 2 e))) tgs)
  4267. (if (and g (> g 0))
  4268. (setcar org-tag-groups-alist
  4269. (append (car org-tag-groups-alist)
  4270. (list (match-string 1 e)))))
  4271. (if g (setq g (1+ g))))
  4272. (t (push (list e) tgs)
  4273. (if (and g (> g 0))
  4274. (setcar org-tag-groups-alist
  4275. (append (car org-tag-groups-alist) (list e))))
  4276. (if g (setq g (1+ g))))))
  4277. (org-set-local 'org-tag-alist nil)
  4278. (while (setq e (pop tgs))
  4279. (or (and (stringp (car e))
  4280. (assoc (car e) org-tag-alist))
  4281. (push e org-tag-alist)))
  4282. ;; Return a list with tag variables
  4283. (list org-file-tags org-tag-alist org-tag-groups-alist))))))
  4284. (defun org-set-regexps-and-options ()
  4285. "Precompute regular expressions used in the current buffer."
  4286. (when (derived-mode-p 'org-mode)
  4287. (org-set-local 'org-todo-kwd-alist nil)
  4288. (org-set-local 'org-todo-key-alist nil)
  4289. (org-set-local 'org-todo-key-trigger nil)
  4290. (org-set-local 'org-todo-keywords-1 nil)
  4291. (org-set-local 'org-done-keywords nil)
  4292. (org-set-local 'org-todo-heads nil)
  4293. (org-set-local 'org-todo-sets nil)
  4294. (org-set-local 'org-todo-log-states nil)
  4295. (org-set-local 'org-file-properties nil)
  4296. (let ((re (org-make-options-regexp
  4297. '("CATEGORY" "TODO" "COLUMNS" "STARTUP" "ARCHIVE"
  4298. "LINK" "PRIORITIES" "CONSTANTS" "PROPERTY" "DRAWERS"
  4299. "SETUPFILE" "OPTIONS")
  4300. "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
  4301. (splitre "[ \t]+")
  4302. (scripts org-use-sub-superscripts)
  4303. kwds kws0 kwsa key log value cat arch const links hw dws
  4304. tail sep kws1 prio props drawers ext-setup-or-nil setup-contents
  4305. (start 0))
  4306. (save-excursion
  4307. (save-restriction
  4308. (widen)
  4309. (goto-char (point-min))
  4310. (while
  4311. (or (and
  4312. ext-setup-or-nil
  4313. (let (ret)
  4314. (with-temp-buffer
  4315. (insert ext-setup-or-nil)
  4316. (let ((major-mode 'org-mode))
  4317. (setq ret (save-match-data
  4318. (org-set-regexps-and-options-for-tags)))))
  4319. ;; Append setupfile tags to existing tags
  4320. (setq org-file-tags (append org-file-tags (nth 0 ret))
  4321. org-tag-alist (append org-tag-alist (nth 1 ret))
  4322. org-tag-groups-alist
  4323. (append org-tag-groups-alist (nth 2 ret)))))
  4324. (and ext-setup-or-nil
  4325. (string-match re ext-setup-or-nil start)
  4326. (setq start (match-end 0)))
  4327. (and (setq ext-setup-or-nil nil start 0)
  4328. (re-search-forward re nil t)))
  4329. (setq key (upcase (match-string 1 ext-setup-or-nil))
  4330. value (org-match-string-no-properties 2 ext-setup-or-nil))
  4331. (if (stringp value) (setq value (org-trim value)))
  4332. (cond
  4333. ((equal key "CATEGORY")
  4334. (setq cat value))
  4335. ((member key '("SEQ_TODO" "TODO"))
  4336. (push (cons 'sequence (org-split-string value splitre)) kwds))
  4337. ((equal key "TYP_TODO")
  4338. (push (cons 'type (org-split-string value splitre)) kwds))
  4339. ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key)
  4340. ;; general TODO-like setup
  4341. (push (cons (intern (downcase (match-string 1 key)))
  4342. (org-split-string value splitre)) kwds))
  4343. ((equal key "COLUMNS")
  4344. (org-set-local 'org-columns-default-format value))
  4345. ((equal key "LINK")
  4346. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  4347. (push (cons (match-string 1 value)
  4348. (org-trim (match-string 2 value)))
  4349. links)))
  4350. ((equal key "PRIORITIES")
  4351. (setq prio (org-split-string value " +")))
  4352. ((equal key "PROPERTY")
  4353. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  4354. (setq props (org-update-property-plist (match-string 1 value)
  4355. (match-string 2 value)
  4356. props))))
  4357. ((equal key "DRAWERS")
  4358. (setq drawers (delete-dups (append org-drawers (org-split-string value splitre)))))
  4359. ((equal key "CONSTANTS")
  4360. (org-table-set-constants))
  4361. ((equal key "STARTUP")
  4362. (let ((opts (org-split-string value splitre))
  4363. l var val)
  4364. (while (setq l (pop opts))
  4365. (when (setq l (assoc l org-startup-options))
  4366. (setq var (nth 1 l) val (nth 2 l))
  4367. (if (not (nth 3 l))
  4368. (set (make-local-variable var) val)
  4369. (if (not (listp (symbol-value var)))
  4370. (set (make-local-variable var) nil))
  4371. (set (make-local-variable var) (symbol-value var))
  4372. (add-to-list var val))))))
  4373. ((equal key "ARCHIVE")
  4374. (setq arch value)
  4375. (remove-text-properties 0 (length arch)
  4376. '(face t fontified t) arch))
  4377. ((equal key "OPTIONS")
  4378. (if (string-match "\\([ \t]\\|\\`\\)\\^:\\(t\\|nil\\|{}\\)" value)
  4379. (setq scripts (read (match-string 2 value)))))
  4380. ((and (equal key "SETUPFILE")
  4381. ;; Prevent checking in Gnus messages
  4382. (not buffer-read-only))
  4383. (setq setup-contents (org-file-contents
  4384. (expand-file-name
  4385. (org-remove-double-quotes value))
  4386. 'noerror))
  4387. (if (not ext-setup-or-nil)
  4388. (setq ext-setup-or-nil setup-contents start 0)
  4389. (setq ext-setup-or-nil
  4390. (concat (substring ext-setup-or-nil 0 start)
  4391. "\n" setup-contents "\n"
  4392. (substring ext-setup-or-nil start)))))))
  4393. ;; search for property blocks
  4394. (goto-char (point-min))
  4395. (while (re-search-forward org-block-regexp nil t)
  4396. (when (equal "PROPERTY" (upcase (match-string 1)))
  4397. (setq value (replace-regexp-in-string
  4398. "[\n\r]" " " (match-string 4)))
  4399. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  4400. (setq props (org-update-property-plist (match-string 1 value)
  4401. (match-string 2 value)
  4402. props)))))))
  4403. (org-set-local 'org-use-sub-superscripts scripts)
  4404. (when cat
  4405. (org-set-local 'org-category (intern cat))
  4406. (push (cons "CATEGORY" cat) props))
  4407. (when prio
  4408. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  4409. (setq prio (mapcar 'string-to-char prio))
  4410. (org-set-local 'org-highest-priority (nth 0 prio))
  4411. (org-set-local 'org-lowest-priority (nth 1 prio))
  4412. (org-set-local 'org-default-priority (nth 2 prio)))
  4413. (and props (org-set-local 'org-file-properties (nreverse props)))
  4414. (and drawers (org-set-local 'org-drawers drawers))
  4415. (and arch (org-set-local 'org-archive-location arch))
  4416. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  4417. ;; Process the TODO keywords
  4418. (unless kwds
  4419. ;; Use the global values as if they had been given locally.
  4420. (setq kwds (default-value 'org-todo-keywords))
  4421. (if (stringp (car kwds))
  4422. (setq kwds (list (cons org-todo-interpretation
  4423. (default-value 'org-todo-keywords)))))
  4424. (setq kwds (reverse kwds)))
  4425. (setq kwds (nreverse kwds))
  4426. (let (inter kws kw)
  4427. (while (setq kws (pop kwds))
  4428. (let ((kws (or
  4429. (run-hook-with-args-until-success
  4430. 'org-todo-setup-filter-hook kws)
  4431. kws)))
  4432. (setq inter (pop kws) sep (member "|" kws)
  4433. kws0 (delete "|" (copy-sequence kws))
  4434. kwsa nil
  4435. kws1 (mapcar
  4436. (lambda (x)
  4437. ;; 1 2
  4438. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  4439. (progn
  4440. (setq kw (match-string 1 x)
  4441. key (and (match-end 2) (match-string 2 x))
  4442. log (org-extract-log-state-settings x))
  4443. (push (cons kw (and key (string-to-char key))) kwsa)
  4444. (and log (push log org-todo-log-states))
  4445. kw)
  4446. (error "Invalid TODO keyword %s" x)))
  4447. kws0)
  4448. kwsa (if kwsa (append '((:startgroup))
  4449. (nreverse kwsa)
  4450. '((:endgroup))))
  4451. hw (car kws1)
  4452. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  4453. tail (list inter hw (car dws) (org-last dws))))
  4454. (add-to-list 'org-todo-heads hw 'append)
  4455. (push kws1 org-todo-sets)
  4456. (setq org-done-keywords (append org-done-keywords dws nil))
  4457. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  4458. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  4459. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  4460. (setq org-todo-sets (nreverse org-todo-sets)
  4461. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  4462. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  4463. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  4464. ;; Compute the regular expressions and other local variables.
  4465. ;; Using `org-outline-regexp-bol' would complicate them much,
  4466. ;; because of the fixed white space at the end of that string.
  4467. (if (not org-done-keywords)
  4468. (setq org-done-keywords (and org-todo-keywords-1
  4469. (list (org-last org-todo-keywords-1)))))
  4470. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  4471. (length org-scheduled-string)
  4472. (length org-clock-string)
  4473. (length org-closed-string)))
  4474. org-drawer-regexp
  4475. (concat "^[ \t]*:\\("
  4476. (mapconcat 'regexp-quote org-drawers "\\|")
  4477. "\\):[ \t]*$")
  4478. org-not-done-keywords
  4479. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  4480. org-todo-regexp
  4481. (concat "\\("
  4482. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  4483. "\\)")
  4484. org-not-done-regexp
  4485. (concat "\\("
  4486. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  4487. "\\)")
  4488. org-not-done-heading-regexp
  4489. (format org-heading-keyword-regexp-format org-not-done-regexp)
  4490. org-todo-line-regexp
  4491. (format org-heading-keyword-maybe-regexp-format org-todo-regexp)
  4492. org-complex-heading-regexp
  4493. (concat "^\\(\\*+\\)"
  4494. "\\(?: +" org-todo-regexp "\\)?"
  4495. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4496. "\\(?: +\\(.*?\\)\\)??"
  4497. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?")
  4498. "[ \t]*$")
  4499. org-complex-heading-regexp-format
  4500. (concat "^\\(\\*+\\)"
  4501. "\\(?: +" org-todo-regexp "\\)?"
  4502. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4503. "\\(?: +"
  4504. ;; Stats cookies can be stuck to body.
  4505. "\\(?:\\[[0-9%%/]+\\] *\\)?"
  4506. "\\(%s\\)"
  4507. "\\(?: *\\[[0-9%%/]+\\]\\)?"
  4508. "\\)"
  4509. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%%:]+:\\)\\)?")
  4510. "[ \t]*$")
  4511. org-todo-line-tags-regexp
  4512. (concat "^\\(\\*+\\)"
  4513. "\\(?: +" org-todo-regexp "\\)?"
  4514. "\\(?: +\\(.*?\\)\\)??"
  4515. (org-re "\\(?:[ \t]+\\(:[[:alnum:]:_@#%]+:\\)\\)?")
  4516. "[ \t]*$")
  4517. org-deadline-regexp (concat "\\<" org-deadline-string)
  4518. org-deadline-time-regexp
  4519. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  4520. org-deadline-time-hour-regexp
  4521. (concat "\\<" org-deadline-string
  4522. " *<\\([^>]+[0-9]\\{1,2\\}:[0-9]\\{2\\}[0-9-+:hdwmy \t.]*\\)>")
  4523. org-deadline-line-regexp
  4524. (concat "\\<\\(" org-deadline-string "\\).*")
  4525. org-scheduled-regexp
  4526. (concat "\\<" org-scheduled-string)
  4527. org-scheduled-time-regexp
  4528. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  4529. org-scheduled-time-hour-regexp
  4530. (concat "\\<" org-scheduled-string
  4531. " *<\\([^>]+[0-9]\\{1,2\\}:[0-9]\\{2\\}[0-9-+:hdwmy \t.]*\\)>")
  4532. org-closed-time-regexp
  4533. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  4534. org-keyword-time-regexp
  4535. (concat "\\<\\(" org-scheduled-string
  4536. "\\|" org-deadline-string
  4537. "\\|" org-closed-string
  4538. "\\|" org-clock-string "\\)"
  4539. " *[[<]\\([^]>]+\\)[]>]")
  4540. org-keyword-time-not-clock-regexp
  4541. (concat "\\<\\(" org-scheduled-string
  4542. "\\|" org-deadline-string
  4543. "\\|" org-closed-string
  4544. "\\)"
  4545. " *[[<]\\([^]>]+\\)[]>]")
  4546. org-maybe-keyword-time-regexp
  4547. (concat "\\(\\<\\(" org-scheduled-string
  4548. "\\|" org-deadline-string
  4549. "\\|" org-closed-string
  4550. "\\|" org-clock-string "\\)\\)?"
  4551. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  4552. org-all-time-keywords
  4553. (mapcar (lambda (w) (substring w 0 -1))
  4554. (list org-scheduled-string org-deadline-string
  4555. org-clock-string org-closed-string)))
  4556. (org-compute-latex-and-related-regexp)
  4557. (org-set-font-lock-defaults))))
  4558. (defun org-file-contents (file &optional noerror)
  4559. "Return the contents of FILE, as a string."
  4560. (if (or (not file)
  4561. (not (file-readable-p file)))
  4562. (if noerror
  4563. (message "Cannot read file \"%s\"" file)
  4564. (error "Cannot read file \"%s\"" file))
  4565. (with-temp-buffer
  4566. (insert-file-contents file)
  4567. (buffer-string))))
  4568. (defun org-extract-log-state-settings (x)
  4569. "Extract the log state setting from a TODO keyword string.
  4570. This will extract info from a string like \"WAIT(w@/!)\"."
  4571. (let (kw key log1 log2)
  4572. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  4573. (setq kw (match-string 1 x)
  4574. key (and (match-end 2) (match-string 2 x))
  4575. log1 (and (match-end 3) (match-string 3 x))
  4576. log2 (and (match-end 4) (match-string 4 x)))
  4577. (and (or log1 log2)
  4578. (list kw
  4579. (and log1 (if (equal log1 "!") 'time 'note))
  4580. (and log2 (if (equal log2 "!") 'time 'note)))))))
  4581. (defun org-remove-keyword-keys (list)
  4582. "Remove a pair of parenthesis at the end of each string in LIST."
  4583. (mapcar (lambda (x)
  4584. (if (string-match "(.*)$" x)
  4585. (substring x 0 (match-beginning 0))
  4586. x))
  4587. list))
  4588. (defun org-assign-fast-keys (alist)
  4589. "Assign fast keys to a keyword-key alist.
  4590. Respect keys that are already there."
  4591. (let (new e (alt ?0))
  4592. (while (setq e (pop alist))
  4593. (if (or (memq (car e) '(:newline :grouptags :endgroup :startgroup))
  4594. (cdr e)) ;; Key already assigned.
  4595. (push e new)
  4596. (let ((clist (string-to-list (downcase (car e))))
  4597. (used (append new alist)))
  4598. (when (= (car clist) ?@)
  4599. (pop clist))
  4600. (while (and clist (rassoc (car clist) used))
  4601. (pop clist))
  4602. (unless clist
  4603. (while (rassoc alt used)
  4604. (incf alt)))
  4605. (push (cons (car e) (or (car clist) alt)) new))))
  4606. (nreverse new)))
  4607. ;;; Some variables used in various places
  4608. (defvar org-window-configuration nil
  4609. "Used in various places to store a window configuration.")
  4610. (defvar org-selected-window nil
  4611. "Used in various places to store a window configuration.")
  4612. (defvar org-finish-function nil
  4613. "Function to be called when `C-c C-c' is used.
  4614. This is for getting out of special buffers like capture.")
  4615. ;; FIXME: Occasionally check by commenting these, to make sure
  4616. ;; no other functions uses these, forgetting to let-bind them.
  4617. (org-no-warnings (defvar entry)) ;; unprefixed, from calendar.el
  4618. (defvar org-last-state)
  4619. (org-no-warnings (defvar date)) ;; unprefixed, from calendar.el
  4620. ;; Defined somewhere in this file, but used before definition.
  4621. (defvar org-entities) ;; defined in org-entities.el
  4622. (defvar org-struct-menu)
  4623. (defvar org-org-menu)
  4624. (defvar org-tbl-menu)
  4625. ;;;; Define the Org-mode
  4626. ;; We use a before-change function to check if a table might need
  4627. ;; an update.
  4628. (defvar org-table-may-need-update t
  4629. "Indicates that a table might need an update.
  4630. This variable is set by `org-before-change-function'.
  4631. `org-table-align' sets it back to nil.")
  4632. (defun org-before-change-function (beg end)
  4633. "Every change indicates that a table might need an update."
  4634. (setq org-table-may-need-update t))
  4635. (defvar org-mode-map)
  4636. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  4637. (defvar org-inhibit-startup-visibility-stuff nil) ; Dynamically-scoped param.
  4638. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  4639. (defvar org-inhibit-logging nil) ; Dynamically-scoped param.
  4640. (defvar org-inhibit-blocking nil) ; Dynamically-scoped param.
  4641. (defvar org-table-buffer-is-an nil)
  4642. (defvar bidi-paragraph-direction)
  4643. (defvar buffer-face-mode-face)
  4644. (require 'outline)
  4645. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  4646. (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"))
  4647. (require 'noutline "noutline" 'noerror) ;; stock XEmacs does not have it
  4648. ;; Other stuff we need.
  4649. (require 'time-date)
  4650. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  4651. (require 'easymenu)
  4652. (require 'overlay)
  4653. ;; (require 'org-macs) moved higher up in the file before it is first used
  4654. (require 'org-entities)
  4655. ;; (require 'org-compat) moved higher up in the file before it is first used
  4656. (require 'org-faces)
  4657. (require 'org-list)
  4658. (require 'org-pcomplete)
  4659. (require 'org-src)
  4660. (require 'org-footnote)
  4661. (require 'org-macro)
  4662. ;; babel
  4663. (require 'ob)
  4664. ;;;###autoload
  4665. (define-derived-mode org-mode outline-mode "Org"
  4666. "Outline-based notes management and organizer, alias
  4667. \"Carsten's outline-mode for keeping track of everything.\"
  4668. Org-mode develops organizational tasks around a NOTES file which
  4669. contains information about projects as plain text. Org-mode is
  4670. implemented on top of outline-mode, which is ideal to keep the content
  4671. of large files well structured. It supports ToDo items, deadlines and
  4672. time stamps, which magically appear in the diary listing of the Emacs
  4673. calendar. Tables are easily created with a built-in table editor.
  4674. Plain text URL-like links connect to websites, emails (VM), Usenet
  4675. messages (Gnus), BBDB entries, and any files related to the project.
  4676. For printing and sharing of notes, an Org-mode file (or a part of it)
  4677. can be exported as a structured ASCII or HTML file.
  4678. The following commands are available:
  4679. \\{org-mode-map}"
  4680. ;; Get rid of Outline menus, they are not needed
  4681. ;; Need to do this here because define-derived-mode sets up
  4682. ;; the keymap so late. Still, it is a waste to call this each time
  4683. ;; we switch another buffer into org-mode.
  4684. (if (featurep 'xemacs)
  4685. (when (boundp 'outline-mode-menu-heading)
  4686. ;; Assume this is Greg's port, it uses easymenu
  4687. (easy-menu-remove outline-mode-menu-heading)
  4688. (easy-menu-remove outline-mode-menu-show)
  4689. (easy-menu-remove outline-mode-menu-hide))
  4690. (define-key org-mode-map [menu-bar headings] 'undefined)
  4691. (define-key org-mode-map [menu-bar hide] 'undefined)
  4692. (define-key org-mode-map [menu-bar show] 'undefined))
  4693. (org-load-modules-maybe)
  4694. (easy-menu-add org-org-menu)
  4695. (easy-menu-add org-tbl-menu)
  4696. (org-install-agenda-files-menu)
  4697. (if org-descriptive-links (add-to-invisibility-spec '(org-link)))
  4698. (add-to-invisibility-spec '(org-cwidth))
  4699. (add-to-invisibility-spec '(org-hide-block . t))
  4700. (when (featurep 'xemacs)
  4701. (org-set-local 'line-move-ignore-invisible t))
  4702. (org-set-local 'outline-regexp org-outline-regexp)
  4703. (org-set-local 'outline-level 'org-outline-level)
  4704. (setq bidi-paragraph-direction 'left-to-right)
  4705. ;; FIXME Circumvent a bug in outline.el (Emacs <24.4)
  4706. (set (make-local-variable 'paragraph-start) " \\|[ \t]*$\\|\\*+ ")
  4707. (when (and org-ellipsis
  4708. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  4709. (fboundp 'make-glyph-code))
  4710. (unless org-display-table
  4711. (setq org-display-table (make-display-table)))
  4712. (set-display-table-slot
  4713. org-display-table 4
  4714. (vconcat (mapcar
  4715. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  4716. org-ellipsis)))
  4717. (if (stringp org-ellipsis) org-ellipsis "..."))))
  4718. (setq buffer-display-table org-display-table))
  4719. (org-set-regexps-and-options-for-tags)
  4720. (org-set-regexps-and-options)
  4721. (when (and org-tag-faces (not org-tags-special-faces-re))
  4722. ;; tag faces set outside customize.... force initialization.
  4723. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  4724. ;; Calc embedded
  4725. (org-set-local 'calc-embedded-open-mode "# ")
  4726. ;; Modify a few syntax entries
  4727. (modify-syntax-entry ?@ "w")
  4728. (modify-syntax-entry ?\" "\"")
  4729. (if org-startup-truncated (setq truncate-lines t))
  4730. (when org-startup-indented (require 'org-indent) (org-indent-mode 1))
  4731. (org-set-local 'font-lock-unfontify-region-function
  4732. 'org-unfontify-region)
  4733. ;; Activate before-change-function
  4734. (org-set-local 'org-table-may-need-update t)
  4735. (org-add-hook 'before-change-functions 'org-before-change-function nil
  4736. 'local)
  4737. ;; Check for running clock before killing a buffer
  4738. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  4739. ;; Initialize macros templates.
  4740. (org-macro-initialize-templates)
  4741. ;; Initialize radio targets.
  4742. (org-update-radio-target-regexp)
  4743. ;; Indentation.
  4744. (org-set-local 'indent-line-function 'org-indent-line)
  4745. (org-set-local 'indent-region-function 'org-indent-region)
  4746. ;; Filling and auto-filling.
  4747. (org-setup-filling)
  4748. ;; Comments.
  4749. (org-setup-comments-handling)
  4750. ;; Beginning/end of defun
  4751. (org-set-local 'beginning-of-defun-function 'org-backward-element)
  4752. (org-set-local 'end-of-defun-function 'org-forward-element)
  4753. ;; Next error for sparse trees
  4754. (org-set-local 'next-error-function 'org-occur-next-match)
  4755. ;; Make sure dependence stuff works reliably, even for users who set it
  4756. ;; too late :-(
  4757. (if org-enforce-todo-dependencies
  4758. (add-hook 'org-blocker-hook
  4759. 'org-block-todo-from-children-or-siblings-or-parent)
  4760. (remove-hook 'org-blocker-hook
  4761. 'org-block-todo-from-children-or-siblings-or-parent))
  4762. (if org-enforce-todo-checkbox-dependencies
  4763. (add-hook 'org-blocker-hook
  4764. 'org-block-todo-from-checkboxes)
  4765. (remove-hook 'org-blocker-hook
  4766. 'org-block-todo-from-checkboxes))
  4767. ;; Align options lines
  4768. (org-set-local
  4769. 'align-mode-rules-list
  4770. '((org-in-buffer-settings
  4771. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  4772. (modes . '(org-mode)))))
  4773. ;; Imenu
  4774. (org-set-local 'imenu-create-index-function
  4775. 'org-imenu-get-tree)
  4776. ;; Make isearch reveal context
  4777. (if (or (featurep 'xemacs)
  4778. (not (boundp 'outline-isearch-open-invisible-function)))
  4779. ;; Emacs 21 and XEmacs make use of the hook
  4780. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  4781. ;; Emacs 22 deals with this through a special variable
  4782. (org-set-local 'outline-isearch-open-invisible-function
  4783. (lambda (&rest ignore) (org-show-context 'isearch)))
  4784. (org-add-hook 'isearch-mode-end-hook 'org-fix-ellipsis-at-bol 'append 'local))
  4785. ;; Setup the pcomplete hooks
  4786. (set (make-local-variable 'pcomplete-command-completion-function)
  4787. 'org-pcomplete-initial)
  4788. (set (make-local-variable 'pcomplete-command-name-function)
  4789. 'org-command-at-point)
  4790. (set (make-local-variable 'pcomplete-default-completion-function)
  4791. 'ignore)
  4792. (set (make-local-variable 'pcomplete-parse-arguments-function)
  4793. 'org-parse-arguments)
  4794. (set (make-local-variable 'pcomplete-termination-string) "")
  4795. (when (>= emacs-major-version 23)
  4796. (set (make-local-variable 'buffer-face-mode-face) 'org-default))
  4797. ;; If empty file that did not turn on org-mode automatically, make it to.
  4798. (if (and org-insert-mode-line-in-empty-file
  4799. (org-called-interactively-p 'any)
  4800. (= (point-min) (point-max)))
  4801. (insert "# -*- mode: org -*-\n\n"))
  4802. (unless org-inhibit-startup
  4803. (org-unmodified
  4804. (and org-startup-with-beamer-mode (org-beamer-mode))
  4805. (when org-startup-align-all-tables
  4806. (org-table-map-tables 'org-table-align 'quietly))
  4807. (when org-startup-with-inline-images
  4808. (org-display-inline-images))
  4809. (when org-startup-with-latex-preview
  4810. (org-preview-latex-fragment))
  4811. (unless org-inhibit-startup-visibility-stuff
  4812. (org-set-startup-visibility))))
  4813. ;; Try to set org-hide correctly
  4814. (set-face-foreground 'org-hide (org-find-invisible-foreground)))
  4815. ;; Update `customize-package-emacs-version-alist'
  4816. (add-to-list 'customize-package-emacs-version-alist
  4817. '(Org ("6.21b" . "23.1") ("6.33x" . "23.2")
  4818. ("7.8.11" . "24.1") ("7.9.4" . "24.3")
  4819. ("8.0" . "24.4")))
  4820. (defvar org-mode-transpose-word-syntax-table
  4821. (let ((st (make-syntax-table)))
  4822. (mapc (lambda(c) (modify-syntax-entry
  4823. (string-to-char (car c)) "w p" st))
  4824. org-emphasis-alist)
  4825. st))
  4826. (when (fboundp 'abbrev-table-put)
  4827. (abbrev-table-put org-mode-abbrev-table
  4828. :parents (list text-mode-abbrev-table)))
  4829. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  4830. (defsubst org-fix-ellipsis-at-bol ()
  4831. (save-excursion (goto-char (window-start)) (recenter 0)))
  4832. (defun org-find-invisible-foreground ()
  4833. (let ((candidates (remove
  4834. "unspecified-bg"
  4835. (nconc
  4836. (list (face-background 'default)
  4837. (face-background 'org-default))
  4838. (mapcar
  4839. (lambda (alist)
  4840. (when (boundp alist)
  4841. (cdr (assoc 'background-color (symbol-value alist)))))
  4842. '(default-frame-alist initial-frame-alist window-system-default-frame-alist))
  4843. (list (face-foreground 'org-hide))))))
  4844. (car (remove nil candidates))))
  4845. (defun org-current-time (&optional rounding-minutes past)
  4846. "Current time, possibly rounded to ROUNDING-MINUTES.
  4847. When ROUNDING-MINUTES is not an integer, fall back on the car of
  4848. `org-time-stamp-rounding-minutes'. When PAST is non-nil, ensure
  4849. the rounding returns a past time."
  4850. (let ((r (or (and (integerp rounding-minutes) rounding-minutes)
  4851. (car org-time-stamp-rounding-minutes)))
  4852. (time (decode-time)) res)
  4853. (if (< r 1)
  4854. (current-time)
  4855. (setq res
  4856. (apply 'encode-time
  4857. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  4858. (nthcdr 2 time))))
  4859. (if (and past (< (org-float-time (time-subtract (current-time) res)) 0))
  4860. (seconds-to-time (- (org-float-time res) (* r 60)))
  4861. res))))
  4862. (defun org-today ()
  4863. "Return today date, considering `org-extend-today-until'."
  4864. (time-to-days
  4865. (time-subtract (current-time)
  4866. (list 0 (* 3600 org-extend-today-until) 0))))
  4867. ;;;; Font-Lock stuff, including the activators
  4868. (defvar org-mouse-map (make-sparse-keymap))
  4869. (org-defkey org-mouse-map [mouse-2] 'org-open-at-mouse)
  4870. (org-defkey org-mouse-map [mouse-3] 'org-find-file-at-mouse)
  4871. (when org-mouse-1-follows-link
  4872. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  4873. (when org-tab-follows-link
  4874. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  4875. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  4876. (require 'font-lock)
  4877. (defconst org-non-link-chars "]\t\n\r<>")
  4878. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  4879. "shell" "elisp" "doi" "message"))
  4880. (defvar org-link-types-re nil
  4881. "Matches a link that has a url-like prefix like \"http:\"")
  4882. (defvar org-link-re-with-space nil
  4883. "Matches a link with spaces, optional angular brackets around it.")
  4884. (defvar org-link-re-with-space2 nil
  4885. "Matches a link with spaces, optional angular brackets around it.")
  4886. (defvar org-link-re-with-space3 nil
  4887. "Matches a link with spaces, only for internal part in bracket links.")
  4888. (defvar org-angle-link-re nil
  4889. "Matches link with angular brackets, spaces are allowed.")
  4890. (defvar org-plain-link-re nil
  4891. "Matches plain link, without spaces.")
  4892. (defvar org-bracket-link-regexp nil
  4893. "Matches a link in double brackets.")
  4894. (defvar org-bracket-link-analytic-regexp nil
  4895. "Regular expression used to analyze links.
  4896. Here is what the match groups contain after a match:
  4897. 1: http:
  4898. 2: http
  4899. 3: path
  4900. 4: [desc]
  4901. 5: desc")
  4902. (defvar org-bracket-link-analytic-regexp++ nil
  4903. "Like `org-bracket-link-analytic-regexp', but include coderef internal type.")
  4904. (defvar org-any-link-re nil
  4905. "Regular expression matching any link.")
  4906. (defconst org-match-sexp-depth 3
  4907. "Number of stacked braces for sub/superscript matching.")
  4908. (defun org-create-multibrace-regexp (left right n)
  4909. "Create a regular expression which will match a balanced sexp.
  4910. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  4911. as single character strings.
  4912. The regexp returned will match the entire expression including the
  4913. delimiters. It will also define a single group which contains the
  4914. match except for the outermost delimiters. The maximum depth of
  4915. stacked delimiters is N. Escaping delimiters is not possible."
  4916. (let* ((nothing (concat "[^" left right "]*?"))
  4917. (or "\\|")
  4918. (re nothing)
  4919. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  4920. (while (> n 1)
  4921. (setq n (1- n)
  4922. re (concat re or next)
  4923. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  4924. (concat left "\\(" re "\\)" right)))
  4925. (defvar org-match-substring-regexp
  4926. (concat
  4927. "\\(\\S-\\)\\([_^]\\)\\("
  4928. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4929. "\\|"
  4930. "\\(" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  4931. "\\|"
  4932. "\\(\\(?:\\*\\|[-+]?[^-+*!@#$%^_ \t\r\n,:\"?<>~;./{}=()]+\\)\\)\\)")
  4933. "The regular expression matching a sub- or superscript.")
  4934. (defvar org-match-substring-with-braces-regexp
  4935. (concat
  4936. "\\(\\S-\\)\\([_^]\\)\\("
  4937. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4938. "\\)")
  4939. "The regular expression matching a sub- or superscript, forcing braces.")
  4940. (defun org-make-link-regexps ()
  4941. "Update the link regular expressions.
  4942. This should be called after the variable `org-link-types' has changed."
  4943. (setq org-link-types-re
  4944. (concat
  4945. "\\`\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):")
  4946. org-link-re-with-space
  4947. (concat
  4948. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4949. "\\([^" org-non-link-chars " ]"
  4950. "[^" org-non-link-chars "]*"
  4951. "[^" org-non-link-chars " ]\\)>?")
  4952. org-link-re-with-space2
  4953. (concat
  4954. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4955. "\\([^" org-non-link-chars " ]"
  4956. "[^\t\n\r]*"
  4957. "[^" org-non-link-chars " ]\\)>?")
  4958. org-link-re-with-space3
  4959. (concat
  4960. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4961. "\\([^" org-non-link-chars " ]"
  4962. "[^\t\n\r]*\\)")
  4963. org-angle-link-re
  4964. (concat
  4965. "<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4966. "\\([^" org-non-link-chars " ]"
  4967. "[^" org-non-link-chars "]*"
  4968. "\\)>")
  4969. org-plain-link-re
  4970. (concat
  4971. "\\<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4972. (org-re "\\([^ \t\n()<>]+\\(?:([[:word:]0-9_]+)\\|\\([^[:punct:] \t\n]\\|/\\)\\)\\)"))
  4973. ;; "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  4974. org-bracket-link-regexp
  4975. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  4976. org-bracket-link-analytic-regexp
  4977. (concat
  4978. "\\[\\["
  4979. "\\(\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):\\)?"
  4980. "\\([^]]+\\)"
  4981. "\\]"
  4982. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4983. "\\]")
  4984. org-bracket-link-analytic-regexp++
  4985. (concat
  4986. "\\[\\["
  4987. "\\(\\(" (mapconcat 'regexp-quote (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  4988. "\\([^]]+\\)"
  4989. "\\]"
  4990. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4991. "\\]")
  4992. org-any-link-re
  4993. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  4994. org-angle-link-re "\\)\\|\\("
  4995. org-plain-link-re "\\)")))
  4996. (org-make-link-regexps)
  4997. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^\r\n>]*?\\)>"
  4998. "Regular expression for fast time stamp matching.")
  4999. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?\\)[]>]"
  5000. "Regular expression for fast time stamp matching.")
  5001. (defconst org-ts-regexp0
  5002. "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\)\\( +[^]+0-9>\r\n -]+\\)?\\( +\\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  5003. "Regular expression matching time strings for analysis.
  5004. This one does not require the space after the date, so it can be used
  5005. on a string that terminates immediately after the date.")
  5006. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]+0-9>\r\n -]*\\)\\( \\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  5007. "Regular expression matching time strings for analysis.")
  5008. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  5009. "Regular expression matching time stamps, with groups.")
  5010. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  5011. "Regular expression matching time stamps (also [..]), with groups.")
  5012. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  5013. "Regular expression matching a time stamp range.")
  5014. (defconst org-tr-regexp-both
  5015. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  5016. "Regular expression matching a time stamp range.")
  5017. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  5018. org-ts-regexp "\\)?")
  5019. "Regular expression matching a time stamp or time stamp range.")
  5020. (defconst org-tsr-regexp-both
  5021. (concat org-ts-regexp-both "\\(--?-?"
  5022. org-ts-regexp-both "\\)?")
  5023. "Regular expression matching a time stamp or time stamp range.
  5024. The time stamps may be either active or inactive.")
  5025. (defvar org-emph-face nil)
  5026. (defun org-do-emphasis-faces (limit)
  5027. "Run through the buffer and add overlays to emphasized strings."
  5028. (let (rtn a)
  5029. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  5030. (if (not (= (char-after (match-beginning 3))
  5031. (char-after (match-beginning 4))))
  5032. (progn
  5033. (setq rtn t)
  5034. (setq a (assoc (match-string 3) org-emphasis-alist))
  5035. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  5036. 'face
  5037. (nth 1 a))
  5038. (and (nth 4 a)
  5039. (org-remove-flyspell-overlays-in
  5040. (match-beginning 0) (match-end 0)))
  5041. (add-text-properties (match-beginning 2) (match-end 2)
  5042. '(font-lock-multiline t org-emphasis t))
  5043. (when org-hide-emphasis-markers
  5044. (add-text-properties (match-end 4) (match-beginning 5)
  5045. '(invisible org-link))
  5046. (add-text-properties (match-beginning 3) (match-end 3)
  5047. '(invisible org-link)))))
  5048. (backward-char 1))
  5049. rtn))
  5050. (defun org-emphasize (&optional char)
  5051. "Insert or change an emphasis, i.e. a font like bold or italic.
  5052. If there is an active region, change that region to a new emphasis.
  5053. If there is no region, just insert the marker characters and position
  5054. the cursor between them.
  5055. CHAR should be the marker character. If it is a space, it means to
  5056. remove the emphasis of the selected region.
  5057. If CHAR is not given (for example in an interactive call) it will be
  5058. prompted for."
  5059. (interactive)
  5060. (let ((erc org-emphasis-regexp-components)
  5061. (prompt "")
  5062. (string "") beg end move c s)
  5063. (if (org-region-active-p)
  5064. (setq beg (region-beginning) end (region-end)
  5065. string (buffer-substring beg end))
  5066. (setq move t))
  5067. (unless char
  5068. (message "Emphasis marker or tag: [%s]"
  5069. (mapconcat (lambda(e) (car e)) org-emphasis-alist ""))
  5070. (setq char (read-char-exclusive)))
  5071. (if (equal char ?\ )
  5072. (setq s "" move nil)
  5073. (unless (assoc (char-to-string char) org-emphasis-alist)
  5074. (user-error "No such emphasis marker: \"%c\"" char))
  5075. (setq s (char-to-string char)))
  5076. (while (and (> (length string) 1)
  5077. (equal (substring string 0 1) (substring string -1))
  5078. (assoc (substring string 0 1) org-emphasis-alist))
  5079. (setq string (substring string 1 -1)))
  5080. (setq string (concat s string s))
  5081. (if beg (delete-region beg end))
  5082. (unless (or (bolp)
  5083. (string-match (concat "[" (nth 0 erc) "\n]")
  5084. (char-to-string (char-before (point)))))
  5085. (insert " "))
  5086. (unless (or (eobp)
  5087. (string-match (concat "[" (nth 1 erc) "\n]")
  5088. (char-to-string (char-after (point)))))
  5089. (insert " ") (backward-char 1))
  5090. (insert string)
  5091. (and move (backward-char 1))))
  5092. (defconst org-nonsticky-props
  5093. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text htmlize-link))
  5094. (defsubst org-rear-nonsticky-at (pos)
  5095. (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
  5096. (defun org-activate-plain-links (limit)
  5097. "Run through the buffer and add overlays to links."
  5098. (let (f hl)
  5099. (when (and (re-search-forward (concat org-plain-link-re) limit t)
  5100. (not (org-in-src-block-p)))
  5101. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5102. (setq f (get-text-property (match-beginning 0) 'face))
  5103. (setq hl (org-match-string-no-properties 0))
  5104. (if (or (eq f 'org-tag)
  5105. (and (listp f) (memq 'org-tag f)))
  5106. nil
  5107. (add-text-properties (match-beginning 0) (match-end 0)
  5108. (list 'mouse-face 'highlight
  5109. 'face 'org-link
  5110. 'htmlize-link `(:uri ,hl)
  5111. 'keymap org-mouse-map))
  5112. (org-rear-nonsticky-at (match-end 0)))
  5113. t)))
  5114. (defun org-activate-code (limit)
  5115. (if (re-search-forward "^[ \t]*\\(:\\(?: .*\\|$\\)\n?\\)" limit t)
  5116. (progn
  5117. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5118. (remove-text-properties (match-beginning 0) (match-end 0)
  5119. '(display t invisible t intangible t))
  5120. t)))
  5121. (defcustom org-src-fontify-natively nil
  5122. "When non-nil, fontify code in code blocks."
  5123. :type 'boolean
  5124. :version "24.1"
  5125. :group 'org-appearance
  5126. :group 'org-babel)
  5127. (defcustom org-allow-promoting-top-level-subtree nil
  5128. "When non-nil, allow promoting a top level subtree.
  5129. The leading star of the top level headline will be replaced
  5130. by a #."
  5131. :type 'boolean
  5132. :version "24.1"
  5133. :group 'org-appearance)
  5134. (defun org-fontify-meta-lines-and-blocks (limit)
  5135. (condition-case nil
  5136. (org-fontify-meta-lines-and-blocks-1 limit)
  5137. (error (message "org-mode fontification error"))))
  5138. (defun org-fontify-meta-lines-and-blocks-1 (limit)
  5139. "Fontify #+ lines and blocks."
  5140. (let ((case-fold-search t))
  5141. (if (re-search-forward
  5142. "^\\([ \t]*#\\(\\(\\+[a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)[ \t]*\\(\\([^ \t\n]*\\)[ \t]*\\(.*\\)\\)\\)"
  5143. limit t)
  5144. (let ((beg (match-beginning 0))
  5145. (block-start (match-end 0))
  5146. (block-end nil)
  5147. (lang (match-string 7))
  5148. (beg1 (line-beginning-position 2))
  5149. (dc1 (downcase (match-string 2)))
  5150. (dc3 (downcase (match-string 3)))
  5151. end end1 quoting block-type ovl)
  5152. (cond
  5153. ((member dc1 '("+html:" "+ascii:" "+latex:"))
  5154. ;; a single line of backend-specific content
  5155. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5156. (remove-text-properties (match-beginning 0) (match-end 0)
  5157. '(display t invisible t intangible t))
  5158. (add-text-properties (match-beginning 1) (match-end 3)
  5159. '(font-lock-fontified t face org-meta-line))
  5160. (add-text-properties (match-beginning 6) (+ (match-end 6) 1)
  5161. '(font-lock-fontified t face org-block))
  5162. ; for backend-specific code
  5163. t)
  5164. ((and (match-end 4) (equal dc3 "+begin"))
  5165. ;; Truly a block
  5166. (setq block-type (downcase (match-string 5))
  5167. quoting (member block-type org-protecting-blocks))
  5168. (when (re-search-forward
  5169. (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
  5170. nil t) ;; on purpose, we look further than LIMIT
  5171. (setq end (min (point-max) (match-end 0))
  5172. end1 (min (point-max) (1- (match-beginning 0))))
  5173. (setq block-end (match-beginning 0))
  5174. (when quoting
  5175. (remove-text-properties beg end
  5176. '(display t invisible t intangible t)))
  5177. (add-text-properties
  5178. beg end
  5179. '(font-lock-fontified t font-lock-multiline t))
  5180. (add-text-properties beg beg1 '(face org-meta-line))
  5181. (add-text-properties end1 (min (point-max) (1+ end))
  5182. '(face org-meta-line)) ; for end_src
  5183. (cond
  5184. ((and lang (not (string= lang "")) org-src-fontify-natively)
  5185. (org-src-font-lock-fontify-block lang block-start block-end)
  5186. ;; remove old background overlays
  5187. (mapc (lambda (ov)
  5188. (if (eq (overlay-get ov 'face) 'org-block-background)
  5189. (delete-overlay ov)))
  5190. (overlays-at (/ (+ beg1 block-end) 2)))
  5191. ;; add a background overlay
  5192. (setq ovl (make-overlay beg1 block-end))
  5193. (overlay-put ovl 'face 'org-block-background)
  5194. (overlay-put ovl 'evaporate t)) ;; make it go away when empty
  5195. (quoting
  5196. (add-text-properties beg1 (min (point-max) (1+ end1))
  5197. '(face org-block))) ; end of source block
  5198. ((not org-fontify-quote-and-verse-blocks))
  5199. ((string= block-type "quote")
  5200. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-quote)))
  5201. ((string= block-type "verse")
  5202. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-verse))))
  5203. (add-text-properties beg beg1 '(face org-block-begin-line))
  5204. (add-text-properties (min (point-max) (1+ end)) (min (point-max) (1+ end1))
  5205. '(face org-block-end-line))
  5206. t))
  5207. ((member dc1 '("+title:" "+author:" "+email:" "+date:"))
  5208. (add-text-properties
  5209. beg (match-end 3)
  5210. (if (member (intern (substring dc1 0 -1)) org-hidden-keywords)
  5211. '(font-lock-fontified t invisible t)
  5212. '(font-lock-fontified t face org-document-info-keyword)))
  5213. (add-text-properties
  5214. (match-beginning 6) (min (point-max) (1+ (match-end 6)))
  5215. (if (string-equal dc1 "+title:")
  5216. '(font-lock-fontified t face org-document-title)
  5217. '(font-lock-fontified t face org-document-info))))
  5218. ((or (equal dc1 "+results")
  5219. (member dc1 '("+begin:" "+end:" "+caption:" "+label:"
  5220. "+orgtbl:" "+tblfm:" "+tblname:" "+results:"
  5221. "+call:" "+header:" "+headers:" "+name:"))
  5222. (and (match-end 4) (equal dc3 "+attr")))
  5223. (add-text-properties
  5224. beg (match-end 0)
  5225. '(font-lock-fontified t face org-meta-line))
  5226. t)
  5227. ((member dc3 '(" " ""))
  5228. (add-text-properties
  5229. beg (match-end 0)
  5230. '(font-lock-fontified t face font-lock-comment-face)))
  5231. ((not (member (char-after beg) '(?\ ?\t)))
  5232. ;; just any other in-buffer setting, but not indented
  5233. (add-text-properties
  5234. beg (match-end 0)
  5235. '(font-lock-fontified t face org-meta-line))
  5236. t)
  5237. (t nil))))))
  5238. (defun org-activate-angle-links (limit)
  5239. "Run through the buffer and add overlays to links."
  5240. (if (and (re-search-forward org-angle-link-re limit t)
  5241. (not (org-in-src-block-p)))
  5242. (progn
  5243. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5244. (add-text-properties (match-beginning 0) (match-end 0)
  5245. (list 'mouse-face 'highlight
  5246. 'keymap org-mouse-map))
  5247. (org-rear-nonsticky-at (match-end 0))
  5248. t)))
  5249. (defun org-activate-footnote-links (limit)
  5250. "Run through the buffer and add overlays to footnotes."
  5251. (let ((fn (org-footnote-next-reference-or-definition limit)))
  5252. (when fn
  5253. (let ((beg (nth 1 fn)) (end (nth 2 fn)))
  5254. (org-remove-flyspell-overlays-in beg end)
  5255. (add-text-properties beg end
  5256. (list 'mouse-face 'highlight
  5257. 'keymap org-mouse-map
  5258. 'help-echo
  5259. (if (= (point-at-bol) beg)
  5260. "Footnote definition"
  5261. "Footnote reference")
  5262. 'font-lock-fontified t
  5263. 'font-lock-multiline t
  5264. 'face 'org-footnote))))))
  5265. (defun org-activate-bracket-links (limit)
  5266. "Run through the buffer and add overlays to bracketed links."
  5267. (if (and (re-search-forward org-bracket-link-regexp limit t)
  5268. (not (org-in-src-block-p)))
  5269. (let* ((hl (org-match-string-no-properties 1))
  5270. (help (concat "LINK: " (save-match-data (org-link-unescape hl))))
  5271. (ip (org-maybe-intangible
  5272. (list 'invisible 'org-link
  5273. 'keymap org-mouse-map 'mouse-face 'highlight
  5274. 'font-lock-multiline t 'help-echo help
  5275. 'htmlize-link `(:uri ,hl))))
  5276. (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
  5277. 'font-lock-multiline t 'help-echo help
  5278. 'htmlize-link `(:uri ,hl))))
  5279. ;; We need to remove the invisible property here. Table narrowing
  5280. ;; may have made some of this invisible.
  5281. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5282. (remove-text-properties (match-beginning 0) (match-end 0)
  5283. '(invisible nil))
  5284. (if (match-end 3)
  5285. (progn
  5286. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  5287. (org-rear-nonsticky-at (match-beginning 3))
  5288. (add-text-properties (match-beginning 3) (match-end 3) vp)
  5289. (org-rear-nonsticky-at (match-end 3))
  5290. (add-text-properties (match-end 3) (match-end 0) ip)
  5291. (org-rear-nonsticky-at (match-end 0)))
  5292. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  5293. (org-rear-nonsticky-at (match-beginning 1))
  5294. (add-text-properties (match-beginning 1) (match-end 1) vp)
  5295. (org-rear-nonsticky-at (match-end 1))
  5296. (add-text-properties (match-end 1) (match-end 0) ip)
  5297. (org-rear-nonsticky-at (match-end 0)))
  5298. t)))
  5299. (defun org-activate-dates (limit)
  5300. "Run through the buffer and add overlays to dates."
  5301. (if (and (re-search-forward org-tsr-regexp-both limit t)
  5302. (not (equal (char-before (match-beginning 0)) 91)))
  5303. (progn
  5304. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5305. (add-text-properties (match-beginning 0) (match-end 0)
  5306. (list 'mouse-face 'highlight
  5307. 'keymap org-mouse-map))
  5308. (org-rear-nonsticky-at (match-end 0))
  5309. (when org-display-custom-times
  5310. (if (match-end 3)
  5311. (org-display-custom-time (match-beginning 3) (match-end 3)))
  5312. (org-display-custom-time (match-beginning 1) (match-end 1)))
  5313. t)))
  5314. (defvar org-target-link-regexp nil
  5315. "Regular expression matching radio targets in plain text.")
  5316. (make-variable-buffer-local 'org-target-link-regexp)
  5317. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  5318. "Regular expression matching a link target.")
  5319. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  5320. "Regular expression matching a radio target.")
  5321. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  5322. "Regular expression matching any target.")
  5323. (defun org-activate-target-links (limit)
  5324. "Run through the buffer and add overlays to target matches."
  5325. (when org-target-link-regexp
  5326. (let ((case-fold-search t))
  5327. (if (re-search-forward org-target-link-regexp limit t)
  5328. (progn
  5329. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5330. (add-text-properties (match-beginning 0) (match-end 0)
  5331. (list 'mouse-face 'highlight
  5332. 'keymap org-mouse-map
  5333. 'help-echo "Radio target link"
  5334. 'org-linked-text t))
  5335. (org-rear-nonsticky-at (match-end 0))
  5336. t)))))
  5337. (defun org-update-radio-target-regexp ()
  5338. "Find all radio targets in this file and update the regular expression."
  5339. (interactive)
  5340. (when (memq 'radio org-activate-links)
  5341. (setq org-target-link-regexp
  5342. (org-make-target-link-regexp (org-all-targets 'radio)))
  5343. (org-restart-font-lock)))
  5344. (defun org-hide-wide-columns (limit)
  5345. (let (s e)
  5346. (setq s (text-property-any (point) (or limit (point-max))
  5347. 'org-cwidth t))
  5348. (when s
  5349. (setq e (next-single-property-change s 'org-cwidth))
  5350. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  5351. (goto-char e)
  5352. t)))
  5353. (defvar org-latex-and-related-regexp nil
  5354. "Regular expression for highlighting LaTeX, entities and sub/superscript.")
  5355. (defvar org-match-substring-regexp)
  5356. (defvar org-match-substring-with-braces-regexp)
  5357. (defun org-compute-latex-and-related-regexp ()
  5358. "Compute regular expression for LaTeX, entities and sub/superscript.
  5359. Result depends on variable `org-highlight-latex-and-related'."
  5360. (org-set-local
  5361. 'org-latex-and-related-regexp
  5362. (let* ((re-sub
  5363. (cond ((not (memq 'script org-highlight-latex-and-related)) nil)
  5364. ((eq org-use-sub-superscripts '{})
  5365. (list org-match-substring-with-braces-regexp))
  5366. (org-use-sub-superscripts (list org-match-substring-regexp))))
  5367. (re-latex
  5368. (when (memq 'latex org-highlight-latex-and-related)
  5369. (let ((matchers (plist-get org-format-latex-options :matchers)))
  5370. (delq nil
  5371. (mapcar (lambda (x)
  5372. (and (member (car x) matchers) (nth 1 x)))
  5373. org-latex-regexps)))))
  5374. (re-entities
  5375. (when (memq 'entities org-highlight-latex-and-related)
  5376. (list "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)"))))
  5377. (mapconcat 'identity (append re-latex re-entities re-sub) "\\|"))))
  5378. (defun org-do-latex-and-related (limit)
  5379. "Highlight LaTeX snippets and environments, entities and sub/superscript.
  5380. LIMIT bounds the search for syntax to highlight. Stop at first
  5381. highlighted object, if any. Return t if some highlighting was
  5382. done, nil otherwise."
  5383. (when (org-string-nw-p org-latex-and-related-regexp)
  5384. (catch 'found
  5385. (while (re-search-forward org-latex-and-related-regexp limit t)
  5386. (unless (memq (car-safe (get-text-property (1+ (match-beginning 0))
  5387. 'face))
  5388. '(org-code org-verbatim underline))
  5389. (let ((offset (if (memq (char-after (1+ (match-beginning 0)))
  5390. '(?_ ?^))
  5391. 1
  5392. 0)))
  5393. (font-lock-prepend-text-property
  5394. (+ offset (match-beginning 0)) (match-end 0)
  5395. 'face 'org-latex-and-related)
  5396. (add-text-properties (+ offset (match-beginning 0)) (match-end 0)
  5397. '(font-lock-multiline t)))
  5398. (throw 'found t)))
  5399. nil)))
  5400. (defun org-restart-font-lock ()
  5401. "Restart `font-lock-mode', to force refontification."
  5402. (when (and (boundp 'font-lock-mode) font-lock-mode)
  5403. (font-lock-mode -1)
  5404. (font-lock-mode 1)))
  5405. (defun org-all-targets (&optional radio)
  5406. "Return a list of all targets in this file.
  5407. When optional argument RADIO is non-nil, only find radio
  5408. targets."
  5409. (let ((re (if radio org-radio-target-regexp org-target-regexp)) rtn)
  5410. (save-excursion
  5411. (goto-char (point-min))
  5412. (while (re-search-forward re nil t)
  5413. ;; Make sure point is really within the object.
  5414. (backward-char)
  5415. (let ((obj (org-element-context)))
  5416. (when (memq (org-element-type obj) '(radio-target target))
  5417. (add-to-list 'rtn (downcase (org-element-property :value obj))))))
  5418. rtn)))
  5419. (defun org-make-target-link-regexp (targets)
  5420. "Make regular expression matching all strings in TARGETS.
  5421. The regular expression finds the targets also if there is a line break
  5422. between words."
  5423. (and targets
  5424. (concat
  5425. "\\<\\("
  5426. (mapconcat
  5427. (lambda (x)
  5428. (setq x (regexp-quote x))
  5429. (while (string-match " +" x)
  5430. (setq x (replace-match "\\s-+" t t x)))
  5431. x)
  5432. targets
  5433. "\\|")
  5434. "\\)\\>")))
  5435. (defun org-activate-tags (limit)
  5436. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \r\n]") limit t)
  5437. (progn
  5438. (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
  5439. (add-text-properties (match-beginning 1) (match-end 1)
  5440. (list 'mouse-face 'highlight
  5441. 'keymap org-mouse-map))
  5442. (org-rear-nonsticky-at (match-end 1))
  5443. t)))
  5444. (defun org-outline-level ()
  5445. "Compute the outline level of the heading at point.
  5446. If this is called at a normal headline, the level is the number of stars.
  5447. Use `org-reduced-level' to remove the effect of `org-odd-levels'."
  5448. (save-excursion
  5449. (if (not (condition-case nil
  5450. (org-back-to-heading t)
  5451. (error nil)))
  5452. 0
  5453. (looking-at org-outline-regexp)
  5454. (1- (- (match-end 0) (match-beginning 0))))))
  5455. (defvar org-font-lock-keywords nil)
  5456. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\+?\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  5457. "Regular expression matching a property line.")
  5458. (defvar org-font-lock-hook nil
  5459. "Functions to be called for special font lock stuff.")
  5460. (defvar org-font-lock-set-keywords-hook nil
  5461. "Functions that can manipulate `org-font-lock-extra-keywords'.
  5462. This is called after `org-font-lock-extra-keywords' is defined, but before
  5463. it is installed to be used by font lock. This can be useful if something
  5464. needs to be inserted at a specific position in the font-lock sequence.")
  5465. (defun org-font-lock-hook (limit)
  5466. "Run `org-font-lock-hook' within LIMIT."
  5467. (run-hook-with-args 'org-font-lock-hook limit))
  5468. (defun org-set-font-lock-defaults ()
  5469. "Set font lock defaults for the current buffer."
  5470. (let* ((em org-fontify-emphasized-text)
  5471. (lk org-activate-links)
  5472. (org-font-lock-extra-keywords
  5473. (list
  5474. ;; Call the hook
  5475. '(org-font-lock-hook)
  5476. ;; Headlines
  5477. `(,(if org-fontify-whole-heading-line
  5478. "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
  5479. "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
  5480. (1 (org-get-level-face 1))
  5481. (2 (org-get-level-face 2))
  5482. (3 (org-get-level-face 3)))
  5483. ;; Table lines
  5484. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  5485. (1 'org-table t))
  5486. ;; Table internals
  5487. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  5488. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  5489. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  5490. '("| *\\(<[lrc]?[0-9]*>\\)" (1 'org-formula t))
  5491. ;; Drawers
  5492. (list org-drawer-regexp '(0 'org-special-keyword t))
  5493. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  5494. ;; Properties
  5495. (list org-property-re
  5496. '(1 'org-special-keyword t)
  5497. '(3 'org-property-value t))
  5498. ;; Links
  5499. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  5500. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  5501. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  5502. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  5503. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  5504. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  5505. (if (memq 'footnote lk) '(org-activate-footnote-links))
  5506. ;; Targets.
  5507. (list org-any-target-regexp '(0 'org-target t))
  5508. ;; Diary sexps.
  5509. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  5510. ;; Macro
  5511. '("{{{.+}}}" (0 'org-macro t))
  5512. '(org-hide-wide-columns (0 nil append))
  5513. ;; TODO keyword
  5514. (list (format org-heading-keyword-regexp-format
  5515. org-todo-regexp)
  5516. '(2 (org-get-todo-face 2) t))
  5517. ;; DONE
  5518. (if org-fontify-done-headline
  5519. (list (format org-heading-keyword-regexp-format
  5520. (concat
  5521. "\\(?:"
  5522. (mapconcat 'regexp-quote org-done-keywords "\\|")
  5523. "\\)"))
  5524. '(2 'org-headline-done t))
  5525. nil)
  5526. ;; Priorities
  5527. '(org-font-lock-add-priority-faces)
  5528. ;; Tags
  5529. '(org-font-lock-add-tag-faces)
  5530. ;; Tags groups
  5531. (if (and org-group-tags org-tag-groups-alist)
  5532. (list (concat org-outline-regexp-bol ".+\\(:"
  5533. (regexp-opt (mapcar 'car org-tag-groups-alist))
  5534. ":\\).*$")
  5535. '(1 'org-tag-group prepend)))
  5536. ;; Special keywords
  5537. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  5538. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  5539. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  5540. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  5541. ;; Emphasis
  5542. (if em
  5543. (if (featurep 'xemacs)
  5544. '(org-do-emphasis-faces (0 nil append))
  5545. '(org-do-emphasis-faces)))
  5546. ;; Checkboxes
  5547. '("^[ \t]*\\(?:[-+*]\\|[0-9]+[.)]\\)[ \t]+\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?\\(\\[[- X]\\]\\)"
  5548. 1 'org-checkbox prepend)
  5549. (if (cdr (assq 'checkbox org-list-automatic-rules))
  5550. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  5551. (0 (org-get-checkbox-statistics-face) t)))
  5552. ;; Description list items
  5553. '("^[ \t]*[-+*][ \t]+\\(.*?[ \t]+::\\)\\([ \t]+\\|$\\)"
  5554. 1 'org-list-dt prepend)
  5555. ;; ARCHIVEd headings
  5556. (list (concat
  5557. org-outline-regexp-bol
  5558. "\\(.*:" org-archive-tag ":.*\\)")
  5559. '(1 'org-archived prepend))
  5560. ;; Specials
  5561. '(org-do-latex-and-related)
  5562. '(org-fontify-entities)
  5563. '(org-raise-scripts)
  5564. ;; Code
  5565. '(org-activate-code (1 'org-code t))
  5566. ;; COMMENT
  5567. (list (format org-heading-keyword-regexp-format
  5568. (concat "\\("
  5569. org-comment-string "\\|" org-quote-string
  5570. "\\)"))
  5571. '(2 'org-special-keyword t))
  5572. ;; Blocks and meta lines
  5573. '(org-fontify-meta-lines-and-blocks))))
  5574. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  5575. (run-hooks 'org-font-lock-set-keywords-hook)
  5576. ;; Now set the full font-lock-keywords
  5577. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  5578. (org-set-local 'font-lock-defaults
  5579. '(org-font-lock-keywords t nil nil backward-paragraph))
  5580. (kill-local-variable 'font-lock-keywords) nil))
  5581. (defun org-toggle-pretty-entities ()
  5582. "Toggle the composition display of entities as UTF8 characters."
  5583. (interactive)
  5584. (org-set-local 'org-pretty-entities (not org-pretty-entities))
  5585. (org-restart-font-lock)
  5586. (if org-pretty-entities
  5587. (message "Entities are now displayed as UTF8 characters")
  5588. (save-restriction
  5589. (widen)
  5590. (org-decompose-region (point-min) (point-max))
  5591. (message "Entities are now displayed as plain text"))))
  5592. (defvar org-custom-properties-overlays nil
  5593. "List of overlays used for custom properties.")
  5594. (make-variable-buffer-local 'org-custom-properties-overlays)
  5595. (defun org-toggle-custom-properties-visibility ()
  5596. "Display or hide properties in `org-custom-properties'."
  5597. (interactive)
  5598. (if org-custom-properties-overlays
  5599. (progn (mapc 'delete-overlay org-custom-properties-overlays)
  5600. (setq org-custom-properties-overlays nil))
  5601. (unless (not org-custom-properties)
  5602. (save-excursion
  5603. (save-restriction
  5604. (widen)
  5605. (goto-char (point-min))
  5606. (while (re-search-forward org-property-re nil t)
  5607. (mapc (lambda(p)
  5608. (when (equal p (substring (match-string 1) 1 -1))
  5609. (let ((o (make-overlay (match-beginning 0) (1+ (match-end 0)))))
  5610. (overlay-put o 'invisible t)
  5611. (overlay-put o 'org-custom-property t)
  5612. (push o org-custom-properties-overlays))))
  5613. org-custom-properties)))))))
  5614. (defun org-fontify-entities (limit)
  5615. "Find an entity to fontify."
  5616. (let (ee)
  5617. (when org-pretty-entities
  5618. (catch 'match
  5619. (while (re-search-forward
  5620. "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]\n]\\)"
  5621. limit t)
  5622. (if (and (not (org-in-indented-comment-line))
  5623. (setq ee (org-entity-get (match-string 1)))
  5624. (= (length (nth 6 ee)) 1))
  5625. (let*
  5626. ((end (if (equal (match-string 2) "{}")
  5627. (match-end 2)
  5628. (match-end 1))))
  5629. (add-text-properties
  5630. (match-beginning 0) end
  5631. (list 'font-lock-fontified t))
  5632. (compose-region (match-beginning 0) end
  5633. (nth 6 ee) nil)
  5634. (backward-char 1)
  5635. (throw 'match t))))
  5636. nil))))
  5637. (defun org-fontify-like-in-org-mode (s &optional odd-levels)
  5638. "Fontify string S like in Org-mode."
  5639. (with-temp-buffer
  5640. (insert s)
  5641. (let ((org-odd-levels-only odd-levels))
  5642. (org-mode)
  5643. (font-lock-fontify-buffer)
  5644. (buffer-string))))
  5645. (defvar org-m nil)
  5646. (defvar org-l nil)
  5647. (defvar org-f nil)
  5648. (defun org-get-level-face (n)
  5649. "Get the right face for match N in font-lock matching of headlines."
  5650. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  5651. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  5652. (if org-cycle-level-faces
  5653. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  5654. (setq org-f (nth (1- (min org-l org-n-level-faces)) org-level-faces)))
  5655. (cond
  5656. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  5657. ((eq n 2) org-f)
  5658. (t (if org-level-color-stars-only nil org-f))))
  5659. (defun org-get-todo-face (kwd)
  5660. "Get the right face for a TODO keyword KWD.
  5661. If KWD is a number, get the corresponding match group."
  5662. (if (numberp kwd) (setq kwd (match-string kwd)))
  5663. (or (org-face-from-face-or-color
  5664. 'todo 'org-todo (cdr (assoc kwd org-todo-keyword-faces)))
  5665. (and (member kwd org-done-keywords) 'org-done)
  5666. 'org-todo))
  5667. (defun org-face-from-face-or-color (context inherit face-or-color)
  5668. "Create a face list that inherits INHERIT, but sets the foreground color.
  5669. When FACE-OR-COLOR is not a string, just return it."
  5670. (if (stringp face-or-color)
  5671. (list :inherit inherit
  5672. (cdr (assoc context org-faces-easy-properties))
  5673. face-or-color)
  5674. face-or-color))
  5675. (defun org-font-lock-add-tag-faces (limit)
  5676. "Add the special tag faces."
  5677. (when (and org-tag-faces org-tags-special-faces-re)
  5678. (while (re-search-forward org-tags-special-faces-re limit t)
  5679. (add-text-properties (match-beginning 1) (match-end 1)
  5680. (list 'face (org-get-tag-face 1)
  5681. 'font-lock-fontified t))
  5682. (backward-char 1))))
  5683. (defun org-font-lock-add-priority-faces (limit)
  5684. "Add the special priority faces."
  5685. (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
  5686. (when (save-match-data (org-at-heading-p))
  5687. (add-text-properties
  5688. (match-beginning 0) (match-end 0)
  5689. (list 'face (or (org-face-from-face-or-color
  5690. 'priority 'org-priority
  5691. (cdr (assoc (char-after (match-beginning 1))
  5692. org-priority-faces)))
  5693. 'org-priority)
  5694. 'font-lock-fontified t)))))
  5695. (defun org-get-tag-face (kwd)
  5696. "Get the right face for a TODO keyword KWD.
  5697. If KWD is a number, get the corresponding match group."
  5698. (if (numberp kwd) (setq kwd (match-string kwd)))
  5699. (or (org-face-from-face-or-color
  5700. 'tag 'org-tag (cdr (assoc kwd org-tag-faces)))
  5701. 'org-tag))
  5702. (defun org-unfontify-region (beg end &optional maybe_loudly)
  5703. "Remove fontification and activation overlays from links."
  5704. (font-lock-default-unfontify-region beg end)
  5705. (let* ((buffer-undo-list t)
  5706. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  5707. (inhibit-modification-hooks t)
  5708. deactivate-mark buffer-file-name buffer-file-truename)
  5709. (org-decompose-region beg end)
  5710. (remove-text-properties beg end
  5711. '(mouse-face t keymap t org-linked-text t
  5712. invisible t intangible t
  5713. org-no-flyspell t org-emphasis t))
  5714. (org-remove-font-lock-display-properties beg end)))
  5715. (defconst org-script-display '(((raise -0.3) (height 0.7))
  5716. ((raise 0.3) (height 0.7))
  5717. ((raise -0.5))
  5718. ((raise 0.5)))
  5719. "Display properties for showing superscripts and subscripts.")
  5720. (defun org-remove-font-lock-display-properties (beg end)
  5721. "Remove specific display properties that have been added by font lock.
  5722. The will remove the raise properties that are used to show superscripts
  5723. and subscripts."
  5724. (let (next prop)
  5725. (while (< beg end)
  5726. (setq next (next-single-property-change beg 'display nil end)
  5727. prop (get-text-property beg 'display))
  5728. (if (member prop org-script-display)
  5729. (put-text-property beg next 'display nil))
  5730. (setq beg next))))
  5731. (defun org-raise-scripts (limit)
  5732. "Add raise properties to sub/superscripts."
  5733. (when (and org-pretty-entities org-pretty-entities-include-sub-superscripts)
  5734. (if (re-search-forward
  5735. (if (eq org-use-sub-superscripts t)
  5736. org-match-substring-regexp
  5737. org-match-substring-with-braces-regexp)
  5738. limit t)
  5739. (let* ((pos (point)) table-p comment-p
  5740. (mpos (match-beginning 3))
  5741. (emph-p (get-text-property mpos 'org-emphasis))
  5742. (link-p (get-text-property mpos 'mouse-face))
  5743. (keyw-p (eq 'org-special-keyword (get-text-property mpos 'face))))
  5744. (goto-char (point-at-bol))
  5745. (setq table-p (org-looking-at-p org-table-dataline-regexp)
  5746. comment-p (org-looking-at-p "^[ \t]*#[ +]"))
  5747. (goto-char pos)
  5748. ;; Handle a_b^c
  5749. (if (member (char-after) '(?_ ?^)) (goto-char (1- pos)))
  5750. (if (or comment-p emph-p link-p keyw-p)
  5751. t
  5752. (put-text-property (match-beginning 3) (match-end 0)
  5753. 'display
  5754. (if (equal (char-after (match-beginning 2)) ?^)
  5755. (nth (if table-p 3 1) org-script-display)
  5756. (nth (if table-p 2 0) org-script-display)))
  5757. (add-text-properties (match-beginning 2) (match-end 2)
  5758. (list 'invisible t
  5759. 'org-dwidth t 'org-dwidth-n 1))
  5760. (if (and (eq (char-after (match-beginning 3)) ?{)
  5761. (eq (char-before (match-end 3)) ?}))
  5762. (progn
  5763. (add-text-properties
  5764. (match-beginning 3) (1+ (match-beginning 3))
  5765. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))
  5766. (add-text-properties
  5767. (1- (match-end 3)) (match-end 3)
  5768. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))))
  5769. t)))))
  5770. ;;;; Visibility cycling, including org-goto and indirect buffer
  5771. ;;; Cycling
  5772. (defvar org-cycle-global-status nil)
  5773. (make-variable-buffer-local 'org-cycle-global-status)
  5774. (put 'org-cycle-global-status 'org-state t)
  5775. (defvar org-cycle-subtree-status nil)
  5776. (make-variable-buffer-local 'org-cycle-subtree-status)
  5777. (put 'org-cycle-subtree-status 'org-state t)
  5778. (defvar org-inlinetask-min-level)
  5779. ;;;###autoload
  5780. (defun org-cycle (&optional arg)
  5781. "TAB-action and visibility cycling for Org-mode.
  5782. This is the command invoked in Org-mode by the TAB key. Its main purpose
  5783. is outline visibility cycling, but it also invokes other actions
  5784. in special contexts.
  5785. - When this function is called with a prefix argument, rotate the entire
  5786. buffer through 3 states (global cycling)
  5787. 1. OVERVIEW: Show only top-level headlines.
  5788. 2. CONTENTS: Show all headlines of all levels, but no body text.
  5789. 3. SHOW ALL: Show everything.
  5790. When called with two `C-u C-u' prefixes, switch to the startup visibility,
  5791. determined by the variable `org-startup-folded', and by any VISIBILITY
  5792. properties in the buffer.
  5793. When called with three `C-u C-u C-u' prefixed, show the entire buffer,
  5794. including any drawers.
  5795. - When inside a table, re-align the table and move to the next field.
  5796. - When point is at the beginning of a headline, rotate the subtree started
  5797. by this line through 3 different states (local cycling)
  5798. 1. FOLDED: Only the main headline is shown.
  5799. 2. CHILDREN: The main headline and the direct children are shown.
  5800. From this state, you can move to one of the children
  5801. and zoom in further.
  5802. 3. SUBTREE: Show the entire subtree, including body text.
  5803. If there is no subtree, switch directly from CHILDREN to FOLDED.
  5804. - When point is at the beginning of an empty headline and the variable
  5805. `org-cycle-level-after-item/entry-creation' is set, cycle the level
  5806. of the headline by demoting and promoting it to likely levels. This
  5807. speeds up creation document structure by pressing TAB once or several
  5808. times right after creating a new headline.
  5809. - When there is a numeric prefix, go up to a heading with level ARG, do
  5810. a `show-subtree' and return to the previous cursor position. If ARG
  5811. is negative, go up that many levels.
  5812. - When point is not at the beginning of a headline, execute the global
  5813. binding for TAB, which is re-indenting the line. See the option
  5814. `org-cycle-emulate-tab' for details.
  5815. - Special case: if point is at the beginning of the buffer and there is
  5816. no headline in line 1, this function will act as if called with prefix arg
  5817. (C-u TAB, same as S-TAB) also when called without prefix arg.
  5818. But only if also the variable `org-cycle-global-at-bob' is t."
  5819. (interactive "P")
  5820. (org-load-modules-maybe)
  5821. (unless (or (run-hook-with-args-until-success 'org-tab-first-hook)
  5822. (and org-cycle-level-after-item/entry-creation
  5823. (or (org-cycle-level)
  5824. (org-cycle-item-indentation))))
  5825. (let* (message-log-max ; Don't populate the *Messages* buffer
  5826. (limit-level
  5827. (or org-cycle-max-level
  5828. (and (boundp 'org-inlinetask-min-level)
  5829. org-inlinetask-min-level
  5830. (1- org-inlinetask-min-level))))
  5831. (nstars (and limit-level
  5832. (if org-odd-levels-only
  5833. (and limit-level (1- (* limit-level 2)))
  5834. limit-level)))
  5835. (org-outline-regexp
  5836. (if (not (derived-mode-p 'org-mode))
  5837. outline-regexp
  5838. (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ "))))
  5839. (bob-special (and org-cycle-global-at-bob (not arg) (bobp)
  5840. (not (looking-at org-outline-regexp))))
  5841. (org-cycle-hook
  5842. (if bob-special
  5843. (delq 'org-optimize-window-after-visibility-change
  5844. (copy-sequence org-cycle-hook))
  5845. org-cycle-hook))
  5846. (pos (point)))
  5847. (if (or bob-special (equal arg '(4)))
  5848. ;; special case: use global cycling
  5849. (setq arg t))
  5850. (cond
  5851. ((equal arg '(16))
  5852. (setq last-command 'dummy)
  5853. (org-set-startup-visibility)
  5854. (message "Startup visibility, plus VISIBILITY properties"))
  5855. ((equal arg '(64))
  5856. (show-all)
  5857. (message "Entire buffer visible, including drawers"))
  5858. ;; Table: enter it or move to the next field.
  5859. ((org-at-table-p 'any)
  5860. (if (org-at-table.el-p)
  5861. (message "Use C-c ' to edit table.el tables")
  5862. (if arg (org-table-edit-field t)
  5863. (org-table-justify-field-maybe)
  5864. (call-interactively 'org-table-next-field))))
  5865. ((run-hook-with-args-until-success
  5866. 'org-tab-after-check-for-table-hook))
  5867. ;; Global cycling: delegate to `org-cycle-internal-global'.
  5868. ((eq arg t) (org-cycle-internal-global))
  5869. ;; Drawers: delegate to `org-flag-drawer'.
  5870. ((and org-drawers org-drawer-regexp
  5871. (save-excursion
  5872. (beginning-of-line 1)
  5873. (looking-at org-drawer-regexp)))
  5874. (org-flag-drawer ; toggle block visibility
  5875. (not (get-char-property (match-end 0) 'invisible))))
  5876. ;; Show-subtree, ARG levels up from here.
  5877. ((integerp arg)
  5878. (save-excursion
  5879. (org-back-to-heading)
  5880. (outline-up-heading (if (< arg 0) (- arg)
  5881. (- (funcall outline-level) arg)))
  5882. (org-show-subtree)))
  5883. ;; Inline task: delegate to `org-inlinetask-toggle-visibility'.
  5884. ((and (featurep 'org-inlinetask)
  5885. (org-inlinetask-at-task-p)
  5886. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5887. (org-inlinetask-toggle-visibility))
  5888. ((org-try-cdlatex-tab))
  5889. ;; At an item/headline: delegate to `org-cycle-internal-local'.
  5890. ((and (or (and org-cycle-include-plain-lists (org-at-item-p))
  5891. (save-excursion (beginning-of-line 1)
  5892. (looking-at org-outline-regexp)))
  5893. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5894. (org-cycle-internal-local))
  5895. ;; From there: TAB emulation and template completion.
  5896. (buffer-read-only (org-back-to-heading))
  5897. ((run-hook-with-args-until-success
  5898. 'org-tab-after-check-for-cycling-hook))
  5899. ((org-try-structure-completion))
  5900. ((run-hook-with-args-until-success
  5901. 'org-tab-before-tab-emulation-hook))
  5902. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  5903. (or (not (bolp))
  5904. (not (looking-at org-outline-regexp))))
  5905. (call-interactively (global-key-binding "\t")))
  5906. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  5907. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  5908. (or (and (eq org-cycle-emulate-tab 'white)
  5909. (= (match-end 0) (point-at-eol)))
  5910. (and (eq org-cycle-emulate-tab 'whitestart)
  5911. (>= (match-end 0) pos))))
  5912. t
  5913. (eq org-cycle-emulate-tab t))
  5914. (call-interactively (global-key-binding "\t")))
  5915. (t (save-excursion
  5916. (org-back-to-heading)
  5917. (org-cycle)))))))
  5918. (defun org-cycle-internal-global ()
  5919. "Do the global cycling action."
  5920. ;; Hack to avoid display of messages for .org attachments in Gnus
  5921. (let (message-log-max ; Don't populate the *Messages* buffer
  5922. (ga (string-match "\\*fontification" (buffer-name))))
  5923. (cond
  5924. ((and (eq last-command this-command)
  5925. (eq org-cycle-global-status 'overview))
  5926. ;; We just created the overview - now do table of contents
  5927. ;; This can be slow in very large buffers, so indicate action
  5928. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  5929. (unless ga (message "CONTENTS..."))
  5930. (org-content)
  5931. (unless ga (message "CONTENTS...done"))
  5932. (setq org-cycle-global-status 'contents)
  5933. (run-hook-with-args 'org-cycle-hook 'contents))
  5934. ((and (eq last-command this-command)
  5935. (eq org-cycle-global-status 'contents))
  5936. ;; We just showed the table of contents - now show everything
  5937. (run-hook-with-args 'org-pre-cycle-hook 'all)
  5938. (show-all)
  5939. (unless ga (message "SHOW ALL"))
  5940. (setq org-cycle-global-status 'all)
  5941. (run-hook-with-args 'org-cycle-hook 'all))
  5942. (t
  5943. ;; Default action: go to overview
  5944. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  5945. (org-overview)
  5946. (unless ga (message "OVERVIEW"))
  5947. (setq org-cycle-global-status 'overview)
  5948. (run-hook-with-args 'org-cycle-hook 'overview)))))
  5949. (defun org-cycle-internal-local ()
  5950. "Do the local cycling action."
  5951. (let (message-log-max ; Don't populate the *Messages* buffer
  5952. (goal-column 0) eoh eol eos has-children children-skipped struct)
  5953. ;; First, determine end of headline (EOH), end of subtree or item
  5954. ;; (EOS), and if item or heading has children (HAS-CHILDREN).
  5955. (save-excursion
  5956. (if (org-at-item-p)
  5957. (progn
  5958. (beginning-of-line)
  5959. (setq struct (org-list-struct))
  5960. (setq eoh (point-at-eol))
  5961. (setq eos (org-list-get-item-end-before-blank (point) struct))
  5962. (setq has-children (org-list-has-child-p (point) struct)))
  5963. (org-back-to-heading)
  5964. (setq eoh (save-excursion (outline-end-of-heading) (point)))
  5965. (setq eos (save-excursion (1- (org-end-of-subtree t t))))
  5966. (setq has-children
  5967. (or (save-excursion
  5968. (let ((level (funcall outline-level)))
  5969. (outline-next-heading)
  5970. (and (org-at-heading-p t)
  5971. (> (funcall outline-level) level))))
  5972. (save-excursion
  5973. (org-list-search-forward (org-item-beginning-re) eos t)))))
  5974. ;; Determine end invisible part of buffer (EOL)
  5975. (beginning-of-line 2)
  5976. ;; XEmacs doesn't have `next-single-char-property-change'
  5977. (if (featurep 'xemacs)
  5978. (while (and (not (eobp)) ;; this is like `next-line'
  5979. (get-char-property (1- (point)) 'invisible))
  5980. (beginning-of-line 2))
  5981. (while (and (not (eobp)) ;; this is like `next-line'
  5982. (get-char-property (1- (point)) 'invisible))
  5983. (goto-char (next-single-char-property-change (point) 'invisible))
  5984. (and (eolp) (beginning-of-line 2))))
  5985. (setq eol (point)))
  5986. ;; Find out what to do next and set `this-command'
  5987. (cond
  5988. ((= eos eoh)
  5989. ;; Nothing is hidden behind this heading
  5990. (unless (org-before-first-heading-p)
  5991. (run-hook-with-args 'org-pre-cycle-hook 'empty))
  5992. (message "EMPTY ENTRY")
  5993. (setq org-cycle-subtree-status nil)
  5994. (save-excursion
  5995. (goto-char eos)
  5996. (outline-next-heading)
  5997. (if (outline-invisible-p) (org-flag-heading nil))))
  5998. ((and (or (>= eol eos)
  5999. (not (string-match "\\S-" (buffer-substring eol eos))))
  6000. (or has-children
  6001. (not (setq children-skipped
  6002. org-cycle-skip-children-state-if-no-children))))
  6003. ;; Entire subtree is hidden in one line: children view
  6004. (unless (org-before-first-heading-p)
  6005. (run-hook-with-args 'org-pre-cycle-hook 'children))
  6006. (if (org-at-item-p)
  6007. (org-list-set-item-visibility (point-at-bol) struct 'children)
  6008. (org-show-entry)
  6009. (org-with-limited-levels (show-children))
  6010. ;; FIXME: This slows down the func way too much.
  6011. ;; How keep drawers hidden in subtree anyway?
  6012. ;; (when (memq 'org-cycle-hide-drawers org-cycle-hook)
  6013. ;; (org-cycle-hide-drawers 'subtree))
  6014. ;; Fold every list in subtree to top-level items.
  6015. (when (eq org-cycle-include-plain-lists 'integrate)
  6016. (save-excursion
  6017. (org-back-to-heading)
  6018. (while (org-list-search-forward (org-item-beginning-re) eos t)
  6019. (beginning-of-line 1)
  6020. (let* ((struct (org-list-struct))
  6021. (prevs (org-list-prevs-alist struct))
  6022. (end (org-list-get-bottom-point struct)))
  6023. (mapc (lambda (e) (org-list-set-item-visibility e struct 'folded))
  6024. (org-list-get-all-items (point) struct prevs))
  6025. (goto-char (if (< end eos) end eos)))))))
  6026. (message "CHILDREN")
  6027. (save-excursion
  6028. (goto-char eos)
  6029. (outline-next-heading)
  6030. (if (outline-invisible-p) (org-flag-heading nil)))
  6031. (setq org-cycle-subtree-status 'children)
  6032. (unless (org-before-first-heading-p)
  6033. (run-hook-with-args 'org-cycle-hook 'children)))
  6034. ((or children-skipped
  6035. (and (eq last-command this-command)
  6036. (eq org-cycle-subtree-status 'children)))
  6037. ;; We just showed the children, or no children are there,
  6038. ;; now show everything.
  6039. (unless (org-before-first-heading-p)
  6040. (run-hook-with-args 'org-pre-cycle-hook 'subtree))
  6041. (outline-flag-region eoh eos nil)
  6042. (message (if children-skipped "SUBTREE (NO CHILDREN)" "SUBTREE"))
  6043. (setq org-cycle-subtree-status 'subtree)
  6044. (unless (org-before-first-heading-p)
  6045. (run-hook-with-args 'org-cycle-hook 'subtree)))
  6046. (t
  6047. ;; Default action: hide the subtree.
  6048. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  6049. (outline-flag-region eoh eos t)
  6050. (message "FOLDED")
  6051. (setq org-cycle-subtree-status 'folded)
  6052. (unless (org-before-first-heading-p)
  6053. (run-hook-with-args 'org-cycle-hook 'folded))))))
  6054. ;;;###autoload
  6055. (defun org-global-cycle (&optional arg)
  6056. "Cycle the global visibility. For details see `org-cycle'.
  6057. With \\[universal-argument] prefix arg, switch to startup visibility.
  6058. With a numeric prefix, show all headlines up to that level."
  6059. (interactive "P")
  6060. (let ((org-cycle-include-plain-lists
  6061. (if (derived-mode-p 'org-mode) org-cycle-include-plain-lists nil)))
  6062. (cond
  6063. ((integerp arg)
  6064. (show-all)
  6065. (hide-sublevels arg)
  6066. (setq org-cycle-global-status 'contents))
  6067. ((equal arg '(4))
  6068. (org-set-startup-visibility)
  6069. (message "Startup visibility, plus VISIBILITY properties."))
  6070. (t
  6071. (org-cycle '(4))))))
  6072. (defun org-set-startup-visibility ()
  6073. "Set the visibility required by startup options and properties."
  6074. (cond
  6075. ((eq org-startup-folded t)
  6076. (org-cycle '(4)))
  6077. ((eq org-startup-folded 'content)
  6078. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  6079. (org-cycle '(4)) (org-cycle '(4)))))
  6080. (unless (eq org-startup-folded 'showeverything)
  6081. (if org-hide-block-startup (org-hide-block-all))
  6082. (org-set-visibility-according-to-property 'no-cleanup)
  6083. (org-cycle-hide-archived-subtrees 'all)
  6084. (org-cycle-hide-drawers 'all)
  6085. (org-cycle-show-empty-lines t)))
  6086. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  6087. "Switch subtree visibilities according to :VISIBILITY: property."
  6088. (interactive)
  6089. (let (org-show-entry-below state)
  6090. (save-excursion
  6091. (goto-char (point-min))
  6092. (while (re-search-forward
  6093. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  6094. nil t)
  6095. (setq state (match-string 1))
  6096. (save-excursion
  6097. (org-back-to-heading t)
  6098. (hide-subtree)
  6099. (org-reveal)
  6100. (cond
  6101. ((equal state '("fold" "folded"))
  6102. (hide-subtree))
  6103. ((equal state "children")
  6104. (org-show-hidden-entry)
  6105. (show-children))
  6106. ((equal state "content")
  6107. (save-excursion
  6108. (save-restriction
  6109. (org-narrow-to-subtree)
  6110. (org-content))))
  6111. ((member state '("all" "showall"))
  6112. (show-subtree)))))
  6113. (unless no-cleanup
  6114. (org-cycle-hide-archived-subtrees 'all)
  6115. (org-cycle-hide-drawers 'all)
  6116. (org-cycle-show-empty-lines 'all)))))
  6117. ;; This function uses outline-regexp instead of the more fundamental
  6118. ;; org-outline-regexp so that org-cycle-global works outside of Org
  6119. ;; buffers, where outline-regexp is needed.
  6120. (defun org-overview ()
  6121. "Switch to overview mode, showing only top-level headlines.
  6122. Really, this shows all headlines with level equal or greater than the level
  6123. of the first headline in the buffer. This is important, because if the
  6124. first headline is not level one, then (hide-sublevels 1) gives confusing
  6125. results."
  6126. (interactive)
  6127. (let ((l (org-current-line))
  6128. (level (save-excursion
  6129. (goto-char (point-min))
  6130. (if (re-search-forward (concat "^" outline-regexp) nil t)
  6131. (progn
  6132. (goto-char (match-beginning 0))
  6133. (funcall outline-level))))))
  6134. (and level (hide-sublevels level))
  6135. (recenter '(4))
  6136. (org-goto-line l)))
  6137. (defun org-content (&optional arg)
  6138. "Show all headlines in the buffer, like a table of contents.
  6139. With numerical argument N, show content up to level N."
  6140. (interactive "P")
  6141. (save-excursion
  6142. ;; Visit all headings and show their offspring
  6143. (and (integerp arg) (org-overview))
  6144. (goto-char (point-max))
  6145. (catch 'exit
  6146. (while (and (progn (condition-case nil
  6147. (outline-previous-visible-heading 1)
  6148. (error (goto-char (point-min))))
  6149. t)
  6150. (looking-at org-outline-regexp))
  6151. (if (integerp arg)
  6152. (show-children (1- arg))
  6153. (show-branches))
  6154. (if (bobp) (throw 'exit nil))))))
  6155. (defun org-optimize-window-after-visibility-change (state)
  6156. "Adjust the window after a change in outline visibility.
  6157. This function is the default value of the hook `org-cycle-hook'."
  6158. (when (get-buffer-window (current-buffer))
  6159. (cond
  6160. ((eq state 'content) nil)
  6161. ((eq state 'all) nil)
  6162. ((eq state 'folded) nil)
  6163. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  6164. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  6165. (defun org-remove-empty-overlays-at (pos)
  6166. "Remove outline overlays that do not contain non-white stuff."
  6167. (mapc
  6168. (lambda (o)
  6169. (and (eq 'outline (overlay-get o 'invisible))
  6170. (not (string-match "\\S-" (buffer-substring (overlay-start o)
  6171. (overlay-end o))))
  6172. (delete-overlay o)))
  6173. (overlays-at pos)))
  6174. (defun org-clean-visibility-after-subtree-move ()
  6175. "Fix visibility issues after moving a subtree."
  6176. ;; First, find a reasonable region to look at:
  6177. ;; Start two siblings above, end three below
  6178. (let* ((beg (save-excursion
  6179. (and (org-get-last-sibling)
  6180. (org-get-last-sibling))
  6181. (point)))
  6182. (end (save-excursion
  6183. (and (org-get-next-sibling)
  6184. (org-get-next-sibling)
  6185. (org-get-next-sibling))
  6186. (if (org-at-heading-p)
  6187. (point-at-eol)
  6188. (point))))
  6189. (level (looking-at "\\*+"))
  6190. (re (if level (concat "^" (regexp-quote (match-string 0)) " "))))
  6191. (save-excursion
  6192. (save-restriction
  6193. (narrow-to-region beg end)
  6194. (when re
  6195. ;; Properly fold already folded siblings
  6196. (goto-char (point-min))
  6197. (while (re-search-forward re nil t)
  6198. (if (and (not (outline-invisible-p))
  6199. (save-excursion
  6200. (goto-char (point-at-eol)) (outline-invisible-p)))
  6201. (hide-entry))))
  6202. (org-cycle-show-empty-lines 'overview)
  6203. (org-cycle-hide-drawers 'overview)))))
  6204. (defun org-cycle-show-empty-lines (state)
  6205. "Show empty lines above all visible headlines.
  6206. The region to be covered depends on STATE when called through
  6207. `org-cycle-hook'. Lisp program can use t for STATE to get the
  6208. entire buffer covered. Note that an empty line is only shown if there
  6209. are at least `org-cycle-separator-lines' empty lines before the headline."
  6210. (when (not (= org-cycle-separator-lines 0))
  6211. (save-excursion
  6212. (let* ((n (abs org-cycle-separator-lines))
  6213. (re (cond
  6214. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  6215. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  6216. (t (let ((ns (number-to-string (- n 2))))
  6217. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  6218. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  6219. beg end b e)
  6220. (cond
  6221. ((memq state '(overview contents t))
  6222. (setq beg (point-min) end (point-max)))
  6223. ((memq state '(children folded))
  6224. (setq beg (point) end (progn (org-end-of-subtree t t)
  6225. (beginning-of-line 2)
  6226. (point)))))
  6227. (when beg
  6228. (goto-char beg)
  6229. (while (re-search-forward re end t)
  6230. (unless (get-char-property (match-end 1) 'invisible)
  6231. (setq e (match-end 1))
  6232. (if (< org-cycle-separator-lines 0)
  6233. (setq b (save-excursion
  6234. (goto-char (match-beginning 0))
  6235. (org-back-over-empty-lines)
  6236. (if (save-excursion
  6237. (goto-char (max (point-min) (1- (point))))
  6238. (org-at-heading-p))
  6239. (1- (point))
  6240. (point))))
  6241. (setq b (match-beginning 1)))
  6242. (outline-flag-region b e nil)))))))
  6243. ;; Never hide empty lines at the end of the file.
  6244. (save-excursion
  6245. (goto-char (point-max))
  6246. (outline-previous-heading)
  6247. (outline-end-of-heading)
  6248. (if (and (looking-at "[ \t\n]+")
  6249. (= (match-end 0) (point-max)))
  6250. (outline-flag-region (point) (match-end 0) nil))))
  6251. (defun org-show-empty-lines-in-parent ()
  6252. "Move to the parent and re-show empty lines before visible headlines."
  6253. (save-excursion
  6254. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  6255. (org-cycle-show-empty-lines context))))
  6256. (defun org-files-list ()
  6257. "Return `org-agenda-files' list, plus all open org-mode files.
  6258. This is useful for operations that need to scan all of a user's
  6259. open and agenda-wise Org files."
  6260. (let ((files (mapcar 'expand-file-name (org-agenda-files))))
  6261. (dolist (buf (buffer-list))
  6262. (with-current-buffer buf
  6263. (if (and (derived-mode-p 'org-mode) (buffer-file-name))
  6264. (let ((file (expand-file-name (buffer-file-name))))
  6265. (unless (member file files)
  6266. (push file files))))))
  6267. files))
  6268. (defsubst org-entry-beginning-position ()
  6269. "Return the beginning position of the current entry."
  6270. (save-excursion (outline-back-to-heading t) (point)))
  6271. (defsubst org-entry-end-position ()
  6272. "Return the end position of the current entry."
  6273. (save-excursion (outline-next-heading) (point)))
  6274. (defun org-cycle-hide-drawers (state)
  6275. "Re-hide all drawers after a visibility state change."
  6276. (when (and (derived-mode-p 'org-mode)
  6277. (not (memq state '(overview folded contents))))
  6278. (save-excursion
  6279. (let* ((globalp (memq state '(contents all)))
  6280. (beg (if globalp (point-min) (point)))
  6281. (end (if globalp (point-max)
  6282. (if (eq state 'children)
  6283. (save-excursion (outline-next-heading) (point))
  6284. (org-end-of-subtree t)))))
  6285. (goto-char beg)
  6286. (while (re-search-forward org-drawer-regexp end t)
  6287. (org-flag-drawer t))))))
  6288. (defun org-cycle-hide-inline-tasks (state)
  6289. "Re-hide inline task when switching to 'contents visibility state."
  6290. (when (and (eq state 'contents)
  6291. (boundp 'org-inlinetask-min-level)
  6292. org-inlinetask-min-level)
  6293. (hide-sublevels (1- org-inlinetask-min-level))))
  6294. (defun org-flag-drawer (flag)
  6295. "When FLAG is non-nil, hide the drawer we are within.
  6296. Otherwise make it visible."
  6297. (save-excursion
  6298. (beginning-of-line 1)
  6299. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  6300. (let ((b (match-end 0)))
  6301. (if (re-search-forward
  6302. "^[ \t]*:END:"
  6303. (save-excursion (outline-next-heading) (point)) t)
  6304. (outline-flag-region b (point-at-eol) flag)
  6305. (user-error ":END: line missing at position %s" b))))))
  6306. (defun org-subtree-end-visible-p ()
  6307. "Is the end of the current subtree visible?"
  6308. (pos-visible-in-window-p
  6309. (save-excursion (org-end-of-subtree t) (point))))
  6310. (defun org-first-headline-recenter (&optional N)
  6311. "Move cursor to the first headline and recenter the headline.
  6312. Optional argument N means put the headline into the Nth line of the window."
  6313. (goto-char (point-min))
  6314. (when (re-search-forward (concat "^\\(" org-outline-regexp "\\)") nil t)
  6315. (beginning-of-line)
  6316. (recenter (prefix-numeric-value N))))
  6317. ;;; Saving and restoring visibility
  6318. (defun org-outline-overlay-data (&optional use-markers)
  6319. "Return a list of the locations of all outline overlays.
  6320. These are overlays with the `invisible' property value `outline'.
  6321. The return value is a list of cons cells, with start and stop
  6322. positions for each overlay.
  6323. If USE-MARKERS is set, return the positions as markers."
  6324. (let (beg end)
  6325. (save-excursion
  6326. (save-restriction
  6327. (widen)
  6328. (delq nil
  6329. (mapcar (lambda (o)
  6330. (when (eq (overlay-get o 'invisible) 'outline)
  6331. (setq beg (overlay-start o)
  6332. end (overlay-end o))
  6333. (and beg end (> end beg)
  6334. (if use-markers
  6335. (cons (move-marker (make-marker) beg)
  6336. (move-marker (make-marker) end))
  6337. (cons beg end)))))
  6338. (overlays-in (point-min) (point-max))))))))
  6339. (defun org-set-outline-overlay-data (data)
  6340. "Create visibility overlays for all positions in DATA.
  6341. DATA should have been made by `org-outline-overlay-data'."
  6342. (let (o)
  6343. (save-excursion
  6344. (save-restriction
  6345. (widen)
  6346. (show-all)
  6347. (mapc (lambda (c)
  6348. (outline-flag-region (car c) (cdr c) t))
  6349. data)))))
  6350. ;;; Folding of blocks
  6351. (defvar org-hide-block-overlays nil
  6352. "Overlays hiding blocks.")
  6353. (make-variable-buffer-local 'org-hide-block-overlays)
  6354. (defun org-block-map (function &optional start end)
  6355. "Call FUNCTION at the head of all source blocks in the current buffer.
  6356. Optional arguments START and END can be used to limit the range."
  6357. (let ((start (or start (point-min)))
  6358. (end (or end (point-max))))
  6359. (save-excursion
  6360. (goto-char start)
  6361. (while (and (< (point) end) (re-search-forward org-block-regexp end t))
  6362. (save-excursion
  6363. (save-match-data
  6364. (goto-char (match-beginning 0))
  6365. (funcall function)))))))
  6366. (defun org-hide-block-toggle-all ()
  6367. "Toggle the visibility of all blocks in the current buffer."
  6368. (org-block-map #'org-hide-block-toggle))
  6369. (defun org-hide-block-all ()
  6370. "Fold all blocks in the current buffer."
  6371. (interactive)
  6372. (org-show-block-all)
  6373. (org-block-map #'org-hide-block-toggle-maybe))
  6374. (defun org-show-block-all ()
  6375. "Unfold all blocks in the current buffer."
  6376. (interactive)
  6377. (mapc 'delete-overlay org-hide-block-overlays)
  6378. (setq org-hide-block-overlays nil))
  6379. (defun org-hide-block-toggle-maybe ()
  6380. "Toggle visibility of block at point."
  6381. (interactive)
  6382. (let ((case-fold-search t))
  6383. (if (save-excursion
  6384. (beginning-of-line 1)
  6385. (looking-at org-block-regexp))
  6386. (progn (org-hide-block-toggle)
  6387. t) ;; to signal that we took action
  6388. nil))) ;; to signal that we did not
  6389. (defun org-hide-block-toggle (&optional force)
  6390. "Toggle the visibility of the current block."
  6391. (interactive)
  6392. (save-excursion
  6393. (beginning-of-line)
  6394. (if (re-search-forward org-block-regexp nil t)
  6395. (let ((start (- (match-beginning 4) 1)) ;; beginning of body
  6396. (end (match-end 0)) ;; end of entire body
  6397. ov)
  6398. (if (memq t (mapcar (lambda (overlay)
  6399. (eq (overlay-get overlay 'invisible)
  6400. 'org-hide-block))
  6401. (overlays-at start)))
  6402. (if (or (not force) (eq force 'off))
  6403. (mapc (lambda (ov)
  6404. (when (member ov org-hide-block-overlays)
  6405. (setq org-hide-block-overlays
  6406. (delq ov org-hide-block-overlays)))
  6407. (when (eq (overlay-get ov 'invisible)
  6408. 'org-hide-block)
  6409. (delete-overlay ov)))
  6410. (overlays-at start)))
  6411. (setq ov (make-overlay start end))
  6412. (overlay-put ov 'invisible 'org-hide-block)
  6413. ;; make the block accessible to isearch
  6414. (overlay-put
  6415. ov 'isearch-open-invisible
  6416. (lambda (ov)
  6417. (when (member ov org-hide-block-overlays)
  6418. (setq org-hide-block-overlays
  6419. (delq ov org-hide-block-overlays)))
  6420. (when (eq (overlay-get ov 'invisible)
  6421. 'org-hide-block)
  6422. (delete-overlay ov))))
  6423. (push ov org-hide-block-overlays)))
  6424. (user-error "Not looking at a source block"))))
  6425. ;; org-tab-after-check-for-cycling-hook
  6426. (add-hook 'org-tab-first-hook 'org-hide-block-toggle-maybe)
  6427. ;; Remove overlays when changing major mode
  6428. (add-hook 'org-mode-hook
  6429. (lambda () (org-add-hook 'change-major-mode-hook
  6430. 'org-show-block-all 'append 'local)))
  6431. ;;; Org-goto
  6432. (defvar org-goto-window-configuration nil)
  6433. (defvar org-goto-marker nil)
  6434. (defvar org-goto-map)
  6435. (defun org-goto-map ()
  6436. "Set the keymap `org-goto'."
  6437. (setq org-goto-map
  6438. (let ((map (make-sparse-keymap)))
  6439. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command
  6440. mouse-drag-region universal-argument org-occur))
  6441. cmd)
  6442. (while (setq cmd (pop cmds))
  6443. (substitute-key-definition cmd cmd map global-map)))
  6444. (suppress-keymap map)
  6445. (org-defkey map "\C-m" 'org-goto-ret)
  6446. (org-defkey map [(return)] 'org-goto-ret)
  6447. (org-defkey map [(left)] 'org-goto-left)
  6448. (org-defkey map [(right)] 'org-goto-right)
  6449. (org-defkey map [(control ?g)] 'org-goto-quit)
  6450. (org-defkey map "\C-i" 'org-cycle)
  6451. (org-defkey map [(tab)] 'org-cycle)
  6452. (org-defkey map [(down)] 'outline-next-visible-heading)
  6453. (org-defkey map [(up)] 'outline-previous-visible-heading)
  6454. (if org-goto-auto-isearch
  6455. (if (fboundp 'define-key-after)
  6456. (define-key-after map [t] 'org-goto-local-auto-isearch)
  6457. nil)
  6458. (org-defkey map "q" 'org-goto-quit)
  6459. (org-defkey map "n" 'outline-next-visible-heading)
  6460. (org-defkey map "p" 'outline-previous-visible-heading)
  6461. (org-defkey map "f" 'outline-forward-same-level)
  6462. (org-defkey map "b" 'outline-backward-same-level)
  6463. (org-defkey map "u" 'outline-up-heading))
  6464. (org-defkey map "/" 'org-occur)
  6465. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  6466. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  6467. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  6468. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  6469. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  6470. map)))
  6471. (defconst org-goto-help
  6472. "Browse buffer copy, to find location or copy text.%s
  6473. RET=jump to location C-g=quit and return to previous location
  6474. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  6475. (defvar org-goto-start-pos) ; dynamically scoped parameter
  6476. (defun org-goto (&optional alternative-interface)
  6477. "Look up a different location in the current file, keeping current visibility.
  6478. When you want look-up or go to a different location in a
  6479. document, the fastest way is often to fold the entire buffer and
  6480. then dive into the tree. This method has the disadvantage, that
  6481. the previous location will be folded, which may not be what you
  6482. want.
  6483. This command works around this by showing a copy of the current
  6484. buffer in an indirect buffer, in overview mode. You can dive
  6485. into the tree in that copy, use org-occur and incremental search
  6486. to find a location. When pressing RET or `Q', the command
  6487. returns to the original buffer in which the visibility is still
  6488. unchanged. After RET it will also jump to the location selected
  6489. in the indirect buffer and expose the headline hierarchy above.
  6490. With a prefix argument, use the alternative interface: e.g. if
  6491. `org-goto-interface' is 'outline use 'outline-path-completion."
  6492. (interactive "P")
  6493. (org-goto-map)
  6494. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  6495. (org-refile-use-outline-path t)
  6496. (org-refile-target-verify-function nil)
  6497. (interface
  6498. (if (not alternative-interface)
  6499. org-goto-interface
  6500. (if (eq org-goto-interface 'outline)
  6501. 'outline-path-completion
  6502. 'outline)))
  6503. (org-goto-start-pos (point))
  6504. (selected-point
  6505. (if (eq interface 'outline)
  6506. (car (org-get-location (current-buffer) org-goto-help))
  6507. (let ((pa (org-refile-get-location "Goto" nil nil t)))
  6508. (org-refile-check-position pa)
  6509. (nth 3 pa)))))
  6510. (if selected-point
  6511. (progn
  6512. (org-mark-ring-push org-goto-start-pos)
  6513. (goto-char selected-point)
  6514. (if (or (outline-invisible-p) (org-invisible-p2))
  6515. (org-show-context 'org-goto)))
  6516. (message "Quit"))))
  6517. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  6518. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  6519. (defvar org-goto-local-auto-isearch-map) ; defined below
  6520. (defun org-get-location (buf help)
  6521. "Let the user select a location in the Org-mode buffer BUF.
  6522. This function uses a recursive edit. It returns the selected position
  6523. or nil."
  6524. (org-no-popups
  6525. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  6526. (isearch-hide-immediately nil)
  6527. (isearch-search-fun-function
  6528. (lambda () 'org-goto-local-search-headings))
  6529. (org-goto-selected-point org-goto-exit-command))
  6530. (save-excursion
  6531. (save-window-excursion
  6532. (delete-other-windows)
  6533. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  6534. (org-pop-to-buffer-same-window
  6535. (condition-case nil
  6536. (make-indirect-buffer (current-buffer) "*org-goto*")
  6537. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  6538. (with-output-to-temp-buffer "*Org Help*"
  6539. (princ (format help (if org-goto-auto-isearch
  6540. " Just type for auto-isearch."
  6541. " n/p/f/b/u to navigate, q to quit."))))
  6542. (org-fit-window-to-buffer (get-buffer-window "*Org Help*"))
  6543. (setq buffer-read-only nil)
  6544. (let ((org-startup-truncated t)
  6545. (org-startup-folded nil)
  6546. (org-startup-align-all-tables nil))
  6547. (org-mode)
  6548. (org-overview))
  6549. (setq buffer-read-only t)
  6550. (if (and (boundp 'org-goto-start-pos)
  6551. (integer-or-marker-p org-goto-start-pos))
  6552. (let ((org-show-hierarchy-above t)
  6553. (org-show-siblings t)
  6554. (org-show-following-heading t))
  6555. (goto-char org-goto-start-pos)
  6556. (and (outline-invisible-p) (org-show-context)))
  6557. (goto-char (point-min)))
  6558. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  6559. (message "Select location and press RET")
  6560. (use-local-map org-goto-map)
  6561. (recursive-edit)))
  6562. (kill-buffer "*org-goto*")
  6563. (cons org-goto-selected-point org-goto-exit-command))))
  6564. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  6565. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  6566. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  6567. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  6568. (defun org-goto-local-search-headings (string bound noerror)
  6569. "Search and make sure that any matches are in headlines."
  6570. (catch 'return
  6571. (while (if isearch-forward
  6572. (search-forward string bound noerror)
  6573. (search-backward string bound noerror))
  6574. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  6575. (and (member :headline context)
  6576. (not (member :tags context))))
  6577. (throw 'return (point))))))
  6578. (defun org-goto-local-auto-isearch ()
  6579. "Start isearch."
  6580. (interactive)
  6581. (goto-char (point-min))
  6582. (let ((keys (this-command-keys)))
  6583. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  6584. (isearch-mode t)
  6585. (isearch-process-search-char (string-to-char keys)))))
  6586. (defun org-goto-ret (&optional arg)
  6587. "Finish `org-goto' by going to the new location."
  6588. (interactive "P")
  6589. (setq org-goto-selected-point (point)
  6590. org-goto-exit-command 'return)
  6591. (throw 'exit nil))
  6592. (defun org-goto-left ()
  6593. "Finish `org-goto' by going to the new location."
  6594. (interactive)
  6595. (if (org-at-heading-p)
  6596. (progn
  6597. (beginning-of-line 1)
  6598. (setq org-goto-selected-point (point)
  6599. org-goto-exit-command 'left)
  6600. (throw 'exit nil))
  6601. (user-error "Not on a heading")))
  6602. (defun org-goto-right ()
  6603. "Finish `org-goto' by going to the new location."
  6604. (interactive)
  6605. (if (org-at-heading-p)
  6606. (progn
  6607. (setq org-goto-selected-point (point)
  6608. org-goto-exit-command 'right)
  6609. (throw 'exit nil))
  6610. (user-error "Not on a heading")))
  6611. (defun org-goto-quit ()
  6612. "Finish `org-goto' without cursor motion."
  6613. (interactive)
  6614. (setq org-goto-selected-point nil)
  6615. (setq org-goto-exit-command 'quit)
  6616. (throw 'exit nil))
  6617. ;;; Indirect buffer display of subtrees
  6618. (defvar org-indirect-dedicated-frame nil
  6619. "This is the frame being used for indirect tree display.")
  6620. (defvar org-last-indirect-buffer nil)
  6621. (defun org-tree-to-indirect-buffer (&optional arg)
  6622. "Create indirect buffer and narrow it to current subtree.
  6623. With a numerical prefix ARG, go up to this level and then take that tree.
  6624. If ARG is negative, go up that many levels.
  6625. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  6626. indirect buffer previously made with this command, to avoid proliferation of
  6627. indirect buffers. However, when you call the command with a \
  6628. \\[universal-argument] prefix, or
  6629. when `org-indirect-buffer-display' is `new-frame', the last buffer
  6630. is kept so that you can work with several indirect buffers at the same time.
  6631. If `org-indirect-buffer-display' is `dedicated-frame', the \
  6632. \\[universal-argument] prefix also
  6633. requests that a new frame be made for the new buffer, so that the dedicated
  6634. frame is not changed."
  6635. (interactive "P")
  6636. (let ((cbuf (current-buffer))
  6637. (cwin (selected-window))
  6638. (pos (point))
  6639. beg end level heading ibuf)
  6640. (save-excursion
  6641. (org-back-to-heading t)
  6642. (when (numberp arg)
  6643. (setq level (org-outline-level))
  6644. (if (< arg 0) (setq arg (+ level arg)))
  6645. (while (> (setq level (org-outline-level)) arg)
  6646. (org-up-heading-safe)))
  6647. (setq beg (point)
  6648. heading (org-get-heading))
  6649. (org-end-of-subtree t t)
  6650. (if (org-at-heading-p) (backward-char 1))
  6651. (setq end (point)))
  6652. (if (and (buffer-live-p org-last-indirect-buffer)
  6653. (not (eq org-indirect-buffer-display 'new-frame))
  6654. (not arg))
  6655. (kill-buffer org-last-indirect-buffer))
  6656. (setq ibuf (org-get-indirect-buffer cbuf)
  6657. org-last-indirect-buffer ibuf)
  6658. (cond
  6659. ((or (eq org-indirect-buffer-display 'new-frame)
  6660. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  6661. (select-frame (make-frame))
  6662. (delete-other-windows)
  6663. (org-pop-to-buffer-same-window ibuf)
  6664. (org-set-frame-title heading))
  6665. ((eq org-indirect-buffer-display 'dedicated-frame)
  6666. (raise-frame
  6667. (select-frame (or (and org-indirect-dedicated-frame
  6668. (frame-live-p org-indirect-dedicated-frame)
  6669. org-indirect-dedicated-frame)
  6670. (setq org-indirect-dedicated-frame (make-frame)))))
  6671. (delete-other-windows)
  6672. (org-pop-to-buffer-same-window ibuf)
  6673. (org-set-frame-title (concat "Indirect: " heading)))
  6674. ((eq org-indirect-buffer-display 'current-window)
  6675. (org-pop-to-buffer-same-window ibuf))
  6676. ((eq org-indirect-buffer-display 'other-window)
  6677. (pop-to-buffer ibuf))
  6678. (t (error "Invalid value")))
  6679. (if (featurep 'xemacs)
  6680. (save-excursion (org-mode) (turn-on-font-lock)))
  6681. (narrow-to-region beg end)
  6682. (show-all)
  6683. (goto-char pos)
  6684. (run-hook-with-args 'org-cycle-hook 'all)
  6685. (and (window-live-p cwin) (select-window cwin))))
  6686. (defun org-get-indirect-buffer (&optional buffer)
  6687. (setq buffer (or buffer (current-buffer)))
  6688. (let ((n 1) (base (buffer-name buffer)) bname)
  6689. (while (buffer-live-p
  6690. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  6691. (setq n (1+ n)))
  6692. (condition-case nil
  6693. (make-indirect-buffer buffer bname 'clone)
  6694. (error (make-indirect-buffer buffer bname)))))
  6695. (defun org-set-frame-title (title)
  6696. "Set the title of the current frame to the string TITLE."
  6697. ;; FIXME: how to name a single frame in XEmacs???
  6698. (unless (featurep 'xemacs)
  6699. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  6700. ;;;; Structure editing
  6701. ;;; Inserting headlines
  6702. (defun org-previous-line-empty-p (&optional next)
  6703. "Is the previous line a blank line?
  6704. When NEXT is non-nil, check the next line instead."
  6705. (save-excursion
  6706. (and (not (bobp))
  6707. (or (beginning-of-line (if next 2 0)) t)
  6708. (save-match-data
  6709. (looking-at "[ \t]*$")))))
  6710. (defun org-insert-heading (&optional arg invisible-ok)
  6711. "Insert a new heading or item with same depth at point.
  6712. If point is in a plain list and ARG is nil, create a new list item.
  6713. With one universal prefix argument, insert a heading even in lists.
  6714. With two universal prefix arguments, insert the heading at the end
  6715. of the parent subtree.
  6716. If point is at the beginning of a headline, insert a sibling before
  6717. the current headline. If point is not at the beginning, split the line
  6718. and create a new headline with the text in the current line after point
  6719. \(see `org-M-RET-may-split-line' on how to modify this behavior).
  6720. When INVISIBLE-OK is set, stop at invisible headlines when going back.
  6721. This is important for non-interactive uses of the command."
  6722. (interactive "P")
  6723. (if (org-called-interactively-p 'any) (org-reveal))
  6724. (let ((itemp (org-in-item-p)))
  6725. (cond
  6726. ((or (= (buffer-size) 0)
  6727. (and (not (save-excursion
  6728. (and (ignore-errors (org-back-to-heading invisible-ok))
  6729. (org-at-heading-p))))
  6730. (or arg (not itemp))))
  6731. (insert
  6732. (if (org-previous-line-empty-p) "" "\n")
  6733. (if (org-in-src-block-p) ",* " "* "))
  6734. (run-hooks 'org-insert-heading-hook))
  6735. ((or arg
  6736. (and (not itemp) org-insert-heading-respect-content)
  6737. (not (org-insert-item
  6738. (save-excursion
  6739. (and itemp
  6740. (goto-char itemp)
  6741. (looking-at org-list-full-item-re)
  6742. (match-string 3))))))
  6743. (let (begn endn)
  6744. (when (org-buffer-narrowed-p)
  6745. (setq begn (point-min) endn (point-max))
  6746. (widen))
  6747. (let* ((empty-line-p nil)
  6748. (eops (equal arg '(16))) ; insert at end of parent subtree
  6749. (org-insert-heading-respect-content
  6750. (or (not (null arg)) org-insert-heading-respect-content))
  6751. (level nil)
  6752. (on-heading (org-at-heading-p))
  6753. ;; Get a level to fall back on
  6754. (fix-level
  6755. (save-excursion
  6756. (org-back-to-heading t)
  6757. (looking-at org-outline-regexp)
  6758. (make-string (1- (length (match-string 0))) ?*)))
  6759. (on-empty-line
  6760. (save-excursion (beginning-of-line 1) (looking-at "^\\s-*$")))
  6761. (head (save-excursion
  6762. (condition-case nil
  6763. (progn
  6764. (org-back-to-heading invisible-ok)
  6765. (when (and (not on-heading)
  6766. (featurep 'org-inlinetask)
  6767. (integerp org-inlinetask-min-level)
  6768. (>= (length (match-string 0))
  6769. org-inlinetask-min-level))
  6770. ;; Find a heading level before the inline task
  6771. (while (and (setq level (org-up-heading-safe))
  6772. (>= level org-inlinetask-min-level)))
  6773. (if (org-at-heading-p)
  6774. (org-back-to-heading invisible-ok)
  6775. (error "This should not happen")))
  6776. (unless (and (save-excursion
  6777. (save-match-data
  6778. (org-backward-heading-same-level 1 invisible-ok))
  6779. (= (point) (match-beginning 0)))
  6780. (not (org-previous-line-empty-p t)))
  6781. (setq empty-line-p (org-previous-line-empty-p)))
  6782. (match-string 0))
  6783. (error (or fix-level "* ")))))
  6784. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  6785. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  6786. pos hide-previous previous-pos)
  6787. (if ;; At the beginning of a heading, open a new line for insertion
  6788. (and (bolp) (org-at-heading-p)
  6789. (not eops)
  6790. (or (bobp)
  6791. (save-excursion (backward-char 1) (not (outline-invisible-p)))))
  6792. (open-line (if blank 2 1))
  6793. (save-excursion
  6794. (setq previous-pos (point-at-bol))
  6795. (end-of-line)
  6796. (setq hide-previous (outline-invisible-p)))
  6797. (and org-insert-heading-respect-content
  6798. (save-excursion
  6799. (while (outline-invisible-p)
  6800. (org-show-subtree)
  6801. (org-up-heading-safe))))
  6802. (let ((split
  6803. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  6804. (save-excursion
  6805. (let ((p (point)))
  6806. (goto-char (point-at-bol))
  6807. (and (looking-at org-complex-heading-regexp)
  6808. (match-beginning 4)
  6809. (> p (match-beginning 4)))))))
  6810. tags pos)
  6811. (cond
  6812. ;; Insert a new line, possibly at end of parent subtree
  6813. ((and (not arg) (not on-heading) (not on-empty-line)
  6814. (not (save-excursion
  6815. (beginning-of-line 1)
  6816. (or (looking-at org-list-full-item-re)
  6817. ;; Don't convert :end: lines to headline
  6818. (looking-at "^\\s-*:end:")
  6819. (looking-at "^\\s-*#\\+end_?")))))
  6820. (beginning-of-line 1))
  6821. (org-insert-heading-respect-content
  6822. (if (not eops)
  6823. (progn
  6824. (org-end-of-subtree nil t)
  6825. (and (looking-at "^\\*") (backward-char 1))
  6826. (while (and (not (bobp))
  6827. ;; Don't delete spaces in empty headlines
  6828. (not (looking-back org-outline-regexp))
  6829. (member (char-before) '(?\ ?\t ?\n)))
  6830. (backward-delete-char 1)))
  6831. (let ((p (point)))
  6832. (org-up-heading-safe)
  6833. (if (= p (point))
  6834. (goto-char (point-max))
  6835. (org-end-of-subtree nil t))))
  6836. (when (featurep 'org-inlinetask)
  6837. (while (and (not (eobp))
  6838. (looking-at "\\(\\*+\\)[ \t]+")
  6839. (>= (length (match-string 1))
  6840. org-inlinetask-min-level))
  6841. (org-end-of-subtree nil t)))
  6842. (or (bolp) (newline))
  6843. (or (org-previous-line-empty-p)
  6844. (and blank (newline)))
  6845. (if (or empty-line-p eops) (open-line 1)))
  6846. ;; Insert a headling containing text after point
  6847. ((org-at-heading-p)
  6848. (when hide-previous
  6849. (show-children)
  6850. (org-show-entry))
  6851. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?[ \t]*$")
  6852. (setq tags (and (match-end 2) (match-string 2)))
  6853. (and (match-end 1)
  6854. (delete-region (match-beginning 1) (match-end 1)))
  6855. (setq pos (point-at-bol))
  6856. (or split (end-of-line 1))
  6857. (delete-horizontal-space)
  6858. (if (string-match "\\`\\*+\\'"
  6859. (buffer-substring (point-at-bol) (point)))
  6860. (insert " "))
  6861. (newline (if blank 2 1))
  6862. (when tags
  6863. (save-excursion
  6864. (goto-char pos)
  6865. (end-of-line 1)
  6866. (insert " " tags)
  6867. (org-set-tags nil 'align))))
  6868. (t
  6869. (or split (end-of-line 1))
  6870. (newline (cond ((and blank (not on-empty-line)) 2)
  6871. (blank 1)
  6872. (on-empty-line 0) (t 1)))))))
  6873. (insert head) (just-one-space)
  6874. (setq pos (point))
  6875. (end-of-line 1)
  6876. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  6877. (when (and org-insert-heading-respect-content hide-previous)
  6878. (save-excursion
  6879. (goto-char previous-pos)
  6880. (hide-subtree)))
  6881. (when (and begn endn)
  6882. (narrow-to-region (min (point) begn) (max (point) endn)))
  6883. (run-hooks 'org-insert-heading-hook)))))))
  6884. (defun org-get-heading (&optional no-tags no-todo)
  6885. "Return the heading of the current entry, without the stars.
  6886. When NO-TAGS is non-nil, don't include tags.
  6887. When NO-TODO is non-nil, don't include TODO keywords."
  6888. (save-excursion
  6889. (org-back-to-heading t)
  6890. (cond
  6891. ((and no-tags no-todo)
  6892. (looking-at org-complex-heading-regexp)
  6893. (match-string 4))
  6894. (no-tags
  6895. (looking-at (concat org-outline-regexp
  6896. "\\(.*?\\)"
  6897. "\\(?:[ \t]+:[[:alnum:]:_@#%]+:\\)?[ \t]*$"))
  6898. (match-string 1))
  6899. (no-todo
  6900. (looking-at org-todo-line-regexp)
  6901. (match-string 3))
  6902. (t (looking-at org-heading-regexp)
  6903. (match-string 2)))))
  6904. (defvar orgstruct-mode) ; defined below
  6905. (defun org-heading-components ()
  6906. "Return the components of the current heading.
  6907. This is a list with the following elements:
  6908. - the level as an integer
  6909. - the reduced level, different if `org-odd-levels-only' is set.
  6910. - the TODO keyword, or nil
  6911. - the priority character, like ?A, or nil if no priority is given
  6912. - the headline text itself, or the tags string if no headline text
  6913. - the tags string, or nil."
  6914. (save-excursion
  6915. (org-back-to-heading t)
  6916. (if (let (case-fold-search)
  6917. (looking-at
  6918. (if orgstruct-mode
  6919. org-heading-regexp
  6920. org-complex-heading-regexp)))
  6921. (if orgstruct-mode
  6922. (list (length (match-string 1))
  6923. (org-reduced-level (length (match-string 1)))
  6924. nil
  6925. nil
  6926. (match-string 2)
  6927. nil)
  6928. (list (length (match-string 1))
  6929. (org-reduced-level (length (match-string 1)))
  6930. (org-match-string-no-properties 2)
  6931. (and (match-end 3) (aref (match-string 3) 2))
  6932. (org-match-string-no-properties 4)
  6933. (org-match-string-no-properties 5))))))
  6934. (defun org-get-entry ()
  6935. "Get the entry text, after heading, entire subtree."
  6936. (save-excursion
  6937. (org-back-to-heading t)
  6938. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  6939. (defun org-insert-heading-after-current ()
  6940. "Insert a new heading with same level as current, after current subtree."
  6941. (interactive)
  6942. (org-back-to-heading)
  6943. (org-insert-heading)
  6944. (org-move-subtree-down)
  6945. (end-of-line 1))
  6946. (defun org-insert-heading-respect-content (&optional arg invisible-ok)
  6947. "Insert heading with `org-insert-heading-respect-content' set to t."
  6948. (interactive "P")
  6949. (let ((org-insert-heading-respect-content t))
  6950. (org-insert-heading arg invisible-ok)))
  6951. (defun org-insert-todo-heading-respect-content (&optional force-state)
  6952. "Insert TODO heading with `org-insert-heading-respect-content' set to t."
  6953. (interactive "P")
  6954. (let ((org-insert-heading-respect-content t))
  6955. (org-insert-todo-heading force-state t)))
  6956. (defun org-insert-todo-heading (arg &optional force-heading)
  6957. "Insert a new heading with the same level and TODO state as current heading.
  6958. If the heading has no TODO state, or if the state is DONE, use the first
  6959. state (TODO by default). Also one prefix arg, force first state. With two
  6960. prefix args, force inserting at the end of the parent subtree."
  6961. (interactive "P")
  6962. (when (or force-heading (not (org-insert-item 'checkbox)))
  6963. (org-insert-heading (or (and (equal arg '(16)) '(16))
  6964. force-heading))
  6965. (save-excursion
  6966. (org-back-to-heading)
  6967. (outline-previous-heading)
  6968. (looking-at org-todo-line-regexp))
  6969. (let*
  6970. ((new-mark-x
  6971. (if (or arg
  6972. (not (match-beginning 2))
  6973. (member (match-string 2) org-done-keywords))
  6974. (car org-todo-keywords-1)
  6975. (match-string 2)))
  6976. (new-mark
  6977. (or
  6978. (run-hook-with-args-until-success
  6979. 'org-todo-get-default-hook new-mark-x nil)
  6980. new-mark-x)))
  6981. (beginning-of-line 1)
  6982. (and (looking-at org-outline-regexp) (goto-char (match-end 0))
  6983. (if org-treat-insert-todo-heading-as-state-change
  6984. (org-todo new-mark)
  6985. (insert new-mark " "))))
  6986. (when org-provide-todo-statistics
  6987. (org-update-parent-todo-statistics))))
  6988. (defun org-insert-subheading (arg)
  6989. "Insert a new subheading and demote it.
  6990. Works for outline headings and for plain lists alike."
  6991. (interactive "P")
  6992. (org-insert-heading arg)
  6993. (cond
  6994. ((org-at-heading-p) (org-do-demote))
  6995. ((org-at-item-p) (org-indent-item))))
  6996. (defun org-insert-todo-subheading (arg)
  6997. "Insert a new subheading with TODO keyword or checkbox and demote it.
  6998. Works for outline headings and for plain lists alike."
  6999. (interactive "P")
  7000. (org-insert-todo-heading arg)
  7001. (cond
  7002. ((org-at-heading-p) (org-do-demote))
  7003. ((org-at-item-p) (org-indent-item))))
  7004. ;;; Promotion and Demotion
  7005. (defvar org-after-demote-entry-hook nil
  7006. "Hook run after an entry has been demoted.
  7007. The cursor will be at the beginning of the entry.
  7008. When a subtree is being demoted, the hook will be called for each node.")
  7009. (defvar org-after-promote-entry-hook nil
  7010. "Hook run after an entry has been promoted.
  7011. The cursor will be at the beginning of the entry.
  7012. When a subtree is being promoted, the hook will be called for each node.")
  7013. (defun org-promote-subtree ()
  7014. "Promote the entire subtree.
  7015. See also `org-promote'."
  7016. (interactive)
  7017. (save-excursion
  7018. (org-with-limited-levels (org-map-tree 'org-promote)))
  7019. (org-fix-position-after-promote))
  7020. (defun org-demote-subtree ()
  7021. "Demote the entire subtree. See `org-demote'.
  7022. See also `org-promote'."
  7023. (interactive)
  7024. (save-excursion
  7025. (org-with-limited-levels (org-map-tree 'org-demote)))
  7026. (org-fix-position-after-promote))
  7027. (defun org-do-promote ()
  7028. "Promote the current heading higher up the tree.
  7029. If the region is active in `transient-mark-mode', promote all headings
  7030. in the region."
  7031. (interactive)
  7032. (save-excursion
  7033. (if (org-region-active-p)
  7034. (org-map-region 'org-promote (region-beginning) (region-end))
  7035. (org-promote)))
  7036. (org-fix-position-after-promote))
  7037. (defun org-do-demote ()
  7038. "Demote the current heading lower down the tree.
  7039. If the region is active in `transient-mark-mode', demote all headings
  7040. in the region."
  7041. (interactive)
  7042. (save-excursion
  7043. (if (org-region-active-p)
  7044. (org-map-region 'org-demote (region-beginning) (region-end))
  7045. (org-demote)))
  7046. (org-fix-position-after-promote))
  7047. (defun org-fix-position-after-promote ()
  7048. "Make sure that after pro/demotion cursor position is right."
  7049. (let ((pos (point)))
  7050. (when (save-excursion
  7051. (beginning-of-line 1)
  7052. (looking-at org-todo-line-regexp)
  7053. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  7054. (cond ((eobp) (insert " "))
  7055. ((eolp) (insert " "))
  7056. ((equal (char-after) ?\ ) (forward-char 1))))))
  7057. (defun org-current-level ()
  7058. "Return the level of the current entry, or nil if before the first headline.
  7059. The level is the number of stars at the beginning of the headline."
  7060. (save-excursion
  7061. (org-with-limited-levels
  7062. (if (ignore-errors (org-back-to-heading t))
  7063. (funcall outline-level)))))
  7064. (defun org-get-previous-line-level ()
  7065. "Return the outline depth of the last headline before the current line.
  7066. Returns 0 for the first headline in the buffer, and nil if before the
  7067. first headline."
  7068. (let ((current-level (org-current-level))
  7069. (prev-level (when (> (line-number-at-pos) 1)
  7070. (save-excursion
  7071. (beginning-of-line 0)
  7072. (org-current-level)))))
  7073. (cond ((null current-level) nil) ; Before first headline
  7074. ((null prev-level) 0) ; At first headline
  7075. (prev-level))))
  7076. (defun org-reduced-level (l)
  7077. "Compute the effective level of a heading.
  7078. This takes into account the setting of `org-odd-levels-only'."
  7079. (cond
  7080. ((zerop l) 0)
  7081. (org-odd-levels-only (1+ (floor (/ l 2))))
  7082. (t l)))
  7083. (defun org-level-increment ()
  7084. "Return the number of stars that will be added or removed at a
  7085. time to headlines when structure editing, based on the value of
  7086. `org-odd-levels-only'."
  7087. (if org-odd-levels-only 2 1))
  7088. (defun org-get-valid-level (level &optional change)
  7089. "Rectify a level change under the influence of `org-odd-levels-only'
  7090. LEVEL is a current level, CHANGE is by how much the level should be
  7091. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  7092. even level numbers will become the next higher odd number."
  7093. (if org-odd-levels-only
  7094. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  7095. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  7096. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  7097. (max 1 (+ level (or change 0)))))
  7098. (if (boundp 'define-obsolete-function-alias)
  7099. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  7100. (define-obsolete-function-alias 'org-get-legal-level
  7101. 'org-get-valid-level)
  7102. (define-obsolete-function-alias 'org-get-legal-level
  7103. 'org-get-valid-level "23.1")))
  7104. (defvar org-called-with-limited-levels nil) ;; Dynamically bound in
  7105. ;; ̀org-with-limited-levels'
  7106. (defun org-promote ()
  7107. "Promote the current heading higher up the tree.
  7108. If the region is active in `transient-mark-mode', promote all headings
  7109. in the region."
  7110. (org-back-to-heading t)
  7111. (let* ((level (save-match-data (funcall outline-level)))
  7112. (after-change-functions (remove 'flyspell-after-change-function
  7113. after-change-functions))
  7114. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  7115. (diff (abs (- level (length up-head) -1))))
  7116. (cond ((and (= level 1) org-called-with-limited-levels
  7117. org-allow-promoting-top-level-subtree)
  7118. (replace-match "# " nil t))
  7119. ((= level 1)
  7120. (user-error "Cannot promote to level 0. UNDO to recover if necessary"))
  7121. (t (replace-match up-head nil t)))
  7122. ;; Fixup tag positioning
  7123. (unless (= level 1)
  7124. (and org-auto-align-tags (org-set-tags nil t))
  7125. (if org-adapt-indentation (org-fixup-indentation (- diff))))
  7126. (run-hooks 'org-after-promote-entry-hook)))
  7127. (defun org-demote ()
  7128. "Demote the current heading lower down the tree.
  7129. If the region is active in `transient-mark-mode', demote all headings
  7130. in the region."
  7131. (org-back-to-heading t)
  7132. (let* ((level (save-match-data (funcall outline-level)))
  7133. (after-change-functions (remove 'flyspell-after-change-function
  7134. after-change-functions))
  7135. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  7136. (diff (abs (- level (length down-head) -1))))
  7137. (replace-match down-head nil t)
  7138. ;; Fixup tag positioning
  7139. (and org-auto-align-tags (org-set-tags nil t))
  7140. (if org-adapt-indentation (org-fixup-indentation diff))
  7141. (run-hooks 'org-after-demote-entry-hook)))
  7142. (defun org-cycle-level ()
  7143. "Cycle the level of an empty headline through possible states.
  7144. This goes first to child, then to parent, level, then up the hierarchy.
  7145. After top level, it switches back to sibling level."
  7146. (interactive)
  7147. (let ((org-adapt-indentation nil))
  7148. (when (org-point-at-end-of-empty-headline)
  7149. (setq this-command 'org-cycle-level) ; Only needed for caching
  7150. (let ((cur-level (org-current-level))
  7151. (prev-level (org-get-previous-line-level)))
  7152. (cond
  7153. ;; If first headline in file, promote to top-level.
  7154. ((= prev-level 0)
  7155. (loop repeat (/ (- cur-level 1) (org-level-increment))
  7156. do (org-do-promote)))
  7157. ;; If same level as prev, demote one.
  7158. ((= prev-level cur-level)
  7159. (org-do-demote))
  7160. ;; If parent is top-level, promote to top level if not already.
  7161. ((= prev-level 1)
  7162. (loop repeat (/ (- cur-level 1) (org-level-increment))
  7163. do (org-do-promote)))
  7164. ;; If top-level, return to prev-level.
  7165. ((= cur-level 1)
  7166. (loop repeat (/ (- prev-level 1) (org-level-increment))
  7167. do (org-do-demote)))
  7168. ;; If less than prev-level, promote one.
  7169. ((< cur-level prev-level)
  7170. (org-do-promote))
  7171. ;; If deeper than prev-level, promote until higher than
  7172. ;; prev-level.
  7173. ((> cur-level prev-level)
  7174. (loop repeat (+ 1 (/ (- cur-level prev-level) (org-level-increment)))
  7175. do (org-do-promote))))
  7176. t))))
  7177. (defun org-map-tree (fun)
  7178. "Call FUN for every heading underneath the current one."
  7179. (org-back-to-heading)
  7180. (let ((level (funcall outline-level)))
  7181. (save-excursion
  7182. (funcall fun)
  7183. (while (and (progn
  7184. (outline-next-heading)
  7185. (> (funcall outline-level) level))
  7186. (not (eobp)))
  7187. (funcall fun)))))
  7188. (defun org-map-region (fun beg end)
  7189. "Call FUN for every heading between BEG and END."
  7190. (let ((org-ignore-region t))
  7191. (save-excursion
  7192. (setq end (copy-marker end))
  7193. (goto-char beg)
  7194. (if (and (re-search-forward org-outline-regexp-bol nil t)
  7195. (< (point) end))
  7196. (funcall fun))
  7197. (while (and (progn
  7198. (outline-next-heading)
  7199. (< (point) end))
  7200. (not (eobp)))
  7201. (funcall fun)))))
  7202. (defvar org-property-end-re) ; silence byte-compiler
  7203. (defun org-fixup-indentation (diff)
  7204. "Change the indentation in the current entry by DIFF.
  7205. However, if any line in the current entry has no indentation, or if it
  7206. would end up with no indentation after the change, nothing at all is done."
  7207. (save-excursion
  7208. (let ((end (save-excursion (outline-next-heading)
  7209. (point-marker)))
  7210. (prohibit (if (> diff 0)
  7211. "^\\S-"
  7212. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  7213. col)
  7214. (unless (save-excursion (end-of-line 1)
  7215. (re-search-forward prohibit end t))
  7216. (while (and (< (point) end)
  7217. (re-search-forward "^[ \t]+" end t))
  7218. (goto-char (match-end 0))
  7219. (setq col (current-column))
  7220. (if (< diff 0) (replace-match ""))
  7221. (org-indent-to-column (+ diff col))))
  7222. (move-marker end nil))))
  7223. (defun org-convert-to-odd-levels ()
  7224. "Convert an org-mode file with all levels allowed to one with odd levels.
  7225. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  7226. level 5 etc."
  7227. (interactive)
  7228. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  7229. (let ((outline-level 'org-outline-level)
  7230. (org-odd-levels-only nil) n)
  7231. (save-excursion
  7232. (goto-char (point-min))
  7233. (while (re-search-forward "^\\*\\*+ " nil t)
  7234. (setq n (- (length (match-string 0)) 2))
  7235. (while (>= (setq n (1- n)) 0)
  7236. (org-demote))
  7237. (end-of-line 1))))))
  7238. (defun org-convert-to-oddeven-levels ()
  7239. "Convert an org-mode file with only odd levels to one with odd/even levels.
  7240. This promotes level 3 to level 2, level 5 to level 3 etc. If the
  7241. file contains a section with an even level, conversion would
  7242. destroy the structure of the file. An error is signaled in this
  7243. case."
  7244. (interactive)
  7245. (goto-char (point-min))
  7246. ;; First check if there are no even levels
  7247. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  7248. (org-show-context t)
  7249. (error "Not all levels are odd in this file. Conversion not possible"))
  7250. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  7251. (let ((outline-regexp org-outline-regexp)
  7252. (outline-level 'org-outline-level)
  7253. (org-odd-levels-only nil) n)
  7254. (save-excursion
  7255. (goto-char (point-min))
  7256. (while (re-search-forward "^\\*\\*+ " nil t)
  7257. (setq n (/ (1- (length (match-string 0))) 2))
  7258. (while (>= (setq n (1- n)) 0)
  7259. (org-promote))
  7260. (end-of-line 1))))))
  7261. (defun org-tr-level (n)
  7262. "Make N odd if required."
  7263. (if org-odd-levels-only (1+ (/ n 2)) n))
  7264. ;;; Vertical tree motion, cutting and pasting of subtrees
  7265. (defun org-move-subtree-up (&optional arg)
  7266. "Move the current subtree up past ARG headlines of the same level."
  7267. (interactive "p")
  7268. (org-move-subtree-down (- (prefix-numeric-value arg))))
  7269. (defun org-move-subtree-down (&optional arg)
  7270. "Move the current subtree down past ARG headlines of the same level."
  7271. (interactive "p")
  7272. (setq arg (prefix-numeric-value arg))
  7273. (let ((movfunc (if (> arg 0) 'org-get-next-sibling
  7274. 'org-get-last-sibling))
  7275. (ins-point (make-marker))
  7276. (cnt (abs arg))
  7277. (col (current-column))
  7278. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  7279. ;; Select the tree
  7280. (org-back-to-heading)
  7281. (setq beg0 (point))
  7282. (save-excursion
  7283. (setq ne-beg (org-back-over-empty-lines))
  7284. (setq beg (point)))
  7285. (save-match-data
  7286. (save-excursion (outline-end-of-heading)
  7287. (setq folded (outline-invisible-p)))
  7288. (outline-end-of-subtree))
  7289. (outline-next-heading)
  7290. (setq ne-end (org-back-over-empty-lines))
  7291. (setq end (point))
  7292. (goto-char beg0)
  7293. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  7294. ;; include less whitespace
  7295. (save-excursion
  7296. (goto-char beg)
  7297. (forward-line (- ne-beg ne-end))
  7298. (setq beg (point))))
  7299. ;; Find insertion point, with error handling
  7300. (while (> cnt 0)
  7301. (or (and (funcall movfunc) (looking-at org-outline-regexp))
  7302. (progn (goto-char beg0)
  7303. (user-error "Cannot move past superior level or buffer limit")))
  7304. (setq cnt (1- cnt)))
  7305. (if (> arg 0)
  7306. ;; Moving forward - still need to move over subtree
  7307. (progn (org-end-of-subtree t t)
  7308. (save-excursion
  7309. (org-back-over-empty-lines)
  7310. (or (bolp) (newline)))))
  7311. (setq ne-ins (org-back-over-empty-lines))
  7312. (move-marker ins-point (point))
  7313. (setq txt (buffer-substring beg end))
  7314. (org-save-markers-in-region beg end)
  7315. (delete-region beg end)
  7316. (org-remove-empty-overlays-at beg)
  7317. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  7318. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  7319. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  7320. (let ((bbb (point)))
  7321. (insert-before-markers txt)
  7322. (org-reinstall-markers-in-region bbb)
  7323. (move-marker ins-point bbb))
  7324. (or (bolp) (insert "\n"))
  7325. (setq ins-end (point))
  7326. (goto-char ins-point)
  7327. (org-skip-whitespace)
  7328. (when (and (< arg 0)
  7329. (org-first-sibling-p)
  7330. (> ne-ins ne-beg))
  7331. ;; Move whitespace back to beginning
  7332. (save-excursion
  7333. (goto-char ins-end)
  7334. (let ((kill-whole-line t))
  7335. (kill-line (- ne-ins ne-beg)) (point)))
  7336. (insert (make-string (- ne-ins ne-beg) ?\n)))
  7337. (move-marker ins-point nil)
  7338. (if folded
  7339. (hide-subtree)
  7340. (org-show-entry)
  7341. (show-children)
  7342. (org-cycle-hide-drawers 'children))
  7343. (org-clean-visibility-after-subtree-move)
  7344. ;; move back to the initial column we were at
  7345. (move-to-column col)))
  7346. (defvar org-subtree-clip ""
  7347. "Clipboard for cut and paste of subtrees.
  7348. This is actually only a copy of the kill, because we use the normal kill
  7349. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  7350. (defvar org-subtree-clip-folded nil
  7351. "Was the last copied subtree folded?
  7352. This is used to fold the tree back after pasting.")
  7353. (defun org-cut-subtree (&optional n)
  7354. "Cut the current subtree into the clipboard.
  7355. With prefix arg N, cut this many sequential subtrees.
  7356. This is a short-hand for marking the subtree and then cutting it."
  7357. (interactive "p")
  7358. (org-copy-subtree n 'cut))
  7359. (defun org-copy-subtree (&optional n cut force-store-markers nosubtrees)
  7360. "Cut the current subtree into the clipboard.
  7361. With prefix arg N, cut this many sequential subtrees.
  7362. This is a short-hand for marking the subtree and then copying it.
  7363. If CUT is non-nil, actually cut the subtree.
  7364. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  7365. of some markers in the region, even if CUT is non-nil. This is
  7366. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  7367. (interactive "p")
  7368. (let (beg end folded (beg0 (point)))
  7369. (if (org-called-interactively-p 'any)
  7370. (org-back-to-heading nil) ; take what looks like a subtree
  7371. (org-back-to-heading t)) ; take what is really there
  7372. (setq beg (point))
  7373. (skip-chars-forward " \t\r\n")
  7374. (save-match-data
  7375. (if nosubtrees
  7376. (outline-next-heading)
  7377. (save-excursion (outline-end-of-heading)
  7378. (setq folded (outline-invisible-p)))
  7379. (condition-case nil
  7380. (org-forward-heading-same-level (1- n) t)
  7381. (error nil))
  7382. (org-end-of-subtree t t)))
  7383. (setq end (point))
  7384. (goto-char beg0)
  7385. (when (> end beg)
  7386. (setq org-subtree-clip-folded folded)
  7387. (when (or cut force-store-markers)
  7388. (org-save-markers-in-region beg end))
  7389. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  7390. (setq org-subtree-clip (current-kill 0))
  7391. (message "%s: Subtree(s) with %d characters"
  7392. (if cut "Cut" "Copied")
  7393. (length org-subtree-clip)))))
  7394. (defun org-paste-subtree (&optional level tree for-yank)
  7395. "Paste the clipboard as a subtree, with modification of headline level.
  7396. The entire subtree is promoted or demoted in order to match a new headline
  7397. level.
  7398. If the cursor is at the beginning of a headline, the same level as
  7399. that headline is used to paste the tree.
  7400. If not, the new level is derived from the *visible* headings
  7401. before and after the insertion point, and taken to be the inferior headline
  7402. level of the two. So if the previous visible heading is level 3 and the
  7403. next is level 4 (or vice versa), level 4 will be used for insertion.
  7404. This makes sure that the subtree remains an independent subtree and does
  7405. not swallow low level entries.
  7406. You can also force a different level, either by using a numeric prefix
  7407. argument, or by inserting the heading marker by hand. For example, if the
  7408. cursor is after \"*****\", then the tree will be shifted to level 5.
  7409. If optional TREE is given, use this text instead of the kill ring.
  7410. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  7411. move back over whitespace before inserting, and move point to the end of
  7412. the inserted text when done."
  7413. (interactive "P")
  7414. (setq tree (or tree (and kill-ring (current-kill 0))))
  7415. (unless (org-kill-is-subtree-p tree)
  7416. (user-error "%s"
  7417. (substitute-command-keys
  7418. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  7419. (org-with-limited-levels
  7420. (let* ((visp (not (outline-invisible-p)))
  7421. (txt tree)
  7422. (^re_ "\\(\\*+\\)[ \t]*")
  7423. (old-level (if (string-match org-outline-regexp-bol txt)
  7424. (- (match-end 0) (match-beginning 0) 1)
  7425. -1))
  7426. (force-level (cond (level (prefix-numeric-value level))
  7427. ((and (looking-at "[ \t]*$")
  7428. (string-match
  7429. "^\\*+$" (buffer-substring
  7430. (point-at-bol) (point))))
  7431. (- (match-end 1) (match-beginning 1)))
  7432. ((and (bolp)
  7433. (looking-at org-outline-regexp))
  7434. (- (match-end 0) (point) 1))))
  7435. (previous-level (save-excursion
  7436. (condition-case nil
  7437. (progn
  7438. (outline-previous-visible-heading 1)
  7439. (if (looking-at ^re_)
  7440. (- (match-end 0) (match-beginning 0) 1)
  7441. 1))
  7442. (error 1))))
  7443. (next-level (save-excursion
  7444. (condition-case nil
  7445. (progn
  7446. (or (looking-at org-outline-regexp)
  7447. (outline-next-visible-heading 1))
  7448. (if (looking-at ^re_)
  7449. (- (match-end 0) (match-beginning 0) 1)
  7450. 1))
  7451. (error 1))))
  7452. (new-level (or force-level (max previous-level next-level)))
  7453. (shift (if (or (= old-level -1)
  7454. (= new-level -1)
  7455. (= old-level new-level))
  7456. 0
  7457. (- new-level old-level)))
  7458. (delta (if (> shift 0) -1 1))
  7459. (func (if (> shift 0) 'org-demote 'org-promote))
  7460. (org-odd-levels-only nil)
  7461. beg end newend)
  7462. ;; Remove the forced level indicator
  7463. (if force-level
  7464. (delete-region (point-at-bol) (point)))
  7465. ;; Paste
  7466. (beginning-of-line (if (bolp) 1 2))
  7467. (setq beg (point))
  7468. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  7469. (insert-before-markers txt)
  7470. (unless (string-match "\n\\'" txt) (insert "\n"))
  7471. (setq newend (point))
  7472. (org-reinstall-markers-in-region beg)
  7473. (setq end (point))
  7474. (goto-char beg)
  7475. (skip-chars-forward " \t\n\r")
  7476. (setq beg (point))
  7477. (if (and (outline-invisible-p) visp)
  7478. (save-excursion (outline-show-heading)))
  7479. ;; Shift if necessary
  7480. (unless (= shift 0)
  7481. (save-restriction
  7482. (narrow-to-region beg end)
  7483. (while (not (= shift 0))
  7484. (org-map-region func (point-min) (point-max))
  7485. (setq shift (+ delta shift)))
  7486. (goto-char (point-min))
  7487. (setq newend (point-max))))
  7488. (when (or (org-called-interactively-p 'interactive) for-yank)
  7489. (message "Clipboard pasted as level %d subtree" new-level))
  7490. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  7491. kill-ring
  7492. (eq org-subtree-clip (current-kill 0))
  7493. org-subtree-clip-folded)
  7494. ;; The tree was folded before it was killed/copied
  7495. (hide-subtree))
  7496. (and for-yank (goto-char newend)))))
  7497. (defun org-kill-is-subtree-p (&optional txt)
  7498. "Check if the current kill is an outline subtree, or a set of trees.
  7499. Returns nil if kill does not start with a headline, or if the first
  7500. headline level is not the largest headline level in the tree.
  7501. So this will actually accept several entries of equal levels as well,
  7502. which is OK for `org-paste-subtree'.
  7503. If optional TXT is given, check this string instead of the current kill."
  7504. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  7505. (re (org-get-limited-outline-regexp))
  7506. (^re (concat "^" re))
  7507. (start-level (and kill
  7508. (string-match
  7509. (concat "\\`\\([ \t\n\r]*?\n\\)?\\(" re "\\)")
  7510. kill)
  7511. (- (match-end 2) (match-beginning 2) 1)))
  7512. (start (1+ (or (match-beginning 2) -1))))
  7513. (if (not start-level)
  7514. (progn
  7515. nil) ;; does not even start with a heading
  7516. (catch 'exit
  7517. (while (setq start (string-match ^re kill (1+ start)))
  7518. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  7519. (throw 'exit nil)))
  7520. t))))
  7521. (defvar org-markers-to-move nil
  7522. "Markers that should be moved with a cut-and-paste operation.
  7523. Those markers are stored together with their positions relative to
  7524. the start of the region.")
  7525. (defun org-save-markers-in-region (beg end)
  7526. "Check markers in region.
  7527. If these markers are between BEG and END, record their position relative
  7528. to BEG, so that after moving the block of text, we can put the markers back
  7529. into place.
  7530. This function gets called just before an entry or tree gets cut from the
  7531. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  7532. called immediately, to move the markers with the entries."
  7533. (setq org-markers-to-move nil)
  7534. (when (featurep 'org-clock)
  7535. (org-clock-save-markers-for-cut-and-paste beg end))
  7536. (when (featurep 'org-agenda)
  7537. (org-agenda-save-markers-for-cut-and-paste beg end)))
  7538. (defun org-check-and-save-marker (marker beg end)
  7539. "Check if MARKER is between BEG and END.
  7540. If yes, remember the marker and the distance to BEG."
  7541. (when (and (marker-buffer marker)
  7542. (equal (marker-buffer marker) (current-buffer)))
  7543. (if (and (>= marker beg) (< marker end))
  7544. (push (cons marker (- marker beg)) org-markers-to-move))))
  7545. (defun org-reinstall-markers-in-region (beg)
  7546. "Move all remembered markers to their position relative to BEG."
  7547. (mapc (lambda (x)
  7548. (move-marker (car x) (+ beg (cdr x))))
  7549. org-markers-to-move)
  7550. (setq org-markers-to-move nil))
  7551. (defun org-narrow-to-subtree ()
  7552. "Narrow buffer to the current subtree."
  7553. (interactive)
  7554. (save-excursion
  7555. (save-match-data
  7556. (org-with-limited-levels
  7557. (narrow-to-region
  7558. (progn (org-back-to-heading t) (point))
  7559. (progn (org-end-of-subtree t t)
  7560. (if (and (org-at-heading-p) (not (eobp))) (backward-char 1))
  7561. (point)))))))
  7562. (defun org-narrow-to-block ()
  7563. "Narrow buffer to the current block."
  7564. (interactive)
  7565. (let* ((case-fold-search t)
  7566. (blockp (org-between-regexps-p "^[ \t]*#\\+begin_.*"
  7567. "^[ \t]*#\\+end_.*")))
  7568. (if blockp
  7569. (narrow-to-region (car blockp) (cdr blockp))
  7570. (user-error "Not in a block"))))
  7571. (eval-when-compile
  7572. (defvar org-property-drawer-re))
  7573. (defvar org-property-start-re) ;; defined below
  7574. (defun org-clone-subtree-with-time-shift (n &optional shift)
  7575. "Clone the task (subtree) at point N times.
  7576. The clones will be inserted as siblings.
  7577. In interactive use, the user will be prompted for the number of
  7578. clones to be produced. If the entry has a timestamp, the user
  7579. will also be prompted for a time shift, which may be a repeater
  7580. as used in time stamps, for example `+3d'. To disable this,
  7581. you can call the function with a universal prefix argument.
  7582. When a valid repeater is given and the entry contains any time
  7583. stamps, the clones will become a sequence in time, with time
  7584. stamps in the subtree shifted for each clone produced. If SHIFT
  7585. is nil or the empty string, time stamps will be left alone. The
  7586. ID property of the original subtree is removed.
  7587. If the original subtree did contain time stamps with a repeater,
  7588. the following will happen:
  7589. - the repeater will be removed in each clone
  7590. - an additional clone will be produced, with the current, unshifted
  7591. date(s) in the entry.
  7592. - the original entry will be placed *after* all the clones, with
  7593. repeater intact.
  7594. - the start days in the repeater in the original entry will be shifted
  7595. to past the last clone.
  7596. In this way you can spell out a number of instances of a repeating task,
  7597. and still retain the repeater to cover future instances of the task."
  7598. (interactive "nNumber of clones to produce: ")
  7599. (let ((shift
  7600. (or shift
  7601. (if (and (not (equal current-prefix-arg '(4)))
  7602. (save-excursion
  7603. (re-search-forward org-ts-regexp-both
  7604. (save-excursion
  7605. (org-end-of-subtree t)
  7606. (point)) t)))
  7607. (read-from-minibuffer
  7608. "Date shift per clone (e.g. +1w, empty to copy unchanged): ")
  7609. ""))) ;; No time shift
  7610. (n-no-remove -1)
  7611. (drawer-re org-drawer-regexp)
  7612. beg end template task idprop
  7613. shift-n shift-what doshift nmin nmax)
  7614. (if (not (and (integerp n) (> n 0)))
  7615. (error "Invalid number of replications %s" n))
  7616. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  7617. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([hdwmy]\\)[ \t]*\\'"
  7618. shift)))
  7619. (error "Invalid shift specification %s" shift))
  7620. (when doshift
  7621. (setq shift-n (string-to-number (match-string 1 shift))
  7622. shift-what (cdr (assoc (match-string 2 shift)
  7623. '(("d" . day) ("w" . week)
  7624. ("m" . month) ("y" . year))))))
  7625. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  7626. (setq nmin 1 nmax n)
  7627. (org-back-to-heading t)
  7628. (setq beg (point))
  7629. (setq idprop (org-entry-get nil "ID"))
  7630. (org-end-of-subtree t t)
  7631. (or (bolp) (insert "\n"))
  7632. (setq end (point))
  7633. (setq template (buffer-substring beg end))
  7634. (when (and doshift
  7635. (string-match "<[^<>\n]+ [.+]?\\+[0-9]+[hdwmy][^<>\n]*>" template))
  7636. (delete-region beg end)
  7637. (setq end beg)
  7638. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  7639. (goto-char end)
  7640. (loop for n from nmin to nmax do
  7641. ;; prepare clone
  7642. (with-temp-buffer
  7643. (insert template)
  7644. (org-mode)
  7645. (goto-char (point-min))
  7646. (org-show-subtree)
  7647. (and idprop (if org-clone-delete-id
  7648. (org-entry-delete nil "ID")
  7649. (org-id-get-create t)))
  7650. (unless (= n 0)
  7651. (while (re-search-forward "^[ \t]*CLOCK:.*$" nil t)
  7652. (kill-whole-line))
  7653. (goto-char (point-min))
  7654. (while (re-search-forward drawer-re nil t)
  7655. (mapc (lambda (d)
  7656. (org-remove-empty-drawer-at d (point))) org-drawers)))
  7657. (goto-char (point-min))
  7658. (when doshift
  7659. (while (re-search-forward org-ts-regexp-both nil t)
  7660. (org-timestamp-change (* n shift-n) shift-what))
  7661. (unless (= n n-no-remove)
  7662. (goto-char (point-min))
  7663. (while (re-search-forward org-ts-regexp nil t)
  7664. (save-excursion
  7665. (goto-char (match-beginning 0))
  7666. (if (looking-at "<[^<>\n]+\\( +[.+]?\\+[0-9]+[hdwmy]\\)")
  7667. (delete-region (match-beginning 1) (match-end 1)))))))
  7668. (setq task (buffer-string)))
  7669. (insert task))
  7670. (goto-char beg)))
  7671. ;;; Outline Sorting
  7672. (defun org-sort (with-case)
  7673. "Call `org-sort-entries', `org-table-sort-lines' or `org-sort-list'.
  7674. Optional argument WITH-CASE means sort case-sensitively."
  7675. (interactive "P")
  7676. (cond
  7677. ((org-at-table-p) (org-call-with-arg 'org-table-sort-lines with-case))
  7678. ((org-at-item-p) (org-call-with-arg 'org-sort-list with-case))
  7679. (t
  7680. (org-call-with-arg 'org-sort-entries with-case))))
  7681. (defun org-sort-remove-invisible (s)
  7682. "Remove invisible links from string S."
  7683. (remove-text-properties 0 (length s) org-rm-props s)
  7684. (while (string-match org-bracket-link-regexp s)
  7685. (setq s (replace-match (if (match-end 2)
  7686. (match-string 3 s)
  7687. (match-string 1 s)) t t s)))
  7688. (let ((st (format " %s " s)))
  7689. (while (string-match org-emph-re st)
  7690. (setq st (replace-match (format " %s " (match-string 4 st)) t t st)))
  7691. (setq s (substring st 1 -1)))
  7692. s)
  7693. (defvar org-priority-regexp) ; defined later in the file
  7694. (defvar org-after-sorting-entries-or-items-hook nil
  7695. "Hook that is run after a bunch of entries or items have been sorted.
  7696. When children are sorted, the cursor is in the parent line when this
  7697. hook gets called. When a region or a plain list is sorted, the cursor
  7698. will be in the first entry of the sorted region/list.")
  7699. (defun org-sort-entries
  7700. (&optional with-case sorting-type getkey-func compare-func property)
  7701. "Sort entries on a certain level of an outline tree.
  7702. If there is an active region, the entries in the region are sorted.
  7703. Else, if the cursor is before the first entry, sort the top-level items.
  7704. Else, the children of the entry at point are sorted.
  7705. Sorting can be alphabetically, numerically, by date/time as given by
  7706. a time stamp, by a property, by priority order, or by a custom function.
  7707. The command prompts for the sorting type unless it has been given to the
  7708. function through the SORTING-TYPE argument, which needs to be a character,
  7709. \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?o ?O ?r ?R ?f ?F). Here is the
  7710. precise meaning of each character:
  7711. n Numerically, by converting the beginning of the entry/item to a number.
  7712. a Alphabetically, ignoring the TODO keyword and the priority, if any.
  7713. o By order of TODO keywords.
  7714. t By date/time, either the first active time stamp in the entry, or, if
  7715. none exist, by the first inactive one.
  7716. s By the scheduled date/time.
  7717. d By deadline date/time.
  7718. c By creation time, which is assumed to be the first inactive time stamp
  7719. at the beginning of a line.
  7720. p By priority according to the cookie.
  7721. r By the value of a property.
  7722. Capital letters will reverse the sort order.
  7723. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  7724. called with point at the beginning of the record. It must return either
  7725. a string or a number that should serve as the sorting key for that record.
  7726. Comparing entries ignores case by default. However, with an optional argument
  7727. WITH-CASE, the sorting considers case as well.
  7728. Sorting is done against the visible part of the headlines, it ignores hidden
  7729. links."
  7730. (interactive "P")
  7731. (let ((case-func (if with-case 'identity 'downcase))
  7732. (cmstr
  7733. ;; The clock marker is lost when using `sort-subr', let's
  7734. ;; store the clocking string.
  7735. (when (equal (marker-buffer org-clock-marker) (current-buffer))
  7736. (save-excursion
  7737. (goto-char org-clock-marker)
  7738. (looking-back "^.*") (match-string-no-properties 0))))
  7739. start beg end stars re re2
  7740. txt what tmp)
  7741. ;; Find beginning and end of region to sort
  7742. (cond
  7743. ((org-region-active-p)
  7744. ;; we will sort the region
  7745. (setq end (region-end)
  7746. what "region")
  7747. (goto-char (region-beginning))
  7748. (if (not (org-at-heading-p)) (outline-next-heading))
  7749. (setq start (point)))
  7750. ((or (org-at-heading-p)
  7751. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  7752. ;; we will sort the children of the current headline
  7753. (org-back-to-heading)
  7754. (setq start (point)
  7755. end (progn (org-end-of-subtree t t)
  7756. (or (bolp) (insert "\n"))
  7757. (org-back-over-empty-lines)
  7758. (point))
  7759. what "children")
  7760. (goto-char start)
  7761. (show-subtree)
  7762. (outline-next-heading))
  7763. (t
  7764. ;; we will sort the top-level entries in this file
  7765. (goto-char (point-min))
  7766. (or (org-at-heading-p) (outline-next-heading))
  7767. (setq start (point))
  7768. (goto-char (point-max))
  7769. (beginning-of-line 1)
  7770. (when (looking-at ".*?\\S-")
  7771. ;; File ends in a non-white line
  7772. (end-of-line 1)
  7773. (insert "\n"))
  7774. (setq end (point-max))
  7775. (setq what "top-level")
  7776. (goto-char start)
  7777. (show-all)))
  7778. (setq beg (point))
  7779. (if (>= beg end) (user-error "Nothing to sort"))
  7780. (looking-at "\\(\\*+\\)")
  7781. (setq stars (match-string 1)
  7782. re (concat "^" (regexp-quote stars) " +")
  7783. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[ \t\n]")
  7784. txt (buffer-substring beg end))
  7785. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  7786. (if (and (not (equal stars "*")) (string-match re2 txt))
  7787. (user-error "Region to sort contains a level above the first entry"))
  7788. (unless sorting-type
  7789. (message
  7790. "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
  7791. [t]ime [s]cheduled [d]eadline [c]reated
  7792. A/N/P/R/O/F/T/S/D/C means reversed:"
  7793. what)
  7794. (setq sorting-type (read-char-exclusive))
  7795. (unless getkey-func
  7796. (and (= (downcase sorting-type) ?f)
  7797. (setq getkey-func
  7798. (org-icompleting-read "Sort using function: "
  7799. obarray 'fboundp t nil nil))
  7800. (setq getkey-func (intern getkey-func))))
  7801. (and (= (downcase sorting-type) ?r)
  7802. (not property)
  7803. (setq property
  7804. (org-icompleting-read "Property: "
  7805. (mapcar 'list (org-buffer-property-keys t))
  7806. nil t))))
  7807. (message "Sorting entries...")
  7808. (save-restriction
  7809. (narrow-to-region start end)
  7810. (let ((dcst (downcase sorting-type))
  7811. (case-fold-search nil)
  7812. (now (current-time)))
  7813. (sort-subr
  7814. (/= dcst sorting-type)
  7815. ;; This function moves to the beginning character of the "record" to
  7816. ;; be sorted.
  7817. (lambda nil
  7818. (if (re-search-forward re nil t)
  7819. (goto-char (match-beginning 0))
  7820. (goto-char (point-max))))
  7821. ;; This function moves to the last character of the "record" being
  7822. ;; sorted.
  7823. (lambda nil
  7824. (save-match-data
  7825. (condition-case nil
  7826. (outline-forward-same-level 1)
  7827. (error
  7828. (goto-char (point-max))))))
  7829. ;; This function returns the value that gets sorted against.
  7830. (lambda nil
  7831. (cond
  7832. ((= dcst ?n)
  7833. (if (looking-at org-complex-heading-regexp)
  7834. (string-to-number (org-sort-remove-invisible (match-string 4)))
  7835. nil))
  7836. ((= dcst ?a)
  7837. (if (looking-at org-complex-heading-regexp)
  7838. (funcall case-func (org-sort-remove-invisible (match-string 4)))
  7839. nil))
  7840. ((= dcst ?t)
  7841. (let ((end (save-excursion (outline-next-heading) (point))))
  7842. (if (or (re-search-forward org-ts-regexp end t)
  7843. (re-search-forward org-ts-regexp-both end t))
  7844. (org-time-string-to-seconds (match-string 0))
  7845. (org-float-time now))))
  7846. ((= dcst ?c)
  7847. (let ((end (save-excursion (outline-next-heading) (point))))
  7848. (if (re-search-forward
  7849. (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
  7850. end t)
  7851. (org-time-string-to-seconds (match-string 0))
  7852. (org-float-time now))))
  7853. ((= dcst ?s)
  7854. (let ((end (save-excursion (outline-next-heading) (point))))
  7855. (if (re-search-forward org-scheduled-time-regexp end t)
  7856. (org-time-string-to-seconds (match-string 1))
  7857. (org-float-time now))))
  7858. ((= dcst ?d)
  7859. (let ((end (save-excursion (outline-next-heading) (point))))
  7860. (if (re-search-forward org-deadline-time-regexp end t)
  7861. (org-time-string-to-seconds (match-string 1))
  7862. (org-float-time now))))
  7863. ((= dcst ?p)
  7864. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  7865. (string-to-char (match-string 2))
  7866. org-default-priority))
  7867. ((= dcst ?r)
  7868. (or (org-entry-get nil property) ""))
  7869. ((= dcst ?o)
  7870. (if (looking-at org-complex-heading-regexp)
  7871. (- 9999 (length (member (match-string 2)
  7872. org-todo-keywords-1)))))
  7873. ((= dcst ?f)
  7874. (if getkey-func
  7875. (progn
  7876. (setq tmp (funcall getkey-func))
  7877. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  7878. tmp)
  7879. (error "Invalid key function `%s'" getkey-func)))
  7880. (t (error "Invalid sorting type `%c'" sorting-type))))
  7881. nil
  7882. (cond
  7883. ((= dcst ?a) 'string<)
  7884. ((= dcst ?f) compare-func)
  7885. ((member dcst '(?p ?t ?s ?d ?c)) '<)))))
  7886. (run-hooks 'org-after-sorting-entries-or-items-hook)
  7887. ;; Reset the clock marker if needed
  7888. (when cmstr
  7889. (save-excursion
  7890. (goto-char start)
  7891. (search-forward cmstr nil t)
  7892. (move-marker org-clock-marker (point))))
  7893. (message "Sorting entries...done")))
  7894. (defun org-do-sort (table what &optional with-case sorting-type)
  7895. "Sort TABLE of WHAT according to SORTING-TYPE.
  7896. The user will be prompted for the SORTING-TYPE if the call to this
  7897. function does not specify it. WHAT is only for the prompt, to indicate
  7898. what is being sorted. The sorting key will be extracted from
  7899. the car of the elements of the table.
  7900. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  7901. (unless sorting-type
  7902. (message
  7903. "Sort %s: [a]lphabetic, [n]umeric, [t]ime. A/N/T means reversed:"
  7904. what)
  7905. (setq sorting-type (read-char-exclusive)))
  7906. (let ((dcst (downcase sorting-type))
  7907. extractfun comparefun)
  7908. ;; Define the appropriate functions
  7909. (cond
  7910. ((= dcst ?n)
  7911. (setq extractfun 'string-to-number
  7912. comparefun (if (= dcst sorting-type) '< '>)))
  7913. ((= dcst ?a)
  7914. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  7915. (lambda(x) (downcase (org-sort-remove-invisible x))))
  7916. comparefun (if (= dcst sorting-type)
  7917. 'string<
  7918. (lambda (a b) (and (not (string< a b))
  7919. (not (string= a b)))))))
  7920. ((= dcst ?t)
  7921. (setq extractfun
  7922. (lambda (x)
  7923. (if (or (string-match org-ts-regexp x)
  7924. (string-match org-ts-regexp-both x))
  7925. (org-float-time
  7926. (org-time-string-to-time (match-string 0 x)))
  7927. 0))
  7928. comparefun (if (= dcst sorting-type) '< '>)))
  7929. (t (error "Invalid sorting type `%c'" sorting-type)))
  7930. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  7931. table)
  7932. (lambda (a b) (funcall comparefun (car a) (car b))))))
  7933. ;;; The orgstruct minor mode
  7934. ;; Define a minor mode which can be used in other modes in order to
  7935. ;; integrate the org-mode structure editing commands.
  7936. ;; This is really a hack, because the org-mode structure commands use
  7937. ;; keys which normally belong to the major mode. Here is how it
  7938. ;; works: The minor mode defines all the keys necessary to operate the
  7939. ;; structure commands, but wraps the commands into a function which
  7940. ;; tests if the cursor is currently at a headline or a plain list
  7941. ;; item. If that is the case, the structure command is used,
  7942. ;; temporarily setting many Org-mode variables like regular
  7943. ;; expressions for filling etc. However, when any of those keys is
  7944. ;; used at a different location, function uses `key-binding' to look
  7945. ;; up if the key has an associated command in another currently active
  7946. ;; keymap (minor modes, major mode, global), and executes that
  7947. ;; command. There might be problems if any of the keys is otherwise
  7948. ;; used as a prefix key.
  7949. (defcustom orgstruct-heading-prefix-regexp nil
  7950. "Regexp that matches the custom prefix of Org headlines in
  7951. orgstruct(++)-mode."
  7952. :group 'org
  7953. :version "24.4"
  7954. :package-version '(Org . "8.0")
  7955. :type 'string)
  7956. ;;;###autoload(put 'orgstruct-heading-prefix-regexp 'safe-local-variable 'stringp)
  7957. (defcustom orgstruct-setup-hook nil
  7958. "Hook run after orgstruct-mode-map is filled."
  7959. :group 'org
  7960. :version "24.4"
  7961. :package-version '(Org . "8.0")
  7962. :type 'hook)
  7963. (defvar orgstruct-initialized nil)
  7964. (defvar org-local-vars nil
  7965. "List of local variables, for use by `orgstruct-mode'.")
  7966. ;;;###autoload
  7967. (define-minor-mode orgstruct-mode
  7968. "Toggle the minor mode `orgstruct-mode'.
  7969. This mode is for using Org-mode structure commands in other
  7970. modes. The following keys behave as if Org-mode were active, if
  7971. the cursor is on a headline, or on a plain list item (both as
  7972. defined by Org-mode)."
  7973. nil " OrgStruct" (make-sparse-keymap)
  7974. (funcall (if orgstruct-mode
  7975. 'add-to-invisibility-spec
  7976. 'remove-from-invisibility-spec)
  7977. '(outline . t))
  7978. (when orgstruct-mode
  7979. (org-load-modules-maybe)
  7980. (unless orgstruct-initialized
  7981. (orgstruct-setup)
  7982. (setq orgstruct-initialized t))))
  7983. ;;;###autoload
  7984. (defun turn-on-orgstruct ()
  7985. "Unconditionally turn on `orgstruct-mode'."
  7986. (orgstruct-mode 1))
  7987. (defvar org-fb-vars nil)
  7988. (make-variable-buffer-local 'org-fb-vars)
  7989. (defun orgstruct++-mode (&optional arg)
  7990. "Toggle `orgstruct-mode', the enhanced version of it.
  7991. In addition to setting orgstruct-mode, this also exports all
  7992. indentation and autofilling variables from org-mode into the
  7993. buffer. It will also recognize item context in multiline items."
  7994. (interactive "P")
  7995. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  7996. (if (< arg 1)
  7997. (progn (orgstruct-mode -1)
  7998. (mapc (lambda(v)
  7999. (org-set-local (car v)
  8000. (if (eq (car-safe (cadr v)) 'quote) (cadadr v) (cadr v))))
  8001. org-fb-vars))
  8002. (orgstruct-mode 1)
  8003. (setq org-fb-vars nil)
  8004. (unless org-local-vars
  8005. (setq org-local-vars (org-get-local-variables)))
  8006. (let (var val)
  8007. (mapc
  8008. (lambda (x)
  8009. (when (string-match
  8010. "^\\(paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|fill-prefix\\|indent-\\)"
  8011. (symbol-name (car x)))
  8012. (setq var (car x) val (nth 1 x))
  8013. (push (list var `(quote ,(eval var))) org-fb-vars)
  8014. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  8015. org-local-vars)
  8016. (org-set-local 'orgstruct-is-++ t))))
  8017. (defvar orgstruct-is-++ nil
  8018. "Is `orgstruct-mode' in ++ version in the current-buffer?")
  8019. (make-variable-buffer-local 'orgstruct-is-++)
  8020. ;;;###autoload
  8021. (defun turn-on-orgstruct++ ()
  8022. "Unconditionally turn on `orgstruct++-mode'."
  8023. (orgstruct++-mode 1))
  8024. (defun orgstruct-error ()
  8025. "Error when there is no default binding for a structure key."
  8026. (interactive)
  8027. (funcall (if (fboundp 'user-error)
  8028. 'user-error
  8029. 'error)
  8030. "This key has no function outside structure elements"))
  8031. (defun orgstruct-setup ()
  8032. "Setup orgstruct keymap."
  8033. (dolist (cell '((org-demote . t)
  8034. (org-metaleft . t)
  8035. (org-metaright . t)
  8036. (org-promote . t)
  8037. (org-shiftmetaleft . t)
  8038. (org-shiftmetaright . t)
  8039. org-backward-element
  8040. org-backward-heading-same-level
  8041. org-ctrl-c-ret
  8042. org-ctrl-c-minus
  8043. org-ctrl-c-star
  8044. org-cycle
  8045. org-forward-heading-same-level
  8046. org-insert-heading
  8047. org-insert-heading-respect-content
  8048. org-kill-note-or-show-branches
  8049. org-mark-subtree
  8050. org-meta-return
  8051. org-metadown
  8052. org-metaup
  8053. org-narrow-to-subtree
  8054. org-promote-subtree
  8055. org-reveal
  8056. org-shiftdown
  8057. org-shiftleft
  8058. org-shiftmetadown
  8059. org-shiftmetaup
  8060. org-shiftright
  8061. org-shifttab
  8062. org-shifttab
  8063. org-shiftup
  8064. org-show-subtree
  8065. org-sort
  8066. org-up-element
  8067. outline-demote
  8068. outline-next-visible-heading
  8069. outline-previous-visible-heading
  8070. outline-promote
  8071. outline-up-heading
  8072. show-children))
  8073. (let ((f (or (car-safe cell) cell))
  8074. (disable-when-heading-prefix (cdr-safe cell)))
  8075. (when (fboundp f)
  8076. (dolist (binding (nconc (where-is-internal f org-mode-map)
  8077. (where-is-internal f outline-mode-map)))
  8078. ;; TODO use local-function-key-map
  8079. (dolist (rep '(("<tab>" . "TAB")
  8080. ("<return>" . "RET")
  8081. ("<escape>" . "ESC")
  8082. ("<delete>" . "DEL")))
  8083. (setq binding (read-kbd-macro
  8084. (let ((case-fold-search))
  8085. (replace-regexp-in-string
  8086. (regexp-quote (cdr rep))
  8087. (car rep)
  8088. (key-description binding))))))
  8089. (let ((key (lookup-key orgstruct-mode-map binding)))
  8090. (when (or (not key) (numberp key))
  8091. (condition-case nil
  8092. (org-defkey orgstruct-mode-map
  8093. binding
  8094. (orgstruct-make-binding f binding disable-when-heading-prefix))
  8095. (error nil))))))))
  8096. (run-hooks 'orgstruct-setup-hook))
  8097. (defun orgstruct-make-binding (fun key disable-when-heading-prefix)
  8098. "Create a function for binding in the structure minor mode.
  8099. FUN is the command to call inside a table. KEY is the key that
  8100. should be checked in for a command to execute outside of tables.
  8101. Non-nil DISABLE-WHEN-HEADING-PREFIX means to disable the command
  8102. if `orgstruct-heading-prefix-regexp' is non-nil."
  8103. (let ((name (concat "orgstruct-hijacker-" (symbol-name fun))))
  8104. (let ((nname name)
  8105. (i 0))
  8106. (while (fboundp (intern nname))
  8107. (setq nname (format "%s-%d" name (setq i (1+ i)))))
  8108. (setq name (intern nname)))
  8109. (eval
  8110. (let ((bindings '((org-heading-regexp
  8111. (concat "^"
  8112. orgstruct-heading-prefix-regexp
  8113. "\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ ]*$"))
  8114. (org-outline-regexp
  8115. (concat orgstruct-heading-prefix-regexp "\\*+ "))
  8116. (org-outline-regexp-bol
  8117. (concat "^" org-outline-regexp))
  8118. (outline-regexp org-outline-regexp)
  8119. (outline-heading-end-regexp "\n")
  8120. (outline-level 'org-outline-level)
  8121. (outline-heading-alist))))
  8122. `(defun ,name (arg)
  8123. ,(concat "In Structure, run `" (symbol-name fun) "'.\n"
  8124. "Outside of structure, run the binding of `"
  8125. (key-description key) "'."
  8126. (when disable-when-heading-prefix
  8127. (concat
  8128. "\nIf `orgstruct-heading-prefix-regexp' is non-nil, this command will always fall\n"
  8129. "back to the default binding due to limitations of Org's implementation of\n"
  8130. "`" (symbol-name fun) "'.")))
  8131. (interactive "p")
  8132. (let* ((disable
  8133. ,(when disable-when-heading-prefix
  8134. '(and orgstruct-heading-prefix-regexp
  8135. (not (string= orgstruct-heading-prefix-regexp "")))))
  8136. (fallback
  8137. (or disable
  8138. (not
  8139. (let* ,bindings
  8140. (org-context-p 'headline 'item
  8141. ,(when (memq fun
  8142. '(org-insert-heading
  8143. org-insert-heading-respect-content
  8144. org-meta-return))
  8145. '(when orgstruct-is-++
  8146. 'item-body))))))))
  8147. (if fallback
  8148. (let* ((orgstruct-mode)
  8149. (binding
  8150. (loop with key = ,key
  8151. for rep in
  8152. '(nil
  8153. ("<\\([^>]*\\)tab>" . "\\1TAB")
  8154. ("<\\([^>]*\\)return>" . "\\1RET")
  8155. ("<\\([^>]*\\)escape>" . "\\1ESC")
  8156. ("<\\([^>]*\\)delete>" . "\\1DEL"))
  8157. do
  8158. (when rep
  8159. (setq key (read-kbd-macro
  8160. (let ((case-fold-search))
  8161. (replace-regexp-in-string
  8162. (car rep)
  8163. (cdr rep)
  8164. (key-description key))))))
  8165. thereis (key-binding key))))
  8166. (if (keymapp binding)
  8167. (set-temporary-overlay-map binding)
  8168. (let ((func (or binding
  8169. (unless disable
  8170. 'orgstruct-error))))
  8171. (when func
  8172. (call-interactively func)))))
  8173. (org-run-like-in-org-mode
  8174. (lambda ()
  8175. (interactive)
  8176. (let* ,bindings
  8177. (call-interactively ',fun)))))))))
  8178. name))
  8179. (defun org-contextualize-keys (alist contexts)
  8180. "Return valid elements in ALIST depending on CONTEXTS.
  8181. `org-agenda-custom-commands' or `org-capture-templates' are the
  8182. values used for ALIST, and `org-agenda-custom-commands-contexts'
  8183. or `org-capture-templates-contexts' are the associated contexts
  8184. definitions."
  8185. (let ((contexts
  8186. ;; normalize contexts
  8187. (mapcar
  8188. (lambda(c) (cond ((listp (cadr c))
  8189. (list (car c) (car c) (cadr c)))
  8190. ((string= "" (cadr c))
  8191. (list (car c) (car c) (caddr c)))
  8192. (t c))) contexts))
  8193. (a alist) c r s)
  8194. ;; loop over all commands or templates
  8195. (while (setq c (pop a))
  8196. (let (vrules repl)
  8197. (cond
  8198. ((not (assoc (car c) contexts))
  8199. (push c r))
  8200. ((and (assoc (car c) contexts)
  8201. (setq vrules (org-contextualize-validate-key
  8202. (car c) contexts)))
  8203. (mapc (lambda (vr)
  8204. (when (not (equal (car vr) (cadr vr)))
  8205. (setq repl vr))) vrules)
  8206. (if (not repl) (push c r)
  8207. (push (cadr repl) s)
  8208. (push
  8209. (cons (car c)
  8210. (cdr (or (assoc (cadr repl) alist)
  8211. (error "Undefined key `%s' as contextual replacement for `%s'"
  8212. (cadr repl) (car c)))))
  8213. r))))))
  8214. ;; Return limited ALIST, possibly with keys modified, and deduplicated
  8215. (delq
  8216. nil
  8217. (delete-dups
  8218. (mapcar (lambda (x)
  8219. (let ((tpl (car x)))
  8220. (when (not (delq
  8221. nil
  8222. (mapcar (lambda(y)
  8223. (equal y tpl)) s))) x)))
  8224. (reverse r))))))
  8225. (defun org-contextualize-validate-key (key contexts)
  8226. "Check CONTEXTS for agenda or capture KEY."
  8227. (let (r rr res)
  8228. (while (setq r (pop contexts))
  8229. (mapc
  8230. (lambda (rr)
  8231. (when
  8232. (and (equal key (car r))
  8233. (if (functionp rr) (funcall rr)
  8234. (or (and (eq (car rr) 'in-file)
  8235. (buffer-file-name)
  8236. (string-match (cdr rr) (buffer-file-name)))
  8237. (and (eq (car rr) 'in-mode)
  8238. (string-match (cdr rr) (symbol-name major-mode)))
  8239. (and (eq (car rr) 'in-buffer)
  8240. (string-match (cdr rr) (buffer-name)))
  8241. (when (and (eq (car rr) 'not-in-file)
  8242. (buffer-file-name))
  8243. (not (string-match (cdr rr) (buffer-file-name))))
  8244. (when (eq (car rr) 'not-in-mode)
  8245. (not (string-match (cdr rr) (symbol-name major-mode))))
  8246. (when (eq (car rr) 'not-in-buffer)
  8247. (not (string-match (cdr rr) (buffer-name)))))))
  8248. (push r res)))
  8249. (car (last r))))
  8250. (delete-dups (delq nil res))))
  8251. (defun org-context-p (&rest contexts)
  8252. "Check if local context is any of CONTEXTS.
  8253. Possible values in the list of contexts are `table', `headline', and `item'."
  8254. (let ((pos (point)))
  8255. (goto-char (point-at-bol))
  8256. (prog1 (or (and (memq 'table contexts)
  8257. (looking-at "[ \t]*|"))
  8258. (and (memq 'headline contexts)
  8259. (looking-at org-outline-regexp))
  8260. (and (memq 'item contexts)
  8261. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  8262. (and (memq 'item-body contexts)
  8263. (org-in-item-p)))
  8264. (goto-char pos))))
  8265. (defun org-get-local-variables ()
  8266. "Return a list of all local variables in an Org mode buffer."
  8267. (let (varlist)
  8268. (with-current-buffer (get-buffer-create "*Org tmp*")
  8269. (erase-buffer)
  8270. (org-mode)
  8271. (setq varlist (buffer-local-variables)))
  8272. (kill-buffer "*Org tmp*")
  8273. (delq nil
  8274. (mapcar
  8275. (lambda (x)
  8276. (setq x
  8277. (if (symbolp x)
  8278. (list x)
  8279. (list (car x) (cdr x))))
  8280. (if (and (not (get (car x) 'org-state))
  8281. (string-match
  8282. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|indent-\\)"
  8283. (symbol-name (car x))))
  8284. x nil))
  8285. varlist))))
  8286. (defun org-clone-local-variables (from-buffer &optional regexp)
  8287. "Clone local variables from FROM-BUFFER.
  8288. Optional argument REGEXP selects variables to clone."
  8289. (mapc
  8290. (lambda (pair)
  8291. (and (symbolp (car pair))
  8292. (or (null regexp)
  8293. (string-match regexp (symbol-name (car pair))))
  8294. (set (make-local-variable (car pair))
  8295. (cdr pair))))
  8296. (buffer-local-variables from-buffer)))
  8297. ;;;###autoload
  8298. (defun org-run-like-in-org-mode (cmd)
  8299. "Run a command, pretending that the current buffer is in Org-mode.
  8300. This will temporarily bind local variables that are typically bound in
  8301. Org-mode to the values they have in Org-mode, and then interactively
  8302. call CMD."
  8303. (org-load-modules-maybe)
  8304. (unless org-local-vars
  8305. (setq org-local-vars (org-get-local-variables)))
  8306. (let (binds)
  8307. (dolist (var org-local-vars)
  8308. (when (or (not (boundp (car var)))
  8309. (eq (symbol-value (car var))
  8310. (default-value (car var))))
  8311. (push (list (car var) `(quote ,(cadr var))) binds)))
  8312. (eval `(let ,binds
  8313. (call-interactively (quote ,cmd))))))
  8314. ;;;; Archiving
  8315. (defun org-get-category (&optional pos force-refresh)
  8316. "Get the category applying to position POS."
  8317. (save-match-data
  8318. (if force-refresh (org-refresh-category-properties))
  8319. (let ((pos (or pos (point))))
  8320. (or (get-text-property pos 'org-category)
  8321. (progn (org-refresh-category-properties)
  8322. (get-text-property pos 'org-category))))))
  8323. (defun org-refresh-category-properties ()
  8324. "Refresh category text properties in the buffer."
  8325. (let ((case-fold-search t)
  8326. (inhibit-read-only t)
  8327. (def-cat (cond
  8328. ((null org-category)
  8329. (if buffer-file-name
  8330. (file-name-sans-extension
  8331. (file-name-nondirectory buffer-file-name))
  8332. "???"))
  8333. ((symbolp org-category) (symbol-name org-category))
  8334. (t org-category)))
  8335. beg end cat pos optionp)
  8336. (org-with-silent-modifications
  8337. (save-excursion
  8338. (save-restriction
  8339. (widen)
  8340. (goto-char (point-min))
  8341. (put-text-property (point) (point-max) 'org-category def-cat)
  8342. (while (re-search-forward
  8343. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  8344. (setq pos (match-end 0)
  8345. optionp (equal (char-after (match-beginning 0)) ?#)
  8346. cat (org-trim (match-string 2)))
  8347. (if optionp
  8348. (setq beg (point-at-bol) end (point-max))
  8349. (org-back-to-heading t)
  8350. (setq beg (point) end (org-end-of-subtree t t)))
  8351. (put-text-property beg end 'org-category cat)
  8352. (put-text-property beg end 'org-category-position beg)
  8353. (goto-char pos)))))))
  8354. (defun org-refresh-properties (dprop tprop)
  8355. "Refresh buffer text properties.
  8356. DPROP is the drawer property and TPROP is the corresponding text
  8357. property to set."
  8358. (let ((case-fold-search t)
  8359. (inhibit-read-only t) p)
  8360. (org-with-silent-modifications
  8361. (save-excursion
  8362. (save-restriction
  8363. (widen)
  8364. (goto-char (point-min))
  8365. (while (re-search-forward (concat "^[ \t]*:" dprop ": +\\(.*\\)[ \t]*$") nil t)
  8366. (setq p (org-match-string-no-properties 1))
  8367. (save-excursion
  8368. (org-back-to-heading t)
  8369. (put-text-property
  8370. (point-at-bol) (org-end-of-subtree t t) tprop p))))))))
  8371. ;;;; Link Stuff
  8372. ;;; Link abbreviations
  8373. (defun org-link-expand-abbrev (link)
  8374. "Apply replacements as defined in `org-link-abbrev-alist'."
  8375. (if (string-match "^\\([^:]*\\)\\(::?\\(.*\\)\\)?$" link)
  8376. (let* ((key (match-string 1 link))
  8377. (as (or (assoc key org-link-abbrev-alist-local)
  8378. (assoc key org-link-abbrev-alist)))
  8379. (tag (and (match-end 2) (match-string 3 link)))
  8380. rpl)
  8381. (if (not as)
  8382. link
  8383. (setq rpl (cdr as))
  8384. (cond
  8385. ((symbolp rpl) (funcall rpl tag))
  8386. ((string-match "%(\\([^)]+\\))" rpl)
  8387. (replace-match
  8388. (save-match-data
  8389. (funcall (intern-soft (match-string 1 rpl)) tag)) t t rpl))
  8390. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  8391. ((string-match "%h" rpl)
  8392. (replace-match (url-hexify-string (or tag "")) t t rpl))
  8393. (t (concat rpl tag)))))
  8394. link))
  8395. ;;; Storing and inserting links
  8396. (defvar org-insert-link-history nil
  8397. "Minibuffer history for links inserted with `org-insert-link'.")
  8398. (defvar org-stored-links nil
  8399. "Contains the links stored with `org-store-link'.")
  8400. (defvar org-store-link-plist nil
  8401. "Plist with info about the most recently link created with `org-store-link'.")
  8402. (defvar org-link-protocols nil
  8403. "Link protocols added to Org-mode using `org-add-link-type'.")
  8404. (defvar org-store-link-functions nil
  8405. "List of functions that are called to create and store a link.
  8406. Each function will be called in turn until one returns a non-nil
  8407. value. Each function should check if it is responsible for creating
  8408. this link (for example by looking at the major mode).
  8409. If not, it must exit and return nil.
  8410. If yes, it should return a non-nil value after a calling
  8411. `org-store-link-props' with a list of properties and values.
  8412. Special properties are:
  8413. :type The link prefix, like \"http\". This must be given.
  8414. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  8415. This is obligatory as well.
  8416. :description Optional default description for the second pair
  8417. of brackets in an Org-mode link. The user can still change
  8418. this when inserting this link into an Org-mode buffer.
  8419. In addition to these, any additional properties can be specified
  8420. and then used in capture templates.")
  8421. (defun org-add-link-type (type &optional follow export)
  8422. "Add TYPE to the list of `org-link-types'.
  8423. Re-compute all regular expressions depending on `org-link-types'
  8424. FOLLOW and EXPORT are two functions.
  8425. FOLLOW should take the link path as the single argument and do whatever
  8426. is necessary to follow the link, for example find a file or display
  8427. a mail message.
  8428. EXPORT should format the link path for export to one of the export formats.
  8429. It should be a function accepting three arguments:
  8430. path the path of the link, the text after the prefix (like \"http:\")
  8431. desc the description of the link, if any, or a description added by
  8432. org-export-normalize-links if there is none
  8433. format the export format, a symbol like `html' or `latex' or `ascii'..
  8434. The function may use the FORMAT information to return different values
  8435. depending on the format. The return value will be put literally into
  8436. the exported file. If the return value is nil, this means Org should
  8437. do what it normally does with links which do not have EXPORT defined.
  8438. Org-mode has a built-in default for exporting links. If you are happy with
  8439. this default, there is no need to define an export function for the link
  8440. type. For a simple example of an export function, see `org-bbdb.el'."
  8441. (add-to-list 'org-link-types type t)
  8442. (org-make-link-regexps)
  8443. (if (assoc type org-link-protocols)
  8444. (setcdr (assoc type org-link-protocols) (list follow export))
  8445. (push (list type follow export) org-link-protocols)))
  8446. (defvar org-agenda-buffer-name) ; Defined in org-agenda.el
  8447. (defvar org-id-link-to-org-use-id) ; Defined in org-id.el
  8448. ;;;###autoload
  8449. (defun org-store-link (arg)
  8450. "\\<org-mode-map>Store an org-link to the current location.
  8451. This link is added to `org-stored-links' and can later be inserted
  8452. into an org-buffer with \\[org-insert-link].
  8453. For some link types, a prefix arg is interpreted.
  8454. For links to Usenet articles, arg negates `org-gnus-prefer-web-links'.
  8455. For file links, arg negates `org-context-in-file-links'.
  8456. A double prefix arg force skipping storing functions that are not
  8457. part of Org's core.
  8458. A triple prefix arg force storing a link for each line in the
  8459. active region."
  8460. (interactive "P")
  8461. (org-load-modules-maybe)
  8462. (if (and (equal arg '(64)) (org-region-active-p))
  8463. (save-excursion
  8464. (let ((end (region-end)))
  8465. (goto-char (region-beginning))
  8466. (set-mark (point))
  8467. (while (< (point-at-eol) end)
  8468. (move-end-of-line 1) (activate-mark)
  8469. (let (current-prefix-arg)
  8470. (call-interactively 'org-store-link))
  8471. (move-beginning-of-line 2)
  8472. (set-mark (point)))))
  8473. (org-with-limited-levels
  8474. (setq org-store-link-plist nil)
  8475. (let (link cpltxt desc description search
  8476. txt custom-id agenda-link sfuns sfunsn)
  8477. (cond
  8478. ;; Store a link using an external link type
  8479. ((and (not (equal arg '(16)))
  8480. (setq sfuns
  8481. (delq
  8482. nil (mapcar (lambda (f)
  8483. (let (fs) (if (funcall f) (push f fs))))
  8484. org-store-link-functions))
  8485. sfunsn (mapcar (lambda (fu) (symbol-name (car fu))) sfuns))
  8486. (or (and (cdr sfuns)
  8487. (funcall (intern
  8488. (completing-read
  8489. "Which function for creating the link? "
  8490. sfunsn t (car sfunsn)))))
  8491. (funcall (caar sfuns)))
  8492. (setq link (plist-get org-store-link-plist :link)
  8493. desc (or (plist-get org-store-link-plist
  8494. :description) link))))
  8495. ;; Store a link from a source code buffer
  8496. ((org-src-edit-buffer-p)
  8497. (let (label gc)
  8498. (while (or (not label)
  8499. (save-excursion
  8500. (save-restriction
  8501. (widen)
  8502. (goto-char (point-min))
  8503. (re-search-forward
  8504. (regexp-quote (format org-coderef-label-format label))
  8505. nil t))))
  8506. (when label (message "Label exists already") (sit-for 2))
  8507. (setq label (read-string "Code line label: " label)))
  8508. (end-of-line 1)
  8509. (setq link (format org-coderef-label-format label))
  8510. (setq gc (- 79 (length link)))
  8511. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  8512. (insert link)
  8513. (setq link (concat "(" label ")") desc nil)))
  8514. ;; We are in the agenda, link to referenced location
  8515. ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
  8516. (let ((m (or (get-text-property (point) 'org-hd-marker)
  8517. (get-text-property (point) 'org-marker))))
  8518. (when m
  8519. (org-with-point-at m
  8520. (setq agenda-link
  8521. (if (org-called-interactively-p 'any)
  8522. (call-interactively 'org-store-link)
  8523. (org-store-link nil)))))))
  8524. ((eq major-mode 'calendar-mode)
  8525. (let ((cd (calendar-cursor-to-date)))
  8526. (setq link
  8527. (format-time-string
  8528. (car org-time-stamp-formats)
  8529. (apply 'encode-time
  8530. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  8531. nil nil nil))))
  8532. (org-store-link-props :type "calendar" :date cd)))
  8533. ((eq major-mode 'help-mode)
  8534. (setq link (concat "help:" (save-excursion
  8535. (goto-char (point-min))
  8536. (looking-at "^[^ ]+")
  8537. (match-string 0))))
  8538. (org-store-link-props :type "help"))
  8539. ((eq major-mode 'w3-mode)
  8540. (setq cpltxt (if (and (buffer-name)
  8541. (not (string-match "Untitled" (buffer-name))))
  8542. (buffer-name)
  8543. (url-view-url t))
  8544. link (url-view-url t))
  8545. (org-store-link-props :type "w3" :url (url-view-url t)))
  8546. ((eq major-mode 'image-mode)
  8547. (setq cpltxt (concat "file:"
  8548. (abbreviate-file-name buffer-file-name))
  8549. link cpltxt)
  8550. (org-store-link-props :type "image" :file buffer-file-name))
  8551. ;; In dired, store a link to the file of the current line
  8552. ((eq major-mode 'dired-mode)
  8553. (let ((file (dired-get-filename nil t)))
  8554. (setq file (if file
  8555. (abbreviate-file-name
  8556. (expand-file-name (dired-get-filename nil t)))
  8557. ;; otherwise, no file so use current directory.
  8558. default-directory))
  8559. (setq cpltxt (concat "file:" file)
  8560. link cpltxt)))
  8561. ((setq search (run-hook-with-args-until-success
  8562. 'org-create-file-search-functions))
  8563. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  8564. "::" search))
  8565. (setq cpltxt (or description link)))
  8566. ((and (buffer-file-name (buffer-base-buffer)) (derived-mode-p 'org-mode))
  8567. (setq custom-id (org-entry-get nil "CUSTOM_ID"))
  8568. (cond
  8569. ;; Store a link using the target at point
  8570. ((org-in-regexp "[^<]<<\\([^<>]+\\)>>[^>]" 1)
  8571. (setq cpltxt
  8572. (concat "file:"
  8573. (abbreviate-file-name
  8574. (buffer-file-name (buffer-base-buffer)))
  8575. "::" (match-string 1))
  8576. link cpltxt))
  8577. ((and (featurep 'org-id)
  8578. (or (eq org-id-link-to-org-use-id t)
  8579. (and (org-called-interactively-p 'any)
  8580. (or (eq org-id-link-to-org-use-id 'create-if-interactive)
  8581. (and (eq org-id-link-to-org-use-id
  8582. 'create-if-interactive-and-no-custom-id)
  8583. (not custom-id))))
  8584. (and org-id-link-to-org-use-id (org-entry-get nil "ID"))))
  8585. ;; Store a link using the ID at point
  8586. (setq link (condition-case nil
  8587. (prog1 (org-id-store-link)
  8588. (setq desc (plist-get org-store-link-plist
  8589. :description)))
  8590. (error
  8591. ;; Probably before first headline, link only to file
  8592. (concat "file:"
  8593. (abbreviate-file-name
  8594. (buffer-file-name (buffer-base-buffer))))))))
  8595. (t
  8596. ;; Just link to current headline
  8597. (setq cpltxt (concat "file:"
  8598. (abbreviate-file-name
  8599. (buffer-file-name (buffer-base-buffer)))))
  8600. ;; Add a context search string
  8601. (when (org-xor org-context-in-file-links arg)
  8602. (let* ((ee (org-element-at-point))
  8603. (et (org-element-type ee))
  8604. (ev (plist-get (cadr ee) :value))
  8605. (ek (plist-get (cadr ee) :key))
  8606. (eok (and (stringp ek) (string-match "name" ek))))
  8607. (setq txt (cond
  8608. ((org-at-heading-p) nil)
  8609. ((and (eq et 'keyword) eok) ev)
  8610. ((org-region-active-p)
  8611. (buffer-substring (region-beginning) (region-end)))))
  8612. (when (or (null txt) (string-match "\\S-" txt))
  8613. (setq cpltxt
  8614. (concat cpltxt "::"
  8615. (condition-case nil
  8616. (org-make-org-heading-search-string txt)
  8617. (error "")))
  8618. desc (or (and (eq et 'keyword) eok ev)
  8619. (nth 4 (ignore-errors (org-heading-components)))
  8620. "NONE")))))
  8621. (if (string-match "::\\'" cpltxt)
  8622. (setq cpltxt (substring cpltxt 0 -2)))
  8623. (setq link cpltxt))))
  8624. ((buffer-file-name (buffer-base-buffer))
  8625. ;; Just link to this file here.
  8626. (setq cpltxt (concat "file:"
  8627. (abbreviate-file-name
  8628. (buffer-file-name (buffer-base-buffer)))))
  8629. ;; Add a context string.
  8630. (when (org-xor org-context-in-file-links arg)
  8631. (setq txt (if (org-region-active-p)
  8632. (buffer-substring (region-beginning) (region-end))
  8633. (buffer-substring (point-at-bol) (point-at-eol))))
  8634. ;; Only use search option if there is some text.
  8635. (when (string-match "\\S-" txt)
  8636. (setq cpltxt
  8637. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  8638. desc "NONE")))
  8639. (setq link cpltxt))
  8640. ((org-called-interactively-p 'interactive)
  8641. (user-error "No method for storing a link from this buffer"))
  8642. (t (setq link nil)))
  8643. ;; We're done setting link and desc, clean up
  8644. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  8645. (setq link (or link cpltxt)
  8646. desc (or desc cpltxt))
  8647. (cond ((equal desc "NONE") (setq desc nil))
  8648. ((string-match org-bracket-link-analytic-regexp desc)
  8649. (let ((d0 (match-string 3 desc))
  8650. (p0 (match-string 5 desc)))
  8651. (setq desc
  8652. (replace-regexp-in-string
  8653. org-bracket-link-regexp
  8654. (concat (or p0 d0)
  8655. (if (equal (length (match-string 0 desc))
  8656. (length desc)) "*" "")) desc)))))
  8657. ;; Return the link
  8658. (if (not (and (or (org-called-interactively-p 'any)
  8659. executing-kbd-macro) link))
  8660. (or agenda-link (and link (org-make-link-string link desc)))
  8661. (push (list link desc) org-stored-links)
  8662. (message "Stored: %s" (or desc link))
  8663. (when custom-id
  8664. (setq link (concat "file:" (abbreviate-file-name
  8665. (buffer-file-name)) "::#" custom-id))
  8666. (push (list link desc) org-stored-links)))))))
  8667. (defun org-store-link-props (&rest plist)
  8668. "Store link properties, extract names and addresses."
  8669. (let (x adr)
  8670. (when (setq x (plist-get plist :from))
  8671. (setq adr (mail-extract-address-components x))
  8672. (setq plist (plist-put plist :fromname (car adr)))
  8673. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  8674. (when (setq x (plist-get plist :to))
  8675. (setq adr (mail-extract-address-components x))
  8676. (setq plist (plist-put plist :toname (car adr)))
  8677. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  8678. (let ((from (plist-get plist :from))
  8679. (to (plist-get plist :to)))
  8680. (when (and from to org-from-is-user-regexp)
  8681. (setq plist
  8682. (plist-put plist :fromto
  8683. (if (string-match org-from-is-user-regexp from)
  8684. (concat "to %t")
  8685. (concat "from %f"))))))
  8686. (setq org-store-link-plist plist))
  8687. (defun org-add-link-props (&rest plist)
  8688. "Add these properties to the link property list."
  8689. (let (key value)
  8690. (while plist
  8691. (setq key (pop plist) value (pop plist))
  8692. (setq org-store-link-plist
  8693. (plist-put org-store-link-plist key value)))))
  8694. (defun org-email-link-description (&optional fmt)
  8695. "Return the description part of an email link.
  8696. This takes information from `org-store-link-plist' and formats it
  8697. according to FMT (default from `org-email-link-description-format')."
  8698. (setq fmt (or fmt org-email-link-description-format))
  8699. (let* ((p org-store-link-plist)
  8700. (to (plist-get p :toaddress))
  8701. (from (plist-get p :fromaddress))
  8702. (table
  8703. (list
  8704. (cons "%c" (plist-get p :fromto))
  8705. (cons "%F" (plist-get p :from))
  8706. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  8707. (cons "%T" (plist-get p :to))
  8708. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  8709. (cons "%s" (plist-get p :subject))
  8710. (cons "%d" (plist-get p :date))
  8711. (cons "%m" (plist-get p :message-id)))))
  8712. (when (string-match "%c" fmt)
  8713. ;; Check if the user wrote this message
  8714. (if (and org-from-is-user-regexp from to
  8715. (save-match-data (string-match org-from-is-user-regexp from)))
  8716. (setq fmt (replace-match "to %t" t t fmt))
  8717. (setq fmt (replace-match "from %f" t t fmt))))
  8718. (org-replace-escapes fmt table)))
  8719. (defun org-make-org-heading-search-string (&optional string)
  8720. "Make search string for the current headline or STRING."
  8721. (let ((s (or string
  8722. (and (derived-mode-p 'org-mode)
  8723. (save-excursion
  8724. (org-back-to-heading t)
  8725. (org-element-property :raw-value (org-element-at-point))))))
  8726. (lines org-context-in-file-links))
  8727. (or string (setq s (concat "*" s))) ; Add * for headlines
  8728. (setq s (replace-regexp-in-string "\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" "" s))
  8729. (when (and string (integerp lines) (> lines 0))
  8730. (let ((slines (org-split-string s "\n")))
  8731. (when (< lines (length slines))
  8732. (setq s (mapconcat
  8733. 'identity
  8734. (reverse (nthcdr (- (length slines) lines)
  8735. (reverse slines))) "\n")))))
  8736. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  8737. (defun org-make-link-string (link &optional description)
  8738. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  8739. (unless (string-match "\\S-" link)
  8740. (error "Empty link"))
  8741. (when (and description
  8742. (stringp description)
  8743. (not (string-match "\\S-" description)))
  8744. (setq description nil))
  8745. (when (stringp description)
  8746. ;; Remove brackets from the description, they are fatal.
  8747. (while (string-match "\\[" description)
  8748. (setq description (replace-match "{" t t description)))
  8749. (while (string-match "\\]" description)
  8750. (setq description (replace-match "}" t t description))))
  8751. (when (equal link description)
  8752. ;; No description needed, it is identical
  8753. (setq description nil))
  8754. (when (and (not description)
  8755. (not (string-match (org-image-file-name-regexp) link))
  8756. (not (equal link (org-link-escape link))))
  8757. (setq description (org-extract-attributes link)))
  8758. (setq link
  8759. (cond ((string-match (org-image-file-name-regexp) link) link)
  8760. ((string-match org-link-types-re link)
  8761. (concat (match-string 1 link)
  8762. (org-link-escape (substring link (match-end 1)))))
  8763. (t (org-link-escape link))))
  8764. (concat "[[" link "]"
  8765. (if description (concat "[" description "]") "")
  8766. "]"))
  8767. (defconst org-link-escape-chars
  8768. '(?\ ?\[ ?\] ?\; ?\= ?\+)
  8769. "List of characters that should be escaped in link.
  8770. This is the list that is used for internal purposes.")
  8771. (defconst org-link-escape-chars-browser
  8772. '(?\ ?\")
  8773. "List of escapes for characters that are problematic in links.
  8774. This is the list that is used before handing over to the browser.")
  8775. (defun org-link-escape (text &optional table merge)
  8776. "Return percent escaped representation of TEXT.
  8777. TEXT is a string with the text to escape.
  8778. Optional argument TABLE is a list with characters that should be
  8779. escaped. When nil, `org-link-escape-chars' is used.
  8780. If optional argument MERGE is set, merge TABLE into
  8781. `org-link-escape-chars'."
  8782. (cond
  8783. ((and table merge)
  8784. (mapc (lambda (defchr)
  8785. (unless (member defchr table)
  8786. (setq table (cons defchr table)))) org-link-escape-chars))
  8787. ((null table)
  8788. (setq table org-link-escape-chars)))
  8789. (mapconcat
  8790. (lambda (char)
  8791. (if (or (member char table)
  8792. (and (or (< char 32) (= char 37) (> char 126))
  8793. org-url-hexify-p))
  8794. (mapconcat (lambda (sequence-element)
  8795. (format "%%%.2X" sequence-element))
  8796. (or (encode-coding-char char 'utf-8)
  8797. (error "Unable to percent escape character: %s"
  8798. (char-to-string char))) "")
  8799. (char-to-string char))) text ""))
  8800. (defun org-link-unescape (str)
  8801. "Unhex hexified Unicode strings as returned from the JavaScript function
  8802. encodeURIComponent. E.g. `%C3%B6' is the german o-Umlaut."
  8803. (unless (and (null str) (string= "" str))
  8804. (let ((pos 0) (case-fold-search t) unhexed)
  8805. (while (setq pos (string-match "\\(%[0-9a-f][0-9a-f]\\)+" str pos))
  8806. (setq unhexed (org-link-unescape-compound (match-string 0 str)))
  8807. (setq str (replace-match unhexed t t str))
  8808. (setq pos (+ pos (length unhexed))))))
  8809. str)
  8810. (defun org-link-unescape-compound (hex)
  8811. "Unhexify Unicode hex-chars. E.g. `%C3%B6' is the German o-Umlaut.
  8812. Note: this function also decodes single byte encodings like
  8813. `%E1' (a-acute) if not followed by another `%[A-F0-9]{2}' group."
  8814. (save-match-data
  8815. (let* ((bytes (cdr (split-string hex "%")))
  8816. (ret "")
  8817. (eat 0)
  8818. (sum 0))
  8819. (while bytes
  8820. (let* ((val (string-to-number (pop bytes) 16))
  8821. (shift-xor
  8822. (if (= 0 eat)
  8823. (cond
  8824. ((>= val 252) (cons 6 252))
  8825. ((>= val 248) (cons 5 248))
  8826. ((>= val 240) (cons 4 240))
  8827. ((>= val 224) (cons 3 224))
  8828. ((>= val 192) (cons 2 192))
  8829. (t (cons 0 0)))
  8830. (cons 6 128))))
  8831. (if (>= val 192) (setq eat (car shift-xor)))
  8832. (setq val (logxor val (cdr shift-xor)))
  8833. (setq sum (+ (lsh sum (car shift-xor)) val))
  8834. (if (> eat 0) (setq eat (- eat 1)))
  8835. (cond
  8836. ((= 0 eat) ;multi byte
  8837. (setq ret (concat ret (org-char-to-string sum)))
  8838. (setq sum 0))
  8839. ((not bytes) ; single byte(s)
  8840. (setq ret (org-link-unescape-single-byte-sequence hex))))
  8841. )) ;; end (while bytes
  8842. ret )))
  8843. (defun org-link-unescape-single-byte-sequence (hex)
  8844. "Unhexify hex-encoded single byte character sequences."
  8845. (mapconcat (lambda (byte)
  8846. (char-to-string (string-to-number byte 16)))
  8847. (cdr (split-string hex "%")) ""))
  8848. (defun org-xor (a b)
  8849. "Exclusive or."
  8850. (if a (not b) b))
  8851. (defun org-fixup-message-id-for-http (s)
  8852. "Replace special characters in a message id, so it can be used in an http query."
  8853. (when (string-match "%" s)
  8854. (setq s (mapconcat (lambda (c)
  8855. (if (eq c ?%)
  8856. "%25"
  8857. (char-to-string c)))
  8858. s "")))
  8859. (while (string-match "<" s)
  8860. (setq s (replace-match "%3C" t t s)))
  8861. (while (string-match ">" s)
  8862. (setq s (replace-match "%3E" t t s)))
  8863. (while (string-match "@" s)
  8864. (setq s (replace-match "%40" t t s)))
  8865. s)
  8866. (defun org-link-prettify (link)
  8867. "Return a human-readable representation of LINK.
  8868. The car of LINK must be a raw link the cdr of LINK must be either
  8869. a link description or nil."
  8870. (let ((desc (or (cadr link) "<no description>")))
  8871. (concat (format "%-45s" (substring desc 0 (min (length desc) 40)))
  8872. "<" (car link) ">")))
  8873. ;;;###autoload
  8874. (defun org-insert-link-global ()
  8875. "Insert a link like Org-mode does.
  8876. This command can be called in any mode to insert a link in Org-mode syntax."
  8877. (interactive)
  8878. (org-load-modules-maybe)
  8879. (org-run-like-in-org-mode 'org-insert-link))
  8880. (defun org-insert-all-links (&optional keep)
  8881. "Insert all links in `org-stored-links'."
  8882. (interactive "P")
  8883. (let ((links (copy-sequence org-stored-links)) l)
  8884. (while (setq l (if keep (pop links) (pop org-stored-links)))
  8885. (insert "- ")
  8886. (org-insert-link nil (car l) (or (cadr l) "<no description>"))
  8887. (insert "\n"))))
  8888. (defun org-link-fontify-links-to-this-file ()
  8889. "Fontify links to the current file in `org-stored-links'."
  8890. (let ((f (buffer-file-name)) a b)
  8891. (setq a (mapcar (lambda(l)
  8892. (let ((ll (car l)))
  8893. (when (and (string-match "^file:\\(.+\\)::" ll)
  8894. (equal f (expand-file-name (match-string 1 ll))))
  8895. ll)))
  8896. org-stored-links))
  8897. (when (featurep 'org-id)
  8898. (setq b (mapcar (lambda(l)
  8899. (let ((ll (car l)))
  8900. (when (and (string-match "^id:\\(.+\\)$" ll)
  8901. (equal f (expand-file-name
  8902. (or (org-id-find-id-file
  8903. (match-string 1 ll)) ""))))
  8904. ll)))
  8905. org-stored-links)))
  8906. (mapcar (lambda(l)
  8907. (put-text-property 0 (length l) 'face 'font-lock-comment-face l))
  8908. (delq nil (append a b)))))
  8909. (defvar org-link-links-in-this-file nil)
  8910. (defun org-insert-link (&optional complete-file link-location default-description)
  8911. "Insert a link. At the prompt, enter the link.
  8912. Completion can be used to insert any of the link protocol prefixes like
  8913. http or ftp in use.
  8914. The history can be used to select a link previously stored with
  8915. `org-store-link'. When the empty string is entered (i.e. if you just
  8916. press RET at the prompt), the link defaults to the most recently
  8917. stored link. As SPC triggers completion in the minibuffer, you need to
  8918. use M-SPC or C-q SPC to force the insertion of a space character.
  8919. You will also be prompted for a description, and if one is given, it will
  8920. be displayed in the buffer instead of the link.
  8921. If there is already a link at point, this command will allow you to edit link
  8922. and description parts.
  8923. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  8924. be selected using completion. The path to the file will be relative to the
  8925. current directory if the file is in the current directory or a subdirectory.
  8926. Otherwise, the link will be the absolute path as completed in the minibuffer
  8927. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  8928. option `org-link-file-path-type'.
  8929. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  8930. the current directory or below.
  8931. With three \\[universal-argument] prefixes, negate the meaning of
  8932. `org-keep-stored-link-after-insertion'.
  8933. If `org-make-link-description-function' is non-nil, this function will be
  8934. called with the link target, and the result will be the default
  8935. link description.
  8936. If the LINK-LOCATION parameter is non-nil, this value will be
  8937. used as the link location instead of reading one interactively.
  8938. If the DEFAULT-DESCRIPTION parameter is non-nil, this value will
  8939. be used as the default description."
  8940. (interactive "P")
  8941. (let* ((wcf (current-window-configuration))
  8942. (origbuf (current-buffer))
  8943. (region (if (org-region-active-p)
  8944. (buffer-substring (region-beginning) (region-end))))
  8945. (remove (and region (list (region-beginning) (region-end))))
  8946. (desc region)
  8947. tmphist ; byte-compile incorrectly complains about this
  8948. (link link-location)
  8949. (abbrevs org-link-abbrev-alist-local)
  8950. entry file all-prefixes auto-desc)
  8951. (cond
  8952. (link-location) ; specified by arg, just use it.
  8953. ((org-in-regexp org-bracket-link-regexp 1)
  8954. ;; We do have a link at point, and we are going to edit it.
  8955. (setq remove (list (match-beginning 0) (match-end 0)))
  8956. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  8957. (setq link (read-string "Link: "
  8958. (org-link-unescape
  8959. (org-match-string-no-properties 1)))))
  8960. ((or (org-in-regexp org-angle-link-re)
  8961. (org-in-regexp org-plain-link-re))
  8962. ;; Convert to bracket link
  8963. (setq remove (list (match-beginning 0) (match-end 0))
  8964. link (read-string "Link: "
  8965. (org-remove-angle-brackets (match-string 0)))))
  8966. ((member complete-file '((4) (16)))
  8967. ;; Completing read for file names.
  8968. (setq link (org-file-complete-link complete-file)))
  8969. (t
  8970. ;; Read link, with completion for stored links.
  8971. (org-link-fontify-links-to-this-file)
  8972. (org-switch-to-buffer-other-window "*Org Links*")
  8973. (with-current-buffer "*Org Links*"
  8974. (erase-buffer)
  8975. (insert "Insert a link.
  8976. Use TAB to complete link prefixes, then RET for type-specific completion support\n")
  8977. (when org-stored-links
  8978. (insert "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  8979. (insert (mapconcat 'org-link-prettify
  8980. (reverse org-stored-links) "\n")))
  8981. (goto-char (point-min)))
  8982. (let ((cw (selected-window)))
  8983. (select-window (get-buffer-window "*Org Links*" 'visible))
  8984. (with-current-buffer "*Org Links*" (setq truncate-lines t))
  8985. (unless (pos-visible-in-window-p (point-max))
  8986. (org-fit-window-to-buffer))
  8987. (and (window-live-p cw) (select-window cw)))
  8988. ;; Fake a link history, containing the stored links.
  8989. (setq tmphist (append (mapcar 'car org-stored-links)
  8990. org-insert-link-history))
  8991. (setq all-prefixes (append (mapcar 'car abbrevs)
  8992. (mapcar 'car org-link-abbrev-alist)
  8993. org-link-types))
  8994. (unwind-protect
  8995. (progn
  8996. (setq link
  8997. (org-completing-read
  8998. "Link: "
  8999. (append
  9000. (mapcar (lambda (x) (concat x ":"))
  9001. all-prefixes)
  9002. (mapcar 'car org-stored-links))
  9003. nil nil nil
  9004. 'tmphist
  9005. (caar org-stored-links)))
  9006. (if (not (string-match "\\S-" link))
  9007. (user-error "No link selected"))
  9008. (mapc (lambda(l)
  9009. (when (equal link (cadr l)) (setq link (car l) auto-desc t)))
  9010. org-stored-links)
  9011. (if (or (member link all-prefixes)
  9012. (and (equal ":" (substring link -1))
  9013. (member (substring link 0 -1) all-prefixes)
  9014. (setq link (substring link 0 -1))))
  9015. (setq link (with-current-buffer origbuf
  9016. (org-link-try-special-completion link)))))
  9017. (set-window-configuration wcf)
  9018. (kill-buffer "*Org Links*"))
  9019. (setq entry (assoc link org-stored-links))
  9020. (or entry (push link org-insert-link-history))
  9021. (setq desc (or desc (nth 1 entry)))))
  9022. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  9023. (not org-keep-stored-link-after-insertion))
  9024. (setq org-stored-links (delq (assoc link org-stored-links)
  9025. org-stored-links)))
  9026. (if (string-match org-plain-link-re link)
  9027. ;; URL-like link, normalize the use of angular brackets.
  9028. (setq link (org-remove-angle-brackets link)))
  9029. ;; Check if we are linking to the current file with a search
  9030. ;; option If yes, simplify the link by using only the search
  9031. ;; option.
  9032. (when (and buffer-file-name
  9033. (string-match "^file:\\(.+?\\)::\\(.+\\)" link))
  9034. (let* ((path (match-string 1 link))
  9035. (case-fold-search nil)
  9036. (search (match-string 2 link)))
  9037. (save-match-data
  9038. (if (equal (file-truename buffer-file-name) (file-truename path))
  9039. ;; We are linking to this same file, with a search option
  9040. (setq link search)))))
  9041. ;; Check if we can/should use a relative path. If yes, simplify the link
  9042. (when (string-match "^\\(file:\\|docview:\\)\\(.*\\)" link)
  9043. (let* ((type (match-string 1 link))
  9044. (path (match-string 2 link))
  9045. (origpath path)
  9046. (case-fold-search nil))
  9047. (cond
  9048. ((or (eq org-link-file-path-type 'absolute)
  9049. (equal complete-file '(16)))
  9050. (setq path (abbreviate-file-name (expand-file-name path))))
  9051. ((eq org-link-file-path-type 'noabbrev)
  9052. (setq path (expand-file-name path)))
  9053. ((eq org-link-file-path-type 'relative)
  9054. (setq path (file-relative-name path)))
  9055. (t
  9056. (save-match-data
  9057. (if (string-match (concat "^" (regexp-quote
  9058. (expand-file-name
  9059. (file-name-as-directory
  9060. default-directory))))
  9061. (expand-file-name path))
  9062. ;; We are linking a file with relative path name.
  9063. (setq path (substring (expand-file-name path)
  9064. (match-end 0)))
  9065. (setq path (abbreviate-file-name (expand-file-name path)))))))
  9066. (setq link (concat type path))
  9067. (if (equal desc origpath)
  9068. (setq desc path))))
  9069. (if org-make-link-description-function
  9070. (setq desc
  9071. (or (condition-case nil
  9072. (funcall org-make-link-description-function link desc)
  9073. (error (progn (message "Can't get link description from `%s'"
  9074. (symbol-name org-make-link-description-function))
  9075. (sit-for 2) nil)))
  9076. (read-string "Description: " default-description)))
  9077. (if default-description (setq desc default-description)
  9078. (setq desc (or (and auto-desc desc)
  9079. (read-string "Description: " desc)))))
  9080. (unless (string-match "\\S-" desc) (setq desc nil))
  9081. (if remove (apply 'delete-region remove))
  9082. (insert (org-make-link-string link desc))))
  9083. (defun org-link-try-special-completion (type)
  9084. "If there is completion support for link type TYPE, offer it."
  9085. (let ((fun (intern (concat "org-" type "-complete-link"))))
  9086. (if (functionp fun)
  9087. (funcall fun)
  9088. (read-string "Link (no completion support): " (concat type ":")))))
  9089. (defun org-file-complete-link (&optional arg)
  9090. "Create a file link using completion."
  9091. (let (file link)
  9092. (setq file (org-iread-file-name "File: "))
  9093. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  9094. (pwd1 (file-name-as-directory (abbreviate-file-name
  9095. (expand-file-name ".")))))
  9096. (cond
  9097. ((equal arg '(16))
  9098. (setq link (concat
  9099. "file:"
  9100. (abbreviate-file-name (expand-file-name file)))))
  9101. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  9102. (setq link (concat "file:" (match-string 1 file))))
  9103. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  9104. (expand-file-name file))
  9105. (setq link (concat
  9106. "file:" (match-string 1 (expand-file-name file)))))
  9107. (t (setq link (concat "file:" file)))))
  9108. link))
  9109. (defun org-iread-file-name (&rest args)
  9110. "Read-file-name using `ido-mode' speedup if available.
  9111. ARGS are arguments that may be passed to `ido-read-file-name' or `read-file-name'.
  9112. See `read-file-name' for a description of parameters."
  9113. (org-without-partial-completion
  9114. (if (and org-completion-use-ido
  9115. (fboundp 'ido-read-file-name)
  9116. (boundp 'ido-mode) ido-mode
  9117. (listp (second args)))
  9118. (let ((ido-enter-matching-directory nil))
  9119. (apply 'ido-read-file-name args))
  9120. (apply 'read-file-name args))))
  9121. (defun org-completing-read (&rest args)
  9122. "Completing-read with SPACE being a normal character."
  9123. (let ((enable-recursive-minibuffers t)
  9124. (minibuffer-local-completion-map
  9125. (copy-keymap minibuffer-local-completion-map)))
  9126. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  9127. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  9128. (org-defkey minibuffer-local-completion-map (kbd "C-c !") 'org-time-stamp-inactive)
  9129. (apply 'org-icompleting-read args)))
  9130. (defun org-completing-read-no-i (&rest args)
  9131. (let (org-completion-use-ido org-completion-use-iswitchb)
  9132. (apply 'org-completing-read args)))
  9133. (defun org-iswitchb-completing-read (prompt choices &rest args)
  9134. "Use iswitch as a completing-read replacement to choose from choices.
  9135. PROMPT is a string to prompt with. CHOICES is a list of strings to choose
  9136. from."
  9137. (let* ((iswitchb-use-virtual-buffers nil)
  9138. (iswitchb-make-buflist-hook
  9139. (lambda ()
  9140. (setq iswitchb-temp-buflist choices))))
  9141. (iswitchb-read-buffer prompt)))
  9142. (defun org-icompleting-read (&rest args)
  9143. "Completing-read using `ido-mode' or `iswitchb' speedups if available."
  9144. (org-without-partial-completion
  9145. (if (and org-completion-use-ido
  9146. (fboundp 'ido-completing-read)
  9147. (boundp 'ido-mode) ido-mode
  9148. (listp (second args)))
  9149. (let ((ido-enter-matching-directory nil))
  9150. (apply 'ido-completing-read (concat (car args))
  9151. (if (consp (car (nth 1 args)))
  9152. (mapcar 'car (nth 1 args))
  9153. (nth 1 args))
  9154. (cddr args)))
  9155. (if (and org-completion-use-iswitchb
  9156. (boundp 'iswitchb-mode) iswitchb-mode
  9157. (listp (second args)))
  9158. (apply 'org-iswitchb-completing-read (concat (car args))
  9159. (if (consp (car (nth 1 args)))
  9160. (mapcar 'car (nth 1 args))
  9161. (nth 1 args))
  9162. (cddr args))
  9163. (apply 'completing-read args)))))
  9164. (defun org-extract-attributes (s)
  9165. "Extract the attributes cookie from a string and set as text property."
  9166. (let (a attr (start 0) key value)
  9167. (save-match-data
  9168. (when (string-match "{{\\([^}]+\\)}}$" s)
  9169. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  9170. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  9171. (setq key (match-string 1 a) value (match-string 2 a)
  9172. start (match-end 0)
  9173. attr (plist-put attr (intern key) value))))
  9174. (org-add-props s nil 'org-attr attr))
  9175. s))
  9176. ;;; Opening/following a link
  9177. (defvar org-link-search-failed nil)
  9178. (defvar org-open-link-functions nil
  9179. "Hook for functions finding a plain text link.
  9180. These functions must take a single argument, the link content.
  9181. They will be called for links that look like [[link text][description]]
  9182. when LINK TEXT does not have a protocol like \"http:\" and does not look
  9183. like a filename (e.g. \"./blue.png\").
  9184. These functions will be called *before* Org attempts to resolve the
  9185. link by doing text searches in the current buffer - so if you want a
  9186. link \"[[target]]\" to still find \"<<target>>\", your function should
  9187. handle this as a special case.
  9188. When the function does handle the link, it must return a non-nil value.
  9189. If it decides that it is not responsible for this link, it must return
  9190. nil to indicate that that Org-mode can continue with other options
  9191. like exact and fuzzy text search.")
  9192. (defun org-next-link (&optional search-backward)
  9193. "Move forward to the next link.
  9194. If the link is in hidden text, expose it."
  9195. (interactive "P")
  9196. (when (and org-link-search-failed (eq this-command last-command))
  9197. (goto-char (point-min))
  9198. (message "Link search wrapped back to beginning of buffer"))
  9199. (setq org-link-search-failed nil)
  9200. (let* ((pos (point))
  9201. (ct (org-context))
  9202. (a (assoc :link ct))
  9203. (srch-fun (if search-backward 're-search-backward 're-search-forward)))
  9204. (cond (a (goto-char (nth (if search-backward 1 2) a)))
  9205. ((looking-at org-any-link-re)
  9206. ;; Don't stay stuck at link without an org-link face
  9207. (forward-char (if search-backward -1 1))))
  9208. (if (funcall srch-fun org-any-link-re nil t)
  9209. (progn
  9210. (goto-char (match-beginning 0))
  9211. (if (outline-invisible-p) (org-show-context)))
  9212. (goto-char pos)
  9213. (setq org-link-search-failed t)
  9214. (message "No further link found"))))
  9215. (defun org-previous-link ()
  9216. "Move backward to the previous link.
  9217. If the link is in hidden text, expose it."
  9218. (interactive)
  9219. (funcall 'org-next-link t))
  9220. (defun org-translate-link (s)
  9221. "Translate a link string if a translation function has been defined."
  9222. (if (and org-link-translation-function
  9223. (fboundp org-link-translation-function)
  9224. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  9225. (progn
  9226. (setq s (funcall org-link-translation-function
  9227. (match-string 1 s) (match-string 2 s)))
  9228. (concat (car s) ":" (cdr s)))
  9229. s))
  9230. (defun org-translate-link-from-planner (type path)
  9231. "Translate a link from Emacs Planner syntax so that Org can follow it.
  9232. This is still an experimental function, your mileage may vary."
  9233. (cond
  9234. ((member type '("http" "https" "news" "ftp"))
  9235. ;; standard Internet links are the same.
  9236. nil)
  9237. ((and (equal type "irc") (string-match "^//" path))
  9238. ;; Planner has two / at the beginning of an irc link, we have 1.
  9239. ;; We should have zero, actually....
  9240. (setq path (substring path 1)))
  9241. ((and (equal type "lisp") (string-match "^/" path))
  9242. ;; Planner has a slash, we do not.
  9243. (setq type "elisp" path (substring path 1)))
  9244. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  9245. ;; A typical message link. Planner has the id after the final slash,
  9246. ;; we separate it with a hash mark
  9247. (setq path (concat (match-string 1 path) "#"
  9248. (org-remove-angle-brackets (match-string 2 path))))))
  9249. (cons type path))
  9250. (defun org-find-file-at-mouse (ev)
  9251. "Open file link or URL at mouse."
  9252. (interactive "e")
  9253. (mouse-set-point ev)
  9254. (org-open-at-point 'in-emacs))
  9255. (defun org-open-at-mouse (ev)
  9256. "Open file link or URL at mouse.
  9257. See the docstring of `org-open-file' for details."
  9258. (interactive "e")
  9259. (mouse-set-point ev)
  9260. (if (eq major-mode 'org-agenda-mode)
  9261. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  9262. (org-open-at-point))
  9263. (defvar org-window-config-before-follow-link nil
  9264. "The window configuration before following a link.
  9265. This is saved in case the need arises to restore it.")
  9266. (defvar org-open-link-marker (make-marker)
  9267. "Marker pointing to the location where `org-open-at-point; was called.")
  9268. ;;;###autoload
  9269. (defun org-open-at-point-global ()
  9270. "Follow a link like Org-mode does.
  9271. This command can be called in any mode to follow a link that has
  9272. Org-mode syntax."
  9273. (interactive)
  9274. (org-run-like-in-org-mode 'org-open-at-point))
  9275. ;;;###autoload
  9276. (defun org-open-link-from-string (s &optional arg reference-buffer)
  9277. "Open a link in the string S, as if it was in Org-mode."
  9278. (interactive "sLink: \nP")
  9279. (let ((reference-buffer (or reference-buffer (current-buffer))))
  9280. (with-temp-buffer
  9281. (let ((org-inhibit-startup (not reference-buffer)))
  9282. (org-mode)
  9283. (insert s)
  9284. (goto-char (point-min))
  9285. (when reference-buffer
  9286. (setq org-link-abbrev-alist-local
  9287. (with-current-buffer reference-buffer
  9288. org-link-abbrev-alist-local)))
  9289. (org-open-at-point arg reference-buffer)))))
  9290. (defvar org-open-at-point-functions nil
  9291. "Hook that is run when following a link at point.
  9292. Functions in this hook must return t if they identify and follow
  9293. a link at point. If they don't find anything interesting at point,
  9294. they must return nil.")
  9295. (defvar clean-buffer-list-kill-buffer-names) ; Defined in midnight.el
  9296. (defun org-open-at-point (&optional arg reference-buffer)
  9297. "Open link at or after point.
  9298. If there is no link at point, this function will search forward up to
  9299. the end of the current line.
  9300. Normally, files will be opened by an appropriate application. If the
  9301. optional prefix argument ARG is non-nil, Emacs will visit the file.
  9302. With a double prefix argument, try to open outside of Emacs, in the
  9303. application the system uses for this file type."
  9304. (interactive "P")
  9305. ;; if in a code block, then open the block's results
  9306. (unless (call-interactively #'org-babel-open-src-block-result)
  9307. (org-load-modules-maybe)
  9308. (move-marker org-open-link-marker (point))
  9309. (setq org-window-config-before-follow-link (current-window-configuration))
  9310. (org-remove-occur-highlights nil nil t)
  9311. (cond
  9312. ((and (org-at-heading-p)
  9313. (not (org-at-timestamp-p t))
  9314. (not (org-in-regexp
  9315. (concat org-plain-link-re "\\|"
  9316. org-bracket-link-regexp "\\|"
  9317. org-angle-link-re "\\|"
  9318. "[ \t]:[^ \t\n]+:[ \t]*$")))
  9319. (not (get-text-property (point) 'org-linked-text)))
  9320. (or (let* ((lkall (org-offer-links-in-entry (current-buffer) (point) arg))
  9321. (lk0 (car lkall))
  9322. (lk (if (stringp lk0) (list lk0) lk0))
  9323. (lkend (cdr lkall)))
  9324. (mapcar (lambda(l)
  9325. (search-forward l nil lkend)
  9326. (goto-char (match-beginning 0))
  9327. (org-open-at-point))
  9328. lk))
  9329. (progn (require 'org-attach) (org-attach-reveal 'if-exists))))
  9330. ((run-hook-with-args-until-success 'org-open-at-point-functions))
  9331. ((and (org-at-timestamp-p t)
  9332. (not (org-in-regexp org-bracket-link-regexp)))
  9333. (org-follow-timestamp-link))
  9334. ((and (or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  9335. (not (org-in-regexp org-any-link-re)))
  9336. (org-footnote-action))
  9337. (t
  9338. (let (type path link line search (pos (point)))
  9339. (catch 'match
  9340. (save-excursion
  9341. (skip-chars-forward "^]\n\r")
  9342. (when (org-in-regexp org-bracket-link-regexp 1)
  9343. (setq link (org-extract-attributes
  9344. (org-link-unescape (org-match-string-no-properties 1))))
  9345. (while (string-match " *\n *" link)
  9346. (setq link (replace-match " " t t link)))
  9347. (setq link (org-link-expand-abbrev link))
  9348. (cond
  9349. ((or (file-name-absolute-p link)
  9350. (string-match "^\\.\\.?/" link))
  9351. (setq type "file" path link))
  9352. ((string-match org-link-re-with-space3 link)
  9353. (setq type (match-string 1 link) path (match-string 2 link)))
  9354. ((string-match "^help:+\\(.+\\)" link)
  9355. (setq type "help" path (match-string 1 link)))
  9356. (t (setq type "thisfile" path link)))
  9357. (throw 'match t)))
  9358. (when (get-text-property (point) 'org-linked-text)
  9359. (setq type "thisfile"
  9360. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  9361. (1+ (point)) (point))
  9362. path (buffer-substring
  9363. (or (previous-single-property-change pos 'org-linked-text)
  9364. (point-min))
  9365. (or (next-single-property-change pos 'org-linked-text)
  9366. (point-max)))
  9367. ;; Ensure we will search for a <<<radio>>> link, not
  9368. ;; a simple reference like <<ref>>
  9369. path (concat "<" path))
  9370. (throw 'match t))
  9371. (save-excursion
  9372. (when (or (org-in-regexp org-angle-link-re)
  9373. (let ((match (org-in-regexp org-plain-link-re)))
  9374. ;; Check a plain link is not within a bracket link
  9375. (and match
  9376. (save-excursion
  9377. (progn
  9378. (goto-char (car match))
  9379. (not (org-in-regexp org-bracket-link-regexp))))))
  9380. (let ((line_ending (save-excursion (end-of-line) (point))))
  9381. ;; We are in a line before a plain or bracket link
  9382. (or (re-search-forward org-plain-link-re line_ending t)
  9383. (re-search-forward org-bracket-link-regexp line_ending t))))
  9384. (setq type (match-string 1)
  9385. path (org-link-unescape (match-string 2)))
  9386. (throw 'match t)))
  9387. (save-excursion
  9388. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@#%:]+\\):[ \t]*$"))
  9389. (setq type "tags"
  9390. path (match-string 1))
  9391. (while (string-match ":" path)
  9392. (setq path (replace-match "+" t t path)))
  9393. (throw 'match t)))
  9394. (when (org-in-regexp "<\\([^><\n]+\\)>")
  9395. (setq type "tree-match"
  9396. path (match-string 1))
  9397. (throw 'match t)))
  9398. (unless path
  9399. (user-error "No link found"))
  9400. ;; switch back to reference buffer
  9401. ;; needed when if called in a temporary buffer through
  9402. ;; org-open-link-from-string
  9403. (with-current-buffer (or reference-buffer (current-buffer))
  9404. ;; Remove any trailing spaces in path
  9405. (if (string-match " +\\'" path)
  9406. (setq path (replace-match "" t t path)))
  9407. (if (and org-link-translation-function
  9408. (fboundp org-link-translation-function))
  9409. ;; Check if we need to translate the link
  9410. (let ((tmp (funcall org-link-translation-function type path)))
  9411. (setq type (car tmp) path (cdr tmp))))
  9412. (cond
  9413. ((assoc type org-link-protocols)
  9414. (funcall (nth 1 (assoc type org-link-protocols)) path))
  9415. ((equal type "help")
  9416. (let ((f-or-v (intern path)))
  9417. (cond ((fboundp f-or-v)
  9418. (describe-function f-or-v))
  9419. ((boundp f-or-v)
  9420. (describe-variable f-or-v))
  9421. (t (error "Not a known function or variable")))))
  9422. ((equal type "mailto")
  9423. (let ((cmd (car org-link-mailto-program))
  9424. (args (cdr org-link-mailto-program)) args1
  9425. (address path) (subject "") a)
  9426. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  9427. (setq address (match-string 1 path)
  9428. subject (org-link-escape (match-string 2 path))))
  9429. (while args
  9430. (cond
  9431. ((not (stringp (car args))) (push (pop args) args1))
  9432. (t (setq a (pop args))
  9433. (if (string-match "%a" a)
  9434. (setq a (replace-match address t t a)))
  9435. (if (string-match "%s" a)
  9436. (setq a (replace-match subject t t a)))
  9437. (push a args1))))
  9438. (apply cmd (nreverse args1))))
  9439. ((member type '("http" "https" "ftp" "news"))
  9440. (browse-url
  9441. (concat type ":"
  9442. (if (org-string-match-p
  9443. (concat "[[:nonascii:]"
  9444. org-link-escape-chars-browser "]")
  9445. path)
  9446. (org-link-escape path org-link-escape-chars-browser)
  9447. path))))
  9448. ((string= type "doi")
  9449. (browse-url
  9450. (concat org-doi-server-url
  9451. (if (org-string-match-p
  9452. (concat "[[:nonascii:]"
  9453. org-link-escape-chars-browser "]")
  9454. path)
  9455. (org-link-escape path org-link-escape-chars-browser)
  9456. path))))
  9457. ((member type '("message"))
  9458. (browse-url (concat type ":" path)))
  9459. ((string= type "tags")
  9460. (org-tags-view arg path))
  9461. ((string= type "tree-match")
  9462. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  9463. ((string= type "file")
  9464. (if (string-match "::\\([0-9]+\\)\\'" path)
  9465. (setq line (string-to-number (match-string 1 path))
  9466. path (substring path 0 (match-beginning 0)))
  9467. (if (string-match "::\\(.+\\)\\'" path)
  9468. (setq search (match-string 1 path)
  9469. path (substring path 0 (match-beginning 0)))))
  9470. (if (string-match "[*?{]" (file-name-nondirectory path))
  9471. (dired path)
  9472. (org-open-file path arg line search)))
  9473. ((string= type "shell")
  9474. (let ((buf (generate-new-buffer "*Org Shell Output"))
  9475. (cmd path))
  9476. (if (or (and (not (string= org-confirm-shell-link-not-regexp ""))
  9477. (string-match org-confirm-shell-link-not-regexp cmd))
  9478. (not org-confirm-shell-link-function)
  9479. (funcall org-confirm-shell-link-function
  9480. (format "Execute \"%s\" in shell? "
  9481. (org-add-props cmd nil
  9482. 'face 'org-warning))))
  9483. (progn
  9484. (message "Executing %s" cmd)
  9485. (shell-command cmd buf)
  9486. (if (featurep 'midnight)
  9487. (setq clean-buffer-list-kill-buffer-names
  9488. (cons buf clean-buffer-list-kill-buffer-names))))
  9489. (error "Abort"))))
  9490. ((string= type "elisp")
  9491. (let ((cmd path))
  9492. (if (or (and (not (string= org-confirm-elisp-link-not-regexp ""))
  9493. (string-match org-confirm-elisp-link-not-regexp cmd))
  9494. (not org-confirm-elisp-link-function)
  9495. (funcall org-confirm-elisp-link-function
  9496. (format "Execute \"%s\" as elisp? "
  9497. (org-add-props cmd nil
  9498. 'face 'org-warning))))
  9499. (message "%s => %s" cmd
  9500. (if (equal (string-to-char cmd) ?\()
  9501. (eval (read cmd))
  9502. (call-interactively (read cmd))))
  9503. (error "Abort"))))
  9504. ((and (string= type "thisfile")
  9505. (run-hook-with-args-until-success
  9506. 'org-open-link-functions path)))
  9507. ((string= type "thisfile")
  9508. (if arg
  9509. (switch-to-buffer-other-window
  9510. (org-get-buffer-for-internal-link (current-buffer)))
  9511. (org-mark-ring-push))
  9512. (let ((cmd `(org-link-search
  9513. ,path
  9514. ,(cond ((equal arg '(4)) ''occur)
  9515. ((equal arg '(16)) ''org-occur))
  9516. ,pos)))
  9517. (condition-case nil (let ((org-link-search-inhibit-query t))
  9518. (eval cmd))
  9519. (error (progn (widen) (eval cmd))))))
  9520. (t (browse-url-at-point)))))))
  9521. (move-marker org-open-link-marker nil)
  9522. (run-hook-with-args 'org-follow-link-hook)))
  9523. (defun org-offer-links-in-entry (buffer marker &optional nth zero)
  9524. "Offer links in the current entry and return the selected link.
  9525. If there is only one link, return it.
  9526. If NTH is an integer, return the NTH link found.
  9527. If ZERO is a string, check also this string for a link, and if
  9528. there is one, return it."
  9529. (with-current-buffer buffer
  9530. (save-excursion
  9531. (save-restriction
  9532. (widen)
  9533. (goto-char marker)
  9534. (let ((re (concat "\\(" org-bracket-link-regexp "\\)\\|"
  9535. "\\(" org-angle-link-re "\\)\\|"
  9536. "\\(" org-plain-link-re "\\)"))
  9537. (cnt ?0)
  9538. (in-emacs (if (integerp nth) nil nth))
  9539. have-zero end links link c)
  9540. (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
  9541. (push (match-string 0 zero) links)
  9542. (setq cnt (1- cnt) have-zero t))
  9543. (save-excursion
  9544. (org-back-to-heading t)
  9545. (setq end (save-excursion (outline-next-heading) (point)))
  9546. (while (re-search-forward re end t)
  9547. (push (match-string 0) links))
  9548. (setq links (org-uniquify (reverse links))))
  9549. (cond
  9550. ((null links)
  9551. (message "No links"))
  9552. ((equal (length links) 1)
  9553. (setq link (car links)))
  9554. ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
  9555. (setq link (nth (if have-zero nth (1- nth)) links)))
  9556. (t ; we have to select a link
  9557. (save-excursion
  9558. (save-window-excursion
  9559. (delete-other-windows)
  9560. (with-output-to-temp-buffer "*Select Link*"
  9561. (mapc (lambda (l)
  9562. (if (not (string-match org-bracket-link-regexp l))
  9563. (princ (format "[%c] %s\n" (incf cnt)
  9564. (org-remove-angle-brackets l)))
  9565. (if (match-end 3)
  9566. (princ (format "[%c] %s (%s)\n" (incf cnt)
  9567. (match-string 3 l) (match-string 1 l)))
  9568. (princ (format "[%c] %s\n" (incf cnt)
  9569. (match-string 1 l))))))
  9570. links))
  9571. (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
  9572. (message "Select link to open, RET to open all:")
  9573. (setq c (read-char-exclusive))
  9574. (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
  9575. (when (equal c ?q) (error "Abort"))
  9576. (if (equal c ?\C-m)
  9577. (setq link links)
  9578. (setq nth (- c ?0))
  9579. (if have-zero (setq nth (1+ nth)))
  9580. (unless (and (integerp nth) (>= (length links) nth))
  9581. (user-error "Invalid link selection"))
  9582. (setq link (nth (1- nth) links)))))
  9583. (cons link end))))))
  9584. ;; Add special file links that specify the way of opening
  9585. (org-add-link-type "file+sys" 'org-open-file-with-system)
  9586. (org-add-link-type "file+emacs" 'org-open-file-with-emacs)
  9587. (defun org-open-file-with-system (path)
  9588. "Open file at PATH using the system way of opening it."
  9589. (org-open-file path 'system))
  9590. (defun org-open-file-with-emacs (path)
  9591. "Open file at PATH in Emacs."
  9592. (org-open-file path 'emacs))
  9593. ;;; File search
  9594. (defvar org-create-file-search-functions nil
  9595. "List of functions to construct the right search string for a file link.
  9596. These functions are called in turn with point at the location to
  9597. which the link should point.
  9598. A function in the hook should first test if it would like to
  9599. handle this file type, for example by checking the `major-mode'
  9600. or the file extension. If it decides not to handle this file, it
  9601. should just return nil to give other functions a chance. If it
  9602. does handle the file, it must return the search string to be used
  9603. when following the link. The search string will be part of the
  9604. file link, given after a double colon, and `org-open-at-point'
  9605. will automatically search for it. If special measures must be
  9606. taken to make the search successful, another function should be
  9607. added to the companion hook `org-execute-file-search-functions',
  9608. which see.
  9609. A function in this hook may also use `setq' to set the variable
  9610. `description' to provide a suggestion for the descriptive text to
  9611. be used for this link when it gets inserted into an Org-mode
  9612. buffer with \\[org-insert-link].")
  9613. (defvar org-execute-file-search-functions nil
  9614. "List of functions to execute a file search triggered by a link.
  9615. Functions added to this hook must accept a single argument, the
  9616. search string that was part of the file link, the part after the
  9617. double colon. The function must first check if it would like to
  9618. handle this search, for example by checking the `major-mode' or
  9619. the file extension. If it decides not to handle this search, it
  9620. should just return nil to give other functions a chance. If it
  9621. does handle the search, it must return a non-nil value to keep
  9622. other functions from trying.
  9623. Each function can access the current prefix argument through the
  9624. variable `current-prefix-arg'. Note that a single prefix is used
  9625. to force opening a link in Emacs, so it may be good to only use a
  9626. numeric or double prefix to guide the search function.
  9627. In case this is needed, a function in this hook can also restore
  9628. the window configuration before `org-open-at-point' was called using:
  9629. (set-window-configuration org-window-config-before-follow-link)")
  9630. (defvar org-link-search-inhibit-query nil) ;; dynamically scoped
  9631. (defun org-link-search (s &optional type avoid-pos stealth)
  9632. "Search for a link search option.
  9633. If S is surrounded by forward slashes, it is interpreted as a
  9634. regular expression. In org-mode files, this will create an `org-occur'
  9635. sparse tree. In ordinary files, `occur' will be used to list matches.
  9636. If the current buffer is in `dired-mode', grep will be used to search
  9637. in all files. If AVOID-POS is given, ignore matches near that position.
  9638. When optional argument STEALTH is non-nil, do not modify
  9639. visibility around point, thus ignoring
  9640. `org-show-hierarchy-above', `org-show-following-heading' and
  9641. `org-show-siblings' variables."
  9642. (let ((case-fold-search t)
  9643. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  9644. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  9645. (append '(("") (" ") ("\t") ("\n"))
  9646. org-emphasis-alist)
  9647. "\\|") "\\)"))
  9648. (pos (point))
  9649. (pre nil) (post nil)
  9650. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  9651. (cond
  9652. ;; First check if there are any special search functions
  9653. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  9654. ;; Now try the builtin stuff
  9655. ((and (equal (string-to-char s0) ?#)
  9656. (> (length s0) 1)
  9657. (save-excursion
  9658. (goto-char (point-min))
  9659. (and
  9660. (re-search-forward
  9661. (concat "^[ \t]*:CUSTOM_ID:[ \t]+"
  9662. (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
  9663. (setq type 'dedicated
  9664. pos (match-beginning 0))))
  9665. ;; There is an exact target for this
  9666. (goto-char pos)
  9667. (org-back-to-heading t)))
  9668. ((save-excursion
  9669. (goto-char (point-min))
  9670. (and
  9671. (re-search-forward
  9672. (concat "<<" (regexp-quote s0) ">>") nil t)
  9673. (setq type 'dedicated
  9674. pos (match-beginning 0))))
  9675. ;; There is an exact target for this
  9676. (goto-char pos))
  9677. ((save-excursion
  9678. (goto-char (point-min))
  9679. (and
  9680. (re-search-forward
  9681. (format "^[ \t]*#\\+NAME: %s" (regexp-quote s0)) nil t)
  9682. (setq type 'dedicated pos (match-beginning 0))))
  9683. ;; Found an element with a matching #+name affiliated keyword.
  9684. (goto-char pos))
  9685. ((and (string-match "^(\\(.*\\))$" s0)
  9686. (save-excursion
  9687. (goto-char (point-min))
  9688. (and
  9689. (re-search-forward
  9690. (concat "[^[]" (regexp-quote
  9691. (format org-coderef-label-format
  9692. (match-string 1 s0))))
  9693. nil t)
  9694. (setq type 'dedicated
  9695. pos (1+ (match-beginning 0))))))
  9696. ;; There is a coderef target for this
  9697. (goto-char pos))
  9698. ((string-match "^/\\(.*\\)/$" s)
  9699. ;; A regular expression
  9700. (cond
  9701. ((derived-mode-p 'org-mode)
  9702. (org-occur (match-string 1 s)))
  9703. (t (org-do-occur (match-string 1 s)))))
  9704. ((and (derived-mode-p 'org-mode) org-link-search-must-match-exact-headline)
  9705. (and (equal (string-to-char s) ?*) (setq s (substring s 1)))
  9706. (goto-char (point-min))
  9707. (cond
  9708. ((let (case-fold-search)
  9709. (re-search-forward (format org-complex-heading-regexp-format
  9710. (regexp-quote s))
  9711. nil t))
  9712. ;; OK, found a match
  9713. (setq type 'dedicated)
  9714. (goto-char (match-beginning 0)))
  9715. ((and (not org-link-search-inhibit-query)
  9716. (eq org-link-search-must-match-exact-headline 'query-to-create)
  9717. (y-or-n-p "No match - create this as a new heading? "))
  9718. (goto-char (point-max))
  9719. (or (bolp) (newline))
  9720. (insert "* " s "\n")
  9721. (beginning-of-line 0))
  9722. (t
  9723. (goto-char pos)
  9724. (error "No match"))))
  9725. (t
  9726. ;; A normal search string
  9727. (when (equal (string-to-char s) ?*)
  9728. ;; Anchor on headlines, post may include tags.
  9729. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  9730. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@#%:+]:[ \t]*\\)?$")
  9731. s (substring s 1)))
  9732. (remove-text-properties
  9733. 0 (length s)
  9734. '(face nil mouse-face nil keymap nil fontified nil) s)
  9735. ;; Make a series of regular expressions to find a match
  9736. (setq words (org-split-string s "[ \n\r\t]+")
  9737. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  9738. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  9739. "\\)" markers)
  9740. re2a_ (concat "\\(" (mapconcat 'downcase words
  9741. "[ \t\r\n]+") "\\)[ \t\r\n]")
  9742. re2a (concat "[ \t\r\n]" re2a_)
  9743. re4_ (concat "\\(" (mapconcat 'downcase words
  9744. "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  9745. re4 (concat "[^a-zA-Z_]" re4_)
  9746. re1 (concat pre re2 post)
  9747. re3 (concat pre (if pre re4_ re4) post)
  9748. re5 (concat pre ".*" re4)
  9749. re2 (concat pre re2)
  9750. re2a (concat pre (if pre re2a_ re2a))
  9751. re4 (concat pre (if pre re4_ re4))
  9752. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  9753. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  9754. re5 "\\)"))
  9755. (cond
  9756. ((eq type 'org-occur) (org-occur reall))
  9757. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  9758. (t (goto-char (point-min))
  9759. (setq type 'fuzzy)
  9760. (if (or (and (org-search-not-self 1 re0 nil t)
  9761. (setq type 'dedicated))
  9762. (org-search-not-self 1 re1 nil t)
  9763. (org-search-not-self 1 re2 nil t)
  9764. (org-search-not-self 1 re2a nil t)
  9765. (org-search-not-self 1 re3 nil t)
  9766. (org-search-not-self 1 re4 nil t)
  9767. (org-search-not-self 1 re5 nil t))
  9768. (goto-char (match-beginning 1))
  9769. (goto-char pos)
  9770. (error "No match"))))))
  9771. (and (derived-mode-p 'org-mode)
  9772. (not stealth)
  9773. (org-show-context 'link-search))
  9774. type))
  9775. (defun org-search-not-self (group &rest args)
  9776. "Execute `re-search-forward', but only accept matches that do not
  9777. enclose the position of `org-open-link-marker'."
  9778. (let ((m org-open-link-marker))
  9779. (catch 'exit
  9780. (while (apply 're-search-forward args)
  9781. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  9782. (goto-char (match-end group))
  9783. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  9784. (> (match-beginning 0) (marker-position m))
  9785. (< (match-end 0) (marker-position m)))
  9786. (save-match-data
  9787. (or (not (org-in-regexp
  9788. org-bracket-link-analytic-regexp 1))
  9789. (not (match-end 4)) ; no description
  9790. (and (<= (match-beginning 4) (point))
  9791. (>= (match-end 4) (point))))))
  9792. (throw 'exit (point))))))))
  9793. (defun org-get-buffer-for-internal-link (buffer)
  9794. "Return a buffer to be used for displaying the link target of internal links."
  9795. (cond
  9796. ((not org-display-internal-link-with-indirect-buffer)
  9797. buffer)
  9798. ((string-match "(Clone)$" (buffer-name buffer))
  9799. (message "Buffer is already a clone, not making another one")
  9800. ;; we also do not modify visibility in this case
  9801. buffer)
  9802. (t ; make a new indirect buffer for displaying the link
  9803. (let* ((bn (buffer-name buffer))
  9804. (ibn (concat bn "(Clone)"))
  9805. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  9806. (with-current-buffer ib (org-overview))
  9807. ib))))
  9808. (defun org-do-occur (regexp &optional cleanup)
  9809. "Call the Emacs command `occur'.
  9810. If CLEANUP is non-nil, remove the printout of the regular expression
  9811. in the *Occur* buffer. This is useful if the regex is long and not useful
  9812. to read."
  9813. (occur regexp)
  9814. (when cleanup
  9815. (let ((cwin (selected-window)) win beg end)
  9816. (when (setq win (get-buffer-window "*Occur*"))
  9817. (select-window win))
  9818. (goto-char (point-min))
  9819. (when (re-search-forward "match[a-z]+" nil t)
  9820. (setq beg (match-end 0))
  9821. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  9822. (setq end (1- (match-beginning 0)))))
  9823. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  9824. (goto-char (point-min))
  9825. (select-window cwin))))
  9826. ;;; The mark ring for links jumps
  9827. (defvar org-mark-ring nil
  9828. "Mark ring for positions before jumps in Org-mode.")
  9829. (defvar org-mark-ring-last-goto nil
  9830. "Last position in the mark ring used to go back.")
  9831. ;; Fill and close the ring
  9832. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  9833. (loop for i from 1 to org-mark-ring-length do
  9834. (push (make-marker) org-mark-ring))
  9835. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  9836. org-mark-ring)
  9837. (defun org-mark-ring-push (&optional pos buffer)
  9838. "Put the current position or POS into the mark ring and rotate it."
  9839. (interactive)
  9840. (setq pos (or pos (point)))
  9841. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  9842. (move-marker (car org-mark-ring)
  9843. (or pos (point))
  9844. (or buffer (current-buffer)))
  9845. (message "%s"
  9846. (substitute-command-keys
  9847. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  9848. (defun org-mark-ring-goto (&optional n)
  9849. "Jump to the previous position in the mark ring.
  9850. With prefix arg N, jump back that many stored positions. When
  9851. called several times in succession, walk through the entire ring.
  9852. Org-mode commands jumping to a different position in the current file,
  9853. or to another Org-mode file, automatically push the old position
  9854. onto the ring."
  9855. (interactive "p")
  9856. (let (p m)
  9857. (if (eq last-command this-command)
  9858. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  9859. (setq p org-mark-ring))
  9860. (setq org-mark-ring-last-goto p)
  9861. (setq m (car p))
  9862. (org-pop-to-buffer-same-window (marker-buffer m))
  9863. (goto-char m)
  9864. (if (or (outline-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  9865. (defun org-remove-angle-brackets (s)
  9866. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  9867. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  9868. s)
  9869. (defun org-add-angle-brackets (s)
  9870. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  9871. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  9872. s)
  9873. (defun org-remove-double-quotes (s)
  9874. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  9875. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  9876. s)
  9877. ;;; Following specific links
  9878. (defun org-follow-timestamp-link ()
  9879. "Open an agenda view for the time-stamp date/range at point."
  9880. (cond
  9881. ((org-at-date-range-p t)
  9882. (let ((org-agenda-start-on-weekday)
  9883. (t1 (match-string 1))
  9884. (t2 (match-string 2)) tt1 tt2)
  9885. (setq tt1 (time-to-days (org-time-string-to-time t1))
  9886. tt2 (time-to-days (org-time-string-to-time t2)))
  9887. (let ((org-agenda-buffer-tmp-name
  9888. (format "*Org Agenda(a:%s)"
  9889. (concat (substring t1 0 10) "--" (substring t2 0 10)))))
  9890. (org-agenda-list nil tt1 (1+ (- tt2 tt1))))))
  9891. ((org-at-timestamp-p t)
  9892. (let ((org-agenda-buffer-tmp-name
  9893. (format "*Org Agenda(a:%s)" (substring (match-string 1) 0 10))))
  9894. (org-agenda-list nil (time-to-days (org-time-string-to-time
  9895. (substring (match-string 1) 0 10)))
  9896. 1)))
  9897. (t (error "This should not happen"))))
  9898. ;;; Following file links
  9899. (declare-function mailcap-parse-mailcaps "mailcap" (&optional path force))
  9900. (declare-function mailcap-extension-to-mime "mailcap" (extn))
  9901. (declare-function mailcap-mime-info
  9902. "mailcap" (string &optional request no-decode))
  9903. (defvar org-wait nil)
  9904. (defun org-open-file (path &optional in-emacs line search)
  9905. "Open the file at PATH.
  9906. First, this expands any special file name abbreviations. Then the
  9907. configuration variable `org-file-apps' is checked if it contains an
  9908. entry for this file type, and if yes, the corresponding command is launched.
  9909. If no application is found, Emacs simply visits the file.
  9910. With optional prefix argument IN-EMACS, Emacs will visit the file.
  9911. With a double \\[universal-argument] \\[universal-argument] \
  9912. prefix arg, Org tries to avoid opening in Emacs
  9913. and to use an external application to visit the file.
  9914. Optional LINE specifies a line to go to, optional SEARCH a string
  9915. to search for. If LINE or SEARCH is given, the file will be
  9916. opened in Emacs, unless an entry from org-file-apps that makes
  9917. use of groups in a regexp matches.
  9918. If you want to change the way frames are used when following a
  9919. link, please customize `org-link-frame-setup'.
  9920. If the file does not exist, an error is thrown."
  9921. (let* ((file (if (equal path "")
  9922. buffer-file-name
  9923. (substitute-in-file-name (expand-file-name path))))
  9924. (file-apps (append org-file-apps (org-default-apps)))
  9925. (apps (org-remove-if
  9926. 'org-file-apps-entry-match-against-dlink-p file-apps))
  9927. (apps-dlink (org-remove-if-not
  9928. 'org-file-apps-entry-match-against-dlink-p file-apps))
  9929. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  9930. (dirp (if remp nil (file-directory-p file)))
  9931. (file (if (and dirp org-open-directory-means-index-dot-org)
  9932. (concat (file-name-as-directory file) "index.org")
  9933. file))
  9934. (a-m-a-p (assq 'auto-mode apps))
  9935. (dfile (downcase file))
  9936. ;; reconstruct the original file: link from the PATH, LINE and SEARCH args
  9937. (link (cond ((and (eq line nil)
  9938. (eq search nil))
  9939. file)
  9940. (line
  9941. (concat file "::" (number-to-string line)))
  9942. (search
  9943. (concat file "::" search))))
  9944. (dlink (downcase link))
  9945. (old-buffer (current-buffer))
  9946. (old-pos (point))
  9947. (old-mode major-mode)
  9948. ext cmd link-match-data)
  9949. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  9950. (setq ext (match-string 1 dfile))
  9951. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  9952. (setq ext (match-string 1 dfile))))
  9953. (cond
  9954. ((member in-emacs '((16) system))
  9955. (setq cmd (cdr (assoc 'system apps))))
  9956. (in-emacs (setq cmd 'emacs))
  9957. (t
  9958. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  9959. (and dirp (cdr (assoc 'directory apps)))
  9960. ; first, try matching against apps-dlink
  9961. ; if we get a match here, store the match data for later
  9962. (let ((match (assoc-default dlink apps-dlink
  9963. 'string-match)))
  9964. (if match
  9965. (progn (setq link-match-data (match-data))
  9966. match)
  9967. (progn (setq in-emacs (or in-emacs line search))
  9968. nil))) ; if we have no match in apps-dlink,
  9969. ; always open the file in emacs if line or search
  9970. ; is given (for backwards compatibility)
  9971. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  9972. 'string-match)
  9973. (cdr (assoc ext apps))
  9974. (cdr (assoc t apps))))))
  9975. (when (eq cmd 'system)
  9976. (setq cmd (cdr (assoc 'system apps))))
  9977. (when (eq cmd 'default)
  9978. (setq cmd (cdr (assoc t apps))))
  9979. (when (eq cmd 'mailcap)
  9980. (require 'mailcap)
  9981. (mailcap-parse-mailcaps)
  9982. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  9983. (command (mailcap-mime-info mime-type)))
  9984. (if (stringp command)
  9985. (setq cmd command)
  9986. (setq cmd 'emacs))))
  9987. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  9988. (not (file-exists-p file))
  9989. (not org-open-non-existing-files))
  9990. (user-error "No such file: %s" file))
  9991. (cond
  9992. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  9993. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  9994. (while (string-match "['\"]%s['\"]" cmd)
  9995. (setq cmd (replace-match "%s" t t cmd)))
  9996. (while (string-match "%s" cmd)
  9997. (setq cmd (replace-match
  9998. (save-match-data
  9999. (shell-quote-argument
  10000. (convert-standard-filename file)))
  10001. t t cmd)))
  10002. ;; Replace "%1", "%2" etc. in command with group matches from regex
  10003. (save-match-data
  10004. (let ((match-index 1)
  10005. (number-of-groups (- (/ (length link-match-data) 2) 1)))
  10006. (set-match-data link-match-data)
  10007. (while (<= match-index number-of-groups)
  10008. (let ((regex (concat "%" (number-to-string match-index)))
  10009. (replace-with (match-string match-index dlink)))
  10010. (while (string-match regex cmd)
  10011. (setq cmd (replace-match replace-with t t cmd))))
  10012. (setq match-index (+ match-index 1)))))
  10013. (save-window-excursion
  10014. (message "Running %s...done" cmd)
  10015. (start-process-shell-command cmd nil cmd)
  10016. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))))
  10017. ((or (stringp cmd)
  10018. (eq cmd 'emacs))
  10019. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  10020. (widen)
  10021. (if line (org-goto-line line)
  10022. (if search (org-link-search search))))
  10023. ((consp cmd)
  10024. (let ((file (convert-standard-filename file)))
  10025. (save-match-data
  10026. (set-match-data link-match-data)
  10027. (eval cmd))))
  10028. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  10029. (and (derived-mode-p 'org-mode) (eq old-mode 'org-mode)
  10030. (or (not (equal old-buffer (current-buffer)))
  10031. (not (equal old-pos (point))))
  10032. (org-mark-ring-push old-pos old-buffer))))
  10033. (defun org-file-apps-entry-match-against-dlink-p (entry)
  10034. "This function returns non-nil if `entry' uses a regular
  10035. expression which should be matched against the whole link by
  10036. org-open-file.
  10037. It assumes that is the case when the entry uses a regular
  10038. expression which has at least one grouping construct and the
  10039. action is either a lisp form or a command string containing
  10040. '%1', i.e. using at least one subexpression match as a
  10041. parameter."
  10042. (let ((selector (car entry))
  10043. (action (cdr entry)))
  10044. (if (stringp selector)
  10045. (and (> (regexp-opt-depth selector) 0)
  10046. (or (and (stringp action)
  10047. (string-match "%[0-9]" action))
  10048. (consp action)))
  10049. nil)))
  10050. (defun org-default-apps ()
  10051. "Return the default applications for this operating system."
  10052. (cond
  10053. ((eq system-type 'darwin)
  10054. org-file-apps-defaults-macosx)
  10055. ((eq system-type 'windows-nt)
  10056. org-file-apps-defaults-windowsnt)
  10057. (t org-file-apps-defaults-gnu)))
  10058. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  10059. "Convert extensions to regular expressions in the cars of LIST.
  10060. Also, weed out any non-string entries, because the return value is used
  10061. only for regexp matching.
  10062. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  10063. point to the symbol `emacs', indicating that the file should
  10064. be opened in Emacs."
  10065. (append
  10066. (delq nil
  10067. (mapcar (lambda (x)
  10068. (if (not (stringp (car x)))
  10069. nil
  10070. (if (string-match "\\W" (car x))
  10071. x
  10072. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  10073. list))
  10074. (if add-auto-mode
  10075. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  10076. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  10077. (defun org-file-remote-p (file)
  10078. "Test whether FILE specifies a location on a remote system.
  10079. Return non-nil if the location is indeed remote.
  10080. For example, the filename \"/user@host:/foo\" specifies a location
  10081. on the system \"/user@host:\"."
  10082. (cond ((fboundp 'file-remote-p)
  10083. (file-remote-p file))
  10084. ((fboundp 'tramp-handle-file-remote-p)
  10085. (tramp-handle-file-remote-p file))
  10086. ((and (boundp 'ange-ftp-name-format)
  10087. (string-match (car ange-ftp-name-format) file))
  10088. t)))
  10089. ;;;; Refiling
  10090. (defun org-get-org-file ()
  10091. "Read a filename, with default directory `org-directory'."
  10092. (let ((default (or org-default-notes-file remember-data-file)))
  10093. (read-file-name (format "File name [%s]: " default)
  10094. (file-name-as-directory org-directory)
  10095. default)))
  10096. (defun org-notes-order-reversed-p ()
  10097. "Check if the current file should receive notes in reversed order."
  10098. (cond
  10099. ((not org-reverse-note-order) nil)
  10100. ((eq t org-reverse-note-order) t)
  10101. ((not (listp org-reverse-note-order)) nil)
  10102. (t (catch 'exit
  10103. (let ((all org-reverse-note-order)
  10104. entry)
  10105. (while (setq entry (pop all))
  10106. (if (string-match (car entry) buffer-file-name)
  10107. (throw 'exit (cdr entry))))
  10108. nil)))))
  10109. (defvar org-refile-target-table nil
  10110. "The list of refile targets, created by `org-refile'.")
  10111. (defvar org-agenda-new-buffers nil
  10112. "Buffers created to visit agenda files.")
  10113. (defvar org-refile-cache nil
  10114. "Cache for refile targets.")
  10115. (defvar org-refile-markers nil
  10116. "All the markers used for caching refile locations.")
  10117. (defun org-refile-marker (pos)
  10118. "Get a new refile marker, but only if caching is in use."
  10119. (if (not org-refile-use-cache)
  10120. pos
  10121. (let ((m (make-marker)))
  10122. (move-marker m pos)
  10123. (push m org-refile-markers)
  10124. m)))
  10125. (defun org-refile-cache-clear ()
  10126. "Clear the refile cache and disable all the markers."
  10127. (mapc (lambda (m) (move-marker m nil)) org-refile-markers)
  10128. (setq org-refile-markers nil)
  10129. (setq org-refile-cache nil)
  10130. (message "Refile cache has been cleared"))
  10131. (defun org-refile-cache-check-set (set)
  10132. "Check if all the markers in the cache still have live buffers."
  10133. (let (marker)
  10134. (catch 'exit
  10135. (while (and set (setq marker (nth 3 (pop set))))
  10136. ;; If `org-refile-use-outline-path' is 'file, marker may be nil
  10137. (when (and marker (null (marker-buffer marker)))
  10138. (message "Please regenerate the refile cache with `C-0 C-c C-w'")
  10139. (sit-for 3)
  10140. (throw 'exit nil)))
  10141. t)))
  10142. (defun org-refile-cache-put (set &rest identifiers)
  10143. "Push the refile targets SET into the cache, under IDENTIFIERS."
  10144. (let* ((key (sha1 (prin1-to-string identifiers)))
  10145. (entry (assoc key org-refile-cache)))
  10146. (if entry
  10147. (setcdr entry set)
  10148. (push (cons key set) org-refile-cache))))
  10149. (defun org-refile-cache-get (&rest identifiers)
  10150. "Retrieve the cached value for refile targets given by IDENTIFIERS."
  10151. (cond
  10152. ((not org-refile-cache) nil)
  10153. ((not org-refile-use-cache) (org-refile-cache-clear) nil)
  10154. (t
  10155. (let ((set (cdr (assoc (sha1 (prin1-to-string identifiers))
  10156. org-refile-cache))))
  10157. (and set (org-refile-cache-check-set set) set)))))
  10158. (defun org-refile-get-targets (&optional default-buffer excluded-entries)
  10159. "Produce a table with refile targets."
  10160. (let ((case-fold-search nil)
  10161. ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
  10162. (entries (or org-refile-targets '((nil . (:level . 1)))))
  10163. targets tgs txt re files f desc descre fast-path-p level pos0)
  10164. (message "Getting targets...")
  10165. (with-current-buffer (or default-buffer (current-buffer))
  10166. (while (setq entry (pop entries))
  10167. (setq files (car entry) desc (cdr entry))
  10168. (setq fast-path-p nil)
  10169. (cond
  10170. ((null files) (setq files (list (current-buffer))))
  10171. ((eq files 'org-agenda-files)
  10172. (setq files (org-agenda-files 'unrestricted)))
  10173. ((and (symbolp files) (fboundp files))
  10174. (setq files (funcall files)))
  10175. ((and (symbolp files) (boundp files))
  10176. (setq files (symbol-value files))))
  10177. (if (stringp files) (setq files (list files)))
  10178. (cond
  10179. ((eq (car desc) :tag)
  10180. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  10181. ((eq (car desc) :todo)
  10182. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  10183. ((eq (car desc) :regexp)
  10184. (setq descre (cdr desc)))
  10185. ((eq (car desc) :level)
  10186. (setq descre (concat "^\\*\\{" (number-to-string
  10187. (if org-odd-levels-only
  10188. (1- (* 2 (cdr desc)))
  10189. (cdr desc)))
  10190. "\\}[ \t]")))
  10191. ((eq (car desc) :maxlevel)
  10192. (setq fast-path-p t)
  10193. (setq descre (concat "^\\*\\{1," (number-to-string
  10194. (if org-odd-levels-only
  10195. (1- (* 2 (cdr desc)))
  10196. (cdr desc)))
  10197. "\\}[ \t]")))
  10198. (t (error "Bad refiling target description %s" desc)))
  10199. (while (setq f (pop files))
  10200. (with-current-buffer
  10201. (if (bufferp f) f (org-get-agenda-file-buffer f))
  10202. (or
  10203. (setq tgs (org-refile-cache-get (buffer-file-name) descre))
  10204. (progn
  10205. (if (bufferp f) (setq f (buffer-file-name
  10206. (buffer-base-buffer f))))
  10207. (setq f (and f (expand-file-name f)))
  10208. (if (eq org-refile-use-outline-path 'file)
  10209. (push (list (file-name-nondirectory f) f nil nil) tgs))
  10210. (save-excursion
  10211. (save-restriction
  10212. (widen)
  10213. (goto-char (point-min))
  10214. (while (re-search-forward descre nil t)
  10215. (goto-char (setq pos0 (point-at-bol)))
  10216. (catch 'next
  10217. (when org-refile-target-verify-function
  10218. (save-match-data
  10219. (or (funcall org-refile-target-verify-function)
  10220. (throw 'next t))))
  10221. (when (and (looking-at org-complex-heading-regexp)
  10222. (not (member (match-string 4) excluded-entries))
  10223. (match-string 4))
  10224. (setq level (org-reduced-level
  10225. (- (match-end 1) (match-beginning 1)))
  10226. txt (org-link-display-format (match-string 4))
  10227. txt (replace-regexp-in-string "\\( *\[[0-9]+/?[0-9]*%?\]\\)+$" "" txt)
  10228. re (format org-complex-heading-regexp-format
  10229. (regexp-quote (match-string 4))))
  10230. (when org-refile-use-outline-path
  10231. (setq txt (mapconcat
  10232. 'org-protect-slash
  10233. (append
  10234. (if (eq org-refile-use-outline-path
  10235. 'file)
  10236. (list (file-name-nondirectory
  10237. (buffer-file-name
  10238. (buffer-base-buffer))))
  10239. (if (eq org-refile-use-outline-path
  10240. 'full-file-path)
  10241. (list (buffer-file-name
  10242. (buffer-base-buffer)))))
  10243. (org-get-outline-path fast-path-p
  10244. level txt)
  10245. (list txt))
  10246. "/")))
  10247. (push (list txt f re (org-refile-marker (point)))
  10248. tgs)))
  10249. (when (= (point) pos0)
  10250. ;; verification function has not moved point
  10251. (goto-char (point-at-eol))))))))
  10252. (when org-refile-use-cache
  10253. (org-refile-cache-put tgs (buffer-file-name) descre))
  10254. (setq targets (append tgs targets))))))
  10255. (message "Getting targets...done")
  10256. (nreverse targets)))
  10257. (defun org-protect-slash (s)
  10258. (while (string-match "/" s)
  10259. (setq s (replace-match "\\" t t s)))
  10260. s)
  10261. (defvar org-olpa (make-vector 20 nil))
  10262. (defun org-get-outline-path (&optional fastp level heading)
  10263. "Return the outline path to the current entry, as a list.
  10264. The parameters FASTP, LEVEL, and HEADING are for use by a scanner
  10265. routine which makes outline path derivations for an entire file,
  10266. avoiding backtracing. Refile target collection makes use of that."
  10267. (if fastp
  10268. (progn
  10269. (if (> level 19)
  10270. (error "Outline path failure, more than 19 levels"))
  10271. (loop for i from level upto 19 do
  10272. (aset org-olpa i nil))
  10273. (prog1
  10274. (delq nil (append org-olpa nil))
  10275. (aset org-olpa level heading)))
  10276. (let (rtn case-fold-search)
  10277. (save-excursion
  10278. (save-restriction
  10279. (widen)
  10280. (while (org-up-heading-safe)
  10281. (when (looking-at org-complex-heading-regexp)
  10282. (push (org-trim
  10283. (replace-regexp-in-string
  10284. ;; Remove statistical/checkboxes cookies
  10285. "\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" ""
  10286. (org-match-string-no-properties 4)))
  10287. rtn)))
  10288. rtn)))))
  10289. (defun org-format-outline-path (path &optional width prefix separator)
  10290. "Format the outline path PATH for display.
  10291. WIDTH is the maximum number of characters that is available.
  10292. PREFIX is a prefix to be included in the returned string,
  10293. such as the file name.
  10294. SEPARATOR is inserted between the different parts of the path,
  10295. the default is \"/\"."
  10296. (setq width (or width 79))
  10297. (if prefix (setq width (- width (length prefix))))
  10298. (if (not path)
  10299. (or prefix "")
  10300. (let* ((nsteps (length path))
  10301. (total-width (+ nsteps (apply '+ (mapcar 'length path))))
  10302. (maxwidth (if (<= total-width width)
  10303. 10000 ;; everything fits
  10304. ;; we need to shorten the level headings
  10305. (/ (- width nsteps) nsteps)))
  10306. (org-odd-levels-only nil)
  10307. (n 0)
  10308. (total (1+ (length prefix))))
  10309. (setq maxwidth (max maxwidth 10))
  10310. (concat prefix
  10311. (if prefix (or separator "/"))
  10312. (mapconcat
  10313. (lambda (h)
  10314. (setq n (1+ n))
  10315. (if (and (= n nsteps) (< maxwidth 10000))
  10316. (setq maxwidth (- total-width total)))
  10317. (if (< (length h) maxwidth)
  10318. (progn (setq total (+ total (length h) 1)) h)
  10319. (setq h (substring h 0 (- maxwidth 2))
  10320. total (+ total maxwidth 1))
  10321. (if (string-match "[ \t]+\\'" h)
  10322. (setq h (substring h 0 (match-beginning 0))))
  10323. (setq h (concat h "..")))
  10324. (org-add-props h nil 'face
  10325. (nth (% (1- n) org-n-level-faces)
  10326. org-level-faces))
  10327. h)
  10328. path (or separator "/"))))))
  10329. (defun org-display-outline-path (&optional file current separator just-return-string)
  10330. "Display the current outline path in the echo area.
  10331. If FILE is non-nil, prepend the output with the file name.
  10332. If CURRENT is non-nil, append the current heading to the output.
  10333. SEPARATOR is passed through to `org-format-outline-path'. It separates
  10334. the different parts of the path and defaults to \"/\".
  10335. If JUST-RETURN-STRING is non-nil, return a string, don't display a message."
  10336. (interactive "P")
  10337. (let* (case-fold-search
  10338. message-log-max ; Don't populate the *Messages* buffer
  10339. (bfn (buffer-file-name (buffer-base-buffer)))
  10340. (path (and (derived-mode-p 'org-mode) (org-get-outline-path)))
  10341. res)
  10342. (if current (setq path (append path
  10343. (save-excursion
  10344. (org-back-to-heading t)
  10345. (if (looking-at org-complex-heading-regexp)
  10346. (list (match-string 4)))))))
  10347. (setq res
  10348. (org-format-outline-path
  10349. path
  10350. (1- (frame-width))
  10351. (and file bfn (concat (file-name-nondirectory bfn) separator))
  10352. separator))
  10353. (if just-return-string
  10354. (org-no-properties res)
  10355. (message "%s" res))))
  10356. (defvar org-refile-history nil
  10357. "History for refiling operations.")
  10358. (defvar org-after-refile-insert-hook nil
  10359. "Hook run after `org-refile' has inserted its stuff at the new location.
  10360. Note that this is still *before* the stuff will be removed from
  10361. the *old* location.")
  10362. (defvar org-capture-last-stored-marker)
  10363. (defvar org-refile-keep nil
  10364. "Non-nil means `org-refile' will copy instead of refile.")
  10365. (defun org-copy ()
  10366. "Like `org-refile', but copy."
  10367. (interactive)
  10368. (let ((org-refile-keep t))
  10369. (funcall 'org-refile nil nil nil "Copy")))
  10370. (defun org-refile (&optional goto default-buffer rfloc msg)
  10371. "Move the entry or entries at point to another heading.
  10372. The list of target headings is compiled using the information in
  10373. `org-refile-targets', which see.
  10374. At the target location, the entry is filed as a subitem of the target
  10375. heading. Depending on `org-reverse-note-order', the new subitem will
  10376. either be the first or the last subitem.
  10377. If there is an active region, all entries in that region will be moved.
  10378. However, the region must fulfill the requirement that the first heading
  10379. is the first one sets the top-level of the moved text - at most siblings
  10380. below it are allowed.
  10381. With prefix arg GOTO, the command will only visit the target location
  10382. and not actually move anything.
  10383. With a double prefix arg \\[universal-argument] \\[universal-argument], \
  10384. go to the location where the last refiling operation has put the subtree.
  10385. With a prefix argument of `2', refile to the running clock.
  10386. RFLOC can be a refile location obtained in a different way.
  10387. MSG is a string to replace \"Refile\" in the default prompt with
  10388. another verb. E.g. `org-copy' sets this parameter to \"Copy\".
  10389. See also `org-refile-use-outline-path' and `org-completion-use-ido'.
  10390. If you are using target caching (see `org-refile-use-cache'),
  10391. you have to clear the target cache in order to find new targets.
  10392. This can be done with a 0 prefix (`C-0 C-c C-w') or a triple
  10393. prefix argument (`C-u C-u C-u C-c C-w')."
  10394. (interactive "P")
  10395. (if (member goto '(0 (64)))
  10396. (org-refile-cache-clear)
  10397. (let* ((actionmsg (or msg "Refile"))
  10398. (cbuf (current-buffer))
  10399. (regionp (org-region-active-p))
  10400. (region-start (and regionp (region-beginning)))
  10401. (region-end (and regionp (region-end)))
  10402. (region-length (and regionp (- region-end region-start)))
  10403. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  10404. pos it nbuf file re level reversed)
  10405. (setq last-command nil)
  10406. (when regionp
  10407. (goto-char region-start)
  10408. (or (bolp) (goto-char (point-at-bol)))
  10409. (setq region-start (point))
  10410. (unless (or (org-kill-is-subtree-p
  10411. (buffer-substring region-start region-end))
  10412. (prog1 org-refile-active-region-within-subtree
  10413. (org-toggle-heading)))
  10414. (user-error "The region is not a (sequence of) subtree(s)")))
  10415. (if (equal goto '(16))
  10416. (org-refile-goto-last-stored)
  10417. (when (or
  10418. (and (equal goto 2)
  10419. org-clock-hd-marker (marker-buffer org-clock-hd-marker)
  10420. (prog1
  10421. (setq it (list (or org-clock-heading "running clock")
  10422. (buffer-file-name
  10423. (marker-buffer org-clock-hd-marker))
  10424. ""
  10425. (marker-position org-clock-hd-marker)))
  10426. (setq goto nil)))
  10427. (setq it (or rfloc
  10428. (let (heading-text)
  10429. (save-excursion
  10430. (unless goto
  10431. (org-back-to-heading t)
  10432. (setq heading-text
  10433. (nth 4 (org-heading-components))))
  10434. (org-refile-get-location
  10435. (cond (goto "Goto")
  10436. (regionp (concat actionmsg " region to"))
  10437. (t (concat actionmsg " subtree \""
  10438. heading-text "\" to")))
  10439. default-buffer
  10440. (and (not (equal '(4) goto))
  10441. org-refile-allow-creating-parent-nodes)
  10442. goto))))))
  10443. (setq file (nth 1 it)
  10444. re (nth 2 it)
  10445. pos (nth 3 it))
  10446. (if (and (not goto)
  10447. pos
  10448. (equal (buffer-file-name) file)
  10449. (if regionp
  10450. (and (>= pos region-start)
  10451. (<= pos region-end))
  10452. (and (>= pos (point))
  10453. (< pos (save-excursion
  10454. (org-end-of-subtree t t))))))
  10455. (error "Cannot refile to position inside the tree or region"))
  10456. (setq nbuf (or (find-buffer-visiting file)
  10457. (find-file-noselect file)))
  10458. (if goto
  10459. (progn
  10460. (org-pop-to-buffer-same-window nbuf)
  10461. (goto-char pos)
  10462. (org-show-context 'org-goto))
  10463. (if regionp
  10464. (progn
  10465. (org-kill-new (buffer-substring region-start region-end))
  10466. (org-save-markers-in-region region-start region-end))
  10467. (org-copy-subtree 1 nil t))
  10468. (with-current-buffer (setq nbuf (or (find-buffer-visiting file)
  10469. (find-file-noselect file)))
  10470. (setq reversed (org-notes-order-reversed-p))
  10471. (save-excursion
  10472. (save-restriction
  10473. (widen)
  10474. (if pos
  10475. (progn
  10476. (goto-char pos)
  10477. (looking-at org-outline-regexp)
  10478. (setq level (org-get-valid-level (funcall outline-level) 1))
  10479. (goto-char
  10480. (if reversed
  10481. (or (outline-next-heading) (point-max))
  10482. (or (save-excursion (org-get-next-sibling))
  10483. (org-end-of-subtree t t)
  10484. (point-max)))))
  10485. (setq level 1)
  10486. (if (not reversed)
  10487. (goto-char (point-max))
  10488. (goto-char (point-min))
  10489. (or (outline-next-heading) (goto-char (point-max)))))
  10490. (if (not (bolp)) (newline))
  10491. (org-paste-subtree level)
  10492. (when org-log-refile
  10493. (org-add-log-setup 'refile nil nil 'findpos
  10494. org-log-refile)
  10495. (unless (eq org-log-refile 'note)
  10496. (save-excursion (org-add-log-note))))
  10497. (and org-auto-align-tags
  10498. (let ((org-loop-over-headlines-in-active-region nil))
  10499. (org-set-tags nil t)))
  10500. (with-demoted-errors
  10501. (bookmark-set "org-refile-last-stored"))
  10502. ;; If we are refiling for capture, make sure that the
  10503. ;; last-capture pointers point here
  10504. (when (org-bound-and-true-p org-refile-for-capture)
  10505. (with-demoted-errors
  10506. (bookmark-set "org-capture-last-stored-marker"))
  10507. (move-marker org-capture-last-stored-marker (point)))
  10508. (if (fboundp 'deactivate-mark) (deactivate-mark))
  10509. (run-hooks 'org-after-refile-insert-hook))))
  10510. (unless org-refile-keep
  10511. (if regionp
  10512. (delete-region (point) (+ (point) region-length))
  10513. (org-cut-subtree)))
  10514. (when (featurep 'org-inlinetask)
  10515. (org-inlinetask-remove-END-maybe))
  10516. (setq org-markers-to-move nil)
  10517. (message (concat actionmsg " to \"%s\" in file %s: done") (car it) file)))))))
  10518. (defun org-refile-goto-last-stored ()
  10519. "Go to the location where the last refile was stored."
  10520. (interactive)
  10521. (bookmark-jump "org-refile-last-stored")
  10522. (message "This is the location of the last refile"))
  10523. (defun org-refile-get-location (&optional prompt default-buffer new-nodes
  10524. no-exclude)
  10525. "Prompt the user for a refile location, using PROMPT.
  10526. PROMPT should not be suffixed with a colon and a space, because
  10527. this function appends the default value from
  10528. `org-refile-history' automatically, if that is not empty.
  10529. When NO-EXCLUDE is set, do not exclude headlines in the current subtree,
  10530. this is used for the GOTO interface."
  10531. (let ((org-refile-targets org-refile-targets)
  10532. (org-refile-use-outline-path org-refile-use-outline-path)
  10533. excluded-entries)
  10534. (when (and (derived-mode-p 'org-mode)
  10535. (not org-refile-use-cache)
  10536. (not no-exclude))
  10537. (org-map-tree
  10538. (lambda()
  10539. (setq excluded-entries
  10540. (append excluded-entries (list (org-get-heading t t)))))))
  10541. (setq org-refile-target-table
  10542. (org-refile-get-targets default-buffer excluded-entries)))
  10543. (unless org-refile-target-table
  10544. (user-error "No refile targets"))
  10545. (let* ((cbuf (current-buffer))
  10546. (partial-completion-mode nil)
  10547. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  10548. (cfunc (if (and org-refile-use-outline-path
  10549. org-outline-path-complete-in-steps)
  10550. 'org-olpath-completing-read
  10551. 'org-icompleting-read))
  10552. (extra (if org-refile-use-outline-path "/" ""))
  10553. (cbnex (concat (buffer-name) extra))
  10554. (filename (and cfn (expand-file-name cfn)))
  10555. (tbl (mapcar
  10556. (lambda (x)
  10557. (if (and (not (member org-refile-use-outline-path
  10558. '(file full-file-path)))
  10559. (not (equal filename (nth 1 x))))
  10560. (cons (concat (car x) extra " ("
  10561. (file-name-nondirectory (nth 1 x)) ")")
  10562. (cdr x))
  10563. (cons (concat (car x) extra) (cdr x))))
  10564. org-refile-target-table))
  10565. (completion-ignore-case t)
  10566. cdef
  10567. (prompt (concat prompt
  10568. (or (and (car org-refile-history)
  10569. (concat " (default " (car org-refile-history) ")"))
  10570. (and (assoc cbnex tbl) (setq cdef cbnex)
  10571. (concat " (default " cbnex ")"))) ": "))
  10572. pa answ parent-target child parent old-hist)
  10573. (setq old-hist org-refile-history)
  10574. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  10575. nil 'org-refile-history (or cdef (car org-refile-history))))
  10576. (setq pa (or (assoc answ tbl) (assoc (concat answ "/") tbl)))
  10577. (org-refile-check-position pa)
  10578. (if pa
  10579. (progn
  10580. (when (or (not org-refile-history)
  10581. (not (eq old-hist org-refile-history))
  10582. (not (equal (car pa) (car org-refile-history))))
  10583. (setq org-refile-history
  10584. (cons (car pa) (if (assoc (car org-refile-history) tbl)
  10585. org-refile-history
  10586. (cdr org-refile-history))))
  10587. (if (equal (car org-refile-history) (nth 1 org-refile-history))
  10588. (pop org-refile-history)))
  10589. pa)
  10590. (if (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  10591. (progn
  10592. (setq parent (match-string 1 answ)
  10593. child (match-string 2 answ))
  10594. (setq parent-target (or (assoc parent tbl)
  10595. (assoc (concat parent "/") tbl)))
  10596. (when (and parent-target
  10597. (or (eq new-nodes t)
  10598. (and (eq new-nodes 'confirm)
  10599. (y-or-n-p (format "Create new node \"%s\"? "
  10600. child)))))
  10601. (org-refile-new-child parent-target child)))
  10602. (user-error "Invalid target location")))))
  10603. (declare-function org-string-nw-p "org-macs" (s))
  10604. (defun org-refile-check-position (refile-pointer)
  10605. "Check if the refile pointer matches the headline to which it points."
  10606. (let* ((file (nth 1 refile-pointer))
  10607. (re (nth 2 refile-pointer))
  10608. (pos (nth 3 refile-pointer))
  10609. buffer)
  10610. (if (and (not (markerp pos)) (not file))
  10611. (user-error "Please save the buffer to a file before refiling")
  10612. (when (org-string-nw-p re)
  10613. (setq buffer (if (markerp pos)
  10614. (marker-buffer pos)
  10615. (or (find-buffer-visiting file)
  10616. (find-file-noselect file))))
  10617. (with-current-buffer buffer
  10618. (save-excursion
  10619. (save-restriction
  10620. (widen)
  10621. (goto-char pos)
  10622. (beginning-of-line 1)
  10623. (unless (org-looking-at-p re)
  10624. (user-error "Invalid refile position, please clear the cache with `C-0 C-c C-w' before refiling")))))))))
  10625. (defun org-refile-new-child (parent-target child)
  10626. "Use refile target PARENT-TARGET to add new CHILD below it."
  10627. (unless parent-target
  10628. (error "Cannot find parent for new node"))
  10629. (let ((file (nth 1 parent-target))
  10630. (pos (nth 3 parent-target))
  10631. level)
  10632. (with-current-buffer (or (find-buffer-visiting file)
  10633. (find-file-noselect file))
  10634. (save-excursion
  10635. (save-restriction
  10636. (widen)
  10637. (if pos
  10638. (goto-char pos)
  10639. (goto-char (point-max))
  10640. (if (not (bolp)) (newline)))
  10641. (when (looking-at org-outline-regexp)
  10642. (setq level (funcall outline-level))
  10643. (org-end-of-subtree t t))
  10644. (org-back-over-empty-lines)
  10645. (insert "\n" (make-string
  10646. (if pos (org-get-valid-level level 1) 1) ?*)
  10647. " " child "\n")
  10648. (beginning-of-line 0)
  10649. (list (concat (car parent-target) "/" child) file "" (point)))))))
  10650. (defun org-olpath-completing-read (prompt collection &rest args)
  10651. "Read an outline path like a file name."
  10652. (let ((thetable collection)
  10653. (org-completion-use-ido nil) ; does not work with ido.
  10654. (org-completion-use-iswitchb nil)) ; or iswitchb
  10655. (apply
  10656. 'org-icompleting-read prompt
  10657. (lambda (string predicate &optional flag)
  10658. (let (rtn r f (l (length string)))
  10659. (cond
  10660. ((eq flag nil)
  10661. ;; try completion
  10662. (try-completion string thetable))
  10663. ((eq flag t)
  10664. ;; all-completions
  10665. (setq rtn (all-completions string thetable predicate))
  10666. (mapcar
  10667. (lambda (x)
  10668. (setq r (substring x l))
  10669. (if (string-match " ([^)]*)$" x)
  10670. (setq f (match-string 0 x))
  10671. (setq f ""))
  10672. (if (string-match "/" r)
  10673. (concat string (substring r 0 (match-end 0)) f)
  10674. x))
  10675. rtn))
  10676. ((eq flag 'lambda)
  10677. ;; exact match?
  10678. (assoc string thetable)))))
  10679. args)))
  10680. ;;;; Dynamic blocks
  10681. (defun org-find-dblock (name)
  10682. "Find the first dynamic block with name NAME in the buffer.
  10683. If not found, stay at current position and return nil."
  10684. (let ((case-fold-search t) pos)
  10685. (save-excursion
  10686. (goto-char (point-min))
  10687. (setq pos (and (re-search-forward
  10688. (concat "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+" name "\\>") nil t)
  10689. (match-beginning 0))))
  10690. (if pos (goto-char pos))
  10691. pos))
  10692. (defconst org-dblock-start-re
  10693. "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  10694. "Matches the start line of a dynamic block, with parameters.")
  10695. (defconst org-dblock-end-re "^[ \t]*#\\+\\(?:END\\|end\\)\\([: \t\r\n]\\|$\\)"
  10696. "Matches the end of a dynamic block.")
  10697. (defun org-create-dblock (plist)
  10698. "Create a dynamic block section, with parameters taken from PLIST.
  10699. PLIST must contain a :name entry which is used as name of the block."
  10700. (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
  10701. (end-of-line 1)
  10702. (newline))
  10703. (let ((col (current-column))
  10704. (name (plist-get plist :name)))
  10705. (insert "#+BEGIN: " name)
  10706. (while plist
  10707. (if (eq (car plist) :name)
  10708. (setq plist (cddr plist))
  10709. (insert " " (prin1-to-string (pop plist)))))
  10710. (insert "\n\n" (make-string col ?\ ) "#+END:\n")
  10711. (beginning-of-line -2)))
  10712. (defun org-prepare-dblock ()
  10713. "Prepare dynamic block for refresh.
  10714. This empties the block, puts the cursor at the insert position and returns
  10715. the property list including an extra property :name with the block name."
  10716. (unless (looking-at org-dblock-start-re)
  10717. (user-error "Not at a dynamic block"))
  10718. (let* ((begdel (1+ (match-end 0)))
  10719. (name (org-no-properties (match-string 1)))
  10720. (params (append (list :name name)
  10721. (read (concat "(" (match-string 3) ")")))))
  10722. (save-excursion
  10723. (beginning-of-line 1)
  10724. (skip-chars-forward " \t")
  10725. (setq params (plist-put params :indentation-column (current-column))))
  10726. (unless (re-search-forward org-dblock-end-re nil t)
  10727. (error "Dynamic block not terminated"))
  10728. (setq params
  10729. (append params
  10730. (list :content (buffer-substring
  10731. begdel (match-beginning 0)))))
  10732. (delete-region begdel (match-beginning 0))
  10733. (goto-char begdel)
  10734. (open-line 1)
  10735. params))
  10736. (defun org-map-dblocks (&optional command)
  10737. "Apply COMMAND to all dynamic blocks in the current buffer.
  10738. If COMMAND is not given, use `org-update-dblock'."
  10739. (let ((cmd (or command 'org-update-dblock)))
  10740. (save-excursion
  10741. (goto-char (point-min))
  10742. (while (re-search-forward org-dblock-start-re nil t)
  10743. (goto-char (match-beginning 0))
  10744. (save-excursion
  10745. (condition-case nil
  10746. (funcall cmd)
  10747. (error (message "Error during update of dynamic block"))))
  10748. (unless (re-search-forward org-dblock-end-re nil t)
  10749. (error "Dynamic block not terminated"))))))
  10750. (defun org-dblock-update (&optional arg)
  10751. "User command for updating dynamic blocks.
  10752. Update the dynamic block at point. With prefix ARG, update all dynamic
  10753. blocks in the buffer."
  10754. (interactive "P")
  10755. (if arg
  10756. (org-update-all-dblocks)
  10757. (or (looking-at org-dblock-start-re)
  10758. (org-beginning-of-dblock))
  10759. (org-update-dblock)))
  10760. (defun org-update-dblock ()
  10761. "Update the dynamic block at point.
  10762. This means to empty the block, parse for parameters and then call
  10763. the correct writing function."
  10764. (interactive)
  10765. (save-window-excursion
  10766. (let* ((pos (point))
  10767. (line (org-current-line))
  10768. (params (org-prepare-dblock))
  10769. (name (plist-get params :name))
  10770. (indent (plist-get params :indentation-column))
  10771. (cmd (intern (concat "org-dblock-write:" name))))
  10772. (message "Updating dynamic block `%s' at line %d..." name line)
  10773. (funcall cmd params)
  10774. (message "Updating dynamic block `%s' at line %d...done" name line)
  10775. (goto-char pos)
  10776. (when (and indent (> indent 0))
  10777. (setq indent (make-string indent ?\ ))
  10778. (save-excursion
  10779. (org-beginning-of-dblock)
  10780. (forward-line 1)
  10781. (while (not (looking-at org-dblock-end-re))
  10782. (insert indent)
  10783. (beginning-of-line 2))
  10784. (when (looking-at org-dblock-end-re)
  10785. (and (looking-at "[ \t]+")
  10786. (replace-match ""))
  10787. (insert indent)))))))
  10788. (defun org-beginning-of-dblock ()
  10789. "Find the beginning of the dynamic block at point.
  10790. Error if there is no such block at point."
  10791. (let ((pos (point))
  10792. beg)
  10793. (end-of-line 1)
  10794. (if (and (re-search-backward org-dblock-start-re nil t)
  10795. (setq beg (match-beginning 0))
  10796. (re-search-forward org-dblock-end-re nil t)
  10797. (> (match-end 0) pos))
  10798. (goto-char beg)
  10799. (goto-char pos)
  10800. (error "Not in a dynamic block"))))
  10801. (defun org-update-all-dblocks ()
  10802. "Update all dynamic blocks in the buffer.
  10803. This function can be used in a hook."
  10804. (interactive)
  10805. (when (derived-mode-p 'org-mode)
  10806. (org-map-dblocks 'org-update-dblock)))
  10807. ;;;; Completion
  10808. (defun org-get-export-keywords ()
  10809. "Return a list of all currently understood export keywords.
  10810. Export keywords include options, block names, attributes and
  10811. keywords relative to each registered export back-end."
  10812. (delq nil
  10813. (let (keywords)
  10814. (mapc
  10815. (lambda (back-end)
  10816. (let ((props (cdr back-end)))
  10817. ;; Back-end name (for keywords, like #+LATEX:)
  10818. (push (upcase (symbol-name (car back-end))) keywords)
  10819. ;; Back-end options.
  10820. (mapc (lambda (option) (push (cadr option) keywords))
  10821. (plist-get (cdr back-end) :options-alist))))
  10822. (org-bound-and-true-p org-export-registered-backends))
  10823. keywords)))
  10824. (defconst org-options-keywords
  10825. '("ARCHIVE:" "AUTHOR:" "BIND:" "CATEGORY:" "COLUMNS:" "CREATOR:" "DATE:"
  10826. "DESCRIPTION:" "DRAWERS:" "EMAIL:" "EXCLUDE_TAGS:" "FILETAGS:" "INCLUDE:"
  10827. "INDEX:" "KEYWORDS:" "LANGUAGE:" "MACRO:" "OPTIONS:" "PROPERTY:"
  10828. "PRIORITIES:" "SELECT_TAGS:" "SEQ_TODO:" "SETUPFILE:" "STARTUP:" "TAGS:"
  10829. "TITLE:" "TODO:" "TYP_TODO:" "SELECT_TAGS:" "EXCLUDE_TAGS:"))
  10830. (defcustom org-structure-template-alist
  10831. '(("s" "#+BEGIN_SRC ?\n\n#+END_SRC"
  10832. "<src lang=\"?\">\n\n</src>")
  10833. ("e" "#+BEGIN_EXAMPLE\n?\n#+END_EXAMPLE"
  10834. "<example>\n?\n</example>")
  10835. ("q" "#+BEGIN_QUOTE\n?\n#+END_QUOTE"
  10836. "<quote>\n?\n</quote>")
  10837. ("v" "#+BEGIN_VERSE\n?\n#+END_VERSE"
  10838. "<verse>\n?\n</verse>")
  10839. ("V" "#+BEGIN_VERBATIM\n?\n#+END_VERBATIM"
  10840. "<verbatim>\n?\n</verbatim>")
  10841. ("c" "#+BEGIN_CENTER\n?\n#+END_CENTER"
  10842. "<center>\n?\n</center>")
  10843. ("l" "#+BEGIN_LaTeX\n?\n#+END_LaTeX"
  10844. "<literal style=\"latex\">\n?\n</literal>")
  10845. ("L" "#+LaTeX: "
  10846. "<literal style=\"latex\">?</literal>")
  10847. ("h" "#+BEGIN_HTML\n?\n#+END_HTML"
  10848. "<literal style=\"html\">\n?\n</literal>")
  10849. ("H" "#+HTML: "
  10850. "<literal style=\"html\">?</literal>")
  10851. ("a" "#+BEGIN_ASCII\n?\n#+END_ASCII")
  10852. ("A" "#+ASCII: ")
  10853. ("i" "#+INDEX: ?"
  10854. "#+INDEX: ?")
  10855. ("I" "#+INCLUDE: %file ?"
  10856. "<include file=%file markup=\"?\">"))
  10857. "Structure completion elements.
  10858. This is a list of abbreviation keys and values. The value gets inserted
  10859. if you type `<' followed by the key and then press the completion key,
  10860. usually `M-TAB'. %file will be replaced by a file name after prompting
  10861. for the file using completion. The cursor will be placed at the position
  10862. of the `?` in the template.
  10863. There are two templates for each key, the first uses the original Org syntax,
  10864. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  10865. the default when the /org-mtags.el/ module has been loaded. See also the
  10866. variable `org-mtags-prefer-muse-templates'."
  10867. :group 'org-completion
  10868. :type '(repeat
  10869. (string :tag "Key")
  10870. (string :tag "Template")
  10871. (string :tag "Muse Template")))
  10872. (defun org-try-structure-completion ()
  10873. "Try to complete a structure template before point.
  10874. This looks for strings like \"<e\" on an otherwise empty line and
  10875. expands them."
  10876. (let ((l (buffer-substring (point-at-bol) (point)))
  10877. a)
  10878. (when (and (looking-at "[ \t]*$")
  10879. (string-match "^[ \t]*<\\([a-zA-Z]+\\)$" l)
  10880. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  10881. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  10882. (match-beginning 1)) a)
  10883. t)))
  10884. (defun org-complete-expand-structure-template (start cell)
  10885. "Expand a structure template."
  10886. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  10887. (rpl (nth (if musep 2 1) cell))
  10888. (ind ""))
  10889. (delete-region start (point))
  10890. (when (string-match "\\`#\\+" rpl)
  10891. (cond
  10892. ((bolp))
  10893. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  10894. (setq ind (buffer-substring (point-at-bol) (point))))
  10895. (t (newline))))
  10896. (setq start (point))
  10897. (if (string-match "%file" rpl)
  10898. (setq rpl (replace-match
  10899. (concat
  10900. "\""
  10901. (save-match-data
  10902. (abbreviate-file-name (read-file-name "Include file: ")))
  10903. "\"")
  10904. t t rpl)))
  10905. (setq rpl (mapconcat 'identity (split-string rpl "\n")
  10906. (concat "\n" ind)))
  10907. (insert rpl)
  10908. (if (re-search-backward "\\?" start t) (delete-char 1))))
  10909. ;;;; TODO, DEADLINE, Comments
  10910. (defun org-toggle-comment ()
  10911. "Change the COMMENT state of an entry."
  10912. (interactive)
  10913. (save-excursion
  10914. (org-back-to-heading)
  10915. (let (case-fold-search)
  10916. (cond
  10917. ((looking-at (format org-heading-keyword-regexp-format
  10918. org-comment-string))
  10919. (goto-char (match-end 1))
  10920. (looking-at (concat " +" org-comment-string))
  10921. (replace-match "" t t)
  10922. (when (eolp) (insert " ")))
  10923. ((looking-at org-outline-regexp)
  10924. (goto-char (match-end 0))
  10925. (insert org-comment-string " "))))))
  10926. (defvar org-last-todo-state-is-todo nil
  10927. "This is non-nil when the last TODO state change led to a TODO state.
  10928. If the last change removed the TODO tag or switched to DONE, then
  10929. this is nil.")
  10930. (defvar org-setting-tags nil) ; dynamically skipped
  10931. (defvar org-todo-setup-filter-hook nil
  10932. "Hook for functions that pre-filter todo specs.
  10933. Each function takes a todo spec and returns either nil or the spec
  10934. transformed into canonical form." )
  10935. (defvar org-todo-get-default-hook nil
  10936. "Hook for functions that get a default item for todo.
  10937. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  10938. nil or a string to be used for the todo mark." )
  10939. (defvar org-agenda-headline-snapshot-before-repeat)
  10940. (defun org-current-effective-time ()
  10941. "Return current time adjusted for `org-extend-today-until' variable."
  10942. (let* ((ct (org-current-time))
  10943. (dct (decode-time ct))
  10944. (ct1
  10945. (cond
  10946. (org-use-last-clock-out-time-as-effective-time
  10947. (or (org-clock-get-last-clock-out-time) ct))
  10948. ((and org-use-effective-time (< (nth 2 dct) org-extend-today-until))
  10949. (encode-time 0 59 23 (1- (nth 3 dct)) (nth 4 dct) (nth 5 dct)))
  10950. (t ct))))
  10951. ct1))
  10952. (defun org-todo-yesterday (&optional arg)
  10953. "Like `org-todo' but the time of change will be 23:59 of yesterday."
  10954. (interactive "P")
  10955. (if (eq major-mode 'org-agenda-mode)
  10956. (apply 'org-agenda-todo-yesterday arg)
  10957. (let* ((hour (third (decode-time
  10958. (org-current-time))))
  10959. (org-extend-today-until (1+ hour)))
  10960. (org-todo arg))))
  10961. (defvar org-block-entry-blocking ""
  10962. "First entry preventing the TODO state change.")
  10963. (defun org-todo (&optional arg)
  10964. "Change the TODO state of an item.
  10965. The state of an item is given by a keyword at the start of the heading,
  10966. like
  10967. *** TODO Write paper
  10968. *** DONE Call mom
  10969. The different keywords are specified in the variable `org-todo-keywords'.
  10970. By default the available states are \"TODO\" and \"DONE\".
  10971. So for this example: when the item starts with TODO, it is changed to DONE.
  10972. When it starts with DONE, the DONE is removed. And when neither TODO nor
  10973. DONE are present, add TODO at the beginning of the heading.
  10974. With \\[universal-argument] prefix arg, use completion to determine the new \
  10975. state.
  10976. With numeric prefix arg, switch to that state.
  10977. With a double \\[universal-argument] prefix, switch to the next set of TODO \
  10978. keywords (nextset).
  10979. With a triple \\[universal-argument] prefix, circumvent any state blocking.
  10980. With a numeric prefix arg of 0, inhibit note taking for the change.
  10981. For calling through lisp, arg is also interpreted in the following way:
  10982. 'none -> empty state
  10983. \"\"(empty string) -> switch to empty state
  10984. 'done -> switch to DONE
  10985. 'nextset -> switch to the next set of keywords
  10986. 'previousset -> switch to the previous set of keywords
  10987. \"WAITING\" -> switch to the specified keyword, but only if it
  10988. really is a member of `org-todo-keywords'."
  10989. (interactive "P")
  10990. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  10991. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  10992. 'region-start-level 'region))
  10993. org-loop-over-headlines-in-active-region)
  10994. (org-map-entries
  10995. `(org-todo ,arg)
  10996. org-loop-over-headlines-in-active-region
  10997. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  10998. (if (equal arg '(16)) (setq arg 'nextset))
  10999. (let ((org-blocker-hook org-blocker-hook)
  11000. commentp
  11001. case-fold-search)
  11002. (when (equal arg '(64))
  11003. (setq arg nil org-blocker-hook nil))
  11004. (when (and org-blocker-hook
  11005. (or org-inhibit-blocking
  11006. (org-entry-get nil "NOBLOCKING")))
  11007. (setq org-blocker-hook nil))
  11008. (save-excursion
  11009. (catch 'exit
  11010. (org-back-to-heading t)
  11011. (when (looking-at (concat "^\\*+ " org-comment-string))
  11012. (org-toggle-comment)
  11013. (setq commentp t))
  11014. (if (looking-at org-outline-regexp) (goto-char (1- (match-end 0))))
  11015. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|[ \t]*$\\)"))
  11016. (looking-at "\\(?: *\\|[ \t]*$\\)"))
  11017. (let* ((match-data (match-data))
  11018. (startpos (point-at-bol))
  11019. (logging (save-match-data (org-entry-get nil "LOGGING" t t)))
  11020. (org-log-done org-log-done)
  11021. (org-log-repeat org-log-repeat)
  11022. (org-todo-log-states org-todo-log-states)
  11023. (org-inhibit-logging
  11024. (if (equal arg 0)
  11025. (progn (setq arg nil) 'note) org-inhibit-logging))
  11026. (this (match-string 1))
  11027. (hl-pos (match-beginning 0))
  11028. (head (org-get-todo-sequence-head this))
  11029. (ass (assoc head org-todo-kwd-alist))
  11030. (interpret (nth 1 ass))
  11031. (done-word (nth 3 ass))
  11032. (final-done-word (nth 4 ass))
  11033. (org-last-state (or this ""))
  11034. (completion-ignore-case t)
  11035. (member (member this org-todo-keywords-1))
  11036. (tail (cdr member))
  11037. (org-state (cond
  11038. ((and org-todo-key-trigger
  11039. (or (and (equal arg '(4))
  11040. (eq org-use-fast-todo-selection 'prefix))
  11041. (and (not arg) org-use-fast-todo-selection
  11042. (not (eq org-use-fast-todo-selection
  11043. 'prefix)))))
  11044. ;; Use fast selection
  11045. (org-fast-todo-selection))
  11046. ((and (equal arg '(4))
  11047. (or (not org-use-fast-todo-selection)
  11048. (not org-todo-key-trigger)))
  11049. ;; Read a state with completion
  11050. (org-icompleting-read
  11051. "State: " (mapcar 'list org-todo-keywords-1)
  11052. nil t))
  11053. ((eq arg 'right)
  11054. (if this
  11055. (if tail (car tail) nil)
  11056. (car org-todo-keywords-1)))
  11057. ((eq arg 'left)
  11058. (if (equal member org-todo-keywords-1)
  11059. nil
  11060. (if this
  11061. (nth (- (length org-todo-keywords-1)
  11062. (length tail) 2)
  11063. org-todo-keywords-1)
  11064. (org-last org-todo-keywords-1))))
  11065. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  11066. (setq arg nil))) ; hack to fall back to cycling
  11067. (arg
  11068. ;; user or caller requests a specific state
  11069. (cond
  11070. ((equal arg "") nil)
  11071. ((eq arg 'none) nil)
  11072. ((eq arg 'done) (or done-word (car org-done-keywords)))
  11073. ((eq arg 'nextset)
  11074. (or (car (cdr (member head org-todo-heads)))
  11075. (car org-todo-heads)))
  11076. ((eq arg 'previousset)
  11077. (let ((org-todo-heads (reverse org-todo-heads)))
  11078. (or (car (cdr (member head org-todo-heads)))
  11079. (car org-todo-heads))))
  11080. ((car (member arg org-todo-keywords-1)))
  11081. ((stringp arg)
  11082. (user-error "State `%s' not valid in this file" arg))
  11083. ((nth (1- (prefix-numeric-value arg))
  11084. org-todo-keywords-1))))
  11085. ((null member) (or head (car org-todo-keywords-1)))
  11086. ((equal this final-done-word) nil) ;; -> make empty
  11087. ((null tail) nil) ;; -> first entry
  11088. ((memq interpret '(type priority))
  11089. (if (eq this-command last-command)
  11090. (car tail)
  11091. (if (> (length tail) 0)
  11092. (or done-word (car org-done-keywords))
  11093. nil)))
  11094. (t
  11095. (car tail))))
  11096. (org-state (or
  11097. (run-hook-with-args-until-success
  11098. 'org-todo-get-default-hook org-state org-last-state)
  11099. org-state))
  11100. (next (if org-state (concat " " org-state " ") " "))
  11101. (change-plist (list :type 'todo-state-change :from this :to org-state
  11102. :position startpos))
  11103. dolog now-done-p)
  11104. (when org-blocker-hook
  11105. (setq org-last-todo-state-is-todo
  11106. (not (member this org-done-keywords)))
  11107. (unless (save-excursion
  11108. (save-match-data
  11109. (org-with-wide-buffer
  11110. (run-hook-with-args-until-failure
  11111. 'org-blocker-hook change-plist))))
  11112. (if (org-called-interactively-p 'interactive)
  11113. (user-error "TODO state change from %s to %s blocked (by \"%s\")"
  11114. this org-state org-block-entry-blocking)
  11115. ;; fail silently
  11116. (message "TODO state change from %s to %s blocked (by \"%s\")"
  11117. this org-state org-block-entry-blocking)
  11118. (throw 'exit nil))))
  11119. (store-match-data match-data)
  11120. (replace-match next t t)
  11121. (unless (pos-visible-in-window-p hl-pos)
  11122. (message "TODO state changed to %s" (org-trim next)))
  11123. (unless head
  11124. (setq head (org-get-todo-sequence-head org-state)
  11125. ass (assoc head org-todo-kwd-alist)
  11126. interpret (nth 1 ass)
  11127. done-word (nth 3 ass)
  11128. final-done-word (nth 4 ass)))
  11129. (when (memq arg '(nextset previousset))
  11130. (message "Keyword-Set %d/%d: %s"
  11131. (- (length org-todo-sets) -1
  11132. (length (memq (assoc org-state org-todo-sets) org-todo-sets)))
  11133. (length org-todo-sets)
  11134. (mapconcat 'identity (assoc org-state org-todo-sets) " ")))
  11135. (setq org-last-todo-state-is-todo
  11136. (not (member org-state org-done-keywords)))
  11137. (setq now-done-p (and (member org-state org-done-keywords)
  11138. (not (member this org-done-keywords))))
  11139. (and logging (org-local-logging logging))
  11140. (when (and (or org-todo-log-states org-log-done)
  11141. (not (eq org-inhibit-logging t))
  11142. (not (memq arg '(nextset previousset))))
  11143. ;; we need to look at recording a time and note
  11144. (setq dolog (or (nth 1 (assoc org-state org-todo-log-states))
  11145. (nth 2 (assoc this org-todo-log-states))))
  11146. (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
  11147. (setq dolog 'time))
  11148. (when (or (and (not org-state) (not org-closed-keep-when-no-todo))
  11149. (and org-state
  11150. (member org-state org-not-done-keywords)
  11151. (not (member this org-not-done-keywords))))
  11152. ;; This is now a todo state and was not one before
  11153. ;; If there was a CLOSED time stamp, get rid of it.
  11154. (org-add-planning-info nil nil 'closed))
  11155. (when (and now-done-p org-log-done)
  11156. ;; It is now done, and it was not done before
  11157. (org-add-planning-info 'closed (org-current-effective-time))
  11158. (if (and (not dolog) (eq 'note org-log-done))
  11159. (org-add-log-setup 'done org-state this 'findpos 'note)))
  11160. (when (and org-state dolog)
  11161. ;; This is a non-nil state, and we need to log it
  11162. (org-add-log-setup 'state org-state this 'findpos dolog)))
  11163. ;; Fixup tag positioning
  11164. (org-todo-trigger-tag-changes org-state)
  11165. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  11166. (when org-provide-todo-statistics
  11167. (org-update-parent-todo-statistics))
  11168. (run-hooks 'org-after-todo-state-change-hook)
  11169. (if (and arg (not (member org-state org-done-keywords)))
  11170. (setq head (org-get-todo-sequence-head org-state)))
  11171. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  11172. ;; Do we need to trigger a repeat?
  11173. (when now-done-p
  11174. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  11175. ;; This is for the agenda, take a snapshot of the headline.
  11176. (save-match-data
  11177. (setq org-agenda-headline-snapshot-before-repeat
  11178. (org-get-heading))))
  11179. (org-auto-repeat-maybe org-state))
  11180. ;; Fixup cursor location if close to the keyword
  11181. (if (and (outline-on-heading-p)
  11182. (not (bolp))
  11183. (save-excursion (beginning-of-line 1)
  11184. (looking-at org-todo-line-regexp))
  11185. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  11186. (progn
  11187. (goto-char (or (match-end 2) (match-end 1)))
  11188. (and (looking-at " ") (just-one-space))))
  11189. (when org-trigger-hook
  11190. (save-excursion
  11191. (run-hook-with-args 'org-trigger-hook change-plist)))
  11192. (when commentp (org-toggle-comment))))))))
  11193. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  11194. "Block turning an entry into a TODO, using the hierarchy.
  11195. This checks whether the current task should be blocked from state
  11196. changes. Such blocking occurs when:
  11197. 1. The task has children which are not all in a completed state.
  11198. 2. A task has a parent with the property :ORDERED:, and there
  11199. are siblings prior to the current task with incomplete
  11200. status.
  11201. 3. The parent of the task is blocked because it has siblings that should
  11202. be done first, or is child of a block grandparent TODO entry."
  11203. (if (not org-enforce-todo-dependencies)
  11204. t ; if locally turned off don't block
  11205. (catch 'dont-block
  11206. ;; If this is not a todo state change, or if this entry is already DONE,
  11207. ;; do not block
  11208. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  11209. (member (plist-get change-plist :from)
  11210. (cons 'done org-done-keywords))
  11211. (member (plist-get change-plist :to)
  11212. (cons 'todo org-not-done-keywords))
  11213. (not (plist-get change-plist :to)))
  11214. (throw 'dont-block t))
  11215. ;; If this task has children, and any are undone, it's blocked
  11216. (save-excursion
  11217. (org-back-to-heading t)
  11218. (let ((this-level (funcall outline-level)))
  11219. (outline-next-heading)
  11220. (let ((child-level (funcall outline-level)))
  11221. (while (and (not (eobp))
  11222. (> child-level this-level))
  11223. ;; this todo has children, check whether they are all
  11224. ;; completed
  11225. (if (and (not (org-entry-is-done-p))
  11226. (org-entry-is-todo-p))
  11227. (progn (setq org-block-entry-blocking (org-get-heading))
  11228. (throw 'dont-block nil)))
  11229. (outline-next-heading)
  11230. (setq child-level (funcall outline-level))))))
  11231. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  11232. ;; any previous siblings are undone, it's blocked
  11233. (save-excursion
  11234. (org-back-to-heading t)
  11235. (let* ((pos (point))
  11236. (parent-pos (and (org-up-heading-safe) (point))))
  11237. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  11238. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  11239. (forward-line 1)
  11240. (re-search-forward org-not-done-heading-regexp pos t))
  11241. (setq org-block-entry-blocking (match-string 0))
  11242. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  11243. ;; Search further up the hierarchy, to see if an ancestor is blocked
  11244. (while t
  11245. (goto-char parent-pos)
  11246. (if (not (looking-at org-not-done-heading-regexp))
  11247. (throw 'dont-block t)) ; do not block, parent is not a TODO
  11248. (setq pos (point))
  11249. (setq parent-pos (and (org-up-heading-safe) (point)))
  11250. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  11251. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  11252. (forward-line 1)
  11253. (re-search-forward org-not-done-heading-regexp pos t)
  11254. (setq org-block-entry-blocking (org-get-heading)))
  11255. (throw 'dont-block nil)))))))) ; block, older sibling not done.
  11256. (defcustom org-track-ordered-property-with-tag nil
  11257. "Should the ORDERED property also be shown as a tag?
  11258. The ORDERED property decides if an entry should require subtasks to be
  11259. completed in sequence. Since a property is not very visible, setting
  11260. this option means that toggling the ORDERED property with the command
  11261. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  11262. not relevant for the behavior, but it makes things more visible.
  11263. Note that toggling the tag with tags commands will not change the property
  11264. and therefore not influence behavior!
  11265. This can be t, meaning the tag ORDERED should be used, It can also be a
  11266. string to select a different tag for this task."
  11267. :group 'org-todo
  11268. :type '(choice
  11269. (const :tag "No tracking" nil)
  11270. (const :tag "Track with ORDERED tag" t)
  11271. (string :tag "Use other tag")))
  11272. (defun org-toggle-ordered-property ()
  11273. "Toggle the ORDERED property of the current entry.
  11274. For better visibility, you can track the value of this property with a tag.
  11275. See variable `org-track-ordered-property-with-tag'."
  11276. (interactive)
  11277. (let* ((t1 org-track-ordered-property-with-tag)
  11278. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  11279. (save-excursion
  11280. (org-back-to-heading)
  11281. (if (org-entry-get nil "ORDERED")
  11282. (progn
  11283. (org-delete-property "ORDERED" "PROPERTIES")
  11284. (and tag (org-toggle-tag tag 'off))
  11285. (message "Subtasks can be completed in arbitrary order"))
  11286. (org-entry-put nil "ORDERED" "t")
  11287. (and tag (org-toggle-tag tag 'on))
  11288. (message "Subtasks must be completed in sequence")))))
  11289. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  11290. (defun org-block-todo-from-checkboxes (change-plist)
  11291. "Block turning an entry into a TODO, using checkboxes.
  11292. This checks whether the current task should be blocked from state
  11293. changes because there are unchecked boxes in this entry."
  11294. (if (not org-enforce-todo-checkbox-dependencies)
  11295. t ; if locally turned off don't block
  11296. (catch 'dont-block
  11297. ;; If this is not a todo state change, or if this entry is already DONE,
  11298. ;; do not block
  11299. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  11300. (member (plist-get change-plist :from)
  11301. (cons 'done org-done-keywords))
  11302. (member (plist-get change-plist :to)
  11303. (cons 'todo org-not-done-keywords))
  11304. (not (plist-get change-plist :to)))
  11305. (throw 'dont-block t))
  11306. ;; If this task has checkboxes that are not checked, it's blocked
  11307. (save-excursion
  11308. (org-back-to-heading t)
  11309. (let ((beg (point)) end)
  11310. (outline-next-heading)
  11311. (setq end (point))
  11312. (goto-char beg)
  11313. (if (org-list-search-forward
  11314. (concat (org-item-beginning-re)
  11315. "\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?"
  11316. "\\[[- ]\\]")
  11317. end t)
  11318. (progn
  11319. (if (boundp 'org-blocked-by-checkboxes)
  11320. (setq org-blocked-by-checkboxes t))
  11321. (throw 'dont-block nil)))))
  11322. t))) ; do not block
  11323. (defun org-entry-blocked-p ()
  11324. "Is the current entry blocked?"
  11325. (org-with-silent-modifications
  11326. (if (org-entry-get nil "NOBLOCKING")
  11327. nil ;; Never block this entry
  11328. (not (run-hook-with-args-until-failure
  11329. 'org-blocker-hook
  11330. (list :type 'todo-state-change
  11331. :position (point)
  11332. :from 'todo
  11333. :to 'done))))))
  11334. (defun org-update-statistics-cookies (all)
  11335. "Update the statistics cookie, either from TODO or from checkboxes.
  11336. This should be called with the cursor in a line with a statistics cookie."
  11337. (interactive "P")
  11338. (if all
  11339. (progn
  11340. (org-update-checkbox-count 'all)
  11341. (org-map-entries 'org-update-parent-todo-statistics))
  11342. (if (not (org-at-heading-p))
  11343. (org-update-checkbox-count)
  11344. (let ((pos (point-marker))
  11345. end l1 l2)
  11346. (ignore-errors (org-back-to-heading t))
  11347. (if (not (org-at-heading-p))
  11348. (org-update-checkbox-count)
  11349. (setq l1 (org-outline-level))
  11350. (setq end (save-excursion
  11351. (outline-next-heading)
  11352. (if (org-at-heading-p) (setq l2 (org-outline-level)))
  11353. (point)))
  11354. (if (and (save-excursion
  11355. (re-search-forward
  11356. "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) \\[[- X]\\]" end t))
  11357. (not (save-excursion (re-search-forward
  11358. ":COOKIE_DATA:.*\\<todo\\>" end t))))
  11359. (org-update-checkbox-count)
  11360. (if (and l2 (> l2 l1))
  11361. (progn
  11362. (goto-char end)
  11363. (org-update-parent-todo-statistics))
  11364. (goto-char pos)
  11365. (beginning-of-line 1)
  11366. (while (re-search-forward
  11367. "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)"
  11368. (point-at-eol) t)
  11369. (replace-match (if (match-end 2) "[100%]" "[0/0]") t t)))))
  11370. (goto-char pos)
  11371. (move-marker pos nil)))))
  11372. (defvar org-entry-property-inherited-from) ;; defined below
  11373. (defun org-update-parent-todo-statistics ()
  11374. "Update any statistics cookie in the parent of the current headline.
  11375. When `org-hierarchical-todo-statistics' is nil, statistics will cover
  11376. the entire subtree and this will travel up the hierarchy and update
  11377. statistics everywhere."
  11378. (let* ((prop (save-excursion (org-up-heading-safe)
  11379. (org-entry-get nil "COOKIE_DATA" 'inherit)))
  11380. (recursive (or (not org-hierarchical-todo-statistics)
  11381. (and prop (string-match "\\<recursive\\>" prop))))
  11382. (lim (or (and prop (marker-position org-entry-property-inherited-from))
  11383. 0))
  11384. (first t)
  11385. (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  11386. level ltoggle l1 new ndel
  11387. (cnt-all 0) (cnt-done 0) is-percent kwd
  11388. checkbox-beg ov ovs ove cookie-present)
  11389. (catch 'exit
  11390. (save-excursion
  11391. (beginning-of-line 1)
  11392. (setq ltoggle (funcall outline-level))
  11393. ;; Three situations are to consider:
  11394. ;; 1. if `org-hierarchical-todo-statistics' is nil, repeat up
  11395. ;; to the top-level ancestor on the headline;
  11396. ;; 2. If parent has "recursive" property, repeat up to the
  11397. ;; headline setting that property, taking inheritance into
  11398. ;; account;
  11399. ;; 3. Else, move up to direct parent and proceed only once.
  11400. (while (and (setq level (org-up-heading-safe))
  11401. (or recursive first)
  11402. (>= (point) lim))
  11403. (setq first nil cookie-present nil)
  11404. (unless (and level
  11405. (not (string-match
  11406. "\\<checkbox\\>"
  11407. (downcase (or (org-entry-get nil "COOKIE_DATA")
  11408. "")))))
  11409. (throw 'exit nil))
  11410. (while (re-search-forward box-re (point-at-eol) t)
  11411. (setq cnt-all 0 cnt-done 0 cookie-present t)
  11412. (setq is-percent (match-end 2) checkbox-beg (match-beginning 0))
  11413. (save-match-data
  11414. (unless (outline-next-heading) (throw 'exit nil))
  11415. (while (and (looking-at org-complex-heading-regexp)
  11416. (> (setq l1 (length (match-string 1))) level))
  11417. (setq kwd (and (or recursive (= l1 ltoggle))
  11418. (match-string 2)))
  11419. (if (or (eq org-provide-todo-statistics 'all-headlines)
  11420. (and (listp org-provide-todo-statistics)
  11421. (or (member kwd org-provide-todo-statistics)
  11422. (member kwd org-done-keywords))))
  11423. (setq cnt-all (1+ cnt-all))
  11424. (if (eq org-provide-todo-statistics t)
  11425. (and kwd (setq cnt-all (1+ cnt-all)))))
  11426. (and (member kwd org-done-keywords)
  11427. (setq cnt-done (1+ cnt-done)))
  11428. (outline-next-heading)))
  11429. (setq new
  11430. (if is-percent
  11431. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  11432. (format "[%d/%d]" cnt-done cnt-all))
  11433. ndel (- (match-end 0) checkbox-beg))
  11434. ;; handle overlays when updating cookie from column view
  11435. (when (setq ov (car (overlays-at checkbox-beg)))
  11436. (setq ovs (overlay-start ov) ove (overlay-end ov))
  11437. (delete-overlay ov))
  11438. (goto-char checkbox-beg)
  11439. (insert new)
  11440. (delete-region (point) (+ (point) ndel))
  11441. (when org-auto-align-tags (org-fix-tags-on-the-fly))
  11442. (when ov (move-overlay ov ovs ove)))
  11443. (when cookie-present
  11444. (run-hook-with-args 'org-after-todo-statistics-hook
  11445. cnt-done (- cnt-all cnt-done))))))
  11446. (run-hooks 'org-todo-statistics-hook)))
  11447. (defvar org-after-todo-statistics-hook nil
  11448. "Hook that is called after a TODO statistics cookie has been updated.
  11449. Each function is called with two arguments: the number of not-done entries
  11450. and the number of done entries.
  11451. For example, the following function, when added to this hook, will switch
  11452. an entry to DONE when all children are done, and back to TODO when new
  11453. entries are set to a TODO status. Note that this hook is only called
  11454. when there is a statistics cookie in the headline!
  11455. (defun org-summary-todo (n-done n-not-done)
  11456. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  11457. (let (org-log-done org-log-states) ; turn off logging
  11458. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  11459. ")
  11460. (defvar org-todo-statistics-hook nil
  11461. "Hook that is run whenever Org thinks TODO statistics should be updated.
  11462. This hook runs even if there is no statistics cookie present, in which case
  11463. `org-after-todo-statistics-hook' would not run.")
  11464. (defun org-todo-trigger-tag-changes (state)
  11465. "Apply the changes defined in `org-todo-state-tags-triggers'."
  11466. (let ((l org-todo-state-tags-triggers)
  11467. changes)
  11468. (when (or (not state) (equal state ""))
  11469. (setq changes (append changes (cdr (assoc "" l)))))
  11470. (when (and (stringp state) (> (length state) 0))
  11471. (setq changes (append changes (cdr (assoc state l)))))
  11472. (when (member state org-not-done-keywords)
  11473. (setq changes (append changes (cdr (assoc 'todo l)))))
  11474. (when (member state org-done-keywords)
  11475. (setq changes (append changes (cdr (assoc 'done l)))))
  11476. (dolist (c changes)
  11477. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  11478. (defun org-local-logging (value)
  11479. "Get logging settings from a property VALUE."
  11480. (let* (words w a)
  11481. ;; directly set the variables, they are already local.
  11482. (setq org-log-done nil
  11483. org-log-repeat nil
  11484. org-todo-log-states nil)
  11485. (setq words (org-split-string value))
  11486. (while (setq w (pop words))
  11487. (cond
  11488. ((setq a (assoc w org-startup-options))
  11489. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  11490. (set (nth 1 a) (nth 2 a))))
  11491. ((setq a (org-extract-log-state-settings w))
  11492. (and (member (car a) org-todo-keywords-1)
  11493. (push a org-todo-log-states)))))))
  11494. (defun org-get-todo-sequence-head (kwd)
  11495. "Return the head of the TODO sequence to which KWD belongs.
  11496. If KWD is not set, check if there is a text property remembering the
  11497. right sequence."
  11498. (let (p)
  11499. (cond
  11500. ((not kwd)
  11501. (or (get-text-property (point-at-bol) 'org-todo-head)
  11502. (progn
  11503. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  11504. nil (point-at-eol)))
  11505. (get-text-property p 'org-todo-head))))
  11506. ((not (member kwd org-todo-keywords-1))
  11507. (car org-todo-keywords-1))
  11508. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  11509. (defun org-fast-todo-selection ()
  11510. "Fast TODO keyword selection with single keys.
  11511. Returns the new TODO keyword, or nil if no state change should occur."
  11512. (let* ((fulltable org-todo-key-alist)
  11513. (done-keywords org-done-keywords) ;; needed for the faces.
  11514. (maxlen (apply 'max (mapcar
  11515. (lambda (x)
  11516. (if (stringp (car x)) (string-width (car x)) 0))
  11517. fulltable)))
  11518. (expert nil)
  11519. (fwidth (+ maxlen 3 1 3))
  11520. (ncol (/ (- (window-width) 4) fwidth))
  11521. tg cnt e c tbl
  11522. groups ingroup)
  11523. (save-excursion
  11524. (save-window-excursion
  11525. (if expert
  11526. (set-buffer (get-buffer-create " *Org todo*"))
  11527. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  11528. (erase-buffer)
  11529. (org-set-local 'org-done-keywords done-keywords)
  11530. (setq tbl fulltable cnt 0)
  11531. (while (setq e (pop tbl))
  11532. (cond
  11533. ((equal e '(:startgroup))
  11534. (push '() groups) (setq ingroup t)
  11535. (when (not (= cnt 0))
  11536. (setq cnt 0)
  11537. (insert "\n"))
  11538. (insert "{ "))
  11539. ((equal e '(:endgroup))
  11540. (setq ingroup nil cnt 0)
  11541. (insert "}\n"))
  11542. ((equal e '(:newline))
  11543. (when (not (= cnt 0))
  11544. (setq cnt 0)
  11545. (insert "\n")
  11546. (setq e (car tbl))
  11547. (while (equal (car tbl) '(:newline))
  11548. (insert "\n")
  11549. (setq tbl (cdr tbl)))))
  11550. (t
  11551. (setq tg (car e) c (cdr e))
  11552. (if ingroup (push tg (car groups)))
  11553. (setq tg (org-add-props tg nil 'face
  11554. (org-get-todo-face tg)))
  11555. (if (and (= cnt 0) (not ingroup)) (insert " "))
  11556. (insert "[" c "] " tg (make-string
  11557. (- fwidth 4 (length tg)) ?\ ))
  11558. (when (= (setq cnt (1+ cnt)) ncol)
  11559. (insert "\n")
  11560. (if ingroup (insert " "))
  11561. (setq cnt 0)))))
  11562. (insert "\n")
  11563. (goto-char (point-min))
  11564. (if (not expert) (org-fit-window-to-buffer))
  11565. (message "[a-z..]:Set [SPC]:clear")
  11566. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  11567. (cond
  11568. ((or (= c ?\C-g)
  11569. (and (= c ?q) (not (rassoc c fulltable))))
  11570. (setq quit-flag t))
  11571. ((= c ?\ ) nil)
  11572. ((setq e (rassoc c fulltable) tg (car e))
  11573. tg)
  11574. (t (setq quit-flag t)))))))
  11575. (defun org-entry-is-todo-p ()
  11576. (member (org-get-todo-state) org-not-done-keywords))
  11577. (defun org-entry-is-done-p ()
  11578. (member (org-get-todo-state) org-done-keywords))
  11579. (defun org-get-todo-state ()
  11580. "Return the TODO keyword of the current subtree."
  11581. (save-excursion
  11582. (org-back-to-heading t)
  11583. (and (looking-at org-todo-line-regexp)
  11584. (match-end 2)
  11585. (match-string 2))))
  11586. (defun org-at-date-range-p (&optional inactive-ok)
  11587. "Is the cursor inside a date range?"
  11588. (interactive)
  11589. (save-excursion
  11590. (catch 'exit
  11591. (let ((pos (point)))
  11592. (skip-chars-backward "^[<\r\n")
  11593. (skip-chars-backward "<[")
  11594. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  11595. (>= (match-end 0) pos)
  11596. (throw 'exit t))
  11597. (skip-chars-backward "^<[\r\n")
  11598. (skip-chars-backward "<[")
  11599. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  11600. (>= (match-end 0) pos)
  11601. (throw 'exit t)))
  11602. nil)))
  11603. (defun org-get-repeat (&optional tagline)
  11604. "Check if there is a deadline/schedule with repeater in this entry."
  11605. (save-match-data
  11606. (save-excursion
  11607. (org-back-to-heading t)
  11608. (and (re-search-forward (if tagline
  11609. (concat tagline "\\s-*" org-repeat-re)
  11610. org-repeat-re)
  11611. (org-entry-end-position) t)
  11612. (match-string-no-properties 1)))))
  11613. (defvar org-last-changed-timestamp)
  11614. (defvar org-last-inserted-timestamp)
  11615. (defvar org-log-post-message)
  11616. (defvar org-log-note-purpose)
  11617. (defvar org-log-note-how)
  11618. (defvar org-log-note-extra)
  11619. (defun org-auto-repeat-maybe (done-word)
  11620. "Check if the current headline contains a repeated deadline/schedule.
  11621. If yes, set TODO state back to what it was and change the base date
  11622. of repeating deadline/scheduled time stamps to new date.
  11623. This function is run automatically after each state change to a DONE state."
  11624. ;; last-state is dynamically scoped into this function
  11625. (let* ((repeat (org-get-repeat))
  11626. (aa (assoc org-last-state org-todo-kwd-alist))
  11627. (interpret (nth 1 aa))
  11628. (head (nth 2 aa))
  11629. (whata '(("h" . hour) ("d" . day) ("m" . month) ("y" . year)))
  11630. (msg "Entry repeats: ")
  11631. (org-log-done nil)
  11632. (org-todo-log-states nil)
  11633. re type n what ts time to-state)
  11634. (when repeat
  11635. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  11636. (setq to-state (or (org-entry-get nil "REPEAT_TO_STATE")
  11637. org-todo-repeat-to-state))
  11638. (unless (and to-state (member to-state org-todo-keywords-1))
  11639. (setq to-state (if (eq interpret 'type) org-last-state head)))
  11640. (org-todo to-state)
  11641. (when (or org-log-repeat (org-entry-get nil "CLOCK"))
  11642. (org-entry-put nil "LAST_REPEAT" (format-time-string
  11643. (org-time-stamp-format t t))))
  11644. (when org-log-repeat
  11645. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  11646. (memq 'org-add-log-note post-command-hook))
  11647. ;; OK, we are already setup for some record
  11648. (if (eq org-log-repeat 'note)
  11649. ;; make sure we take a note, not only a time stamp
  11650. (setq org-log-note-how 'note))
  11651. ;; Set up for taking a record
  11652. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  11653. org-last-state
  11654. 'findpos org-log-repeat)))
  11655. (org-back-to-heading t)
  11656. (org-add-planning-info nil nil 'closed)
  11657. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  11658. org-deadline-time-regexp "\\)\\|\\("
  11659. org-ts-regexp "\\)"))
  11660. (while (re-search-forward
  11661. re (save-excursion (outline-next-heading) (point)) t)
  11662. (setq type (if (match-end 1) org-scheduled-string
  11663. (if (match-end 3) org-deadline-string "Plain:"))
  11664. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  11665. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts)
  11666. (setq n (string-to-number (match-string 2 ts))
  11667. what (match-string 3 ts))
  11668. (if (equal what "w") (setq n (* n 7) what "d"))
  11669. (if (and (equal what "h") (not (string-match "[0-9]\\{1,2\\}:[0-9]\\{2\\}" ts)))
  11670. (user-error "Cannot repeat in Repeat in %d hour(s) because no hour has been set" n))
  11671. ;; Preparation, see if we need to modify the start date for the change
  11672. (when (match-end 1)
  11673. (setq time (save-match-data (org-time-string-to-time ts)))
  11674. (cond
  11675. ((equal (match-string 1 ts) ".")
  11676. ;; Shift starting date to today
  11677. (org-timestamp-change
  11678. (- (org-today) (time-to-days time))
  11679. 'day))
  11680. ((equal (match-string 1 ts) "+")
  11681. (let ((nshiftmax 10) (nshift 0))
  11682. (while (or (= nshift 0)
  11683. (<= (time-to-days time)
  11684. (time-to-days (current-time))))
  11685. (when (= (incf nshift) nshiftmax)
  11686. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  11687. (error "Abort")))
  11688. (org-timestamp-change n (cdr (assoc what whata)))
  11689. (org-at-timestamp-p t)
  11690. (setq ts (match-string 1))
  11691. (setq time (save-match-data (org-time-string-to-time ts)))))
  11692. (org-timestamp-change (- n) (cdr (assoc what whata)))
  11693. ;; rematch, so that we have everything in place for the real shift
  11694. (org-at-timestamp-p t)
  11695. (setq ts (match-string 1))
  11696. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts))))
  11697. (save-excursion (org-timestamp-change n (cdr (assoc what whata)) nil t))
  11698. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  11699. (setq org-log-post-message msg)
  11700. (message "%s" msg))))
  11701. (defun org-show-todo-tree (arg)
  11702. "Make a compact tree which shows all headlines marked with TODO.
  11703. The tree will show the lines where the regexp matches, and all higher
  11704. headlines above the match.
  11705. With a \\[universal-argument] prefix, prompt for a regexp to match.
  11706. With a numeric prefix N, construct a sparse tree for the Nth element
  11707. of `org-todo-keywords-1'."
  11708. (interactive "P")
  11709. (let ((case-fold-search nil)
  11710. (kwd-re
  11711. (cond ((null arg) org-not-done-regexp)
  11712. ((equal arg '(4))
  11713. (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
  11714. (mapcar 'list org-todo-keywords-1))))
  11715. (concat "\\("
  11716. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  11717. "\\)\\>")))
  11718. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  11719. (regexp-quote (nth (1- (prefix-numeric-value arg))
  11720. org-todo-keywords-1)))
  11721. (t (user-error "Invalid prefix argument: %s" arg)))))
  11722. (message "%d TODO entries found"
  11723. (org-occur (concat "^" org-outline-regexp " *" kwd-re )))))
  11724. (defun org-deadline (arg &optional time)
  11725. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  11726. With one universal prefix argument, remove any deadline from the item.
  11727. With two universal prefix arguments, prompt for a warning delay.
  11728. With argument TIME, set the deadline at the corresponding date. TIME
  11729. can either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  11730. (interactive "P")
  11731. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  11732. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  11733. 'region-start-level 'region))
  11734. org-loop-over-headlines-in-active-region)
  11735. (org-map-entries
  11736. `(org-deadline ',arg ,time)
  11737. org-loop-over-headlines-in-active-region
  11738. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  11739. (let* ((old-date (org-entry-get nil "DEADLINE"))
  11740. (old-date-time (if old-date (org-time-string-to-time old-date)))
  11741. (repeater (and old-date
  11742. (string-match
  11743. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  11744. old-date)
  11745. (match-string 1 old-date))))
  11746. (cond
  11747. ((equal arg '(4))
  11748. (when (and old-date org-log-redeadline)
  11749. (org-add-log-setup 'deldeadline nil old-date 'findpos
  11750. org-log-redeadline))
  11751. (org-remove-timestamp-with-keyword org-deadline-string)
  11752. (message "Item no longer has a deadline."))
  11753. ((equal arg '(16))
  11754. (save-excursion
  11755. (if (re-search-forward
  11756. org-deadline-time-regexp
  11757. (save-excursion (outline-next-heading) (point)) t)
  11758. (let* ((rpl0 (match-string 1))
  11759. (rpl (replace-regexp-in-string " -[0-9]+[hdwmy]" "" rpl0)))
  11760. (replace-match
  11761. (concat org-deadline-string
  11762. " <" rpl
  11763. (format " -%dd"
  11764. (abs
  11765. (- (time-to-days
  11766. (save-match-data
  11767. (org-read-date nil t nil "Warn starting from" old-date-time)))
  11768. (time-to-days old-date-time))))
  11769. ">") t t))
  11770. (user-error "No deadline information to update"))))
  11771. (t
  11772. (org-add-planning-info 'deadline time 'closed)
  11773. (when (and old-date org-log-redeadline
  11774. (not (equal old-date
  11775. (substring org-last-inserted-timestamp 1 -1))))
  11776. (org-add-log-setup 'redeadline nil old-date 'findpos
  11777. org-log-redeadline))
  11778. (when repeater
  11779. (save-excursion
  11780. (org-back-to-heading t)
  11781. (when (re-search-forward (concat org-deadline-string " "
  11782. org-last-inserted-timestamp)
  11783. (save-excursion
  11784. (outline-next-heading) (point)) t)
  11785. (goto-char (1- (match-end 0)))
  11786. (insert " " repeater)
  11787. (setq org-last-inserted-timestamp
  11788. (concat (substring org-last-inserted-timestamp 0 -1)
  11789. " " repeater
  11790. (substring org-last-inserted-timestamp -1))))))
  11791. (message "Deadline on %s" org-last-inserted-timestamp))))))
  11792. (defun org-schedule (arg &optional time)
  11793. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  11794. With one universal prefix argument, remove any scheduling date from the item.
  11795. With two universal prefix arguments, prompt for a delay cookie.
  11796. With argument TIME, scheduled at the corresponding date. TIME can
  11797. either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  11798. (interactive "P")
  11799. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  11800. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  11801. 'region-start-level 'region))
  11802. org-loop-over-headlines-in-active-region)
  11803. (org-map-entries
  11804. `(org-schedule ',arg ,time)
  11805. org-loop-over-headlines-in-active-region
  11806. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  11807. (let* ((old-date (org-entry-get nil "SCHEDULED"))
  11808. (old-date-time (if old-date (org-time-string-to-time old-date)))
  11809. (repeater (and old-date
  11810. (string-match
  11811. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  11812. old-date)
  11813. (match-string 1 old-date))))
  11814. (cond
  11815. ((equal arg '(4))
  11816. (progn
  11817. (when (and old-date org-log-reschedule)
  11818. (org-add-log-setup 'delschedule nil old-date 'findpos
  11819. org-log-reschedule))
  11820. (org-remove-timestamp-with-keyword org-scheduled-string)
  11821. (message "Item is no longer scheduled.")))
  11822. ((equal arg '(16))
  11823. (save-excursion
  11824. (if (re-search-forward
  11825. org-scheduled-time-regexp
  11826. (save-excursion (outline-next-heading) (point)) t)
  11827. (let* ((rpl0 (match-string 1))
  11828. (rpl (replace-regexp-in-string " -[0-9]+[hdwmy]" "" rpl0)))
  11829. (replace-match
  11830. (concat org-scheduled-string
  11831. " <" rpl
  11832. (format " -%dd"
  11833. (abs
  11834. (- (time-to-days
  11835. (save-match-data
  11836. (org-read-date nil t nil "Delay until" old-date-time)))
  11837. (time-to-days old-date-time))))
  11838. ">") t t))
  11839. (user-error "No scheduled information to update"))))
  11840. (t
  11841. (org-add-planning-info 'scheduled time 'closed)
  11842. (when (and old-date org-log-reschedule
  11843. (not (equal old-date
  11844. (substring org-last-inserted-timestamp 1 -1))))
  11845. (org-add-log-setup 'reschedule nil old-date 'findpos
  11846. org-log-reschedule))
  11847. (when repeater
  11848. (save-excursion
  11849. (org-back-to-heading t)
  11850. (when (re-search-forward (concat org-scheduled-string " "
  11851. org-last-inserted-timestamp)
  11852. (save-excursion
  11853. (outline-next-heading) (point)) t)
  11854. (goto-char (1- (match-end 0)))
  11855. (insert " " repeater)
  11856. (setq org-last-inserted-timestamp
  11857. (concat (substring org-last-inserted-timestamp 0 -1)
  11858. " " repeater
  11859. (substring org-last-inserted-timestamp -1))))))
  11860. (message "Scheduled to %s" org-last-inserted-timestamp))))))
  11861. (defun org-get-scheduled-time (pom &optional inherit)
  11862. "Get the scheduled time as a time tuple, of a format suitable
  11863. for calling org-schedule with, or if there is no scheduling,
  11864. returns nil."
  11865. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  11866. (when time
  11867. (apply 'encode-time (org-parse-time-string time)))))
  11868. (defun org-get-deadline-time (pom &optional inherit)
  11869. "Get the deadline as a time tuple, of a format suitable for
  11870. calling org-deadline with, or if there is no scheduling, returns
  11871. nil."
  11872. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  11873. (when time
  11874. (apply 'encode-time (org-parse-time-string time)))))
  11875. (defun org-remove-timestamp-with-keyword (keyword)
  11876. "Remove all time stamps with KEYWORD in the current entry."
  11877. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  11878. beg)
  11879. (save-excursion
  11880. (org-back-to-heading t)
  11881. (setq beg (point))
  11882. (outline-next-heading)
  11883. (while (re-search-backward re beg t)
  11884. (replace-match "")
  11885. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  11886. (equal (char-before) ?\ ))
  11887. (backward-delete-char 1)
  11888. (if (string-match "^[ \t]*$" (buffer-substring
  11889. (point-at-bol) (point-at-eol)))
  11890. (delete-region (point-at-bol)
  11891. (min (point-max) (1+ (point-at-eol))))))))))
  11892. (defun org-add-planning-info (what &optional time &rest remove)
  11893. "Insert new timestamp with keyword in the line directly after the headline.
  11894. WHAT indicates what kind of time stamp to add. TIME indicates the time to use.
  11895. If non is given, the user is prompted for a date.
  11896. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  11897. be removed."
  11898. (interactive)
  11899. (let (org-time-was-given org-end-time-was-given ts
  11900. end default-time default-input)
  11901. (catch 'exit
  11902. (when (and (memq what '(scheduled deadline))
  11903. (or (not time)
  11904. (and (stringp time)
  11905. (string-match "^[-+]+[0-9]" time))))
  11906. ;; Try to get a default date/time from existing timestamp
  11907. (save-excursion
  11908. (org-back-to-heading t)
  11909. (setq end (save-excursion (outline-next-heading) (point)))
  11910. (when (re-search-forward (if (eq what 'scheduled)
  11911. org-scheduled-time-regexp
  11912. org-deadline-time-regexp)
  11913. end t)
  11914. (setq ts (match-string 1)
  11915. default-time
  11916. (apply 'encode-time (org-parse-time-string ts))
  11917. default-input (and ts (org-get-compact-tod ts))))))
  11918. (when what
  11919. (setq time
  11920. (if (stringp time)
  11921. ;; This is a string (relative or absolute), set proper date
  11922. (apply 'encode-time
  11923. (org-read-date-analyze
  11924. time default-time (decode-time default-time)))
  11925. ;; If necessary, get the time from the user
  11926. (or time (org-read-date nil 'to-time nil nil
  11927. default-time default-input)))))
  11928. (when (and org-insert-labeled-timestamps-at-point
  11929. (member what '(scheduled deadline)))
  11930. (insert
  11931. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  11932. (org-insert-time-stamp time org-time-was-given
  11933. nil nil nil (list org-end-time-was-given))
  11934. (setq what nil))
  11935. (save-excursion
  11936. (save-restriction
  11937. (let (col list elt ts buffer-invisibility-spec)
  11938. (org-back-to-heading t)
  11939. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"))
  11940. (goto-char (match-end 1))
  11941. (setq col (current-column))
  11942. (goto-char (match-end 0))
  11943. (if (eobp) (insert "\n") (forward-char 1))
  11944. (when (and (not what)
  11945. (not (looking-at
  11946. (concat "[ \t]*"
  11947. org-keyword-time-not-clock-regexp))))
  11948. ;; Nothing to add, nothing to remove...... :-)
  11949. (throw 'exit nil))
  11950. (if (and (not (looking-at org-outline-regexp))
  11951. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  11952. "[^\r\n]*"))
  11953. (not (equal (match-string 1) org-clock-string)))
  11954. (narrow-to-region (match-beginning 0) (match-end 0))
  11955. (insert-before-markers "\n")
  11956. (backward-char 1)
  11957. (narrow-to-region (point) (point))
  11958. (and org-adapt-indentation (org-indent-to-column col)))
  11959. ;; Check if we have to remove something.
  11960. (setq list (cons what remove))
  11961. (while list
  11962. (setq elt (pop list))
  11963. (when (or (and (eq elt 'scheduled)
  11964. (re-search-forward org-scheduled-time-regexp nil t))
  11965. (and (eq elt 'deadline)
  11966. (re-search-forward org-deadline-time-regexp nil t))
  11967. (and (eq elt 'closed)
  11968. (re-search-forward org-closed-time-regexp nil t)))
  11969. (replace-match "")
  11970. (if (looking-at "--+<[^>]+>") (replace-match ""))))
  11971. (and (looking-at "[ \t]+") (replace-match ""))
  11972. (and org-adapt-indentation (bolp) (org-indent-to-column col))
  11973. (when what
  11974. (insert
  11975. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  11976. (cond ((eq what 'scheduled) org-scheduled-string)
  11977. ((eq what 'deadline) org-deadline-string)
  11978. ((eq what 'closed) org-closed-string))
  11979. " ")
  11980. (setq ts (org-insert-time-stamp
  11981. time
  11982. (or org-time-was-given
  11983. (and (eq what 'closed) org-log-done-with-time))
  11984. (eq what 'closed)
  11985. nil nil (list org-end-time-was-given)))
  11986. (insert
  11987. (if (not (or (bolp) (eq (char-before) ?\ )
  11988. (memq (char-after) '(32 10))
  11989. (eobp))) " " ""))
  11990. (end-of-line 1))
  11991. (goto-char (point-min))
  11992. (widen)
  11993. (if (and (looking-at "[ \t]*\n")
  11994. (equal (char-before) ?\n))
  11995. (delete-region (1- (point)) (point-at-eol)))
  11996. ts))))))
  11997. (defvar org-log-note-marker (make-marker))
  11998. (defvar org-log-note-purpose nil)
  11999. (defvar org-log-note-state nil)
  12000. (defvar org-log-note-previous-state nil)
  12001. (defvar org-log-note-how nil)
  12002. (defvar org-log-note-extra nil)
  12003. (defvar org-log-note-window-configuration nil)
  12004. (defvar org-log-note-return-to (make-marker))
  12005. (defvar org-log-note-effective-time nil
  12006. "Remembered current time so that dynamically scoped
  12007. `org-extend-today-until' affects tha timestamps in state change
  12008. log")
  12009. (defvar org-log-post-message nil
  12010. "Message to be displayed after a log note has been stored.
  12011. The auto-repeater uses this.")
  12012. (defun org-add-note ()
  12013. "Add a note to the current entry.
  12014. This is done in the same way as adding a state change note."
  12015. (interactive)
  12016. (org-add-log-setup 'note nil nil 'findpos nil))
  12017. (defvar org-property-end-re)
  12018. (defun org-add-log-setup (&optional purpose state prev-state
  12019. findpos how extra)
  12020. "Set up the post command hook to take a note.
  12021. If this is about to TODO state change, the new state is expected in STATE.
  12022. When FINDPOS is non-nil, find the correct position for the note in
  12023. the current entry. If not, assume that it can be inserted at point.
  12024. HOW is an indicator what kind of note should be created.
  12025. EXTRA is additional text that will be inserted into the notes buffer."
  12026. (let* ((org-log-into-drawer (org-log-into-drawer))
  12027. (drawer (cond ((stringp org-log-into-drawer)
  12028. org-log-into-drawer)
  12029. (org-log-into-drawer "LOGBOOK"))))
  12030. (save-restriction
  12031. (save-excursion
  12032. (when findpos
  12033. (org-back-to-heading t)
  12034. (narrow-to-region (point) (save-excursion
  12035. (outline-next-heading) (point)))
  12036. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"
  12037. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  12038. "[^\r\n]*\\)?"))
  12039. (goto-char (match-end 0))
  12040. (cond
  12041. (drawer
  12042. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  12043. nil t)
  12044. (progn
  12045. (goto-char (match-end 0))
  12046. (or org-log-states-order-reversed
  12047. (and (re-search-forward org-property-end-re nil t)
  12048. (goto-char (1- (match-beginning 0))))))
  12049. (insert "\n:" drawer ":\n:END:")
  12050. (beginning-of-line 0)
  12051. (org-indent-line)
  12052. (beginning-of-line 2)
  12053. (org-indent-line)
  12054. (end-of-line 0)))
  12055. ((and org-log-state-notes-insert-after-drawers
  12056. (save-excursion
  12057. (forward-line) (looking-at org-drawer-regexp)))
  12058. (forward-line)
  12059. (while (looking-at org-drawer-regexp)
  12060. (goto-char (match-end 0))
  12061. (re-search-forward org-property-end-re (point-max) t)
  12062. (forward-line))
  12063. (forward-line -1)))
  12064. (unless org-log-states-order-reversed
  12065. (and (= (char-after) ?\n) (forward-char 1))
  12066. (org-skip-over-state-notes)
  12067. (skip-chars-backward " \t\n\r")))
  12068. (move-marker org-log-note-marker (point))
  12069. (setq org-log-note-purpose purpose
  12070. org-log-note-state state
  12071. org-log-note-previous-state prev-state
  12072. org-log-note-how how
  12073. org-log-note-extra extra
  12074. org-log-note-effective-time (org-current-effective-time))
  12075. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  12076. (defun org-skip-over-state-notes ()
  12077. "Skip past the list of State notes in an entry."
  12078. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  12079. (when (ignore-errors (goto-char (org-in-item-p)))
  12080. (let* ((struct (org-list-struct))
  12081. (prevs (org-list-prevs-alist struct)))
  12082. (while (looking-at "[ \t]*- State")
  12083. (goto-char (or (org-list-get-next-item (point) struct prevs)
  12084. (org-list-get-item-end (point) struct)))))))
  12085. (defun org-add-log-note (&optional purpose)
  12086. "Pop up a window for taking a note, and add this note later at point."
  12087. (remove-hook 'post-command-hook 'org-add-log-note)
  12088. (setq org-log-note-window-configuration (current-window-configuration))
  12089. (delete-other-windows)
  12090. (move-marker org-log-note-return-to (point))
  12091. (org-pop-to-buffer-same-window (marker-buffer org-log-note-marker))
  12092. (goto-char org-log-note-marker)
  12093. (org-switch-to-buffer-other-window "*Org Note*")
  12094. (erase-buffer)
  12095. (if (memq org-log-note-how '(time state))
  12096. (let (current-prefix-arg) (org-store-log-note))
  12097. (let ((org-inhibit-startup t)) (org-mode))
  12098. (insert (format "# Insert note for %s.
  12099. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  12100. (cond
  12101. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  12102. ((eq org-log-note-purpose 'done) "closed todo item")
  12103. ((eq org-log-note-purpose 'state)
  12104. (format "state change from \"%s\" to \"%s\""
  12105. (or org-log-note-previous-state "")
  12106. (or org-log-note-state "")))
  12107. ((eq org-log-note-purpose 'reschedule)
  12108. "rescheduling")
  12109. ((eq org-log-note-purpose 'delschedule)
  12110. "no longer scheduled")
  12111. ((eq org-log-note-purpose 'redeadline)
  12112. "changing deadline")
  12113. ((eq org-log-note-purpose 'deldeadline)
  12114. "removing deadline")
  12115. ((eq org-log-note-purpose 'refile)
  12116. "refiling")
  12117. ((eq org-log-note-purpose 'note)
  12118. "this entry")
  12119. (t (error "This should not happen")))))
  12120. (if org-log-note-extra (insert org-log-note-extra))
  12121. (org-set-local 'org-finish-function 'org-store-log-note)
  12122. (run-hooks 'org-log-buffer-setup-hook)))
  12123. (defvar org-note-abort nil) ; dynamically scoped
  12124. (defun org-store-log-note ()
  12125. "Finish taking a log note, and insert it to where it belongs."
  12126. (let ((txt (buffer-string)))
  12127. (kill-buffer (current-buffer))
  12128. (let ((note (cdr (assq org-log-note-purpose org-log-note-headings)))
  12129. lines ind bul)
  12130. (while (string-match "\\`# .*\n[ \t\n]*" txt)
  12131. (setq txt (replace-match "" t t txt)))
  12132. (if (string-match "\\s-+\\'" txt)
  12133. (setq txt (replace-match "" t t txt)))
  12134. (setq lines (org-split-string txt "\n"))
  12135. (when (and note (string-match "\\S-" note))
  12136. (setq note
  12137. (org-replace-escapes
  12138. note
  12139. (list (cons "%u" (user-login-name))
  12140. (cons "%U" user-full-name)
  12141. (cons "%t" (format-time-string
  12142. (org-time-stamp-format 'long 'inactive)
  12143. org-log-note-effective-time))
  12144. (cons "%T" (format-time-string
  12145. (org-time-stamp-format 'long nil)
  12146. org-log-note-effective-time))
  12147. (cons "%d" (format-time-string
  12148. (org-time-stamp-format nil 'inactive)
  12149. org-log-note-effective-time))
  12150. (cons "%D" (format-time-string
  12151. (org-time-stamp-format nil nil)
  12152. org-log-note-effective-time))
  12153. (cons "%s" (if org-log-note-state
  12154. (concat "\"" org-log-note-state "\"")
  12155. ""))
  12156. (cons "%S" (if org-log-note-previous-state
  12157. (concat "\"" org-log-note-previous-state "\"")
  12158. "\"\"")))))
  12159. (if lines (setq note (concat note " \\\\")))
  12160. (push note lines))
  12161. (when (or current-prefix-arg org-note-abort)
  12162. (when org-log-into-drawer
  12163. (org-remove-empty-drawer-at
  12164. (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
  12165. org-log-note-marker))
  12166. (setq lines nil))
  12167. (when lines
  12168. (with-current-buffer (marker-buffer org-log-note-marker)
  12169. (save-excursion
  12170. (goto-char org-log-note-marker)
  12171. (move-marker org-log-note-marker nil)
  12172. (end-of-line 1)
  12173. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  12174. (setq ind (save-excursion
  12175. (if (ignore-errors (goto-char (org-in-item-p)))
  12176. (let ((struct (org-list-struct)))
  12177. (org-list-get-ind
  12178. (org-list-get-top-point struct) struct))
  12179. (skip-chars-backward " \r\t\n")
  12180. (cond
  12181. ((and (org-at-heading-p)
  12182. org-adapt-indentation)
  12183. (1+ (org-current-level)))
  12184. ((org-at-heading-p) 0)
  12185. (t (org-get-indentation))))))
  12186. (setq bul (org-list-bullet-string "-"))
  12187. (org-indent-line-to ind)
  12188. (insert bul (pop lines))
  12189. (let ((ind-body (+ (length bul) ind)))
  12190. (while lines
  12191. (insert "\n")
  12192. (org-indent-line-to ind-body)
  12193. (insert (pop lines))))
  12194. (message "Note stored")
  12195. (org-back-to-heading t)
  12196. (org-cycle-hide-drawers 'children))
  12197. ;; Fix `buffer-undo-list' when `org-store-log-note' is called
  12198. ;; from within `org-add-log-note' because `buffer-undo-list'
  12199. ;; is then modified outside of `org-with-remote-undo'.
  12200. (when (eq this-command 'org-agenda-todo)
  12201. (setcdr buffer-undo-list (cddr buffer-undo-list)))))))
  12202. ;; Don't add undo information when called from `org-agenda-todo'
  12203. (let ((buffer-undo-list (eq this-command 'org-agenda-todo)))
  12204. (set-window-configuration org-log-note-window-configuration)
  12205. (with-current-buffer (marker-buffer org-log-note-return-to)
  12206. (goto-char org-log-note-return-to))
  12207. (move-marker org-log-note-return-to nil)
  12208. (and org-log-post-message (message "%s" org-log-post-message))))
  12209. (defun org-remove-empty-drawer-at (drawer pos)
  12210. "Remove an empty drawer DRAWER at position POS.
  12211. POS may also be a marker."
  12212. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  12213. (save-excursion
  12214. (save-restriction
  12215. (widen)
  12216. (goto-char pos)
  12217. (if (org-in-regexp
  12218. (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
  12219. (replace-match ""))))))
  12220. (defvar org-ts-type nil)
  12221. (defun org-sparse-tree (&optional arg type)
  12222. "Create a sparse tree, prompt for the details.
  12223. This command can create sparse trees. You first need to select the type
  12224. of match used to create the tree:
  12225. t Show all TODO entries.
  12226. T Show entries with a specific TODO keyword.
  12227. m Show entries selected by a tags/property match.
  12228. p Enter a property name and its value (both with completion on existing
  12229. names/values) and show entries with that property.
  12230. r Show entries matching a regular expression (`/' can be used as well).
  12231. b Show deadlines and scheduled items before a date.
  12232. a Show deadlines and scheduled items after a date.
  12233. d Show deadlines due within `org-deadline-warning-days'.
  12234. D Show deadlines and scheduled items between a date range."
  12235. (interactive "P")
  12236. (let (ans kwd value ts-type)
  12237. (setq type (or type org-sparse-tree-default-date-type))
  12238. (setq org-ts-type type)
  12239. (message "Sparse tree: [/]regexp [t]odo [T]odo-kwd [m]atch [p]roperty\n [d]eadlines [b]efore-date [a]fter-date [D]ates range\n [c]ycle through date types: %s"
  12240. (cond ((eq type 'all) "all timestamps")
  12241. ((eq type 'scheduled) "only scheduled")
  12242. ((eq type 'deadline) "only deadline")
  12243. ((eq type 'active) "only active timestamps")
  12244. ((eq type 'inactive) "only inactive timestamps")
  12245. ((eq type 'scheduled-or-deadline) "scheduled/deadline")
  12246. ((eq type 'closed) "with a closed time-stamp")
  12247. (t "scheduled/deadline")))
  12248. (setq ans (read-char-exclusive))
  12249. (cond
  12250. ((equal ans ?c)
  12251. (org-sparse-tree
  12252. arg (cadr (member type '(scheduled-or-deadline
  12253. all scheduled deadline active inactive closed)))))
  12254. ((equal ans ?d)
  12255. (call-interactively 'org-check-deadlines))
  12256. ((equal ans ?b)
  12257. (call-interactively 'org-check-before-date))
  12258. ((equal ans ?a)
  12259. (call-interactively 'org-check-after-date))
  12260. ((equal ans ?D)
  12261. (call-interactively 'org-check-dates-range))
  12262. ((equal ans ?t)
  12263. (call-interactively 'org-show-todo-tree))
  12264. ((equal ans ?T)
  12265. (org-show-todo-tree '(4)))
  12266. ((member ans '(?T ?m))
  12267. (call-interactively 'org-match-sparse-tree))
  12268. ((member ans '(?p ?P))
  12269. (setq kwd (org-icompleting-read "Property: "
  12270. (mapcar 'list (org-buffer-property-keys))))
  12271. (setq value (org-icompleting-read "Value: "
  12272. (mapcar 'list (org-property-values kwd))))
  12273. (unless (string-match "\\`{.*}\\'" value)
  12274. (setq value (concat "\"" value "\"")))
  12275. (org-match-sparse-tree arg (concat kwd "=" value)))
  12276. ((member ans '(?r ?R ?/))
  12277. (call-interactively 'org-occur))
  12278. (t (user-error "No such sparse tree command \"%c\"" ans)))))
  12279. (defvar org-occur-highlights nil
  12280. "List of overlays used for occur matches.")
  12281. (make-variable-buffer-local 'org-occur-highlights)
  12282. (defvar org-occur-parameters nil
  12283. "Parameters of the active org-occur calls.
  12284. This is a list, each call to org-occur pushes as cons cell,
  12285. containing the regular expression and the callback, onto the list.
  12286. The list can contain several entries if `org-occur' has been called
  12287. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  12288. will only contain one set of parameters. When the highlights are
  12289. removed (for example with `C-c C-c', or with the next edit (depending
  12290. on `org-remove-highlights-with-change'), this variable is emptied
  12291. as well.")
  12292. (make-variable-buffer-local 'org-occur-parameters)
  12293. (defun org-occur (regexp &optional keep-previous callback)
  12294. "Make a compact tree which shows all matches of REGEXP.
  12295. The tree will show the lines where the regexp matches, and all higher
  12296. headlines above the match. It will also show the heading after the match,
  12297. to make sure editing the matching entry is easy.
  12298. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  12299. call to `org-occur' will be kept, to allow stacking of calls to this
  12300. command.
  12301. If CALLBACK is non-nil, it is a function which is called to confirm
  12302. that the match should indeed be shown."
  12303. (interactive "sRegexp: \nP")
  12304. (when (equal regexp "")
  12305. (user-error "Regexp cannot be empty"))
  12306. (unless keep-previous
  12307. (org-remove-occur-highlights nil nil t))
  12308. (push (cons regexp callback) org-occur-parameters)
  12309. (let ((cnt 0))
  12310. (save-excursion
  12311. (goto-char (point-min))
  12312. (if (or (not keep-previous) ; do not want to keep
  12313. (not org-occur-highlights)) ; no previous matches
  12314. ;; hide everything
  12315. (org-overview))
  12316. (while (re-search-forward regexp nil t)
  12317. (when (or (not callback)
  12318. (save-match-data (funcall callback)))
  12319. (setq cnt (1+ cnt))
  12320. (when org-highlight-sparse-tree-matches
  12321. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  12322. (org-show-context 'occur-tree))))
  12323. (when org-remove-highlights-with-change
  12324. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  12325. nil 'local))
  12326. (unless org-sparse-tree-open-archived-trees
  12327. (org-hide-archived-subtrees (point-min) (point-max)))
  12328. (run-hooks 'org-occur-hook)
  12329. (if (org-called-interactively-p 'interactive)
  12330. (message "%d match(es) for regexp %s" cnt regexp))
  12331. cnt))
  12332. (defun org-occur-next-match (&optional n reset)
  12333. "Function for `next-error-function' to find sparse tree matches.
  12334. N is the number of matches to move, when negative move backwards.
  12335. RESET is entirely ignored - this function always goes back to the
  12336. starting point when no match is found."
  12337. (let* ((limit (if (< n 0) (point-min) (point-max)))
  12338. (search-func (if (< n 0)
  12339. 'previous-single-char-property-change
  12340. 'next-single-char-property-change))
  12341. (n (abs n))
  12342. (pos (point))
  12343. p1)
  12344. (catch 'exit
  12345. (while (setq p1 (funcall search-func (point) 'org-type))
  12346. (when (equal p1 limit)
  12347. (goto-char pos)
  12348. (error "No more matches"))
  12349. (when (equal (get-char-property p1 'org-type) 'org-occur)
  12350. (setq n (1- n))
  12351. (when (= n 0)
  12352. (goto-char p1)
  12353. (throw 'exit (point))))
  12354. (goto-char p1))
  12355. (goto-char p1)
  12356. (error "No more matches"))))
  12357. (defun org-show-context (&optional key)
  12358. "Make sure point and context are visible.
  12359. How much context is shown depends upon the variables
  12360. `org-show-hierarchy-above', `org-show-following-heading',
  12361. `org-show-entry-below' and `org-show-siblings'."
  12362. (let ((heading-p (org-at-heading-p t))
  12363. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  12364. (following-p (org-get-alist-option org-show-following-heading key))
  12365. (entry-p (org-get-alist-option org-show-entry-below key))
  12366. (siblings-p (org-get-alist-option org-show-siblings key)))
  12367. ;; Show heading or entry text
  12368. (if (and heading-p (not entry-p))
  12369. (org-flag-heading nil) ; only show the heading
  12370. (and (or entry-p (outline-invisible-p) (org-invisible-p2))
  12371. (org-show-hidden-entry))) ; show entire entry
  12372. (when following-p
  12373. ;; Show next sibling, or heading below text
  12374. (save-excursion
  12375. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  12376. (org-flag-heading nil))))
  12377. (when siblings-p (org-show-siblings))
  12378. (when hierarchy-p
  12379. ;; show all higher headings, possibly with siblings
  12380. (save-excursion
  12381. (while (and (condition-case nil
  12382. (progn (org-up-heading-all 1) t)
  12383. (error nil))
  12384. (not (bobp)))
  12385. (org-flag-heading nil)
  12386. (when siblings-p (org-show-siblings)))))
  12387. (unless (eq key 'agenda) (org-fix-ellipsis-at-bol))))
  12388. (defvar org-reveal-start-hook nil
  12389. "Hook run before revealing a location.")
  12390. (defun org-reveal (&optional siblings)
  12391. "Show current entry, hierarchy above it, and the following headline.
  12392. This can be used to show a consistent set of context around locations
  12393. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  12394. not t for the search context.
  12395. With optional argument SIBLINGS, on each level of the hierarchy all
  12396. siblings are shown. This repairs the tree structure to what it would
  12397. look like when opened with hierarchical calls to `org-cycle'.
  12398. With double optional argument \\[universal-argument] \\[universal-argument], \
  12399. go to the parent and show the
  12400. entire tree."
  12401. (interactive "P")
  12402. (run-hooks 'org-reveal-start-hook)
  12403. (let ((org-show-hierarchy-above t)
  12404. (org-show-following-heading t)
  12405. (org-show-siblings (if siblings t org-show-siblings)))
  12406. (org-show-context nil))
  12407. (when (equal siblings '(16))
  12408. (save-excursion
  12409. (when (org-up-heading-safe)
  12410. (org-show-subtree)
  12411. (run-hook-with-args 'org-cycle-hook 'subtree)))))
  12412. (defun org-highlight-new-match (beg end)
  12413. "Highlight from BEG to END and mark the highlight is an occur headline."
  12414. (let ((ov (make-overlay beg end)))
  12415. (overlay-put ov 'face 'secondary-selection)
  12416. (overlay-put ov 'org-type 'org-occur)
  12417. (push ov org-occur-highlights)))
  12418. (defun org-remove-occur-highlights (&optional beg end noremove)
  12419. "Remove the occur highlights from the buffer.
  12420. BEG and END are ignored. If NOREMOVE is nil, remove this function
  12421. from the `before-change-functions' in the current buffer."
  12422. (interactive)
  12423. (unless org-inhibit-highlight-removal
  12424. (mapc 'delete-overlay org-occur-highlights)
  12425. (setq org-occur-highlights nil)
  12426. (setq org-occur-parameters nil)
  12427. (unless noremove
  12428. (remove-hook 'before-change-functions
  12429. 'org-remove-occur-highlights 'local))))
  12430. ;;;; Priorities
  12431. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  12432. "Regular expression matching the priority indicator.")
  12433. (defvar org-remove-priority-next-time nil)
  12434. (defun org-priority-up ()
  12435. "Increase the priority of the current item."
  12436. (interactive)
  12437. (org-priority 'up))
  12438. (defun org-priority-down ()
  12439. "Decrease the priority of the current item."
  12440. (interactive)
  12441. (org-priority 'down))
  12442. (defun org-priority (&optional action show)
  12443. "Change the priority of an item.
  12444. ACTION can be `set', `up', `down', or a character."
  12445. (interactive "P")
  12446. (if (equal action '(4))
  12447. (org-show-priority)
  12448. (unless org-enable-priority-commands
  12449. (user-error "Priority commands are disabled"))
  12450. (setq action (or action 'set))
  12451. (let (current new news have remove)
  12452. (save-excursion
  12453. (org-back-to-heading t)
  12454. (if (looking-at org-priority-regexp)
  12455. (setq current (string-to-char (match-string 2))
  12456. have t))
  12457. (cond
  12458. ((eq action 'remove)
  12459. (setq remove t new ?\ ))
  12460. ((or (eq action 'set)
  12461. (if (featurep 'xemacs) (characterp action) (integerp action)))
  12462. (if (not (eq action 'set))
  12463. (setq new action)
  12464. (message "Priority %c-%c, SPC to remove: "
  12465. org-highest-priority org-lowest-priority)
  12466. (save-match-data
  12467. (setq new (read-char-exclusive))))
  12468. (if (and (= (upcase org-highest-priority) org-highest-priority)
  12469. (= (upcase org-lowest-priority) org-lowest-priority))
  12470. (setq new (upcase new)))
  12471. (cond ((equal new ?\ ) (setq remove t))
  12472. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  12473. (user-error "Priority must be between `%c' and `%c'"
  12474. org-highest-priority org-lowest-priority))))
  12475. ((eq action 'up)
  12476. (setq new (if have
  12477. (1- current) ; normal cycling
  12478. ;; last priority was empty
  12479. (if (eq last-command this-command)
  12480. org-lowest-priority ; wrap around empty to lowest
  12481. ;; default
  12482. (if org-priority-start-cycle-with-default
  12483. org-default-priority
  12484. (1- org-default-priority))))))
  12485. ((eq action 'down)
  12486. (setq new (if have
  12487. (1+ current) ; normal cycling
  12488. ;; last priority was empty
  12489. (if (eq last-command this-command)
  12490. org-highest-priority ; wrap around empty to highest
  12491. ;; default
  12492. (if org-priority-start-cycle-with-default
  12493. org-default-priority
  12494. (1+ org-default-priority))))))
  12495. (t (user-error "Invalid action")))
  12496. (if (or (< (upcase new) org-highest-priority)
  12497. (> (upcase new) org-lowest-priority))
  12498. (if (and (memq action '(up down))
  12499. (not have) (not (eq last-command this-command)))
  12500. ;; `new' is from default priority
  12501. (error
  12502. "The default can not be set, see `org-default-priority' why")
  12503. ;; normal cycling: `new' is beyond highest/lowest priority
  12504. ;; and is wrapped around to the empty priority
  12505. (setq remove t)))
  12506. (setq news (format "%c" new))
  12507. (if have
  12508. (if remove
  12509. (replace-match "" t t nil 1)
  12510. (replace-match news t t nil 2))
  12511. (if remove
  12512. (user-error "No priority cookie found in line")
  12513. (let ((case-fold-search nil))
  12514. (looking-at org-todo-line-regexp))
  12515. (if (match-end 2)
  12516. (progn
  12517. (goto-char (match-end 2))
  12518. (insert " [#" news "]"))
  12519. (goto-char (match-beginning 3))
  12520. (insert "[#" news "] "))))
  12521. (org-preserve-lc (org-set-tags nil 'align)))
  12522. (if remove
  12523. (message "Priority removed")
  12524. (message "Priority of current item set to %s" news)))))
  12525. (defun org-show-priority ()
  12526. "Show the priority of the current item.
  12527. This priority is composed of the main priority given with the [#A] cookies,
  12528. and by additional input from the age of a schedules or deadline entry."
  12529. (interactive)
  12530. (let ((pri (if (eq major-mode 'org-agenda-mode)
  12531. (org-get-at-bol 'priority)
  12532. (save-excursion
  12533. (save-match-data
  12534. (beginning-of-line)
  12535. (and (looking-at org-heading-regexp)
  12536. (org-get-priority (match-string 0))))))))
  12537. (message "Priority is %d" (if pri pri -1000))))
  12538. (defun org-get-priority (s)
  12539. "Find priority cookie and return priority."
  12540. (save-match-data
  12541. (if (functionp org-get-priority-function)
  12542. (funcall org-get-priority-function)
  12543. (if (not (string-match org-priority-regexp s))
  12544. (* 1000 (- org-lowest-priority org-default-priority))
  12545. (* 1000 (- org-lowest-priority
  12546. (string-to-char (match-string 2 s))))))))
  12547. ;;;; Tags
  12548. (defvar org-agenda-archives-mode)
  12549. (defvar org-map-continue-from nil
  12550. "Position from where mapping should continue.
  12551. Can be set by the action argument to `org-scan-tags' and `org-map-entries'.")
  12552. (defvar org-scanner-tags nil
  12553. "The current tag list while the tags scanner is running.")
  12554. (defvar org-trust-scanner-tags nil
  12555. "Should `org-get-tags-at' use the tags for the scanner.
  12556. This is for internal dynamical scoping only.
  12557. When this is non-nil, the function `org-get-tags-at' will return the value
  12558. of `org-scanner-tags' instead of building the list by itself. This
  12559. can lead to large speed-ups when the tags scanner is used in a file with
  12560. many entries, and when the list of tags is retrieved, for example to
  12561. obtain a list of properties. Building the tags list for each entry in such
  12562. a file becomes an N^2 operation - but with this variable set, it scales
  12563. as N.")
  12564. (defun org-scan-tags (action matcher todo-only &optional start-level)
  12565. "Sca headline tags with inheritance and produce output ACTION.
  12566. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  12567. or `agenda' to produce an entry list for an agenda view. It can also be
  12568. a Lisp form or a function that should be called at each matched headline, in
  12569. this case the return value is a list of all return values from these calls.
  12570. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  12571. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  12572. only lines with a not-done TODO keyword are included in the output.
  12573. This should be the same variable that was scoped into
  12574. and set by `org-make-tags-matcher' when it constructed MATCHER.
  12575. START-LEVEL can be a string with asterisks, reducing the scope to
  12576. headlines matching this string."
  12577. (require 'org-agenda)
  12578. (let* ((re (concat "^"
  12579. (if start-level
  12580. ;; Get the correct level to match
  12581. (concat "\\*\\{" (number-to-string start-level) "\\} ")
  12582. org-outline-regexp)
  12583. " *\\(\\<\\("
  12584. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  12585. (org-re "\\)\\>\\)? *\\(.*?\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*$")))
  12586. (props (list 'face 'default
  12587. 'done-face 'org-agenda-done
  12588. 'undone-face 'default
  12589. 'mouse-face 'highlight
  12590. 'org-not-done-regexp org-not-done-regexp
  12591. 'org-todo-regexp org-todo-regexp
  12592. 'org-complex-heading-regexp org-complex-heading-regexp
  12593. 'help-echo
  12594. (format "mouse-2 or RET jump to org file %s"
  12595. (abbreviate-file-name
  12596. (or (buffer-file-name (buffer-base-buffer))
  12597. (buffer-name (buffer-base-buffer)))))))
  12598. (case-fold-search nil)
  12599. (org-map-continue-from nil)
  12600. lspos tags tags-list
  12601. (tags-alist (list (cons 0 org-file-tags)))
  12602. (llast 0) rtn rtn1 level category i txt
  12603. todo marker entry priority)
  12604. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  12605. (setq action (list 'lambda nil action)))
  12606. (save-excursion
  12607. (goto-char (point-min))
  12608. (when (eq action 'sparse-tree)
  12609. (org-overview)
  12610. (org-remove-occur-highlights))
  12611. (while (re-search-forward re nil t)
  12612. (setq org-map-continue-from nil)
  12613. (catch :skip
  12614. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  12615. tags (if (match-end 4) (org-match-string-no-properties 4)))
  12616. (goto-char (setq lspos (match-beginning 0)))
  12617. (setq level (org-reduced-level (org-outline-level))
  12618. category (org-get-category))
  12619. (setq i llast llast level)
  12620. ;; remove tag lists from same and sublevels
  12621. (while (>= i level)
  12622. (when (setq entry (assoc i tags-alist))
  12623. (setq tags-alist (delete entry tags-alist)))
  12624. (setq i (1- i)))
  12625. ;; add the next tags
  12626. (when tags
  12627. (setq tags (org-split-string tags ":")
  12628. tags-alist
  12629. (cons (cons level tags) tags-alist)))
  12630. ;; compile tags for current headline
  12631. (setq tags-list
  12632. (if org-use-tag-inheritance
  12633. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  12634. tags)
  12635. org-scanner-tags tags-list)
  12636. (when org-use-tag-inheritance
  12637. (setcdr (car tags-alist)
  12638. (mapcar (lambda (x)
  12639. (setq x (copy-sequence x))
  12640. (org-add-prop-inherited x))
  12641. (cdar tags-alist))))
  12642. (when (and tags org-use-tag-inheritance
  12643. (or (not (eq t org-use-tag-inheritance))
  12644. org-tags-exclude-from-inheritance))
  12645. ;; selective inheritance, remove uninherited ones
  12646. (setcdr (car tags-alist)
  12647. (org-remove-uninherited-tags (cdar tags-alist))))
  12648. (when (and
  12649. ;; eval matcher only when the todo condition is OK
  12650. (and (or (not todo-only) (member todo org-not-done-keywords))
  12651. (let ((case-fold-search t) (org-trust-scanner-tags t))
  12652. (eval matcher)))
  12653. ;; Call the skipper, but return t if it does not skip,
  12654. ;; so that the `and' form continues evaluating
  12655. (progn
  12656. (unless (eq action 'sparse-tree) (org-agenda-skip))
  12657. t)
  12658. ;; Check if timestamps are deselecting this entry
  12659. (or (not todo-only)
  12660. (and (member todo org-not-done-keywords)
  12661. (or (not org-agenda-tags-todo-honor-ignore-options)
  12662. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item))))))
  12663. ;; select this headline
  12664. (cond
  12665. ((eq action 'sparse-tree)
  12666. (and org-highlight-sparse-tree-matches
  12667. (org-get-heading) (match-end 0)
  12668. (org-highlight-new-match
  12669. (match-beginning 1) (match-end 1)))
  12670. (org-show-context 'tags-tree))
  12671. ((eq action 'agenda)
  12672. (setq txt (org-agenda-format-item
  12673. ""
  12674. (concat
  12675. (if (eq org-tags-match-list-sublevels 'indented)
  12676. (make-string (1- level) ?.) "")
  12677. (org-get-heading))
  12678. level category
  12679. tags-list)
  12680. priority (org-get-priority txt))
  12681. (goto-char lspos)
  12682. (setq marker (org-agenda-new-marker))
  12683. (org-add-props txt props
  12684. 'org-marker marker 'org-hd-marker marker 'org-category category
  12685. 'todo-state todo
  12686. 'priority priority 'type "tagsmatch")
  12687. (push txt rtn))
  12688. ((functionp action)
  12689. (setq org-map-continue-from nil)
  12690. (save-excursion
  12691. (setq rtn1 (funcall action))
  12692. (push rtn1 rtn)))
  12693. (t (user-error "Invalid action")))
  12694. ;; if we are to skip sublevels, jump to end of subtree
  12695. (unless org-tags-match-list-sublevels
  12696. (org-end-of-subtree t)
  12697. (backward-char 1))))
  12698. ;; Get the correct position from where to continue
  12699. (if org-map-continue-from
  12700. (goto-char org-map-continue-from)
  12701. (and (= (point) lspos) (end-of-line 1)))))
  12702. (when (and (eq action 'sparse-tree)
  12703. (not org-sparse-tree-open-archived-trees))
  12704. (org-hide-archived-subtrees (point-min) (point-max)))
  12705. (nreverse rtn)))
  12706. (defun org-remove-uninherited-tags (tags)
  12707. "Remove all tags that are not inherited from the list TAGS."
  12708. (cond
  12709. ((eq org-use-tag-inheritance t)
  12710. (if org-tags-exclude-from-inheritance
  12711. (org-delete-all org-tags-exclude-from-inheritance tags)
  12712. tags))
  12713. ((not org-use-tag-inheritance) nil)
  12714. ((stringp org-use-tag-inheritance)
  12715. (delq nil (mapcar
  12716. (lambda (x)
  12717. (if (and (string-match org-use-tag-inheritance x)
  12718. (not (member x org-tags-exclude-from-inheritance)))
  12719. x nil))
  12720. tags)))
  12721. ((listp org-use-tag-inheritance)
  12722. (delq nil (mapcar
  12723. (lambda (x)
  12724. (if (member x org-use-tag-inheritance) x nil))
  12725. tags)))))
  12726. (defun org-match-sparse-tree (&optional todo-only match)
  12727. "Create a sparse tree according to tags string MATCH.
  12728. MATCH can contain positive and negative selection of tags, like
  12729. \"+WORK+URGENT-WITHBOSS\".
  12730. If optional argument TODO-ONLY is non-nil, only select lines that are
  12731. also TODO lines."
  12732. (interactive "P")
  12733. (org-agenda-prepare-buffers (list (current-buffer)))
  12734. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  12735. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  12736. (defvar org-cached-props nil)
  12737. (defun org-cached-entry-get (pom property)
  12738. (if (or (eq t org-use-property-inheritance)
  12739. (and (stringp org-use-property-inheritance)
  12740. (string-match org-use-property-inheritance property))
  12741. (and (listp org-use-property-inheritance)
  12742. (member property org-use-property-inheritance)))
  12743. ;; Caching is not possible, check it directly
  12744. (org-entry-get pom property 'inherit)
  12745. ;; Get all properties, so that we can do complicated checks easily
  12746. (cdr (assoc property (or org-cached-props
  12747. (setq org-cached-props
  12748. (org-entry-properties pom)))))))
  12749. (defun org-global-tags-completion-table (&optional files)
  12750. "Return the list of all tags in all agenda buffer/files.
  12751. Optional FILES argument is a list of files which can be used
  12752. instead of the agenda files."
  12753. (save-excursion
  12754. (org-uniquify
  12755. (delq nil
  12756. (apply 'append
  12757. (mapcar
  12758. (lambda (file)
  12759. (set-buffer (find-file-noselect file))
  12760. (append (org-get-buffer-tags)
  12761. (mapcar (lambda (x) (if (stringp (car-safe x))
  12762. (list (car-safe x)) nil))
  12763. org-tag-alist)))
  12764. (if (and files (car files))
  12765. files
  12766. (org-agenda-files))))))))
  12767. (defun org-make-tags-matcher (match)
  12768. "Create the TAGS/TODO matcher form for the selection string MATCH.
  12769. The variable `todo-only' is scoped dynamically into this function.
  12770. It will be set to t if the matcher restricts matching to TODO entries,
  12771. otherwise will not be touched.
  12772. Returns a cons of the selection string MATCH and the constructed
  12773. lisp form implementing the matcher. The matcher is to be evaluated
  12774. at an Org entry, with point on the headline, and returns t if the
  12775. entry matches the selection string MATCH. The returned lisp form
  12776. references two variables with information about the entry, which
  12777. must be bound around the form's evaluation: todo, the TODO keyword
  12778. at the entry (or nil of none); and tags-list, the list of all tags
  12779. at the entry including inherited ones. Additionally, the category
  12780. of the entry (if any) must be specified as the text property
  12781. 'org-category on the headline.
  12782. See also `org-scan-tags'.
  12783. "
  12784. (declare (special todo-only))
  12785. (unless (boundp 'todo-only)
  12786. (error "`org-make-tags-matcher' expects todo-only to be scoped in"))
  12787. (unless match
  12788. ;; Get a new match request, with completion against the global
  12789. ;; tags table and the local tags in current buffer
  12790. (let ((org-last-tags-completion-table
  12791. (org-uniquify
  12792. (delq nil (append (org-get-buffer-tags)
  12793. (org-global-tags-completion-table))))))
  12794. (setq match (org-completing-read-no-i
  12795. "Match: " 'org-tags-completion-function nil nil nil
  12796. 'org-tags-history))))
  12797. ;; Parse the string and create a lisp form
  12798. (let ((match0 match)
  12799. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\(\\(?:[[:alnum:]_]+\\(?:\\\\-\\)*\\)+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@#%]+\\)"))
  12800. minus tag mm
  12801. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  12802. orterms term orlist re-p str-p level-p level-op time-p
  12803. prop-p pn pv po gv rest)
  12804. ;; Expand group tags
  12805. (setq match (org-tags-expand match))
  12806. (if (string-match "/+" match)
  12807. ;; match contains also a todo-matching request
  12808. (progn
  12809. (setq tagsmatch (substring match 0 (match-beginning 0))
  12810. todomatch (substring match (match-end 0)))
  12811. (if (string-match "^!" todomatch)
  12812. (setq todo-only t todomatch (substring todomatch 1)))
  12813. (if (string-match "^\\s-*$" todomatch)
  12814. (setq todomatch nil)))
  12815. ;; only matching tags
  12816. (setq tagsmatch match todomatch nil))
  12817. ;; Make the tags matcher
  12818. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  12819. (setq tagsmatcher t)
  12820. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  12821. (while (setq term (pop orterms))
  12822. (while (and (equal (substring term -1) "\\") orterms)
  12823. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  12824. (while (string-match re term)
  12825. (setq rest (substring term (match-end 0))
  12826. minus (and (match-end 1)
  12827. (equal (match-string 1 term) "-"))
  12828. tag (save-match-data (replace-regexp-in-string
  12829. "\\\\-" "-"
  12830. (match-string 2 term)))
  12831. re-p (equal (string-to-char tag) ?{)
  12832. level-p (match-end 4)
  12833. prop-p (match-end 5)
  12834. mm (cond
  12835. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  12836. (level-p
  12837. (setq level-op (org-op-to-function (match-string 3 term)))
  12838. `(,level-op level ,(string-to-number
  12839. (match-string 4 term))))
  12840. (prop-p
  12841. (setq pn (match-string 5 term)
  12842. po (match-string 6 term)
  12843. pv (match-string 7 term)
  12844. re-p (equal (string-to-char pv) ?{)
  12845. str-p (equal (string-to-char pv) ?\")
  12846. time-p (save-match-data
  12847. (string-match "^\"[[<].*[]>]\"$" pv))
  12848. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  12849. (if time-p (setq pv (org-matcher-time pv)))
  12850. (setq po (org-op-to-function po (if time-p 'time str-p)))
  12851. (cond
  12852. ((equal pn "CATEGORY")
  12853. (setq gv '(get-text-property (point) 'org-category)))
  12854. ((equal pn "TODO")
  12855. (setq gv 'todo))
  12856. (t
  12857. (setq gv `(org-cached-entry-get nil ,pn))))
  12858. (if re-p
  12859. (if (eq po 'org<>)
  12860. `(not (string-match ,pv (or ,gv "")))
  12861. `(string-match ,pv (or ,gv "")))
  12862. (if str-p
  12863. `(,po (or ,gv "") ,pv)
  12864. `(,po (string-to-number (or ,gv ""))
  12865. ,(string-to-number pv) ))))
  12866. (t `(member ,tag tags-list)))
  12867. mm (if minus (list 'not mm) mm)
  12868. term rest)
  12869. (push mm tagsmatcher))
  12870. (push (if (> (length tagsmatcher) 1)
  12871. (cons 'and tagsmatcher)
  12872. (car tagsmatcher))
  12873. orlist)
  12874. (setq tagsmatcher nil))
  12875. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  12876. (setq tagsmatcher
  12877. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  12878. ;; Make the todo matcher
  12879. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  12880. (setq todomatcher t)
  12881. (setq orterms (org-split-string todomatch "|") orlist nil)
  12882. (while (setq term (pop orterms))
  12883. (while (string-match re term)
  12884. (setq minus (and (match-end 1)
  12885. (equal (match-string 1 term) "-"))
  12886. kwd (match-string 2 term)
  12887. re-p (equal (string-to-char kwd) ?{)
  12888. term (substring term (match-end 0))
  12889. mm (if re-p
  12890. `(string-match ,(substring kwd 1 -1) todo)
  12891. (list 'equal 'todo kwd))
  12892. mm (if minus (list 'not mm) mm))
  12893. (push mm todomatcher))
  12894. (push (if (> (length todomatcher) 1)
  12895. (cons 'and todomatcher)
  12896. (car todomatcher))
  12897. orlist)
  12898. (setq todomatcher nil))
  12899. (setq todomatcher (if (> (length orlist) 1)
  12900. (cons 'or orlist) (car orlist))))
  12901. ;; Return the string and lisp forms of the matcher
  12902. (setq matcher (if todomatcher
  12903. (list 'and tagsmatcher todomatcher)
  12904. tagsmatcher))
  12905. (when todo-only
  12906. (setq matcher (list 'and '(member todo org-not-done-keywords)
  12907. matcher)))
  12908. (cons match0 matcher)))
  12909. (defun org-tags-expand (match &optional single-as-list downcased)
  12910. "Expand group tags in MATCH.
  12911. This replaces every group tag in MATCH with a regexp tag search.
  12912. For example, a group tag \"Work\" defined as { Work : Lab Conf }
  12913. will be replaced like this:
  12914. Work => {\\(?:Work\\|Lab\\|Conf\\)}
  12915. +Work => +{\\(?:Work\\|Lab\\|Conf\\)}
  12916. -Work => -{\\(?:Work\\|Lab\\|Conf\\)}
  12917. Replacing by a regexp preserves the structure of the match.
  12918. E.g., this expansion
  12919. Work|Home => {\\(?:Work\\|Lab\\|Conf\\}|Home
  12920. will match anything tagged with \"Lab\" and \"Home\", or tagged
  12921. with \"Conf\" and \"Home\" or tagged with \"Work\" and \"home\".
  12922. When the optional argument SINGLE-AS-LIST is non-nil, MATCH is
  12923. assumed to be a single group tag, and the function will return
  12924. the list of tags in this group.
  12925. When DOWNCASE is non-nil, expand downcased TAGS."
  12926. (if org-group-tags
  12927. (let* ((case-fold-search t)
  12928. (stable org-mode-syntax-table)
  12929. (tal (or org-tag-groups-alist-for-agenda
  12930. org-tag-groups-alist))
  12931. (tal (if downcased
  12932. (mapcar (lambda(tg) (mapcar 'downcase tg)) tal) tal))
  12933. (tml (mapcar 'car tal))
  12934. (rtnmatch match) rpl)
  12935. ;; @ and _ are allowed as word-components in tags
  12936. (modify-syntax-entry ?@ "w" stable)
  12937. (modify-syntax-entry ?_ "w" stable)
  12938. (while (and tml
  12939. (string-match
  12940. (concat "\\(?1:[+-]?\\)\\(?2:\\<"
  12941. (regexp-opt tml) "\\>\\)") rtnmatch))
  12942. (let* ((dir (match-string 1 rtnmatch))
  12943. (tag (match-string 2 rtnmatch))
  12944. (tag (if downcased (downcase tag) tag)))
  12945. (setq tml (delete tag tml))
  12946. (when (not (get-text-property 0 'grouptag (match-string 2 rtnmatch)))
  12947. (setq rpl (append (org-uniquify rpl) (assoc tag tal)))
  12948. (setq rpl (concat dir "{\\<" (regexp-opt rpl) "\\>}"))
  12949. (if (stringp rpl) (org-add-props rpl '(grouptag t)))
  12950. (setq rtnmatch (replace-match rpl t t rtnmatch)))))
  12951. (if single-as-list
  12952. (or (reverse rpl) (list rtnmatch))
  12953. rtnmatch))
  12954. (if single-as-list (list (if downcased (downcase match) match))
  12955. match)))
  12956. (defun org-op-to-function (op &optional stringp)
  12957. "Turn an operator into the appropriate function."
  12958. (setq op
  12959. (cond
  12960. ((equal op "<" ) '(< string< org-time<))
  12961. ((equal op ">" ) '(> org-string> org-time>))
  12962. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  12963. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  12964. ((member op '("=" "==")) '(= string= org-time=))
  12965. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  12966. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  12967. (defun org<> (a b) (not (= a b)))
  12968. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  12969. (defun org-string>= (a b) (not (string< a b)))
  12970. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  12971. (defun org-string<> (a b) (not (string= a b)))
  12972. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  12973. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  12974. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  12975. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  12976. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  12977. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  12978. (defun org-2ft (s)
  12979. "Convert S to a floating point time.
  12980. If S is already a number, just return it. If it is a string, parse
  12981. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  12982. (cond
  12983. ((numberp s) s)
  12984. ((stringp s)
  12985. (condition-case nil
  12986. (float-time (apply 'encode-time (org-parse-time-string s)))
  12987. (error 0.)))
  12988. (t 0.)))
  12989. (defun org-time-today ()
  12990. "Time in seconds today at 0:00.
  12991. Returns the float number of seconds since the beginning of the
  12992. epoch to the beginning of today (00:00)."
  12993. (float-time (apply 'encode-time
  12994. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  12995. (defun org-matcher-time (s)
  12996. "Interpret a time comparison value."
  12997. (save-match-data
  12998. (cond
  12999. ((string= s "<now>") (float-time))
  13000. ((string= s "<today>") (org-time-today))
  13001. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  13002. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  13003. ((string-match "^<\\([-+][0-9]+\\)\\([hdwmy]\\)>$" s)
  13004. (+ (org-time-today)
  13005. (* (string-to-number (match-string 1 s))
  13006. (cdr (assoc (match-string 2 s)
  13007. '(("d" . 86400.0) ("w" . 604800.0)
  13008. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  13009. (t (org-2ft s)))))
  13010. (defun org-match-any-p (re list)
  13011. "Does re match any element of list?"
  13012. (setq list (mapcar (lambda (x) (string-match re x)) list))
  13013. (delq nil list))
  13014. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  13015. (defvar org-tags-overlay (make-overlay 1 1))
  13016. (org-detach-overlay org-tags-overlay)
  13017. (defun org-get-local-tags-at (&optional pos)
  13018. "Get a list of tags defined in the current headline."
  13019. (org-get-tags-at pos 'local))
  13020. (defun org-get-local-tags ()
  13021. "Get a list of tags defined in the current headline."
  13022. (org-get-tags-at nil 'local))
  13023. (defun org-get-tags-at (&optional pos local)
  13024. "Get a list of all headline tags applicable at POS.
  13025. POS defaults to point. If tags are inherited, the list contains
  13026. the targets in the same sequence as the headlines appear, i.e.
  13027. the tags of the current headline come last.
  13028. When LOCAL is non-nil, only return tags from the current headline,
  13029. ignore inherited ones."
  13030. (interactive)
  13031. (if (and org-trust-scanner-tags
  13032. (or (not pos) (equal pos (point)))
  13033. (not local))
  13034. org-scanner-tags
  13035. (let (tags ltags lastpos parent)
  13036. (save-excursion
  13037. (save-restriction
  13038. (widen)
  13039. (goto-char (or pos (point)))
  13040. (save-match-data
  13041. (catch 'done
  13042. (condition-case nil
  13043. (progn
  13044. (org-back-to-heading t)
  13045. (while (not (equal lastpos (point)))
  13046. (setq lastpos (point))
  13047. (when (looking-at
  13048. (org-re "[^\r\n]+?:\\([[:alnum:]_@#%:]+\\):[ \t]*$"))
  13049. (setq ltags (org-split-string
  13050. (org-match-string-no-properties 1) ":"))
  13051. (when parent
  13052. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  13053. (setq tags (append
  13054. (if parent
  13055. (org-remove-uninherited-tags ltags)
  13056. ltags)
  13057. tags)))
  13058. (or org-use-tag-inheritance (throw 'done t))
  13059. (if local (throw 'done t))
  13060. (or (org-up-heading-safe) (error nil))
  13061. (setq parent t)))
  13062. (error nil)))))
  13063. (if local
  13064. tags
  13065. (reverse (delete-dups
  13066. (reverse (append
  13067. (org-remove-uninherited-tags
  13068. org-file-tags) tags)))))))))
  13069. (defun org-add-prop-inherited (s)
  13070. (add-text-properties 0 (length s) '(inherited t) s)
  13071. s)
  13072. (defun org-toggle-tag (tag &optional onoff)
  13073. "Toggle the tag TAG for the current line.
  13074. If ONOFF is `on' or `off', don't toggle but set to this state."
  13075. (let (res current)
  13076. (save-excursion
  13077. (org-back-to-heading t)
  13078. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t]*$")
  13079. (point-at-eol) t)
  13080. (progn
  13081. (setq current (match-string 1))
  13082. (replace-match ""))
  13083. (setq current ""))
  13084. (setq current (nreverse (org-split-string current ":")))
  13085. (cond
  13086. ((eq onoff 'on)
  13087. (setq res t)
  13088. (or (member tag current) (push tag current)))
  13089. ((eq onoff 'off)
  13090. (or (not (member tag current)) (setq current (delete tag current))))
  13091. (t (if (member tag current)
  13092. (setq current (delete tag current))
  13093. (setq res t)
  13094. (push tag current))))
  13095. (end-of-line 1)
  13096. (if current
  13097. (progn
  13098. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  13099. (org-set-tags nil t))
  13100. (delete-horizontal-space))
  13101. (run-hooks 'org-after-tags-change-hook))
  13102. res))
  13103. (defun org-align-tags-here (to-col)
  13104. ;; Assumes that this is a headline
  13105. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  13106. (beginning-of-line 1)
  13107. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13108. (< pos (match-beginning 2)))
  13109. (progn
  13110. (setq tags-l (- (match-end 2) (match-beginning 2)))
  13111. (goto-char (match-beginning 1))
  13112. (insert " ")
  13113. (delete-region (point) (1+ (match-beginning 2)))
  13114. (setq ncol (max (current-column)
  13115. (1+ col)
  13116. (if (> to-col 0)
  13117. to-col
  13118. (- (abs to-col) tags-l))))
  13119. (setq p (point))
  13120. (insert (make-string (- ncol (current-column)) ?\ ))
  13121. (setq ncol (current-column))
  13122. (when indent-tabs-mode (tabify p (point-at-eol)))
  13123. (org-move-to-column (min ncol col) t))
  13124. (goto-char pos))))
  13125. (defun org-set-tags-command (&optional arg just-align)
  13126. "Call the set-tags command for the current entry."
  13127. (interactive "P")
  13128. (if (or (org-at-heading-p) (and arg (org-before-first-heading-p)))
  13129. (org-set-tags arg just-align)
  13130. (save-excursion
  13131. (unless (and (org-region-active-p)
  13132. org-loop-over-headlines-in-active-region)
  13133. (org-back-to-heading t))
  13134. (org-set-tags arg just-align))))
  13135. (defun org-set-tags-to (data)
  13136. "Set the tags of the current entry to DATA, replacing the current tags.
  13137. DATA may be a tags string like :aa:bb:cc:, or a list of tags.
  13138. If DATA is nil or the empty string, any tags will be removed."
  13139. (interactive "sTags: ")
  13140. (setq data
  13141. (cond
  13142. ((eq data nil) "")
  13143. ((equal data "") "")
  13144. ((stringp data)
  13145. (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
  13146. ":"))
  13147. ((listp data)
  13148. (concat ":" (mapconcat 'identity data ":") ":"))))
  13149. (when data
  13150. (save-excursion
  13151. (org-back-to-heading t)
  13152. (when (looking-at org-complex-heading-regexp)
  13153. (if (match-end 5)
  13154. (progn
  13155. (goto-char (match-beginning 5))
  13156. (insert data)
  13157. (delete-region (point) (point-at-eol))
  13158. (org-set-tags nil 'align))
  13159. (goto-char (point-at-eol))
  13160. (insert " " data)
  13161. (org-set-tags nil 'align)))
  13162. (beginning-of-line 1)
  13163. (if (looking-at ".*?\\([ \t]+\\)$")
  13164. (delete-region (match-beginning 1) (match-end 1))))))
  13165. (defun org-align-all-tags ()
  13166. "Align the tags i all headings."
  13167. (interactive)
  13168. (save-excursion
  13169. (or (ignore-errors (org-back-to-heading t))
  13170. (outline-next-heading))
  13171. (if (org-at-heading-p)
  13172. (org-set-tags t)
  13173. (message "No headings"))))
  13174. (defvar org-indent-indentation-per-level)
  13175. (defun org-set-tags (&optional arg just-align)
  13176. "Set the tags for the current headline.
  13177. With prefix ARG, realign all tags in headings in the current buffer."
  13178. (interactive "P")
  13179. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  13180. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  13181. 'region-start-level 'region))
  13182. org-loop-over-headlines-in-active-region)
  13183. (org-map-entries
  13184. ;; We don't use ARG and JUST-ALIGN here these args are not
  13185. ;; useful when looping over headlines
  13186. `(org-set-tags)
  13187. org-loop-over-headlines-in-active-region
  13188. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  13189. (let* ((re org-outline-regexp-bol)
  13190. (current (unless arg (org-get-tags-string)))
  13191. (col (current-column))
  13192. (org-setting-tags t)
  13193. table current-tags inherited-tags ; computed below when needed
  13194. tags p0 c0 c1 rpl di tc level)
  13195. (if arg
  13196. (save-excursion
  13197. (goto-char (point-min))
  13198. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  13199. (while (re-search-forward re nil t)
  13200. (org-set-tags nil t)
  13201. (end-of-line 1)))
  13202. (message "All tags realigned to column %d" org-tags-column))
  13203. (if just-align
  13204. (setq tags current)
  13205. ;; Get a new set of tags from the user
  13206. (save-excursion
  13207. (setq table (append org-tag-persistent-alist
  13208. (or org-tag-alist (org-get-buffer-tags))
  13209. (and
  13210. org-complete-tags-always-offer-all-agenda-tags
  13211. (org-global-tags-completion-table
  13212. (org-agenda-files))))
  13213. org-last-tags-completion-table table
  13214. current-tags (org-split-string current ":")
  13215. inherited-tags (nreverse
  13216. (nthcdr (length current-tags)
  13217. (nreverse (org-get-tags-at))))
  13218. tags
  13219. (if (or (eq t org-use-fast-tag-selection)
  13220. (and org-use-fast-tag-selection
  13221. (delq nil (mapcar 'cdr table))))
  13222. (org-fast-tag-selection
  13223. current-tags inherited-tags table
  13224. (if org-fast-tag-selection-include-todo
  13225. org-todo-key-alist))
  13226. (let ((org-add-colon-after-tag-completion (< 1 (length table))))
  13227. (org-trim
  13228. (org-icompleting-read "Tags: "
  13229. 'org-tags-completion-function
  13230. nil nil current 'org-tags-history))))))
  13231. (while (string-match "[-+&]+" tags)
  13232. ;; No boolean logic, just a list
  13233. (setq tags (replace-match ":" t t tags))))
  13234. (setq tags (replace-regexp-in-string "[,]" ":" tags))
  13235. (if org-tags-sort-function
  13236. (setq tags (mapconcat 'identity
  13237. (sort (org-split-string
  13238. tags (org-re "[^[:alnum:]_@#%]+"))
  13239. org-tags-sort-function) ":")))
  13240. (if (string-match "\\`[\t ]*\\'" tags)
  13241. (setq tags "")
  13242. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  13243. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  13244. ;; Insert new tags at the correct column
  13245. (beginning-of-line 1)
  13246. (setq level (or (and (looking-at org-outline-regexp)
  13247. (- (match-end 0) (point) 1))
  13248. 1))
  13249. (cond
  13250. ((and (equal current "") (equal tags "")))
  13251. ((re-search-forward
  13252. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  13253. (point-at-eol) t)
  13254. (if (equal tags "")
  13255. (setq rpl "")
  13256. (goto-char (match-beginning 0))
  13257. (setq c0 (current-column)
  13258. ;; compute offset for the case of org-indent-mode active
  13259. di (if org-indent-mode
  13260. (* (1- org-indent-indentation-per-level) (1- level))
  13261. 0)
  13262. p0 (if (equal (char-before) ?*) (1+ (point)) (point))
  13263. tc (+ org-tags-column (if (> org-tags-column 0) (- di) di))
  13264. c1 (max (1+ c0) (if (> tc 0) tc (- (- tc) (length tags))))
  13265. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  13266. (replace-match rpl t t)
  13267. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  13268. tags)
  13269. (t (error "Tags alignment failed")))
  13270. (org-move-to-column col)
  13271. (unless just-align
  13272. (run-hooks 'org-after-tags-change-hook))))))
  13273. (defun org-change-tag-in-region (beg end tag off)
  13274. "Add or remove TAG for each entry in the region.
  13275. This works in the agenda, and also in an org-mode buffer."
  13276. (interactive
  13277. (list (region-beginning) (region-end)
  13278. (let ((org-last-tags-completion-table
  13279. (if (derived-mode-p 'org-mode)
  13280. (org-uniquify
  13281. (delq nil (append (org-get-buffer-tags)
  13282. (org-global-tags-completion-table))))
  13283. (org-global-tags-completion-table))))
  13284. (org-icompleting-read
  13285. "Tag: " 'org-tags-completion-function nil nil nil
  13286. 'org-tags-history))
  13287. (progn
  13288. (message "[s]et or [r]emove? ")
  13289. (equal (read-char-exclusive) ?r))))
  13290. (if (fboundp 'deactivate-mark) (deactivate-mark))
  13291. (let ((agendap (equal major-mode 'org-agenda-mode))
  13292. l1 l2 m buf pos newhead (cnt 0))
  13293. (goto-char end)
  13294. (setq l2 (1- (org-current-line)))
  13295. (goto-char beg)
  13296. (setq l1 (org-current-line))
  13297. (loop for l from l1 to l2 do
  13298. (org-goto-line l)
  13299. (setq m (get-text-property (point) 'org-hd-marker))
  13300. (when (or (and (derived-mode-p 'org-mode) (org-at-heading-p))
  13301. (and agendap m))
  13302. (setq buf (if agendap (marker-buffer m) (current-buffer))
  13303. pos (if agendap m (point)))
  13304. (with-current-buffer buf
  13305. (save-excursion
  13306. (save-restriction
  13307. (goto-char pos)
  13308. (setq cnt (1+ cnt))
  13309. (org-toggle-tag tag (if off 'off 'on))
  13310. (setq newhead (org-get-heading)))))
  13311. (and agendap (org-agenda-change-all-lines newhead m))))
  13312. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  13313. (defun org-tags-completion-function (string predicate &optional flag)
  13314. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  13315. (confirm (lambda (x) (stringp (car x)))))
  13316. (if (string-match "^\\(.*[-+:&,|]\\)\\([^-+:&,|]*\\)$" string)
  13317. (setq s1 (match-string 1 string)
  13318. s2 (match-string 2 string))
  13319. (setq s1 "" s2 string))
  13320. (cond
  13321. ((eq flag nil)
  13322. ;; try completion
  13323. (setq rtn (try-completion s2 ctable confirm))
  13324. (if (stringp rtn)
  13325. (setq rtn
  13326. (concat s1 s2 (substring rtn (length s2))
  13327. (if (and org-add-colon-after-tag-completion
  13328. (assoc rtn ctable))
  13329. ":" ""))))
  13330. rtn)
  13331. ((eq flag t)
  13332. ;; all-completions
  13333. (all-completions s2 ctable confirm))
  13334. ((eq flag 'lambda)
  13335. ;; exact match?
  13336. (assoc s2 ctable)))))
  13337. (defun org-fast-tag-insert (kwd tags face &optional end)
  13338. "Insert KDW, and the TAGS, the latter with face FACE.
  13339. Also insert END."
  13340. (insert (format "%-12s" (concat kwd ":"))
  13341. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  13342. (or end "")))
  13343. (defun org-fast-tag-show-exit (flag)
  13344. (save-excursion
  13345. (org-goto-line 3)
  13346. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  13347. (replace-match ""))
  13348. (when flag
  13349. (end-of-line 1)
  13350. (org-move-to-column (- (window-width) 19) t)
  13351. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  13352. (defun org-set-current-tags-overlay (current prefix)
  13353. "Add an overlay to CURRENT tag with PREFIX."
  13354. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  13355. (if (featurep 'xemacs)
  13356. (org-overlay-display org-tags-overlay (concat prefix s)
  13357. 'secondary-selection)
  13358. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  13359. (org-overlay-display org-tags-overlay (concat prefix s)))))
  13360. (defvar org-last-tag-selection-key nil)
  13361. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  13362. "Fast tag selection with single keys.
  13363. CURRENT is the current list of tags in the headline, INHERITED is the
  13364. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  13365. possibly with grouping information. TODO-TABLE is a similar table with
  13366. TODO keywords, should these have keys assigned to them.
  13367. If the keys are nil, a-z are automatically assigned.
  13368. Returns the new tags string, or nil to not change the current settings."
  13369. (let* ((fulltable (append table todo-table))
  13370. (maxlen (apply 'max (mapcar
  13371. (lambda (x)
  13372. (if (stringp (car x)) (string-width (car x)) 0))
  13373. fulltable)))
  13374. (buf (current-buffer))
  13375. (expert (eq org-fast-tag-selection-single-key 'expert))
  13376. (buffer-tags nil)
  13377. (fwidth (+ maxlen 3 1 3))
  13378. (ncol (/ (- (window-width) 4) fwidth))
  13379. (i-face 'org-done)
  13380. (c-face 'org-todo)
  13381. tg cnt e c char c1 c2 ntable tbl rtn
  13382. ov-start ov-end ov-prefix
  13383. (exit-after-next org-fast-tag-selection-single-key)
  13384. (done-keywords org-done-keywords)
  13385. groups ingroup)
  13386. (save-excursion
  13387. (beginning-of-line 1)
  13388. (if (looking-at
  13389. (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13390. (setq ov-start (match-beginning 1)
  13391. ov-end (match-end 1)
  13392. ov-prefix "")
  13393. (setq ov-start (1- (point-at-eol))
  13394. ov-end (1+ ov-start))
  13395. (skip-chars-forward "^\n\r")
  13396. (setq ov-prefix
  13397. (concat
  13398. (buffer-substring (1- (point)) (point))
  13399. (if (> (current-column) org-tags-column)
  13400. " "
  13401. (make-string (- org-tags-column (current-column)) ?\ ))))))
  13402. (move-overlay org-tags-overlay ov-start ov-end)
  13403. (save-window-excursion
  13404. (if expert
  13405. (set-buffer (get-buffer-create " *Org tags*"))
  13406. (delete-other-windows)
  13407. (split-window-vertically)
  13408. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  13409. (erase-buffer)
  13410. (org-set-local 'org-done-keywords done-keywords)
  13411. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  13412. (org-fast-tag-insert "Current" current c-face "\n\n")
  13413. (org-fast-tag-show-exit exit-after-next)
  13414. (org-set-current-tags-overlay current ov-prefix)
  13415. (setq tbl fulltable char ?a cnt 0)
  13416. (while (setq e (pop tbl))
  13417. (cond
  13418. ((equal (car e) :startgroup)
  13419. (push '() groups) (setq ingroup t)
  13420. (when (not (= cnt 0))
  13421. (setq cnt 0)
  13422. (insert "\n"))
  13423. (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
  13424. ((equal (car e) :endgroup)
  13425. (setq ingroup nil cnt 0)
  13426. (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
  13427. ((equal e '(:newline))
  13428. (when (not (= cnt 0))
  13429. (setq cnt 0)
  13430. (insert "\n")
  13431. (setq e (car tbl))
  13432. (while (equal (car tbl) '(:newline))
  13433. (insert "\n")
  13434. (setq tbl (cdr tbl)))))
  13435. ((equal e '(:grouptags)) nil)
  13436. (t
  13437. (setq tg (copy-sequence (car e)) c2 nil)
  13438. (if (cdr e)
  13439. (setq c (cdr e))
  13440. ;; automatically assign a character.
  13441. (setq c1 (string-to-char
  13442. (downcase (substring
  13443. tg (if (= (string-to-char tg) ?@) 1 0)))))
  13444. (if (or (rassoc c1 ntable) (rassoc c1 table))
  13445. (while (or (rassoc char ntable) (rassoc char table))
  13446. (setq char (1+ char)))
  13447. (setq c2 c1))
  13448. (setq c (or c2 char)))
  13449. (if ingroup (push tg (car groups)))
  13450. (setq tg (org-add-props tg nil 'face
  13451. (cond
  13452. ((not (assoc tg table))
  13453. (org-get-todo-face tg))
  13454. ((member tg current) c-face)
  13455. ((member tg inherited) i-face))))
  13456. (if (equal (caar tbl) :grouptags)
  13457. (org-add-props tg nil 'face 'org-tag-group))
  13458. (if (and (= cnt 0) (not ingroup)) (insert " "))
  13459. (insert "[" c "] " tg (make-string
  13460. (- fwidth 4 (length tg)) ?\ ))
  13461. (push (cons tg c) ntable)
  13462. (when (= (setq cnt (1+ cnt)) ncol)
  13463. (insert "\n")
  13464. (if ingroup (insert " "))
  13465. (setq cnt 0)))))
  13466. (setq ntable (nreverse ntable))
  13467. (insert "\n")
  13468. (goto-char (point-min))
  13469. (if (not expert) (org-fit-window-to-buffer))
  13470. (setq rtn
  13471. (catch 'exit
  13472. (while t
  13473. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
  13474. (if (not groups) "no " "")
  13475. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  13476. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  13477. (setq org-last-tag-selection-key c)
  13478. (cond
  13479. ((= c ?\r) (throw 'exit t))
  13480. ((= c ?!)
  13481. (setq groups (not groups))
  13482. (goto-char (point-min))
  13483. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  13484. ((= c ?\C-c)
  13485. (if (not expert)
  13486. (org-fast-tag-show-exit
  13487. (setq exit-after-next (not exit-after-next)))
  13488. (setq expert nil)
  13489. (delete-other-windows)
  13490. (set-window-buffer (split-window-vertically) " *Org tags*")
  13491. (org-switch-to-buffer-other-window " *Org tags*")
  13492. (org-fit-window-to-buffer)))
  13493. ((or (= c ?\C-g)
  13494. (and (= c ?q) (not (rassoc c ntable))))
  13495. (org-detach-overlay org-tags-overlay)
  13496. (setq quit-flag t))
  13497. ((= c ?\ )
  13498. (setq current nil)
  13499. (if exit-after-next (setq exit-after-next 'now)))
  13500. ((= c ?\t)
  13501. (condition-case nil
  13502. (setq tg (org-icompleting-read
  13503. "Tag: "
  13504. (or buffer-tags
  13505. (with-current-buffer buf
  13506. (org-get-buffer-tags)))))
  13507. (quit (setq tg "")))
  13508. (when (string-match "\\S-" tg)
  13509. (add-to-list 'buffer-tags (list tg))
  13510. (if (member tg current)
  13511. (setq current (delete tg current))
  13512. (push tg current)))
  13513. (if exit-after-next (setq exit-after-next 'now)))
  13514. ((setq e (rassoc c todo-table) tg (car e))
  13515. (with-current-buffer buf
  13516. (save-excursion (org-todo tg)))
  13517. (if exit-after-next (setq exit-after-next 'now)))
  13518. ((setq e (rassoc c ntable) tg (car e))
  13519. (if (member tg current)
  13520. (setq current (delete tg current))
  13521. (loop for g in groups do
  13522. (if (member tg g)
  13523. (mapc (lambda (x)
  13524. (setq current (delete x current)))
  13525. g)))
  13526. (push tg current))
  13527. (if exit-after-next (setq exit-after-next 'now))))
  13528. ;; Create a sorted list
  13529. (setq current
  13530. (sort current
  13531. (lambda (a b)
  13532. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  13533. (if (eq exit-after-next 'now) (throw 'exit t))
  13534. (goto-char (point-min))
  13535. (beginning-of-line 2)
  13536. (delete-region (point) (point-at-eol))
  13537. (org-fast-tag-insert "Current" current c-face)
  13538. (org-set-current-tags-overlay current ov-prefix)
  13539. (while (re-search-forward
  13540. (org-re "\\[.\\] \\([[:alnum:]_@#%]+\\)") nil t)
  13541. (setq tg (match-string 1))
  13542. (add-text-properties
  13543. (match-beginning 1) (match-end 1)
  13544. (list 'face
  13545. (cond
  13546. ((member tg current) c-face)
  13547. ((member tg inherited) i-face)
  13548. (t (get-text-property (match-beginning 1) 'face))))))
  13549. (goto-char (point-min)))))
  13550. (org-detach-overlay org-tags-overlay)
  13551. (if rtn
  13552. (mapconcat 'identity current ":")
  13553. nil))))
  13554. (defun org-get-tags-string ()
  13555. "Get the TAGS string in the current headline."
  13556. (unless (org-at-heading-p t)
  13557. (user-error "Not on a heading"))
  13558. (save-excursion
  13559. (beginning-of-line 1)
  13560. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13561. (org-match-string-no-properties 1)
  13562. "")))
  13563. (defun org-get-tags ()
  13564. "Get the list of tags specified in the current headline."
  13565. (org-split-string (org-get-tags-string) ":"))
  13566. (defun org-get-buffer-tags ()
  13567. "Get a table of all tags used in the buffer, for completion."
  13568. (let (tags)
  13569. (save-excursion
  13570. (goto-char (point-min))
  13571. (while (re-search-forward
  13572. (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t\r\n]") nil t)
  13573. (when (equal (char-after (point-at-bol 0)) ?*)
  13574. (mapc (lambda (x) (add-to-list 'tags x))
  13575. (org-split-string (org-match-string-no-properties 1) ":")))))
  13576. (mapc (lambda (s) (add-to-list 'tags s)) org-file-tags)
  13577. (mapcar 'list tags)))
  13578. ;;;; The mapping API
  13579. (defun org-map-entries (func &optional match scope &rest skip)
  13580. "Call FUNC at each headline selected by MATCH in SCOPE.
  13581. FUNC is a function or a lisp form. The function will be called without
  13582. arguments, with the cursor positioned at the beginning of the headline.
  13583. The return values of all calls to the function will be collected and
  13584. returned as a list.
  13585. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  13586. does not need to preserve point. After evaluation, the cursor will be
  13587. moved to the end of the line (presumably of the headline of the
  13588. processed entry) and search continues from there. Under some
  13589. circumstances, this may not produce the wanted results. For example,
  13590. if you have removed (e.g. archived) the current (sub)tree it could
  13591. mean that the next entry will be skipped entirely. In such cases, you
  13592. can specify the position from where search should continue by making
  13593. FUNC set the variable `org-map-continue-from' to the desired buffer
  13594. position.
  13595. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  13596. Only headlines that are matched by this query will be considered during
  13597. the iteration. When MATCH is nil or t, all headlines will be
  13598. visited by the iteration.
  13599. SCOPE determines the scope of this command. It can be any of:
  13600. nil The current buffer, respecting the restriction if any
  13601. tree The subtree started with the entry at point
  13602. region The entries within the active region, if any
  13603. region-start-level
  13604. The entries within the active region, but only those at
  13605. the same level than the first one.
  13606. file The current buffer, without restriction
  13607. file-with-archives
  13608. The current buffer, and any archives associated with it
  13609. agenda All agenda files
  13610. agenda-with-archives
  13611. All agenda files with any archive files associated with them
  13612. \(file1 file2 ...)
  13613. If this is a list, all files in the list will be scanned
  13614. The remaining args are treated as settings for the skipping facilities of
  13615. the scanner. The following items can be given here:
  13616. archive skip trees with the archive tag
  13617. comment skip trees with the COMMENT keyword
  13618. function or Emacs Lisp form:
  13619. will be used as value for `org-agenda-skip-function', so
  13620. whenever the function returns a position, FUNC will not be
  13621. called for that entry and search will continue from the
  13622. position returned
  13623. If your function needs to retrieve the tags including inherited tags
  13624. at the *current* entry, you can use the value of the variable
  13625. `org-scanner-tags' which will be much faster than getting the value
  13626. with `org-get-tags-at'. If your function gets properties with
  13627. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  13628. to t around the call to `org-entry-properties' to get the same speedup.
  13629. Note that if your function moves around to retrieve tags and properties at
  13630. a *different* entry, you cannot use these techniques."
  13631. (unless (and (or (eq scope 'region) (eq scope 'region-start-level))
  13632. (not (org-region-active-p)))
  13633. (let* ((org-agenda-archives-mode nil) ; just to make sure
  13634. (org-agenda-skip-archived-trees (memq 'archive skip))
  13635. (org-agenda-skip-comment-trees (memq 'comment skip))
  13636. (org-agenda-skip-function
  13637. (car (org-delete-all '(comment archive) skip)))
  13638. (org-tags-match-list-sublevels t)
  13639. (start-level (eq scope 'region-start-level))
  13640. matcher file res
  13641. org-todo-keywords-for-agenda
  13642. org-done-keywords-for-agenda
  13643. org-todo-keyword-alist-for-agenda
  13644. org-drawers-for-agenda
  13645. org-tag-alist-for-agenda
  13646. todo-only)
  13647. (cond
  13648. ((eq match t) (setq matcher t))
  13649. ((eq match nil) (setq matcher t))
  13650. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  13651. (save-window-excursion
  13652. (save-restriction
  13653. (cond ((eq scope 'tree)
  13654. (org-back-to-heading t)
  13655. (org-narrow-to-subtree)
  13656. (setq scope nil))
  13657. ((and (or (eq scope 'region) (eq scope 'region-start-level))
  13658. (org-region-active-p))
  13659. ;; If needed, set start-level to a string like "2"
  13660. (when start-level
  13661. (save-excursion
  13662. (goto-char (region-beginning))
  13663. (unless (org-at-heading-p) (outline-next-heading))
  13664. (setq start-level (org-current-level))))
  13665. (narrow-to-region (region-beginning)
  13666. (save-excursion
  13667. (goto-char (region-end))
  13668. (unless (and (bolp) (org-at-heading-p))
  13669. (outline-next-heading))
  13670. (point)))
  13671. (setq scope nil)))
  13672. (if (not scope)
  13673. (progn
  13674. (org-agenda-prepare-buffers
  13675. (list (buffer-file-name (current-buffer))))
  13676. (setq res (org-scan-tags func matcher todo-only start-level)))
  13677. ;; Get the right scope
  13678. (cond
  13679. ((and scope (listp scope) (symbolp (car scope)))
  13680. (setq scope (eval scope)))
  13681. ((eq scope 'agenda)
  13682. (setq scope (org-agenda-files t)))
  13683. ((eq scope 'agenda-with-archives)
  13684. (setq scope (org-agenda-files t))
  13685. (setq scope (org-add-archive-files scope)))
  13686. ((eq scope 'file)
  13687. (setq scope (list (buffer-file-name))))
  13688. ((eq scope 'file-with-archives)
  13689. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  13690. (org-agenda-prepare-buffers scope)
  13691. (while (setq file (pop scope))
  13692. (with-current-buffer (org-find-base-buffer-visiting file)
  13693. (save-excursion
  13694. (save-restriction
  13695. (widen)
  13696. (goto-char (point-min))
  13697. (setq res (append res (org-scan-tags func matcher todo-only))))))))))
  13698. res)))
  13699. ;;;; Properties
  13700. ;;; Setting and retrieving properties
  13701. (defconst org-special-properties
  13702. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  13703. "TIMESTAMP" "TIMESTAMP_IA" "BLOCKED" "FILE" "CLOCKSUM" "CLOCKSUM_T")
  13704. "The special properties valid in Org-mode.
  13705. These are properties that are not defined in the property drawer,
  13706. but in some other way.")
  13707. (defconst org-default-properties
  13708. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  13709. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  13710. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  13711. "EXPORT_OPTIONS" "EXPORT_TEXT" "EXPORT_FILE_NAME"
  13712. "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
  13713. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER" "REPEAT_TO_STATE"
  13714. "CLOCK_MODELINE_TOTAL" "STYLE" "HTML_CONTAINER_CLASS")
  13715. "Some properties that are used by Org-mode for various purposes.
  13716. Being in this list makes sure that they are offered for completion.")
  13717. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  13718. "Regular expression matching the first line of a property drawer.")
  13719. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  13720. "Regular expression matching the last line of a property drawer.")
  13721. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  13722. "Regular expression matching the first line of a property drawer.")
  13723. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  13724. "Regular expression matching the first line of a property drawer.")
  13725. (defconst org-property-drawer-re
  13726. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  13727. org-property-end-re "\\)\n?")
  13728. "Matches an entire property drawer.")
  13729. (defconst org-clock-drawer-re
  13730. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  13731. org-property-end-re "\\)\n?")
  13732. "Matches an entire clock drawer.")
  13733. (defsubst org-re-property (property)
  13734. "Return a regexp matching a PROPERTY line.
  13735. Match group 1 will be set to the value."
  13736. (concat "^[ \t]*:" (regexp-quote property) ":[ \t]*\\(\\S-.*\\)"))
  13737. (defsubst org-re-property-keyword (property)
  13738. "Return a regexp matching a PROPERTY line, possibly with no
  13739. value for the property."
  13740. (concat "^[ \t]*:" (regexp-quote property) ":[ \t]*\\(\\S-.*\\)?"))
  13741. (defun org-property-action ()
  13742. "Do an action on properties."
  13743. (interactive)
  13744. (let (c)
  13745. (org-at-property-p)
  13746. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  13747. (setq c (read-char-exclusive))
  13748. (cond
  13749. ((equal c ?s)
  13750. (call-interactively 'org-set-property))
  13751. ((equal c ?d)
  13752. (call-interactively 'org-delete-property))
  13753. ((equal c ?D)
  13754. (call-interactively 'org-delete-property-globally))
  13755. ((equal c ?c)
  13756. (call-interactively 'org-compute-property-at-point))
  13757. (t (user-error "No such property action %c" c)))))
  13758. (defun org-inc-effort ()
  13759. "Increment the value of the effort property in the current entry."
  13760. (interactive)
  13761. (org-set-effort nil t))
  13762. (defvar org-clock-effort) ;; Defined in org-clock.el
  13763. (defvar org-clock-current-task) ;; Defined in org-clock.el
  13764. (defun org-set-effort (&optional value increment)
  13765. "Set the effort property of the current entry.
  13766. With numerical prefix arg, use the nth allowed value, 0 stands for the
  13767. 10th allowed value.
  13768. When INCREMENT is non-nil, set the property to the next allowed value."
  13769. (interactive "P")
  13770. (if (equal value 0) (setq value 10))
  13771. (let* ((completion-ignore-case t)
  13772. (prop org-effort-property)
  13773. (cur (org-entry-get nil prop))
  13774. (allowed (org-property-get-allowed-values nil prop 'table))
  13775. (existing (mapcar 'list (org-property-values prop)))
  13776. (heading (nth 4 (org-heading-components)))
  13777. rpl
  13778. (val (cond
  13779. ((stringp value) value)
  13780. ((and allowed (integerp value))
  13781. (or (car (nth (1- value) allowed))
  13782. (car (org-last allowed))))
  13783. ((and allowed increment)
  13784. (or (caadr (member (list cur) allowed))
  13785. (user-error "Allowed effort values are not set")))
  13786. (allowed
  13787. (message "Select 1-9,0, [RET%s]: %s"
  13788. (if cur (concat "=" cur) "")
  13789. (mapconcat 'car allowed " "))
  13790. (setq rpl (read-char-exclusive))
  13791. (if (equal rpl ?\r)
  13792. cur
  13793. (setq rpl (- rpl ?0))
  13794. (if (equal rpl 0) (setq rpl 10))
  13795. (if (and (> rpl 0) (<= rpl (length allowed)))
  13796. (car (nth (1- rpl) allowed))
  13797. (org-completing-read "Effort: " allowed nil))))
  13798. (t
  13799. (let (org-completion-use-ido org-completion-use-iswitchb)
  13800. (org-completing-read
  13801. (concat "Effort " (if (and cur (string-match "\\S-" cur))
  13802. (concat "[" cur "]") "")
  13803. ": ")
  13804. existing nil nil "" nil cur))))))
  13805. (unless (equal (org-entry-get nil prop) val)
  13806. (org-entry-put nil prop val))
  13807. (save-excursion
  13808. (org-back-to-heading t)
  13809. (put-text-property (point-at-bol) (point-at-eol) 'org-effort val))
  13810. (when (string= heading org-clock-current-task)
  13811. (setq org-clock-effort (get-text-property (point-at-bol) 'org-effort))
  13812. (org-clock-update-mode-line))
  13813. (message "%s is now %s" prop val)))
  13814. (defun org-at-property-p ()
  13815. "Is cursor inside a property drawer?"
  13816. (save-excursion
  13817. (beginning-of-line 1)
  13818. (when (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))
  13819. (save-match-data ;; Used by calling procedures
  13820. (let ((p (point))
  13821. (range (unless (org-before-first-heading-p)
  13822. (org-get-property-block))))
  13823. (and range (<= (car range) p) (< p (cdr range))))))))
  13824. (defun org-get-property-block (&optional beg end force)
  13825. "Return the (beg . end) range of the body of the property drawer.
  13826. BEG and END are the beginning and end of the current subtree, or of
  13827. the part before the first headline. If they are not given, they will
  13828. be found. If the drawer does not exist and FORCE is non-nil, create
  13829. the drawer."
  13830. (catch 'exit
  13831. (save-excursion
  13832. (let* ((beg (or beg (and (org-before-first-heading-p) (point-min))
  13833. (progn (org-back-to-heading t) (point))))
  13834. (end (or end (and (not (outline-next-heading)) (point-max))
  13835. (point))))
  13836. (goto-char beg)
  13837. (if (re-search-forward org-property-start-re end t)
  13838. (setq beg (1+ (match-end 0)))
  13839. (if force
  13840. (save-excursion
  13841. (org-insert-property-drawer)
  13842. (setq end (progn (outline-next-heading) (point))))
  13843. (throw 'exit nil))
  13844. (goto-char beg)
  13845. (if (re-search-forward org-property-start-re end t)
  13846. (setq beg (1+ (match-end 0)))))
  13847. (if (re-search-forward org-property-end-re end t)
  13848. (setq end (match-beginning 0))
  13849. (or force (throw 'exit nil))
  13850. (goto-char beg)
  13851. (setq end beg)
  13852. (org-indent-line)
  13853. (insert ":END:\n"))
  13854. (cons beg end)))))
  13855. (defun org-entry-properties (&optional pom which specific)
  13856. "Get all properties of the entry at point-or-marker POM.
  13857. This includes the TODO keyword, the tags, time strings for deadline,
  13858. scheduled, and clocking, and any additional properties defined in the
  13859. entry. The return value is an alist, keys may occur multiple times
  13860. if the property key was used several times.
  13861. POM may also be nil, in which case the current entry is used.
  13862. If WHICH is nil or `all', get all properties. If WHICH is
  13863. `special' or `standard', only get that subclass. If WHICH
  13864. is a string only get exactly this property. SPECIFIC can be a string, the
  13865. specific property we are interested in. Specifying it can speed
  13866. things up because then unnecessary parsing is avoided."
  13867. (setq which (or which 'all))
  13868. (org-with-point-at pom
  13869. (let ((clockstr (substring org-clock-string 0 -1))
  13870. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY" "BLOCKED"))
  13871. (case-fold-search nil)
  13872. beg end range props sum-props key key1 value string clocksum clocksumt)
  13873. (save-excursion
  13874. (when (condition-case nil
  13875. (and (derived-mode-p 'org-mode) (org-back-to-heading t))
  13876. (error nil))
  13877. (setq beg (point))
  13878. (setq sum-props (get-text-property (point) 'org-summaries))
  13879. (setq clocksum (get-text-property (point) :org-clock-minutes)
  13880. clocksumt (get-text-property (point) :org-clock-minutes-today))
  13881. (outline-next-heading)
  13882. (setq end (point))
  13883. (when (memq which '(all special))
  13884. ;; Get the special properties, like TODO and tags
  13885. (goto-char beg)
  13886. (when (and (or (not specific) (string= specific "TODO"))
  13887. (looking-at org-todo-line-regexp) (match-end 2))
  13888. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  13889. (when (and (or (not specific) (string= specific "PRIORITY"))
  13890. (looking-at org-priority-regexp))
  13891. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  13892. (when (or (not specific) (string= specific "FILE"))
  13893. (push (cons "FILE" buffer-file-name) props))
  13894. (when (and (or (not specific) (string= specific "TAGS"))
  13895. (setq value (org-get-tags-string))
  13896. (string-match "\\S-" value))
  13897. (push (cons "TAGS" value) props))
  13898. (when (and (or (not specific) (string= specific "ALLTAGS"))
  13899. (setq value (org-get-tags-at)))
  13900. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":")
  13901. ":"))
  13902. props))
  13903. (when (or (not specific) (string= specific "BLOCKED"))
  13904. (push (cons "BLOCKED" (if (org-entry-blocked-p) "t" "")) props))
  13905. (when (or (not specific)
  13906. (member specific
  13907. '("SCHEDULED" "DEADLINE" "CLOCK" "CLOSED"
  13908. "TIMESTAMP" "TIMESTAMP_IA")))
  13909. (catch 'match
  13910. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  13911. (setq key (if (match-end 1)
  13912. (substring (org-match-string-no-properties 1)
  13913. 0 -1))
  13914. string (if (equal key clockstr)
  13915. (org-trim
  13916. (buffer-substring-no-properties
  13917. (match-beginning 3) (goto-char
  13918. (point-at-eol))))
  13919. (substring (org-match-string-no-properties 3)
  13920. 1 -1)))
  13921. ;; Get the correct property name from the key. This is
  13922. ;; necessary if the user has configured time keywords.
  13923. (setq key1 (concat key ":"))
  13924. (cond
  13925. ((not key)
  13926. (setq key
  13927. (if (= (char-after (match-beginning 3)) ?\[)
  13928. "TIMESTAMP_IA" "TIMESTAMP")))
  13929. ((equal key1 org-scheduled-string) (setq key "SCHEDULED"))
  13930. ((equal key1 org-deadline-string) (setq key "DEADLINE"))
  13931. ((equal key1 org-closed-string) (setq key "CLOSED"))
  13932. ((equal key1 org-clock-string) (setq key "CLOCK")))
  13933. (if (and specific (equal key specific) (not (equal key "CLOCK")))
  13934. (progn
  13935. (push (cons key string) props)
  13936. ;; no need to search further if match is found
  13937. (throw 'match t))
  13938. (when (or (equal key "CLOCK") (not (assoc key props)))
  13939. (push (cons key string) props)))))))
  13940. (when (memq which '(all standard))
  13941. ;; Get the standard properties, like :PROP: ...
  13942. (setq range (org-get-property-block beg end))
  13943. (when range
  13944. (goto-char (car range))
  13945. (while (re-search-forward
  13946. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  13947. (cdr range) t)
  13948. (setq key (org-match-string-no-properties 1)
  13949. value (org-trim (or (org-match-string-no-properties 2) "")))
  13950. (unless (member key excluded)
  13951. (push (cons key (or value "")) props)))))
  13952. (if clocksum
  13953. (push (cons "CLOCKSUM"
  13954. (org-columns-number-to-string (/ (float clocksum) 60.)
  13955. 'add_times))
  13956. props))
  13957. (if clocksumt
  13958. (push (cons "CLOCKSUM_T"
  13959. (org-columns-number-to-string (/ (float clocksumt) 60.)
  13960. 'add_times))
  13961. props))
  13962. (unless (assoc "CATEGORY" props)
  13963. (push (cons "CATEGORY" (org-get-category)) props))
  13964. (append sum-props (nreverse props)))))))
  13965. (defun org-entry-get (pom property &optional inherit literal-nil)
  13966. "Get value of PROPERTY for entry or content at point-or-marker POM.
  13967. If INHERIT is non-nil and the entry does not have the property,
  13968. then also check higher levels of the hierarchy.
  13969. If INHERIT is the symbol `selective', use inheritance only if the setting
  13970. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  13971. If the property is present but empty, the return value is the empty string.
  13972. If the property is not present at all, nil is returned.
  13973. If LITERAL-NIL is set, return the string value \"nil\" as a string,
  13974. do not interpret it as the list atom nil. This is used for inheritance
  13975. when a \"nil\" value can supersede a non-nil value higher up the hierarchy."
  13976. (org-with-point-at pom
  13977. (if (and inherit (if (eq inherit 'selective)
  13978. (org-property-inherit-p property)
  13979. t))
  13980. (org-entry-get-with-inheritance property literal-nil)
  13981. (if (member property org-special-properties)
  13982. ;; We need a special property. Use `org-entry-properties' to
  13983. ;; retrieve it, but specify the wanted property
  13984. (cdr (assoc property (org-entry-properties nil 'special property)))
  13985. (let ((range (org-get-property-block)))
  13986. (when (and range (not (eq (car range) (cdr range))))
  13987. (let* ((props (list (or (assoc property org-file-properties)
  13988. (assoc property org-global-properties)
  13989. (assoc property org-global-properties-fixed))))
  13990. (ap (lambda (key)
  13991. (when (re-search-forward
  13992. (org-re-property key) (cdr range) t)
  13993. (setq props
  13994. (org-update-property-plist
  13995. key
  13996. (if (match-end 1)
  13997. (org-match-string-no-properties 1) "")
  13998. props)))))
  13999. val)
  14000. (goto-char (car range))
  14001. (funcall ap property)
  14002. (goto-char (car range))
  14003. (while (funcall ap (concat property "+")))
  14004. (setq val (cdr (assoc property props)))
  14005. (when val (if literal-nil val (org-not-nil val))))))))))
  14006. (defun org-property-or-variable-value (var &optional inherit)
  14007. "Check if there is a property fixing the value of VAR.
  14008. If yes, return this value. If not, return the current value of the variable."
  14009. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  14010. (if (and prop (stringp prop) (string-match "\\S-" prop))
  14011. (read prop)
  14012. (symbol-value var))))
  14013. (defun org-entry-delete (pom property &optional delete-empty-drawer)
  14014. "Delete the property PROPERTY from entry at point-or-marker POM.
  14015. When optional argument DELETE-EMPTY-DRAWER is a string, it defines
  14016. an empty drawer to delete."
  14017. (org-with-point-at pom
  14018. (if (member property org-special-properties)
  14019. nil ; cannot delete these properties.
  14020. (let ((range (org-get-property-block)))
  14021. (if (and range
  14022. (goto-char (car range))
  14023. (re-search-forward
  14024. (org-re-property property)
  14025. (cdr range) t))
  14026. (progn
  14027. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  14028. (and delete-empty-drawer
  14029. (org-remove-empty-drawer-at
  14030. delete-empty-drawer (car range)))
  14031. t)
  14032. nil)))))
  14033. ;; Multi-values properties are properties that contain multiple values
  14034. ;; These values are assumed to be single words, separated by whitespace.
  14035. (defun org-entry-add-to-multivalued-property (pom property value)
  14036. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  14037. (let* ((old (org-entry-get pom property))
  14038. (values (and old (org-split-string old "[ \t]"))))
  14039. (setq value (org-entry-protect-space value))
  14040. (unless (member value values)
  14041. (setq values (append values (list value)))
  14042. (org-entry-put pom property
  14043. (mapconcat 'identity values " ")))))
  14044. (defun org-entry-remove-from-multivalued-property (pom property value)
  14045. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  14046. (let* ((old (org-entry-get pom property))
  14047. (values (and old (org-split-string old "[ \t]"))))
  14048. (setq value (org-entry-protect-space value))
  14049. (when (member value values)
  14050. (setq values (delete value values))
  14051. (org-entry-put pom property
  14052. (mapconcat 'identity values " ")))))
  14053. (defun org-entry-member-in-multivalued-property (pom property value)
  14054. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  14055. (let* ((old (org-entry-get pom property))
  14056. (values (and old (org-split-string old "[ \t]"))))
  14057. (setq value (org-entry-protect-space value))
  14058. (member value values)))
  14059. (defun org-entry-get-multivalued-property (pom property)
  14060. "Return a list of values in a multivalued property."
  14061. (let* ((value (org-entry-get pom property))
  14062. (values (and value (org-split-string value "[ \t]"))))
  14063. (mapcar 'org-entry-restore-space values)))
  14064. (defun org-entry-put-multivalued-property (pom property &rest values)
  14065. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  14066. VALUES should be a list of strings. Spaces will be protected."
  14067. (org-entry-put pom property
  14068. (mapconcat 'org-entry-protect-space values " "))
  14069. (let* ((value (org-entry-get pom property))
  14070. (values (and value (org-split-string value "[ \t]"))))
  14071. (mapcar 'org-entry-restore-space values)))
  14072. (defun org-entry-protect-space (s)
  14073. "Protect spaces and newline in string S."
  14074. (while (string-match " " s)
  14075. (setq s (replace-match "%20" t t s)))
  14076. (while (string-match "\n" s)
  14077. (setq s (replace-match "%0A" t t s)))
  14078. s)
  14079. (defun org-entry-restore-space (s)
  14080. "Restore spaces and newline in string S."
  14081. (while (string-match "%20" s)
  14082. (setq s (replace-match " " t t s)))
  14083. (while (string-match "%0A" s)
  14084. (setq s (replace-match "\n" t t s)))
  14085. s)
  14086. (defvar org-entry-property-inherited-from (make-marker)
  14087. "Marker pointing to the entry from where a property was inherited.
  14088. Each call to `org-entry-get-with-inheritance' will set this marker to the
  14089. location of the entry where the inheritance search matched. If there was
  14090. no match, the marker will point nowhere.
  14091. Note that also `org-entry-get' calls this function, if the INHERIT flag
  14092. is set.")
  14093. (defun org-entry-get-with-inheritance (property &optional literal-nil)
  14094. "Get PROPERTY of entry or content at point, search higher levels if needed.
  14095. The search will stop at the first ancestor which has the property defined.
  14096. If the value found is \"nil\", return nil to show that the property
  14097. should be considered as undefined (this is the meaning of nil here).
  14098. However, if LITERAL-NIL is set, return the string value \"nil\" instead."
  14099. (move-marker org-entry-property-inherited-from nil)
  14100. (let (tmp)
  14101. (save-excursion
  14102. (save-restriction
  14103. (widen)
  14104. (catch 'ex
  14105. (while t
  14106. (when (setq tmp (org-entry-get nil property nil 'literal-nil))
  14107. (or (ignore-errors (org-back-to-heading t))
  14108. (goto-char (point-min)))
  14109. (move-marker org-entry-property-inherited-from (point))
  14110. (throw 'ex tmp))
  14111. (or (ignore-errors (org-up-heading-safe))
  14112. (throw 'ex nil))))))
  14113. (setq tmp (or tmp
  14114. (cdr (assoc property org-file-properties))
  14115. (cdr (assoc property org-global-properties))
  14116. (cdr (assoc property org-global-properties-fixed))))
  14117. (if literal-nil tmp (org-not-nil tmp))))
  14118. (defvar org-property-changed-functions nil
  14119. "Hook called when the value of a property has changed.
  14120. Each hook function should accept two arguments, the name of the property
  14121. and the new value.")
  14122. (defun org-entry-put (pom property value)
  14123. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  14124. (org-with-point-at pom
  14125. (org-back-to-heading t)
  14126. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  14127. range)
  14128. (cond
  14129. ((equal property "TODO")
  14130. (when (and (stringp value) (string-match "\\S-" value)
  14131. (not (member value org-todo-keywords-1)))
  14132. (user-error "\"%s\" is not a valid TODO state" value))
  14133. (if (or (not value)
  14134. (not (string-match "\\S-" value)))
  14135. (setq value 'none))
  14136. (org-todo value)
  14137. (org-set-tags nil 'align))
  14138. ((equal property "PRIORITY")
  14139. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  14140. (string-to-char value) ?\ ))
  14141. (org-set-tags nil 'align))
  14142. ((equal property "CLOCKSUM")
  14143. (if (not (re-search-forward
  14144. (concat org-clock-string ".*\\]--\\(\\[[^]]+\\]\\)") nil t))
  14145. (error "Cannot find a clock log")
  14146. (goto-char (- (match-end 1) 2))
  14147. (cond
  14148. ((eq value 'earlier) (org-timestamp-down))
  14149. ((eq value 'later) (org-timestamp-up)))
  14150. (org-clock-sum-current-item)))
  14151. ((equal property "SCHEDULED")
  14152. (if (re-search-forward org-scheduled-time-regexp end t)
  14153. (cond
  14154. ((eq value 'earlier) (org-timestamp-change -1 'day))
  14155. ((eq value 'later) (org-timestamp-change 1 'day))
  14156. (t (call-interactively 'org-schedule)))
  14157. (call-interactively 'org-schedule)))
  14158. ((equal property "DEADLINE")
  14159. (if (re-search-forward org-deadline-time-regexp end t)
  14160. (cond
  14161. ((eq value 'earlier) (org-timestamp-change -1 'day))
  14162. ((eq value 'later) (org-timestamp-change 1 'day))
  14163. (t (call-interactively 'org-deadline)))
  14164. (call-interactively 'org-deadline)))
  14165. ((member property org-special-properties)
  14166. (error "The %s property can not yet be set with `org-entry-put'"
  14167. property))
  14168. (t ; a non-special property
  14169. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  14170. (setq range (org-get-property-block beg end 'force))
  14171. (goto-char (car range))
  14172. (if (re-search-forward
  14173. (org-re-property-keyword property) (cdr range) t)
  14174. (progn
  14175. (delete-region (match-beginning 0) (match-end 0))
  14176. (goto-char (match-beginning 0)))
  14177. (goto-char (cdr range))
  14178. (insert "\n")
  14179. (backward-char 1)
  14180. (org-indent-line))
  14181. (insert ":" property ":")
  14182. (and value (insert " " value))
  14183. (org-indent-line)))))
  14184. (run-hook-with-args 'org-property-changed-functions property value)))
  14185. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  14186. "Get all property keys in the current buffer.
  14187. With INCLUDE-SPECIALS, also list the special properties that reflect things
  14188. like tags and TODO state.
  14189. With INCLUDE-DEFAULTS, also include properties that has special meaning
  14190. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING
  14191. and others.
  14192. With INCLUDE-COLUMNS, also include property names given in COLUMN
  14193. formats in the current buffer."
  14194. (let (rtn range cfmt s p)
  14195. (save-excursion
  14196. (save-restriction
  14197. (widen)
  14198. (goto-char (point-min))
  14199. (while (re-search-forward org-property-start-re nil t)
  14200. (setq range (org-get-property-block))
  14201. (goto-char (car range))
  14202. (while (re-search-forward
  14203. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  14204. (cdr range) t)
  14205. (add-to-list 'rtn (org-match-string-no-properties 1)))
  14206. (outline-next-heading))))
  14207. (when include-specials
  14208. (setq rtn (append org-special-properties rtn)))
  14209. (when include-defaults
  14210. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties)
  14211. (add-to-list 'rtn org-effort-property))
  14212. (when include-columns
  14213. (save-excursion
  14214. (save-restriction
  14215. (widen)
  14216. (goto-char (point-min))
  14217. (while (re-search-forward
  14218. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  14219. nil t)
  14220. (setq cfmt (match-string 2) s 0)
  14221. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  14222. cfmt s)
  14223. (setq s (match-end 0)
  14224. p (match-string 1 cfmt))
  14225. (unless (or (equal p "ITEM")
  14226. (member p org-special-properties))
  14227. (add-to-list 'rtn (match-string 1 cfmt))))))))
  14228. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  14229. (defun org-property-values (key)
  14230. "Return a list of all values of property KEY in the current buffer."
  14231. (save-excursion
  14232. (save-restriction
  14233. (widen)
  14234. (goto-char (point-min))
  14235. (let ((re (org-re-property key))
  14236. values)
  14237. (while (re-search-forward re nil t)
  14238. (add-to-list 'values (org-trim (match-string 1))))
  14239. (delete "" values)))))
  14240. (defun org-insert-property-drawer ()
  14241. "Insert a property drawer into the current entry."
  14242. (org-back-to-heading t)
  14243. (looking-at org-outline-regexp)
  14244. (let ((indent (if org-adapt-indentation
  14245. (- (match-end 0) (match-beginning 0))
  14246. 0))
  14247. (beg (point))
  14248. (re (concat "^[ \t]*" org-keyword-time-regexp))
  14249. end hiddenp)
  14250. (outline-next-heading)
  14251. (setq end (point))
  14252. (goto-char beg)
  14253. (while (re-search-forward re end t))
  14254. (setq hiddenp (outline-invisible-p))
  14255. (end-of-line 1)
  14256. (and (equal (char-after) ?\n) (forward-char 1))
  14257. (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
  14258. (if (member (match-string 1) '("CLOCK:" ":END:"))
  14259. ;; just skip this line
  14260. (beginning-of-line 2)
  14261. ;; Drawer start, find the end
  14262. (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
  14263. (beginning-of-line 1)))
  14264. (org-skip-over-state-notes)
  14265. (skip-chars-backward " \t\n\r")
  14266. (if (eq (char-before) ?*) (forward-char 1))
  14267. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  14268. (beginning-of-line 0)
  14269. (org-indent-to-column indent)
  14270. (beginning-of-line 2)
  14271. (org-indent-to-column indent)
  14272. (beginning-of-line 0)
  14273. (if hiddenp
  14274. (save-excursion
  14275. (org-back-to-heading t)
  14276. (hide-entry))
  14277. (org-flag-drawer t))))
  14278. (defun org-insert-drawer (&optional arg drawer)
  14279. "Insert a drawer at point.
  14280. Optional argument DRAWER, when non-nil, is a string representing
  14281. drawer's name. Otherwise, the user is prompted for a name.
  14282. If a region is active, insert the drawer around that region
  14283. instead.
  14284. Point is left between drawer's boundaries."
  14285. (interactive "P")
  14286. (let* ((logbook (if (stringp org-log-into-drawer) org-log-into-drawer
  14287. "LOGBOOK"))
  14288. ;; SYSTEM-DRAWERS is a list of drawer names that are used
  14289. ;; internally by Org. They are meant to be inserted
  14290. ;; automatically.
  14291. (system-drawers `("CLOCK" ,logbook "PROPERTIES"))
  14292. ;; Remove system drawers from list. Note: For some reason,
  14293. ;; `org-completing-read' ignores the predicate while
  14294. ;; `completing-read' handles it fine.
  14295. (drawer (if arg "PROPERTIES"
  14296. (or drawer
  14297. (completing-read
  14298. "Drawer: " org-drawers
  14299. (lambda (d) (not (member d system-drawers))))))))
  14300. (cond
  14301. ;; With C-u, fall back on `org-insert-property-drawer'
  14302. (arg (org-insert-property-drawer))
  14303. ;; With an active region, insert a drawer at point.
  14304. ((not (org-region-active-p))
  14305. (progn
  14306. (unless (bolp) (insert "\n"))
  14307. (insert (format ":%s:\n\n:END:\n" drawer))
  14308. (forward-line -2)))
  14309. ;; Otherwise, insert the drawer at point
  14310. (t
  14311. (let ((rbeg (region-beginning))
  14312. (rend (copy-marker (region-end))))
  14313. (unwind-protect
  14314. (progn
  14315. (goto-char rbeg)
  14316. (beginning-of-line)
  14317. (when (save-excursion
  14318. (re-search-forward org-outline-regexp-bol rend t))
  14319. (user-error "Drawers cannot contain headlines"))
  14320. ;; Position point at the beginning of the first
  14321. ;; non-blank line in region. Insert drawer's opening
  14322. ;; there, then indent it.
  14323. (org-skip-whitespace)
  14324. (beginning-of-line)
  14325. (insert ":" drawer ":\n")
  14326. (forward-line -1)
  14327. (indent-for-tab-command)
  14328. ;; Move point to the beginning of the first blank line
  14329. ;; after the last non-blank line in region. Insert
  14330. ;; drawer's closing, then indent it.
  14331. (goto-char rend)
  14332. (skip-chars-backward " \r\t\n")
  14333. (insert "\n:END:")
  14334. (deactivate-mark t)
  14335. (indent-for-tab-command)
  14336. (unless (eolp) (insert "\n")))
  14337. ;; Clear marker, whatever the outcome of insertion is.
  14338. (set-marker rend nil)))))))
  14339. (defvar org-property-set-functions-alist nil
  14340. "Property set function alist.
  14341. Each entry should have the following format:
  14342. (PROPERTY . READ-FUNCTION)
  14343. The read function will be called with the same argument as
  14344. `org-completing-read'.")
  14345. (defun org-set-property-function (property)
  14346. "Get the function that should be used to set PROPERTY.
  14347. This is computed according to `org-property-set-functions-alist'."
  14348. (or (cdr (assoc property org-property-set-functions-alist))
  14349. 'org-completing-read))
  14350. (defun org-read-property-value (property)
  14351. "Read PROPERTY value from user."
  14352. (let* ((completion-ignore-case t)
  14353. (allowed (org-property-get-allowed-values nil property 'table))
  14354. (cur (org-entry-get nil property))
  14355. (prompt (concat property " value"
  14356. (if (and cur (string-match "\\S-" cur))
  14357. (concat " [" cur "]") "") ": "))
  14358. (set-function (org-set-property-function property))
  14359. (val (if allowed
  14360. (funcall set-function prompt allowed nil
  14361. (not (get-text-property 0 'org-unrestricted
  14362. (caar allowed))))
  14363. (let (org-completion-use-ido org-completion-use-iswitchb)
  14364. (funcall set-function prompt
  14365. (mapcar 'list (org-property-values property))
  14366. nil nil "" nil cur)))))
  14367. (if (equal val "")
  14368. cur
  14369. val)))
  14370. (defvar org-last-set-property nil)
  14371. (defvar org-last-set-property-value nil)
  14372. (defun org-read-property-name ()
  14373. "Read a property name."
  14374. (let* ((completion-ignore-case t)
  14375. (keys (org-buffer-property-keys nil t t))
  14376. (default-prop (or (save-excursion
  14377. (save-match-data
  14378. (beginning-of-line)
  14379. (and (looking-at "^\\s-*:\\([^:\n]+\\):")
  14380. (null (string= (match-string 1) "END"))
  14381. (match-string 1))))
  14382. org-last-set-property))
  14383. (property (org-icompleting-read
  14384. (concat "Property"
  14385. (if default-prop (concat " [" default-prop "]") "")
  14386. ": ")
  14387. (mapcar 'list keys)
  14388. nil nil nil nil
  14389. default-prop)))
  14390. (if (member property keys)
  14391. property
  14392. (or (cdr (assoc (downcase property)
  14393. (mapcar (lambda (x) (cons (downcase x) x))
  14394. keys)))
  14395. property))))
  14396. (defun org-set-property-and-value (use-last)
  14397. "Allow to set [PROPERTY]: [value] direction from prompt.
  14398. When use-default, don't even ask, just use the last
  14399. \"[PROPERTY]: [value]\" string from the history."
  14400. (interactive "P")
  14401. (let* ((completion-ignore-case t)
  14402. (pv (or (and use-last org-last-set-property-value)
  14403. (org-completing-read
  14404. "Enter a \"[Property]: [value]\" pair: "
  14405. nil nil nil nil nil
  14406. org-last-set-property-value)))
  14407. prop val)
  14408. (when (string-match "^[ \t]*\\([^:]+\\):[ \t]*\\(.*\\)[ \t]*$" pv)
  14409. (setq prop (match-string 1 pv)
  14410. val (match-string 2 pv))
  14411. (org-set-property prop val))))
  14412. (defun org-set-property (property value)
  14413. "In the current entry, set PROPERTY to VALUE.
  14414. When called interactively, this will prompt for a property name, offering
  14415. completion on existing and default properties. And then it will prompt
  14416. for a value, offering completion either on allowed values (via an inherited
  14417. xxx_ALL property) or on existing values in other instances of this property
  14418. in the current file."
  14419. (interactive (list nil nil))
  14420. (let* ((property (or property (org-read-property-name)))
  14421. (value (or value (org-read-property-value property)))
  14422. (fn (cdr (assoc property org-properties-postprocess-alist))))
  14423. (setq org-last-set-property property)
  14424. (setq org-last-set-property-value (concat property ": " value))
  14425. ;; Possibly postprocess the inserted value:
  14426. (when fn (setq value (funcall fn value)))
  14427. (unless (equal (org-entry-get nil property) value)
  14428. (org-entry-put nil property value))))
  14429. (defun org-delete-property (property &optional delete-empty-drawer)
  14430. "In the current entry, delete PROPERTY.
  14431. When optional argument DELETE-EMPTY-DRAWER is a string, it defines
  14432. an empty drawer to delete."
  14433. (interactive
  14434. (let* ((completion-ignore-case t)
  14435. (prop (org-icompleting-read "Property: "
  14436. (org-entry-properties nil 'standard))))
  14437. (list prop)))
  14438. (message "Property %s %s" property
  14439. (if (org-entry-delete nil property delete-empty-drawer)
  14440. "deleted"
  14441. "was not present in the entry")))
  14442. (defun org-delete-property-globally (property)
  14443. "Remove PROPERTY globally, from all entries."
  14444. (interactive
  14445. (let* ((completion-ignore-case t)
  14446. (prop (org-icompleting-read
  14447. "Globally remove property: "
  14448. (mapcar 'list (org-buffer-property-keys)))))
  14449. (list prop)))
  14450. (save-excursion
  14451. (save-restriction
  14452. (widen)
  14453. (goto-char (point-min))
  14454. (let ((cnt 0))
  14455. (while (re-search-forward
  14456. (org-re-property property)
  14457. nil t)
  14458. (setq cnt (1+ cnt))
  14459. (delete-region (match-beginning 0) (1+ (point-at-eol))))
  14460. (message "Property \"%s\" removed from %d entries" property cnt)))))
  14461. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  14462. (defun org-compute-property-at-point ()
  14463. "Compute the property at point.
  14464. This looks for an enclosing column format, extracts the operator and
  14465. then applies it to the property in the column format's scope."
  14466. (interactive)
  14467. (unless (org-at-property-p)
  14468. (user-error "Not at a property"))
  14469. (let ((prop (org-match-string-no-properties 2)))
  14470. (org-columns-get-format-and-top-level)
  14471. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  14472. (user-error "No operator defined for property %s" prop))
  14473. (org-columns-compute prop)))
  14474. (defvar org-property-allowed-value-functions nil
  14475. "Hook for functions supplying allowed values for a specific property.
  14476. The functions must take a single argument, the name of the property, and
  14477. return a flat list of allowed values. If \":ETC\" is one of
  14478. the values, this means that these values are intended as defaults for
  14479. completion, but that other values should be allowed too.
  14480. The functions must return nil if they are not responsible for this
  14481. property.")
  14482. (defun org-property-get-allowed-values (pom property &optional table)
  14483. "Get allowed values for the property PROPERTY.
  14484. When TABLE is non-nil, return an alist that can directly be used for
  14485. completion."
  14486. (let (vals)
  14487. (cond
  14488. ((equal property "TODO")
  14489. (setq vals (org-with-point-at pom
  14490. (append org-todo-keywords-1 '("")))))
  14491. ((equal property "PRIORITY")
  14492. (let ((n org-lowest-priority))
  14493. (while (>= n org-highest-priority)
  14494. (push (char-to-string n) vals)
  14495. (setq n (1- n)))))
  14496. ((member property org-special-properties))
  14497. ((setq vals (run-hook-with-args-until-success
  14498. 'org-property-allowed-value-functions property)))
  14499. (t
  14500. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  14501. (when (and vals (string-match "\\S-" vals))
  14502. (setq vals (car (read-from-string (concat "(" vals ")"))))
  14503. (setq vals (mapcar (lambda (x)
  14504. (cond ((stringp x) x)
  14505. ((numberp x) (number-to-string x))
  14506. ((symbolp x) (symbol-name x))
  14507. (t "???")))
  14508. vals)))))
  14509. (when (member ":ETC" vals)
  14510. (setq vals (remove ":ETC" vals))
  14511. (org-add-props (car vals) '(org-unrestricted t)))
  14512. (if table (mapcar 'list vals) vals)))
  14513. (defun org-property-previous-allowed-value (&optional previous)
  14514. "Switch to the next allowed value for this property."
  14515. (interactive)
  14516. (org-property-next-allowed-value t))
  14517. (defun org-property-next-allowed-value (&optional previous)
  14518. "Switch to the next allowed value for this property."
  14519. (interactive)
  14520. (unless (org-at-property-p)
  14521. (user-error "Not at a property"))
  14522. (let* ((prop (car (save-match-data (org-split-string (match-string 1) ":"))))
  14523. (key (match-string 2))
  14524. (value (match-string 3))
  14525. (allowed (or (org-property-get-allowed-values (point) key)
  14526. (and (member value '("[ ]" "[-]" "[X]"))
  14527. '("[ ]" "[X]"))))
  14528. (heading (save-match-data (nth 4 (org-heading-components))))
  14529. nval)
  14530. (unless allowed
  14531. (user-error "Allowed values for this property have not been defined"))
  14532. (if previous (setq allowed (reverse allowed)))
  14533. (if (member value allowed)
  14534. (setq nval (car (cdr (member value allowed)))))
  14535. (setq nval (or nval (car allowed)))
  14536. (if (equal nval value)
  14537. (user-error "Only one allowed value for this property"))
  14538. (org-at-property-p)
  14539. (replace-match (concat " :" key ": " nval) t t)
  14540. (org-indent-line)
  14541. (beginning-of-line 1)
  14542. (skip-chars-forward " \t")
  14543. (when (equal prop org-effort-property)
  14544. (save-excursion
  14545. (org-back-to-heading t)
  14546. (put-text-property (point-at-bol) (point-at-eol) 'org-effort nval))
  14547. (when (string= org-clock-current-task heading)
  14548. (setq org-clock-effort nval)
  14549. (org-clock-update-mode-line)))
  14550. (run-hook-with-args 'org-property-changed-functions key nval)))
  14551. (defun org-find-olp (path &optional this-buffer)
  14552. "Return a marker pointing to the entry at outline path OLP.
  14553. If anything goes wrong, throw an error.
  14554. You can wrap this call to catch the error like this:
  14555. (condition-case msg
  14556. (org-mobile-locate-entry (match-string 4))
  14557. (error (nth 1 msg)))
  14558. The return value will then be either a string with the error message,
  14559. or a marker if everything is OK.
  14560. If THIS-BUFFER is set, the outline path does not contain a file,
  14561. only headings."
  14562. (let* ((file (if this-buffer buffer-file-name (pop path)))
  14563. (buffer (if this-buffer (current-buffer) (find-file-noselect file)))
  14564. (level 1)
  14565. (lmin 1)
  14566. (lmax 1)
  14567. limit re end found pos heading cnt flevel)
  14568. (unless buffer (error "File not found :%s" file))
  14569. (with-current-buffer buffer
  14570. (save-excursion
  14571. (save-restriction
  14572. (widen)
  14573. (setq limit (point-max))
  14574. (goto-char (point-min))
  14575. (while (setq heading (pop path))
  14576. (setq re (format org-complex-heading-regexp-format
  14577. (regexp-quote heading)))
  14578. (setq cnt 0 pos (point))
  14579. (while (re-search-forward re end t)
  14580. (setq level (- (match-end 1) (match-beginning 1)))
  14581. (if (and (>= level lmin) (<= level lmax))
  14582. (setq found (match-beginning 0) flevel level cnt (1+ cnt))))
  14583. (when (= cnt 0) (error "Heading not found on level %d: %s"
  14584. lmax heading))
  14585. (when (> cnt 1) (error "Heading not unique on level %d: %s"
  14586. lmax heading))
  14587. (goto-char found)
  14588. (setq lmin (1+ flevel) lmax (+ lmin (if org-odd-levels-only 1 0)))
  14589. (setq end (save-excursion (org-end-of-subtree t t))))
  14590. (when (org-at-heading-p)
  14591. (point-marker)))))))
  14592. (defun org-find-exact-headline-in-buffer (heading &optional buffer pos-only)
  14593. "Find node HEADING in BUFFER.
  14594. Return a marker to the heading if it was found, or nil if not.
  14595. If POS-ONLY is set, return just the position instead of a marker.
  14596. The heading text must match exact, but it may have a TODO keyword,
  14597. a priority cookie and tags in the standard locations."
  14598. (with-current-buffer (or buffer (current-buffer))
  14599. (save-excursion
  14600. (save-restriction
  14601. (widen)
  14602. (goto-char (point-min))
  14603. (let (case-fold-search)
  14604. (if (re-search-forward
  14605. (format org-complex-heading-regexp-format
  14606. (regexp-quote heading)) nil t)
  14607. (if pos-only
  14608. (match-beginning 0)
  14609. (move-marker (make-marker) (match-beginning 0)))))))))
  14610. (defun org-find-exact-heading-in-directory (heading &optional dir)
  14611. "Find Org node headline HEADING in all .org files in directory DIR.
  14612. When the target headline is found, return a marker to this location."
  14613. (let ((files (directory-files (or dir default-directory)
  14614. nil "\\`[^.#].*\\.org\\'"))
  14615. file visiting m buffer)
  14616. (catch 'found
  14617. (while (setq file (pop files))
  14618. (message "trying %s" file)
  14619. (setq visiting (org-find-base-buffer-visiting file))
  14620. (setq buffer (or visiting (find-file-noselect file)))
  14621. (setq m (org-find-exact-headline-in-buffer
  14622. heading buffer))
  14623. (when (and (not m) (not visiting)) (kill-buffer buffer))
  14624. (and m (throw 'found m))))))
  14625. (defun org-find-entry-with-id (ident)
  14626. "Locate the entry that contains the ID property with exact value IDENT.
  14627. IDENT can be a string, a symbol or a number, this function will search for
  14628. the string representation of it.
  14629. Return the position where this entry starts, or nil if there is no such entry."
  14630. (interactive "sID: ")
  14631. (let ((id (cond
  14632. ((stringp ident) ident)
  14633. ((symbol-name ident) (symbol-name ident))
  14634. ((numberp ident) (number-to-string ident))
  14635. (t (error "IDENT %s must be a string, symbol or number" ident))))
  14636. (case-fold-search nil))
  14637. (save-excursion
  14638. (save-restriction
  14639. (widen)
  14640. (goto-char (point-min))
  14641. (when (re-search-forward
  14642. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  14643. nil t)
  14644. (org-back-to-heading t)
  14645. (point))))))
  14646. ;;;; Timestamps
  14647. (defvar org-last-changed-timestamp nil)
  14648. (defvar org-last-inserted-timestamp nil
  14649. "The last time stamp inserted with `org-insert-time-stamp'.")
  14650. (defvar org-time-was-given) ; dynamically scoped parameter
  14651. (defvar org-end-time-was-given) ; dynamically scoped parameter
  14652. (defvar org-ts-what) ; dynamically scoped parameter
  14653. (defun org-time-stamp (arg &optional inactive)
  14654. "Prompt for a date/time and insert a time stamp.
  14655. If the user specifies a time like HH:MM or if this command is
  14656. called with at least one prefix argument, the time stamp contains
  14657. the date and the time. Otherwise, only the date is be included.
  14658. All parts of a date not specified by the user is filled in from
  14659. the current date/time. So if you just press return without
  14660. typing anything, the time stamp will represent the current
  14661. date/time.
  14662. If there is already a timestamp at the cursor, it will be
  14663. modified.
  14664. With two universal prefix arguments, insert an active timestamp
  14665. with the current time without prompting the user."
  14666. (interactive "P")
  14667. (let* ((ts nil)
  14668. (default-time
  14669. ;; Default time is either today, or, when entering a range,
  14670. ;; the range start.
  14671. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  14672. (save-excursion
  14673. (re-search-backward
  14674. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  14675. (- (point) 20) t)))
  14676. (apply 'encode-time (org-parse-time-string (match-string 1)))
  14677. (current-time)))
  14678. (default-input (and ts (org-get-compact-tod ts)))
  14679. (repeater (save-excursion
  14680. (save-match-data
  14681. (beginning-of-line)
  14682. (when (re-search-forward
  14683. "\\([.+-]+[0-9]+[hdwmy] ?\\)+" ;;\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  14684. (save-excursion (progn (end-of-line) (point))) t)
  14685. (match-string 0)))))
  14686. org-time-was-given org-end-time-was-given time)
  14687. (cond
  14688. ((and (org-at-timestamp-p t)
  14689. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  14690. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  14691. (insert "--")
  14692. (setq time (let ((this-command this-command))
  14693. (org-read-date arg 'totime nil nil
  14694. default-time default-input inactive)))
  14695. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  14696. ((org-at-timestamp-p t)
  14697. (setq time (let ((this-command this-command))
  14698. (org-read-date arg 'totime nil nil default-time default-input inactive)))
  14699. (when (org-at-timestamp-p t) ; just to get the match data
  14700. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  14701. (replace-match "")
  14702. (setq org-last-changed-timestamp
  14703. (org-insert-time-stamp
  14704. time (or org-time-was-given arg)
  14705. inactive nil nil (list org-end-time-was-given)))
  14706. (when repeater (goto-char (1- (point))) (insert " " repeater)
  14707. (setq org-last-changed-timestamp
  14708. (concat (substring org-last-inserted-timestamp 0 -1)
  14709. " " repeater ">"))))
  14710. (message "Timestamp updated"))
  14711. ((equal arg '(16))
  14712. (org-insert-time-stamp (current-time) t))
  14713. (t
  14714. (setq time (let ((this-command this-command))
  14715. (org-read-date arg 'totime nil nil default-time default-input inactive)))
  14716. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  14717. nil nil (list org-end-time-was-given))))))
  14718. ;; FIXME: can we use this for something else, like computing time differences?
  14719. (defun org-get-compact-tod (s)
  14720. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  14721. (let* ((t1 (match-string 1 s))
  14722. (h1 (string-to-number (match-string 2 s)))
  14723. (m1 (string-to-number (match-string 3 s)))
  14724. (t2 (and (match-end 4) (match-string 5 s)))
  14725. (h2 (and t2 (string-to-number (match-string 6 s))))
  14726. (m2 (and t2 (string-to-number (match-string 7 s))))
  14727. dh dm)
  14728. (if (not t2)
  14729. t1
  14730. (setq dh (- h2 h1) dm (- m2 m1))
  14731. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  14732. (concat t1 "+" (number-to-string dh)
  14733. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  14734. (defun org-time-stamp-inactive (&optional arg)
  14735. "Insert an inactive time stamp.
  14736. An inactive time stamp is enclosed in square brackets instead of angle
  14737. brackets. It is inactive in the sense that it does not trigger agenda entries,
  14738. does not link to the calendar and cannot be changed with the S-cursor keys.
  14739. So these are more for recording a certain time/date."
  14740. (interactive "P")
  14741. (org-time-stamp arg 'inactive))
  14742. (defvar org-date-ovl (make-overlay 1 1))
  14743. (overlay-put org-date-ovl 'face 'org-date-selected)
  14744. (org-detach-overlay org-date-ovl)
  14745. (defvar org-ans1) ; dynamically scoped parameter
  14746. (defvar org-ans2) ; dynamically scoped parameter
  14747. (defvar org-plain-time-of-day-regexp) ; defined below
  14748. (defvar org-overriding-default-time nil) ; dynamically scoped
  14749. (defvar org-read-date-overlay nil)
  14750. (defvar org-dcst nil) ; dynamically scoped
  14751. (defvar org-read-date-history nil)
  14752. (defvar org-read-date-final-answer nil)
  14753. (defvar org-read-date-analyze-futurep nil)
  14754. (defvar org-read-date-analyze-forced-year nil)
  14755. (defvar org-read-date-inactive)
  14756. (defvar org-read-date-minibuffer-local-map
  14757. (let ((map (make-sparse-keymap)))
  14758. (set-keymap-parent map minibuffer-local-map)
  14759. (org-defkey map (kbd ".")
  14760. (lambda () (interactive)
  14761. ;; Are we at the beginning of the prompt?
  14762. (if (looking-back "^[^:]+: ")
  14763. (org-eval-in-calendar '(calendar-goto-today))
  14764. (insert "."))))
  14765. (org-defkey map (kbd "C-.")
  14766. (lambda () (interactive)
  14767. (org-eval-in-calendar '(calendar-goto-today))))
  14768. (org-defkey map [(meta shift left)]
  14769. (lambda () (interactive)
  14770. (org-eval-in-calendar '(calendar-backward-month 1))))
  14771. (org-defkey map [(meta shift right)]
  14772. (lambda () (interactive)
  14773. (org-eval-in-calendar '(calendar-forward-month 1))))
  14774. (org-defkey map [(meta shift up)]
  14775. (lambda () (interactive)
  14776. (org-eval-in-calendar '(calendar-backward-year 1))))
  14777. (org-defkey map [(meta shift down)]
  14778. (lambda () (interactive)
  14779. (org-eval-in-calendar '(calendar-forward-year 1))))
  14780. (org-defkey map [?\e (shift left)]
  14781. (lambda () (interactive)
  14782. (org-eval-in-calendar '(calendar-backward-month 1))))
  14783. (org-defkey map [?\e (shift right)]
  14784. (lambda () (interactive)
  14785. (org-eval-in-calendar '(calendar-forward-month 1))))
  14786. (org-defkey map [?\e (shift up)]
  14787. (lambda () (interactive)
  14788. (org-eval-in-calendar '(calendar-backward-year 1))))
  14789. (org-defkey map [?\e (shift down)]
  14790. (lambda () (interactive)
  14791. (org-eval-in-calendar '(calendar-forward-year 1))))
  14792. (org-defkey map [(shift up)]
  14793. (lambda () (interactive)
  14794. (org-eval-in-calendar '(calendar-backward-week 1))))
  14795. (org-defkey map [(shift down)]
  14796. (lambda () (interactive)
  14797. (org-eval-in-calendar '(calendar-forward-week 1))))
  14798. (org-defkey map [(shift left)]
  14799. (lambda () (interactive)
  14800. (org-eval-in-calendar '(calendar-backward-day 1))))
  14801. (org-defkey map [(shift right)]
  14802. (lambda () (interactive)
  14803. (org-eval-in-calendar '(calendar-forward-day 1))))
  14804. (org-defkey map "!"
  14805. (lambda () (interactive)
  14806. (org-eval-in-calendar '(diary-view-entries))
  14807. (message "")))
  14808. (org-defkey map ">"
  14809. (lambda () (interactive)
  14810. (org-eval-in-calendar '(scroll-calendar-left 1))))
  14811. (org-defkey map "<"
  14812. (lambda () (interactive)
  14813. (org-eval-in-calendar '(scroll-calendar-right 1))))
  14814. (org-defkey map "\C-v"
  14815. (lambda () (interactive)
  14816. (org-eval-in-calendar
  14817. '(calendar-scroll-left-three-months 1))))
  14818. (org-defkey map "\M-v"
  14819. (lambda () (interactive)
  14820. (org-eval-in-calendar
  14821. '(calendar-scroll-right-three-months 1))))
  14822. map)
  14823. "Keymap for minibuffer commands when using `org-read-date'.")
  14824. (defun org-read-date (&optional org-with-time to-time from-string prompt
  14825. default-time default-input inactive)
  14826. "Read a date, possibly a time, and make things smooth for the user.
  14827. The prompt will suggest to enter an ISO date, but you can also enter anything
  14828. which will at least partially be understood by `parse-time-string'.
  14829. Unrecognized parts of the date will default to the current day, month, year,
  14830. hour and minute. If this command is called to replace a timestamp at point,
  14831. or to enter the second timestamp of a range, the default time is taken
  14832. from the existing stamp. Furthermore, the command prefers the future,
  14833. so if you are giving a date where the year is not given, and the day-month
  14834. combination is already past in the current year, it will assume you
  14835. mean next year. For details, see the manual. A few examples:
  14836. 3-2-5 --> 2003-02-05
  14837. feb 15 --> currentyear-02-15
  14838. 2/15 --> currentyear-02-15
  14839. sep 12 9 --> 2009-09-12
  14840. 12:45 --> today 12:45
  14841. 22 sept 0:34 --> currentyear-09-22 0:34
  14842. 12 --> currentyear-currentmonth-12
  14843. Fri --> nearest Friday after today
  14844. -Tue --> last Tuesday
  14845. etc.
  14846. Furthermore you can specify a relative date by giving, as the *first* thing
  14847. in the input: a plus/minus sign, a number and a letter [hdwmy] to indicate
  14848. change in days weeks, months, years.
  14849. With a single plus or minus, the date is relative to today. With a double
  14850. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  14851. +4d --> four days from today
  14852. +4 --> same as above
  14853. +2w --> two weeks from today
  14854. ++5 --> five days from default date
  14855. The function understands only English month and weekday abbreviations.
  14856. While prompting, a calendar is popped up - you can also select the
  14857. date with the mouse (button 1). The calendar shows a period of three
  14858. months. To scroll it to other months, use the keys `>' and `<'.
  14859. If you don't like the calendar, turn it off with
  14860. \(setq org-read-date-popup-calendar nil)
  14861. With optional argument TO-TIME, the date will immediately be converted
  14862. to an internal time.
  14863. With an optional argument ORG-WITH-TIME, the prompt will suggest to
  14864. also insert a time. Note that when ORG-WITH-TIME is not set, you can
  14865. still enter a time, and this function will inform the calling routine
  14866. about this change. The calling routine may then choose to change the
  14867. format used to insert the time stamp into the buffer to include the time.
  14868. With optional argument FROM-STRING, read from this string instead from
  14869. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  14870. the time/date that is used for everything that is not specified by the
  14871. user."
  14872. (require 'parse-time)
  14873. (let* ((org-time-stamp-rounding-minutes
  14874. (if (equal org-with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  14875. (org-dcst org-display-custom-times)
  14876. (ct (org-current-time))
  14877. (org-def (or org-overriding-default-time default-time ct))
  14878. (org-defdecode (decode-time org-def))
  14879. (dummy (progn
  14880. (when (< (nth 2 org-defdecode) org-extend-today-until)
  14881. (setcar (nthcdr 2 org-defdecode) -1)
  14882. (setcar (nthcdr 1 org-defdecode) 59)
  14883. (setq org-def (apply 'encode-time org-defdecode)
  14884. org-defdecode (decode-time org-def)))))
  14885. (mouse-autoselect-window nil) ; Don't let the mouse jump
  14886. (calendar-frame-setup nil)
  14887. (calendar-setup nil)
  14888. (calendar-move-hook nil)
  14889. (calendar-view-diary-initially-flag nil)
  14890. (calendar-view-holidays-initially-flag nil)
  14891. (timestr (format-time-string
  14892. (if org-with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") org-def))
  14893. (prompt (concat (if prompt (concat prompt " ") "")
  14894. (format "Date+time [%s]: " timestr)))
  14895. ans (org-ans0 "") org-ans1 org-ans2 final)
  14896. (cond
  14897. (from-string (setq ans from-string))
  14898. (org-read-date-popup-calendar
  14899. (save-excursion
  14900. (save-window-excursion
  14901. (calendar)
  14902. (org-eval-in-calendar '(setq cursor-type nil) t)
  14903. (unwind-protect
  14904. (progn
  14905. (calendar-forward-day (- (time-to-days org-def)
  14906. (calendar-absolute-from-gregorian
  14907. (calendar-current-date))))
  14908. (org-eval-in-calendar nil t)
  14909. (let* ((old-map (current-local-map))
  14910. (map (copy-keymap calendar-mode-map))
  14911. (minibuffer-local-map
  14912. (copy-keymap org-read-date-minibuffer-local-map)))
  14913. (org-defkey map (kbd "RET") 'org-calendar-select)
  14914. (org-defkey map [mouse-1] 'org-calendar-select-mouse)
  14915. (org-defkey map [mouse-2] 'org-calendar-select-mouse)
  14916. (unwind-protect
  14917. (progn
  14918. (use-local-map map)
  14919. (setq org-read-date-inactive inactive)
  14920. (add-hook 'post-command-hook 'org-read-date-display)
  14921. (setq org-ans0 (read-string prompt default-input
  14922. 'org-read-date-history nil))
  14923. ;; org-ans0: from prompt
  14924. ;; org-ans1: from mouse click
  14925. ;; org-ans2: from calendar motion
  14926. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  14927. (remove-hook 'post-command-hook 'org-read-date-display)
  14928. (use-local-map old-map)
  14929. (when org-read-date-overlay
  14930. (delete-overlay org-read-date-overlay)
  14931. (setq org-read-date-overlay nil)))))
  14932. (bury-buffer "*Calendar*")))))
  14933. (t ; Naked prompt only
  14934. (unwind-protect
  14935. (setq ans (read-string prompt default-input
  14936. 'org-read-date-history timestr))
  14937. (when org-read-date-overlay
  14938. (delete-overlay org-read-date-overlay)
  14939. (setq org-read-date-overlay nil)))))
  14940. (setq final (org-read-date-analyze ans org-def org-defdecode))
  14941. (when org-read-date-analyze-forced-year
  14942. (message "Year was forced into %s"
  14943. (if org-read-date-force-compatible-dates
  14944. "compatible range (1970-2037)"
  14945. "range representable on this machine"))
  14946. (ding))
  14947. ;; One round trip to get rid of 34th of August and stuff like that....
  14948. (setq final (decode-time (apply 'encode-time final)))
  14949. (setq org-read-date-final-answer ans)
  14950. (if to-time
  14951. (apply 'encode-time final)
  14952. (if (and (boundp 'org-time-was-given) org-time-was-given)
  14953. (format "%04d-%02d-%02d %02d:%02d"
  14954. (nth 5 final) (nth 4 final) (nth 3 final)
  14955. (nth 2 final) (nth 1 final))
  14956. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  14957. (defvar org-def)
  14958. (defvar org-defdecode)
  14959. (defvar org-with-time)
  14960. (defun org-read-date-display ()
  14961. "Display the current date prompt interpretation in the minibuffer."
  14962. (when org-read-date-display-live
  14963. (when org-read-date-overlay
  14964. (delete-overlay org-read-date-overlay))
  14965. (when (minibufferp (current-buffer))
  14966. (save-excursion
  14967. (end-of-line 1)
  14968. (while (not (equal (buffer-substring
  14969. (max (point-min) (- (point) 4)) (point))
  14970. " "))
  14971. (insert " ")))
  14972. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  14973. " " (or org-ans1 org-ans2)))
  14974. (org-end-time-was-given nil)
  14975. (f (org-read-date-analyze ans org-def org-defdecode))
  14976. (fmts (if org-dcst
  14977. org-time-stamp-custom-formats
  14978. org-time-stamp-formats))
  14979. (fmt (if (or org-with-time
  14980. (and (boundp 'org-time-was-given) org-time-was-given))
  14981. (cdr fmts)
  14982. (car fmts)))
  14983. (txt (format-time-string fmt (apply 'encode-time f)))
  14984. (txt (if org-read-date-inactive (concat "[" (substring txt 1 -1) "]") txt))
  14985. (txt (concat "=> " txt)))
  14986. (when (and org-end-time-was-given
  14987. (string-match org-plain-time-of-day-regexp txt))
  14988. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  14989. org-end-time-was-given
  14990. (substring txt (match-end 0)))))
  14991. (when org-read-date-analyze-futurep
  14992. (setq txt (concat txt " (=>F)")))
  14993. (setq org-read-date-overlay
  14994. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  14995. (org-overlay-display org-read-date-overlay txt 'secondary-selection)))))
  14996. (defun org-read-date-analyze (ans org-def org-defdecode)
  14997. "Analyze the combined answer of the date prompt."
  14998. ;; FIXME: cleanup and comment
  14999. (let ((nowdecode (decode-time (current-time)))
  15000. delta deltan deltaw deltadef year month day
  15001. hour minute second wday pm h2 m2 tl wday1
  15002. iso-year iso-weekday iso-week iso-year iso-date futurep kill-year)
  15003. (setq org-read-date-analyze-futurep nil
  15004. org-read-date-analyze-forced-year nil)
  15005. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  15006. (setq ans "+0"))
  15007. (when (setq delta (org-read-date-get-relative ans (current-time) org-def))
  15008. (setq ans (replace-match "" t t ans)
  15009. deltan (car delta)
  15010. deltaw (nth 1 delta)
  15011. deltadef (nth 2 delta)))
  15012. ;; Check if there is an iso week date in there. If yes, store the
  15013. ;; info and postpone interpreting it until the rest of the parsing
  15014. ;; is done.
  15015. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  15016. (setq iso-year (if (match-end 1)
  15017. (org-small-year-to-year
  15018. (string-to-number (match-string 1 ans))))
  15019. iso-weekday (if (match-end 3)
  15020. (string-to-number (match-string 3 ans)))
  15021. iso-week (string-to-number (match-string 2 ans)))
  15022. (setq ans (replace-match "" t t ans)))
  15023. ;; Help matching ISO dates with single digit month or day, like 2006-8-11.
  15024. (when (string-match
  15025. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  15026. (setq year (if (match-end 2)
  15027. (string-to-number (match-string 2 ans))
  15028. (progn (setq kill-year t)
  15029. (string-to-number (format-time-string "%Y"))))
  15030. month (string-to-number (match-string 3 ans))
  15031. day (string-to-number (match-string 4 ans)))
  15032. (if (< year 100) (setq year (+ 2000 year)))
  15033. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  15034. t nil ans)))
  15035. ;; Help matching dotted european dates
  15036. (when (string-match
  15037. "^ *\\(3[01]\\|0?[1-9]\\|[12][0-9]\\)\\. ?\\(0?[1-9]\\|1[012]\\)\\.\\( ?[1-9][0-9]\\{3\\}\\)?" ans)
  15038. (setq year (if (match-end 3) (string-to-number (match-string 3 ans))
  15039. (setq kill-year t)
  15040. (string-to-number (format-time-string "%Y")))
  15041. day (string-to-number (match-string 1 ans))
  15042. month (string-to-number (match-string 2 ans))
  15043. ans (replace-match (format "%04d-%02d-%02d" year month day)
  15044. t nil ans)))
  15045. ;; Help matching american dates, like 5/30 or 5/30/7
  15046. (when (string-match
  15047. "^ *\\(0?[1-9]\\|1[012]\\)/\\(0?[1-9]\\|[12][0-9]\\|3[01]\\)\\(/\\([0-9]+\\)\\)?\\([^/0-9]\\|$\\)" ans)
  15048. (setq year (if (match-end 4)
  15049. (string-to-number (match-string 4 ans))
  15050. (progn (setq kill-year t)
  15051. (string-to-number (format-time-string "%Y"))))
  15052. month (string-to-number (match-string 1 ans))
  15053. day (string-to-number (match-string 2 ans)))
  15054. (if (< year 100) (setq year (+ 2000 year)))
  15055. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  15056. t nil ans)))
  15057. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  15058. ;; If there is a time with am/pm, and *no* time without it, we convert
  15059. ;; so that matching will be successful.
  15060. (loop for i from 1 to 2 do ; twice, for end time as well
  15061. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  15062. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  15063. (setq hour (string-to-number (match-string 1 ans))
  15064. minute (if (match-end 3)
  15065. (string-to-number (match-string 3 ans))
  15066. 0)
  15067. pm (equal ?p
  15068. (string-to-char (downcase (match-string 4 ans)))))
  15069. (if (and (= hour 12) (not pm))
  15070. (setq hour 0)
  15071. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  15072. (setq ans (replace-match (format "%02d:%02d" hour minute)
  15073. t t ans))))
  15074. ;; Check if a time range is given as a duration
  15075. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  15076. (setq hour (string-to-number (match-string 1 ans))
  15077. h2 (+ hour (string-to-number (match-string 3 ans)))
  15078. minute (string-to-number (match-string 2 ans))
  15079. m2 (+ minute (if (match-end 5) (string-to-number
  15080. (match-string 5 ans))0)))
  15081. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  15082. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  15083. t t ans)))
  15084. ;; Check if there is a time range
  15085. (when (boundp 'org-end-time-was-given)
  15086. (setq org-time-was-given nil)
  15087. (when (and (string-match org-plain-time-of-day-regexp ans)
  15088. (match-end 8))
  15089. (setq org-end-time-was-given (match-string 8 ans))
  15090. (setq ans (concat (substring ans 0 (match-beginning 7))
  15091. (substring ans (match-end 7))))))
  15092. (setq tl (parse-time-string ans)
  15093. day (or (nth 3 tl) (nth 3 org-defdecode))
  15094. month (or (nth 4 tl)
  15095. (if (and org-read-date-prefer-future
  15096. (nth 3 tl) (< (nth 3 tl) (nth 3 nowdecode)))
  15097. (prog1 (1+ (nth 4 nowdecode)) (setq futurep t))
  15098. (nth 4 org-defdecode)))
  15099. year (or (and (not kill-year) (nth 5 tl))
  15100. (if (and org-read-date-prefer-future
  15101. (nth 4 tl) (< (nth 4 tl) (nth 4 nowdecode)))
  15102. (prog1 (1+ (nth 5 nowdecode)) (setq futurep t))
  15103. (nth 5 org-defdecode)))
  15104. hour (or (nth 2 tl) (nth 2 org-defdecode))
  15105. minute (or (nth 1 tl) (nth 1 org-defdecode))
  15106. second (or (nth 0 tl) 0)
  15107. wday (nth 6 tl))
  15108. (when (and (eq org-read-date-prefer-future 'time)
  15109. (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
  15110. (equal day (nth 3 nowdecode))
  15111. (equal month (nth 4 nowdecode))
  15112. (equal year (nth 5 nowdecode))
  15113. (nth 2 tl)
  15114. (or (< (nth 2 tl) (nth 2 nowdecode))
  15115. (and (= (nth 2 tl) (nth 2 nowdecode))
  15116. (nth 1 tl)
  15117. (< (nth 1 tl) (nth 1 nowdecode)))))
  15118. (setq day (1+ day)
  15119. futurep t))
  15120. ;; Special date definitions below
  15121. (cond
  15122. (iso-week
  15123. ;; There was an iso week
  15124. (require 'cal-iso)
  15125. (setq futurep nil)
  15126. (setq year (or iso-year year)
  15127. day (or iso-weekday wday 1)
  15128. wday nil ; to make sure that the trigger below does not match
  15129. iso-date (calendar-gregorian-from-absolute
  15130. (calendar-absolute-from-iso
  15131. (list iso-week day year))))
  15132. ; FIXME: Should we also push ISO weeks into the future?
  15133. ; (when (and org-read-date-prefer-future
  15134. ; (not iso-year)
  15135. ; (< (calendar-absolute-from-gregorian iso-date)
  15136. ; (time-to-days (current-time))))
  15137. ; (setq year (1+ year)
  15138. ; iso-date (calendar-gregorian-from-absolute
  15139. ; (calendar-absolute-from-iso
  15140. ; (list iso-week day year)))))
  15141. (setq month (car iso-date)
  15142. year (nth 2 iso-date)
  15143. day (nth 1 iso-date)))
  15144. (deltan
  15145. (setq futurep nil)
  15146. (unless deltadef
  15147. (let ((now (decode-time (current-time))))
  15148. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  15149. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  15150. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  15151. ((equal deltaw "m") (setq month (+ month deltan)))
  15152. ((equal deltaw "y") (setq year (+ year deltan)))))
  15153. ((and wday (not (nth 3 tl)))
  15154. ;; Weekday was given, but no day, so pick that day in the week
  15155. ;; on or after the derived date.
  15156. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  15157. (unless (equal wday wday1)
  15158. (setq day (+ day (% (- wday wday1 -7) 7))))))
  15159. (if (and (boundp 'org-time-was-given)
  15160. (nth 2 tl))
  15161. (setq org-time-was-given t))
  15162. (if (< year 100) (setq year (+ 2000 year)))
  15163. ;; Check of the date is representable
  15164. (if org-read-date-force-compatible-dates
  15165. (progn
  15166. (if (< year 1970)
  15167. (setq year 1970 org-read-date-analyze-forced-year t))
  15168. (if (> year 2037)
  15169. (setq year 2037 org-read-date-analyze-forced-year t)))
  15170. (condition-case nil
  15171. (ignore (encode-time second minute hour day month year))
  15172. (error
  15173. (setq year (nth 5 org-defdecode))
  15174. (setq org-read-date-analyze-forced-year t))))
  15175. (setq org-read-date-analyze-futurep futurep)
  15176. (list second minute hour day month year)))
  15177. (defvar parse-time-weekdays)
  15178. (defun org-read-date-get-relative (s today default)
  15179. "Check string S for special relative date string.
  15180. TODAY and DEFAULT are internal times, for today and for a default.
  15181. Return shift list (N what def-flag)
  15182. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  15183. N is the number of WHATs to shift.
  15184. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  15185. the DEFAULT date rather than TODAY."
  15186. (require 'parse-time)
  15187. (when (and
  15188. (string-match
  15189. (concat
  15190. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  15191. "\\([0-9]+\\)?"
  15192. "\\([hdwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  15193. "\\([ \t]\\|$\\)") s)
  15194. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  15195. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  15196. (string-to-char (substring (match-string 1 s) -1))
  15197. ?+))
  15198. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  15199. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  15200. (what (if (match-end 3) (match-string 3 s) "d"))
  15201. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  15202. (date (if rel default today))
  15203. (wday (nth 6 (decode-time date)))
  15204. delta)
  15205. (if wday1
  15206. (progn
  15207. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  15208. (if (= delta 0) (setq delta 7))
  15209. (if (= dir ?-)
  15210. (progn
  15211. (setq delta (- delta 7))
  15212. (if (= delta 0) (setq delta -7))))
  15213. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  15214. (list delta "d" rel))
  15215. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  15216. (defun org-order-calendar-date-args (arg1 arg2 arg3)
  15217. "Turn a user-specified date into the internal representation.
  15218. The internal representation needed by the calendar is (month day year).
  15219. This is a wrapper to handle the brain-dead convention in calendar that
  15220. user function argument order change dependent on argument order."
  15221. (if (boundp 'calendar-date-style)
  15222. (cond
  15223. ((eq calendar-date-style 'american)
  15224. (list arg1 arg2 arg3))
  15225. ((eq calendar-date-style 'european)
  15226. (list arg2 arg1 arg3))
  15227. ((eq calendar-date-style 'iso)
  15228. (list arg2 arg3 arg1)))
  15229. (org-no-warnings ;; european-calendar-style is obsolete as of version 23.1
  15230. (if (org-bound-and-true-p european-calendar-style)
  15231. (list arg2 arg1 arg3)
  15232. (list arg1 arg2 arg3)))))
  15233. (defun org-eval-in-calendar (form &optional keepdate)
  15234. "Eval FORM in the calendar window and return to current window.
  15235. When KEEPDATE is non-nil, update `org-ans2' from the cursor date,
  15236. otherwise stick to the current value of `org-ans2'."
  15237. (let ((sf (selected-frame))
  15238. (sw (selected-window)))
  15239. (select-window (get-buffer-window "*Calendar*" t))
  15240. (eval form)
  15241. (when (and (not keepdate) (calendar-cursor-to-date))
  15242. (let* ((date (calendar-cursor-to-date))
  15243. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15244. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  15245. (move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  15246. (select-window sw)
  15247. (org-select-frame-set-input-focus sf)))
  15248. (defun org-calendar-select ()
  15249. "Return to `org-read-date' with the date currently selected.
  15250. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  15251. (interactive)
  15252. (when (calendar-cursor-to-date)
  15253. (let* ((date (calendar-cursor-to-date))
  15254. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15255. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  15256. (if (active-minibuffer-window) (exit-minibuffer))))
  15257. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  15258. "Insert a date stamp for the date given by the internal TIME.
  15259. WITH-HM means use the stamp format that includes the time of the day.
  15260. INACTIVE means use square brackets instead of angular ones, so that the
  15261. stamp will not contribute to the agenda.
  15262. PRE and POST are optional strings to be inserted before and after the
  15263. stamp.
  15264. The command returns the inserted time stamp."
  15265. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  15266. stamp)
  15267. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  15268. (insert-before-markers (or pre ""))
  15269. (when (listp extra)
  15270. (setq extra (car extra))
  15271. (if (and (stringp extra)
  15272. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  15273. (setq extra (format "-%02d:%02d"
  15274. (string-to-number (match-string 1 extra))
  15275. (string-to-number (match-string 2 extra))))
  15276. (setq extra nil)))
  15277. (when extra
  15278. (setq fmt (concat (substring fmt 0 -1) extra (substring fmt -1))))
  15279. (insert-before-markers (setq stamp (format-time-string fmt time)))
  15280. (insert-before-markers (or post ""))
  15281. (setq org-last-inserted-timestamp stamp)))
  15282. (defun org-toggle-time-stamp-overlays ()
  15283. "Toggle the use of custom time stamp formats."
  15284. (interactive)
  15285. (setq org-display-custom-times (not org-display-custom-times))
  15286. (unless org-display-custom-times
  15287. (let ((p (point-min)) (bmp (buffer-modified-p)))
  15288. (while (setq p (next-single-property-change p 'display))
  15289. (if (and (get-text-property p 'display)
  15290. (eq (get-text-property p 'face) 'org-date))
  15291. (remove-text-properties
  15292. p (setq p (next-single-property-change p 'display))
  15293. '(display t))))
  15294. (set-buffer-modified-p bmp)))
  15295. (if (featurep 'xemacs)
  15296. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  15297. (org-restart-font-lock)
  15298. (setq org-table-may-need-update t)
  15299. (if org-display-custom-times
  15300. (message "Time stamps are overlaid with custom format")
  15301. (message "Time stamp overlays removed")))
  15302. (defun org-display-custom-time (beg end)
  15303. "Overlay modified time stamp format over timestamp between BEG and END."
  15304. (let* ((ts (buffer-substring beg end))
  15305. t1 w1 with-hm tf time str w2 (off 0))
  15306. (save-match-data
  15307. (setq t1 (org-parse-time-string ts t))
  15308. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)?\\'" ts)
  15309. (setq off (- (match-end 0) (match-beginning 0)))))
  15310. (setq end (- end off))
  15311. (setq w1 (- end beg)
  15312. with-hm (and (nth 1 t1) (nth 2 t1))
  15313. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  15314. time (org-fix-decoded-time t1)
  15315. str (org-add-props
  15316. (format-time-string
  15317. (substring tf 1 -1) (apply 'encode-time time))
  15318. nil 'mouse-face 'highlight)
  15319. w2 (length str))
  15320. (if (not (= w2 w1))
  15321. (add-text-properties (1+ beg) (+ 2 beg)
  15322. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  15323. (if (featurep 'xemacs)
  15324. (progn
  15325. (put-text-property beg end 'invisible t)
  15326. (put-text-property beg end 'end-glyph (make-glyph str)))
  15327. (put-text-property beg end 'display str))))
  15328. (defun org-translate-time (string)
  15329. "Translate all timestamps in STRING to custom format.
  15330. But do this only if the variable `org-display-custom-times' is set."
  15331. (when org-display-custom-times
  15332. (save-match-data
  15333. (let* ((start 0)
  15334. (re org-ts-regexp-both)
  15335. t1 with-hm inactive tf time str beg end)
  15336. (while (setq start (string-match re string start))
  15337. (setq beg (match-beginning 0)
  15338. end (match-end 0)
  15339. t1 (save-match-data
  15340. (org-parse-time-string (substring string beg end) t))
  15341. with-hm (and (nth 1 t1) (nth 2 t1))
  15342. inactive (equal (substring string beg (1+ beg)) "[")
  15343. tf (funcall (if with-hm 'cdr 'car)
  15344. org-time-stamp-custom-formats)
  15345. time (org-fix-decoded-time t1)
  15346. str (format-time-string
  15347. (concat
  15348. (if inactive "[" "<") (substring tf 1 -1)
  15349. (if inactive "]" ">"))
  15350. (apply 'encode-time time))
  15351. string (replace-match str t t string)
  15352. start (+ start (length str)))))))
  15353. string)
  15354. (defun org-fix-decoded-time (time)
  15355. "Set 0 instead of nil for the first 6 elements of time.
  15356. Don't touch the rest."
  15357. (let ((n 0))
  15358. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  15359. (define-obsolete-function-alias 'org-days-to-time 'org-time-stamp-to-now "24.4")
  15360. (defun org-time-stamp-to-now (timestamp-string &optional seconds)
  15361. "Difference between TIMESTAMP-STRING and now in days.
  15362. If SECONDS is non-nil, return the difference in seconds."
  15363. (let ((fdiff (if seconds 'org-float-time 'time-to-days)))
  15364. (- (funcall fdiff (org-time-string-to-time timestamp-string))
  15365. (funcall fdiff (current-time)))))
  15366. (defun org-deadline-close (timestamp-string &optional ndays)
  15367. "Is the time in TIMESTAMP-STRING close to the current date?"
  15368. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  15369. (and (< (org-time-stamp-to-now timestamp-string) ndays)
  15370. (not (org-entry-is-done-p))))
  15371. (defun org-get-wdays (ts &optional delay zero-delay)
  15372. "Get the deadline lead time appropriate for timestring TS.
  15373. When DELAY is non-nil, get the delay time for scheduled items
  15374. instead of the deadline lead time. When ZERO-DELAY is non-nil
  15375. and `org-scheduled-delay-days' is 0, enforce 0 as the delay,
  15376. don't try to find the delay cookie in the scheduled timestamp."
  15377. (let ((tv (if delay org-scheduled-delay-days
  15378. org-deadline-warning-days)))
  15379. (cond
  15380. ((or (and delay (< tv 0))
  15381. (and delay zero-delay (<= tv 0))
  15382. (and (not delay) (<= tv 0)))
  15383. ;; Enforce this value no matter what
  15384. (- tv))
  15385. ((string-match "-\\([0-9]+\\)\\([hdwmy]\\)\\(\\'\\|>\\| \\)" ts)
  15386. ;; lead time is specified.
  15387. (floor (* (string-to-number (match-string 1 ts))
  15388. (cdr (assoc (match-string 2 ts)
  15389. '(("d" . 1) ("w" . 7)
  15390. ("m" . 30.4) ("y" . 365.25)
  15391. ("h" . 0.041667)))))))
  15392. ;; go for the default.
  15393. (t tv))))
  15394. (defun org-calendar-select-mouse (ev)
  15395. "Return to `org-read-date' with the date currently selected.
  15396. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  15397. (interactive "e")
  15398. (mouse-set-point ev)
  15399. (when (calendar-cursor-to-date)
  15400. (let* ((date (calendar-cursor-to-date))
  15401. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15402. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  15403. (if (active-minibuffer-window) (exit-minibuffer))))
  15404. (defun org-check-deadlines (ndays)
  15405. "Check if there are any deadlines due or past due.
  15406. A deadline is considered due if it happens within `org-deadline-warning-days'
  15407. days from today's date. If the deadline appears in an entry marked DONE,
  15408. it is not shown. The prefix arg NDAYS can be used to test that many
  15409. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  15410. (interactive "P")
  15411. (let* ((org-warn-days
  15412. (cond
  15413. ((equal ndays '(4)) 100000)
  15414. (ndays (prefix-numeric-value ndays))
  15415. (t (abs org-deadline-warning-days))))
  15416. (case-fold-search nil)
  15417. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  15418. (callback
  15419. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  15420. (message "%d deadlines past-due or due within %d days"
  15421. (org-occur regexp nil callback)
  15422. org-warn-days)))
  15423. (defsubst org-re-timestamp (type)
  15424. "Return a regexp for timestamp TYPE.
  15425. Allowed values for TYPE are:
  15426. all: all timestamps
  15427. active: only active timestamps (<...>)
  15428. inactive: only inactive timestamps ([...])
  15429. scheduled: only scheduled timestamps
  15430. deadline: only deadline timestamps
  15431. closed: only closed time-stamps
  15432. When TYPE is nil, fall back on returning a regexp that matches
  15433. both scheduled and deadline timestamps."
  15434. (cond ((eq type 'all) "\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}\\(?: +[^]+0-9> \n -]+\\)?\\(?: +[0-9]\\{1,2\\}:[0-9]\\{2\\}\\)?\\)")
  15435. ((eq type 'active) org-ts-regexp)
  15436. ((eq type 'inactive) "\\[\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^ \n>]*?\\)\\]")
  15437. ((eq type 'scheduled) (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>"))
  15438. ((eq type 'deadline) (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  15439. ((eq type 'closed) (concat org-closed-string " \\[\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^ \n>]*?\\)\\]"))
  15440. ((eq type 'scheduled-or-deadline)
  15441. (concat "\\<\\(?:" org-deadline-string "\\|" org-scheduled-string "\\) *<\\([^>]+\\)>"))))
  15442. (defun org-check-before-date (date)
  15443. "Check if there are deadlines or scheduled entries before DATE."
  15444. (interactive (list (org-read-date)))
  15445. (let ((case-fold-search nil)
  15446. (regexp (org-re-timestamp org-ts-type))
  15447. (callback
  15448. (lambda () (time-less-p
  15449. (org-time-string-to-time (match-string 1))
  15450. (org-time-string-to-time date)))))
  15451. (message "%d entries before %s"
  15452. (org-occur regexp nil callback) date)))
  15453. (defun org-check-after-date (date)
  15454. "Check if there are deadlines or scheduled entries after DATE."
  15455. (interactive (list (org-read-date)))
  15456. (let ((case-fold-search nil)
  15457. (regexp (org-re-timestamp org-ts-type))
  15458. (callback
  15459. (lambda () (not
  15460. (time-less-p
  15461. (org-time-string-to-time (match-string 1))
  15462. (org-time-string-to-time date))))))
  15463. (message "%d entries after %s"
  15464. (org-occur regexp nil callback) date)))
  15465. (defun org-check-dates-range (start-date end-date)
  15466. "Check for deadlines/scheduled entries between START-DATE and END-DATE."
  15467. (interactive (list (org-read-date nil nil nil "Range starts")
  15468. (org-read-date nil nil nil "Range end")))
  15469. (let ((case-fold-search nil)
  15470. (regexp (org-re-timestamp org-ts-type))
  15471. (callback
  15472. (lambda ()
  15473. (let ((match (match-string 1)))
  15474. (and
  15475. (not (time-less-p
  15476. (org-time-string-to-time match)
  15477. (org-time-string-to-time start-date)))
  15478. (time-less-p
  15479. (org-time-string-to-time match)
  15480. (org-time-string-to-time end-date)))))))
  15481. (message "%d entries between %s and %s"
  15482. (org-occur regexp nil callback) start-date end-date)))
  15483. (defun org-evaluate-time-range (&optional to-buffer)
  15484. "Evaluate a time range by computing the difference between start and end.
  15485. Normally the result is just printed in the echo area, but with prefix arg
  15486. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  15487. If the time range is actually in a table, the result is inserted into the
  15488. next column.
  15489. For time difference computation, a year is assumed to be exactly 365
  15490. days in order to avoid rounding problems."
  15491. (interactive "P")
  15492. (or
  15493. (org-clock-update-time-maybe)
  15494. (save-excursion
  15495. (unless (org-at-date-range-p t)
  15496. (goto-char (point-at-bol))
  15497. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  15498. (if (not (org-at-date-range-p t))
  15499. (user-error "Not at a time-stamp range, and none found in current line")))
  15500. (let* ((ts1 (match-string 1))
  15501. (ts2 (match-string 2))
  15502. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  15503. (match-end (match-end 0))
  15504. (time1 (org-time-string-to-time ts1))
  15505. (time2 (org-time-string-to-time ts2))
  15506. (t1 (org-float-time time1))
  15507. (t2 (org-float-time time2))
  15508. (diff (abs (- t2 t1)))
  15509. (negative (< (- t2 t1) 0))
  15510. ;; (ys (floor (* 365 24 60 60)))
  15511. (ds (* 24 60 60))
  15512. (hs (* 60 60))
  15513. (fy "%dy %dd %02d:%02d")
  15514. (fy1 "%dy %dd")
  15515. (fd "%dd %02d:%02d")
  15516. (fd1 "%dd")
  15517. (fh "%02d:%02d")
  15518. y d h m align)
  15519. (if havetime
  15520. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  15521. y 0
  15522. d (floor (/ diff ds)) diff (mod diff ds)
  15523. h (floor (/ diff hs)) diff (mod diff hs)
  15524. m (floor (/ diff 60)))
  15525. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  15526. y 0
  15527. d (floor (+ (/ diff ds) 0.5))
  15528. h 0 m 0))
  15529. (if (not to-buffer)
  15530. (message "%s" (org-make-tdiff-string y d h m))
  15531. (if (org-at-table-p)
  15532. (progn
  15533. (goto-char match-end)
  15534. (setq align t)
  15535. (and (looking-at " *|") (goto-char (match-end 0))))
  15536. (goto-char match-end))
  15537. (if (looking-at
  15538. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  15539. (replace-match ""))
  15540. (if negative (insert " -"))
  15541. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  15542. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  15543. (insert " " (format fh h m))))
  15544. (if align (org-table-align))
  15545. (message "Time difference inserted")))))
  15546. (defun org-make-tdiff-string (y d h m)
  15547. (let ((fmt "")
  15548. (l nil))
  15549. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  15550. l (push y l)))
  15551. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  15552. l (push d l)))
  15553. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  15554. l (push h l)))
  15555. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  15556. l (push m l)))
  15557. (apply 'format fmt (nreverse l))))
  15558. (defun org-time-string-to-time (s &optional buffer pos)
  15559. "Convert a timestamp string into internal time."
  15560. (condition-case errdata
  15561. (apply 'encode-time (org-parse-time-string s))
  15562. (error (error "Bad timestamp `%s'%s\nError was: %s"
  15563. s (if (not (and buffer pos))
  15564. ""
  15565. (format " at %d in buffer `%s'" pos buffer))
  15566. (cdr errdata)))))
  15567. (defun org-time-string-to-seconds (s)
  15568. "Convert a timestamp string to a number of seconds."
  15569. (org-float-time (org-time-string-to-time s)))
  15570. (defun org-time-string-to-absolute (s &optional daynr prefer show-all buffer pos)
  15571. "Convert a time stamp to an absolute day number.
  15572. If there is a specifier for a cyclic time stamp, get the closest
  15573. date to DAYNR.
  15574. PREFER and SHOW-ALL are passed through to `org-closest-date'.
  15575. The variable `date' is bound by the calendar when this is called."
  15576. (cond
  15577. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  15578. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  15579. daynr
  15580. (+ daynr 1000)))
  15581. ((and daynr (string-match "\\+[0-9]+[hdwmy]" s))
  15582. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  15583. (time-to-days (current-time))) (match-string 0 s)
  15584. prefer show-all))
  15585. (t (time-to-days
  15586. (condition-case errdata
  15587. (apply 'encode-time (org-parse-time-string s))
  15588. (error (error "Bad timestamp `%s'%s\nError was: %s"
  15589. s (if (not (and buffer pos))
  15590. ""
  15591. (format " at %d in buffer `%s'" pos buffer))
  15592. (cdr errdata))))))))
  15593. (defun org-days-to-iso-week (days)
  15594. "Return the iso week number."
  15595. (require 'cal-iso)
  15596. (car (calendar-iso-from-absolute days)))
  15597. (defun org-small-year-to-year (year)
  15598. "Convert 2-digit years into 4-digit years.
  15599. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2037.
  15600. The year 2000 cannot be abbreviated. Any year larger than 99
  15601. is returned unchanged."
  15602. (if (< year 38)
  15603. (setq year (+ 2000 year))
  15604. (if (< year 100)
  15605. (setq year (+ 1900 year))))
  15606. year)
  15607. (defun org-time-from-absolute (d)
  15608. "Return the time corresponding to date D.
  15609. D may be an absolute day number, or a calendar-type list (month day year)."
  15610. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  15611. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  15612. (defun org-calendar-holiday ()
  15613. "List of holidays, for Diary display in Org-mode."
  15614. (require 'holidays)
  15615. (let ((hl (funcall
  15616. (if (fboundp 'calendar-check-holidays)
  15617. 'calendar-check-holidays 'check-calendar-holidays) date)))
  15618. (if hl (mapconcat 'identity hl "; "))))
  15619. (defun org-diary-sexp-entry (sexp entry date)
  15620. "Process a SEXP diary ENTRY for DATE."
  15621. (require 'diary-lib)
  15622. (let ((result (if calendar-debug-sexp
  15623. (let ((stack-trace-on-error t))
  15624. (eval (car (read-from-string sexp))))
  15625. (condition-case nil
  15626. (eval (car (read-from-string sexp)))
  15627. (error
  15628. (beep)
  15629. (message "Bad sexp at line %d in %s: %s"
  15630. (org-current-line)
  15631. (buffer-file-name) sexp)
  15632. (sleep-for 2))))))
  15633. (cond ((stringp result) (split-string result "; "))
  15634. ((and (consp result)
  15635. (not (consp (cdr result)))
  15636. (stringp (cdr result))) (cdr result))
  15637. ((and (consp result)
  15638. (stringp (car result))) result)
  15639. (result entry))))
  15640. (defun org-diary-to-ical-string (frombuf)
  15641. "Get iCalendar entries from diary entries in buffer FROMBUF.
  15642. This uses the icalendar.el library."
  15643. (let* ((tmpdir (if (featurep 'xemacs)
  15644. (temp-directory)
  15645. temporary-file-directory))
  15646. (tmpfile (make-temp-name
  15647. (expand-file-name "orgics" tmpdir)))
  15648. buf rtn b e)
  15649. (with-current-buffer frombuf
  15650. (icalendar-export-region (point-min) (point-max) tmpfile)
  15651. (setq buf (find-buffer-visiting tmpfile))
  15652. (set-buffer buf)
  15653. (goto-char (point-min))
  15654. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  15655. (setq b (match-beginning 0)))
  15656. (goto-char (point-max))
  15657. (if (re-search-backward "^END:VEVENT" nil t)
  15658. (setq e (match-end 0)))
  15659. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  15660. (kill-buffer buf)
  15661. (delete-file tmpfile)
  15662. rtn))
  15663. (defun org-closest-date (start current change prefer show-all)
  15664. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  15665. When PREFER is `past', return a date that is either CURRENT or past.
  15666. When PREFER is `future', return a date that is either CURRENT or future.
  15667. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  15668. ;; Make the proper lists from the dates
  15669. (catch 'exit
  15670. (let ((a1 '(("h" . hour)
  15671. ("d" . day)
  15672. ("w" . week)
  15673. ("m" . month)
  15674. ("y" . year)))
  15675. (shour (nth 2 (org-parse-time-string start)))
  15676. dn dw sday cday n1 n2 n0
  15677. d m y y1 y2 date1 date2 nmonths nm ny m2)
  15678. (setq start (org-date-to-gregorian start)
  15679. current (org-date-to-gregorian
  15680. (if show-all
  15681. current
  15682. (time-to-days (current-time))))
  15683. sday (calendar-absolute-from-gregorian start)
  15684. cday (calendar-absolute-from-gregorian current))
  15685. (if (<= cday sday) (throw 'exit sday))
  15686. (if (string-match "\\(\\+[0-9]+\\)\\([hdwmy]\\)" change)
  15687. (setq dn (string-to-number (match-string 1 change))
  15688. dw (cdr (assoc (match-string 2 change) a1)))
  15689. (user-error "Invalid change specifier: %s" change))
  15690. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  15691. (cond
  15692. ((eq dw 'hour)
  15693. (let ((missing-hours
  15694. (mod (+ (- (* 24 (- cday sday)) shour) org-extend-today-until)
  15695. dn)))
  15696. (setq n1 (if (zerop missing-hours) cday
  15697. (- cday (1+ (floor (/ missing-hours 24)))))
  15698. n2 (+ cday (floor (/ (- dn missing-hours) 24))))))
  15699. ((eq dw 'day)
  15700. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  15701. n2 (+ n1 dn)))
  15702. ((eq dw 'year)
  15703. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  15704. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  15705. (setq date1 (list m d y1)
  15706. n1 (calendar-absolute-from-gregorian date1)
  15707. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  15708. n2 (calendar-absolute-from-gregorian date2)))
  15709. ((eq dw 'month)
  15710. ;; approx number of month between the two dates
  15711. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  15712. ;; How often does dn fit in there?
  15713. (setq d (nth 1 start) m (car start) y (nth 2 start)
  15714. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  15715. m (+ m nm)
  15716. ny (floor (/ m 12))
  15717. y (+ y ny)
  15718. m (- m (* ny 12)))
  15719. (while (> m 12) (setq m (- m 12) y (1+ y)))
  15720. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  15721. (setq m2 (+ m dn) y2 y)
  15722. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  15723. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  15724. (while (<= n2 cday)
  15725. (setq n1 n2 m m2 y y2)
  15726. (setq m2 (+ m dn) y2 y)
  15727. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  15728. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  15729. ;; Make sure n1 is the earlier date
  15730. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  15731. (if show-all
  15732. (cond
  15733. ((eq prefer 'past) (if (= cday n2) n2 n1))
  15734. ((eq prefer 'future) (if (= cday n1) n1 n2))
  15735. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  15736. (cond
  15737. ((eq prefer 'past) (if (= cday n2) n2 n1))
  15738. ((eq prefer 'future) (if (= cday n1) n1 n2))
  15739. (t (if (= cday n1) n1 n2)))))))
  15740. (defun org-date-to-gregorian (date)
  15741. "Turn any specification of DATE into a Gregorian date for the calendar."
  15742. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  15743. ((and (listp date) (= (length date) 3)) date)
  15744. ((stringp date)
  15745. (setq date (org-parse-time-string date))
  15746. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  15747. ((listp date)
  15748. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  15749. (defun org-parse-time-string (s &optional nodefault)
  15750. "Parse the standard Org-mode time string.
  15751. This should be a lot faster than the normal `parse-time-string'.
  15752. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  15753. hour and minute fields will be nil if not given."
  15754. (cond ((string-match org-ts-regexp0 s)
  15755. (list 0
  15756. (if (or (match-beginning 8) (not nodefault))
  15757. (string-to-number (or (match-string 8 s) "0")))
  15758. (if (or (match-beginning 7) (not nodefault))
  15759. (string-to-number (or (match-string 7 s) "0")))
  15760. (string-to-number (match-string 4 s))
  15761. (string-to-number (match-string 3 s))
  15762. (string-to-number (match-string 2 s))
  15763. nil nil nil))
  15764. ((string-match "^<[^>]+>$" s)
  15765. (decode-time (seconds-to-time (org-matcher-time s))))
  15766. (t (error "Not a standard Org-mode time string: %s" s))))
  15767. (defun org-timestamp-up (&optional arg)
  15768. "Increase the date item at the cursor by one.
  15769. If the cursor is on the year, change the year. If it is on the month,
  15770. the day or the time, change that.
  15771. With prefix ARG, change by that many units."
  15772. (interactive "p")
  15773. (org-timestamp-change (prefix-numeric-value arg) nil 'updown))
  15774. (defun org-timestamp-down (&optional arg)
  15775. "Decrease the date item at the cursor by one.
  15776. If the cursor is on the year, change the year. If it is on the month,
  15777. the day or the time, change that.
  15778. With prefix ARG, change by that many units."
  15779. (interactive "p")
  15780. (org-timestamp-change (- (prefix-numeric-value arg)) nil 'updown))
  15781. (defun org-timestamp-up-day (&optional arg)
  15782. "Increase the date in the time stamp by one day.
  15783. With prefix ARG, change that many days."
  15784. (interactive "p")
  15785. (if (and (not (org-at-timestamp-p t))
  15786. (org-at-heading-p))
  15787. (org-todo 'up)
  15788. (org-timestamp-change (prefix-numeric-value arg) 'day 'updown)))
  15789. (defun org-timestamp-down-day (&optional arg)
  15790. "Decrease the date in the time stamp by one day.
  15791. With prefix ARG, change that many days."
  15792. (interactive "p")
  15793. (if (and (not (org-at-timestamp-p t))
  15794. (org-at-heading-p))
  15795. (org-todo 'down)
  15796. (org-timestamp-change (- (prefix-numeric-value arg)) 'day) 'updown))
  15797. (defun org-at-timestamp-p (&optional inactive-ok)
  15798. "Determine if the cursor is in or at a timestamp."
  15799. (interactive)
  15800. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  15801. (pos (point))
  15802. (ans (or (looking-at tsr)
  15803. (save-excursion
  15804. (skip-chars-backward "^[<\n\r\t")
  15805. (if (> (point) (point-min)) (backward-char 1))
  15806. (and (looking-at tsr)
  15807. (> (- (match-end 0) pos) -1))))))
  15808. (and ans
  15809. (boundp 'org-ts-what)
  15810. (setq org-ts-what
  15811. (cond
  15812. ((= pos (match-beginning 0)) 'bracket)
  15813. ;; Point is considered to be "on the bracket" whether
  15814. ;; it's really on it or right after it.
  15815. ((= pos (1- (match-end 0))) 'bracket)
  15816. ((= pos (match-end 0)) 'after)
  15817. ((org-pos-in-match-range pos 2) 'year)
  15818. ((org-pos-in-match-range pos 3) 'month)
  15819. ((org-pos-in-match-range pos 7) 'hour)
  15820. ((org-pos-in-match-range pos 8) 'minute)
  15821. ((or (org-pos-in-match-range pos 4)
  15822. (org-pos-in-match-range pos 5)) 'day)
  15823. ((and (> pos (or (match-end 8) (match-end 5)))
  15824. (< pos (match-end 0)))
  15825. (- pos (or (match-end 8) (match-end 5))))
  15826. (t 'day))))
  15827. ans))
  15828. (defun org-toggle-timestamp-type ()
  15829. "Toggle the type (<active> or [inactive]) of a time stamp."
  15830. (interactive)
  15831. (when (org-at-timestamp-p t)
  15832. (let ((beg (match-beginning 0)) (end (match-end 0))
  15833. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  15834. (save-excursion
  15835. (goto-char beg)
  15836. (while (re-search-forward "[][<>]" end t)
  15837. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  15838. t t)))
  15839. (message "Timestamp is now %sactive"
  15840. (if (equal (char-after beg) ?<) "" "in")))))
  15841. (defun org-at-clock-log-p nil
  15842. "Is the cursor on the clock log line?"
  15843. (save-excursion
  15844. (move-beginning-of-line 1)
  15845. (looking-at "^[ \t]*CLOCK:")))
  15846. (defvar org-clock-history) ; defined in org-clock.el
  15847. (defvar org-clock-adjust-closest nil) ; defined in org-clock.el
  15848. (defun org-timestamp-change (n &optional what updown suppress-tmp-delay)
  15849. "Change the date in the time stamp at point.
  15850. The date will be changed by N times WHAT. WHAT can be `day', `month',
  15851. `year', `minute', `second'. If WHAT is not given, the cursor position
  15852. in the timestamp determines what will be changed.
  15853. When SUPPRESS-TMP-DELAY is non-nil, suppress delays like \"--2d\"."
  15854. (let ((origin (point)) origin-cat
  15855. with-hm inactive
  15856. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  15857. org-ts-what
  15858. extra rem
  15859. ts time time0 fixnext clrgx)
  15860. (if (not (org-at-timestamp-p t))
  15861. (user-error "Not at a timestamp"))
  15862. (if (and (not what) (eq org-ts-what 'bracket))
  15863. (org-toggle-timestamp-type)
  15864. ;; Point isn't on brackets. Remember the part of the time-stamp
  15865. ;; the point was in. Indeed, size of time-stamps may change,
  15866. ;; but point must be kept in the same category nonetheless.
  15867. (setq origin-cat org-ts-what)
  15868. (if (and (not what) (not (eq org-ts-what 'day))
  15869. org-display-custom-times
  15870. (get-text-property (point) 'display)
  15871. (not (get-text-property (1- (point)) 'display)))
  15872. (setq org-ts-what 'day))
  15873. (setq org-ts-what (or what org-ts-what)
  15874. inactive (= (char-after (match-beginning 0)) ?\[)
  15875. ts (match-string 0))
  15876. (replace-match "")
  15877. (when (string-match
  15878. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?-?[-+][0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)*\\)[]>]"
  15879. ts)
  15880. (setq extra (match-string 1 ts))
  15881. (if suppress-tmp-delay
  15882. (setq extra (replace-regexp-in-string " --[0-9]+[hdwmy]" "" extra))))
  15883. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  15884. (setq with-hm t))
  15885. (setq time0 (org-parse-time-string ts))
  15886. (when (and updown
  15887. (eq org-ts-what 'minute)
  15888. (not current-prefix-arg))
  15889. ;; This looks like s-up and s-down. Change by one rounding step.
  15890. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  15891. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  15892. (setcar (cdr time0) (+ (nth 1 time0)
  15893. (if (> n 0) (- rem) (- dm rem))))))
  15894. (setq time
  15895. (encode-time (or (car time0) 0)
  15896. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  15897. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  15898. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  15899. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  15900. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  15901. (nthcdr 6 time0)))
  15902. (when (and (member org-ts-what '(hour minute))
  15903. extra
  15904. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  15905. (setq extra (org-modify-ts-extra
  15906. extra
  15907. (if (eq org-ts-what 'hour) 2 5)
  15908. n dm)))
  15909. (when (integerp org-ts-what)
  15910. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  15911. (if (eq what 'calendar)
  15912. (let ((cal-date (org-get-date-from-calendar)))
  15913. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  15914. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  15915. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  15916. (setcar time0 (or (car time0) 0))
  15917. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  15918. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  15919. (setq time (apply 'encode-time time0))))
  15920. ;; Insert the new time-stamp, and ensure point stays in the same
  15921. ;; category as before (i.e. not after the last position in that
  15922. ;; category).
  15923. (let ((pos (point)))
  15924. ;; Stay before inserted string. `save-excursion' is of no use.
  15925. (setq org-last-changed-timestamp
  15926. (org-insert-time-stamp time with-hm inactive nil nil extra))
  15927. (goto-char pos))
  15928. (save-match-data
  15929. (looking-at org-ts-regexp3)
  15930. (goto-char (cond
  15931. ;; `day' category ends before `hour' if any, or at
  15932. ;; the end of the day name.
  15933. ((eq origin-cat 'day)
  15934. (min (or (match-beginning 7) (1- (match-end 5))) origin))
  15935. ((eq origin-cat 'hour) (min (match-end 7) origin))
  15936. ((eq origin-cat 'minute) (min (1- (match-end 8)) origin))
  15937. ((integerp origin-cat) (min (1- (match-end 0)) origin))
  15938. ;; `year' and `month' have both fixed size: point
  15939. ;; couldn't have moved into another part.
  15940. (t origin))))
  15941. ;; Update clock if on a CLOCK line.
  15942. (org-clock-update-time-maybe)
  15943. ;; Maybe adjust the closest clock in `org-clock-history'
  15944. (when org-clock-adjust-closest
  15945. (if (not (and (org-at-clock-log-p)
  15946. (< 1 (length (delq nil (mapcar 'marker-position
  15947. org-clock-history))))))
  15948. (message "No clock to adjust")
  15949. (cond ((save-excursion ; fix previous clock?
  15950. (re-search-backward org-ts-regexp0 nil t)
  15951. (org-looking-back (concat org-clock-string " \\[")))
  15952. (setq fixnext 1 clrgx (concat org-ts-regexp0 "\\] =>.*$")))
  15953. ((save-excursion ; fix next clock?
  15954. (re-search-backward org-ts-regexp0 nil t)
  15955. (looking-at (concat org-ts-regexp0 "\\] =>")))
  15956. (setq fixnext -1 clrgx (concat org-clock-string " \\[" org-ts-regexp0))))
  15957. (save-window-excursion
  15958. ;; Find closest clock to point, adjust the previous/next one in history
  15959. (let* ((p (save-excursion (org-back-to-heading t)))
  15960. (cl (mapcar (lambda(c) (abs (- (marker-position c) p))) org-clock-history))
  15961. (clfixnth
  15962. (+ fixnext (- (length cl) (or (length (member (apply #'min cl) cl)) 100))))
  15963. (clfixpos (if (> 0 clfixnth) nil (nth clfixnth org-clock-history))))
  15964. (if (not clfixpos)
  15965. (message "No clock to adjust")
  15966. (save-excursion
  15967. (org-goto-marker-or-bmk clfixpos)
  15968. (org-show-subtree)
  15969. (when (re-search-forward clrgx nil t)
  15970. (goto-char (match-beginning 1))
  15971. (let (org-clock-adjust-closest)
  15972. (org-timestamp-change n org-ts-what updown))
  15973. (message "Clock adjusted in %s for heading: %s"
  15974. (file-name-nondirectory (buffer-file-name))
  15975. (org-get-heading t t)))))))))
  15976. ;; Try to recenter the calendar window, if any.
  15977. (if (and org-calendar-follow-timestamp-change
  15978. (get-buffer-window "*Calendar*" t)
  15979. (memq org-ts-what '(day month year)))
  15980. (org-recenter-calendar (time-to-days time))))))
  15981. (defun org-modify-ts-extra (s pos n dm)
  15982. "Change the different parts of the lead-time and repeat fields in timestamp."
  15983. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  15984. ng h m new rem)
  15985. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  15986. (cond
  15987. ((or (org-pos-in-match-range pos 2)
  15988. (org-pos-in-match-range pos 3))
  15989. (setq m (string-to-number (match-string 3 s))
  15990. h (string-to-number (match-string 2 s)))
  15991. (if (org-pos-in-match-range pos 2)
  15992. (setq h (+ h n))
  15993. (setq n (* dm (org-no-warnings (signum n))))
  15994. (when (not (= 0 (setq rem (% m dm))))
  15995. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  15996. (setq m (+ m n)))
  15997. (if (< m 0) (setq m (+ m 60) h (1- h)))
  15998. (if (> m 59) (setq m (- m 60) h (1+ h)))
  15999. (setq h (min 24 (max 0 h)))
  16000. (setq ng 1 new (format "-%02d:%02d" h m)))
  16001. ((org-pos-in-match-range pos 6)
  16002. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  16003. ((org-pos-in-match-range pos 5)
  16004. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  16005. ((org-pos-in-match-range pos 9)
  16006. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  16007. ((org-pos-in-match-range pos 8)
  16008. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  16009. (when ng
  16010. (setq s (concat
  16011. (substring s 0 (match-beginning ng))
  16012. new
  16013. (substring s (match-end ng))))))
  16014. s))
  16015. (defun org-recenter-calendar (date)
  16016. "If the calendar is visible, recenter it to DATE."
  16017. (let ((cwin (get-buffer-window "*Calendar*" t)))
  16018. (when cwin
  16019. (let ((calendar-move-hook nil))
  16020. (with-selected-window cwin
  16021. (calendar-goto-date (if (listp date) date
  16022. (calendar-gregorian-from-absolute date))))))))
  16023. (defun org-goto-calendar (&optional arg)
  16024. "Go to the Emacs calendar at the current date.
  16025. If there is a time stamp in the current line, go to that date.
  16026. A prefix ARG can be used to force the current date."
  16027. (interactive "P")
  16028. (let ((tsr org-ts-regexp) diff
  16029. (calendar-move-hook nil)
  16030. (calendar-view-holidays-initially-flag nil)
  16031. (calendar-view-diary-initially-flag nil))
  16032. (if (or (org-at-timestamp-p)
  16033. (save-excursion
  16034. (beginning-of-line 1)
  16035. (looking-at (concat ".*" tsr))))
  16036. (let ((d1 (time-to-days (current-time)))
  16037. (d2 (time-to-days
  16038. (org-time-string-to-time (match-string 1)))))
  16039. (setq diff (- d2 d1))))
  16040. (calendar)
  16041. (calendar-goto-today)
  16042. (if (and diff (not arg)) (calendar-forward-day diff))))
  16043. (defun org-get-date-from-calendar ()
  16044. "Return a list (month day year) of date at point in calendar."
  16045. (with-current-buffer "*Calendar*"
  16046. (save-match-data
  16047. (calendar-cursor-to-date))))
  16048. (defun org-date-from-calendar ()
  16049. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  16050. If there is already a time stamp at the cursor position, update it."
  16051. (interactive)
  16052. (if (org-at-timestamp-p t)
  16053. (org-timestamp-change 0 'calendar)
  16054. (let ((cal-date (org-get-date-from-calendar)))
  16055. (org-insert-time-stamp
  16056. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  16057. (defcustom org-effort-durations
  16058. `(("h" . 60)
  16059. ("d" . ,(* 60 8))
  16060. ("w" . ,(* 60 8 5))
  16061. ("m" . ,(* 60 8 5 4))
  16062. ("y" . ,(* 60 8 5 40)))
  16063. "Conversion factor to minutes for an effort modifier.
  16064. Each entry has the form (MODIFIER . MINUTES).
  16065. In an effort string, a number followed by MODIFIER is multiplied
  16066. by the specified number of MINUTES to obtain an effort in
  16067. minutes.
  16068. For example, if the value of this variable is ((\"hours\" . 60)), then an
  16069. effort string \"2hours\" is equivalent to 120 minutes."
  16070. :group 'org-agenda
  16071. :version "24.1"
  16072. :type '(alist :key-type (string :tag "Modifier")
  16073. :value-type (number :tag "Minutes")))
  16074. (defun org-minutes-to-clocksum-string (m)
  16075. "Format number of minutes as a clocksum string.
  16076. The format is determined by `org-time-clocksum-format',
  16077. `org-time-clocksum-use-fractional' and
  16078. `org-time-clocksum-fractional-format' and
  16079. `org-time-clocksum-use-effort-durations'."
  16080. (let ((clocksum "")
  16081. (m (round m)) ; Don't allow fractions of minutes
  16082. h d w mo y fmt n)
  16083. (setq h (if org-time-clocksum-use-effort-durations
  16084. (cdr (assoc "h" org-effort-durations)) 60)
  16085. d (if org-time-clocksum-use-effort-durations
  16086. (/ (cdr (assoc "d" org-effort-durations)) h) 24)
  16087. w (if org-time-clocksum-use-effort-durations
  16088. (/ (cdr (assoc "w" org-effort-durations)) (* d h)) 7)
  16089. mo (if org-time-clocksum-use-effort-durations
  16090. (/ (cdr (assoc "m" org-effort-durations)) (* d h)) 30)
  16091. y (if org-time-clocksum-use-effort-durations
  16092. (/ (cdr (assoc "y" org-effort-durations)) (* d h)) 365))
  16093. ;; fractional format
  16094. (if org-time-clocksum-use-fractional
  16095. (cond
  16096. ;; single format string
  16097. ((stringp org-time-clocksum-fractional-format)
  16098. (format org-time-clocksum-fractional-format (/ m (float h))))
  16099. ;; choice of fractional formats for different time units
  16100. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :years))
  16101. (> (/ (truncate m) (* y d h)) 0))
  16102. (format fmt (/ m (* y d (float h)))))
  16103. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :months))
  16104. (> (/ (truncate m) (* mo d h)) 0))
  16105. (format fmt (/ m (* mo d (float h)))))
  16106. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :weeks))
  16107. (> (/ (truncate m) (* w d h)) 0))
  16108. (format fmt (/ m (* w d (float h)))))
  16109. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :days))
  16110. (> (/ (truncate m) (* d h)) 0))
  16111. (format fmt (/ m (* d (float h)))))
  16112. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :hours))
  16113. (> (/ (truncate m) h) 0))
  16114. (format fmt (/ m (float h))))
  16115. ((setq fmt (plist-get org-time-clocksum-fractional-format :minutes))
  16116. (format fmt m))
  16117. ;; fall back to smallest time unit with a format
  16118. ((setq fmt (plist-get org-time-clocksum-fractional-format :hours))
  16119. (format fmt (/ m (float h))))
  16120. ((setq fmt (plist-get org-time-clocksum-fractional-format :days))
  16121. (format fmt (/ m (* d (float h)))))
  16122. ((setq fmt (plist-get org-time-clocksum-fractional-format :weeks))
  16123. (format fmt (/ m (* w d (float h)))))
  16124. ((setq fmt (plist-get org-time-clocksum-fractional-format :months))
  16125. (format fmt (/ m (* mo d (float h)))))
  16126. ((setq fmt (plist-get org-time-clocksum-fractional-format :years))
  16127. (format fmt (/ m (* y d (float h))))))
  16128. ;; standard (non-fractional) format, with single format string
  16129. (if (stringp org-time-clocksum-format)
  16130. (format org-time-clocksum-format (setq n (/ m h)) (- m (* h n)))
  16131. ;; separate formats components
  16132. (and (setq fmt (plist-get org-time-clocksum-format :years))
  16133. (or (> (setq n (/ (truncate m) (* y d h))) 0)
  16134. (plist-get org-time-clocksum-format :require-years))
  16135. (setq clocksum (concat clocksum (format fmt n))
  16136. m (- m (* n y d h))))
  16137. (and (setq fmt (plist-get org-time-clocksum-format :months))
  16138. (or (> (setq n (/ (truncate m) (* mo d h))) 0)
  16139. (plist-get org-time-clocksum-format :require-months))
  16140. (setq clocksum (concat clocksum (format fmt n))
  16141. m (- m (* n mo d h))))
  16142. (and (setq fmt (plist-get org-time-clocksum-format :weeks))
  16143. (or (> (setq n (/ (truncate m) (* w d h))) 0)
  16144. (plist-get org-time-clocksum-format :require-weeks))
  16145. (setq clocksum (concat clocksum (format fmt n))
  16146. m (- m (* n w d h))))
  16147. (and (setq fmt (plist-get org-time-clocksum-format :days))
  16148. (or (> (setq n (/ (truncate m) (* d h))) 0)
  16149. (plist-get org-time-clocksum-format :require-days))
  16150. (setq clocksum (concat clocksum (format fmt n))
  16151. m (- m (* n d h))))
  16152. (and (setq fmt (plist-get org-time-clocksum-format :hours))
  16153. (or (> (setq n (/ (truncate m) h)) 0)
  16154. (plist-get org-time-clocksum-format :require-hours))
  16155. (setq clocksum (concat clocksum (format fmt n))
  16156. m (- m (* n h))))
  16157. (and (setq fmt (plist-get org-time-clocksum-format :minutes))
  16158. (or (> m 0) (plist-get org-time-clocksum-format :require-minutes))
  16159. (setq clocksum (concat clocksum (format fmt m))))
  16160. ;; return formatted time duration
  16161. clocksum))))
  16162. (defalias 'org-minutes-to-hh:mm-string 'org-minutes-to-clocksum-string)
  16163. (make-obsolete 'org-minutes-to-hh:mm-string 'org-minutes-to-clocksum-string
  16164. "Org mode version 8.0")
  16165. (defun org-hours-to-clocksum-string (n)
  16166. (org-minutes-to-clocksum-string (* n 60)))
  16167. (defun org-hh:mm-string-to-minutes (s)
  16168. "Convert a string H:MM to a number of minutes.
  16169. If the string is just a number, interpret it as minutes.
  16170. In fact, the first hh:mm or number in the string will be taken,
  16171. there can be extra stuff in the string.
  16172. If no number is found, the return value is 0."
  16173. (cond
  16174. ((integerp s) s)
  16175. ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  16176. (+ (* (string-to-number (match-string 1 s)) 60)
  16177. (string-to-number (match-string 2 s))))
  16178. ((string-match "\\([0-9]+\\)" s)
  16179. (string-to-number (match-string 1 s)))
  16180. (t 0)))
  16181. (defcustom org-image-actual-width t
  16182. "Should we use the actual width of images when inlining them?
  16183. When set to `t', always use the image width.
  16184. When set to a number, use imagemagick (when available) to set
  16185. the image's width to this value.
  16186. When set to a number in a list, try to get the width from any
  16187. #+ATTR.* keyword if it matches a width specification like
  16188. #+ATTR_HTML: :width 300px
  16189. and fall back on that number if none is found.
  16190. When set to nil, try to get the width from an #+ATTR.* keyword
  16191. and fall back on the original width if none is found.
  16192. This requires Emacs >= 24.1, build with imagemagick support."
  16193. :group 'org-appearance
  16194. :version "24.4"
  16195. :package-version '(Org . "8.0")
  16196. :type '(choice
  16197. (const :tag "Use the image width" t)
  16198. (integer :tag "Use a number of pixels")
  16199. (list :tag "Use #+ATTR* or a number of pixels" (integer))
  16200. (const :tag "Use #+ATTR* or don't resize" nil)))
  16201. (defcustom org-agenda-inhibit-startup nil
  16202. "Inhibit startup when preparing agenda buffers.
  16203. When this variable is `t' (the default), the initialization of
  16204. the Org agenda buffers is inhibited: e.g. the visibility state
  16205. is not set, the tables are not re-aligned, etc."
  16206. :type 'boolean
  16207. :version "24.3"
  16208. :group 'org-agenda)
  16209. (defun org-duration-string-to-minutes (s &optional output-to-string)
  16210. "Convert a duration string S to minutes.
  16211. A bare number is interpreted as minutes, modifiers can be set by
  16212. customizing `org-effort-durations' (which see).
  16213. Entries containing a colon are interpreted as H:MM by
  16214. `org-hh:mm-string-to-minutes'."
  16215. (let ((result 0)
  16216. (re (concat "\\([0-9.]+\\) *\\("
  16217. (regexp-opt (mapcar 'car org-effort-durations))
  16218. "\\)")))
  16219. (while (string-match re s)
  16220. (incf result (* (cdr (assoc (match-string 2 s) org-effort-durations))
  16221. (string-to-number (match-string 1 s))))
  16222. (setq s (replace-match "" nil t s)))
  16223. (setq result (floor result))
  16224. (incf result (org-hh:mm-string-to-minutes s))
  16225. (if output-to-string (number-to-string result) result)))
  16226. ;;;; Files
  16227. (defun org-save-all-org-buffers ()
  16228. "Save all Org-mode buffers without user confirmation."
  16229. (interactive)
  16230. (message "Saving all Org-mode buffers...")
  16231. (save-some-buffers t (lambda () (derived-mode-p 'org-mode)))
  16232. (when (featurep 'org-id) (org-id-locations-save))
  16233. (message "Saving all Org-mode buffers... done"))
  16234. (defun org-revert-all-org-buffers ()
  16235. "Revert all Org-mode buffers.
  16236. Prompt for confirmation when there are unsaved changes.
  16237. Be sure you know what you are doing before letting this function
  16238. overwrite your changes.
  16239. This function is useful in a setup where one tracks org files
  16240. with a version control system, to revert on one machine after pulling
  16241. changes from another. I believe the procedure must be like this:
  16242. 1. M-x org-save-all-org-buffers
  16243. 2. Pull changes from the other machine, resolve conflicts
  16244. 3. M-x org-revert-all-org-buffers"
  16245. (interactive)
  16246. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  16247. (user-error "Abort"))
  16248. (save-excursion
  16249. (save-window-excursion
  16250. (mapc
  16251. (lambda (b)
  16252. (when (and (with-current-buffer b (derived-mode-p 'org-mode))
  16253. (with-current-buffer b buffer-file-name))
  16254. (org-pop-to-buffer-same-window b)
  16255. (revert-buffer t 'no-confirm)))
  16256. (buffer-list))
  16257. (when (and (featurep 'org-id) org-id-track-globally)
  16258. (org-id-locations-load)))))
  16259. ;;;; Agenda files
  16260. ;;;###autoload
  16261. (defun org-switchb (&optional arg)
  16262. "Switch between Org buffers.
  16263. With one prefix argument, restrict available buffers to files.
  16264. With two prefix arguments, restrict available buffers to agenda files.
  16265. Defaults to `iswitchb' for buffer name completion.
  16266. Set `org-completion-use-ido' to make it use ido instead."
  16267. (interactive "P")
  16268. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  16269. ((equal arg '(16)) (org-buffer-list 'agenda))
  16270. (t (org-buffer-list))))
  16271. (org-completion-use-iswitchb org-completion-use-iswitchb)
  16272. (org-completion-use-ido org-completion-use-ido))
  16273. (unless (or org-completion-use-ido org-completion-use-iswitchb)
  16274. (setq org-completion-use-iswitchb t))
  16275. (org-pop-to-buffer-same-window
  16276. (org-icompleting-read "Org buffer: "
  16277. (mapcar 'list (mapcar 'buffer-name blist))
  16278. nil t))))
  16279. ;;; Define some older names previously used for this functionality
  16280. ;;;###autoload
  16281. (defalias 'org-ido-switchb 'org-switchb)
  16282. ;;;###autoload
  16283. (defalias 'org-iswitchb 'org-switchb)
  16284. (defun org-buffer-list (&optional predicate exclude-tmp)
  16285. "Return a list of Org buffers.
  16286. PREDICATE can be `export', `files' or `agenda'.
  16287. export restrict the list to Export buffers.
  16288. files restrict the list to buffers visiting Org files.
  16289. agenda restrict the list to buffers visiting agenda files.
  16290. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  16291. (let* ((bfn nil)
  16292. (agenda-files (and (eq predicate 'agenda)
  16293. (mapcar 'file-truename (org-agenda-files t))))
  16294. (filter
  16295. (cond
  16296. ((eq predicate 'files)
  16297. (lambda (b) (with-current-buffer b (derived-mode-p 'org-mode))))
  16298. ((eq predicate 'export)
  16299. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  16300. ((eq predicate 'agenda)
  16301. (lambda (b)
  16302. (with-current-buffer b
  16303. (and (derived-mode-p 'org-mode)
  16304. (setq bfn (buffer-file-name b))
  16305. (member (file-truename bfn) agenda-files)))))
  16306. (t (lambda (b) (with-current-buffer b
  16307. (or (derived-mode-p 'org-mode)
  16308. (string-match "\*Org .*Export"
  16309. (buffer-name b)))))))))
  16310. (delq nil
  16311. (mapcar
  16312. (lambda(b)
  16313. (if (and (funcall filter b)
  16314. (or (not exclude-tmp)
  16315. (not (string-match "tmp" (buffer-name b)))))
  16316. b
  16317. nil))
  16318. (buffer-list)))))
  16319. (defun org-agenda-files (&optional unrestricted archives)
  16320. "Get the list of agenda files.
  16321. Optional UNRESTRICTED means return the full list even if a restriction
  16322. is currently in place.
  16323. When ARCHIVES is t, include all archive files that are really being
  16324. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  16325. `org-agenda-archives-mode' is t."
  16326. (let ((files
  16327. (cond
  16328. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  16329. ((stringp org-agenda-files) (org-read-agenda-file-list))
  16330. ((listp org-agenda-files) org-agenda-files)
  16331. (t (error "Invalid value of `org-agenda-files'")))))
  16332. (setq files (apply 'append
  16333. (mapcar (lambda (f)
  16334. (if (file-directory-p f)
  16335. (directory-files
  16336. f t org-agenda-file-regexp)
  16337. (list f)))
  16338. files)))
  16339. (when org-agenda-skip-unavailable-files
  16340. (setq files (delq nil
  16341. (mapcar (function
  16342. (lambda (file)
  16343. (and (file-readable-p file) file)))
  16344. files))))
  16345. (when (or (eq archives t)
  16346. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  16347. (setq files (org-add-archive-files files)))
  16348. files))
  16349. (defun org-agenda-file-p (&optional file)
  16350. "Return non-nil, if FILE is an agenda file.
  16351. If FILE is omitted, use the file associated with the current
  16352. buffer."
  16353. (member (or file (buffer-file-name))
  16354. (org-agenda-files t)))
  16355. (defun org-edit-agenda-file-list ()
  16356. "Edit the list of agenda files.
  16357. Depending on setup, this either uses customize to edit the variable
  16358. `org-agenda-files', or it visits the file that is holding the list. In the
  16359. latter case, the buffer is set up in a way that saving it automatically kills
  16360. the buffer and restores the previous window configuration."
  16361. (interactive)
  16362. (if (stringp org-agenda-files)
  16363. (let ((cw (current-window-configuration)))
  16364. (find-file org-agenda-files)
  16365. (org-set-local 'org-window-configuration cw)
  16366. (org-add-hook 'after-save-hook
  16367. (lambda ()
  16368. (set-window-configuration
  16369. (prog1 org-window-configuration
  16370. (kill-buffer (current-buffer))))
  16371. (org-install-agenda-files-menu)
  16372. (message "New agenda file list installed"))
  16373. nil 'local)
  16374. (message "%s" (substitute-command-keys
  16375. "Edit list and finish with \\[save-buffer]")))
  16376. (customize-variable 'org-agenda-files)))
  16377. (defun org-store-new-agenda-file-list (list)
  16378. "Set new value for the agenda file list and save it correctly."
  16379. (if (stringp org-agenda-files)
  16380. (let ((fe (org-read-agenda-file-list t)) b u)
  16381. (while (setq b (find-buffer-visiting org-agenda-files))
  16382. (kill-buffer b))
  16383. (with-temp-file org-agenda-files
  16384. (insert
  16385. (mapconcat
  16386. (lambda (f) ;; Keep un-expanded entries.
  16387. (if (setq u (assoc f fe))
  16388. (cdr u)
  16389. f))
  16390. list "\n")
  16391. "\n")))
  16392. (let ((org-mode-hook nil) (org-inhibit-startup t)
  16393. (org-insert-mode-line-in-empty-file nil))
  16394. (setq org-agenda-files list)
  16395. (customize-save-variable 'org-agenda-files org-agenda-files))))
  16396. (defun org-read-agenda-file-list (&optional pair-with-expansion)
  16397. "Read the list of agenda files from a file.
  16398. If PAIR-WITH-EXPANSION is t return pairs with un-expanded
  16399. filenames, used by `org-store-new-agenda-file-list' to write back
  16400. un-expanded file names."
  16401. (when (file-directory-p org-agenda-files)
  16402. (error "`org-agenda-files' cannot be a single directory"))
  16403. (when (stringp org-agenda-files)
  16404. (with-temp-buffer
  16405. (insert-file-contents org-agenda-files)
  16406. (mapcar
  16407. (lambda (f)
  16408. (let ((e (expand-file-name (substitute-in-file-name f)
  16409. org-directory)))
  16410. (if pair-with-expansion
  16411. (cons e f)
  16412. e)))
  16413. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*")))))
  16414. ;;;###autoload
  16415. (defun org-cycle-agenda-files ()
  16416. "Cycle through the files in `org-agenda-files'.
  16417. If the current buffer visits an agenda file, find the next one in the list.
  16418. If the current buffer does not, find the first agenda file."
  16419. (interactive)
  16420. (let* ((fs (org-agenda-files t))
  16421. (files (append fs (list (car fs))))
  16422. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  16423. file)
  16424. (unless files (user-error "No agenda files"))
  16425. (catch 'exit
  16426. (while (setq file (pop files))
  16427. (if (equal (file-truename file) tcf)
  16428. (when (car files)
  16429. (find-file (car files))
  16430. (throw 'exit t))))
  16431. (find-file (car fs)))
  16432. (if (buffer-base-buffer) (org-pop-to-buffer-same-window (buffer-base-buffer)))))
  16433. (defun org-agenda-file-to-front (&optional to-end)
  16434. "Move/add the current file to the top of the agenda file list.
  16435. If the file is not present in the list, it is added to the front. If it is
  16436. present, it is moved there. With optional argument TO-END, add/move to the
  16437. end of the list."
  16438. (interactive "P")
  16439. (let ((org-agenda-skip-unavailable-files nil)
  16440. (file-alist (mapcar (lambda (x)
  16441. (cons (file-truename x) x))
  16442. (org-agenda-files t)))
  16443. (ctf (file-truename
  16444. (or buffer-file-name
  16445. (user-error "Please save the current buffer to a file"))))
  16446. x had)
  16447. (setq x (assoc ctf file-alist) had x)
  16448. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  16449. (if to-end
  16450. (setq file-alist (append (delq x file-alist) (list x)))
  16451. (setq file-alist (cons x (delq x file-alist))))
  16452. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  16453. (org-install-agenda-files-menu)
  16454. (message "File %s to %s of agenda file list"
  16455. (if had "moved" "added") (if to-end "end" "front"))))
  16456. (defun org-remove-file (&optional file)
  16457. "Remove current file from the list of files in variable `org-agenda-files'.
  16458. These are the files which are being checked for agenda entries.
  16459. Optional argument FILE means use this file instead of the current."
  16460. (interactive)
  16461. (let* ((org-agenda-skip-unavailable-files nil)
  16462. (file (or file buffer-file-name
  16463. (user-error "Current buffer does not visit a file")))
  16464. (true-file (file-truename file))
  16465. (afile (abbreviate-file-name file))
  16466. (files (delq nil (mapcar
  16467. (lambda (x)
  16468. (if (equal true-file
  16469. (file-truename x))
  16470. nil x))
  16471. (org-agenda-files t)))))
  16472. (if (not (= (length files) (length (org-agenda-files t))))
  16473. (progn
  16474. (org-store-new-agenda-file-list files)
  16475. (org-install-agenda-files-menu)
  16476. (message "Removed file: %s" afile))
  16477. (message "File was not in list: %s (not removed)" afile))))
  16478. (defun org-file-menu-entry (file)
  16479. (vector file (list 'find-file file) t))
  16480. (defun org-check-agenda-file (file)
  16481. "Make sure FILE exists. If not, ask user what to do."
  16482. (when (not (file-exists-p file))
  16483. (message "Non-existent agenda file %s. [R]emove from list or [A]bort?"
  16484. (abbreviate-file-name file))
  16485. (let ((r (downcase (read-char-exclusive))))
  16486. (cond
  16487. ((equal r ?r)
  16488. (org-remove-file file)
  16489. (throw 'nextfile t))
  16490. (t (error "Abort"))))))
  16491. (defun org-get-agenda-file-buffer (file)
  16492. "Get a buffer visiting FILE. If the buffer needs to be created, add
  16493. it to the list of buffers which might be released later."
  16494. (let ((buf (org-find-base-buffer-visiting file)))
  16495. (if buf
  16496. buf ; just return it
  16497. ;; Make a new buffer and remember it
  16498. (setq buf (find-file-noselect file))
  16499. (if buf (push buf org-agenda-new-buffers))
  16500. buf)))
  16501. (defun org-release-buffers (blist)
  16502. "Release all buffers in list, asking the user for confirmation when needed.
  16503. When a buffer is unmodified, it is just killed. When modified, it is saved
  16504. \(if the user agrees) and then killed."
  16505. (let (buf file)
  16506. (while (setq buf (pop blist))
  16507. (setq file (buffer-file-name buf))
  16508. (when (and (buffer-modified-p buf)
  16509. file
  16510. (y-or-n-p (format "Save file %s? " file)))
  16511. (with-current-buffer buf (save-buffer)))
  16512. (kill-buffer buf))))
  16513. (defun org-agenda-prepare-buffers (files)
  16514. "Create buffers for all agenda files, protect archived trees and comments."
  16515. (interactive)
  16516. (let ((pa '(:org-archived t))
  16517. (pc '(:org-comment t))
  16518. (pall '(:org-archived t :org-comment t))
  16519. (inhibit-read-only t)
  16520. (org-inhibit-startup org-agenda-inhibit-startup)
  16521. (rea (concat ":" org-archive-tag ":"))
  16522. file re)
  16523. (setq org-tag-alist-for-agenda nil
  16524. org-tag-groups-alist-for-agenda nil)
  16525. (save-excursion
  16526. (save-restriction
  16527. (while (setq file (pop files))
  16528. (catch 'nextfile
  16529. (if (bufferp file)
  16530. (set-buffer file)
  16531. (org-check-agenda-file file)
  16532. (set-buffer (org-get-agenda-file-buffer file)))
  16533. (widen)
  16534. (org-set-regexps-and-options-for-tags)
  16535. (goto-char (point-min))
  16536. (let ((case-fold-search t))
  16537. (when (search-forward "#+setupfile" nil t)
  16538. ;; Don't set all regexps and options systematically as
  16539. ;; this is only run for setting agenda tags from setup
  16540. ;; file
  16541. (org-set-regexps-and-options)))
  16542. (org-refresh-category-properties)
  16543. (org-refresh-properties org-effort-property 'org-effort)
  16544. (org-refresh-properties "APPT_WARNTIME" 'org-appt-warntime)
  16545. (setq org-todo-keywords-for-agenda
  16546. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  16547. (setq org-done-keywords-for-agenda
  16548. (append org-done-keywords-for-agenda org-done-keywords))
  16549. (setq org-todo-keyword-alist-for-agenda
  16550. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  16551. (setq org-drawers-for-agenda
  16552. (append org-drawers-for-agenda org-drawers))
  16553. (setq org-tag-alist-for-agenda
  16554. (org-uniquify
  16555. (append org-tag-alist-for-agenda
  16556. org-tag-alist
  16557. org-tag-persistent-alist)))
  16558. (if org-group-tags
  16559. (setq org-tag-groups-alist-for-agenda
  16560. (org-uniquify-alist
  16561. (append org-tag-groups-alist-for-agenda org-tag-groups-alist))))
  16562. (org-with-silent-modifications
  16563. (save-excursion
  16564. (remove-text-properties (point-min) (point-max) pall)
  16565. (when org-agenda-skip-archived-trees
  16566. (goto-char (point-min))
  16567. (while (re-search-forward rea nil t)
  16568. (if (org-at-heading-p t)
  16569. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  16570. (goto-char (point-min))
  16571. (setq re (format org-heading-keyword-regexp-format
  16572. org-comment-string))
  16573. (while (re-search-forward re nil t)
  16574. (add-text-properties
  16575. (match-beginning 0) (org-end-of-subtree t) pc))))))))
  16576. (setq org-todo-keywords-for-agenda
  16577. (org-uniquify org-todo-keywords-for-agenda))
  16578. (setq org-todo-keyword-alist-for-agenda
  16579. (org-uniquify org-todo-keyword-alist-for-agenda))))
  16580. ;;;; CDLaTeX minor mode
  16581. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  16582. "Keymap for the minor `org-cdlatex-mode'.")
  16583. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  16584. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  16585. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  16586. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  16587. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  16588. (defvar org-cdlatex-texmathp-advice-is-done nil
  16589. "Flag remembering if we have applied the advice to texmathp already.")
  16590. (define-minor-mode org-cdlatex-mode
  16591. "Toggle the minor `org-cdlatex-mode'.
  16592. This mode supports entering LaTeX environment and math in LaTeX fragments
  16593. in Org-mode.
  16594. \\{org-cdlatex-mode-map}"
  16595. nil " OCDL" nil
  16596. (when org-cdlatex-mode
  16597. (require 'cdlatex)
  16598. (run-hooks 'cdlatex-mode-hook)
  16599. (cdlatex-compute-tables))
  16600. (unless org-cdlatex-texmathp-advice-is-done
  16601. (setq org-cdlatex-texmathp-advice-is-done t)
  16602. (defadvice texmathp (around org-math-always-on activate)
  16603. "Always return t in org-mode buffers.
  16604. This is because we want to insert math symbols without dollars even outside
  16605. the LaTeX math segments. If Orgmode thinks that point is actually inside
  16606. an embedded LaTeX fragment, let texmathp do its job.
  16607. \\[org-cdlatex-mode-map]"
  16608. (interactive)
  16609. (let (p)
  16610. (cond
  16611. ((not (derived-mode-p 'org-mode)) ad-do-it)
  16612. ((eq this-command 'cdlatex-math-symbol)
  16613. (setq ad-return-value t
  16614. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  16615. (t
  16616. (let ((p (org-inside-LaTeX-fragment-p)))
  16617. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  16618. (setq ad-return-value t
  16619. texmathp-why '("Org-mode embedded math" . 0))
  16620. (if p ad-do-it)))))))))
  16621. (defun turn-on-org-cdlatex ()
  16622. "Unconditionally turn on `org-cdlatex-mode'."
  16623. (org-cdlatex-mode 1))
  16624. (defun org-try-cdlatex-tab ()
  16625. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  16626. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  16627. - inside a LaTeX fragment, or
  16628. - after the first word in a line, where an abbreviation expansion could
  16629. insert a LaTeX environment."
  16630. (when org-cdlatex-mode
  16631. (cond
  16632. ;; Before any word on the line: No expansion possible.
  16633. ((save-excursion (skip-chars-backward " \t") (bolp)) nil)
  16634. ;; Just after first word on the line: Expand it. Make sure it
  16635. ;; cannot happen on headlines, though.
  16636. ((save-excursion
  16637. (skip-chars-backward "a-zA-Z0-9*")
  16638. (skip-chars-backward " \t")
  16639. (and (bolp) (not (org-at-heading-p))))
  16640. (cdlatex-tab) t)
  16641. ((org-inside-LaTeX-fragment-p) (cdlatex-tab) t))))
  16642. (defun org-cdlatex-underscore-caret (&optional arg)
  16643. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  16644. Revert to the normal definition outside of these fragments."
  16645. (interactive "P")
  16646. (if (org-inside-LaTeX-fragment-p)
  16647. (call-interactively 'cdlatex-sub-superscript)
  16648. (let (org-cdlatex-mode)
  16649. (call-interactively (key-binding (vector last-input-event))))))
  16650. (defun org-cdlatex-math-modify (&optional arg)
  16651. "Execute `cdlatex-math-modify' in LaTeX fragments.
  16652. Revert to the normal definition outside of these fragments."
  16653. (interactive "P")
  16654. (if (org-inside-LaTeX-fragment-p)
  16655. (call-interactively 'cdlatex-math-modify)
  16656. (let (org-cdlatex-mode)
  16657. (call-interactively (key-binding (vector last-input-event))))))
  16658. ;;;; LaTeX fragments
  16659. (defvar org-latex-regexps
  16660. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  16661. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  16662. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  16663. ("$1" "\\([^$]\\|^\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  16664. ("$" "\\([^$]\\|^\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  16665. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  16666. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
  16667. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
  16668. "Regular expressions for matching embedded LaTeX.")
  16669. (defun org-inside-LaTeX-fragment-p ()
  16670. "Test if point is inside a LaTeX fragment.
  16671. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  16672. sequence appearing also before point.
  16673. Even though the matchers for math are configurable, this function assumes
  16674. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  16675. delimiters are skipped when they have been removed by customization.
  16676. The return value is nil, or a cons cell with the delimiter and the
  16677. position of this delimiter.
  16678. This function does a reasonably good job, but can locally be fooled by
  16679. for example currency specifications. For example it will assume being in
  16680. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  16681. fragments that are properly closed, but during editing, we have to live
  16682. with the uncertainty caused by missing closing delimiters. This function
  16683. looks only before point, not after."
  16684. (catch 'exit
  16685. (let ((pos (point))
  16686. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  16687. (lim (progn
  16688. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  16689. (point)))
  16690. dd-on str (start 0) m re)
  16691. (goto-char pos)
  16692. (when dodollar
  16693. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  16694. re (nth 1 (assoc "$" org-latex-regexps)))
  16695. (while (string-match re str start)
  16696. (cond
  16697. ((= (match-end 0) (length str))
  16698. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  16699. ((= (match-end 0) (- (length str) 5))
  16700. (throw 'exit nil))
  16701. (t (setq start (match-end 0))))))
  16702. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  16703. (goto-char pos)
  16704. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  16705. (and (match-beginning 2) (throw 'exit nil))
  16706. ;; count $$
  16707. (while (re-search-backward "\\$\\$" lim t)
  16708. (setq dd-on (not dd-on)))
  16709. (goto-char pos)
  16710. (if dd-on (cons "$$" m))))))
  16711. (defun org-inside-latex-macro-p ()
  16712. "Is point inside a LaTeX macro or its arguments?"
  16713. (save-match-data
  16714. (org-in-regexp
  16715. "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")))
  16716. (defvar org-latex-fragment-image-overlays nil
  16717. "List of overlays carrying the images of latex fragments.")
  16718. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  16719. (defun org-remove-latex-fragment-image-overlays ()
  16720. "Remove all overlays with LaTeX fragment images in current buffer."
  16721. (mapc 'delete-overlay org-latex-fragment-image-overlays)
  16722. (setq org-latex-fragment-image-overlays nil))
  16723. (defun org-preview-latex-fragment (&optional subtree)
  16724. "Preview the LaTeX fragment at point, or all locally or globally.
  16725. If the cursor is in a LaTeX fragment, create the image and overlay
  16726. it over the source code. If there is no fragment at point, display
  16727. all fragments in the current text, from one headline to the next. With
  16728. prefix SUBTREE, display all fragments in the current subtree. With a
  16729. double prefix arg \\[universal-argument] \\[universal-argument], or when \
  16730. the cursor is before the first headline,
  16731. display all fragments in the buffer.
  16732. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  16733. (interactive "P")
  16734. (unless buffer-file-name
  16735. (user-error "Can't preview LaTeX fragment in a non-file buffer"))
  16736. (when (display-graphic-p)
  16737. (org-remove-latex-fragment-image-overlays)
  16738. (save-excursion
  16739. (save-restriction
  16740. (let (beg end at msg)
  16741. (cond
  16742. ((or (equal subtree '(16))
  16743. (not (save-excursion
  16744. (re-search-backward org-outline-regexp-bol nil t))))
  16745. (setq beg (point-min) end (point-max)
  16746. msg "Creating images for buffer...%s"))
  16747. ((equal subtree '(4))
  16748. (org-back-to-heading)
  16749. (setq beg (point) end (org-end-of-subtree t)
  16750. msg "Creating images for subtree...%s"))
  16751. (t
  16752. (if (setq at (org-inside-LaTeX-fragment-p))
  16753. (goto-char (max (point-min) (- (cdr at) 2)))
  16754. (org-back-to-heading))
  16755. (setq beg (point) end (progn (outline-next-heading) (point))
  16756. msg (if at "Creating image...%s"
  16757. "Creating images for entry...%s"))))
  16758. (message msg "")
  16759. (narrow-to-region beg end)
  16760. (goto-char beg)
  16761. (org-format-latex
  16762. (concat org-latex-preview-ltxpng-directory (file-name-sans-extension
  16763. (file-name-nondirectory
  16764. buffer-file-name)))
  16765. default-directory 'overlays msg at 'forbuffer
  16766. org-latex-create-formula-image-program)
  16767. (message msg "done. Use `C-c C-c' to remove images."))))))
  16768. (defun org-format-latex (prefix &optional dir overlays msg at
  16769. forbuffer processing-type)
  16770. "Replace LaTeX fragments with links to an image, and produce images.
  16771. Some of the options can be changed using the variable
  16772. `org-format-latex-options'."
  16773. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  16774. (let* ((prefixnodir (file-name-nondirectory prefix))
  16775. (absprefix (expand-file-name prefix dir))
  16776. (todir (file-name-directory absprefix))
  16777. (opt org-format-latex-options)
  16778. (optnew org-format-latex-options)
  16779. (matchers (plist-get opt :matchers))
  16780. (re-list org-latex-regexps)
  16781. (cnt 0) txt hash link beg end re e checkdir
  16782. string
  16783. m n block-type block linkfile movefile ov)
  16784. ;; Check the different regular expressions
  16785. (while (setq e (pop re-list))
  16786. (setq m (car e) re (nth 1 e) n (nth 2 e) block-type (nth 3 e)
  16787. block (if block-type "\n\n" ""))
  16788. (when (member m matchers)
  16789. (goto-char (point-min))
  16790. (while (re-search-forward re nil t)
  16791. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  16792. (or (not overlays)
  16793. (not (eq (get-char-property (match-beginning n)
  16794. 'org-overlay-type)
  16795. 'org-latex-overlay))))
  16796. (cond
  16797. ((eq processing-type 'verbatim))
  16798. ((eq processing-type 'mathjax)
  16799. ;; Prepare for MathJax processing.
  16800. (setq string (match-string n))
  16801. (when (member m '("$" "$1"))
  16802. (save-excursion
  16803. (delete-region (match-beginning n) (match-end n))
  16804. (goto-char (match-beginning n))
  16805. (insert (concat "\\(" (substring string 1 -1) "\\)")))))
  16806. ((or (eq processing-type 'dvipng)
  16807. (eq processing-type 'imagemagick))
  16808. ;; Process to an image.
  16809. (setq txt (match-string n)
  16810. beg (match-beginning n) end (match-end n)
  16811. cnt (1+ cnt))
  16812. (let ((face (face-at-point))
  16813. (fg (plist-get opt :foreground))
  16814. (bg (plist-get opt :background))
  16815. ;; Ensure full list is printed.
  16816. print-length print-level)
  16817. (when forbuffer
  16818. ;; Get the colors from the face at point.
  16819. (goto-char beg)
  16820. (when (eq fg 'auto)
  16821. (setq fg (face-attribute face :foreground nil 'default)))
  16822. (when (eq bg 'auto)
  16823. (setq bg (face-attribute face :background nil 'default)))
  16824. (setq optnew (copy-sequence opt))
  16825. (plist-put optnew :foreground fg)
  16826. (plist-put optnew :background bg))
  16827. (setq hash (sha1 (prin1-to-string
  16828. (list org-format-latex-header
  16829. org-latex-default-packages-alist
  16830. org-latex-packages-alist
  16831. org-format-latex-options
  16832. forbuffer txt fg bg)))
  16833. linkfile (format "%s_%s.png" prefix hash)
  16834. movefile (format "%s_%s.png" absprefix hash)))
  16835. (setq link (concat block "[[file:" linkfile "]]" block))
  16836. (if msg (message msg cnt))
  16837. (goto-char beg)
  16838. (unless checkdir ; Ensure the directory exists.
  16839. (setq checkdir t)
  16840. (or (file-directory-p todir) (make-directory todir t)))
  16841. (unless (file-exists-p movefile)
  16842. (org-create-formula-image
  16843. txt movefile optnew forbuffer processing-type))
  16844. (if overlays
  16845. (progn
  16846. (mapc (lambda (o)
  16847. (if (eq (overlay-get o 'org-overlay-type)
  16848. 'org-latex-overlay)
  16849. (delete-overlay o)))
  16850. (overlays-in beg end))
  16851. (setq ov (make-overlay beg end))
  16852. (overlay-put ov 'org-overlay-type 'org-latex-overlay)
  16853. (if (featurep 'xemacs)
  16854. (progn
  16855. (overlay-put ov 'invisible t)
  16856. (overlay-put
  16857. ov 'end-glyph
  16858. (make-glyph (vector 'png :file movefile))))
  16859. (overlay-put
  16860. ov 'display
  16861. (list 'image :type 'png :file movefile :ascent 'center)))
  16862. (push ov org-latex-fragment-image-overlays)
  16863. (goto-char end))
  16864. (delete-region beg end)
  16865. (insert (org-add-props link
  16866. (list 'org-latex-src
  16867. (replace-regexp-in-string
  16868. "\"" "" txt)
  16869. 'org-latex-src-embed-type
  16870. (if block-type 'paragraph 'character))))))
  16871. ((eq processing-type 'mathml)
  16872. ;; Process to MathML
  16873. (unless (save-match-data (org-format-latex-mathml-available-p))
  16874. (user-error "LaTeX to MathML converter not configured"))
  16875. (setq txt (match-string n)
  16876. beg (match-beginning n) end (match-end n)
  16877. cnt (1+ cnt))
  16878. (if msg (message msg cnt))
  16879. (goto-char beg)
  16880. (delete-region beg end)
  16881. (insert (org-format-latex-as-mathml
  16882. txt block-type prefix dir)))
  16883. (t
  16884. (error "Unknown conversion type %s for LaTeX fragments"
  16885. processing-type)))))))))
  16886. (defun org-create-math-formula (latex-frag &optional mathml-file)
  16887. "Convert LATEX-FRAG to MathML and store it in MATHML-FILE.
  16888. Use `org-latex-to-mathml-convert-command'. If the conversion is
  16889. sucessful, return the portion between \"<math...> </math>\"
  16890. elements otherwise return nil. When MATHML-FILE is specified,
  16891. write the results in to that file. When invoked as an
  16892. interactive command, prompt for LATEX-FRAG, with initial value
  16893. set to the current active region and echo the results for user
  16894. inspection."
  16895. (interactive (list (let ((frag (when (org-region-active-p)
  16896. (buffer-substring-no-properties
  16897. (region-beginning) (region-end)))))
  16898. (read-string "LaTeX Fragment: " frag nil frag))))
  16899. (unless latex-frag (error "Invalid LaTeX fragment"))
  16900. (let* ((tmp-in-file (file-relative-name
  16901. (make-temp-name (expand-file-name "ltxmathml-in"))))
  16902. (ignore (write-region latex-frag nil tmp-in-file))
  16903. (tmp-out-file (file-relative-name
  16904. (make-temp-name (expand-file-name "ltxmathml-out"))))
  16905. (cmd (format-spec
  16906. org-latex-to-mathml-convert-command
  16907. `((?j . ,(shell-quote-argument
  16908. (expand-file-name org-latex-to-mathml-jar-file)))
  16909. (?I . ,(shell-quote-argument tmp-in-file))
  16910. (?o . ,(shell-quote-argument tmp-out-file)))))
  16911. mathml shell-command-output)
  16912. (when (org-called-interactively-p 'any)
  16913. (unless (org-format-latex-mathml-available-p)
  16914. (user-error "LaTeX to MathML converter not configured")))
  16915. (message "Running %s" cmd)
  16916. (setq shell-command-output (shell-command-to-string cmd))
  16917. (setq mathml
  16918. (when (file-readable-p tmp-out-file)
  16919. (with-current-buffer (find-file-noselect tmp-out-file t)
  16920. (goto-char (point-min))
  16921. (when (re-search-forward
  16922. (concat
  16923. (regexp-quote
  16924. "<math xmlns=\"http://www.w3.org/1998/Math/MathML\">")
  16925. "\\(.\\|\n\\)*"
  16926. (regexp-quote "</math>")) nil t)
  16927. (prog1 (match-string 0) (kill-buffer))))))
  16928. (cond
  16929. (mathml
  16930. (setq mathml
  16931. (concat "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" mathml))
  16932. (when mathml-file
  16933. (write-region mathml nil mathml-file))
  16934. (when (org-called-interactively-p 'any)
  16935. (message mathml)))
  16936. ((message "LaTeX to MathML conversion failed")
  16937. (message shell-command-output)))
  16938. (delete-file tmp-in-file)
  16939. (when (file-exists-p tmp-out-file)
  16940. (delete-file tmp-out-file))
  16941. mathml))
  16942. (defun org-format-latex-as-mathml (latex-frag latex-frag-type
  16943. prefix &optional dir)
  16944. "Use `org-create-math-formula' but check local cache first."
  16945. (let* ((absprefix (expand-file-name prefix dir))
  16946. (print-length nil) (print-level nil)
  16947. (formula-id (concat
  16948. "formula-"
  16949. (sha1
  16950. (prin1-to-string
  16951. (list latex-frag
  16952. org-latex-to-mathml-convert-command)))))
  16953. (formula-cache (format "%s-%s.mathml" absprefix formula-id))
  16954. (formula-cache-dir (file-name-directory formula-cache)))
  16955. (unless (file-directory-p formula-cache-dir)
  16956. (make-directory formula-cache-dir t))
  16957. (unless (file-exists-p formula-cache)
  16958. (org-create-math-formula latex-frag formula-cache))
  16959. (if (file-exists-p formula-cache)
  16960. ;; Successful conversion. Return the link to MathML file.
  16961. (org-add-props
  16962. (format "[[file:%s]]" (file-relative-name formula-cache dir))
  16963. (list 'org-latex-src (replace-regexp-in-string "\"" "" latex-frag)
  16964. 'org-latex-src-embed-type (if latex-frag-type
  16965. 'paragraph 'character)))
  16966. ;; Failed conversion. Return the LaTeX fragment verbatim
  16967. latex-frag)))
  16968. (defun org-create-formula-image (string tofile options buffer &optional type)
  16969. "Create an image from LaTeX source using dvipng or convert.
  16970. This function calls either `org-create-formula-image-with-dvipng'
  16971. or `org-create-formula-image-with-imagemagick' depending on the
  16972. value of `org-latex-create-formula-image-program' or on the value
  16973. of the optional TYPE variable.
  16974. Note: ultimately these two function should be combined as they
  16975. share a good deal of logic."
  16976. (org-check-external-command
  16977. "latex" "needed to convert LaTeX fragments to images")
  16978. (funcall
  16979. (case (or type org-latex-create-formula-image-program)
  16980. ('dvipng
  16981. (org-check-external-command
  16982. "dvipng" "needed to convert LaTeX fragments to images")
  16983. #'org-create-formula-image-with-dvipng)
  16984. ('imagemagick
  16985. (org-check-external-command
  16986. "convert" "you need to install imagemagick")
  16987. #'org-create-formula-image-with-imagemagick)
  16988. (t (error
  16989. "Invalid value of `org-latex-create-formula-image-program'")))
  16990. string tofile options buffer))
  16991. (declare-function org-export--get-global-options "ox" (&optional backend))
  16992. (declare-function org-export--get-inbuffer-options "ox" (&optional backend))
  16993. (defun org-create-formula--latex-header ()
  16994. "Return LaTeX header appropriate for previewing a LaTeX snippet."
  16995. (org-latex-guess-inputenc
  16996. (org-splice-latex-header
  16997. org-format-latex-header
  16998. org-latex-default-packages-alist
  16999. org-latex-packages-alist t
  17000. (plist-get
  17001. (org-combine-plists
  17002. (org-export--get-global-options 'latex)
  17003. (org-export--get-inbuffer-options 'latex))
  17004. :latex-header))))
  17005. ;; This function borrows from Ganesh Swami's latex2png.el
  17006. (defun org-create-formula-image-with-dvipng (string tofile options buffer)
  17007. "This calls dvipng."
  17008. (require 'ox-latex)
  17009. (let* ((tmpdir (if (featurep 'xemacs)
  17010. (temp-directory)
  17011. temporary-file-directory))
  17012. (texfilebase (make-temp-name
  17013. (expand-file-name "orgtex" tmpdir)))
  17014. (texfile (concat texfilebase ".tex"))
  17015. (dvifile (concat texfilebase ".dvi"))
  17016. (pngfile (concat texfilebase ".png"))
  17017. (fnh (if (featurep 'xemacs)
  17018. (font-height (face-font 'default))
  17019. (face-attribute 'default :height nil)))
  17020. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  17021. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  17022. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  17023. "Black"))
  17024. (bg (or (plist-get options (if buffer :background :html-background))
  17025. "Transparent")))
  17026. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground))
  17027. (unless (string= fg "Transparent") (setq fg (org-dvipng-color-format fg))))
  17028. (if (eq bg 'default) (setq bg (org-dvipng-color :background))
  17029. (unless (string= bg "Transparent") (setq bg (org-dvipng-color-format bg))))
  17030. (let ((latex-header (org-create-formula--latex-header)))
  17031. (with-temp-file texfile
  17032. (insert latex-header)
  17033. (insert "\n\\begin{document}\n" string "\n\\end{document}\n")))
  17034. (let ((dir default-directory))
  17035. (condition-case nil
  17036. (progn
  17037. (cd tmpdir)
  17038. (call-process "latex" nil nil nil texfile))
  17039. (error nil))
  17040. (cd dir))
  17041. (if (not (file-exists-p dvifile))
  17042. (progn (message "Failed to create dvi file from %s" texfile) nil)
  17043. (condition-case nil
  17044. (if (featurep 'xemacs)
  17045. (call-process "dvipng" nil nil nil
  17046. "-fg" fg "-bg" bg
  17047. "-T" "tight"
  17048. "-o" pngfile
  17049. dvifile)
  17050. (call-process "dvipng" nil nil nil
  17051. "-fg" fg "-bg" bg
  17052. "-D" dpi
  17053. ;;"-x" scale "-y" scale
  17054. "-T" "tight"
  17055. "-o" pngfile
  17056. dvifile))
  17057. (error nil))
  17058. (if (not (file-exists-p pngfile))
  17059. (if org-format-latex-signal-error
  17060. (error "Failed to create png file from %s" texfile)
  17061. (message "Failed to create png file from %s" texfile)
  17062. nil)
  17063. ;; Use the requested file name and clean up
  17064. (copy-file pngfile tofile 'replace)
  17065. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png" ".out") do
  17066. (if (file-exists-p (concat texfilebase e))
  17067. (delete-file (concat texfilebase e))))
  17068. pngfile))))
  17069. (declare-function org-latex-compile "ox-latex" (texfile &optional snippet))
  17070. (defun org-create-formula-image-with-imagemagick (string tofile options buffer)
  17071. "This calls convert, which is included into imagemagick."
  17072. (require 'ox-latex)
  17073. (let* ((tmpdir (if (featurep 'xemacs)
  17074. (temp-directory)
  17075. temporary-file-directory))
  17076. (texfilebase (make-temp-name
  17077. (expand-file-name "orgtex" tmpdir)))
  17078. (texfile (concat texfilebase ".tex"))
  17079. (pdffile (concat texfilebase ".pdf"))
  17080. (pngfile (concat texfilebase ".png"))
  17081. (fnh (if (featurep 'xemacs)
  17082. (font-height (face-font 'default))
  17083. (face-attribute 'default :height nil)))
  17084. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  17085. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  17086. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  17087. "black"))
  17088. (bg (or (plist-get options (if buffer :background :html-background))
  17089. "white")))
  17090. (if (eq fg 'default) (setq fg (org-latex-color :foreground))
  17091. (setq fg (org-latex-color-format fg)))
  17092. (if (eq bg 'default) (setq bg (org-latex-color :background))
  17093. (setq bg (org-latex-color-format
  17094. (if (string= bg "Transparent") "white" bg))))
  17095. (let ((latex-header (org-create-formula--latex-header)))
  17096. (with-temp-file texfile
  17097. (insert latex-header)
  17098. (insert "\n\\begin{document}\n"
  17099. "\\definecolor{fg}{rgb}{" fg "}\n"
  17100. "\\definecolor{bg}{rgb}{" bg "}\n"
  17101. "\n\\pagecolor{bg}\n"
  17102. "\n{\\color{fg}\n"
  17103. string
  17104. "\n}\n"
  17105. "\n\\end{document}\n")))
  17106. (org-latex-compile texfile t)
  17107. (if (not (file-exists-p pdffile))
  17108. (progn (message "Failed to create pdf file from %s" texfile) nil)
  17109. (condition-case nil
  17110. (if (featurep 'xemacs)
  17111. (call-process "convert" nil nil nil
  17112. "-density" "96"
  17113. "-trim"
  17114. "-antialias"
  17115. pdffile
  17116. "-quality" "100"
  17117. ;; "-sharpen" "0x1.0"
  17118. pngfile)
  17119. (call-process "convert" nil nil nil
  17120. "-density" dpi
  17121. "-trim"
  17122. "-antialias"
  17123. pdffile
  17124. "-quality" "100"
  17125. ;; "-sharpen" "0x1.0"
  17126. pngfile))
  17127. (error nil))
  17128. (if (not (file-exists-p pngfile))
  17129. (if org-format-latex-signal-error
  17130. (error "Failed to create png file from %s" texfile)
  17131. (message "Failed to create png file from %s" texfile)
  17132. nil)
  17133. ;; Use the requested file name and clean up
  17134. (copy-file pngfile tofile 'replace)
  17135. (loop for e in '(".pdf" ".tex" ".aux" ".log" ".png") do
  17136. (if (file-exists-p (concat texfilebase e))
  17137. (delete-file (concat texfilebase e))))
  17138. pngfile))))
  17139. (defun org-splice-latex-header (tpl def-pkg pkg snippets-p &optional extra)
  17140. "Fill a LaTeX header template TPL.
  17141. In the template, the following place holders will be recognized:
  17142. [DEFAULT-PACKAGES] \\usepackage statements for DEF-PKG
  17143. [NO-DEFAULT-PACKAGES] do not include DEF-PKG
  17144. [PACKAGES] \\usepackage statements for PKG
  17145. [NO-PACKAGES] do not include PKG
  17146. [EXTRA] the string EXTRA
  17147. [NO-EXTRA] do not include EXTRA
  17148. For backward compatibility, if both the positive and the negative place
  17149. holder is missing, the positive one (without the \"NO-\") will be
  17150. assumed to be present at the end of the template.
  17151. DEF-PKG and PKG are assumed to be alists of options/packagename lists.
  17152. EXTRA is a string.
  17153. SNIPPETS-P indicates if this is run to create snippet images for HTML."
  17154. (let (rpl (end ""))
  17155. (if (string-match "^[ \t]*\\[\\(NO-\\)?DEFAULT-PACKAGES\\][ \t]*\n?" tpl)
  17156. (setq rpl (if (or (match-end 1) (not def-pkg))
  17157. "" (org-latex-packages-to-string def-pkg snippets-p t))
  17158. tpl (replace-match rpl t t tpl))
  17159. (if def-pkg (setq end (org-latex-packages-to-string def-pkg snippets-p))))
  17160. (if (string-match "\\[\\(NO-\\)?PACKAGES\\][ \t]*\n?" tpl)
  17161. (setq rpl (if (or (match-end 1) (not pkg))
  17162. "" (org-latex-packages-to-string pkg snippets-p t))
  17163. tpl (replace-match rpl t t tpl))
  17164. (if pkg (setq end
  17165. (concat end "\n"
  17166. (org-latex-packages-to-string pkg snippets-p)))))
  17167. (if (string-match "\\[\\(NO-\\)?EXTRA\\][ \t]*\n?" tpl)
  17168. (setq rpl (if (or (match-end 1) (not extra))
  17169. "" (concat extra "\n"))
  17170. tpl (replace-match rpl t t tpl))
  17171. (if (and extra (string-match "\\S-" extra))
  17172. (setq end (concat end "\n" extra))))
  17173. (if (string-match "\\S-" end)
  17174. (concat tpl "\n" end)
  17175. tpl)))
  17176. (defun org-latex-packages-to-string (pkg &optional snippets-p newline)
  17177. "Turn an alist of packages into a string with the \\usepackage macros."
  17178. (setq pkg (mapconcat (lambda(p)
  17179. (cond
  17180. ((stringp p) p)
  17181. ((and snippets-p (>= (length p) 3) (not (nth 2 p)))
  17182. (format "%% Package %s omitted" (cadr p)))
  17183. ((equal "" (car p))
  17184. (format "\\usepackage{%s}" (cadr p)))
  17185. (t
  17186. (format "\\usepackage[%s]{%s}"
  17187. (car p) (cadr p)))))
  17188. pkg
  17189. "\n"))
  17190. (if newline (concat pkg "\n") pkg))
  17191. (defun org-dvipng-color (attr)
  17192. "Return a RGB color specification for dvipng."
  17193. (apply 'format "rgb %s %s %s"
  17194. (mapcar 'org-normalize-color
  17195. (if (featurep 'xemacs)
  17196. (color-rgb-components
  17197. (face-property 'default
  17198. (cond ((eq attr :foreground) 'foreground)
  17199. ((eq attr :background) 'background))))
  17200. (color-values (face-attribute 'default attr nil))))))
  17201. (defun org-dvipng-color-format (color-name)
  17202. "Convert COLOR-NAME to a RGB color value for dvipng."
  17203. (apply 'format "rgb %s %s %s"
  17204. (mapcar 'org-normalize-color
  17205. (color-values color-name))))
  17206. (defun org-latex-color (attr)
  17207. "Return a RGB color for the LaTeX color package."
  17208. (apply 'format "%s,%s,%s"
  17209. (mapcar 'org-normalize-color
  17210. (if (featurep 'xemacs)
  17211. (color-rgb-components
  17212. (face-property 'default
  17213. (cond ((eq attr :foreground) 'foreground)
  17214. ((eq attr :background) 'background))))
  17215. (color-values (face-attribute 'default attr nil))))))
  17216. (defun org-latex-color-format (color-name)
  17217. "Convert COLOR-NAME to a RGB color value."
  17218. (apply 'format "%s,%s,%s"
  17219. (mapcar 'org-normalize-color
  17220. (color-values color-name))))
  17221. (defun org-normalize-color (value)
  17222. "Return string to be used as color value for an RGB component."
  17223. (format "%g" (/ value 65535.0)))
  17224. ;; Image display
  17225. (defvar org-inline-image-overlays nil)
  17226. (make-variable-buffer-local 'org-inline-image-overlays)
  17227. (defun org-toggle-inline-images (&optional include-linked)
  17228. "Toggle the display of inline images.
  17229. INCLUDE-LINKED is passed to `org-display-inline-images'."
  17230. (interactive "P")
  17231. (if org-inline-image-overlays
  17232. (progn
  17233. (org-remove-inline-images)
  17234. (message "Inline image display turned off"))
  17235. (org-display-inline-images include-linked)
  17236. (if (and (org-called-interactively-p)
  17237. org-inline-image-overlays)
  17238. (message "%d images displayed inline"
  17239. (length org-inline-image-overlays))
  17240. (message "No images to display inline"))))
  17241. (defun org-redisplay-inline-images ()
  17242. "Refresh the display of inline images."
  17243. (interactive)
  17244. (if (not org-inline-image-overlays)
  17245. (org-toggle-inline-images)
  17246. (org-toggle-inline-images)
  17247. (org-toggle-inline-images)))
  17248. (defun org-display-inline-images (&optional include-linked refresh beg end)
  17249. "Display inline images.
  17250. Normally only links without a description part are inlined, because this
  17251. is how it will work for export. When INCLUDE-LINKED is set, also links
  17252. with a description part will be inlined. This can be nice for a quick
  17253. look at those images, but it does not reflect what exported files will look
  17254. like.
  17255. When REFRESH is set, refresh existing images between BEG and END.
  17256. This will create new image displays only if necessary.
  17257. BEG and END default to the buffer boundaries."
  17258. (interactive "P")
  17259. (when (display-graphic-p)
  17260. (unless refresh
  17261. (org-remove-inline-images)
  17262. (if (fboundp 'clear-image-cache) (clear-image-cache)))
  17263. (save-excursion
  17264. (save-restriction
  17265. (widen)
  17266. (setq beg (or beg (point-min)) end (or end (point-max)))
  17267. (goto-char beg)
  17268. (let ((re (concat "\\[\\[\\(\\(file:\\)\\|\\([./~]\\)\\)\\([^]\n]+?"
  17269. (substring (org-image-file-name-regexp) 0 -2)
  17270. "\\)\\]" (if include-linked "" "\\]")))
  17271. (case-fold-search t)
  17272. old file ov img type attrwidth width)
  17273. (while (re-search-forward re end t)
  17274. (setq old (get-char-property-and-overlay (match-beginning 1)
  17275. 'org-image-overlay)
  17276. file (expand-file-name
  17277. (concat (or (match-string 3) "") (match-string 4))))
  17278. (when (image-type-available-p 'imagemagick)
  17279. (setq attrwidth (if (or (listp org-image-actual-width)
  17280. (null org-image-actual-width))
  17281. (save-excursion
  17282. (save-match-data
  17283. (when (re-search-backward
  17284. "#\\+attr.*:width[ \t]+\\([^ ]+\\)"
  17285. (save-excursion
  17286. (re-search-backward "^[ \t]*$\\|\\`" nil t)) t)
  17287. (string-to-number (match-string 1))))))
  17288. width (cond ((eq org-image-actual-width t) nil)
  17289. ((null org-image-actual-width) attrwidth)
  17290. ((numberp org-image-actual-width)
  17291. org-image-actual-width)
  17292. ((listp org-image-actual-width)
  17293. (or attrwidth (car org-image-actual-width))))
  17294. type (if width 'imagemagick)))
  17295. (when (file-exists-p file)
  17296. (if (and (car-safe old) refresh)
  17297. (image-refresh (overlay-get (cdr old) 'display))
  17298. (setq img (save-match-data (create-image file type nil :width width)))
  17299. (when img
  17300. (setq ov (make-overlay (match-beginning 0) (match-end 0)))
  17301. (overlay-put ov 'display img)
  17302. (overlay-put ov 'face 'default)
  17303. (overlay-put ov 'org-image-overlay t)
  17304. (overlay-put ov 'modification-hooks
  17305. (list 'org-display-inline-remove-overlay))
  17306. (push ov org-inline-image-overlays))))))))))
  17307. (define-obsolete-function-alias
  17308. 'org-display-inline-modification-hook 'org-display-inline-remove-overlay "24.3")
  17309. (defun org-display-inline-remove-overlay (ov after beg end &optional len)
  17310. "Remove inline-display overlay if a corresponding region is modified."
  17311. (let ((inhibit-modification-hooks t))
  17312. (when (and ov after)
  17313. (delete ov org-inline-image-overlays)
  17314. (delete-overlay ov))))
  17315. (defun org-remove-inline-images ()
  17316. "Remove inline display of images."
  17317. (interactive)
  17318. (mapc 'delete-overlay org-inline-image-overlays)
  17319. (setq org-inline-image-overlays nil))
  17320. ;;;; Key bindings
  17321. ;; Outline functions from `outline-mode-prefix-map'
  17322. ;; that can be remapped in Org:
  17323. (define-key org-mode-map [remap outline-mark-subtree] 'org-mark-subtree)
  17324. (define-key org-mode-map [remap show-subtree] 'org-show-subtree)
  17325. (define-key org-mode-map [remap outline-forward-same-level]
  17326. 'org-forward-heading-same-level)
  17327. (define-key org-mode-map [remap outline-backward-same-level]
  17328. 'org-backward-heading-same-level)
  17329. (define-key org-mode-map [remap show-branches]
  17330. 'org-kill-note-or-show-branches)
  17331. (define-key org-mode-map [remap outline-promote] 'org-promote-subtree)
  17332. (define-key org-mode-map [remap outline-demote] 'org-demote-subtree)
  17333. (define-key org-mode-map [remap outline-insert-heading] 'org-ctrl-c-ret)
  17334. ;; Outline functions from `outline-mode-prefix-map' that can not
  17335. ;; be remapped in Org:
  17336. ;;
  17337. ;; - the column "key binding" shows whether the Outline function is still
  17338. ;; available in Org mode on the same key that it has been bound to in
  17339. ;; Outline mode:
  17340. ;; - "overridden": key used for a different functionality in Org mode
  17341. ;; - else: key still bound to the same Outline function in Org mode
  17342. ;;
  17343. ;; | Outline function | key binding | Org replacement |
  17344. ;; |------------------------------------+-------------+-----------------------|
  17345. ;; | `outline-next-visible-heading' | `C-c C-n' | still same function |
  17346. ;; | `outline-previous-visible-heading' | `C-c C-p' | still same function |
  17347. ;; | `outline-up-heading' | `C-c C-u' | still same function |
  17348. ;; | `outline-move-subtree-up' | overridden | better: org-shiftup |
  17349. ;; | `outline-move-subtree-down' | overridden | better: org-shiftdown |
  17350. ;; | `show-entry' | overridden | no replacement |
  17351. ;; | `show-children' | `C-c C-i' | visibility cycling |
  17352. ;; | `show-branches' | `C-c C-k' | still same function |
  17353. ;; | `show-subtree' | overridden | visibility cycling |
  17354. ;; | `show-all' | overridden | no replacement |
  17355. ;; | `hide-subtree' | overridden | visibility cycling |
  17356. ;; | `hide-body' | overridden | no replacement |
  17357. ;; | `hide-entry' | overridden | visibility cycling |
  17358. ;; | `hide-leaves' | overridden | no replacement |
  17359. ;; | `hide-sublevels' | overridden | no replacement |
  17360. ;; | `hide-other' | overridden | no replacement |
  17361. ;; Make `C-c C-x' a prefix key
  17362. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  17363. ;; TAB key with modifiers
  17364. (org-defkey org-mode-map "\C-i" 'org-cycle)
  17365. (org-defkey org-mode-map [(tab)] 'org-cycle)
  17366. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  17367. (org-defkey org-mode-map "\M-\t" 'pcomplete)
  17368. ;; The following line is necessary under Suse GNU/Linux
  17369. (unless (featurep 'xemacs)
  17370. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  17371. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  17372. (define-key org-mode-map [backtab] 'org-shifttab)
  17373. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  17374. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  17375. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  17376. ;; Cursor keys with modifiers
  17377. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  17378. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  17379. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  17380. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  17381. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  17382. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  17383. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  17384. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  17385. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  17386. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  17387. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  17388. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  17389. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  17390. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  17391. (org-defkey org-mode-map [(control shift up)] 'org-shiftcontrolup)
  17392. (org-defkey org-mode-map [(control shift down)] 'org-shiftcontroldown)
  17393. ;; Babel keys
  17394. (define-key org-mode-map org-babel-key-prefix org-babel-map)
  17395. (mapc (lambda (pair)
  17396. (define-key org-babel-map (car pair) (cdr pair)))
  17397. org-babel-key-bindings)
  17398. ;;; Extra keys for tty access.
  17399. ;; We only set them when really needed because otherwise the
  17400. ;; menus don't show the simple keys
  17401. (when (or org-use-extra-keys
  17402. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  17403. (not window-system))
  17404. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  17405. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  17406. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  17407. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  17408. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  17409. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  17410. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  17411. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  17412. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  17413. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  17414. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  17415. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  17416. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  17417. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  17418. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  17419. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  17420. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  17421. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  17422. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  17423. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  17424. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  17425. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
  17426. (org-defkey org-mode-map [?\e (tab)] 'pcomplete)
  17427. (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
  17428. (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
  17429. (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
  17430. (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
  17431. (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
  17432. ;; All the other keys
  17433. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  17434. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  17435. (if (boundp 'narrow-map)
  17436. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  17437. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  17438. (if (boundp 'narrow-map)
  17439. (org-defkey narrow-map "b" 'org-narrow-to-block)
  17440. (org-defkey org-mode-map "\C-xnb" 'org-narrow-to-block))
  17441. (if (boundp 'narrow-map)
  17442. (org-defkey narrow-map "e" 'org-narrow-to-element)
  17443. (org-defkey org-mode-map "\C-xne" 'org-narrow-to-element))
  17444. (org-defkey org-mode-map "\C-\M-t" 'org-transpose-element)
  17445. (org-defkey org-mode-map "\M-}" 'org-forward-element)
  17446. (org-defkey org-mode-map "\M-{" 'org-backward-element)
  17447. (org-defkey org-mode-map "\C-c\C-^" 'org-up-element)
  17448. (org-defkey org-mode-map "\C-c\C-_" 'org-down-element)
  17449. (org-defkey org-mode-map "\C-c\C-f" 'org-forward-heading-same-level)
  17450. (org-defkey org-mode-map "\C-c\C-b" 'org-backward-heading-same-level)
  17451. (org-defkey org-mode-map "\C-c\M-f" 'org-next-block)
  17452. (org-defkey org-mode-map "\C-c\M-b" 'org-previous-block)
  17453. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  17454. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  17455. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
  17456. (org-defkey org-mode-map "\C-c\C-xd" 'org-insert-drawer)
  17457. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  17458. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  17459. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  17460. (org-defkey org-mode-map "\C-c\C-xq" 'org-toggle-tags-groups)
  17461. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  17462. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  17463. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  17464. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  17465. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  17466. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  17467. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  17468. (org-defkey org-mode-map "\C-c\M-w" 'org-copy)
  17469. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  17470. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  17471. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  17472. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  17473. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  17474. (org-defkey org-mode-map "\C-c\C-xv" 'org-copy-visible)
  17475. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  17476. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  17477. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  17478. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  17479. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  17480. (org-defkey org-mode-map "\C-c\C-\M-l" 'org-insert-all-links)
  17481. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  17482. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  17483. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  17484. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  17485. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  17486. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  17487. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  17488. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  17489. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  17490. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  17491. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  17492. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  17493. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  17494. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  17495. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  17496. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  17497. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  17498. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  17499. (org-defkey org-mode-map "\C-c^" 'org-sort)
  17500. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  17501. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  17502. (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
  17503. (org-defkey org-mode-map [remap open-line] 'org-open-line)
  17504. (org-defkey org-mode-map "\C-m" 'org-return)
  17505. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  17506. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  17507. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  17508. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  17509. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  17510. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  17511. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  17512. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  17513. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  17514. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  17515. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  17516. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  17517. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  17518. (org-defkey org-mode-map "\C-c\C-e" 'org-export-dispatch)
  17519. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  17520. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  17521. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  17522. (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
  17523. (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
  17524. (org-defkey org-mode-map "\C-c@" 'org-mark-subtree)
  17525. (org-defkey org-mode-map "\M-h" 'org-mark-element)
  17526. (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
  17527. ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
  17528. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  17529. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  17530. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  17531. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  17532. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  17533. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-in-last)
  17534. (org-defkey org-mode-map "\C-c\C-x\C-z" 'org-resolve-clocks)
  17535. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  17536. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  17537. (org-defkey org-mode-map "\C-c\C-x\C-q" 'org-clock-cancel)
  17538. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  17539. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  17540. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  17541. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  17542. (org-defkey org-mode-map "\C-c\C-x\C-v" 'org-toggle-inline-images)
  17543. (org-defkey org-mode-map "\C-c\C-x\C-\M-v" 'org-redisplay-inline-images)
  17544. (org-defkey org-mode-map "\C-c\C-x\\" 'org-toggle-pretty-entities)
  17545. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  17546. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  17547. (org-defkey org-mode-map "\C-c\C-xP" 'org-set-property-and-value)
  17548. (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
  17549. (org-defkey org-mode-map "\C-c\C-xE" 'org-inc-effort)
  17550. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  17551. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  17552. (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
  17553. (org-defkey org-mode-map [(control ?c) (control ?x) ?\:] 'org-timer-cancel-timer)
  17554. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  17555. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  17556. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  17557. (org-defkey org-mode-map "\C-c\C-x_" 'org-timer-stop)
  17558. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  17559. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  17560. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  17561. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  17562. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  17563. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  17564. (when (featurep 'xemacs)
  17565. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  17566. (defconst org-speed-commands-default
  17567. '(
  17568. ("Outline Navigation")
  17569. ("n" . (org-speed-move-safe 'outline-next-visible-heading))
  17570. ("p" . (org-speed-move-safe 'outline-previous-visible-heading))
  17571. ("f" . (org-speed-move-safe 'org-forward-heading-same-level))
  17572. ("b" . (org-speed-move-safe 'org-backward-heading-same-level))
  17573. ("F" . org-next-block)
  17574. ("B" . org-previous-block)
  17575. ("u" . (org-speed-move-safe 'outline-up-heading))
  17576. ("j" . org-goto)
  17577. ("g" . (org-refile t))
  17578. ("Outline Visibility")
  17579. ("c" . org-cycle)
  17580. ("C" . org-shifttab)
  17581. (" " . org-display-outline-path)
  17582. ("s" . org-narrow-to-subtree)
  17583. ("=" . org-columns)
  17584. ("Outline Structure Editing")
  17585. ("U" . org-shiftmetaup)
  17586. ("D" . org-shiftmetadown)
  17587. ("r" . org-metaright)
  17588. ("l" . org-metaleft)
  17589. ("R" . org-shiftmetaright)
  17590. ("L" . org-shiftmetaleft)
  17591. ("i" . (progn (forward-char 1) (call-interactively
  17592. 'org-insert-heading-respect-content)))
  17593. ("^" . org-sort)
  17594. ("w" . org-refile)
  17595. ("a" . org-archive-subtree-default-with-confirmation)
  17596. ("@" . org-mark-subtree)
  17597. ("#" . org-toggle-comment)
  17598. ("Clock Commands")
  17599. ("I" . org-clock-in)
  17600. ("O" . org-clock-out)
  17601. ("Meta Data Editing")
  17602. ("t" . org-todo)
  17603. ("," . (org-priority))
  17604. ("0" . (org-priority ?\ ))
  17605. ("1" . (org-priority ?A))
  17606. ("2" . (org-priority ?B))
  17607. ("3" . (org-priority ?C))
  17608. (":" . org-set-tags-command)
  17609. ("e" . org-set-effort)
  17610. ("E" . org-inc-effort)
  17611. ("W" . (lambda(m) (interactive "sMinutes before warning: ")
  17612. (org-entry-put (point) "APPT_WARNTIME" m)))
  17613. ("Agenda Views etc")
  17614. ("v" . org-agenda)
  17615. ("/" . org-sparse-tree)
  17616. ("Misc")
  17617. ("o" . org-open-at-point)
  17618. ("?" . org-speed-command-help)
  17619. ("<" . (org-agenda-set-restriction-lock 'subtree))
  17620. (">" . (org-agenda-remove-restriction-lock))
  17621. )
  17622. "The default speed commands.")
  17623. (defun org-print-speed-command (e)
  17624. (if (> (length (car e)) 1)
  17625. (progn
  17626. (princ "\n")
  17627. (princ (car e))
  17628. (princ "\n")
  17629. (princ (make-string (length (car e)) ?-))
  17630. (princ "\n"))
  17631. (princ (car e))
  17632. (princ " ")
  17633. (if (symbolp (cdr e))
  17634. (princ (symbol-name (cdr e)))
  17635. (prin1 (cdr e)))
  17636. (princ "\n")))
  17637. (defun org-speed-command-help ()
  17638. "Show the available speed commands."
  17639. (interactive)
  17640. (if (not org-use-speed-commands)
  17641. (user-error "Speed commands are not activated, customize `org-use-speed-commands'")
  17642. (with-output-to-temp-buffer "*Help*"
  17643. (princ "User-defined Speed commands\n===========================\n")
  17644. (mapc 'org-print-speed-command org-speed-commands-user)
  17645. (princ "\n")
  17646. (princ "Built-in Speed commands\n=======================\n")
  17647. (mapc 'org-print-speed-command org-speed-commands-default))
  17648. (with-current-buffer "*Help*"
  17649. (setq truncate-lines t))))
  17650. (defun org-speed-move-safe (cmd)
  17651. "Execute CMD, but make sure that the cursor always ends up in a headline.
  17652. If not, return to the original position and throw an error."
  17653. (interactive)
  17654. (let ((pos (point)))
  17655. (call-interactively cmd)
  17656. (unless (and (bolp) (org-at-heading-p))
  17657. (goto-char pos)
  17658. (error "Boundary reached while executing %s" cmd))))
  17659. (defvar org-self-insert-command-undo-counter 0)
  17660. (defvar org-table-auto-blank-field) ; defined in org-table.el
  17661. (defvar org-speed-command nil)
  17662. (define-obsolete-function-alias
  17663. 'org-speed-command-default-hook 'org-speed-command-activate "24.3")
  17664. (defun org-speed-command-activate (keys)
  17665. "Hook for activating single-letter speed commands.
  17666. `org-speed-commands-default' specifies a minimal command set.
  17667. Use `org-speed-commands-user' for further customization."
  17668. (when (or (and (bolp) (looking-at org-outline-regexp))
  17669. (and (functionp org-use-speed-commands)
  17670. (funcall org-use-speed-commands)))
  17671. (cdr (assoc keys (append org-speed-commands-user
  17672. org-speed-commands-default)))))
  17673. (define-obsolete-function-alias
  17674. 'org-babel-speed-command-hook 'org-babel-speed-command-activate "24.3")
  17675. (defun org-babel-speed-command-activate (keys)
  17676. "Hook for activating single-letter code block commands."
  17677. (when (and (bolp) (looking-at org-babel-src-block-regexp))
  17678. (cdr (assoc keys org-babel-key-bindings))))
  17679. (defcustom org-speed-command-hook
  17680. '(org-speed-command-default-hook org-babel-speed-command-hook)
  17681. "Hook for activating speed commands at strategic locations.
  17682. Hook functions are called in sequence until a valid handler is
  17683. found.
  17684. Each hook takes a single argument, a user-pressed command key
  17685. which is also a `self-insert-command' from the global map.
  17686. Within the hook, examine the cursor position and the command key
  17687. and return nil or a valid handler as appropriate. Handler could
  17688. be one of an interactive command, a function, or a form.
  17689. Set `org-use-speed-commands' to non-nil value to enable this
  17690. hook. The default setting is `org-speed-command-activate'."
  17691. :group 'org-structure
  17692. :version "24.1"
  17693. :type 'hook)
  17694. (defun org-self-insert-command (N)
  17695. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  17696. If the cursor is in a table looking at whitespace, the whitespace is
  17697. overwritten, and the table is not marked as requiring realignment."
  17698. (interactive "p")
  17699. (org-check-before-invisible-edit 'insert)
  17700. (cond
  17701. ((and org-use-speed-commands
  17702. (setq org-speed-command
  17703. (run-hook-with-args-until-success
  17704. 'org-speed-command-hook (this-command-keys))))
  17705. (cond
  17706. ((commandp org-speed-command)
  17707. (setq this-command org-speed-command)
  17708. (call-interactively org-speed-command))
  17709. ((functionp org-speed-command)
  17710. (funcall org-speed-command))
  17711. ((and org-speed-command (listp org-speed-command))
  17712. (eval org-speed-command))
  17713. (t (let (org-use-speed-commands)
  17714. (call-interactively 'org-self-insert-command)))))
  17715. ((and
  17716. (org-table-p)
  17717. (progn
  17718. ;; check if we blank the field, and if that triggers align
  17719. (and (featurep 'org-table) org-table-auto-blank-field
  17720. (member last-command
  17721. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c yas/expand))
  17722. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  17723. ;; got extra space, this field does not determine column width
  17724. (let (org-table-may-need-update) (org-table-blank-field))
  17725. ;; no extra space, this field may determine column width
  17726. (org-table-blank-field)))
  17727. t)
  17728. (eq N 1)
  17729. (looking-at "[^|\n]* |"))
  17730. (let (org-table-may-need-update)
  17731. (goto-char (1- (match-end 0)))
  17732. (backward-delete-char 1)
  17733. (goto-char (match-beginning 0))
  17734. (self-insert-command N)))
  17735. (t
  17736. (setq org-table-may-need-update t)
  17737. (self-insert-command N)
  17738. (org-fix-tags-on-the-fly)
  17739. (if org-self-insert-cluster-for-undo
  17740. (if (not (eq last-command 'org-self-insert-command))
  17741. (setq org-self-insert-command-undo-counter 1)
  17742. (if (>= org-self-insert-command-undo-counter 20)
  17743. (setq org-self-insert-command-undo-counter 1)
  17744. (and (> org-self-insert-command-undo-counter 0)
  17745. buffer-undo-list (listp buffer-undo-list)
  17746. (not (cadr buffer-undo-list)) ; remove nil entry
  17747. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  17748. (setq org-self-insert-command-undo-counter
  17749. (1+ org-self-insert-command-undo-counter))))))))
  17750. (defun org-check-before-invisible-edit (kind)
  17751. "Check is editing if kind KIND would be dangerous with invisible text around.
  17752. The detailed reaction depends on the user option `org-catch-invisible-edits'."
  17753. ;; First, try to get out of here as quickly as possible, to reduce overhead
  17754. (if (and org-catch-invisible-edits
  17755. (or (not (boundp 'visible-mode)) (not visible-mode))
  17756. (or (get-char-property (point) 'invisible)
  17757. (get-char-property (max (point-min) (1- (point))) 'invisible)))
  17758. ;; OK, we need to take a closer look
  17759. (let* ((invisible-at-point (get-char-property (point) 'invisible))
  17760. (invisible-before-point (if (bobp) nil (get-char-property
  17761. (1- (point)) 'invisible)))
  17762. (border-and-ok-direction
  17763. (or
  17764. ;; Check if we are acting predictably before invisible text
  17765. (and invisible-at-point (not invisible-before-point)
  17766. (memq kind '(insert delete-backward)))
  17767. ;; Check if we are acting predictably after invisible text
  17768. ;; This works not well, and I have turned it off. It seems
  17769. ;; better to always show and stop after invisible text.
  17770. ;; (and (not invisible-at-point) invisible-before-point
  17771. ;; (memq kind '(insert delete)))
  17772. )))
  17773. (when (or (memq invisible-at-point '(outline org-hide-block t))
  17774. (memq invisible-before-point '(outline org-hide-block t)))
  17775. (if (eq org-catch-invisible-edits 'error)
  17776. (user-error "Editing in invisible areas is prohibited, make them visible first"))
  17777. (if (and org-custom-properties-overlays
  17778. (y-or-n-p "Display invisible properties in this buffer? "))
  17779. (org-toggle-custom-properties-visibility)
  17780. ;; Make the area visible
  17781. (save-excursion
  17782. (if invisible-before-point
  17783. (goto-char (previous-single-char-property-change
  17784. (point) 'invisible)))
  17785. (org-cycle))
  17786. (cond
  17787. ((eq org-catch-invisible-edits 'show)
  17788. ;; That's it, we do the edit after showing
  17789. (message
  17790. "Unfolding invisible region around point before editing")
  17791. (sit-for 1))
  17792. ((and (eq org-catch-invisible-edits 'smart)
  17793. border-and-ok-direction)
  17794. (message "Unfolding invisible region around point before editing"))
  17795. (t
  17796. ;; Don't do the edit, make the user repeat it in full visibility
  17797. (user-error "Edit in invisible region aborted, repeat to confirm with text visible"))))))))
  17798. (defun org-fix-tags-on-the-fly ()
  17799. (when (and (equal (char-after (point-at-bol)) ?*)
  17800. (org-at-heading-p))
  17801. (org-align-tags-here org-tags-column)))
  17802. (defun org-delete-backward-char (N)
  17803. "Like `delete-backward-char', insert whitespace at field end in tables.
  17804. When deleting backwards, in tables this function will insert whitespace in
  17805. front of the next \"|\" separator, to keep the table aligned. The table will
  17806. still be marked for re-alignment if the field did fill the entire column,
  17807. because, in this case the deletion might narrow the column."
  17808. (interactive "p")
  17809. (save-match-data
  17810. (org-check-before-invisible-edit 'delete-backward)
  17811. (if (and (org-table-p)
  17812. (eq N 1)
  17813. (string-match "|" (buffer-substring (point-at-bol) (point)))
  17814. (looking-at ".*?|"))
  17815. (let ((pos (point))
  17816. (noalign (looking-at "[^|\n\r]* |"))
  17817. (c org-table-may-need-update))
  17818. (backward-delete-char N)
  17819. (if (not overwrite-mode)
  17820. (progn
  17821. (skip-chars-forward "^|")
  17822. (insert " ")
  17823. (goto-char (1- pos))))
  17824. ;; noalign: if there were two spaces at the end, this field
  17825. ;; does not determine the width of the column.
  17826. (if noalign (setq org-table-may-need-update c)))
  17827. (backward-delete-char N)
  17828. (org-fix-tags-on-the-fly))))
  17829. (defun org-delete-char (N)
  17830. "Like `delete-char', but insert whitespace at field end in tables.
  17831. When deleting characters, in tables this function will insert whitespace in
  17832. front of the next \"|\" separator, to keep the table aligned. The table will
  17833. still be marked for re-alignment if the field did fill the entire column,
  17834. because, in this case the deletion might narrow the column."
  17835. (interactive "p")
  17836. (save-match-data
  17837. (org-check-before-invisible-edit 'delete)
  17838. (if (and (org-table-p)
  17839. (not (bolp))
  17840. (not (= (char-after) ?|))
  17841. (eq N 1))
  17842. (if (looking-at ".*?|")
  17843. (let ((pos (point))
  17844. (noalign (looking-at "[^|\n\r]* |"))
  17845. (c org-table-may-need-update))
  17846. (replace-match
  17847. (concat (substring (match-string 0) 1 -1) " |") nil t)
  17848. (goto-char pos)
  17849. ;; noalign: if there were two spaces at the end, this field
  17850. ;; does not determine the width of the column.
  17851. (if noalign (setq org-table-may-need-update c)))
  17852. (delete-char N))
  17853. (delete-char N)
  17854. (org-fix-tags-on-the-fly))))
  17855. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  17856. (put 'org-self-insert-command 'delete-selection t)
  17857. (put 'orgtbl-self-insert-command 'delete-selection t)
  17858. (put 'org-delete-char 'delete-selection 'supersede)
  17859. (put 'org-delete-backward-char 'delete-selection 'supersede)
  17860. (put 'org-yank 'delete-selection 'yank)
  17861. ;; Make `flyspell-mode' delay after some commands
  17862. (put 'org-self-insert-command 'flyspell-delayed t)
  17863. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  17864. (put 'org-delete-char 'flyspell-delayed t)
  17865. (put 'org-delete-backward-char 'flyspell-delayed t)
  17866. ;; Make pabbrev-mode expand after org-mode commands
  17867. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  17868. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  17869. ;; How to do this: Measure non-white length of current string
  17870. ;; If equal to column width, we should realign.
  17871. (defun org-remap (map &rest commands)
  17872. "In MAP, remap the functions given in COMMANDS.
  17873. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  17874. (let (new old)
  17875. (while commands
  17876. (setq old (pop commands) new (pop commands))
  17877. (if (fboundp 'command-remapping)
  17878. (org-defkey map (vector 'remap old) new)
  17879. (substitute-key-definition old new map global-map)))))
  17880. (defun org-transpose-words ()
  17881. "Transpose words for Org.
  17882. This uses the `org-mode-transpose-word-syntax-table' syntax
  17883. table, which interprets characters in `org-emphasis-alist' as
  17884. word constituants."
  17885. (interactive)
  17886. (with-syntax-table org-mode-transpose-word-syntax-table
  17887. (call-interactively 'transpose-words)))
  17888. (org-remap org-mode-map 'transpose-words 'org-transpose-words)
  17889. (when (eq org-enable-table-editor 'optimized)
  17890. ;; If the user wants maximum table support, we need to hijack
  17891. ;; some standard editing functions
  17892. (org-remap org-mode-map
  17893. 'self-insert-command 'org-self-insert-command
  17894. 'delete-char 'org-delete-char
  17895. 'delete-backward-char 'org-delete-backward-char)
  17896. (org-defkey org-mode-map "|" 'org-force-self-insert))
  17897. (defvar org-ctrl-c-ctrl-c-hook nil
  17898. "Hook for functions attaching themselves to `C-c C-c'.
  17899. This can be used to add additional functionality to the C-c C-c
  17900. key which executes context-dependent commands. This hook is run
  17901. before any other test, while `org-ctrl-c-ctrl-c-final-hook' is
  17902. run after the last test.
  17903. Each function will be called with no arguments. The function
  17904. must check if the context is appropriate for it to act. If yes,
  17905. it should do its thing and then return a non-nil value. If the
  17906. context is wrong, just do nothing and return nil.")
  17907. (defvar org-ctrl-c-ctrl-c-final-hook nil
  17908. "Hook for functions attaching themselves to `C-c C-c'.
  17909. This can be used to add additional functionality to the C-c C-c
  17910. key which executes context-dependent commands. This hook is run
  17911. after any other test, while `org-ctrl-c-ctrl-c-hook' is run
  17912. before the first test.
  17913. Each function will be called with no arguments. The function
  17914. must check if the context is appropriate for it to act. If yes,
  17915. it should do its thing and then return a non-nil value. If the
  17916. context is wrong, just do nothing and return nil.")
  17917. (defvar org-tab-first-hook nil
  17918. "Hook for functions to attach themselves to TAB.
  17919. See `org-ctrl-c-ctrl-c-hook' for more information.
  17920. This hook runs as the first action when TAB is pressed, even before
  17921. `org-cycle' messes around with the `outline-regexp' to cater for
  17922. inline tasks and plain list item folding.
  17923. If any function in this hook returns t, any other actions that
  17924. would have been caused by TAB (such as table field motion or visibility
  17925. cycling) will not occur.")
  17926. (defvar org-tab-after-check-for-table-hook nil
  17927. "Hook for functions to attach themselves to TAB.
  17928. See `org-ctrl-c-ctrl-c-hook' for more information.
  17929. This hook runs after it has been established that the cursor is not in a
  17930. table, but before checking if the cursor is in a headline or if global cycling
  17931. should be done.
  17932. If any function in this hook returns t, not other actions like visibility
  17933. cycling will be done.")
  17934. (defvar org-tab-after-check-for-cycling-hook nil
  17935. "Hook for functions to attach themselves to TAB.
  17936. See `org-ctrl-c-ctrl-c-hook' for more information.
  17937. This hook runs after it has been established that not table field motion and
  17938. not visibility should be done because of current context. This is probably
  17939. the place where a package like yasnippets can hook in.")
  17940. (defvar org-tab-before-tab-emulation-hook nil
  17941. "Hook for functions to attach themselves to TAB.
  17942. See `org-ctrl-c-ctrl-c-hook' for more information.
  17943. This hook runs after every other options for TAB have been exhausted, but
  17944. before indentation and \t insertion takes place.")
  17945. (defvar org-metaleft-hook nil
  17946. "Hook for functions attaching themselves to `M-left'.
  17947. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17948. (defvar org-metaright-hook nil
  17949. "Hook for functions attaching themselves to `M-right'.
  17950. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17951. (defvar org-metaup-hook nil
  17952. "Hook for functions attaching themselves to `M-up'.
  17953. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17954. (defvar org-metadown-hook nil
  17955. "Hook for functions attaching themselves to `M-down'.
  17956. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17957. (defvar org-shiftmetaleft-hook nil
  17958. "Hook for functions attaching themselves to `M-S-left'.
  17959. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17960. (defvar org-shiftmetaright-hook nil
  17961. "Hook for functions attaching themselves to `M-S-right'.
  17962. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17963. (defvar org-shiftmetaup-hook nil
  17964. "Hook for functions attaching themselves to `M-S-up'.
  17965. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17966. (defvar org-shiftmetadown-hook nil
  17967. "Hook for functions attaching themselves to `M-S-down'.
  17968. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17969. (defvar org-metareturn-hook nil
  17970. "Hook for functions attaching themselves to `M-RET'.
  17971. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17972. (defvar org-shiftup-hook nil
  17973. "Hook for functions attaching themselves to `S-up'.
  17974. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17975. (defvar org-shiftup-final-hook nil
  17976. "Hook for functions attaching themselves to `S-up'.
  17977. This one runs after all other options except shift-select have been excluded.
  17978. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17979. (defvar org-shiftdown-hook nil
  17980. "Hook for functions attaching themselves to `S-down'.
  17981. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17982. (defvar org-shiftdown-final-hook nil
  17983. "Hook for functions attaching themselves to `S-down'.
  17984. This one runs after all other options except shift-select have been excluded.
  17985. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17986. (defvar org-shiftleft-hook nil
  17987. "Hook for functions attaching themselves to `S-left'.
  17988. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17989. (defvar org-shiftleft-final-hook nil
  17990. "Hook for functions attaching themselves to `S-left'.
  17991. This one runs after all other options except shift-select have been excluded.
  17992. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17993. (defvar org-shiftright-hook nil
  17994. "Hook for functions attaching themselves to `S-right'.
  17995. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17996. (defvar org-shiftright-final-hook nil
  17997. "Hook for functions attaching themselves to `S-right'.
  17998. This one runs after all other options except shift-select have been excluded.
  17999. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18000. (defun org-modifier-cursor-error ()
  18001. "Throw an error, a modified cursor command was applied in wrong context."
  18002. (user-error "This command is active in special context like tables, headlines or items"))
  18003. (defun org-shiftselect-error ()
  18004. "Throw an error because Shift-Cursor command was applied in wrong context."
  18005. (if (and (boundp 'shift-select-mode) shift-select-mode)
  18006. (user-error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
  18007. (user-error "This command works only in special context like headlines or timestamps")))
  18008. (defun org-call-for-shift-select (cmd)
  18009. (let ((this-command-keys-shift-translated t))
  18010. (call-interactively cmd)))
  18011. (defun org-shifttab (&optional arg)
  18012. "Global visibility cycling or move to previous table field.
  18013. Call `org-table-previous-field' within a table.
  18014. When ARG is nil, cycle globally through visibility states.
  18015. When ARG is a numeric prefix, show contents of this level."
  18016. (interactive "P")
  18017. (cond
  18018. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  18019. ((integerp arg)
  18020. (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
  18021. (message "Content view to level: %d" arg)
  18022. (org-content (prefix-numeric-value arg2))
  18023. (org-cycle-show-empty-lines t)
  18024. (setq org-cycle-global-status 'overview)))
  18025. (t (call-interactively 'org-global-cycle))))
  18026. (defun org-shiftmetaleft ()
  18027. "Promote subtree or delete table column.
  18028. Calls `org-promote-subtree', `org-outdent-item-tree', or
  18029. `org-table-delete-column', depending on context. See the
  18030. individual commands for more information."
  18031. (interactive)
  18032. (cond
  18033. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  18034. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  18035. ((org-at-heading-p) (call-interactively 'org-promote-subtree))
  18036. ((if (not (org-region-active-p)) (org-at-item-p)
  18037. (save-excursion (goto-char (region-beginning))
  18038. (org-at-item-p)))
  18039. (call-interactively 'org-outdent-item-tree))
  18040. (t (org-modifier-cursor-error))))
  18041. (defun org-shiftmetaright ()
  18042. "Demote subtree or insert table column.
  18043. Calls `org-demote-subtree', `org-indent-item-tree', or
  18044. `org-table-insert-column', depending on context. See the
  18045. individual commands for more information."
  18046. (interactive)
  18047. (cond
  18048. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  18049. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  18050. ((org-at-heading-p) (call-interactively 'org-demote-subtree))
  18051. ((if (not (org-region-active-p)) (org-at-item-p)
  18052. (save-excursion (goto-char (region-beginning))
  18053. (org-at-item-p)))
  18054. (call-interactively 'org-indent-item-tree))
  18055. (t (org-modifier-cursor-error))))
  18056. (defun org-shiftmetaup (&optional arg)
  18057. "Move subtree up or kill table row.
  18058. Calls `org-move-subtree-up' or `org-table-kill-row' or
  18059. `org-move-item-up' or `org-timestamp-up', depending on context.
  18060. See the individual commands for more information."
  18061. (interactive "P")
  18062. (cond
  18063. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  18064. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  18065. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  18066. ((org-at-item-p) (call-interactively 'org-move-item-up))
  18067. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  18068. (call-interactively 'org-timestamp-up)))
  18069. (t (call-interactively 'org-drag-line-backward))))
  18070. (defun org-shiftmetadown (&optional arg)
  18071. "Move subtree down or insert table row.
  18072. Calls `org-move-subtree-down' or `org-table-insert-row' or
  18073. `org-move-item-down' or `org-timestamp-up', depending on context.
  18074. See the individual commands for more information."
  18075. (interactive "P")
  18076. (cond
  18077. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  18078. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  18079. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  18080. ((org-at-item-p) (call-interactively 'org-move-item-down))
  18081. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  18082. (call-interactively 'org-timestamp-down)))
  18083. (t (call-interactively 'org-drag-line-forward))))
  18084. (defsubst org-hidden-tree-error ()
  18085. (user-error
  18086. "Hidden subtree, open with TAB or use subtree command M-S-<left>/<right>"))
  18087. (defun org-metaleft (&optional arg)
  18088. "Promote heading or move table column to left.
  18089. Calls `org-do-promote' or `org-table-move-column', depending on context.
  18090. With no specific context, calls the Emacs default `backward-word'.
  18091. See the individual commands for more information."
  18092. (interactive "P")
  18093. (cond
  18094. ((run-hook-with-args-until-success 'org-metaleft-hook))
  18095. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  18096. ((org-with-limited-levels
  18097. (or (org-at-heading-p)
  18098. (and (org-region-active-p)
  18099. (save-excursion
  18100. (goto-char (region-beginning))
  18101. (org-at-heading-p)))))
  18102. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  18103. (call-interactively 'org-do-promote))
  18104. ;; At an inline task.
  18105. ((org-at-heading-p)
  18106. (call-interactively 'org-inlinetask-promote))
  18107. ((or (org-at-item-p)
  18108. (and (org-region-active-p)
  18109. (save-excursion
  18110. (goto-char (region-beginning))
  18111. (org-at-item-p))))
  18112. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  18113. (call-interactively 'org-outdent-item))
  18114. (t (call-interactively 'backward-word))))
  18115. (defun org-metaright (&optional arg)
  18116. "Demote a subtree, a list item or move table column to right.
  18117. In front of a drawer or a block keyword, indent it correctly.
  18118. With no specific context, calls the Emacs default `forward-word'.
  18119. See the individual commands for more information."
  18120. (interactive "P")
  18121. (cond
  18122. ((run-hook-with-args-until-success 'org-metaright-hook))
  18123. ((org-at-table-p) (call-interactively 'org-table-move-column))
  18124. ((org-at-drawer-p) (call-interactively 'org-indent-drawer))
  18125. ((org-at-block-p) (call-interactively 'org-indent-block))
  18126. ((org-with-limited-levels
  18127. (or (org-at-heading-p)
  18128. (and (org-region-active-p)
  18129. (save-excursion
  18130. (goto-char (region-beginning))
  18131. (org-at-heading-p)))))
  18132. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  18133. (call-interactively 'org-do-demote))
  18134. ;; At an inline task.
  18135. ((org-at-heading-p)
  18136. (call-interactively 'org-inlinetask-demote))
  18137. ((or (org-at-item-p)
  18138. (and (org-region-active-p)
  18139. (save-excursion
  18140. (goto-char (region-beginning))
  18141. (org-at-item-p))))
  18142. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  18143. (call-interactively 'org-indent-item))
  18144. (t (call-interactively 'forward-word))))
  18145. (defun org-check-for-hidden (what)
  18146. "Check if there are hidden headlines/items in the current visual line.
  18147. WHAT can be either `headlines' or `items'. If the current line is
  18148. an outline or item heading and it has a folded subtree below it,
  18149. this function returns t, nil otherwise."
  18150. (let ((re (cond
  18151. ((eq what 'headlines) org-outline-regexp-bol)
  18152. ((eq what 'items) (org-item-beginning-re))
  18153. (t (error "This should not happen"))))
  18154. beg end)
  18155. (save-excursion
  18156. (catch 'exit
  18157. (unless (org-region-active-p)
  18158. (setq beg (point-at-bol))
  18159. (beginning-of-line 2)
  18160. (while (and (not (eobp)) ;; this is like `next-line'
  18161. (get-char-property (1- (point)) 'invisible))
  18162. (beginning-of-line 2))
  18163. (setq end (point))
  18164. (goto-char beg)
  18165. (goto-char (point-at-eol))
  18166. (setq end (max end (point)))
  18167. (while (re-search-forward re end t)
  18168. (if (get-char-property (match-beginning 0) 'invisible)
  18169. (throw 'exit t))))
  18170. nil))))
  18171. (defun org-metaup (&optional arg)
  18172. "Move subtree up or move table row up.
  18173. Calls `org-move-subtree-up' or `org-table-move-row' or
  18174. `org-move-item-up', depending on context. See the individual commands
  18175. for more information."
  18176. (interactive "P")
  18177. (cond
  18178. ((run-hook-with-args-until-success 'org-metaup-hook))
  18179. ((org-region-active-p)
  18180. (let* ((a (min (region-beginning) (region-end)))
  18181. (b (1- (max (region-beginning) (region-end))))
  18182. (c (save-excursion (goto-char a)
  18183. (move-beginning-of-line 0)))
  18184. (d (save-excursion (goto-char a)
  18185. (move-end-of-line 0) (point))))
  18186. (transpose-regions a b c d)
  18187. (goto-char c)))
  18188. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  18189. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  18190. ((org-at-item-p) (call-interactively 'org-move-item-up))
  18191. (t (org-drag-element-backward))))
  18192. (defun org-metadown (&optional arg)
  18193. "Move subtree down or move table row down.
  18194. Calls `org-move-subtree-down' or `org-table-move-row' or
  18195. `org-move-item-down', depending on context. See the individual
  18196. commands for more information."
  18197. (interactive "P")
  18198. (cond
  18199. ((run-hook-with-args-until-success 'org-metadown-hook))
  18200. ((org-region-active-p)
  18201. (let* ((a (min (region-beginning) (region-end)))
  18202. (b (max (region-beginning) (region-end)))
  18203. (c (save-excursion (goto-char b)
  18204. (move-beginning-of-line 1)))
  18205. (d (save-excursion (goto-char b)
  18206. (move-end-of-line 1) (1+ (point)))))
  18207. (transpose-regions a b c d)
  18208. (goto-char d)))
  18209. ((org-at-table-p) (call-interactively 'org-table-move-row))
  18210. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  18211. ((org-at-item-p) (call-interactively 'org-move-item-down))
  18212. (t (org-drag-element-forward))))
  18213. (defun org-shiftup (&optional arg)
  18214. "Increase item in timestamp or increase priority of current headline.
  18215. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  18216. depending on context. See the individual commands for more information."
  18217. (interactive "P")
  18218. (cond
  18219. ((run-hook-with-args-until-success 'org-shiftup-hook))
  18220. ((and org-support-shift-select (org-region-active-p))
  18221. (org-call-for-shift-select 'previous-line))
  18222. ((org-at-timestamp-p t)
  18223. (call-interactively (if org-edit-timestamp-down-means-later
  18224. 'org-timestamp-down 'org-timestamp-up)))
  18225. ((and (not (eq org-support-shift-select 'always))
  18226. org-enable-priority-commands
  18227. (org-at-heading-p))
  18228. (call-interactively 'org-priority-up))
  18229. ((and (not org-support-shift-select) (org-at-item-p))
  18230. (call-interactively 'org-previous-item))
  18231. ((org-clocktable-try-shift 'up arg))
  18232. ((run-hook-with-args-until-success 'org-shiftup-final-hook))
  18233. (org-support-shift-select
  18234. (org-call-for-shift-select 'previous-line))
  18235. (t (org-shiftselect-error))))
  18236. (defun org-shiftdown (&optional arg)
  18237. "Decrease item in timestamp or decrease priority of current headline.
  18238. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  18239. depending on context. See the individual commands for more information."
  18240. (interactive "P")
  18241. (cond
  18242. ((run-hook-with-args-until-success 'org-shiftdown-hook))
  18243. ((and org-support-shift-select (org-region-active-p))
  18244. (org-call-for-shift-select 'next-line))
  18245. ((org-at-timestamp-p t)
  18246. (call-interactively (if org-edit-timestamp-down-means-later
  18247. 'org-timestamp-up 'org-timestamp-down)))
  18248. ((and (not (eq org-support-shift-select 'always))
  18249. org-enable-priority-commands
  18250. (org-at-heading-p))
  18251. (call-interactively 'org-priority-down))
  18252. ((and (not org-support-shift-select) (org-at-item-p))
  18253. (call-interactively 'org-next-item))
  18254. ((org-clocktable-try-shift 'down arg))
  18255. ((run-hook-with-args-until-success 'org-shiftdown-final-hook))
  18256. (org-support-shift-select
  18257. (org-call-for-shift-select 'next-line))
  18258. (t (org-shiftselect-error))))
  18259. (defun org-shiftright (&optional arg)
  18260. "Cycle the thing at point or in the current line, depending on context.
  18261. Depending on context, this does one of the following:
  18262. - switch a timestamp at point one day into the future
  18263. - on a headline, switch to the next TODO keyword.
  18264. - on an item, switch entire list to the next bullet type
  18265. - on a property line, switch to the next allowed value
  18266. - on a clocktable definition line, move time block into the future"
  18267. (interactive "P")
  18268. (cond
  18269. ((run-hook-with-args-until-success 'org-shiftright-hook))
  18270. ((and org-support-shift-select (org-region-active-p))
  18271. (org-call-for-shift-select 'forward-char))
  18272. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  18273. ((and (not (eq org-support-shift-select 'always))
  18274. (org-at-heading-p))
  18275. (let ((org-inhibit-logging
  18276. (not org-treat-S-cursor-todo-selection-as-state-change))
  18277. (org-inhibit-blocking
  18278. (not org-treat-S-cursor-todo-selection-as-state-change)))
  18279. (org-call-with-arg 'org-todo 'right)))
  18280. ((or (and org-support-shift-select
  18281. (not (eq org-support-shift-select 'always))
  18282. (org-at-item-bullet-p))
  18283. (and (not org-support-shift-select) (org-at-item-p)))
  18284. (org-call-with-arg 'org-cycle-list-bullet nil))
  18285. ((and (not (eq org-support-shift-select 'always))
  18286. (org-at-property-p))
  18287. (call-interactively 'org-property-next-allowed-value))
  18288. ((org-clocktable-try-shift 'right arg))
  18289. ((run-hook-with-args-until-success 'org-shiftright-final-hook))
  18290. (org-support-shift-select
  18291. (org-call-for-shift-select 'forward-char))
  18292. (t (org-shiftselect-error))))
  18293. (defun org-shiftleft (&optional arg)
  18294. "Cycle the thing at point or in the current line, depending on context.
  18295. Depending on context, this does one of the following:
  18296. - switch a timestamp at point one day into the past
  18297. - on a headline, switch to the previous TODO keyword.
  18298. - on an item, switch entire list to the previous bullet type
  18299. - on a property line, switch to the previous allowed value
  18300. - on a clocktable definition line, move time block into the past"
  18301. (interactive "P")
  18302. (cond
  18303. ((run-hook-with-args-until-success 'org-shiftleft-hook))
  18304. ((and org-support-shift-select (org-region-active-p))
  18305. (org-call-for-shift-select 'backward-char))
  18306. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  18307. ((and (not (eq org-support-shift-select 'always))
  18308. (org-at-heading-p))
  18309. (let ((org-inhibit-logging
  18310. (not org-treat-S-cursor-todo-selection-as-state-change))
  18311. (org-inhibit-blocking
  18312. (not org-treat-S-cursor-todo-selection-as-state-change)))
  18313. (org-call-with-arg 'org-todo 'left)))
  18314. ((or (and org-support-shift-select
  18315. (not (eq org-support-shift-select 'always))
  18316. (org-at-item-bullet-p))
  18317. (and (not org-support-shift-select) (org-at-item-p)))
  18318. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  18319. ((and (not (eq org-support-shift-select 'always))
  18320. (org-at-property-p))
  18321. (call-interactively 'org-property-previous-allowed-value))
  18322. ((org-clocktable-try-shift 'left arg))
  18323. ((run-hook-with-args-until-success 'org-shiftleft-final-hook))
  18324. (org-support-shift-select
  18325. (org-call-for-shift-select 'backward-char))
  18326. (t (org-shiftselect-error))))
  18327. (defun org-shiftcontrolright ()
  18328. "Switch to next TODO set."
  18329. (interactive)
  18330. (cond
  18331. ((and org-support-shift-select (org-region-active-p))
  18332. (org-call-for-shift-select 'forward-word))
  18333. ((and (not (eq org-support-shift-select 'always))
  18334. (org-at-heading-p))
  18335. (org-call-with-arg 'org-todo 'nextset))
  18336. (org-support-shift-select
  18337. (org-call-for-shift-select 'forward-word))
  18338. (t (org-shiftselect-error))))
  18339. (defun org-shiftcontrolleft ()
  18340. "Switch to previous TODO set."
  18341. (interactive)
  18342. (cond
  18343. ((and org-support-shift-select (org-region-active-p))
  18344. (org-call-for-shift-select 'backward-word))
  18345. ((and (not (eq org-support-shift-select 'always))
  18346. (org-at-heading-p))
  18347. (org-call-with-arg 'org-todo 'previousset))
  18348. (org-support-shift-select
  18349. (org-call-for-shift-select 'backward-word))
  18350. (t (org-shiftselect-error))))
  18351. (defun org-shiftcontrolup (&optional n)
  18352. "Change timestamps synchronously up in CLOCK log lines.
  18353. Optional argument N tells to change by that many units."
  18354. (interactive "P")
  18355. (cond ((and (not org-support-shift-select)
  18356. (org-at-clock-log-p)
  18357. (org-at-timestamp-p t))
  18358. (org-clock-timestamps-up n))
  18359. (t (org-shiftselect-error))))
  18360. (defun org-shiftcontroldown (&optional n)
  18361. "Change timestamps synchronously down in CLOCK log lines.
  18362. Optional argument N tells to change by that many units."
  18363. (interactive "P")
  18364. (cond ((and (not org-support-shift-select)
  18365. (org-at-clock-log-p)
  18366. (org-at-timestamp-p t))
  18367. (org-clock-timestamps-down n))
  18368. (t (org-shiftselect-error))))
  18369. (defun org-ctrl-c-ret ()
  18370. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  18371. (interactive)
  18372. (cond
  18373. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  18374. (t (call-interactively 'org-insert-heading))))
  18375. (defun org-find-visible ()
  18376. (let ((s (point)))
  18377. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  18378. (get-char-property s 'invisible)))
  18379. s))
  18380. (defun org-find-invisible ()
  18381. (let ((s (point)))
  18382. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  18383. (not (get-char-property s 'invisible))))
  18384. s))
  18385. (defun org-copy-visible (beg end)
  18386. "Copy the visible parts of the region."
  18387. (interactive "r")
  18388. (let (snippets s)
  18389. (save-excursion
  18390. (save-restriction
  18391. (narrow-to-region beg end)
  18392. (setq s (goto-char (point-min)))
  18393. (while (not (= (point) (point-max)))
  18394. (goto-char (org-find-invisible))
  18395. (push (buffer-substring s (point)) snippets)
  18396. (setq s (goto-char (org-find-visible))))))
  18397. (kill-new (apply 'concat (nreverse snippets)))))
  18398. (defun org-copy-special ()
  18399. "Copy region in table or copy current subtree.
  18400. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  18401. See the individual commands for more information."
  18402. (interactive)
  18403. (call-interactively
  18404. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  18405. (defun org-cut-special ()
  18406. "Cut region in table or cut current subtree.
  18407. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  18408. See the individual commands for more information."
  18409. (interactive)
  18410. (call-interactively
  18411. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  18412. (defun org-paste-special (arg)
  18413. "Paste rectangular region into table, or past subtree relative to level.
  18414. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  18415. See the individual commands for more information."
  18416. (interactive "P")
  18417. (if (org-at-table-p)
  18418. (org-table-paste-rectangle)
  18419. (org-paste-subtree arg)))
  18420. (defsubst org-in-fixed-width-region-p ()
  18421. "Is point in a fixed-width region?"
  18422. (save-match-data
  18423. (eq 'fixed-width (org-element-type (org-element-at-point)))))
  18424. (defun org-edit-special (&optional arg)
  18425. "Call a special editor for the element at point.
  18426. When at a table, call the formula editor with `org-table-edit-formulas'.
  18427. When in a source code block, call `org-edit-src-code'.
  18428. When in a fixed-width region, call `org-edit-fixed-width-region'.
  18429. When at an #+INCLUDE keyword, visit the included file.
  18430. On a link, call `ffap' to visit the link at point.
  18431. Otherwise, return a user error."
  18432. (interactive "P")
  18433. (let ((element (org-element-at-point)))
  18434. (assert (not buffer-read-only) nil
  18435. "Buffer is read-only: %s" (buffer-name))
  18436. (case (org-element-type element)
  18437. (src-block
  18438. (if (not arg) (org-edit-src-code)
  18439. (let* ((info (org-babel-get-src-block-info))
  18440. (lang (nth 0 info))
  18441. (params (nth 2 info))
  18442. (session (cdr (assq :session params))))
  18443. (if (not session) (org-edit-src-code)
  18444. ;; At a src-block with a session and function called with
  18445. ;; an ARG: switch to the buffer related to the inferior
  18446. ;; process.
  18447. (switch-to-buffer
  18448. (funcall (intern (concat "org-babel-prep-session:" lang))
  18449. session params))))))
  18450. (keyword
  18451. (if (member (org-element-property :key element) '("INCLUDE" "SETUPFILE"))
  18452. (find-file
  18453. (org-remove-double-quotes
  18454. (car (org-split-string (org-element-property :value element)))))
  18455. (user-error "No special environment to edit here")))
  18456. (table
  18457. (if (eq (org-element-property :type element) 'table.el)
  18458. (org-edit-src-code)
  18459. (call-interactively 'org-table-edit-formulas)))
  18460. ;; Only Org tables contain `table-row' type elements.
  18461. (table-row (call-interactively 'org-table-edit-formulas))
  18462. ((example-block export-block) (org-edit-src-code))
  18463. (fixed-width (org-edit-fixed-width-region))
  18464. (otherwise
  18465. ;; No notable element at point. Though, we may be at a link,
  18466. ;; which is an object. Thus, scan deeper.
  18467. (if (eq (org-element-type (org-element-context element)) 'link)
  18468. (call-interactively 'ffap)
  18469. (user-error "No special environment to edit here"))))))
  18470. (defvar org-table-coordinate-overlays) ; defined in org-table.el
  18471. (defun org-ctrl-c-ctrl-c (&optional arg)
  18472. "Set tags in headline, or update according to changed information at point.
  18473. This command does many different things, depending on context:
  18474. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  18475. this is what we do.
  18476. - If the cursor is on a statistics cookie, update it.
  18477. - If the cursor is in a headline, prompt for tags and insert them
  18478. into the current line, aligned to `org-tags-column'. When called
  18479. with prefix arg, realign all tags in the current buffer.
  18480. - If the cursor is in one of the special #+KEYWORD lines, this
  18481. triggers scanning the buffer for these lines and updating the
  18482. information.
  18483. - If the cursor is inside a table, realign the table. This command
  18484. works even if the automatic table editor has been turned off.
  18485. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  18486. the entire table.
  18487. - If the cursor is at a footnote reference or definition, jump to
  18488. the corresponding definition or references, respectively.
  18489. - If the cursor is a the beginning of a dynamic block, update it.
  18490. - If the current buffer is a capture buffer, close note and file it.
  18491. - If the cursor is on a <<<target>>>, update radio targets and
  18492. corresponding links in this buffer.
  18493. - If the cursor is on a numbered item in a plain list, renumber the
  18494. ordered list.
  18495. - If the cursor is on a checkbox, toggle it.
  18496. - If the cursor is on a code block, evaluate it. The variable
  18497. `org-confirm-babel-evaluate' can be used to control prompting
  18498. before code block evaluation, by default every code block
  18499. evaluation requires confirmation. Code block evaluation can be
  18500. inhibited by setting `org-babel-no-eval-on-ctrl-c-ctrl-c'."
  18501. (interactive "P")
  18502. (cond
  18503. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  18504. org-occur-highlights
  18505. org-latex-fragment-image-overlays)
  18506. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  18507. (org-remove-occur-highlights)
  18508. (org-remove-latex-fragment-image-overlays)
  18509. (message "Temporary highlights/overlays removed from current buffer"))
  18510. ((and (local-variable-p 'org-finish-function (current-buffer))
  18511. (fboundp org-finish-function))
  18512. (funcall org-finish-function))
  18513. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  18514. (t
  18515. (let* ((context (org-element-context)) (type (org-element-type context)))
  18516. ;; Test if point is within a blank line.
  18517. (if (save-excursion (beginning-of-line) (looking-at "[ \t]*$"))
  18518. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  18519. (user-error "C-c C-c can do nothing useful at this location"))
  18520. ;; For convenience: at the first line of a paragraph on the
  18521. ;; same line as an item, apply function on that item instead.
  18522. (when (eq type 'paragraph)
  18523. (let ((parent (org-element-property :parent context)))
  18524. (when (and (eq (org-element-type parent) 'item)
  18525. (= (point-at-bol) (org-element-property :begin parent)))
  18526. (setq context parent type 'item))))
  18527. ;; Act according to type of element or object at point.
  18528. (case type
  18529. (clock (org-clock-update-time-maybe))
  18530. (dynamic-block
  18531. (save-excursion
  18532. (goto-char (org-element-property :post-affiliated context))
  18533. (org-update-dblock)))
  18534. (footnote-definition
  18535. (goto-char (org-element-property :post-affiliated context))
  18536. (call-interactively 'org-footnote-action))
  18537. (footnote-reference (call-interactively 'org-footnote-action))
  18538. ((headline inlinetask)
  18539. (save-excursion (goto-char (org-element-property :begin context))
  18540. (call-interactively 'org-set-tags)))
  18541. (item
  18542. ;; At an item: a double C-u set checkbox to "[-]"
  18543. ;; unconditionally, whereas a single one will toggle its
  18544. ;; presence. Without an universal argument, if the item
  18545. ;; has a checkbox, toggle it. Otherwise repair the list.
  18546. (let* ((box (org-element-property :checkbox context))
  18547. (struct (org-element-property :structure context))
  18548. (old-struct (copy-tree struct))
  18549. (parents (org-list-parents-alist struct))
  18550. (prevs (org-list-prevs-alist struct))
  18551. (orderedp (org-not-nil (org-entry-get nil "ORDERED"))))
  18552. (org-list-set-checkbox
  18553. (org-element-property :begin context) struct
  18554. (cond ((equal arg '(16)) "[-]")
  18555. ((and (not box) (equal arg '(4))) "[ ]")
  18556. ((or (not box) (equal arg '(4))) nil)
  18557. ((eq box 'on) "[ ]")
  18558. (t "[X]")))
  18559. ;; Mimic `org-list-write-struct' but with grabbing
  18560. ;; a return value from `org-list-struct-fix-box'.
  18561. (org-list-struct-fix-ind struct parents 2)
  18562. (org-list-struct-fix-item-end struct)
  18563. (org-list-struct-fix-bul struct prevs)
  18564. (org-list-struct-fix-ind struct parents)
  18565. (let ((block-item
  18566. (org-list-struct-fix-box struct parents prevs orderedp)))
  18567. (if (and box (equal struct old-struct))
  18568. (if (equal arg '(16))
  18569. (message "Checkboxes already reset")
  18570. (user-error "Cannot toggle this checkbox: %s"
  18571. (if (eq box 'on)
  18572. "all subitems checked"
  18573. "unchecked subitems")))
  18574. (org-list-struct-apply-struct struct old-struct)
  18575. (org-update-checkbox-count-maybe))
  18576. (when block-item
  18577. (message "Checkboxes were removed due to empty box at line %d"
  18578. (org-current-line block-item))))))
  18579. (keyword
  18580. (let ((org-inhibit-startup-visibility-stuff t)
  18581. (org-startup-align-all-tables nil))
  18582. (when (boundp 'org-table-coordinate-overlays)
  18583. (mapc 'delete-overlay org-table-coordinate-overlays)
  18584. (setq org-table-coordinate-overlays nil))
  18585. (org-save-outline-visibility 'use-markers (org-mode-restart)))
  18586. (message "Local setup has been refreshed"))
  18587. (plain-list
  18588. ;; At a plain list, with a double C-u argument, set
  18589. ;; checkboxes of each item to "[-]", whereas a single one
  18590. ;; will toggle their presence according to the state of the
  18591. ;; first item in the list. Without an argument, repair the
  18592. ;; list.
  18593. (let* ((begin (org-element-property :contents-begin context))
  18594. (beginm (move-marker (make-marker) begin))
  18595. (struct (org-element-property :structure context))
  18596. (old-struct (copy-tree struct))
  18597. (first-box (save-excursion
  18598. (goto-char begin)
  18599. (looking-at org-list-full-item-re)
  18600. (match-string-no-properties 3)))
  18601. (new-box (cond ((equal arg '(16)) "[-]")
  18602. ((equal arg '(4)) (unless first-box "[ ]"))
  18603. ((equal first-box "[X]") "[ ]")
  18604. (t "[X]"))))
  18605. (cond
  18606. (arg
  18607. (mapc (lambda (pos) (org-list-set-checkbox pos struct new-box))
  18608. (org-list-get-all-items
  18609. begin struct (org-list-prevs-alist struct))))
  18610. ((and first-box (eq (point) begin))
  18611. ;; For convenience, when point is at bol on the first
  18612. ;; item of the list and no argument is provided, simply
  18613. ;; toggle checkbox of that item, if any.
  18614. (org-list-set-checkbox begin struct new-box)))
  18615. (org-list-write-struct
  18616. struct (org-list-parents-alist struct) old-struct)
  18617. (org-update-checkbox-count-maybe)
  18618. (save-excursion (goto-char beginm) (org-list-send-list 'maybe))))
  18619. ((property-drawer node-property)
  18620. (call-interactively 'org-property-action))
  18621. ((radio-target target)
  18622. (call-interactively 'org-update-radio-target-regexp))
  18623. (statistics-cookie
  18624. (call-interactively 'org-update-statistics-cookies))
  18625. ((table table-cell table-row)
  18626. ;; At a table, recalculate every field and align it. Also
  18627. ;; send the table if necessary. If the table has
  18628. ;; a `table.el' type, just give up. At a table row or
  18629. ;; cell, maybe recalculate line but always align table.
  18630. (if (eq (org-element-property :type context) 'table.el)
  18631. (message "Use C-c ' to edit table.el tables")
  18632. (let ((org-enable-table-editor t))
  18633. (if (or (eq type 'table)
  18634. ;; Check if point is at a TBLFM line.
  18635. (and (eq type 'table-row)
  18636. (= (point) (org-element-property :end context))))
  18637. (save-excursion
  18638. (if (org-at-TBLFM-p)
  18639. (progn (require 'org-table)
  18640. (org-table-calc-current-TBLFM))
  18641. (goto-char (org-element-property :contents-begin context))
  18642. (org-call-with-arg 'org-table-recalculate (or arg t))
  18643. (orgtbl-send-table 'maybe)))
  18644. (org-table-maybe-eval-formula)
  18645. (cond (arg (call-interactively 'org-table-recalculate))
  18646. ((org-table-maybe-recalculate-line))
  18647. (t (org-table-align)))))))
  18648. (timestamp (org-timestamp-change 0 'day))
  18649. (otherwise
  18650. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  18651. (user-error
  18652. "C-c C-c can do nothing useful at this location")))))))))
  18653. (defun org-mode-restart ()
  18654. "Restart Org-mode, to scan again for special lines.
  18655. Also updates the keyword regular expressions."
  18656. (interactive)
  18657. (org-mode)
  18658. (message "Org-mode restarted"))
  18659. (defun org-kill-note-or-show-branches ()
  18660. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  18661. (interactive)
  18662. (if (not org-finish-function)
  18663. (progn
  18664. (hide-subtree)
  18665. (call-interactively 'show-branches))
  18666. (let ((org-note-abort t))
  18667. (funcall org-finish-function))))
  18668. (defun org-open-line (n)
  18669. "Insert a new row in tables, call `open-line' elsewhere."
  18670. (interactive "*p")
  18671. (if (org-at-table-p)
  18672. (org-table-insert-row)
  18673. (open-line n)))
  18674. (defun org-return (&optional indent)
  18675. "Goto next table row or insert a newline.
  18676. Calls `org-table-next-row' or `newline', depending on context.
  18677. See the individual commands for more information."
  18678. (interactive)
  18679. (let (org-ts-what)
  18680. (cond
  18681. ((or (bobp) (org-in-src-block-p))
  18682. (if indent (newline-and-indent) (newline)))
  18683. ((org-at-table-p)
  18684. (org-table-justify-field-maybe)
  18685. (call-interactively 'org-table-next-row))
  18686. ;; when `newline-and-indent' is called within a list, make sure
  18687. ;; text moved stays inside the item.
  18688. ((and (org-in-item-p) indent)
  18689. (if (and (org-at-item-p) (>= (point) (match-end 0)))
  18690. (progn
  18691. (save-match-data (newline))
  18692. (org-indent-line-to (length (match-string 0))))
  18693. (let ((ind (org-get-indentation)))
  18694. (newline)
  18695. (if (org-looking-back org-list-end-re)
  18696. (org-indent-line)
  18697. (org-indent-line-to ind)))))
  18698. ((and org-return-follows-link
  18699. (org-at-timestamp-p t)
  18700. (not (eq org-ts-what 'after)))
  18701. (org-follow-timestamp-link))
  18702. ((and org-return-follows-link
  18703. (let ((tprop (get-text-property (point) 'face)))
  18704. (or (eq tprop 'org-link)
  18705. (and (listp tprop) (memq 'org-link tprop)))))
  18706. (call-interactively 'org-open-at-point))
  18707. ((and (org-at-heading-p)
  18708. (looking-at
  18709. (org-re "\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$")))
  18710. (org-show-entry)
  18711. (end-of-line 1)
  18712. (newline))
  18713. (t (if indent (newline-and-indent) (newline))))))
  18714. (defun org-return-indent ()
  18715. "Goto next table row or insert a newline and indent.
  18716. Calls `org-table-next-row' or `newline-and-indent', depending on
  18717. context. See the individual commands for more information."
  18718. (interactive)
  18719. (org-return t))
  18720. (defun org-ctrl-c-star ()
  18721. "Compute table, or change heading status of lines.
  18722. Calls `org-table-recalculate' or `org-toggle-heading',
  18723. depending on context."
  18724. (interactive)
  18725. (cond
  18726. ((org-at-table-p)
  18727. (call-interactively 'org-table-recalculate))
  18728. (t
  18729. ;; Convert all lines in region to list items
  18730. (call-interactively 'org-toggle-heading))))
  18731. (defun org-ctrl-c-minus ()
  18732. "Insert separator line in table or modify bullet status of line.
  18733. Also turns a plain line or a region of lines into list items.
  18734. Calls `org-table-insert-hline', `org-toggle-item', or
  18735. `org-cycle-list-bullet', depending on context."
  18736. (interactive)
  18737. (cond
  18738. ((org-at-table-p)
  18739. (call-interactively 'org-table-insert-hline))
  18740. ((org-region-active-p)
  18741. (call-interactively 'org-toggle-item))
  18742. ((org-in-item-p)
  18743. (call-interactively 'org-cycle-list-bullet))
  18744. (t
  18745. (call-interactively 'org-toggle-item))))
  18746. (defun org-toggle-item (arg)
  18747. "Convert headings or normal lines to items, items to normal lines.
  18748. If there is no active region, only the current line is considered.
  18749. If the first non blank line in the region is a headline, convert
  18750. all headlines to items, shifting text accordingly.
  18751. If it is an item, convert all items to normal lines.
  18752. If it is normal text, change region into a list of items.
  18753. With a prefix argument ARG, change the region in a single item."
  18754. (interactive "P")
  18755. (let ((shift-text
  18756. (function
  18757. ;; Shift text in current section to IND, from point to END.
  18758. ;; The function leaves point to END line.
  18759. (lambda (ind end)
  18760. (let ((min-i 1000) (end (copy-marker end)))
  18761. ;; First determine the minimum indentation (MIN-I) of
  18762. ;; the text.
  18763. (save-excursion
  18764. (catch 'exit
  18765. (while (< (point) end)
  18766. (let ((i (org-get-indentation)))
  18767. (cond
  18768. ;; Skip blank lines and inline tasks.
  18769. ((looking-at "^[ \t]*$"))
  18770. ((looking-at org-outline-regexp-bol))
  18771. ;; We can't find less than 0 indentation.
  18772. ((zerop i) (throw 'exit (setq min-i 0)))
  18773. ((< i min-i) (setq min-i i))))
  18774. (forward-line))))
  18775. ;; Then indent each line so that a line indented to
  18776. ;; MIN-I becomes indented to IND. Ignore blank lines
  18777. ;; and inline tasks in the process.
  18778. (let ((delta (- ind min-i)))
  18779. (while (< (point) end)
  18780. (unless (or (looking-at "^[ \t]*$")
  18781. (looking-at org-outline-regexp-bol))
  18782. (org-indent-line-to (+ (org-get-indentation) delta)))
  18783. (forward-line)))))))
  18784. (skip-blanks
  18785. (function
  18786. ;; Return beginning of first non-blank line, starting from
  18787. ;; line at POS.
  18788. (lambda (pos)
  18789. (save-excursion
  18790. (goto-char pos)
  18791. (skip-chars-forward " \r\t\n")
  18792. (point-at-bol)))))
  18793. beg end)
  18794. ;; Determine boundaries of changes.
  18795. (if (org-region-active-p)
  18796. (setq beg (funcall skip-blanks (region-beginning))
  18797. end (copy-marker (region-end)))
  18798. (setq beg (funcall skip-blanks (point-at-bol))
  18799. end (copy-marker (point-at-eol))))
  18800. ;; Depending on the starting line, choose an action on the text
  18801. ;; between BEG and END.
  18802. (org-with-limited-levels
  18803. (save-excursion
  18804. (goto-char beg)
  18805. (cond
  18806. ;; Case 1. Start at an item: de-itemize. Note that it only
  18807. ;; happens when a region is active: `org-ctrl-c-minus'
  18808. ;; would call `org-cycle-list-bullet' otherwise.
  18809. ((org-at-item-p)
  18810. (while (< (point) end)
  18811. (when (org-at-item-p)
  18812. (skip-chars-forward " \t")
  18813. (delete-region (point) (match-end 0)))
  18814. (forward-line)))
  18815. ;; Case 2. Start at an heading: convert to items.
  18816. ((org-at-heading-p)
  18817. (let* ((bul (org-list-bullet-string "-"))
  18818. (bul-len (length bul))
  18819. ;; Indentation of the first heading. It should be
  18820. ;; relative to the indentation of its parent, if any.
  18821. (start-ind (save-excursion
  18822. (cond
  18823. ((not org-adapt-indentation) 0)
  18824. ((not (outline-previous-heading)) 0)
  18825. (t (length (match-string 0))))))
  18826. ;; Level of first heading. Further headings will be
  18827. ;; compared to it to determine hierarchy in the list.
  18828. (ref-level (org-reduced-level (org-outline-level))))
  18829. (while (< (point) end)
  18830. (let* ((level (org-reduced-level (org-outline-level)))
  18831. (delta (max 0 (- level ref-level))))
  18832. ;; If current headline is less indented than the first
  18833. ;; one, set it as reference, in order to preserve
  18834. ;; subtrees.
  18835. (when (< level ref-level) (setq ref-level level))
  18836. (replace-match bul t t)
  18837. (org-indent-line-to (+ start-ind (* delta bul-len)))
  18838. ;; Ensure all text down to END (or SECTION-END) belongs
  18839. ;; to the newly created item.
  18840. (let ((section-end (save-excursion
  18841. (or (outline-next-heading) (point)))))
  18842. (forward-line)
  18843. (funcall shift-text
  18844. (+ start-ind (* (1+ delta) bul-len))
  18845. (min end section-end)))))))
  18846. ;; Case 3. Normal line with ARG: make the first line of region
  18847. ;; an item, and shift indentation of others lines to
  18848. ;; set them as item's body.
  18849. (arg (let* ((bul (org-list-bullet-string "-"))
  18850. (bul-len (length bul))
  18851. (ref-ind (org-get-indentation)))
  18852. (skip-chars-forward " \t")
  18853. (insert bul)
  18854. (forward-line)
  18855. (while (< (point) end)
  18856. ;; Ensure that lines less indented than first one
  18857. ;; still get included in item body.
  18858. (funcall shift-text
  18859. (+ ref-ind bul-len)
  18860. (min end (save-excursion (or (outline-next-heading)
  18861. (point)))))
  18862. (forward-line))))
  18863. ;; Case 4. Normal line without ARG: turn each non-item line
  18864. ;; into an item.
  18865. (t
  18866. (while (< (point) end)
  18867. (unless (or (org-at-heading-p) (org-at-item-p))
  18868. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  18869. (replace-match
  18870. (concat "\\1" (org-list-bullet-string "-") "\\2"))))
  18871. (forward-line))))))))
  18872. (defun org-toggle-heading (&optional nstars)
  18873. "Convert headings to normal text, or items or text to headings.
  18874. If there is no active region, only convert the current line.
  18875. With a \\[universal-argument] prefix, convert the whole list at
  18876. point into heading.
  18877. In a region:
  18878. - If the first non blank line is a headline, remove the stars
  18879. from all headlines in the region.
  18880. - If it is a normal line, turn each and every normal line (i.e.,
  18881. not an heading or an item) in the region into headings. If you
  18882. want to convert only the first line of this region, use one
  18883. universal prefix argument.
  18884. - If it is a plain list item, turn all plain list items into headings.
  18885. When converting a line into a heading, the number of stars is chosen
  18886. such that the lines become children of the current entry. However,
  18887. when a numeric prefix argument is given, its value determines the
  18888. number of stars to add."
  18889. (interactive "P")
  18890. (let ((skip-blanks
  18891. (function
  18892. ;; Return beginning of first non-blank line, starting from
  18893. ;; line at POS.
  18894. (lambda (pos)
  18895. (save-excursion
  18896. (goto-char pos)
  18897. (while (org-at-comment-p) (forward-line))
  18898. (skip-chars-forward " \r\t\n")
  18899. (point-at-bol)))))
  18900. beg end toggled)
  18901. ;; Determine boundaries of changes. If a universal prefix has
  18902. ;; been given, put the list in a region. If region ends at a bol,
  18903. ;; do not consider the last line to be in the region.
  18904. (when (and current-prefix-arg (org-at-item-p))
  18905. (if (listp current-prefix-arg) (setq current-prefix-arg 1))
  18906. (org-mark-element))
  18907. (if (org-region-active-p)
  18908. (setq beg (funcall skip-blanks (region-beginning))
  18909. end (copy-marker (save-excursion
  18910. (goto-char (region-end))
  18911. (if (bolp) (point) (point-at-eol)))))
  18912. (setq beg (funcall skip-blanks (point-at-bol))
  18913. end (copy-marker (point-at-eol))))
  18914. ;; Ensure inline tasks don't count as headings.
  18915. (org-with-limited-levels
  18916. (save-excursion
  18917. (goto-char beg)
  18918. (cond
  18919. ;; Case 1. Started at an heading: de-star headings.
  18920. ((org-at-heading-p)
  18921. (while (< (point) end)
  18922. (when (org-at-heading-p t)
  18923. (looking-at org-outline-regexp) (replace-match "")
  18924. (setq toggled t))
  18925. (forward-line)))
  18926. ;; Case 2. Started at an item: change items into headlines.
  18927. ;; One star will be added by `org-list-to-subtree'.
  18928. ((org-at-item-p)
  18929. (let* ((stars (make-string
  18930. ;; subtract the star that will be added again by
  18931. ;; `org-list-to-subtree'
  18932. (if (numberp nstars) (1- nstars)
  18933. (or (org-current-level) 0))
  18934. ?*))
  18935. (add-stars
  18936. (cond (nstars "") ; stars from prefix only
  18937. ((equal stars "") "") ; before first heading
  18938. (org-odd-levels-only "*") ; inside heading, odd
  18939. (t "")))) ; inside heading, oddeven
  18940. (while (< (point) end)
  18941. (when (org-at-item-p)
  18942. ;; Pay attention to cases when region ends before list.
  18943. (let* ((struct (org-list-struct))
  18944. (list-end (min (org-list-get-bottom-point struct) (1+ end))))
  18945. (save-restriction
  18946. (narrow-to-region (point) list-end)
  18947. (insert
  18948. (org-list-to-subtree
  18949. (org-list-parse-list t)
  18950. '(:istart (concat stars add-stars (funcall get-stars depth))
  18951. :icount (concat stars add-stars (funcall get-stars depth)))))))
  18952. (setq toggled t))
  18953. (forward-line))))
  18954. ;; Case 3. Started at normal text: make every line an heading,
  18955. ;; skipping headlines and items.
  18956. (t (let* ((stars
  18957. (make-string
  18958. (if (numberp nstars) nstars (or (org-current-level) 0)) ?*))
  18959. (add-stars
  18960. (cond (nstars "") ; stars from prefix only
  18961. ((equal stars "") "*") ; before first heading
  18962. (org-odd-levels-only "**") ; inside heading, odd
  18963. (t "*"))) ; inside heading, oddeven
  18964. (rpl (concat stars add-stars " "))
  18965. (lend (if (listp nstars) (save-excursion (end-of-line) (point)))))
  18966. (while (< (point) (if (equal nstars '(4)) lend end))
  18967. (when (and (not (or (org-at-heading-p) (org-at-item-p) (org-at-comment-p)))
  18968. (looking-at "\\([ \t]*\\)\\(\\S-\\)"))
  18969. (replace-match (concat rpl (match-string 2))) (setq toggled t))
  18970. (forward-line)))))))
  18971. (unless toggled (message "Cannot toggle heading from here"))))
  18972. (defun org-meta-return (&optional arg)
  18973. "Insert a new heading or wrap a region in a table.
  18974. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  18975. See the individual commands for more information."
  18976. (interactive "P")
  18977. (org-check-before-invisible-edit 'insert)
  18978. (cond
  18979. ((run-hook-with-args-until-success 'org-metareturn-hook))
  18980. ((or (org-at-drawer-p) (org-in-drawer-p) (org-at-property-p))
  18981. (newline-and-indent))
  18982. ((org-at-table-p)
  18983. (call-interactively 'org-table-wrap-region))
  18984. (t (call-interactively 'org-insert-heading))))
  18985. ;;; Menu entries
  18986. (defsubst org-in-subtree-not-table-p ()
  18987. "Are we in a subtree and not in a table?"
  18988. (and (not (org-before-first-heading-p))
  18989. (not (org-at-table-p))))
  18990. ;; Define the Org-mode menus
  18991. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  18992. '("Tbl"
  18993. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  18994. ["Next Field" org-cycle (org-at-table-p)]
  18995. ["Previous Field" org-shifttab (org-at-table-p)]
  18996. ["Next Row" org-return (org-at-table-p)]
  18997. "--"
  18998. ["Blank Field" org-table-blank-field (org-at-table-p)]
  18999. ["Edit Field" org-table-edit-field (org-at-table-p)]
  19000. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  19001. "--"
  19002. ("Column"
  19003. ["Move Column Left" org-metaleft (org-at-table-p)]
  19004. ["Move Column Right" org-metaright (org-at-table-p)]
  19005. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  19006. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  19007. ("Row"
  19008. ["Move Row Up" org-metaup (org-at-table-p)]
  19009. ["Move Row Down" org-metadown (org-at-table-p)]
  19010. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  19011. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  19012. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  19013. "--"
  19014. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  19015. ("Rectangle"
  19016. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  19017. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  19018. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  19019. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  19020. "--"
  19021. ("Calculate"
  19022. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  19023. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  19024. ["Edit Formulas" org-edit-special (org-at-table-p)]
  19025. "--"
  19026. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  19027. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  19028. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  19029. "--"
  19030. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  19031. "--"
  19032. ["Sum Column/Rectangle" org-table-sum
  19033. (or (org-at-table-p) (org-region-active-p))]
  19034. ["Which Column?" org-table-current-column (org-at-table-p)])
  19035. ["Debug Formulas"
  19036. org-table-toggle-formula-debugger
  19037. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  19038. ["Show Col/Row Numbers"
  19039. org-table-toggle-coordinate-overlays
  19040. :style toggle
  19041. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  19042. "--"
  19043. ["Create" org-table-create (and (not (org-at-table-p))
  19044. org-enable-table-editor)]
  19045. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  19046. ["Import from File" org-table-import (not (org-at-table-p))]
  19047. ["Export to File" org-table-export (org-at-table-p)]
  19048. "--"
  19049. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  19050. (easy-menu-define org-org-menu org-mode-map "Org menu"
  19051. '("Org"
  19052. ("Show/Hide"
  19053. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  19054. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  19055. ["Sparse Tree..." org-sparse-tree t]
  19056. ["Reveal Context" org-reveal t]
  19057. ["Show All" show-all t]
  19058. "--"
  19059. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  19060. "--"
  19061. ["New Heading" org-insert-heading t]
  19062. ("Navigate Headings"
  19063. ["Up" outline-up-heading t]
  19064. ["Next" outline-next-visible-heading t]
  19065. ["Previous" outline-previous-visible-heading t]
  19066. ["Next Same Level" outline-forward-same-level t]
  19067. ["Previous Same Level" outline-backward-same-level t]
  19068. "--"
  19069. ["Jump" org-goto t])
  19070. ("Edit Structure"
  19071. ["Refile Subtree" org-refile (org-in-subtree-not-table-p)]
  19072. "--"
  19073. ["Move Subtree Up" org-shiftmetaup (org-in-subtree-not-table-p)]
  19074. ["Move Subtree Down" org-shiftmetadown (org-in-subtree-not-table-p)]
  19075. "--"
  19076. ["Copy Subtree" org-copy-special (org-in-subtree-not-table-p)]
  19077. ["Cut Subtree" org-cut-special (org-in-subtree-not-table-p)]
  19078. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  19079. "--"
  19080. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  19081. "--"
  19082. ["Copy visible text" org-copy-visible t]
  19083. "--"
  19084. ["Promote Heading" org-metaleft (org-in-subtree-not-table-p)]
  19085. ["Promote Subtree" org-shiftmetaleft (org-in-subtree-not-table-p)]
  19086. ["Demote Heading" org-metaright (org-in-subtree-not-table-p)]
  19087. ["Demote Subtree" org-shiftmetaright (org-in-subtree-not-table-p)]
  19088. "--"
  19089. ["Sort Region/Children" org-sort t]
  19090. "--"
  19091. ["Convert to odd levels" org-convert-to-odd-levels t]
  19092. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  19093. ("Editing"
  19094. ["Emphasis..." org-emphasize t]
  19095. ["Edit Source Example" org-edit-special t]
  19096. "--"
  19097. ["Footnote new/jump" org-footnote-action t]
  19098. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  19099. ("Archive"
  19100. ["Archive (default method)" org-archive-subtree-default (org-in-subtree-not-table-p)]
  19101. "--"
  19102. ["Move Subtree to Archive file" org-advertized-archive-subtree (org-in-subtree-not-table-p)]
  19103. ["Toggle ARCHIVE tag" org-toggle-archive-tag (org-in-subtree-not-table-p)]
  19104. ["Move subtree to Archive sibling" org-archive-to-archive-sibling (org-in-subtree-not-table-p)]
  19105. )
  19106. "--"
  19107. ("Hyperlinks"
  19108. ["Store Link (Global)" org-store-link t]
  19109. ["Find existing link to here" org-occur-link-in-agenda-files t]
  19110. ["Insert Link" org-insert-link t]
  19111. ["Follow Link" org-open-at-point t]
  19112. "--"
  19113. ["Next link" org-next-link t]
  19114. ["Previous link" org-previous-link t]
  19115. "--"
  19116. ["Descriptive Links"
  19117. org-toggle-link-display
  19118. :style radio
  19119. :selected org-descriptive-links
  19120. ]
  19121. ["Literal Links"
  19122. org-toggle-link-display
  19123. :style radio
  19124. :selected (not org-descriptive-links)])
  19125. "--"
  19126. ("TODO Lists"
  19127. ["TODO/DONE/-" org-todo t]
  19128. ("Select keyword"
  19129. ["Next keyword" org-shiftright (org-at-heading-p)]
  19130. ["Previous keyword" org-shiftleft (org-at-heading-p)]
  19131. ["Complete Keyword" pcomplete (assq :todo-keyword (org-context))]
  19132. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))]
  19133. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))])
  19134. ["Show TODO Tree" org-show-todo-tree :active t :keys "C-c / t"]
  19135. ["Global TODO list" org-todo-list :active t :keys "C-c a t"]
  19136. "--"
  19137. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  19138. :selected org-enforce-todo-dependencies :style toggle :active t]
  19139. "Settings for tree at point"
  19140. ["Do Children sequentially" org-toggle-ordered-property :style radio
  19141. :selected (org-entry-get nil "ORDERED")
  19142. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  19143. ["Do Children parallel" org-toggle-ordered-property :style radio
  19144. :selected (not (org-entry-get nil "ORDERED"))
  19145. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  19146. "--"
  19147. ["Set Priority" org-priority t]
  19148. ["Priority Up" org-shiftup t]
  19149. ["Priority Down" org-shiftdown t]
  19150. "--"
  19151. ["Get news from all feeds" org-feed-update-all t]
  19152. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  19153. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  19154. ("TAGS and Properties"
  19155. ["Set Tags" org-set-tags-command (not (org-before-first-heading-p))]
  19156. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  19157. "--"
  19158. ["Set property" org-set-property (not (org-before-first-heading-p))]
  19159. ["Column view of properties" org-columns t]
  19160. ["Insert Column View DBlock" org-insert-columns-dblock t])
  19161. ("Dates and Scheduling"
  19162. ["Timestamp" org-time-stamp (not (org-before-first-heading-p))]
  19163. ["Timestamp (inactive)" org-time-stamp-inactive (not (org-before-first-heading-p))]
  19164. ("Change Date"
  19165. ["1 Day Later" org-shiftright (org-at-timestamp-p)]
  19166. ["1 Day Earlier" org-shiftleft (org-at-timestamp-p)]
  19167. ["1 ... Later" org-shiftup (org-at-timestamp-p)]
  19168. ["1 ... Earlier" org-shiftdown (org-at-timestamp-p)])
  19169. ["Compute Time Range" org-evaluate-time-range t]
  19170. ["Schedule Item" org-schedule (not (org-before-first-heading-p))]
  19171. ["Deadline" org-deadline (not (org-before-first-heading-p))]
  19172. "--"
  19173. ["Custom time format" org-toggle-time-stamp-overlays
  19174. :style radio :selected org-display-custom-times]
  19175. "--"
  19176. ["Goto Calendar" org-goto-calendar t]
  19177. ["Date from Calendar" org-date-from-calendar t]
  19178. "--"
  19179. ["Start/Restart Timer" org-timer-start t]
  19180. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  19181. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  19182. ["Insert Timer String" org-timer t]
  19183. ["Insert Timer Item" org-timer-item t])
  19184. ("Logging work"
  19185. ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
  19186. ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
  19187. ["Clock out" org-clock-out t]
  19188. ["Clock cancel" org-clock-cancel t]
  19189. "--"
  19190. ["Mark as default task" org-clock-mark-default-task t]
  19191. ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
  19192. ["Goto running clock" org-clock-goto t]
  19193. "--"
  19194. ["Display times" org-clock-display t]
  19195. ["Create clock table" org-clock-report t]
  19196. "--"
  19197. ["Record DONE time"
  19198. (progn (setq org-log-done (not org-log-done))
  19199. (message "Switching to %s will %s record a timestamp"
  19200. (car org-done-keywords)
  19201. (if org-log-done "automatically" "not")))
  19202. :style toggle :selected org-log-done])
  19203. "--"
  19204. ["Agenda Command..." org-agenda t]
  19205. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  19206. ("File List for Agenda")
  19207. ("Special views current file"
  19208. ["TODO Tree" org-show-todo-tree t]
  19209. ["Check Deadlines" org-check-deadlines t]
  19210. ["Timeline" org-timeline t]
  19211. ["Tags/Property tree" org-match-sparse-tree t])
  19212. "--"
  19213. ["Export/Publish..." org-export-dispatch t]
  19214. ("LaTeX"
  19215. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  19216. :selected org-cdlatex-mode]
  19217. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  19218. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  19219. ["Modify math symbol" org-cdlatex-math-modify
  19220. (org-inside-LaTeX-fragment-p)]
  19221. ["Insert citation" org-reftex-citation t]
  19222. "--"
  19223. ["Template for BEAMER" (org-beamer-insert-options-template) t])
  19224. "--"
  19225. ("MobileOrg"
  19226. ["Push Files and Views" org-mobile-push t]
  19227. ["Get Captured and Flagged" org-mobile-pull t]
  19228. ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
  19229. "--"
  19230. ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
  19231. "--"
  19232. ("Documentation"
  19233. ["Show Version" org-version t]
  19234. ["Info Documentation" org-info t])
  19235. ("Customize"
  19236. ["Browse Org Group" org-customize t]
  19237. "--"
  19238. ["Expand This Menu" org-create-customize-menu
  19239. (fboundp 'customize-menu-create)])
  19240. ["Send bug report" org-submit-bug-report t]
  19241. "--"
  19242. ("Refresh/Reload"
  19243. ["Refresh setup current buffer" org-mode-restart t]
  19244. ["Reload Org (after update)" org-reload t]
  19245. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x !"])
  19246. ))
  19247. (defun org-info (&optional node)
  19248. "Read documentation for Org-mode in the info system.
  19249. With optional NODE, go directly to that node."
  19250. (interactive)
  19251. (info (format "(org)%s" (or node ""))))
  19252. ;;;###autoload
  19253. (defun org-submit-bug-report ()
  19254. "Submit a bug report on Org-mode via mail.
  19255. Don't hesitate to report any problems or inaccurate documentation.
  19256. If you don't have setup sending mail from (X)Emacs, please copy the
  19257. output buffer into your mail program, as it gives us important
  19258. information about your Org-mode version and configuration."
  19259. (interactive)
  19260. (require 'reporter)
  19261. (org-load-modules-maybe)
  19262. (org-require-autoloaded-modules)
  19263. (let ((reporter-prompt-for-summary-p "Bug report subject: "))
  19264. (reporter-submit-bug-report
  19265. "emacs-orgmode@gnu.org"
  19266. (org-version nil 'full)
  19267. (let (list)
  19268. (save-window-excursion
  19269. (org-pop-to-buffer-same-window (get-buffer-create "*Warn about privacy*"))
  19270. (delete-other-windows)
  19271. (erase-buffer)
  19272. (insert "You are about to submit a bug report to the Org-mode mailing list.
  19273. We would like to add your full Org-mode and Outline configuration to the
  19274. bug report. This greatly simplifies the work of the maintainer and
  19275. other experts on the mailing list.
  19276. HOWEVER, some variables you have customized may contain private
  19277. information. The names of customers, colleagues, or friends, might
  19278. appear in the form of file names, tags, todo states, or search strings.
  19279. If you answer yes to the prompt, you might want to check and remove
  19280. such private information before sending the email.")
  19281. (add-text-properties (point-min) (point-max) '(face org-warning))
  19282. (when (yes-or-no-p "Include your Org-mode configuration ")
  19283. (mapatoms
  19284. (lambda (v)
  19285. (and (boundp v)
  19286. (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
  19287. (or (and (symbol-value v)
  19288. (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
  19289. (and
  19290. (get v 'custom-type) (get v 'standard-value)
  19291. (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
  19292. (push v list)))))
  19293. (kill-buffer (get-buffer "*Warn about privacy*"))
  19294. list))
  19295. nil nil
  19296. "Remember to cover the basics, that is, what you expected to happen and
  19297. what in fact did happen. You don't know how to make a good report? See
  19298. http://orgmode.org/manual/Feedback.html#Feedback
  19299. Your bug report will be posted to the Org-mode mailing list.
  19300. ------------------------------------------------------------------------")
  19301. (save-excursion
  19302. (if (re-search-backward "^\\(Subject: \\)Org-mode version \\(.*?\\);[ \t]*\\(.*\\)" nil t)
  19303. (replace-match "\\1Bug: \\3 [\\2]")))))
  19304. (defun org-install-agenda-files-menu ()
  19305. (let ((bl (buffer-list)))
  19306. (save-excursion
  19307. (while bl
  19308. (set-buffer (pop bl))
  19309. (if (derived-mode-p 'org-mode) (setq bl nil)))
  19310. (when (derived-mode-p 'org-mode)
  19311. (easy-menu-change
  19312. '("Org") "File List for Agenda"
  19313. (append
  19314. (list
  19315. ["Edit File List" (org-edit-agenda-file-list) t]
  19316. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  19317. ["Remove Current File from List" org-remove-file t]
  19318. ["Cycle through agenda files" org-cycle-agenda-files t]
  19319. ["Occur in all agenda files" org-occur-in-agenda-files t]
  19320. "--")
  19321. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  19322. ;;;; Documentation
  19323. (defun org-require-autoloaded-modules ()
  19324. (interactive)
  19325. (mapc 'require
  19326. '(org-agenda org-archive org-attach org-clock org-colview org-id
  19327. org-remember org-table org-timer)))
  19328. ;;;###autoload
  19329. (defun org-reload (&optional uncompiled)
  19330. "Reload all org lisp files.
  19331. With prefix arg UNCOMPILED, load the uncompiled versions."
  19332. (interactive "P")
  19333. (require 'loadhist)
  19334. (let* ((org-dir (org-find-library-dir "org"))
  19335. (contrib-dir (or (org-find-library-dir "org-contribdir") org-dir))
  19336. (feature-re "^\\(org\\|ob\\|ox\\)\\(-.*\\)?")
  19337. (remove-re (mapconcat 'identity
  19338. (mapcar (lambda (f) (concat "^" f "$"))
  19339. (list (if (featurep 'xemacs)
  19340. "org-colview"
  19341. "org-colview-xemacs")
  19342. "org" "org-loaddefs" "org-version"))
  19343. "\\|"))
  19344. (feats (delete-dups
  19345. (mapcar 'file-name-sans-extension
  19346. (mapcar 'file-name-nondirectory
  19347. (delq nil
  19348. (mapcar 'feature-file
  19349. features))))))
  19350. (lfeat (append
  19351. (sort
  19352. (setq feats
  19353. (delq nil (mapcar
  19354. (lambda (f)
  19355. (if (and (string-match feature-re f)
  19356. (not (string-match remove-re f)))
  19357. f nil))
  19358. feats)))
  19359. 'string-lessp)
  19360. (list "org-version" "org")))
  19361. (load-suffixes (when (boundp 'load-suffixes) load-suffixes))
  19362. (load-suffixes (if uncompiled (reverse load-suffixes) load-suffixes))
  19363. load-uncore load-misses)
  19364. (setq load-misses
  19365. (delq 't
  19366. (mapcar (lambda (f)
  19367. (or (org-load-noerror-mustsuffix (concat org-dir f))
  19368. (and (string= org-dir contrib-dir)
  19369. (org-load-noerror-mustsuffix (concat contrib-dir f)))
  19370. (and (org-load-noerror-mustsuffix (concat (org-find-library-dir f) f))
  19371. (add-to-list 'load-uncore f 'append)
  19372. 't)
  19373. f))
  19374. lfeat)))
  19375. (if load-uncore
  19376. (message "The following feature%s found in load-path, please check if that's correct:\n%s"
  19377. (if (> (length load-uncore) 1) "s were" " was") load-uncore))
  19378. (if load-misses
  19379. (message "Some error occured while reloading Org feature%s\n%s\nPlease check *Messages*!\n%s"
  19380. (if (> (length load-misses) 1) "s" "") load-misses (org-version nil 'full))
  19381. (message "Successfully reloaded Org\n%s" (org-version nil 'full)))))
  19382. ;;;###autoload
  19383. (defun org-customize ()
  19384. "Call the customize function with org as argument."
  19385. (interactive)
  19386. (org-load-modules-maybe)
  19387. (org-require-autoloaded-modules)
  19388. (customize-browse 'org))
  19389. (defun org-create-customize-menu ()
  19390. "Create a full customization menu for Org-mode, insert it into the menu."
  19391. (interactive)
  19392. (org-load-modules-maybe)
  19393. (org-require-autoloaded-modules)
  19394. (if (fboundp 'customize-menu-create)
  19395. (progn
  19396. (easy-menu-change
  19397. '("Org") "Customize"
  19398. `(["Browse Org group" org-customize t]
  19399. "--"
  19400. ,(customize-menu-create 'org)
  19401. ["Set" Custom-set t]
  19402. ["Save" Custom-save t]
  19403. ["Reset to Current" Custom-reset-current t]
  19404. ["Reset to Saved" Custom-reset-saved t]
  19405. ["Reset to Standard Settings" Custom-reset-standard t]))
  19406. (message "\"Org\"-menu now contains full customization menu"))
  19407. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  19408. ;;;; Miscellaneous stuff
  19409. ;;; Generally useful functions
  19410. (defun org-get-at-bol (property)
  19411. "Get text property PROPERTY at beginning of line."
  19412. (get-text-property (point-at-bol) property))
  19413. (defun org-find-text-property-in-string (prop s)
  19414. "Return the first non-nil value of property PROP in string S."
  19415. (or (get-text-property 0 prop s)
  19416. (get-text-property (or (next-single-property-change 0 prop s) 0)
  19417. prop s)))
  19418. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  19419. "Display the given MESSAGE as a warning."
  19420. (if (fboundp 'display-warning)
  19421. (display-warning 'org message
  19422. (if (featurep 'xemacs) 'warning :warning))
  19423. (let ((buf (get-buffer-create "*Org warnings*")))
  19424. (with-current-buffer buf
  19425. (goto-char (point-max))
  19426. (insert "Warning (Org): " message)
  19427. (unless (bolp)
  19428. (newline)))
  19429. (display-buffer buf)
  19430. (sit-for 0))))
  19431. (defun org-eval (form)
  19432. "Eval FORM and return result."
  19433. (condition-case error
  19434. (eval form)
  19435. (error (format "%%![Error: %s]" error))))
  19436. (defun org-in-clocktable-p ()
  19437. "Check if the cursor is in a clocktable."
  19438. (let ((pos (point)) start)
  19439. (save-excursion
  19440. (end-of-line 1)
  19441. (and (re-search-backward "^[ \t]*#\\+BEGIN:[ \t]+clocktable" nil t)
  19442. (setq start (match-beginning 0))
  19443. (re-search-forward "^[ \t]*#\\+END:.*" nil t)
  19444. (>= (match-end 0) pos)
  19445. start))))
  19446. (defun org-in-commented-line ()
  19447. "Is point in a line starting with `#'?"
  19448. (equal (char-after (point-at-bol)) ?#))
  19449. (defun org-in-indented-comment-line ()
  19450. "Is point in a line starting with `#' after some white space?"
  19451. (save-excursion
  19452. (save-match-data
  19453. (goto-char (point-at-bol))
  19454. (looking-at "[ \t]*#"))))
  19455. (defun org-in-verbatim-emphasis ()
  19456. (save-match-data
  19457. (and (org-in-regexp org-emph-re 2)
  19458. (>= (point) (match-beginning 3))
  19459. (<= (point) (match-end 4))
  19460. (member (match-string 3) '("=" "~")))))
  19461. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  19462. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  19463. (if (and marker (marker-buffer marker)
  19464. (buffer-live-p (marker-buffer marker)))
  19465. (progn
  19466. (org-pop-to-buffer-same-window (marker-buffer marker))
  19467. (if (or (> marker (point-max)) (< marker (point-min)))
  19468. (widen))
  19469. (goto-char marker)
  19470. (org-show-context 'org-goto))
  19471. (if bookmark
  19472. (bookmark-jump bookmark)
  19473. (error "Cannot find location"))))
  19474. (defun org-quote-csv-field (s)
  19475. "Quote field for inclusion in CSV material."
  19476. (if (string-match "[\",]" s)
  19477. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  19478. s))
  19479. (defun org-force-self-insert (N)
  19480. "Needed to enforce self-insert under remapping."
  19481. (interactive "p")
  19482. (self-insert-command N))
  19483. (defun org-string-width (s)
  19484. "Compute width of string, ignoring invisible characters.
  19485. This ignores character with invisibility property `org-link', and also
  19486. characters with property `org-cwidth', because these will become invisible
  19487. upon the next fontification round."
  19488. (let (b l)
  19489. (when (or (eq t buffer-invisibility-spec)
  19490. (assq 'org-link buffer-invisibility-spec))
  19491. (while (setq b (text-property-any 0 (length s)
  19492. 'invisible 'org-link s))
  19493. (setq s (concat (substring s 0 b)
  19494. (substring s (or (next-single-property-change
  19495. b 'invisible s) (length s)))))))
  19496. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  19497. (setq s (concat (substring s 0 b)
  19498. (substring s (or (next-single-property-change
  19499. b 'org-cwidth s) (length s))))))
  19500. (setq l (string-width s) b -1)
  19501. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  19502. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  19503. l))
  19504. (defun org-shorten-string (s maxlength)
  19505. "Shorten string S so tht it is no longer than MAXLENGTH characters.
  19506. If the string is shorter or has length MAXLENGTH, just return the
  19507. original string. If it is longer, the functions finds a space in the
  19508. string, breaks this string off at that locations and adds three dots
  19509. as ellipsis. Including the ellipsis, the string will not be longer
  19510. than MAXLENGTH. If finding a good breaking point in the string does
  19511. not work, the string is just chopped off in the middle of a word
  19512. if necessary."
  19513. (if (<= (length s) maxlength)
  19514. s
  19515. (let* ((n (max (- maxlength 4) 1))
  19516. (re (concat "\\`\\(.\\{1," (int-to-string n) "\\}[^ ]\\)\\([ ]\\|\\'\\)")))
  19517. (if (string-match re s)
  19518. (concat (match-string 1 s) "...")
  19519. (concat (substring s 0 (max (- maxlength 3) 0)) "...")))))
  19520. (defun org-get-indentation (&optional line)
  19521. "Get the indentation of the current line, interpreting tabs.
  19522. When LINE is given, assume it represents a line and compute its indentation."
  19523. (if line
  19524. (if (string-match "^ *" (org-remove-tabs line))
  19525. (match-end 0))
  19526. (save-excursion
  19527. (beginning-of-line 1)
  19528. (skip-chars-forward " \t")
  19529. (current-column))))
  19530. (defun org-get-string-indentation (s)
  19531. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  19532. (let ((n -1) (i 0) (w tab-width) c)
  19533. (catch 'exit
  19534. (while (< (setq n (1+ n)) (length s))
  19535. (setq c (aref s n))
  19536. (cond ((= c ?\ ) (setq i (1+ i)))
  19537. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  19538. (t (throw 'exit t)))))
  19539. i))
  19540. (defun org-remove-tabs (s &optional width)
  19541. "Replace tabulators in S with spaces.
  19542. Assumes that s is a single line, starting in column 0."
  19543. (setq width (or width tab-width))
  19544. (while (string-match "\t" s)
  19545. (setq s (replace-match
  19546. (make-string
  19547. (- (* width (/ (+ (match-beginning 0) width) width))
  19548. (match-beginning 0)) ?\ )
  19549. t t s)))
  19550. s)
  19551. (defun org-fix-indentation (line ind)
  19552. "Fix indentation in LINE.
  19553. IND is a cons cell with target and minimum indentation.
  19554. If the current indentation in LINE is smaller than the minimum,
  19555. leave it alone. If it is larger than ind, set it to the target."
  19556. (let* ((l (org-remove-tabs line))
  19557. (i (org-get-indentation l))
  19558. (i1 (car ind)) (i2 (cdr ind)))
  19559. (if (>= i i2) (setq l (substring line i2)))
  19560. (if (> i1 0)
  19561. (concat (make-string i1 ?\ ) l)
  19562. l)))
  19563. (defun org-remove-indentation (code &optional n)
  19564. "Remove the maximum common indentation from the lines in CODE.
  19565. N may optionally be the number of spaces to remove."
  19566. (with-temp-buffer
  19567. (insert code)
  19568. (org-do-remove-indentation n)
  19569. (buffer-string)))
  19570. (defun org-do-remove-indentation (&optional n)
  19571. "Remove the maximum common indentation from the buffer."
  19572. (untabify (point-min) (point-max))
  19573. (let ((min 10000) re)
  19574. (if n
  19575. (setq min n)
  19576. (goto-char (point-min))
  19577. (while (re-search-forward "^ *[^ \n]" nil t)
  19578. (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
  19579. (unless (or (= min 0) (= min 10000))
  19580. (setq re (format "^ \\{%d\\}" min))
  19581. (goto-char (point-min))
  19582. (while (re-search-forward re nil t)
  19583. (replace-match "")
  19584. (end-of-line 1))
  19585. min)))
  19586. (defun org-fill-template (template alist)
  19587. "Find each %key of ALIST in TEMPLATE and replace it."
  19588. (let ((case-fold-search nil)
  19589. entry key value)
  19590. (setq alist (sort (copy-sequence alist)
  19591. (lambda (a b) (< (length (car a)) (length (car b))))))
  19592. (while (setq entry (pop alist))
  19593. (setq template
  19594. (replace-regexp-in-string
  19595. (concat "%" (regexp-quote (car entry)))
  19596. (or (cdr entry) "") template t t)))
  19597. template))
  19598. (defun org-base-buffer (buffer)
  19599. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  19600. (if (not buffer)
  19601. buffer
  19602. (or (buffer-base-buffer buffer)
  19603. buffer)))
  19604. (defun org-trim (s)
  19605. "Remove whitespace at beginning and end of string."
  19606. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  19607. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  19608. s)
  19609. (defun org-wrap (string &optional width lines)
  19610. "Wrap string to either a number of lines, or a width in characters.
  19611. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  19612. that costs. If there is a word longer than WIDTH, the text is actually
  19613. wrapped to the length of that word.
  19614. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  19615. many lines, whatever width that takes.
  19616. The return value is a list of lines, without newlines at the end."
  19617. (let* ((words (org-split-string string "[ \t\n]+"))
  19618. (maxword (apply 'max (mapcar 'org-string-width words)))
  19619. w ll)
  19620. (cond (width
  19621. (org-do-wrap words (max maxword width)))
  19622. (lines
  19623. (setq w maxword)
  19624. (setq ll (org-do-wrap words maxword))
  19625. (if (<= (length ll) lines)
  19626. ll
  19627. (setq ll words)
  19628. (while (> (length ll) lines)
  19629. (setq w (1+ w))
  19630. (setq ll (org-do-wrap words w)))
  19631. ll))
  19632. (t (error "Cannot wrap this")))))
  19633. (defun org-do-wrap (words width)
  19634. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  19635. (let (lines line)
  19636. (while words
  19637. (setq line (pop words))
  19638. (while (and words (< (+ (length line) (length (car words))) width))
  19639. (setq line (concat line " " (pop words))))
  19640. (setq lines (push line lines)))
  19641. (nreverse lines)))
  19642. (defun org-split-string (string &optional separators)
  19643. "Splits STRING into substrings at SEPARATORS.
  19644. No empty strings are returned if there are matches at the beginning
  19645. and end of string."
  19646. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  19647. (start 0)
  19648. notfirst
  19649. (list nil))
  19650. (while (and (string-match rexp string
  19651. (if (and notfirst
  19652. (= start (match-beginning 0))
  19653. (< start (length string)))
  19654. (1+ start) start))
  19655. (< (match-beginning 0) (length string)))
  19656. (setq notfirst t)
  19657. (or (eq (match-beginning 0) 0)
  19658. (and (eq (match-beginning 0) (match-end 0))
  19659. (eq (match-beginning 0) start))
  19660. (setq list
  19661. (cons (substring string start (match-beginning 0))
  19662. list)))
  19663. (setq start (match-end 0)))
  19664. (or (eq start (length string))
  19665. (setq list
  19666. (cons (substring string start)
  19667. list)))
  19668. (nreverse list)))
  19669. (defun org-quote-vert (s)
  19670. "Replace \"|\" with \"\\vert\"."
  19671. (while (string-match "|" s)
  19672. (setq s (replace-match "\\vert" t t s)))
  19673. s)
  19674. (defun org-uuidgen-p (s)
  19675. "Is S an ID created by UUIDGEN?"
  19676. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  19677. (defun org-in-src-block-p (&optional inside)
  19678. "Whether point is in a code source block.
  19679. When INSIDE is non-nil, don't consider we are within a src block
  19680. when point is at #+BEGIN_SRC or #+END_SRC."
  19681. (let ((case-fold-search t) ov)
  19682. (or (and (setq ov (overlays-at (point)))
  19683. (memq 'org-block-background
  19684. (overlay-properties (car ov))))
  19685. (and (not inside)
  19686. (save-match-data
  19687. (save-excursion
  19688. (beginning-of-line)
  19689. (looking-at ".*#\\+\\(begin\\|end\\)_src")))))))
  19690. (defun org-context ()
  19691. "Return a list of contexts of the current cursor position.
  19692. If several contexts apply, all are returned.
  19693. Each context entry is a list with a symbol naming the context, and
  19694. two positions indicating start and end of the context. Possible
  19695. contexts are:
  19696. :headline anywhere in a headline
  19697. :headline-stars on the leading stars in a headline
  19698. :todo-keyword on a TODO keyword (including DONE) in a headline
  19699. :tags on the TAGS in a headline
  19700. :priority on the priority cookie in a headline
  19701. :item on the first line of a plain list item
  19702. :item-bullet on the bullet/number of a plain list item
  19703. :checkbox on the checkbox in a plain list item
  19704. :table in an org-mode table
  19705. :table-special on a special filed in a table
  19706. :table-table in a table.el table
  19707. :clocktable in a clocktable
  19708. :src-block in a source block
  19709. :link on a hyperlink
  19710. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE, COMMENT, QUOTE.
  19711. :target on a <<target>>
  19712. :radio-target on a <<<radio-target>>>
  19713. :latex-fragment on a LaTeX fragment
  19714. :latex-preview on a LaTeX fragment with overlaid preview image
  19715. This function expects the position to be visible because it uses font-lock
  19716. faces as a help to recognize the following contexts: :table-special, :link,
  19717. and :keyword."
  19718. (let* ((f (get-text-property (point) 'face))
  19719. (faces (if (listp f) f (list f)))
  19720. (case-fold-search t)
  19721. (p (point)) clist o)
  19722. ;; First the large context
  19723. (cond
  19724. ((org-at-heading-p t)
  19725. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  19726. (when (progn
  19727. (beginning-of-line 1)
  19728. (looking-at org-todo-line-tags-regexp))
  19729. (push (org-point-in-group p 1 :headline-stars) clist)
  19730. (push (org-point-in-group p 2 :todo-keyword) clist)
  19731. (push (org-point-in-group p 4 :tags) clist))
  19732. (goto-char p)
  19733. (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
  19734. (if (looking-at "\\[#[A-Z0-9]\\]")
  19735. (push (org-point-in-group p 0 :priority) clist)))
  19736. ((org-at-item-p)
  19737. (push (org-point-in-group p 2 :item-bullet) clist)
  19738. (push (list :item (point-at-bol)
  19739. (save-excursion (org-end-of-item) (point)))
  19740. clist)
  19741. (and (org-at-item-checkbox-p)
  19742. (push (org-point-in-group p 0 :checkbox) clist)))
  19743. ((org-at-table-p)
  19744. (push (list :table (org-table-begin) (org-table-end)) clist)
  19745. (if (memq 'org-formula faces)
  19746. (push (list :table-special
  19747. (previous-single-property-change p 'face)
  19748. (next-single-property-change p 'face)) clist)))
  19749. ((org-at-table-p 'any)
  19750. (push (list :table-table) clist)))
  19751. (goto-char p)
  19752. (let ((case-fold-search t))
  19753. ;; New the "medium" contexts: clocktables, source blocks
  19754. (cond ((org-in-clocktable-p)
  19755. (push (list :clocktable
  19756. (and (or (looking-at "#\\+BEGIN: clocktable")
  19757. (search-backward "#+BEGIN: clocktable" nil t))
  19758. (match-beginning 0))
  19759. (and (re-search-forward "#\\+END:?" nil t)
  19760. (match-end 0))) clist))
  19761. ((org-in-src-block-p)
  19762. (push (list :src-block
  19763. (and (or (looking-at "#\\+BEGIN_SRC")
  19764. (search-backward "#+BEGIN_SRC" nil t))
  19765. (match-beginning 0))
  19766. (and (search-forward "#+END_SRC" nil t)
  19767. (match-beginning 0))) clist))))
  19768. (goto-char p)
  19769. ;; Now the small context
  19770. (cond
  19771. ((org-at-timestamp-p)
  19772. (push (org-point-in-group p 0 :timestamp) clist))
  19773. ((memq 'org-link faces)
  19774. (push (list :link
  19775. (previous-single-property-change p 'face)
  19776. (next-single-property-change p 'face)) clist))
  19777. ((memq 'org-special-keyword faces)
  19778. (push (list :keyword
  19779. (previous-single-property-change p 'face)
  19780. (next-single-property-change p 'face)) clist))
  19781. ((org-at-target-p)
  19782. (push (org-point-in-group p 0 :target) clist)
  19783. (goto-char (1- (match-beginning 0)))
  19784. (if (looking-at org-radio-target-regexp)
  19785. (push (org-point-in-group p 0 :radio-target) clist))
  19786. (goto-char p))
  19787. ((setq o (car (delq nil
  19788. (mapcar
  19789. (lambda (x)
  19790. (if (memq x org-latex-fragment-image-overlays) x))
  19791. (overlays-at (point))))))
  19792. (push (list :latex-fragment
  19793. (overlay-start o) (overlay-end o)) clist)
  19794. (push (list :latex-preview
  19795. (overlay-start o) (overlay-end o)) clist))
  19796. ((org-inside-LaTeX-fragment-p)
  19797. ;; FIXME: positions wrong.
  19798. (push (list :latex-fragment (point) (point)) clist)))
  19799. (setq clist (nreverse (delq nil clist)))
  19800. clist))
  19801. ;; FIXME: Compare with at-regexp-p Do we need both?
  19802. (defun org-in-regexp (re &optional nlines visually)
  19803. "Check if point is inside a match of regexp.
  19804. Normally only the current line is checked, but you can include NLINES extra
  19805. lines both before and after point into the search.
  19806. If VISUALLY is set, require that the cursor is not after the match but
  19807. really on, so that the block visually is on the match."
  19808. (catch 'exit
  19809. (let ((pos (point))
  19810. (eol (point-at-eol (+ 1 (or nlines 0))))
  19811. (inc (if visually 1 0)))
  19812. (save-excursion
  19813. (beginning-of-line (- 1 (or nlines 0)))
  19814. (while (re-search-forward re eol t)
  19815. (if (and (<= (match-beginning 0) pos)
  19816. (>= (+ inc (match-end 0)) pos))
  19817. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  19818. (defun org-at-regexp-p (regexp)
  19819. "Is point inside a match of REGEXP in the current line?"
  19820. (catch 'exit
  19821. (save-excursion
  19822. (let ((pos (point)) (end (point-at-eol)))
  19823. (beginning-of-line 1)
  19824. (while (re-search-forward regexp end t)
  19825. (if (and (<= (match-beginning 0) pos)
  19826. (>= (match-end 0) pos))
  19827. (throw 'exit t)))
  19828. nil))))
  19829. (defun org-between-regexps-p (start-re end-re &optional lim-up lim-down)
  19830. "Non-nil when point is between matches of START-RE and END-RE.
  19831. Also return a non-nil value when point is on one of the matches.
  19832. Optional arguments LIM-UP and LIM-DOWN bound the search; they are
  19833. buffer positions. Default values are the positions of headlines
  19834. surrounding the point.
  19835. The functions returns a cons cell whose car (resp. cdr) is the
  19836. position before START-RE (resp. after END-RE)."
  19837. (save-match-data
  19838. (let ((pos (point))
  19839. (limit-up (or lim-up (save-excursion (outline-previous-heading))))
  19840. (limit-down (or lim-down (save-excursion (outline-next-heading))))
  19841. beg end)
  19842. (save-excursion
  19843. ;; Point is on a block when on START-RE or if START-RE can be
  19844. ;; found before it...
  19845. (and (or (org-at-regexp-p start-re)
  19846. (re-search-backward start-re limit-up t))
  19847. (setq beg (match-beginning 0))
  19848. ;; ... and END-RE after it...
  19849. (goto-char (match-end 0))
  19850. (re-search-forward end-re limit-down t)
  19851. (> (setq end (match-end 0)) pos)
  19852. ;; ... without another START-RE in-between.
  19853. (goto-char (match-beginning 0))
  19854. (not (re-search-backward start-re (1+ beg) t))
  19855. ;; Return value.
  19856. (cons beg end))))))
  19857. (defun org-in-block-p (names)
  19858. "Non-nil when point belongs to a block whose name belongs to NAMES.
  19859. NAMES is a list of strings containing names of blocks.
  19860. Return first block name matched, or nil. Beware that in case of
  19861. nested blocks, the returned name may not belong to the closest
  19862. block from point."
  19863. (save-match-data
  19864. (catch 'exit
  19865. (let ((case-fold-search t)
  19866. (lim-up (save-excursion (outline-previous-heading)))
  19867. (lim-down (save-excursion (outline-next-heading))))
  19868. (mapc (lambda (name)
  19869. (let ((n (regexp-quote name)))
  19870. (when (org-between-regexps-p
  19871. (concat "^[ \t]*#\\+begin_" n)
  19872. (concat "^[ \t]*#\\+end_" n)
  19873. lim-up lim-down)
  19874. (throw 'exit n))))
  19875. names))
  19876. nil)))
  19877. (defun org-in-drawer-p ()
  19878. "Is point within a drawer?"
  19879. (save-match-data
  19880. (let ((case-fold-search t)
  19881. (lim-up (save-excursion (outline-previous-heading)))
  19882. (lim-down (save-excursion (outline-next-heading))))
  19883. (org-between-regexps-p
  19884. (concat "^[ \t]*:" (regexp-opt org-drawers) ":")
  19885. "^[ \t]*:end:.*$"
  19886. lim-up lim-down))))
  19887. (defun org-occur-in-agenda-files (regexp &optional nlines)
  19888. "Call `multi-occur' with buffers for all agenda files."
  19889. (interactive "sOrg-files matching: \np")
  19890. (let* ((files (org-agenda-files))
  19891. (tnames (mapcar 'file-truename files))
  19892. (extra org-agenda-text-search-extra-files)
  19893. f)
  19894. (when (eq (car extra) 'agenda-archives)
  19895. (setq extra (cdr extra))
  19896. (setq files (org-add-archive-files files)))
  19897. (while (setq f (pop extra))
  19898. (unless (member (file-truename f) tnames)
  19899. (add-to-list 'files f 'append)
  19900. (add-to-list 'tnames (file-truename f) 'append)))
  19901. (multi-occur
  19902. (mapcar (lambda (x)
  19903. (with-current-buffer
  19904. (or (get-file-buffer x) (find-file-noselect x))
  19905. (widen)
  19906. (current-buffer)))
  19907. files)
  19908. regexp)))
  19909. (if (boundp 'occur-mode-find-occurrence-hook)
  19910. ;; Emacs 23
  19911. (add-hook 'occur-mode-find-occurrence-hook
  19912. (lambda ()
  19913. (when (derived-mode-p 'org-mode)
  19914. (org-reveal))))
  19915. ;; Emacs 22
  19916. (defadvice occur-mode-goto-occurrence
  19917. (after org-occur-reveal activate)
  19918. (and (derived-mode-p 'org-mode) (org-reveal)))
  19919. (defadvice occur-mode-goto-occurrence-other-window
  19920. (after org-occur-reveal activate)
  19921. (and (derived-mode-p 'org-mode) (org-reveal)))
  19922. (defadvice occur-mode-display-occurrence
  19923. (after org-occur-reveal activate)
  19924. (when (derived-mode-p 'org-mode)
  19925. (let ((pos (occur-mode-find-occurrence)))
  19926. (with-current-buffer (marker-buffer pos)
  19927. (save-excursion
  19928. (goto-char pos)
  19929. (org-reveal)))))))
  19930. (defun org-occur-link-in-agenda-files ()
  19931. "Create a link and search for it in the agendas.
  19932. The link is not stored in `org-stored-links', it is just created
  19933. for the search purpose."
  19934. (interactive)
  19935. (let ((link (condition-case nil
  19936. (org-store-link nil)
  19937. (error "Unable to create a link to here"))))
  19938. (org-occur-in-agenda-files (regexp-quote link))))
  19939. (defun org-reverse-string (string)
  19940. "Return the reverse of STRING."
  19941. (apply 'string (reverse (string-to-list string))))
  19942. (defsubst org-uniquify (list)
  19943. "Non-destructively remove duplicate elements from LIST."
  19944. (let ((res (copy-sequence list))) (delete-dups res)))
  19945. (defun org-uniquify-alist (alist)
  19946. "Merge elements of ALIST with the same key.
  19947. For example, in this alist:
  19948. \(org-uniquify-alist '((a 1) (b 2) (a 3)))
  19949. => '((a 1 3) (b 2))
  19950. merge (a 1) and (a 3) into (a 1 3).
  19951. The function returns the new ALIST."
  19952. (let (rtn)
  19953. (mapc
  19954. (lambda (e)
  19955. (let (n)
  19956. (if (not (assoc (car e) rtn))
  19957. (push e rtn)
  19958. (setq n (cons (car e) (append (cdr (assoc (car e) rtn)) (cdr e))))
  19959. (setq rtn (assq-delete-all (car e) rtn))
  19960. (push n rtn))))
  19961. alist)
  19962. rtn))
  19963. (defun org-delete-all (elts list)
  19964. "Remove all elements in ELTS from LIST."
  19965. (while elts
  19966. (setq list (delete (pop elts) list)))
  19967. list)
  19968. (defun org-count (cl-item cl-seq)
  19969. "Count the number of occurrences of ITEM in SEQ.
  19970. Taken from `count' in cl-seq.el with all keyword arguments removed."
  19971. (let ((cl-end (length cl-seq)) (cl-start 0) (cl-count 0) cl-x)
  19972. (when (consp cl-seq) (setq cl-seq (nthcdr cl-start cl-seq)))
  19973. (while (< cl-start cl-end)
  19974. (setq cl-x (if (consp cl-seq) (pop cl-seq) (aref cl-seq cl-start)))
  19975. (if (equal cl-item cl-x) (setq cl-count (1+ cl-count)))
  19976. (setq cl-start (1+ cl-start)))
  19977. cl-count))
  19978. (defun org-remove-if (predicate seq)
  19979. "Remove everything from SEQ that fulfills PREDICATE."
  19980. (let (res e)
  19981. (while seq
  19982. (setq e (pop seq))
  19983. (if (not (funcall predicate e)) (push e res)))
  19984. (nreverse res)))
  19985. (defun org-remove-if-not (predicate seq)
  19986. "Remove everything from SEQ that does not fulfill PREDICATE."
  19987. (let (res e)
  19988. (while seq
  19989. (setq e (pop seq))
  19990. (if (funcall predicate e) (push e res)))
  19991. (nreverse res)))
  19992. (defun org-reduce (cl-func cl-seq &rest cl-keys)
  19993. "Reduce two-argument FUNCTION across SEQ.
  19994. Taken from `reduce' in cl-seq.el with all keyword arguments but
  19995. \":initial-value\" removed."
  19996. (let ((cl-accum (cond ((memq :initial-value cl-keys)
  19997. (cadr (memq :initial-value cl-keys)))
  19998. (cl-seq (pop cl-seq))
  19999. (t (funcall cl-func)))))
  20000. (while cl-seq
  20001. (setq cl-accum (funcall cl-func cl-accum (pop cl-seq))))
  20002. cl-accum))
  20003. (defun org-back-over-empty-lines ()
  20004. "Move backwards over whitespace, to the beginning of the first empty line.
  20005. Returns the number of empty lines passed."
  20006. (let ((pos (point)))
  20007. (if (cdr (assoc 'heading org-blank-before-new-entry))
  20008. (skip-chars-backward " \t\n\r")
  20009. (unless (eobp)
  20010. (forward-line -1)))
  20011. (beginning-of-line 2)
  20012. (goto-char (min (point) pos))
  20013. (count-lines (point) pos)))
  20014. (defun org-skip-whitespace ()
  20015. (skip-chars-forward " \t\n\r"))
  20016. (defun org-point-in-group (point group &optional context)
  20017. "Check if POINT is in match-group GROUP.
  20018. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  20019. match. If the match group does not exist or point is not inside it,
  20020. return nil."
  20021. (and (match-beginning group)
  20022. (>= point (match-beginning group))
  20023. (<= point (match-end group))
  20024. (if context
  20025. (list context (match-beginning group) (match-end group))
  20026. t)))
  20027. (defun org-switch-to-buffer-other-window (&rest args)
  20028. "Switch to buffer in a second window on the current frame.
  20029. In particular, do not allow pop-up frames.
  20030. Returns the newly created buffer."
  20031. (org-no-popups
  20032. (apply 'switch-to-buffer-other-window args)))
  20033. (defun org-combine-plists (&rest plists)
  20034. "Create a single property list from all plists in PLISTS.
  20035. The process starts by copying the first list, and then setting properties
  20036. from the other lists. Settings in the last list are the most significant
  20037. ones and overrule settings in the other lists."
  20038. (let ((rtn (copy-sequence (pop plists)))
  20039. p v ls)
  20040. (while plists
  20041. (setq ls (pop plists))
  20042. (while ls
  20043. (setq p (pop ls) v (pop ls))
  20044. (setq rtn (plist-put rtn p v))))
  20045. rtn))
  20046. (defun org-replace-escapes (string table)
  20047. "Replace %-escapes in STRING with values in TABLE.
  20048. TABLE is an association list with keys like \"%a\" and string values.
  20049. The sequences in STRING may contain normal field width and padding information,
  20050. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  20051. so values can contain further %-escapes if they are define later in TABLE."
  20052. (let ((tbl (copy-alist table))
  20053. (case-fold-search nil)
  20054. (pchg 0)
  20055. e re rpl)
  20056. (while (setq e (pop tbl))
  20057. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  20058. (when (and (cdr e) (string-match re (cdr e)))
  20059. (let ((sref (substring (cdr e) (match-beginning 0) (match-end 0)))
  20060. (safe "SREF"))
  20061. (add-text-properties 0 3 (list 'sref sref) safe)
  20062. (setcdr e (replace-match safe t t (cdr e)))))
  20063. (while (string-match re string)
  20064. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  20065. (cdr e)))
  20066. (setq string (replace-match rpl t t string))))
  20067. (while (setq pchg (next-property-change pchg string))
  20068. (let ((sref (get-text-property pchg 'sref string)))
  20069. (when (and sref (string-match "SREF" string pchg))
  20070. (setq string (replace-match sref t t string)))))
  20071. string))
  20072. (defun org-sublist (list start end)
  20073. "Return a section of LIST, from START to END.
  20074. Counting starts at 1."
  20075. (let (rtn (c start))
  20076. (setq list (nthcdr (1- start) list))
  20077. (while (and list (<= c end))
  20078. (push (pop list) rtn)
  20079. (setq c (1+ c)))
  20080. (nreverse rtn)))
  20081. (defun org-find-base-buffer-visiting (file)
  20082. "Like `find-buffer-visiting' but always return the base buffer and
  20083. not an indirect buffer."
  20084. (let ((buf (or (get-file-buffer file)
  20085. (find-buffer-visiting file))))
  20086. (if buf
  20087. (or (buffer-base-buffer buf) buf)
  20088. nil)))
  20089. (defun org-image-file-name-regexp (&optional extensions)
  20090. "Return regexp matching the file names of images.
  20091. If EXTENSIONS is given, only match these."
  20092. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  20093. (image-file-name-regexp)
  20094. (let ((image-file-name-extensions
  20095. (or extensions
  20096. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  20097. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  20098. (concat "\\."
  20099. (regexp-opt (nconc (mapcar 'upcase
  20100. image-file-name-extensions)
  20101. image-file-name-extensions)
  20102. t)
  20103. "\\'"))))
  20104. (defun org-file-image-p (file &optional extensions)
  20105. "Return non-nil if FILE is an image."
  20106. (save-match-data
  20107. (string-match (org-image-file-name-regexp extensions) file)))
  20108. (defun org-get-cursor-date (&optional with-time)
  20109. "Return the date at cursor in as a time.
  20110. This works in the calendar and in the agenda, anywhere else it just
  20111. returns the current time.
  20112. If WITH-TIME is non-nil, returns the time of the event at point (in
  20113. the agenda) or the current time of the day."
  20114. (let (date day defd tp tm hod mod)
  20115. (when with-time
  20116. (setq tp (get-text-property (point) 'time))
  20117. (when (and tp (string-match "\\([0-9][0-9]\\):\\([0-9][0-9]\\)" tp))
  20118. (setq hod (string-to-number (match-string 1 tp))
  20119. mod (string-to-number (match-string 2 tp))))
  20120. (or tp (setq hod (nth 2 (decode-time (current-time)))
  20121. mod (nth 1 (decode-time (current-time))))))
  20122. (cond
  20123. ((eq major-mode 'calendar-mode)
  20124. (setq date (calendar-cursor-to-date)
  20125. defd (encode-time 0 (or mod 0) (or hod 0)
  20126. (nth 1 date) (nth 0 date) (nth 2 date))))
  20127. ((eq major-mode 'org-agenda-mode)
  20128. (setq day (get-text-property (point) 'day))
  20129. (if day
  20130. (setq date (calendar-gregorian-from-absolute day)
  20131. defd (encode-time 0 (or mod 0) (or hod 0)
  20132. (nth 1 date) (nth 0 date) (nth 2 date))))))
  20133. (or defd (current-time))))
  20134. (defun org-mark-subtree (&optional up)
  20135. "Mark the current subtree.
  20136. This puts point at the start of the current subtree, and mark at
  20137. the end. If a numeric prefix UP is given, move up into the
  20138. hierarchy of headlines by UP levels before marking the subtree."
  20139. (interactive "P")
  20140. (org-with-limited-levels
  20141. (cond ((org-at-heading-p) (beginning-of-line))
  20142. ((org-before-first-heading-p) (user-error "Not in a subtree"))
  20143. (t (outline-previous-visible-heading 1))))
  20144. (when up (while (and (> up 0) (org-up-heading-safe)) (decf up)))
  20145. (if (org-called-interactively-p 'any)
  20146. (call-interactively 'org-mark-element)
  20147. (org-mark-element)))
  20148. ;;; Indentation
  20149. (defun org-indent-line ()
  20150. "Indent line depending on context."
  20151. (interactive)
  20152. (let* ((pos (point))
  20153. (itemp (org-at-item-p))
  20154. (case-fold-search t)
  20155. (org-drawer-regexp (or org-drawer-regexp "\000"))
  20156. (inline-task-p (and (featurep 'org-inlinetask)
  20157. (org-inlinetask-in-task-p)))
  20158. (inline-re (and inline-task-p
  20159. (org-inlinetask-outline-regexp)))
  20160. column)
  20161. (if (and orgstruct-is-++ (eq pos (point)))
  20162. (let ((indent-line-function (cadadr (assoc 'indent-line-function org-fb-vars))))
  20163. (indent-according-to-mode))
  20164. (beginning-of-line 1)
  20165. (cond
  20166. ;; Headings
  20167. ((looking-at org-outline-regexp) (setq column 0))
  20168. ;; Footnote definition
  20169. ((looking-at org-footnote-definition-re) (setq column 0))
  20170. ;; Literal examples
  20171. ((looking-at "[ \t]*:\\( \\|$\\)")
  20172. (setq column (org-get-indentation))) ; do nothing
  20173. ;; Lists
  20174. ((ignore-errors (goto-char (org-in-item-p)))
  20175. (setq column (if itemp
  20176. (org-get-indentation)
  20177. (org-list-item-body-column (point))))
  20178. (goto-char pos))
  20179. ;; Drawers
  20180. ((and (looking-at "[ \t]*:END:")
  20181. (save-excursion (re-search-backward org-drawer-regexp nil t)))
  20182. (save-excursion
  20183. (goto-char (1- (match-beginning 1)))
  20184. (setq column (current-column))))
  20185. ;; Special blocks
  20186. ((and (looking-at "[ \t]*#\\+end_\\([a-z]+\\)")
  20187. (save-excursion
  20188. (re-search-backward
  20189. (concat "^[ \t]*#\\+begin_" (downcase (match-string 1))) nil t)))
  20190. (setq column (org-get-indentation (match-string 0))))
  20191. ((and (not (looking-at "[ \t]*#\\+begin_"))
  20192. (org-between-regexps-p "^[ \t]*#\\+begin_" "[ \t]*#\\+end_"))
  20193. (save-excursion
  20194. (re-search-backward "^[ \t]*#\\+begin_\\([a-z]+\\)" nil t))
  20195. (setq column
  20196. (cond ((equal (downcase (match-string 1)) "src")
  20197. ;; src blocks: let `org-edit-src-exit' handle them
  20198. (org-get-indentation))
  20199. ((equal (downcase (match-string 1)) "example")
  20200. (max (org-get-indentation)
  20201. (org-get-indentation (match-string 0))))
  20202. (t
  20203. (org-get-indentation (match-string 0))))))
  20204. ;; This line has nothing special, look at the previous relevant
  20205. ;; line to compute indentation
  20206. (t
  20207. (beginning-of-line 0)
  20208. (while (and (not (bobp))
  20209. (not (looking-at org-table-line-regexp))
  20210. (not (looking-at org-drawer-regexp))
  20211. ;; When point started in an inline task, do not move
  20212. ;; above task starting line.
  20213. (not (and inline-task-p (looking-at inline-re)))
  20214. ;; Skip drawers, blocks, empty lines, verbatim,
  20215. ;; comments, tables, footnotes definitions, lists,
  20216. ;; inline tasks.
  20217. (or (and (looking-at "[ \t]*:END:")
  20218. (re-search-backward org-drawer-regexp nil t))
  20219. (and (looking-at "[ \t]*#\\+end_")
  20220. (re-search-backward "[ \t]*#\\+begin_"nil t))
  20221. (looking-at "[ \t]*[\n:#|]")
  20222. (looking-at org-footnote-definition-re)
  20223. (and (not inline-task-p)
  20224. (featurep 'org-inlinetask)
  20225. (org-inlinetask-in-task-p)
  20226. (or (org-inlinetask-goto-beginning) t))))
  20227. (beginning-of-line 0))
  20228. (cond
  20229. ;; There was a list item above.
  20230. ((save-excursion
  20231. (and (ignore-errors (goto-char (org-in-item-p)))
  20232. (goto-char
  20233. (org-list-get-top-point (org-list-struct)))))
  20234. (looking-at org-list-full-item-re)
  20235. (setq column (length (match-string 0))))
  20236. ;; There was an heading above.
  20237. ((looking-at "\\*+[ \t]+")
  20238. (if (not org-adapt-indentation)
  20239. (setq column 0)
  20240. (goto-char (match-end 0))
  20241. (setq column (current-column))))
  20242. ;; A drawer had started and is unfinished
  20243. ((looking-at org-drawer-regexp)
  20244. (goto-char (1- (match-beginning 1)))
  20245. (setq column (current-column)))
  20246. ;; Else, nothing noticeable found: get indentation and go on.
  20247. (t (setq column (org-get-indentation))))))
  20248. ;; Now apply indentation and move cursor accordingly
  20249. (goto-char pos)
  20250. (if (<= (current-column) (current-indentation))
  20251. (org-indent-line-to column)
  20252. (save-excursion (org-indent-line-to column)))
  20253. ;; Special polishing for properties, see `org-property-format'
  20254. (setq column (current-column))
  20255. (beginning-of-line 1)
  20256. (if (looking-at
  20257. "\\([ \t]*\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  20258. (replace-match (concat (match-string 1)
  20259. (format org-property-format
  20260. (match-string 2) (match-string 3)))
  20261. t t))
  20262. (org-move-to-column column))))
  20263. (defun org-indent-drawer ()
  20264. "Indent the drawer at point."
  20265. (interactive)
  20266. (let ((p (point))
  20267. (e (and (save-excursion (re-search-forward ":END:" nil t))
  20268. (match-end 0)))
  20269. (folded
  20270. (save-excursion
  20271. (end-of-line)
  20272. (when (overlays-at (point))
  20273. (member 'invisible (overlay-properties
  20274. (car (overlays-at (point)))))))))
  20275. (when folded (org-cycle))
  20276. (indent-for-tab-command)
  20277. (while (and (move-beginning-of-line 2) (< (point) e))
  20278. (indent-for-tab-command))
  20279. (goto-char p)
  20280. (when folded (org-cycle)))
  20281. (message "Drawer at point indented"))
  20282. (defun org-indent-block ()
  20283. "Indent the block at point."
  20284. (interactive)
  20285. (let ((p (point))
  20286. (case-fold-search t)
  20287. (e (and (save-excursion (re-search-forward "#\\+end_?\\(?:[a-z]+\\)?" nil t))
  20288. (match-end 0)))
  20289. (folded
  20290. (save-excursion
  20291. (end-of-line)
  20292. (when (overlays-at (point))
  20293. (member 'invisible (overlay-properties
  20294. (car (overlays-at (point)))))))))
  20295. (when folded (org-cycle))
  20296. (indent-for-tab-command)
  20297. (while (and (move-beginning-of-line 2) (< (point) e))
  20298. (indent-for-tab-command))
  20299. (goto-char p)
  20300. (when folded (org-cycle)))
  20301. (message "Block at point indented"))
  20302. (defun org-indent-region (start end)
  20303. "Indent region."
  20304. (interactive "r")
  20305. (save-excursion
  20306. (let ((line-end (org-current-line end)))
  20307. (goto-char start)
  20308. (while (< (org-current-line) line-end)
  20309. (cond ((org-in-src-block-p) (org-src-native-tab-command-maybe))
  20310. (t (call-interactively 'org-indent-line)))
  20311. (move-beginning-of-line 2)))))
  20312. ;;; Filling
  20313. ;; We use our own fill-paragraph and auto-fill functions.
  20314. ;; `org-fill-paragraph' relies on adaptive filling and context
  20315. ;; checking. Appropriate `fill-prefix' is computed with
  20316. ;; `org-adaptive-fill-function'.
  20317. ;; `org-auto-fill-function' takes care of auto-filling. It calls
  20318. ;; `do-auto-fill' only on valid areas with `fill-prefix' shadowed with
  20319. ;; `org-adaptive-fill-function' value. Internally,
  20320. ;; `org-comment-line-break-function' breaks the line.
  20321. ;; `org-setup-filling' installs filling and auto-filling related
  20322. ;; variables during `org-mode' initialization.
  20323. (defun org-setup-filling ()
  20324. (interactive)
  20325. ;; Prevent auto-fill from inserting unwanted new items.
  20326. (when (boundp 'fill-nobreak-predicate)
  20327. (org-set-local
  20328. 'fill-nobreak-predicate
  20329. (org-uniquify
  20330. (append fill-nobreak-predicate
  20331. '(org-fill-paragraph-separate-nobreak-p
  20332. org-fill-line-break-nobreak-p
  20333. org-fill-paragraph-with-timestamp-nobreak-p)))))
  20334. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  20335. (org-set-local 'auto-fill-inhibit-regexp nil)
  20336. (org-set-local 'adaptive-fill-function 'org-adaptive-fill-function)
  20337. (org-set-local 'normal-auto-fill-function 'org-auto-fill-function)
  20338. (org-set-local 'comment-line-break-function 'org-comment-line-break-function))
  20339. (defvar org-element-paragraph-separate) ; org-element.el
  20340. (defun org-fill-paragraph-separate-nobreak-p ()
  20341. "Non-nil when a line break at point would insert a new item."
  20342. (looking-at (substring org-element-paragraph-separate 1)))
  20343. (defun org-fill-line-break-nobreak-p ()
  20344. "Non-nil when a line break at point would create an Org line break."
  20345. (save-excursion
  20346. (skip-chars-backward "[ \t]")
  20347. (skip-chars-backward "\\\\")
  20348. (looking-at "\\\\\\\\\\($\\|[^\\\\]\\)")))
  20349. (defun org-fill-paragraph-with-timestamp-nobreak-p ()
  20350. "Non-nil when a line break at point would insert a new item."
  20351. (and (org-at-timestamp-p t)
  20352. (not (looking-at org-ts-regexp-both))))
  20353. (declare-function message-in-body-p "message" ())
  20354. (defvar orgtbl-line-start-regexp) ; From org-table.el
  20355. (defun org-adaptive-fill-function ()
  20356. "Compute a fill prefix for the current line.
  20357. Return fill prefix, as a string, or nil if current line isn't
  20358. meant to be filled."
  20359. (let (prefix)
  20360. (catch 'exit
  20361. (when (derived-mode-p 'message-mode)
  20362. (save-excursion
  20363. (beginning-of-line)
  20364. (cond ((or (not (message-in-body-p))
  20365. (looking-at orgtbl-line-start-regexp))
  20366. (throw 'exit nil))
  20367. ((looking-at message-cite-prefix-regexp)
  20368. (throw 'exit (match-string-no-properties 0)))
  20369. ((looking-at org-outline-regexp)
  20370. (throw 'exit (make-string (length (match-string 0)) ? ))))))
  20371. (org-with-wide-buffer
  20372. (let* ((p (line-beginning-position))
  20373. (element (save-excursion
  20374. (beginning-of-line)
  20375. (or (ignore-errors (org-element-at-point))
  20376. (user-error "An element cannot be parsed line %d"
  20377. (line-number-at-pos (point))))))
  20378. (type (org-element-type element))
  20379. (post-affiliated (org-element-property :post-affiliated element)))
  20380. (unless (and post-affiliated (< p post-affiliated))
  20381. (case type
  20382. (comment (looking-at "[ \t]*# ?") (match-string 0))
  20383. (footnote-definition "")
  20384. ((item plain-list)
  20385. (make-string (org-list-item-body-column
  20386. (or post-affiliated
  20387. (org-element-property :begin element)))
  20388. ? ))
  20389. (paragraph
  20390. ;; Fill prefix is usually the same as the current line,
  20391. ;; except if the paragraph is at the beginning of an item.
  20392. (let ((parent (org-element-property :parent element)))
  20393. (cond ((eq (org-element-type parent) 'item)
  20394. (make-string (org-list-item-body-column
  20395. (org-element-property :begin parent))
  20396. ? ))
  20397. ((save-excursion (beginning-of-line) (looking-at "[ \t]+"))
  20398. (match-string 0))
  20399. (t ""))))
  20400. (comment-block
  20401. ;; Only fill contents if P is within block boundaries.
  20402. (let* ((cbeg (save-excursion (goto-char post-affiliated)
  20403. (forward-line)
  20404. (point)))
  20405. (cend (save-excursion
  20406. (goto-char (org-element-property :end element))
  20407. (skip-chars-backward " \r\t\n")
  20408. (line-beginning-position))))
  20409. (when (and (>= p cbeg) (< p cend))
  20410. (if (save-excursion (beginning-of-line) (looking-at "[ \t]+"))
  20411. (match-string 0)
  20412. "")))))))))))
  20413. (declare-function message-goto-body "message" ())
  20414. (defvar message-cite-prefix-regexp) ; From message.el
  20415. (defun org-fill-paragraph (&optional justify)
  20416. "Fill element at point, when applicable.
  20417. This function only applies to comment blocks, comments, example
  20418. blocks and paragraphs. Also, as a special case, re-align table
  20419. when point is at one.
  20420. If JUSTIFY is non-nil (interactively, with prefix argument),
  20421. justify as well. If `sentence-end-double-space' is non-nil, then
  20422. period followed by one space does not end a sentence, so don't
  20423. break a line there. The variable `fill-column' controls the
  20424. width for filling.
  20425. For convenience, when point is at a plain list, an item or
  20426. a footnote definition, try to fill the first paragraph within."
  20427. (interactive)
  20428. (if (and (derived-mode-p 'message-mode)
  20429. (or (not (message-in-body-p))
  20430. (save-excursion (move-beginning-of-line 1)
  20431. (looking-at message-cite-prefix-regexp))))
  20432. ;; First ensure filling is correct in message-mode.
  20433. (let ((fill-paragraph-function
  20434. (cadadr (assoc 'fill-paragraph-function org-fb-vars)))
  20435. (fill-prefix (cadadr (assoc 'fill-prefix org-fb-vars)))
  20436. (paragraph-start (cadadr (assoc 'paragraph-start org-fb-vars)))
  20437. (paragraph-separate
  20438. (cadadr (assoc 'paragraph-separate org-fb-vars))))
  20439. (fill-paragraph nil))
  20440. (with-syntax-table org-mode-transpose-word-syntax-table
  20441. ;; Move to end of line in order to get the first paragraph
  20442. ;; within a plain list or a footnote definition.
  20443. (let ((element (save-excursion
  20444. (end-of-line)
  20445. (or (ignore-errors (org-element-at-point))
  20446. (user-error "An element cannot be parsed line %d"
  20447. (line-number-at-pos (point)))))))
  20448. ;; First check if point is in a blank line at the beginning of
  20449. ;; the buffer. In that case, ignore filling.
  20450. (case (org-element-type element)
  20451. ;; Use major mode filling function is src blocks.
  20452. (src-block (org-babel-do-key-sequence-in-edit-buffer (kbd "M-q")))
  20453. ;; Align Org tables, leave table.el tables as-is.
  20454. (table-row (org-table-align) t)
  20455. (table
  20456. (when (eq (org-element-property :type element) 'org)
  20457. (save-excursion
  20458. (goto-char (org-element-property :post-affiliated element))
  20459. (org-table-align)))
  20460. t)
  20461. (paragraph
  20462. ;; Paragraphs may contain `line-break' type objects.
  20463. (let ((beg (max (point-min)
  20464. (org-element-property :contents-begin element)))
  20465. (end (min (point-max)
  20466. (org-element-property :contents-end element))))
  20467. ;; Do nothing if point is at an affiliated keyword.
  20468. (if (< (line-end-position) beg) t
  20469. (when (derived-mode-p 'message-mode)
  20470. ;; In `message-mode', do not fill following citation
  20471. ;; in current paragraph nor text before message body.
  20472. (let ((body-start (save-excursion (message-goto-body))))
  20473. (when body-start (setq beg (max body-start beg))))
  20474. (when (save-excursion
  20475. (re-search-forward
  20476. (concat "^" message-cite-prefix-regexp) end t))
  20477. (setq end (match-beginning 0))))
  20478. ;; Fill paragraph, taking line breaks into account.
  20479. ;; For that, slice the paragraph using line breaks as
  20480. ;; separators, and fill the parts in reverse order to
  20481. ;; avoid messing with markers.
  20482. (save-excursion
  20483. (goto-char end)
  20484. (mapc
  20485. (lambda (pos)
  20486. (fill-region-as-paragraph pos (point) justify)
  20487. (goto-char pos))
  20488. ;; Find the list of ending positions for line breaks
  20489. ;; in the current paragraph. Add paragraph
  20490. ;; beginning to include first slice.
  20491. (nreverse
  20492. (cons beg
  20493. (org-element-map
  20494. (org-element--parse-objects
  20495. beg end nil (org-element-restriction 'paragraph))
  20496. 'line-break
  20497. (lambda (lb) (org-element-property :end lb)))))))
  20498. t)))
  20499. ;; Contents of `comment-block' type elements should be
  20500. ;; filled as plain text, but only if point is within block
  20501. ;; markers.
  20502. (comment-block
  20503. (let* ((case-fold-search t)
  20504. (beg (save-excursion
  20505. (goto-char (org-element-property :begin element))
  20506. (re-search-forward "^[ \t]*#\\+begin_comment" nil t)
  20507. (forward-line)
  20508. (point)))
  20509. (end (save-excursion
  20510. (goto-char (org-element-property :end element))
  20511. (re-search-backward "^[ \t]*#\\+end_comment" nil t)
  20512. (line-beginning-position))))
  20513. (when (and (>= (point) beg) (< (point) end))
  20514. (fill-region-as-paragraph
  20515. (save-excursion
  20516. (end-of-line)
  20517. (re-search-backward "^[ \t]*$" beg 'move)
  20518. (line-beginning-position))
  20519. (save-excursion
  20520. (beginning-of-line)
  20521. (re-search-forward "^[ \t]*$" end 'move)
  20522. (line-beginning-position))
  20523. justify)))
  20524. t)
  20525. ;; Fill comments.
  20526. (comment (fill-comment-paragraph justify))
  20527. ;; Ignore every other element.
  20528. (otherwise t))))))
  20529. (defun org-auto-fill-function ()
  20530. "Auto-fill function."
  20531. ;; Check if auto-filling is meaningful.
  20532. (let ((fc (current-fill-column)))
  20533. (when (and fc (> (current-column) fc))
  20534. (let* ((fill-prefix (org-adaptive-fill-function))
  20535. ;; Enforce empty fill prefix, if required. Otherwise, it
  20536. ;; will be computed again.
  20537. (adaptive-fill-mode (not (equal fill-prefix ""))))
  20538. (when fill-prefix (do-auto-fill))))))
  20539. (defun org-comment-line-break-function (&optional soft)
  20540. "Break line at point and indent, continuing comment if within one.
  20541. The inserted newline is marked hard if variable
  20542. `use-hard-newlines' is true, unless optional argument SOFT is
  20543. non-nil."
  20544. (if soft (insert-and-inherit ?\n) (newline 1))
  20545. (save-excursion (forward-char -1) (delete-horizontal-space))
  20546. (delete-horizontal-space)
  20547. (indent-to-left-margin)
  20548. (insert-before-markers-and-inherit fill-prefix))
  20549. ;;; Comments
  20550. ;; Org comments syntax is quite complex. It requires the entire line
  20551. ;; to be just a comment. Also, even with the right syntax at the
  20552. ;; beginning of line, some some elements (i.e. verse-block or
  20553. ;; example-block) don't accept comments. Usual Emacs comment commands
  20554. ;; cannot cope with those requirements. Therefore, Org replaces them.
  20555. ;; Org still relies on `comment-dwim', but cannot trust
  20556. ;; `comment-only-p'. So, `comment-region-function' and
  20557. ;; `uncomment-region-function' both point
  20558. ;; to`org-comment-or-uncomment-region'. Eventually,
  20559. ;; `org-insert-comment' takes care of insertion of comments at the
  20560. ;; beginning of line.
  20561. ;; `org-setup-comments-handling' install comments related variables
  20562. ;; during `org-mode' initialization.
  20563. (defun org-setup-comments-handling ()
  20564. (interactive)
  20565. (org-set-local 'comment-use-syntax nil)
  20566. (org-set-local 'comment-start "# ")
  20567. (org-set-local 'comment-start-skip "^\\s-*#\\(?: \\|$\\)")
  20568. (org-set-local 'comment-insert-comment-function 'org-insert-comment)
  20569. (org-set-local 'comment-region-function 'org-comment-or-uncomment-region)
  20570. (org-set-local 'uncomment-region-function 'org-comment-or-uncomment-region))
  20571. (defun org-insert-comment ()
  20572. "Insert an empty comment above current line.
  20573. If the line is empty, insert comment at its beginning."
  20574. (beginning-of-line)
  20575. (if (looking-at "\\s-*$") (replace-match "") (open-line 1))
  20576. (org-indent-line)
  20577. (insert "# "))
  20578. (defvar comment-empty-lines) ; From newcomment.el.
  20579. (defun org-comment-or-uncomment-region (beg end &rest ignore)
  20580. "Comment or uncomment each non-blank line in the region.
  20581. Uncomment each non-blank line between BEG and END if it only
  20582. contains commented lines. Otherwise, comment them."
  20583. (save-restriction
  20584. ;; Restrict region
  20585. (narrow-to-region (save-excursion (goto-char beg)
  20586. (skip-chars-forward " \r\t\n" end)
  20587. (line-beginning-position))
  20588. (save-excursion (goto-char end)
  20589. (skip-chars-backward " \r\t\n" beg)
  20590. (line-end-position)))
  20591. (let ((uncommentp
  20592. ;; UNCOMMENTP is non-nil when every non blank line between
  20593. ;; BEG and END is a comment.
  20594. (save-excursion
  20595. (goto-char (point-min))
  20596. (while (and (not (eobp))
  20597. (let ((element (org-element-at-point)))
  20598. (and (eq (org-element-type element) 'comment)
  20599. (goto-char (min (point-max)
  20600. (org-element-property
  20601. :end element)))))))
  20602. (eobp))))
  20603. (if uncommentp
  20604. ;; Only blank lines and comments in region: uncomment it.
  20605. (save-excursion
  20606. (goto-char (point-min))
  20607. (while (not (eobp))
  20608. (when (looking-at "[ \t]*\\(#\\(?: \\|$\\)\\)")
  20609. (replace-match "" nil nil nil 1))
  20610. (forward-line)))
  20611. ;; Comment each line in region.
  20612. (let ((min-indent (point-max)))
  20613. ;; First find the minimum indentation across all lines.
  20614. (save-excursion
  20615. (goto-char (point-min))
  20616. (while (and (not (eobp)) (not (zerop min-indent)))
  20617. (unless (looking-at "[ \t]*$")
  20618. (setq min-indent (min min-indent (current-indentation))))
  20619. (forward-line)))
  20620. ;; Then loop over all lines.
  20621. (save-excursion
  20622. (goto-char (point-min))
  20623. (while (not (eobp))
  20624. (unless (and (not comment-empty-lines) (looking-at "[ \t]*$"))
  20625. ;; Don't get fooled by invisible text (e.g. link path)
  20626. ;; when moving to column MIN-INDENT.
  20627. (let ((buffer-invisibility-spec nil))
  20628. (org-move-to-column min-indent t))
  20629. (insert comment-start))
  20630. (forward-line))))))))
  20631. ;;; Planning
  20632. ;; This section contains tools to operate on timestamp objects, as
  20633. ;; returned by, e.g. `org-element-context'.
  20634. (defun org-timestamp-has-time-p (timestamp)
  20635. "Non-nil when TIMESTAMP has a time specified."
  20636. (org-element-property :hour-start timestamp))
  20637. (defun org-timestamp-format (timestamp format &optional end utc)
  20638. "Format a TIMESTAMP element into a string.
  20639. FORMAT is a format specifier to be passed to
  20640. `format-time-string'.
  20641. When optional argument END is non-nil, use end of date-range or
  20642. time-range, if possible.
  20643. When optional argument UTC is non-nil, time will be expressed as
  20644. Universal Time."
  20645. (format-time-string
  20646. format
  20647. (apply 'encode-time
  20648. (cons 0
  20649. (mapcar
  20650. (lambda (prop) (or (org-element-property prop timestamp) 0))
  20651. (if end '(:minute-end :hour-end :day-end :month-end :year-end)
  20652. '(:minute-start :hour-start :day-start :month-start
  20653. :year-start)))))
  20654. utc))
  20655. (defun org-timestamp-split-range (timestamp &optional end)
  20656. "Extract a timestamp object from a date or time range.
  20657. TIMESTAMP is a timestamp object. END, when non-nil, means extract
  20658. the end of the range. Otherwise, extract its start.
  20659. Return a new timestamp object sharing the same parent as
  20660. TIMESTAMP."
  20661. (let ((type (org-element-property :type timestamp)))
  20662. (if (memq type '(active inactive diary)) timestamp
  20663. (let ((split-ts (list 'timestamp (copy-sequence (nth 1 timestamp)))))
  20664. ;; Set new type.
  20665. (org-element-put-property
  20666. split-ts :type (if (eq type 'active-range) 'active 'inactive))
  20667. ;; Copy start properties over end properties if END is
  20668. ;; non-nil. Otherwise, copy end properties over `start' ones.
  20669. (let ((p-alist '((:minute-start . :minute-end)
  20670. (:hour-start . :hour-end)
  20671. (:day-start . :day-end)
  20672. (:month-start . :month-end)
  20673. (:year-start . :year-end))))
  20674. (dolist (p-cell p-alist)
  20675. (org-element-put-property
  20676. split-ts
  20677. (funcall (if end 'car 'cdr) p-cell)
  20678. (org-element-property
  20679. (funcall (if end 'cdr 'car) p-cell) split-ts)))
  20680. ;; Eventually refresh `:raw-value'.
  20681. (org-element-put-property split-ts :raw-value nil)
  20682. (org-element-put-property
  20683. split-ts :raw-value (org-element-interpret-data split-ts)))))))
  20684. (defun org-timestamp-translate (timestamp &optional boundary)
  20685. "Apply `org-translate-time' on a TIMESTAMP object.
  20686. When optional argument BOUNDARY is non-nil, it is either the
  20687. symbol `start' or `end'. In this case, only translate the
  20688. starting or ending part of TIMESTAMP if it is a date or time
  20689. range. Otherwise, translate both parts."
  20690. (if (and (not boundary)
  20691. (memq (org-element-property :type timestamp)
  20692. '(active-range inactive-range)))
  20693. (concat
  20694. (org-translate-time
  20695. (org-element-property :raw-value
  20696. (org-timestamp-split-range timestamp)))
  20697. "--"
  20698. (org-translate-time
  20699. (org-element-property :raw-value
  20700. (org-timestamp-split-range timestamp t))))
  20701. (org-translate-time
  20702. (org-element-property
  20703. :raw-value
  20704. (if (not boundary) timestamp
  20705. (org-timestamp-split-range timestamp (eq boundary 'end)))))))
  20706. ;;; Other stuff.
  20707. (defun org-toggle-fixed-width-section (arg)
  20708. "Toggle the fixed-width export.
  20709. If there is no active region, the QUOTE keyword at the current headline is
  20710. inserted or removed. When present, it causes the text between this headline
  20711. and the next to be exported as fixed-width text, and unmodified.
  20712. If there is an active region, this command adds or removes a colon as the
  20713. first character of this line. If the first character of a line is a colon,
  20714. this line is also exported in fixed-width font."
  20715. (interactive "P")
  20716. (let* ((cc 0)
  20717. (regionp (org-region-active-p))
  20718. (beg (if regionp (region-beginning) (point)))
  20719. (end (if regionp (region-end)))
  20720. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  20721. (case-fold-search nil)
  20722. (re "[ \t]*\\(:\\(?: \\|$\\)\\)")
  20723. off)
  20724. (if regionp
  20725. (save-excursion
  20726. (goto-char beg)
  20727. (setq cc (current-column))
  20728. (beginning-of-line 1)
  20729. (setq off (looking-at re))
  20730. (while (> nlines 0)
  20731. (setq nlines (1- nlines))
  20732. (beginning-of-line 1)
  20733. (cond
  20734. (arg
  20735. (org-move-to-column cc t)
  20736. (insert ": \n")
  20737. (forward-line -1))
  20738. ((and off (looking-at re))
  20739. (replace-match "" t t nil 1))
  20740. ((not off) (org-move-to-column cc t) (insert ": ")))
  20741. (forward-line 1)))
  20742. (save-excursion
  20743. (org-back-to-heading)
  20744. (cond
  20745. ((looking-at (format org-heading-keyword-regexp-format
  20746. org-quote-string))
  20747. (goto-char (match-end 1))
  20748. (looking-at (concat " +" org-quote-string))
  20749. (replace-match "" t t)
  20750. (when (eolp) (insert " ")))
  20751. ((looking-at org-outline-regexp)
  20752. (goto-char (match-end 0))
  20753. (insert org-quote-string " ")))))))
  20754. (defun org-reftex-citation ()
  20755. "Use reftex-citation to insert a citation into the buffer.
  20756. This looks for a line like
  20757. #+BIBLIOGRAPHY: foo plain option:-d
  20758. and derives from it that foo.bib is the bibliography file relevant
  20759. for this document. It then installs the necessary environment for RefTeX
  20760. to work in this buffer and calls `reftex-citation' to insert a citation
  20761. into the buffer.
  20762. Export of such citations to both LaTeX and HTML is handled by the contributed
  20763. package org-exp-bibtex by Taru Karttunen."
  20764. (interactive)
  20765. (let ((reftex-docstruct-symbol 'rds)
  20766. (reftex-cite-format "\\cite{%l}")
  20767. rds bib)
  20768. (save-excursion
  20769. (save-restriction
  20770. (widen)
  20771. (let ((case-fold-search t)
  20772. (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
  20773. (if (not (save-excursion
  20774. (or (re-search-forward re nil t)
  20775. (re-search-backward re nil t))))
  20776. (error "No bibliography defined in file")
  20777. (setq bib (concat (match-string 1) ".bib")
  20778. rds (list (list 'bib bib)))))))
  20779. (call-interactively 'reftex-citation)))
  20780. ;;;; Functions extending outline functionality
  20781. (defun org-beginning-of-line (&optional arg)
  20782. "Go to the beginning of the current line. If that is invisible, continue
  20783. to a visible line beginning. This makes the function of C-a more intuitive.
  20784. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  20785. first attempt, and only move to after the tags when the cursor is already
  20786. beyond the end of the headline."
  20787. (interactive "P")
  20788. (let ((pos (point))
  20789. (special (if (consp org-special-ctrl-a/e)
  20790. (car org-special-ctrl-a/e)
  20791. org-special-ctrl-a/e))
  20792. refpos)
  20793. (if (org-bound-and-true-p visual-line-mode)
  20794. (beginning-of-visual-line 1)
  20795. (beginning-of-line 1))
  20796. (if (and arg (fboundp 'move-beginning-of-line))
  20797. (call-interactively 'move-beginning-of-line)
  20798. (if (bobp)
  20799. nil
  20800. (backward-char 1)
  20801. (if (org-truely-invisible-p)
  20802. (while (and (not (bobp)) (org-truely-invisible-p))
  20803. (backward-char 1)
  20804. (beginning-of-line 1))
  20805. (forward-char 1))))
  20806. (when special
  20807. (cond
  20808. ((and (looking-at org-complex-heading-regexp)
  20809. (= (char-after (match-end 1)) ?\ ))
  20810. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  20811. (point-at-eol)))
  20812. (goto-char
  20813. (if (eq special t)
  20814. (cond ((> pos refpos) refpos)
  20815. ((= pos (point)) refpos)
  20816. (t (point)))
  20817. (cond ((> pos (point)) (point))
  20818. ((not (eq last-command this-command)) (point))
  20819. (t refpos)))))
  20820. ((org-at-item-p)
  20821. ;; Being at an item and not looking at an the item means point
  20822. ;; was previously moved to beginning of a visual line, which
  20823. ;; doesn't contain the item. Therefore, do nothing special,
  20824. ;; just stay here.
  20825. (when (looking-at org-list-full-item-re)
  20826. ;; Set special position at first white space character after
  20827. ;; bullet, and check-box, if any.
  20828. (let ((after-bullet
  20829. (let ((box (match-end 3)))
  20830. (if (not box) (match-end 1)
  20831. (let ((after (char-after box)))
  20832. (if (and after (= after ? )) (1+ box) box))))))
  20833. ;; Special case: Move point to special position when
  20834. ;; currently after it or at beginning of line.
  20835. (if (eq special t)
  20836. (when (or (> pos after-bullet) (= (point) pos))
  20837. (goto-char after-bullet))
  20838. ;; Reversed case: Move point to special position when
  20839. ;; point was already at beginning of line and command is
  20840. ;; repeated.
  20841. (when (and (= (point) pos) (eq last-command this-command))
  20842. (goto-char after-bullet))))))))
  20843. (org-no-warnings
  20844. (and (featurep 'xemacs) (setq zmacs-region-stays t))))
  20845. (setq disable-point-adjustment
  20846. (or (not (invisible-p (point)))
  20847. (not (invisible-p (max (point-min) (1- (point))))))))
  20848. (defun org-end-of-line (&optional arg)
  20849. "Go to the end of the line.
  20850. If this is a headline, and `org-special-ctrl-a/e' is set, ignore
  20851. tags on the first attempt, and only move to after the tags when
  20852. the cursor is already beyond the end of the headline."
  20853. (interactive "P")
  20854. (let ((special (if (consp org-special-ctrl-a/e) (cdr org-special-ctrl-a/e)
  20855. org-special-ctrl-a/e))
  20856. (move-fun (cond ((org-bound-and-true-p visual-line-mode)
  20857. 'end-of-visual-line)
  20858. ((fboundp 'move-end-of-line) 'move-end-of-line)
  20859. (t 'end-of-line))))
  20860. (if (or (not special) arg) (call-interactively move-fun)
  20861. (let* ((element (save-excursion (beginning-of-line)
  20862. (org-element-at-point)))
  20863. (type (org-element-type element)))
  20864. (cond
  20865. ((memq type '(headline inlinetask))
  20866. (let ((pos (point)))
  20867. (beginning-of-line 1)
  20868. (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*\\)?$"))
  20869. (if (eq special t)
  20870. (if (or (< pos (match-beginning 1)) (= pos (match-end 0)))
  20871. (goto-char (match-beginning 1))
  20872. (goto-char (match-end 0)))
  20873. (if (or (< pos (match-end 0))
  20874. (not (eq this-command last-command)))
  20875. (goto-char (match-end 0))
  20876. (goto-char (match-beginning 1))))
  20877. (call-interactively move-fun))))
  20878. ((org-element-property :hiddenp element)
  20879. ;; If element is hidden, `move-end-of-line' would put point
  20880. ;; after it. Use `end-of-line' to stay on current line.
  20881. (call-interactively 'end-of-line))
  20882. (t (call-interactively move-fun)))))
  20883. (org-no-warnings (and (featurep 'xemacs) (setq zmacs-region-stays t))))
  20884. (setq disable-point-adjustment
  20885. (or (not (invisible-p (point)))
  20886. (not (invisible-p (max (point-min) (1- (point))))))))
  20887. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  20888. (define-key org-mode-map "\C-e" 'org-end-of-line)
  20889. (defun org-backward-sentence (&optional arg)
  20890. "Go to beginning of sentence, or beginning of table field.
  20891. This will call `backward-sentence' or `org-table-beginning-of-field',
  20892. depending on context."
  20893. (interactive "P")
  20894. (cond
  20895. ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
  20896. (t (call-interactively 'backward-sentence))))
  20897. (defun org-forward-sentence (&optional arg)
  20898. "Go to end of sentence, or end of table field.
  20899. This will call `forward-sentence' or `org-table-end-of-field',
  20900. depending on context."
  20901. (interactive "P")
  20902. (cond
  20903. ((org-at-table-p) (call-interactively 'org-table-end-of-field))
  20904. (t (call-interactively 'forward-sentence))))
  20905. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  20906. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  20907. (defun org-kill-line (&optional arg)
  20908. "Kill line, to tags or end of line."
  20909. (interactive "P")
  20910. (cond
  20911. ((or (not org-special-ctrl-k)
  20912. (bolp)
  20913. (not (org-at-heading-p)))
  20914. (if (and (get-char-property (min (point-max) (point-at-eol)) 'invisible)
  20915. org-ctrl-k-protect-subtree)
  20916. (if (or (eq org-ctrl-k-protect-subtree 'error)
  20917. (not (y-or-n-p "Kill hidden subtree along with headline? ")))
  20918. (user-error "C-k aborted as it would kill a hidden subtree")))
  20919. (call-interactively
  20920. (if (org-bound-and-true-p visual-line-mode) 'kill-visual-line 'kill-line)))
  20921. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$"))
  20922. (kill-region (point) (match-beginning 1))
  20923. (org-set-tags nil t))
  20924. (t (kill-region (point) (point-at-eol)))))
  20925. (define-key org-mode-map "\C-k" 'org-kill-line)
  20926. (defun org-yank (&optional arg)
  20927. "Yank. If the kill is a subtree, treat it specially.
  20928. This command will look at the current kill and check if is a single
  20929. subtree, or a series of subtrees[1]. If it passes the test, and if the
  20930. cursor is at the beginning of a line or after the stars of a currently
  20931. empty headline, then the yank is handled specially. How exactly depends
  20932. on the value of the following variables, both set by default.
  20933. org-yank-folded-subtrees
  20934. When set, the subtree(s) will be folded after insertion, but only
  20935. if doing so would now swallow text after the yanked text.
  20936. org-yank-adjusted-subtrees
  20937. When set, the subtree will be promoted or demoted in order to
  20938. fit into the local outline tree structure, which means that the level
  20939. will be adjusted so that it becomes the smaller one of the two
  20940. *visible* surrounding headings.
  20941. Any prefix to this command will cause `yank' to be called directly with
  20942. no special treatment. In particular, a simple \\[universal-argument] prefix \
  20943. will just
  20944. plainly yank the text as it is.
  20945. \[1] The test checks if the first non-white line is a heading
  20946. and if there are no other headings with fewer stars."
  20947. (interactive "P")
  20948. (org-yank-generic 'yank arg))
  20949. (defun org-yank-generic (command arg)
  20950. "Perform some yank-like command.
  20951. This function implements the behavior described in the `org-yank'
  20952. documentation. However, it has been generalized to work for any
  20953. interactive command with similar behavior."
  20954. ;; pretend to be command COMMAND
  20955. (setq this-command command)
  20956. (if arg
  20957. (call-interactively command)
  20958. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  20959. (and (org-kill-is-subtree-p)
  20960. (or (bolp)
  20961. (and (looking-at "[ \t]*$")
  20962. (string-match
  20963. "\\`\\*+\\'"
  20964. (buffer-substring (point-at-bol) (point)))))))
  20965. swallowp)
  20966. (cond
  20967. ((and subtreep org-yank-folded-subtrees)
  20968. (let ((beg (point))
  20969. end)
  20970. (if (and subtreep org-yank-adjusted-subtrees)
  20971. (org-paste-subtree nil nil 'for-yank)
  20972. (call-interactively command))
  20973. (setq end (point))
  20974. (goto-char beg)
  20975. (when (and (bolp) subtreep
  20976. (not (setq swallowp
  20977. (org-yank-folding-would-swallow-text beg end))))
  20978. (org-with-limited-levels
  20979. (or (looking-at org-outline-regexp)
  20980. (re-search-forward org-outline-regexp-bol end t))
  20981. (while (and (< (point) end) (looking-at org-outline-regexp))
  20982. (hide-subtree)
  20983. (org-cycle-show-empty-lines 'folded)
  20984. (condition-case nil
  20985. (outline-forward-same-level 1)
  20986. (error (goto-char end))))))
  20987. (when swallowp
  20988. (message
  20989. "Inserted text not folded because that would swallow text"))
  20990. (goto-char end)
  20991. (skip-chars-forward " \t\n\r")
  20992. (beginning-of-line 1)
  20993. (push-mark beg 'nomsg)))
  20994. ((and subtreep org-yank-adjusted-subtrees)
  20995. (let ((beg (point-at-bol)))
  20996. (org-paste-subtree nil nil 'for-yank)
  20997. (push-mark beg 'nomsg)))
  20998. (t
  20999. (call-interactively command))))))
  21000. (defun org-yank-folding-would-swallow-text (beg end)
  21001. "Would hide-subtree at BEG swallow any text after END?"
  21002. (let (level)
  21003. (org-with-limited-levels
  21004. (save-excursion
  21005. (goto-char beg)
  21006. (when (or (looking-at org-outline-regexp)
  21007. (re-search-forward org-outline-regexp-bol end t))
  21008. (setq level (org-outline-level)))
  21009. (goto-char end)
  21010. (skip-chars-forward " \t\r\n\v\f")
  21011. (if (or (eobp)
  21012. (and (bolp) (looking-at org-outline-regexp)
  21013. (<= (org-outline-level) level)))
  21014. nil ; Nothing would be swallowed
  21015. t))))) ; something would swallow
  21016. (define-key org-mode-map "\C-y" 'org-yank)
  21017. (defun org-truely-invisible-p ()
  21018. "Check if point is at a character currently not visible.
  21019. This version does not only check the character property, but also
  21020. `visible-mode'."
  21021. ;; Early versions of noutline don't have `outline-invisible-p'.
  21022. (if (org-bound-and-true-p visible-mode)
  21023. nil
  21024. (outline-invisible-p)))
  21025. (defun org-invisible-p2 ()
  21026. "Check if point is at a character currently not visible."
  21027. (save-excursion
  21028. (if (and (eolp) (not (bobp))) (backward-char 1))
  21029. ;; Early versions of noutline don't have `outline-invisible-p'.
  21030. (outline-invisible-p)))
  21031. (defun org-back-to-heading (&optional invisible-ok)
  21032. "Call `outline-back-to-heading', but provide a better error message."
  21033. (condition-case nil
  21034. (outline-back-to-heading invisible-ok)
  21035. (error (error "Before first headline at position %d in buffer %s"
  21036. (point) (current-buffer)))))
  21037. (defun org-before-first-heading-p ()
  21038. "Before first heading?"
  21039. (save-excursion
  21040. (end-of-line)
  21041. (null (re-search-backward org-outline-regexp-bol nil t))))
  21042. (defun org-at-heading-p (&optional ignored)
  21043. (outline-on-heading-p t))
  21044. ;; Compatibility alias with Org versions < 7.8.03
  21045. (defalias 'org-on-heading-p 'org-at-heading-p)
  21046. (defun org-at-comment-p nil
  21047. "Is cursor in a line starting with a # character?"
  21048. (save-excursion
  21049. (beginning-of-line)
  21050. (looking-at "^#")))
  21051. (defun org-at-drawer-p nil
  21052. "Is cursor at a drawer keyword?"
  21053. (save-excursion
  21054. (move-beginning-of-line 1)
  21055. (looking-at org-drawer-regexp)))
  21056. (defun org-at-block-p nil
  21057. "Is cursor at a block keyword?"
  21058. (save-excursion
  21059. (move-beginning-of-line 1)
  21060. (looking-at org-block-regexp)))
  21061. (defun org-point-at-end-of-empty-headline ()
  21062. "If point is at the end of an empty headline, return t, else nil.
  21063. If the heading only contains a TODO keyword, it is still still considered
  21064. empty."
  21065. (and (looking-at "[ \t]*$")
  21066. (when org-todo-line-regexp
  21067. (save-excursion
  21068. (beginning-of-line 1)
  21069. (let ((case-fold-search nil))
  21070. (looking-at org-todo-line-regexp)
  21071. (string= (match-string 3) ""))))))
  21072. (defun org-at-heading-or-item-p ()
  21073. (or (org-at-heading-p) (org-at-item-p)))
  21074. (defun org-at-target-p ()
  21075. (or (org-in-regexp org-radio-target-regexp)
  21076. (org-in-regexp org-target-regexp)))
  21077. ;; Compatibility alias with Org versions < 7.8.03
  21078. (defalias 'org-on-target-p 'org-at-target-p)
  21079. (defun org-up-heading-all (arg)
  21080. "Move to the heading line of which the present line is a subheading.
  21081. This function considers both visible and invisible heading lines.
  21082. With argument, move up ARG levels."
  21083. (if (fboundp 'outline-up-heading-all)
  21084. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  21085. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  21086. (defun org-up-heading-safe ()
  21087. "Move to the heading line of which the present line is a subheading.
  21088. This version will not throw an error. It will return the level of the
  21089. headline found, or nil if no higher level is found.
  21090. Also, this function will be a lot faster than `outline-up-heading',
  21091. because it relies on stars being the outline starters. This can really
  21092. make a significant difference in outlines with very many siblings."
  21093. (let (start-level re)
  21094. (org-back-to-heading t)
  21095. (setq start-level (funcall outline-level))
  21096. (if (equal start-level 1)
  21097. nil
  21098. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  21099. (if (re-search-backward re nil t)
  21100. (funcall outline-level)))))
  21101. (defun org-first-sibling-p ()
  21102. "Is this heading the first child of its parents?"
  21103. (interactive)
  21104. (let ((re org-outline-regexp-bol)
  21105. level l)
  21106. (unless (org-at-heading-p t)
  21107. (user-error "Not at a heading"))
  21108. (setq level (funcall outline-level))
  21109. (save-excursion
  21110. (if (not (re-search-backward re nil t))
  21111. t
  21112. (setq l (funcall outline-level))
  21113. (< l level)))))
  21114. (defun org-goto-sibling (&optional previous)
  21115. "Goto the next sibling, even if it is invisible.
  21116. When PREVIOUS is set, go to the previous sibling instead. Returns t
  21117. when a sibling was found. When none is found, return nil and don't
  21118. move point."
  21119. (let ((fun (if previous 're-search-backward 're-search-forward))
  21120. (pos (point))
  21121. (re org-outline-regexp-bol)
  21122. level l)
  21123. (when (condition-case nil (org-back-to-heading t) (error nil))
  21124. (setq level (funcall outline-level))
  21125. (catch 'exit
  21126. (or previous (forward-char 1))
  21127. (while (funcall fun re nil t)
  21128. (setq l (funcall outline-level))
  21129. (when (< l level) (goto-char pos) (throw 'exit nil))
  21130. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  21131. (goto-char pos)
  21132. nil))))
  21133. (defun org-show-siblings ()
  21134. "Show all siblings of the current headline."
  21135. (save-excursion
  21136. (while (org-goto-sibling) (org-flag-heading nil)))
  21137. (save-excursion
  21138. (while (org-goto-sibling 'previous)
  21139. (org-flag-heading nil))))
  21140. (defun org-goto-first-child ()
  21141. "Goto the first child, even if it is invisible.
  21142. Return t when a child was found. Otherwise don't move point and
  21143. return nil."
  21144. (let (level (pos (point)) (re org-outline-regexp-bol))
  21145. (when (condition-case nil (org-back-to-heading t) (error nil))
  21146. (setq level (outline-level))
  21147. (forward-char 1)
  21148. (if (and (re-search-forward re nil t) (> (outline-level) level))
  21149. (progn (goto-char (match-beginning 0)) t)
  21150. (goto-char pos) nil))))
  21151. (defun org-show-hidden-entry ()
  21152. "Show an entry where even the heading is hidden."
  21153. (save-excursion
  21154. (org-show-entry)))
  21155. (defun org-flag-heading (flag &optional entry)
  21156. "Flag the current heading. FLAG non-nil means make invisible.
  21157. When ENTRY is non-nil, show the entire entry."
  21158. (save-excursion
  21159. (org-back-to-heading t)
  21160. ;; Check if we should show the entire entry
  21161. (if entry
  21162. (progn
  21163. (org-show-entry)
  21164. (save-excursion
  21165. (and (outline-next-heading)
  21166. (org-flag-heading nil))))
  21167. (outline-flag-region (max (point-min) (1- (point)))
  21168. (save-excursion (outline-end-of-heading) (point))
  21169. flag))))
  21170. (defun org-get-next-sibling ()
  21171. "Move to next heading of the same level, and return point.
  21172. If there is no such heading, return nil.
  21173. This is like outline-next-sibling, but invisible headings are ok."
  21174. (let ((level (funcall outline-level)))
  21175. (outline-next-heading)
  21176. (while (and (not (eobp)) (> (funcall outline-level) level))
  21177. (outline-next-heading))
  21178. (if (or (eobp) (< (funcall outline-level) level))
  21179. nil
  21180. (point))))
  21181. (defun org-get-last-sibling ()
  21182. "Move to previous heading of the same level, and return point.
  21183. If there is no such heading, return nil."
  21184. (let ((opoint (point))
  21185. (level (funcall outline-level)))
  21186. (outline-previous-heading)
  21187. (when (and (/= (point) opoint) (outline-on-heading-p t))
  21188. (while (and (> (funcall outline-level) level)
  21189. (not (bobp)))
  21190. (outline-previous-heading))
  21191. (if (< (funcall outline-level) level)
  21192. nil
  21193. (point)))))
  21194. (defun org-end-of-subtree (&optional invisible-ok to-heading)
  21195. "Goto to the end of a subtree."
  21196. ;; This contains an exact copy of the original function, but it uses
  21197. ;; `org-back-to-heading', to make it work also in invisible
  21198. ;; trees. And is uses an invisible-ok argument.
  21199. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  21200. ;; Furthermore, when used inside Org, finding the end of a large subtree
  21201. ;; with many children and grandchildren etc, this can be much faster
  21202. ;; than the outline version.
  21203. (org-back-to-heading invisible-ok)
  21204. (let ((first t)
  21205. (level (funcall outline-level)))
  21206. (if (and (derived-mode-p 'org-mode) (< level 1000))
  21207. ;; A true heading (not a plain list item), in Org-mode
  21208. ;; This means we can easily find the end by looking
  21209. ;; only for the right number of stars. Using a regexp to do
  21210. ;; this is so much faster than using a Lisp loop.
  21211. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  21212. (forward-char 1)
  21213. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  21214. ;; something else, do it the slow way
  21215. (while (and (not (eobp))
  21216. (or first (> (funcall outline-level) level)))
  21217. (setq first nil)
  21218. (outline-next-heading)))
  21219. (unless to-heading
  21220. (if (memq (preceding-char) '(?\n ?\^M))
  21221. (progn
  21222. ;; Go to end of line before heading
  21223. (forward-char -1)
  21224. (if (memq (preceding-char) '(?\n ?\^M))
  21225. ;; leave blank line before heading
  21226. (forward-char -1))))))
  21227. (point))
  21228. (defadvice outline-end-of-subtree (around prefer-org-version activate compile)
  21229. "Use Org version in org-mode, for dramatic speed-up."
  21230. (if (derived-mode-p 'org-mode)
  21231. (progn
  21232. (org-end-of-subtree nil t)
  21233. (unless (eobp) (backward-char 1)))
  21234. ad-do-it))
  21235. (defun org-end-of-meta-data-and-drawers ()
  21236. "Jump to the first text after meta data and drawers in the current entry.
  21237. This will move over empty lines, lines with planning time stamps,
  21238. clocking lines, and drawers."
  21239. (org-back-to-heading t)
  21240. (let ((end (save-excursion (outline-next-heading) (point)))
  21241. (re (concat "\\(" org-drawer-regexp "\\)"
  21242. "\\|" "[ \t]*" org-keyword-time-regexp)))
  21243. (forward-line 1)
  21244. (while (re-search-forward re end t)
  21245. (if (not (match-end 1))
  21246. ;; empty or planning line
  21247. (forward-line 1)
  21248. ;; a drawer, find the end
  21249. (re-search-forward "^[ \t]*:END:" end 'move)
  21250. (forward-line 1)))
  21251. (and (re-search-forward "[^\n]" nil t) (backward-char 1))
  21252. (point)))
  21253. (defun org-forward-heading-same-level (arg &optional invisible-ok)
  21254. "Move forward to the ARG'th subheading at same level as this one.
  21255. Stop at the first and last subheadings of a superior heading.
  21256. Normally this only looks at visible headings, but when INVISIBLE-OK is
  21257. non-nil it will also look at invisible ones."
  21258. (interactive "p")
  21259. (if (not (ignore-errors (org-back-to-heading invisible-ok)))
  21260. (if (and arg (< arg 0))
  21261. (goto-char (point-min))
  21262. (outline-next-heading))
  21263. (org-at-heading-p)
  21264. (let ((level (- (match-end 0) (match-beginning 0) 1))
  21265. (f (if (and arg (< arg 0))
  21266. 're-search-backward
  21267. 're-search-forward))
  21268. (count (if arg (abs arg) 1))
  21269. (result (point)))
  21270. (while (and (prog1 (> count 0)
  21271. (forward-char (if (and arg (< arg 0)) -1 1)))
  21272. (funcall f org-outline-regexp-bol nil 'move))
  21273. (let ((l (- (match-end 0) (match-beginning 0) 1)))
  21274. (cond ((< l level) (setq count 0))
  21275. ((and (= l level)
  21276. (or invisible-ok
  21277. (progn
  21278. (goto-char (line-beginning-position))
  21279. (not (outline-invisible-p)))))
  21280. (setq count (1- count))
  21281. (when (eq l level)
  21282. (setq result (point)))))))
  21283. (goto-char result))
  21284. (beginning-of-line 1)))
  21285. (defun org-backward-heading-same-level (arg &optional invisible-ok)
  21286. "Move backward to the ARG'th subheading at same level as this one.
  21287. Stop at the first and last subheadings of a superior heading."
  21288. (interactive "p")
  21289. (org-forward-heading-same-level (if arg (- arg) -1) invisible-ok))
  21290. (defun org-next-block (arg &optional backward block-regexp)
  21291. "Jump to the next block.
  21292. With a prefix argument ARG, jump forward ARG many source blocks.
  21293. When BACKWARD is non-nil, jump to the previous block.
  21294. When BLOCK-REGEXP is non-nil, use this regexp to find blocks."
  21295. (interactive "p")
  21296. (let ((re (or block-regexp org-block-regexp))
  21297. (re-search-fn (or (and backward 're-search-backward)
  21298. 're-search-forward)))
  21299. (if (looking-at re) (forward-char 1))
  21300. (condition-case nil
  21301. (funcall re-search-fn re nil nil arg)
  21302. (error (error "No %s code blocks" (if backward "previous" "further" ))))
  21303. (goto-char (match-beginning 0)) (org-show-context)))
  21304. (defun org-previous-block (arg &optional block-regexp)
  21305. "Jump to the previous block.
  21306. With a prefix argument ARG, jump backward ARG many source blocks.
  21307. When BLOCK-REGEXP is non-nil, use this regexp to find blocks."
  21308. (interactive "p")
  21309. (org-next-block arg t block-regexp))
  21310. (defun org-forward-element ()
  21311. "Move forward by one element.
  21312. Move to the next element at the same level, when possible."
  21313. (interactive)
  21314. (cond ((eobp) (user-error "Cannot move further down"))
  21315. ((org-with-limited-levels (org-at-heading-p))
  21316. (let ((origin (point)))
  21317. (goto-char (org-end-of-subtree nil t))
  21318. (unless (org-with-limited-levels (org-at-heading-p))
  21319. (goto-char origin)
  21320. (user-error "Cannot move further down"))))
  21321. (t
  21322. (let* ((elem (org-element-at-point))
  21323. (end (org-element-property :end elem))
  21324. (parent (org-element-property :parent elem)))
  21325. (if (and parent (= (org-element-property :contents-end parent) end))
  21326. (goto-char (org-element-property :end parent))
  21327. (goto-char end))))))
  21328. (defun org-backward-element ()
  21329. "Move backward by one element.
  21330. Move to the previous element at the same level, when possible."
  21331. (interactive)
  21332. (cond ((bobp) (user-error "Cannot move further up"))
  21333. ((org-with-limited-levels (org-at-heading-p))
  21334. ;; At a headline, move to the previous one, if any, or stay
  21335. ;; here.
  21336. (let ((origin (point)))
  21337. (org-with-limited-levels (org-backward-heading-same-level 1))
  21338. ;; When current headline has no sibling above, move to its
  21339. ;; parent.
  21340. (when (= (point) origin)
  21341. (or (org-with-limited-levels (org-up-heading-safe))
  21342. (progn (goto-char origin)
  21343. (user-error "Cannot move further up"))))))
  21344. (t
  21345. (let* ((trail (org-element-at-point 'keep-trail))
  21346. (elem (car trail))
  21347. (prev-elem (nth 1 trail))
  21348. (beg (org-element-property :begin elem)))
  21349. (cond
  21350. ;; Move to beginning of current element if point isn't
  21351. ;; there already.
  21352. ((/= (point) beg) (goto-char beg))
  21353. (prev-elem (goto-char (org-element-property :begin prev-elem)))
  21354. ((org-before-first-heading-p) (goto-char (point-min)))
  21355. (t (org-back-to-heading)))))))
  21356. (defun org-up-element ()
  21357. "Move to upper element."
  21358. (interactive)
  21359. (if (org-with-limited-levels (org-at-heading-p))
  21360. (unless (org-up-heading-safe) (user-error "No surrounding element"))
  21361. (let* ((elem (org-element-at-point))
  21362. (parent (org-element-property :parent elem)))
  21363. (if parent (goto-char (org-element-property :begin parent))
  21364. (if (org-with-limited-levels (org-before-first-heading-p))
  21365. (user-error "No surrounding element")
  21366. (org-with-limited-levels (org-back-to-heading)))))))
  21367. (defvar org-element-greater-elements)
  21368. (defun org-down-element ()
  21369. "Move to inner element."
  21370. (interactive)
  21371. (let ((element (org-element-at-point)))
  21372. (cond
  21373. ((memq (org-element-type element) '(plain-list table))
  21374. (goto-char (org-element-property :contents-begin element))
  21375. (forward-char))
  21376. ((memq (org-element-type element) org-element-greater-elements)
  21377. ;; If contents are hidden, first disclose them.
  21378. (when (org-element-property :hiddenp element) (org-cycle))
  21379. (goto-char (or (org-element-property :contents-begin element)
  21380. (user-error "No content for this element"))))
  21381. (t (user-error "No inner element")))))
  21382. (defun org-drag-element-backward ()
  21383. "Move backward element at point."
  21384. (interactive)
  21385. (if (org-with-limited-levels (org-at-heading-p)) (org-move-subtree-up)
  21386. (let* ((trail (org-element-at-point 'keep-trail))
  21387. (elem (car trail))
  21388. (prev-elem (nth 1 trail)))
  21389. ;; Error out if no previous element or previous element is
  21390. ;; a parent of the current one.
  21391. (if (or (not prev-elem) (org-element-nested-p elem prev-elem))
  21392. (user-error "Cannot drag element backward")
  21393. (let ((pos (point)))
  21394. (org-element-swap-A-B prev-elem elem)
  21395. (goto-char (+ (org-element-property :begin prev-elem)
  21396. (- pos (org-element-property :begin elem)))))))))
  21397. (defun org-drag-element-forward ()
  21398. "Move forward element at point."
  21399. (interactive)
  21400. (let* ((pos (point))
  21401. (elem (org-element-at-point)))
  21402. (when (= (point-max) (org-element-property :end elem))
  21403. (user-error "Cannot drag element forward"))
  21404. (goto-char (org-element-property :end elem))
  21405. (let ((next-elem (org-element-at-point)))
  21406. (when (or (org-element-nested-p elem next-elem)
  21407. (and (eq (org-element-type next-elem) 'headline)
  21408. (not (eq (org-element-type elem) 'headline))))
  21409. (goto-char pos)
  21410. (user-error "Cannot drag element forward"))
  21411. ;; Compute new position of point: it's shifted by NEXT-ELEM
  21412. ;; body's length (without final blanks) and by the length of
  21413. ;; blanks between ELEM and NEXT-ELEM.
  21414. (let ((size-next (- (save-excursion
  21415. (goto-char (org-element-property :end next-elem))
  21416. (skip-chars-backward " \r\t\n")
  21417. (forward-line)
  21418. ;; Small correction if buffer doesn't end
  21419. ;; with a newline character.
  21420. (if (and (eolp) (not (bolp))) (1+ (point)) (point)))
  21421. (org-element-property :begin next-elem)))
  21422. (size-blank (- (org-element-property :end elem)
  21423. (save-excursion
  21424. (goto-char (org-element-property :end elem))
  21425. (skip-chars-backward " \r\t\n")
  21426. (forward-line)
  21427. (point)))))
  21428. (org-element-swap-A-B elem next-elem)
  21429. (goto-char (+ pos size-next size-blank))))))
  21430. (defun org-drag-line-forward (arg)
  21431. "Drag the line at point ARG lines forward."
  21432. (interactive "p")
  21433. (dotimes (n (abs arg))
  21434. (let ((c (current-column)))
  21435. (if (< 0 arg)
  21436. (progn
  21437. (beginning-of-line 2)
  21438. (transpose-lines 1)
  21439. (beginning-of-line 0))
  21440. (transpose-lines 1)
  21441. (beginning-of-line -1))
  21442. (org-move-to-column c))))
  21443. (defun org-drag-line-backward (arg)
  21444. "Drag the line at point ARG lines backward."
  21445. (interactive "p")
  21446. (org-drag-line-forward (- arg)))
  21447. (defun org-mark-element ()
  21448. "Put point at beginning of this element, mark at end.
  21449. Interactively, if this command is repeated or (in Transient Mark
  21450. mode) if the mark is active, it marks the next element after the
  21451. ones already marked."
  21452. (interactive)
  21453. (let (deactivate-mark)
  21454. (if (and (org-called-interactively-p 'any)
  21455. (or (and (eq last-command this-command) (mark t))
  21456. (and transient-mark-mode mark-active)))
  21457. (set-mark
  21458. (save-excursion
  21459. (goto-char (mark))
  21460. (goto-char (org-element-property :end (org-element-at-point)))))
  21461. (let ((element (org-element-at-point)))
  21462. (end-of-line)
  21463. (push-mark (org-element-property :end element) t t)
  21464. (goto-char (org-element-property :begin element))))))
  21465. (defun org-narrow-to-element ()
  21466. "Narrow buffer to current element."
  21467. (interactive)
  21468. (let ((elem (org-element-at-point)))
  21469. (cond
  21470. ((eq (car elem) 'headline)
  21471. (narrow-to-region
  21472. (org-element-property :begin elem)
  21473. (org-element-property :end elem)))
  21474. ((memq (car elem) org-element-greater-elements)
  21475. (narrow-to-region
  21476. (org-element-property :contents-begin elem)
  21477. (org-element-property :contents-end elem)))
  21478. (t
  21479. (narrow-to-region
  21480. (org-element-property :begin elem)
  21481. (org-element-property :end elem))))))
  21482. (defun org-transpose-element ()
  21483. "Transpose current and previous elements, keeping blank lines between.
  21484. Point is moved after both elements."
  21485. (interactive)
  21486. (org-skip-whitespace)
  21487. (let ((end (org-element-property :end (org-element-at-point))))
  21488. (org-drag-element-backward)
  21489. (goto-char end)))
  21490. (defun org-unindent-buffer ()
  21491. "Un-indent the visible part of the buffer.
  21492. Relative indentation (between items, inside blocks, etc.) isn't
  21493. modified."
  21494. (interactive)
  21495. (unless (eq major-mode 'org-mode)
  21496. (user-error "Cannot un-indent a buffer not in Org mode"))
  21497. (let* ((parse-tree (org-element-parse-buffer 'greater-element))
  21498. unindent-tree ; For byte-compiler.
  21499. (unindent-tree
  21500. (function
  21501. (lambda (contents)
  21502. (mapc
  21503. (lambda (element)
  21504. (if (memq (org-element-type element) '(headline section))
  21505. (funcall unindent-tree (org-element-contents element))
  21506. (save-excursion
  21507. (save-restriction
  21508. (narrow-to-region
  21509. (org-element-property :begin element)
  21510. (org-element-property :end element))
  21511. (org-do-remove-indentation)))))
  21512. (reverse contents))))))
  21513. (funcall unindent-tree (org-element-contents parse-tree))))
  21514. (defun org-show-subtree ()
  21515. "Show everything after this heading at deeper levels."
  21516. (interactive)
  21517. (outline-flag-region
  21518. (point)
  21519. (save-excursion
  21520. (org-end-of-subtree t t))
  21521. nil))
  21522. (defun org-show-entry ()
  21523. "Show the body directly following this heading.
  21524. Show the heading too, if it is currently invisible."
  21525. (interactive)
  21526. (save-excursion
  21527. (condition-case nil
  21528. (progn
  21529. (org-back-to-heading t)
  21530. (outline-flag-region
  21531. (max (point-min) (1- (point)))
  21532. (save-excursion
  21533. (if (re-search-forward
  21534. (concat "[\r\n]\\(" org-outline-regexp "\\)") nil t)
  21535. (match-beginning 1)
  21536. (point-max)))
  21537. nil)
  21538. (org-cycle-hide-drawers 'children))
  21539. (error nil))))
  21540. (defun org-make-options-regexp (kwds &optional extra)
  21541. "Make a regular expression for keyword lines."
  21542. (concat
  21543. "^#\\+\\("
  21544. (mapconcat 'regexp-quote kwds "\\|")
  21545. (if extra (concat "\\|" extra))
  21546. "\\):[ \t]*\\(.*\\)"))
  21547. ;; Make isearch reveal the necessary context
  21548. (defun org-isearch-end ()
  21549. "Reveal context after isearch exits."
  21550. (when isearch-success ; only if search was successful
  21551. (if (featurep 'xemacs)
  21552. ;; Under XEmacs, the hook is run in the correct place,
  21553. ;; we directly show the context.
  21554. (org-show-context 'isearch)
  21555. ;; In Emacs the hook runs *before* restoring the overlays.
  21556. ;; So we have to use a one-time post-command-hook to do this.
  21557. ;; (Emacs 22 has a special variable, see function `org-mode')
  21558. (unless (and (boundp 'isearch-mode-end-hook-quit)
  21559. isearch-mode-end-hook-quit)
  21560. ;; Only when the isearch was not quitted.
  21561. (org-add-hook 'post-command-hook 'org-isearch-post-command
  21562. 'append 'local)))
  21563. (org-fix-ellipsis-at-bol)))
  21564. (defun org-isearch-post-command ()
  21565. "Remove self from hook, and show context."
  21566. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  21567. (org-show-context 'isearch))
  21568. ;;;; Integration with and fixes for other packages
  21569. ;;; Imenu support
  21570. (defvar org-imenu-markers nil
  21571. "All markers currently used by Imenu.")
  21572. (make-variable-buffer-local 'org-imenu-markers)
  21573. (defun org-imenu-new-marker (&optional pos)
  21574. "Return a new marker for use by Imenu, and remember the marker."
  21575. (let ((m (make-marker)))
  21576. (move-marker m (or pos (point)))
  21577. (push m org-imenu-markers)
  21578. m))
  21579. (defun org-imenu-get-tree ()
  21580. "Produce the index for Imenu."
  21581. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  21582. (setq org-imenu-markers nil)
  21583. (let* ((n org-imenu-depth)
  21584. (re (concat "^" (org-get-limited-outline-regexp)))
  21585. (subs (make-vector (1+ n) nil))
  21586. (last-level 0)
  21587. m level head0 head)
  21588. (save-excursion
  21589. (save-restriction
  21590. (widen)
  21591. (goto-char (point-max))
  21592. (while (re-search-backward re nil t)
  21593. (setq level (org-reduced-level (funcall outline-level)))
  21594. (when (and (<= level n)
  21595. (looking-at org-complex-heading-regexp)
  21596. (setq head0 (org-match-string-no-properties 4)))
  21597. (setq head (org-link-display-format head0)
  21598. m (org-imenu-new-marker))
  21599. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  21600. (if (>= level last-level)
  21601. (push (cons head m) (aref subs level))
  21602. (push (cons head (aref subs (1+ level))) (aref subs level))
  21603. (loop for i from (1+ level) to n do (aset subs i nil)))
  21604. (setq last-level level)))))
  21605. (aref subs 1)))
  21606. (eval-after-load "imenu"
  21607. '(progn
  21608. (add-hook 'imenu-after-jump-hook
  21609. (lambda ()
  21610. (if (derived-mode-p 'org-mode)
  21611. (org-show-context 'org-goto))))))
  21612. (defun org-link-display-format (link)
  21613. "Replace a link with its the description.
  21614. If there is no description, use the link target."
  21615. (save-match-data
  21616. (if (string-match org-bracket-link-analytic-regexp link)
  21617. (replace-match (if (match-end 5)
  21618. (match-string 5 link)
  21619. (concat (match-string 1 link)
  21620. (match-string 3 link)))
  21621. nil t link)
  21622. link)))
  21623. (defun org-toggle-link-display ()
  21624. "Toggle the literal or descriptive display of links."
  21625. (interactive)
  21626. (if org-descriptive-links
  21627. (progn (org-remove-from-invisibility-spec '(org-link))
  21628. (org-restart-font-lock)
  21629. (setq org-descriptive-links nil))
  21630. (progn (add-to-invisibility-spec '(org-link))
  21631. (org-restart-font-lock)
  21632. (setq org-descriptive-links t))))
  21633. ;; Speedbar support
  21634. (defvar org-speedbar-restriction-lock-overlay (make-overlay 1 1)
  21635. "Overlay marking the agenda restriction line in speedbar.")
  21636. (overlay-put org-speedbar-restriction-lock-overlay
  21637. 'face 'org-agenda-restriction-lock)
  21638. (overlay-put org-speedbar-restriction-lock-overlay
  21639. 'help-echo "Agendas are currently limited to this item.")
  21640. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  21641. (defun org-speedbar-set-agenda-restriction ()
  21642. "Restrict future agenda commands to the location at point in speedbar.
  21643. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  21644. (interactive)
  21645. (require 'org-agenda)
  21646. (let (p m tp np dir txt)
  21647. (cond
  21648. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  21649. 'org-imenu t))
  21650. (setq m (get-text-property p 'org-imenu-marker))
  21651. (with-current-buffer (marker-buffer m)
  21652. (goto-char m)
  21653. (org-agenda-set-restriction-lock 'subtree)))
  21654. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  21655. 'speedbar-function 'speedbar-find-file))
  21656. (setq tp (previous-single-property-change
  21657. (1+ p) 'speedbar-function)
  21658. np (next-single-property-change
  21659. tp 'speedbar-function)
  21660. dir (speedbar-line-directory)
  21661. txt (buffer-substring-no-properties (or tp (point-min))
  21662. (or np (point-max))))
  21663. (with-current-buffer (find-file-noselect
  21664. (let ((default-directory dir))
  21665. (expand-file-name txt)))
  21666. (unless (derived-mode-p 'org-mode)
  21667. (user-error "Cannot restrict to non-Org-mode file"))
  21668. (org-agenda-set-restriction-lock 'file)))
  21669. (t (user-error "Don't know how to restrict Org-mode's agenda")))
  21670. (move-overlay org-speedbar-restriction-lock-overlay
  21671. (point-at-bol) (point-at-eol))
  21672. (setq current-prefix-arg nil)
  21673. (org-agenda-maybe-redo)))
  21674. (eval-after-load "speedbar"
  21675. '(progn
  21676. (speedbar-add-supported-extension ".org")
  21677. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  21678. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  21679. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  21680. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  21681. (add-hook 'speedbar-visiting-tag-hook
  21682. (lambda () (and (derived-mode-p 'org-mode) (org-show-context 'org-goto))))))
  21683. ;;; Fixes and Hacks for problems with other packages
  21684. ;; Make flyspell not check words in links, to not mess up our keymap
  21685. (defvar org-element-affiliated-keywords) ; From org-element.el
  21686. (defvar org-element-block-name-alist) ; From org-element.el
  21687. (defun org-mode-flyspell-verify ()
  21688. "Don't let flyspell put overlays at active buttons, or on
  21689. {todo,all-time,additional-option-like}-keywords."
  21690. (require 'org-element) ; For `org-element-affiliated-keywords'
  21691. (let ((pos (max (1- (point)) (point-min)))
  21692. (word (thing-at-point 'word)))
  21693. (and (not (get-text-property pos 'keymap))
  21694. (not (get-text-property pos 'org-no-flyspell))
  21695. (not (member word org-todo-keywords-1))
  21696. (not (member word org-all-time-keywords))
  21697. (not (member word org-options-keywords))
  21698. (not (member word (mapcar 'car org-startup-options)))
  21699. (not (member-ignore-case word org-element-affiliated-keywords))
  21700. (not (member-ignore-case word (org-get-export-keywords)))
  21701. (not (member-ignore-case
  21702. word (mapcar 'car org-element-block-name-alist)))
  21703. (not (member-ignore-case word '("BEGIN" "END" "ATTR"))))))
  21704. (defun org-remove-flyspell-overlays-in (beg end)
  21705. "Remove flyspell overlays in region."
  21706. (and (org-bound-and-true-p flyspell-mode)
  21707. (fboundp 'flyspell-delete-region-overlays)
  21708. (flyspell-delete-region-overlays beg end))
  21709. (add-text-properties beg end '(org-no-flyspell t)))
  21710. ;; Make `bookmark-jump' shows the jump location if it was hidden.
  21711. (eval-after-load "bookmark"
  21712. '(if (boundp 'bookmark-after-jump-hook)
  21713. ;; We can use the hook
  21714. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  21715. ;; Hook not available, use advice
  21716. (defadvice bookmark-jump (after org-make-visible activate)
  21717. "Make the position visible."
  21718. (org-bookmark-jump-unhide))))
  21719. ;; Make sure saveplace shows the location if it was hidden
  21720. (eval-after-load "saveplace"
  21721. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  21722. "Make the position visible."
  21723. (org-bookmark-jump-unhide)))
  21724. ;; Make sure ecb shows the location if it was hidden
  21725. (eval-after-load "ecb"
  21726. '(defadvice ecb-method-clicked (after esf/org-show-context activate)
  21727. "Make hierarchy visible when jumping into location from ECB tree buffer."
  21728. (if (derived-mode-p 'org-mode)
  21729. (org-show-context))))
  21730. (defun org-bookmark-jump-unhide ()
  21731. "Unhide the current position, to show the bookmark location."
  21732. (and (derived-mode-p 'org-mode)
  21733. (or (outline-invisible-p)
  21734. (save-excursion (goto-char (max (point-min) (1- (point))))
  21735. (outline-invisible-p)))
  21736. (org-show-context 'bookmark-jump)))
  21737. ;; Make session.el ignore our circular variable
  21738. (eval-after-load "session"
  21739. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  21740. ;;;; Finish up
  21741. (provide 'org)
  21742. (run-hooks 'org-load-hook)
  21743. ;;; org.el ends here