org.el 851 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515175161751717518175191752017521175221752317524175251752617527175281752917530175311753217533175341753517536175371753817539175401754117542175431754417545175461754717548175491755017551175521755317554175551755617557175581755917560175611756217563175641756517566175671756817569175701757117572175731757417575175761757717578175791758017581175821758317584175851758617587175881758917590175911759217593175941759517596175971759817599176001760117602176031760417605176061760717608176091761017611176121761317614176151761617617176181761917620176211762217623176241762517626176271762817629176301763117632176331763417635176361763717638176391764017641176421764317644176451764617647176481764917650176511765217653176541765517656176571765817659176601766117662176631766417665176661766717668176691767017671176721767317674176751767617677176781767917680176811768217683176841768517686176871768817689176901769117692176931769417695176961769717698176991770017701177021770317704177051770617707177081770917710177111771217713177141771517716177171771817719177201772117722177231772417725177261772717728177291773017731177321773317734177351773617737177381773917740177411774217743177441774517746177471774817749177501775117752177531775417755177561775717758177591776017761177621776317764177651776617767177681776917770177711777217773177741777517776177771777817779177801778117782177831778417785177861778717788177891779017791177921779317794177951779617797177981779917800178011780217803178041780517806178071780817809178101781117812178131781417815178161781717818178191782017821178221782317824178251782617827178281782917830178311783217833178341783517836178371783817839178401784117842178431784417845178461784717848178491785017851178521785317854178551785617857178581785917860178611786217863178641786517866178671786817869178701787117872178731787417875178761787717878178791788017881178821788317884178851788617887178881788917890178911789217893178941789517896178971789817899179001790117902179031790417905179061790717908179091791017911179121791317914179151791617917179181791917920179211792217923179241792517926179271792817929179301793117932179331793417935179361793717938179391794017941179421794317944179451794617947179481794917950179511795217953179541795517956179571795817959179601796117962179631796417965179661796717968179691797017971179721797317974179751797617977179781797917980179811798217983179841798517986179871798817989179901799117992179931799417995179961799717998179991800018001180021800318004180051800618007180081800918010180111801218013180141801518016180171801818019180201802118022180231802418025180261802718028180291803018031180321803318034180351803618037180381803918040180411804218043180441804518046180471804818049180501805118052180531805418055180561805718058180591806018061180621806318064180651806618067180681806918070180711807218073180741807518076180771807818079180801808118082180831808418085180861808718088180891809018091180921809318094180951809618097180981809918100181011810218103181041810518106181071810818109181101811118112181131811418115181161811718118181191812018121181221812318124181251812618127181281812918130181311813218133181341813518136181371813818139181401814118142181431814418145181461814718148181491815018151181521815318154181551815618157181581815918160181611816218163181641816518166181671816818169181701817118172181731817418175181761817718178181791818018181181821818318184181851818618187181881818918190181911819218193181941819518196181971819818199182001820118202182031820418205182061820718208182091821018211182121821318214182151821618217182181821918220182211822218223182241822518226182271822818229182301823118232182331823418235182361823718238182391824018241182421824318244182451824618247182481824918250182511825218253182541825518256182571825818259182601826118262182631826418265182661826718268182691827018271182721827318274182751827618277182781827918280182811828218283182841828518286182871828818289182901829118292182931829418295182961829718298182991830018301183021830318304183051830618307183081830918310183111831218313183141831518316183171831818319183201832118322183231832418325183261832718328183291833018331183321833318334183351833618337183381833918340183411834218343183441834518346183471834818349183501835118352183531835418355183561835718358183591836018361183621836318364183651836618367183681836918370183711837218373183741837518376183771837818379183801838118382183831838418385183861838718388183891839018391183921839318394183951839618397183981839918400184011840218403184041840518406184071840818409184101841118412184131841418415184161841718418184191842018421184221842318424184251842618427184281842918430184311843218433184341843518436184371843818439184401844118442184431844418445184461844718448184491845018451184521845318454184551845618457184581845918460184611846218463184641846518466184671846818469184701847118472184731847418475184761847718478184791848018481184821848318484184851848618487184881848918490184911849218493184941849518496184971849818499185001850118502185031850418505185061850718508185091851018511185121851318514185151851618517185181851918520185211852218523185241852518526185271852818529185301853118532185331853418535185361853718538185391854018541185421854318544185451854618547185481854918550185511855218553185541855518556185571855818559185601856118562185631856418565185661856718568185691857018571185721857318574185751857618577185781857918580185811858218583185841858518586185871858818589185901859118592185931859418595185961859718598185991860018601186021860318604186051860618607186081860918610186111861218613186141861518616186171861818619186201862118622186231862418625186261862718628186291863018631186321863318634186351863618637186381863918640186411864218643186441864518646186471864818649186501865118652186531865418655186561865718658186591866018661186621866318664186651866618667186681866918670186711867218673186741867518676186771867818679186801868118682186831868418685186861868718688186891869018691186921869318694186951869618697186981869918700187011870218703187041870518706187071870818709187101871118712187131871418715187161871718718187191872018721187221872318724187251872618727187281872918730187311873218733187341873518736187371873818739187401874118742187431874418745187461874718748187491875018751187521875318754187551875618757187581875918760187611876218763187641876518766187671876818769187701877118772187731877418775187761877718778187791878018781187821878318784187851878618787187881878918790187911879218793187941879518796187971879818799188001880118802188031880418805188061880718808188091881018811188121881318814188151881618817188181881918820188211882218823188241882518826188271882818829188301883118832188331883418835188361883718838188391884018841188421884318844188451884618847188481884918850188511885218853188541885518856188571885818859188601886118862188631886418865188661886718868188691887018871188721887318874188751887618877188781887918880188811888218883188841888518886188871888818889188901889118892188931889418895188961889718898188991890018901189021890318904189051890618907189081890918910189111891218913189141891518916189171891818919189201892118922189231892418925189261892718928189291893018931189321893318934189351893618937189381893918940189411894218943189441894518946189471894818949189501895118952189531895418955189561895718958189591896018961189621896318964189651896618967189681896918970189711897218973189741897518976189771897818979189801898118982189831898418985189861898718988189891899018991189921899318994189951899618997189981899919000190011900219003190041900519006190071900819009190101901119012190131901419015190161901719018190191902019021190221902319024190251902619027190281902919030190311903219033190341903519036190371903819039190401904119042190431904419045190461904719048190491905019051190521905319054190551905619057190581905919060190611906219063190641906519066190671906819069190701907119072190731907419075190761907719078190791908019081190821908319084190851908619087190881908919090190911909219093190941909519096190971909819099191001910119102191031910419105191061910719108191091911019111191121911319114191151911619117191181911919120191211912219123191241912519126191271912819129191301913119132191331913419135191361913719138191391914019141191421914319144191451914619147191481914919150191511915219153191541915519156191571915819159191601916119162191631916419165191661916719168191691917019171191721917319174191751917619177191781917919180191811918219183191841918519186191871918819189191901919119192191931919419195191961919719198191991920019201192021920319204192051920619207192081920919210192111921219213192141921519216192171921819219192201922119222192231922419225192261922719228192291923019231192321923319234192351923619237192381923919240192411924219243192441924519246192471924819249192501925119252192531925419255192561925719258192591926019261192621926319264192651926619267192681926919270192711927219273192741927519276192771927819279192801928119282192831928419285192861928719288192891929019291192921929319294192951929619297192981929919300193011930219303193041930519306193071930819309193101931119312193131931419315193161931719318193191932019321193221932319324193251932619327193281932919330193311933219333193341933519336193371933819339193401934119342193431934419345193461934719348193491935019351193521935319354193551935619357193581935919360193611936219363193641936519366193671936819369193701937119372193731937419375193761937719378193791938019381193821938319384193851938619387193881938919390193911939219393193941939519396193971939819399194001940119402194031940419405194061940719408194091941019411194121941319414194151941619417194181941919420194211942219423194241942519426194271942819429194301943119432194331943419435194361943719438194391944019441194421944319444194451944619447194481944919450194511945219453194541945519456194571945819459194601946119462194631946419465194661946719468194691947019471194721947319474194751947619477194781947919480194811948219483194841948519486194871948819489194901949119492194931949419495194961949719498194991950019501195021950319504195051950619507195081950919510195111951219513195141951519516195171951819519195201952119522195231952419525195261952719528195291953019531195321953319534195351953619537195381953919540195411954219543195441954519546195471954819549195501955119552195531955419555195561955719558195591956019561195621956319564195651956619567195681956919570195711957219573195741957519576195771957819579195801958119582195831958419585195861958719588195891959019591195921959319594195951959619597195981959919600196011960219603196041960519606196071960819609196101961119612196131961419615196161961719618196191962019621196221962319624196251962619627196281962919630196311963219633196341963519636196371963819639196401964119642196431964419645196461964719648196491965019651196521965319654196551965619657196581965919660196611966219663196641966519666196671966819669196701967119672196731967419675196761967719678196791968019681196821968319684196851968619687196881968919690196911969219693196941969519696196971969819699197001970119702197031970419705197061970719708197091971019711197121971319714197151971619717197181971919720197211972219723197241972519726197271972819729197301973119732197331973419735197361973719738197391974019741197421974319744197451974619747197481974919750197511975219753197541975519756197571975819759197601976119762197631976419765197661976719768197691977019771197721977319774197751977619777197781977919780197811978219783197841978519786197871978819789197901979119792197931979419795197961979719798197991980019801198021980319804198051980619807198081980919810198111981219813198141981519816198171981819819198201982119822198231982419825198261982719828198291983019831198321983319834198351983619837198381983919840198411984219843198441984519846198471984819849198501985119852198531985419855198561985719858198591986019861198621986319864198651986619867198681986919870198711987219873198741987519876198771987819879198801988119882198831988419885198861988719888198891989019891198921989319894198951989619897198981989919900199011990219903199041990519906199071990819909199101991119912199131991419915199161991719918199191992019921199221992319924199251992619927199281992919930199311993219933199341993519936199371993819939199401994119942199431994419945199461994719948199491995019951199521995319954199551995619957199581995919960199611996219963199641996519966199671996819969199701997119972199731997419975199761997719978199791998019981199821998319984199851998619987199881998919990199911999219993199941999519996199971999819999200002000120002200032000420005200062000720008200092001020011200122001320014200152001620017200182001920020200212002220023200242002520026200272002820029200302003120032200332003420035200362003720038200392004020041200422004320044200452004620047200482004920050200512005220053200542005520056200572005820059200602006120062200632006420065200662006720068200692007020071200722007320074200752007620077200782007920080200812008220083200842008520086200872008820089200902009120092200932009420095200962009720098200992010020101201022010320104201052010620107201082010920110201112011220113201142011520116201172011820119201202012120122201232012420125201262012720128201292013020131201322013320134201352013620137201382013920140201412014220143201442014520146201472014820149201502015120152201532015420155201562015720158201592016020161201622016320164201652016620167201682016920170201712017220173201742017520176201772017820179201802018120182201832018420185201862018720188201892019020191201922019320194201952019620197201982019920200202012020220203202042020520206202072020820209202102021120212202132021420215202162021720218202192022020221202222022320224202252022620227202282022920230202312023220233202342023520236202372023820239202402024120242202432024420245202462024720248202492025020251202522025320254202552025620257202582025920260202612026220263202642026520266202672026820269202702027120272202732027420275202762027720278202792028020281202822028320284202852028620287202882028920290202912029220293202942029520296202972029820299203002030120302203032030420305203062030720308203092031020311203122031320314203152031620317203182031920320203212032220323203242032520326203272032820329203302033120332203332033420335203362033720338203392034020341203422034320344203452034620347203482034920350203512035220353203542035520356203572035820359203602036120362203632036420365203662036720368203692037020371203722037320374203752037620377203782037920380203812038220383203842038520386203872038820389203902039120392203932039420395203962039720398203992040020401204022040320404204052040620407204082040920410204112041220413204142041520416204172041820419204202042120422204232042420425204262042720428204292043020431204322043320434204352043620437204382043920440204412044220443204442044520446204472044820449204502045120452204532045420455204562045720458204592046020461204622046320464204652046620467204682046920470204712047220473204742047520476204772047820479204802048120482204832048420485204862048720488204892049020491204922049320494204952049620497204982049920500205012050220503205042050520506205072050820509205102051120512205132051420515205162051720518205192052020521205222052320524205252052620527205282052920530205312053220533205342053520536205372053820539205402054120542205432054420545205462054720548205492055020551205522055320554205552055620557205582055920560205612056220563205642056520566205672056820569205702057120572205732057420575205762057720578205792058020581205822058320584205852058620587205882058920590205912059220593205942059520596205972059820599206002060120602206032060420605206062060720608206092061020611206122061320614206152061620617206182061920620206212062220623206242062520626206272062820629206302063120632206332063420635206362063720638206392064020641206422064320644206452064620647206482064920650206512065220653206542065520656206572065820659206602066120662206632066420665206662066720668206692067020671206722067320674206752067620677206782067920680206812068220683206842068520686206872068820689206902069120692206932069420695206962069720698206992070020701207022070320704207052070620707207082070920710207112071220713207142071520716207172071820719207202072120722207232072420725207262072720728207292073020731207322073320734207352073620737207382073920740207412074220743207442074520746207472074820749207502075120752207532075420755207562075720758207592076020761207622076320764207652076620767207682076920770207712077220773207742077520776207772077820779207802078120782207832078420785207862078720788207892079020791207922079320794207952079620797207982079920800208012080220803208042080520806208072080820809208102081120812208132081420815208162081720818208192082020821208222082320824208252082620827208282082920830208312083220833208342083520836208372083820839208402084120842208432084420845208462084720848208492085020851208522085320854208552085620857208582085920860208612086220863208642086520866208672086820869208702087120872208732087420875208762087720878208792088020881208822088320884208852088620887208882088920890208912089220893208942089520896208972089820899209002090120902209032090420905209062090720908209092091020911209122091320914209152091620917209182091920920209212092220923209242092520926209272092820929209302093120932209332093420935209362093720938209392094020941209422094320944209452094620947209482094920950209512095220953209542095520956209572095820959209602096120962209632096420965209662096720968209692097020971209722097320974209752097620977209782097920980209812098220983209842098520986209872098820989209902099120992209932099420995209962099720998209992100021001210022100321004210052100621007210082100921010210112101221013210142101521016210172101821019210202102121022210232102421025210262102721028210292103021031210322103321034210352103621037210382103921040210412104221043210442104521046210472104821049210502105121052210532105421055210562105721058210592106021061210622106321064210652106621067210682106921070210712107221073210742107521076210772107821079210802108121082210832108421085210862108721088210892109021091210922109321094210952109621097210982109921100211012110221103211042110521106211072110821109211102111121112211132111421115211162111721118211192112021121211222112321124211252112621127211282112921130211312113221133211342113521136211372113821139211402114121142211432114421145211462114721148211492115021151211522115321154211552115621157211582115921160211612116221163211642116521166211672116821169211702117121172211732117421175211762117721178211792118021181211822118321184211852118621187211882118921190211912119221193211942119521196211972119821199212002120121202212032120421205212062120721208212092121021211212122121321214212152121621217212182121921220212212122221223212242122521226212272122821229212302123121232212332123421235212362123721238212392124021241212422124321244212452124621247212482124921250212512125221253212542125521256212572125821259212602126121262212632126421265212662126721268212692127021271212722127321274212752127621277212782127921280212812128221283212842128521286212872128821289212902129121292212932129421295212962129721298212992130021301213022130321304213052130621307213082130921310213112131221313213142131521316213172131821319213202132121322213232132421325213262132721328213292133021331213322133321334213352133621337213382133921340213412134221343213442134521346213472134821349213502135121352213532135421355213562135721358213592136021361213622136321364213652136621367213682136921370213712137221373213742137521376213772137821379213802138121382213832138421385213862138721388213892139021391213922139321394213952139621397213982139921400214012140221403214042140521406214072140821409214102141121412214132141421415214162141721418214192142021421214222142321424214252142621427214282142921430214312143221433214342143521436214372143821439214402144121442214432144421445214462144721448214492145021451214522145321454214552145621457214582145921460214612146221463214642146521466214672146821469214702147121472214732147421475214762147721478214792148021481214822148321484214852148621487214882148921490214912149221493214942149521496214972149821499215002150121502215032150421505215062150721508215092151021511215122151321514215152151621517215182151921520215212152221523215242152521526215272152821529215302153121532215332153421535215362153721538215392154021541215422154321544215452154621547215482154921550215512155221553215542155521556215572155821559215602156121562215632156421565215662156721568215692157021571215722157321574215752157621577215782157921580215812158221583215842158521586215872158821589215902159121592215932159421595215962159721598215992160021601216022160321604216052160621607216082160921610216112161221613216142161521616216172161821619216202162121622216232162421625216262162721628216292163021631216322163321634216352163621637216382163921640216412164221643216442164521646216472164821649216502165121652216532165421655216562165721658216592166021661216622166321664216652166621667216682166921670216712167221673216742167521676216772167821679216802168121682216832168421685216862168721688216892169021691216922169321694216952169621697216982169921700217012170221703217042170521706217072170821709217102171121712217132171421715217162171721718217192172021721217222172321724217252172621727217282172921730217312173221733217342173521736217372173821739217402174121742217432174421745217462174721748217492175021751217522175321754217552175621757217582175921760217612176221763217642176521766217672176821769217702177121772217732177421775217762177721778217792178021781217822178321784217852178621787217882178921790217912179221793217942179521796217972179821799218002180121802218032180421805218062180721808218092181021811218122181321814218152181621817218182181921820218212182221823218242182521826218272182821829218302183121832218332183421835218362183721838218392184021841218422184321844218452184621847218482184921850218512185221853218542185521856218572185821859218602186121862218632186421865218662186721868218692187021871218722187321874218752187621877218782187921880218812188221883218842188521886218872188821889218902189121892218932189421895218962189721898218992190021901219022190321904219052190621907219082190921910219112191221913219142191521916219172191821919219202192121922219232192421925219262192721928219292193021931219322193321934219352193621937219382193921940219412194221943219442194521946219472194821949219502195121952219532195421955219562195721958219592196021961219622196321964219652196621967219682196921970219712197221973219742197521976219772197821979219802198121982219832198421985219862198721988219892199021991219922199321994219952199621997219982199922000220012200222003220042200522006220072200822009220102201122012220132201422015220162201722018220192202022021220222202322024220252202622027220282202922030220312203222033220342203522036220372203822039220402204122042220432204422045220462204722048220492205022051220522205322054220552205622057220582205922060220612206222063220642206522066220672206822069220702207122072220732207422075220762207722078220792208022081220822208322084220852208622087220882208922090220912209222093220942209522096220972209822099221002210122102221032210422105221062210722108221092211022111221122211322114221152211622117221182211922120221212212222123221242212522126221272212822129221302213122132221332213422135221362213722138221392214022141221422214322144221452214622147221482214922150221512215222153221542215522156221572215822159221602216122162221632216422165221662216722168221692217022171221722217322174221752217622177221782217922180221812218222183221842218522186221872218822189221902219122192221932219422195221962219722198221992220022201222022220322204222052220622207222082220922210222112221222213222142221522216222172221822219222202222122222222232222422225222262222722228222292223022231222322223322234222352223622237222382223922240222412224222243222442224522246222472224822249222502225122252222532225422255222562225722258222592226022261222622226322264222652226622267222682226922270222712227222273222742227522276222772227822279222802228122282222832228422285222862228722288222892229022291222922229322294222952229622297222982229922300223012230222303223042230522306223072230822309223102231122312223132231422315223162231722318223192232022321223222232322324223252232622327223282232922330223312233222333223342233522336223372233822339223402234122342223432234422345223462234722348223492235022351223522235322354223552235622357223582235922360223612236222363223642236522366223672236822369223702237122372223732237422375223762237722378223792238022381223822238322384223852238622387223882238922390223912239222393223942239522396223972239822399224002240122402224032240422405224062240722408224092241022411224122241322414224152241622417224182241922420224212242222423224242242522426224272242822429224302243122432224332243422435224362243722438224392244022441224422244322444224452244622447224482244922450224512245222453224542245522456224572245822459224602246122462224632246422465224662246722468224692247022471224722247322474224752247622477224782247922480224812248222483224842248522486224872248822489224902249122492224932249422495224962249722498224992250022501225022250322504225052250622507225082250922510225112251222513225142251522516225172251822519225202252122522225232252422525225262252722528225292253022531225322253322534225352253622537225382253922540225412254222543225442254522546225472254822549225502255122552
  1. ;;; org.el --- Outline-based notes management and organizer
  2. ;; Carstens outline-mode for keeping track of everything.
  3. ;; Copyright (C) 2004-2012 Free Software Foundation, Inc.
  4. ;;
  5. ;; Author: Carsten Dominik <carsten at orgmode dot org>
  6. ;; Maintainer: Bastien Guerry <bzg at gnu 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. ;;
  24. ;;; Commentary:
  25. ;;
  26. ;; Org-mode is a mode for keeping notes, maintaining ToDo lists, and doing
  27. ;; project planning with a fast and effective plain-text system.
  28. ;;
  29. ;; Org-mode develops organizational tasks around NOTES files that contain
  30. ;; information about projects as plain text. Org-mode is implemented on
  31. ;; top of outline-mode, which makes it possible to keep the content of
  32. ;; large files well structured. Visibility cycling and structure editing
  33. ;; help to work with the tree. Tables are easily created with a built-in
  34. ;; table editor. Org-mode supports ToDo items, deadlines, time stamps,
  35. ;; and scheduling. It dynamically compiles entries into an agenda that
  36. ;; utilizes and smoothly integrates much of the Emacs calendar and diary.
  37. ;; Plain text URL-like links connect to websites, emails, Usenet
  38. ;; messages, BBDB entries, and any files related to the projects. For
  39. ;; printing and sharing of notes, an Org-mode file can be exported as a
  40. ;; structured ASCII file, as HTML, or (todo and agenda items only) as an
  41. ;; iCalendar file. It can also serve as a publishing tool for a set of
  42. ;; linked webpages.
  43. ;;
  44. ;; Installation and Activation
  45. ;; ---------------------------
  46. ;; See the corresponding sections in the manual at
  47. ;;
  48. ;; http://orgmode.org/org.html#Installation
  49. ;;
  50. ;; Documentation
  51. ;; -------------
  52. ;; The documentation of Org-mode can be found in the TeXInfo file. The
  53. ;; distribution also contains a PDF version of it. At the homepage of
  54. ;; Org-mode, you can read the same text online as HTML. There is also an
  55. ;; excellent reference card made by Philip Rooke. This card can be found
  56. ;; in the etc/ directory of Emacs 22.
  57. ;;
  58. ;; A list of recent changes can be found at
  59. ;; http://orgmode.org/Changes.html
  60. ;;
  61. ;;; Code:
  62. (defvar org-inhibit-highlight-removal nil) ; dynamically scoped param
  63. (defvar org-table-formula-constants-local nil
  64. "Local version of `org-table-formula-constants'.")
  65. (make-variable-buffer-local 'org-table-formula-constants-local)
  66. ;;;; Require other packages
  67. (eval-when-compile
  68. (require 'cl)
  69. (require 'gnus-sum))
  70. (require 'calendar)
  71. (require 'find-func)
  72. (require 'format-spec)
  73. (let ((load-suffixes (list ".el")))
  74. (load "org-loaddefs" 'noerror nil nil 'mustsuffix))
  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 an 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. (when (fboundp 'defvaralias)
  93. (unless (boundp 'calendar-view-holidays-initially-flag)
  94. (defvaralias 'calendar-view-holidays-initially-flag
  95. 'view-calendar-holidays-initially))
  96. (unless (boundp 'calendar-view-diary-initially-flag)
  97. (defvaralias 'calendar-view-diary-initially-flag
  98. 'view-diary-entries-initially))
  99. (unless (boundp 'diary-fancy-buffer)
  100. (defvaralias 'diary-fancy-buffer 'fancy-diary-buffer)))
  101. (declare-function org-inlinetask-at-task-p "org-inlinetask" ())
  102. (declare-function org-inlinetask-outline-regexp "org-inlinetask" ())
  103. (declare-function org-inlinetask-toggle-visibility "org-inlinetask" ())
  104. (declare-function org-pop-to-buffer-same-window "org-compat" (&optional buffer-or-name norecord label))
  105. (declare-function org-at-clock-log-p "org-clock" ())
  106. (declare-function org-clock-get-last-clock-out-time "org-clock" ())
  107. (declare-function org-clock-timestamps-up "org-clock" ())
  108. (declare-function org-clock-timestamps-down "org-clock" ())
  109. (declare-function org-clock-sum-current-item "org-clock" (&optional tstart))
  110. (declare-function orgtbl-mode "org-table" (&optional arg))
  111. (declare-function org-clock-out "org-clock" (&optional switch-to-state fail-quietly at-time))
  112. (declare-function org-beamer-mode "org-beamer" ())
  113. (declare-function org-table-edit-field "org-table" (arg))
  114. (declare-function org-table-justify-field-maybe "org-table" (&optional new))
  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-table-align "org-table" ())
  120. (declare-function org-table-paste-rectangle "org-table" ())
  121. (declare-function org-table-maybe-eval-formula "org-table" ())
  122. (declare-function org-table-maybe-recalculate-line "org-table" ())
  123. ;; load languages based on value of `org-babel-load-languages'
  124. (defvar org-babel-load-languages)
  125. ;;;###autoload
  126. (defun org-babel-do-load-languages (sym value)
  127. "Load the languages defined in `org-babel-load-languages'."
  128. (set-default sym value)
  129. (mapc (lambda (pair)
  130. (let ((active (cdr pair)) (lang (symbol-name (car pair))))
  131. (if active
  132. (progn
  133. (require (intern (concat "ob-" lang))))
  134. (progn
  135. (funcall 'fmakunbound
  136. (intern (concat "org-babel-execute:" lang)))
  137. (funcall 'fmakunbound
  138. (intern (concat "org-babel-expand-body:" lang)))))))
  139. org-babel-load-languages))
  140. (defcustom org-babel-load-languages '((emacs-lisp . t))
  141. "Languages which can be evaluated in Org-mode buffers.
  142. This list can be used to load support for any of the languages
  143. below, note that each language will depend on a different set of
  144. system executables and/or Emacs modes. When a language is
  145. \"loaded\", then code blocks in that language can be evaluated
  146. with `org-babel-execute-src-block' bound by default to C-c
  147. C-c (note the `org-babel-no-eval-on-ctrl-c-ctrl-c' variable can
  148. be set to remove code block evaluation from the C-c C-c
  149. keybinding. By default only Emacs Lisp (which has no
  150. requirements) is loaded."
  151. :group 'org-babel
  152. :set 'org-babel-do-load-languages
  153. :version "24.1"
  154. :type '(alist :tag "Babel Languages"
  155. :key-type
  156. (choice
  157. (const :tag "Awk" awk)
  158. (const :tag "C" C)
  159. (const :tag "R" R)
  160. (const :tag "Asymptote" asymptote)
  161. (const :tag "Calc" calc)
  162. (const :tag "Clojure" clojure)
  163. (const :tag "CSS" css)
  164. (const :tag "Ditaa" ditaa)
  165. (const :tag "Dot" dot)
  166. (const :tag "Emacs Lisp" emacs-lisp)
  167. (const :tag "Fortran" fortran)
  168. (const :tag "Gnuplot" gnuplot)
  169. (const :tag "Haskell" haskell)
  170. (const :tag "IO" io)
  171. (const :tag "Java" java)
  172. (const :tag "Javascript" js)
  173. (const :tag "LaTeX" latex)
  174. (const :tag "Ledger" ledger)
  175. (const :tag "Lilypond" lilypond)
  176. (const :tag "Lisp" lisp)
  177. (const :tag "Maxima" maxima)
  178. (const :tag "Matlab" matlab)
  179. (const :tag "Mscgen" mscgen)
  180. (const :tag "Ocaml" ocaml)
  181. (const :tag "Octave" octave)
  182. (const :tag "Org" org)
  183. (const :tag "Perl" perl)
  184. (const :tag "Pico Lisp" picolisp)
  185. (const :tag "PlantUML" plantuml)
  186. (const :tag "Python" python)
  187. (const :tag "Ruby" ruby)
  188. (const :tag "Sass" sass)
  189. (const :tag "Scala" scala)
  190. (const :tag "Scheme" scheme)
  191. (const :tag "Screen" screen)
  192. (const :tag "Shell Script" sh)
  193. (const :tag "Shen" shen)
  194. (const :tag "Sql" sql)
  195. (const :tag "Sqlite" sqlite))
  196. :value-type (boolean :tag "Activate" :value t)))
  197. ;;;; Customization variables
  198. (defcustom org-clone-delete-id nil
  199. "Remove ID property of clones of a subtree.
  200. When non-nil, clones of a subtree don't inherit the ID property.
  201. Otherwise they inherit the ID property with a new unique
  202. identifier."
  203. :type 'boolean
  204. :version "24.1"
  205. :group 'org-id)
  206. ;;; Version
  207. (require 'org-compat)
  208. (org-check-version)
  209. ;;;###autoload
  210. (defun org-version (&optional here full message)
  211. "Show the org-mode version in the echo area.
  212. With prefix argument HERE, insert it at point.
  213. When FULL is non-nil, use a verbose version string.
  214. When MESSAGE is non-nil, display a message with the version."
  215. (interactive "P")
  216. (let* ((org-dir (ignore-errors (org-find-library-dir "org")))
  217. (save-load-suffixes load-suffixes)
  218. (load-suffixes (list ".el"))
  219. (org-install-dir (ignore-errors (org-find-library-dir "org-loaddefs")))
  220. (org-trash (or
  221. (and (fboundp 'org-release) (fboundp 'org-git-version))
  222. (load (concat org-dir "org-version")
  223. 'noerror 'nomessage nil 'mustsuffix)))
  224. (load-suffixes save-load-suffixes)
  225. (org-version (org-release))
  226. (git-version (org-git-version))
  227. (version (format "Org-mode version %s (%s @ %s)"
  228. org-version
  229. git-version
  230. (if org-install-dir
  231. (if (string= org-dir org-install-dir)
  232. org-install-dir
  233. (concat "mixed installation! " org-install-dir " and " org-dir))
  234. "org-loaddefs.el can not be found!")))
  235. (_version (if full version org-version)))
  236. (if (org-called-interactively-p 'interactive)
  237. (if here
  238. (insert version)
  239. (message version))
  240. (if message (message _version))
  241. _version)))
  242. (defconst org-version (org-version))
  243. ;;; Compatibility constants
  244. ;;; The custom variables
  245. (defgroup org nil
  246. "Outline-based notes management and organizer."
  247. :tag "Org"
  248. :group 'outlines
  249. :group 'calendar)
  250. (defcustom org-mode-hook nil
  251. "Mode hook for Org-mode, run after the mode was turned on."
  252. :group 'org
  253. :type 'hook)
  254. (defcustom org-load-hook nil
  255. "Hook that is run after org.el has been loaded."
  256. :group 'org
  257. :type 'hook)
  258. (defcustom org-log-buffer-setup-hook nil
  259. "Hook that is run after an Org log buffer is created."
  260. :group 'org
  261. :version "24.1"
  262. :type 'hook)
  263. (defvar org-modules) ; defined below
  264. (defvar org-modules-loaded nil
  265. "Have the modules been loaded already?")
  266. (defun org-load-modules-maybe (&optional force)
  267. "Load all extensions listed in `org-modules'."
  268. (when (or force (not org-modules-loaded))
  269. (mapc (lambda (ext)
  270. (condition-case nil (require ext)
  271. (error (message "Problems while trying to load feature `%s'" ext))))
  272. org-modules)
  273. (setq org-modules-loaded t)))
  274. (defun org-set-modules (var value)
  275. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  276. (set var value)
  277. (when (featurep 'org)
  278. (org-load-modules-maybe 'force)))
  279. (when (org-bound-and-true-p org-modules)
  280. (let ((a (member 'org-infojs org-modules)))
  281. (and a (setcar a 'org-jsinfo))))
  282. (defcustom org-modules '(org-bbdb org-bibtex org-docview org-gnus org-info org-jsinfo org-irc org-mew org-mhe org-rmail org-vm org-w3m org-wl)
  283. "Modules that should always be loaded together with org.el.
  284. If a description starts with <C>, the file is not part of Emacs
  285. and loading it will require that you have downloaded and properly installed
  286. the org-mode distribution.
  287. You can also use this system to load external packages (i.e. neither Org
  288. core modules, nor modules from the CONTRIB directory). Just add symbols
  289. to the end of the list. If the package is called org-xyz.el, then you need
  290. to add the symbol `xyz', and the package must have a call to
  291. (provide 'org-xyz)"
  292. :group 'org
  293. :set 'org-set-modules
  294. :type
  295. '(set :greedy t
  296. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  297. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  298. (const :tag " crypt: Encryption of subtrees" org-crypt)
  299. (const :tag " ctags: Access to Emacs tags with links" org-ctags)
  300. (const :tag " docview: Links to doc-view buffers" org-docview)
  301. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  302. (const :tag " id: Global IDs for identifying entries" org-id)
  303. (const :tag " info: Links to Info nodes" org-info)
  304. (const :tag " jsinfo: Set up Sebastian Rose's JavaScript org-info.js" org-jsinfo)
  305. (const :tag " habit: Track your consistency with habits" org-habit)
  306. (const :tag " inlinetask: Tasks independent of outline hierarchy" org-inlinetask)
  307. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  308. (const :tag " mac-message: Links to messages in Apple Mail" org-mac-message)
  309. (const :tag " mew Links to Mew folders/messages" org-mew)
  310. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  311. (const :tag " protocol: Intercept calls from emacsclient" org-protocol)
  312. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  313. (const :tag " special-blocks: Turn blocks into LaTeX envs and HTML divs" org-special-blocks)
  314. (const :tag " vm: Links to VM folders/messages" org-vm)
  315. (const :tag " wl: Links to Wanderlust folders/messages" org-wl)
  316. (const :tag " w3m: Special cut/paste from w3m to Org-mode." org-w3m)
  317. (const :tag " mouse: Additional mouse support" org-mouse)
  318. (const :tag " TaskJuggler: Export tasks to a TaskJuggler project" org-taskjuggler)
  319. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  320. (const :tag "C bookmark: Org-mode links to bookmarks" org-bookmark)
  321. (const :tag "C checklist: Extra functions for checklists in repeated tasks" org-checklist)
  322. (const :tag "C choose: Use TODO keywords to mark decisions states" org-choose)
  323. (const :tag "C collector: Collect properties into tables" org-collector)
  324. (const :tag "C depend: TODO dependencies for Org-mode\n\t\t\t(PARTIALLY OBSOLETE, see built-in dependency support))" org-depend)
  325. (const :tag "C drill: Flashcards and spaced repetition for Org-mode" org-drill)
  326. (const :tag "C elisp-symbol: Org-mode links to emacs-lisp symbols" org-elisp-symbol)
  327. (const :tag "C eshell Support for links to working directories in eshell" org-eshell)
  328. (const :tag "C eval: Include command output as text" org-eval)
  329. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  330. (const :tag "C expiry: Expiry mechanism for Org-mode entries" org-expiry)
  331. (const :tag "C exp-bibtex: Export citations using BibTeX" org-exp-bibtex)
  332. (const :tag "C git-link: Provide org links to specific file version" org-git-link)
  333. (const :tag "C interactive-query: Interactive modification of tags query\n\t\t\t(PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query)
  334. (const :tag "C invoice: Help manage client invoices in Org-mode" org-invoice)
  335. (const :tag "C jira: Add a jira:ticket protocol to Org-mode" org-jira)
  336. (const :tag "C learn: SuperMemo's incremental learning algorithm" org-learn)
  337. (const :tag "C mairix: Hook mairix search into Org-mode for different MUAs" org-mairix)
  338. (const :tag "C notmuch: Provide org links to notmuch searches or messages" org-notmuch)
  339. (const :tag "C mac-iCal Imports events from iCal.app to the Emacs diary" org-mac-iCal)
  340. (const :tag "C mac-link-grabber Grab links and URLs from various Mac applications" org-mac-link-grabber)
  341. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  342. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  343. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  344. (const :tag "C registry: A registry for Org-mode links" org-registry)
  345. (const :tag "C org2rem: Convert org appointments into reminders" org2rem)
  346. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  347. (const :tag "C secretary: Team management with org-mode" org-secretary)
  348. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  349. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  350. (const :tag "C track: Keep up with Org-mode development" org-track)
  351. (const :tag "C velocity Something like Notational Velocity for Org" org-velocity)
  352. (const :tag "C wikinodes: CamelCase wiki-like links" org-wikinodes)
  353. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  354. (defcustom org-support-shift-select nil
  355. "Non-nil means make shift-cursor commands select text when possible.
  356. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys
  357. start selecting a region, or enlarge regions started in this way.
  358. In Org-mode, in special contexts, these same keys are used for
  359. other purposes, important enough to compete with shift selection.
  360. Org tries to balance these needs by supporting `shift-select-mode'
  361. outside these special contexts, under control of this variable.
  362. The default of this variable is nil, to avoid confusing behavior. Shifted
  363. cursor keys will then execute Org commands in the following contexts:
  364. - on a headline, changing TODO state (left/right) and priority (up/down)
  365. - on a time stamp, changing the time
  366. - in a plain list item, changing the bullet type
  367. - in a property definition line, switching between allowed values
  368. - in the BEGIN line of a clock table (changing the time block).
  369. Outside these contexts, the commands will throw an error.
  370. When this variable is t and the cursor is not in a special
  371. context, Org-mode will support shift-selection for making and
  372. enlarging regions. To make this more effective, the bullet
  373. cycling will no longer happen anywhere in an item line, but only
  374. if the cursor is exactly on the bullet.
  375. If you set this variable to the symbol `always', then the keys
  376. will not be special in headlines, property lines, and item lines,
  377. to make shift selection work there as well. If this is what you
  378. want, you can use the following alternative commands: `C-c C-t'
  379. and `C-c ,' to change TODO state and priority, `C-u C-u C-c C-t'
  380. can be used to switch TODO sets, `C-c -' to cycle item bullet
  381. types, and properties can be edited by hand or in column view.
  382. However, when the cursor is on a timestamp, shift-cursor commands
  383. will still edit the time stamp - this is just too good to give up.
  384. XEmacs user should have this variable set to nil, because
  385. `shift-select-mode' is in Emacs 23 or later only."
  386. :group 'org
  387. :type '(choice
  388. (const :tag "Never" nil)
  389. (const :tag "When outside special context" t)
  390. (const :tag "Everywhere except timestamps" always)))
  391. (defcustom org-loop-over-headlines-in-active-region nil
  392. "Shall some commands act upon headlines in the active region?
  393. When set to `t', some commands will be performed in all headlines
  394. within the active region.
  395. When set to `start-level', some commands will be performed in all
  396. headlines within the active region, provided that these headlines
  397. are of the same level than the first one.
  398. When set to a string, those commands will be performed on the
  399. matching headlines within the active region. Such string must be
  400. a tags/property/todo match as it is used in the agenda tags view.
  401. The list of commands is: `org-schedule', `org-deadline',
  402. `org-todo', `org-archive-subtree', `org-archive-set-tag' and
  403. `org-archive-to-archive-sibling'. The archiving commands skip
  404. already archived entries."
  405. :type '(choice (const :tag "Don't loop" nil)
  406. (const :tag "All headlines in active region" t)
  407. (const :tag "In active region, headlines at the same level than the first one" 'start-level)
  408. (string :tag "Tags/Property/Todo matcher"))
  409. :version "24.1"
  410. :group 'org-todo
  411. :group 'org-archive)
  412. (defgroup org-startup nil
  413. "Options concerning startup of Org-mode."
  414. :tag "Org Startup"
  415. :group 'org)
  416. (defcustom org-startup-folded t
  417. "Non-nil means entering Org-mode will switch to OVERVIEW.
  418. This can also be configured on a per-file basis by adding one of
  419. the following lines anywhere in the buffer:
  420. #+STARTUP: fold (or `overview', this is equivalent)
  421. #+STARTUP: nofold (or `showall', this is equivalent)
  422. #+STARTUP: content
  423. #+STARTUP: showeverything"
  424. :group 'org-startup
  425. :type '(choice
  426. (const :tag "nofold: show all" nil)
  427. (const :tag "fold: overview" t)
  428. (const :tag "content: all headlines" content)
  429. (const :tag "show everything, even drawers" showeverything)))
  430. (defcustom org-startup-truncated t
  431. "Non-nil means entering Org-mode will set `truncate-lines'.
  432. This is useful since some lines containing links can be very long and
  433. uninteresting. Also tables look terrible when wrapped."
  434. :group 'org-startup
  435. :type 'boolean)
  436. (defcustom org-startup-indented nil
  437. "Non-nil means turn on `org-indent-mode' on startup.
  438. This can also be configured on a per-file basis by adding one of
  439. the following lines anywhere in the buffer:
  440. #+STARTUP: indent
  441. #+STARTUP: noindent"
  442. :group 'org-structure
  443. :type '(choice
  444. (const :tag "Not" nil)
  445. (const :tag "Globally (slow on startup in large files)" t)))
  446. (defcustom org-use-sub-superscripts t
  447. "Non-nil means interpret \"_\" and \"^\" for export.
  448. When this option is turned on, you can use TeX-like syntax for sub- and
  449. superscripts. Several characters after \"_\" or \"^\" will be
  450. considered as a single item - so grouping with {} is normally not
  451. needed. For example, the following things will be parsed as single
  452. sub- or superscripts.
  453. 10^24 or 10^tau several digits will be considered 1 item.
  454. 10^-12 or 10^-tau a leading sign with digits or a word
  455. x^2-y^3 will be read as x^2 - y^3, because items are
  456. terminated by almost any nonword/nondigit char.
  457. x_{i^2} or x^(2-i) braces or parenthesis do grouping.
  458. Still, ambiguity is possible - so when in doubt use {} to enclose the
  459. sub/superscript. If you set this variable to the symbol `{}',
  460. the braces are *required* in order to trigger interpretations as
  461. sub/superscript. This can be helpful in documents that need \"_\"
  462. frequently in plain text.
  463. Not all export backends support this, but HTML does.
  464. This option can also be set with the #+OPTIONS line, e.g. \"^:nil\"."
  465. :group 'org-startup
  466. :group 'org-export-translation
  467. :version "24.1"
  468. :type '(choice
  469. (const :tag "Always interpret" t)
  470. (const :tag "Only with braces" {})
  471. (const :tag "Never interpret" nil)))
  472. (if (fboundp 'defvaralias)
  473. (defvaralias 'org-export-with-sub-superscripts 'org-use-sub-superscripts))
  474. (defcustom org-startup-with-beamer-mode nil
  475. "Non-nil means turn on `org-beamer-mode' on startup.
  476. This can also be configured on a per-file basis by adding one of
  477. the following lines anywhere in the buffer:
  478. #+STARTUP: beamer"
  479. :group 'org-startup
  480. :version "24.1"
  481. :type 'boolean)
  482. (defcustom org-startup-align-all-tables nil
  483. "Non-nil means align all tables when visiting a file.
  484. This is useful when the column width in tables is forced with <N> cookies
  485. in table fields. Such tables will look correct only after the first re-align.
  486. This can also be configured on a per-file basis by adding one of
  487. the following lines anywhere in the buffer:
  488. #+STARTUP: align
  489. #+STARTUP: noalign"
  490. :group 'org-startup
  491. :type 'boolean)
  492. (defcustom org-startup-with-inline-images nil
  493. "Non-nil means show inline images when loading a new Org file.
  494. This can also be configured on a per-file basis by adding one of
  495. the following lines anywhere in the buffer:
  496. #+STARTUP: inlineimages
  497. #+STARTUP: noinlineimages"
  498. :group 'org-startup
  499. :version "24.1"
  500. :type 'boolean)
  501. (defcustom org-insert-mode-line-in-empty-file nil
  502. "Non-nil means insert the first line setting Org-mode in empty files.
  503. When the function `org-mode' is called interactively in an empty file, this
  504. normally means that the file name does not automatically trigger Org-mode.
  505. To ensure that the file will always be in Org-mode in the future, a
  506. line enforcing Org-mode will be inserted into the buffer, if this option
  507. has been set."
  508. :group 'org-startup
  509. :type 'boolean)
  510. (defcustom org-replace-disputed-keys nil
  511. "Non-nil means use alternative key bindings for some keys.
  512. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  513. These keys are also used by other packages like shift-selection-mode'
  514. \(built into Emacs 23), `CUA-mode' or `windmove.el'.
  515. If you want to use Org-mode together with one of these other modes,
  516. or more generally if you would like to move some Org-mode commands to
  517. other keys, set this variable and configure the keys with the variable
  518. `org-disputed-keys'.
  519. This option is only relevant at load-time of Org-mode, and must be set
  520. *before* org.el is loaded. Changing it requires a restart of Emacs to
  521. become effective."
  522. :group 'org-startup
  523. :type 'boolean)
  524. (defcustom org-use-extra-keys nil
  525. "Non-nil means use extra key sequence definitions for certain commands.
  526. This happens automatically if you run XEmacs or if `window-system'
  527. is nil. This variable lets you do the same manually. You must
  528. set it before loading org.
  529. Example: on Carbon Emacs 22 running graphically, with an external
  530. keyboard on a Powerbook, the default way of setting M-left might
  531. not work for either Alt or ESC. Setting this variable will make
  532. it work for ESC."
  533. :group 'org-startup
  534. :type 'boolean)
  535. (if (fboundp 'defvaralias)
  536. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  537. (defcustom org-disputed-keys
  538. '(([(shift up)] . [(meta p)])
  539. ([(shift down)] . [(meta n)])
  540. ([(shift left)] . [(meta -)])
  541. ([(shift right)] . [(meta +)])
  542. ([(control shift right)] . [(meta shift +)])
  543. ([(control shift left)] . [(meta shift -)]))
  544. "Keys for which Org-mode and other modes compete.
  545. This is an alist, cars are the default keys, second element specifies
  546. the alternative to use when `org-replace-disputed-keys' is t.
  547. Keys can be specified in any syntax supported by `define-key'.
  548. The value of this option takes effect only at Org-mode's startup,
  549. therefore you'll have to restart Emacs to apply it after changing."
  550. :group 'org-startup
  551. :type 'alist)
  552. (defun org-key (key)
  553. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  554. Or return the original if not disputed.
  555. Also apply the translations defined in `org-xemacs-key-equivalents'."
  556. (when org-replace-disputed-keys
  557. (let* ((nkey (key-description key))
  558. (x (org-find-if (lambda (x)
  559. (equal (key-description (car x)) nkey))
  560. org-disputed-keys)))
  561. (setq key (if x (cdr x) key))))
  562. (when (featurep 'xemacs)
  563. (setq key (or (cdr (assoc key org-xemacs-key-equivalents)) key)))
  564. key)
  565. (defun org-find-if (predicate seq)
  566. (catch 'exit
  567. (while seq
  568. (if (funcall predicate (car seq))
  569. (throw 'exit (car seq))
  570. (pop seq)))))
  571. (defun org-defkey (keymap key def)
  572. "Define a key, possibly translated, as returned by `org-key'."
  573. (define-key keymap (org-key key) def))
  574. (defcustom org-ellipsis nil
  575. "The ellipsis to use in the Org-mode outline.
  576. When nil, just use the standard three dots. When a string, use that instead,
  577. When a face, use the standard 3 dots, but with the specified face.
  578. The change affects only Org-mode (which will then use its own display table).
  579. Changing this requires executing `M-x org-mode' in a buffer to become
  580. effective."
  581. :group 'org-startup
  582. :type '(choice (const :tag "Default" nil)
  583. (face :tag "Face" :value org-warning)
  584. (string :tag "String" :value "...#")))
  585. (defvar org-display-table nil
  586. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  587. (defgroup org-keywords nil
  588. "Keywords in Org-mode."
  589. :tag "Org Keywords"
  590. :group 'org)
  591. (defcustom org-deadline-string "DEADLINE:"
  592. "String to mark deadline entries.
  593. A deadline is this string, followed by a time stamp. Should be a word,
  594. terminated by a colon. You can insert a schedule keyword and
  595. a timestamp with \\[org-deadline].
  596. Changes become only effective after restarting Emacs."
  597. :group 'org-keywords
  598. :type 'string)
  599. (defcustom org-scheduled-string "SCHEDULED:"
  600. "String to mark scheduled TODO entries.
  601. A schedule is this string, followed by a time stamp. Should be a word,
  602. terminated by a colon. You can insert a schedule keyword and
  603. a timestamp with \\[org-schedule].
  604. Changes become only effective after restarting Emacs."
  605. :group 'org-keywords
  606. :type 'string)
  607. (defcustom org-closed-string "CLOSED:"
  608. "String used as the prefix for timestamps logging closing a TODO entry."
  609. :group 'org-keywords
  610. :type 'string)
  611. (defcustom org-clock-string "CLOCK:"
  612. "String used as prefix for timestamps clocking work hours on an item."
  613. :group 'org-keywords
  614. :type 'string)
  615. (defconst org-planning-or-clock-line-re (concat "^[ \t]*\\("
  616. org-scheduled-string "\\|"
  617. org-deadline-string "\\|"
  618. org-closed-string "\\|"
  619. org-clock-string "\\)")
  620. "Matches a line with planning or clock info.")
  621. (defcustom org-comment-string "COMMENT"
  622. "Entries starting with this keyword will never be exported.
  623. An entry can be toggled between COMMENT and normal with
  624. \\[org-toggle-comment].
  625. Changes become only effective after restarting Emacs."
  626. :group 'org-keywords
  627. :type 'string)
  628. (defcustom org-quote-string "QUOTE"
  629. "Entries starting with this keyword will be exported in fixed-width font.
  630. Quoting applies only to the text in the entry following the headline, and does
  631. not extend beyond the next headline, even if that is lower level.
  632. An entry can be toggled between QUOTE and normal with
  633. \\[org-toggle-fixed-width-section]."
  634. :group 'org-keywords
  635. :type 'string)
  636. (defconst org-repeat-re
  637. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*?\\([.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)"
  638. "Regular expression for specifying repeated events.
  639. After a match, group 1 contains the repeat expression.")
  640. (defgroup org-structure nil
  641. "Options concerning the general structure of Org-mode files."
  642. :tag "Org Structure"
  643. :group 'org)
  644. (defgroup org-reveal-location nil
  645. "Options about how to make context of a location visible."
  646. :tag "Org Reveal Location"
  647. :group 'org-structure)
  648. (defconst org-context-choice
  649. '(choice
  650. (const :tag "Always" t)
  651. (const :tag "Never" nil)
  652. (repeat :greedy t :tag "Individual contexts"
  653. (cons
  654. (choice :tag "Context"
  655. (const agenda)
  656. (const org-goto)
  657. (const occur-tree)
  658. (const tags-tree)
  659. (const link-search)
  660. (const mark-goto)
  661. (const bookmark-jump)
  662. (const isearch)
  663. (const default))
  664. (boolean))))
  665. "Contexts for the reveal options.")
  666. (defcustom org-show-hierarchy-above '((default . t))
  667. "Non-nil means show full hierarchy when revealing a location.
  668. Org-mode often shows locations in an org-mode file which might have
  669. been invisible before. When this is set, the hierarchy of headings
  670. above the exposed location is shown.
  671. Turning this off for example for sparse trees makes them very compact.
  672. Instead of t, this can also be an alist specifying this option for different
  673. contexts. Valid contexts are
  674. agenda when exposing an entry from the agenda
  675. org-goto when using the command `org-goto' on key C-c C-j
  676. occur-tree when using the command `org-occur' on key C-c /
  677. tags-tree when constructing a sparse tree based on tags matches
  678. link-search when exposing search matches associated with a link
  679. mark-goto when exposing the jump goal of a mark
  680. bookmark-jump when exposing a bookmark location
  681. isearch when exiting from an incremental search
  682. default default for all contexts not set explicitly"
  683. :group 'org-reveal-location
  684. :type org-context-choice)
  685. (defcustom org-show-following-heading '((default . nil))
  686. "Non-nil means show following heading when revealing a location.
  687. Org-mode often shows locations in an org-mode file which might have
  688. been invisible before. When this is set, the heading following the
  689. match is shown.
  690. Turning this off for example for sparse trees makes them very compact,
  691. but makes it harder to edit the location of the match. In such a case,
  692. use the command \\[org-reveal] to show more context.
  693. Instead of t, this can also be an alist specifying this option for different
  694. contexts. See `org-show-hierarchy-above' for valid contexts."
  695. :group 'org-reveal-location
  696. :type org-context-choice)
  697. (defcustom org-show-siblings '((default . nil) (isearch t))
  698. "Non-nil means show all sibling heading when revealing a location.
  699. Org-mode often shows locations in an org-mode file which might have
  700. been invisible before. When this is set, the sibling of the current entry
  701. heading are all made visible. If `org-show-hierarchy-above' is t,
  702. the same happens on each level of the hierarchy above the current entry.
  703. By default this is on for the isearch context, off for all other contexts.
  704. Turning this off for example for sparse trees makes them very compact,
  705. but makes it harder to edit the location of the match. In such a case,
  706. use the command \\[org-reveal] to show more context.
  707. Instead of t, this can also be an alist specifying this option for different
  708. contexts. See `org-show-hierarchy-above' for valid contexts."
  709. :group 'org-reveal-location
  710. :type org-context-choice)
  711. (defcustom org-show-entry-below '((default . nil))
  712. "Non-nil means show the entry below a headline when revealing a location.
  713. Org-mode often shows locations in an org-mode file which might have
  714. been invisible before. When this is set, the text below the headline that is
  715. exposed is also shown.
  716. By default this is off for all contexts.
  717. Instead of t, this can also be an alist specifying this option for different
  718. contexts. See `org-show-hierarchy-above' for valid contexts."
  719. :group 'org-reveal-location
  720. :type org-context-choice)
  721. (defcustom org-indirect-buffer-display 'other-window
  722. "How should indirect tree buffers be displayed?
  723. This applies to indirect buffers created with the commands
  724. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  725. Valid values are:
  726. current-window Display in the current window
  727. other-window Just display in another window.
  728. dedicated-frame Create one new frame, and re-use it each time.
  729. new-frame Make a new frame each time. Note that in this case
  730. previously-made indirect buffers are kept, and you need to
  731. kill these buffers yourself."
  732. :group 'org-structure
  733. :group 'org-agenda-windows
  734. :type '(choice
  735. (const :tag "In current window" current-window)
  736. (const :tag "In current frame, other window" other-window)
  737. (const :tag "Each time a new frame" new-frame)
  738. (const :tag "One dedicated frame" dedicated-frame)))
  739. (defcustom org-use-speed-commands nil
  740. "Non-nil means activate single letter commands at beginning of a headline.
  741. This may also be a function to test for appropriate locations where speed
  742. commands should be active."
  743. :group 'org-structure
  744. :type '(choice
  745. (const :tag "Never" nil)
  746. (const :tag "At beginning of headline stars" t)
  747. (function)))
  748. (defcustom org-speed-commands-user nil
  749. "Alist of additional speed commands.
  750. This list will be checked before `org-speed-commands-default'
  751. when the variable `org-use-speed-commands' is non-nil
  752. and when the cursor is at the beginning of a headline.
  753. The car if each entry is a string with a single letter, which must
  754. be assigned to `self-insert-command' in the global map.
  755. The cdr is either a command to be called interactively, a function
  756. to be called, or a form to be evaluated.
  757. An entry that is just a list with a single string will be interpreted
  758. as a descriptive headline that will be added when listing the speed
  759. commands in the Help buffer using the `?' speed command."
  760. :group 'org-structure
  761. :type '(repeat :value ("k" . ignore)
  762. (choice :value ("k" . ignore)
  763. (list :tag "Descriptive Headline" (string :tag "Headline"))
  764. (cons :tag "Letter and Command"
  765. (string :tag "Command letter")
  766. (choice
  767. (function)
  768. (sexp))))))
  769. (defgroup org-cycle nil
  770. "Options concerning visibility cycling in Org-mode."
  771. :tag "Org Cycle"
  772. :group 'org-structure)
  773. (defcustom org-cycle-skip-children-state-if-no-children t
  774. "Non-nil means skip CHILDREN state in entries that don't have any."
  775. :group 'org-cycle
  776. :type 'boolean)
  777. (defcustom org-cycle-max-level nil
  778. "Maximum level which should still be subject to visibility cycling.
  779. Levels higher than this will, for cycling, be treated as text, not a headline.
  780. When `org-odd-levels-only' is set, a value of N in this variable actually
  781. means 2N-1 stars as the limiting headline.
  782. When nil, cycle all levels.
  783. Note that the limiting level of cycling is also influenced by
  784. `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
  785. `org-inlinetask-min-level' is, cycling will be limited to levels one less
  786. than its value."
  787. :group 'org-cycle
  788. :type '(choice
  789. (const :tag "No limit" nil)
  790. (integer :tag "Maximum level")))
  791. (defcustom org-drawers '("PROPERTIES" "CLOCK" "LOGBOOK" "RESULTS")
  792. "Names of drawers. Drawers are not opened by cycling on the headline above.
  793. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  794. this:
  795. :DRAWERNAME:
  796. .....
  797. :END:
  798. The drawer \"PROPERTIES\" is special for capturing properties through
  799. the property API.
  800. Drawers can be defined on the per-file basis with a line like:
  801. #+DRAWERS: HIDDEN STATE PROPERTIES"
  802. :group 'org-structure
  803. :group 'org-cycle
  804. :type '(repeat (string :tag "Drawer Name")))
  805. (defcustom org-hide-block-startup nil
  806. "Non-nil means entering Org-mode will fold all blocks.
  807. This can also be set in on a per-file basis with
  808. #+STARTUP: hideblocks
  809. #+STARTUP: showblocks"
  810. :group 'org-startup
  811. :group 'org-cycle
  812. :type 'boolean)
  813. (defcustom org-cycle-global-at-bob nil
  814. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  815. This makes it possible to do global cycling without having to use S-TAB or
  816. \\[universal-argument] TAB. For this special case to work, the first line
  817. of the buffer must not be a headline -- it may be empty or some other text.
  818. When used in this way, `org-cycle-hook' is disabled temporarily to make
  819. sure the cursor stays at the beginning of the buffer. When this option is
  820. nil, don't do anything special at the beginning of the buffer."
  821. :group 'org-cycle
  822. :type 'boolean)
  823. (defcustom org-cycle-level-after-item/entry-creation t
  824. "Non-nil means cycle entry level or item indentation in new empty entries.
  825. When the cursor is at the end of an empty headline, i.e with only stars
  826. and maybe a TODO keyword, TAB will then switch the entry to become a child,
  827. and then all possible ancestor states, before returning to the original state.
  828. This makes data entry extremely fast: M-RET to create a new headline,
  829. on TAB to make it a child, two or more tabs to make it a (grand-)uncle.
  830. When the cursor is at the end of an empty plain list item, one TAB will
  831. make it a subitem, two or more tabs will back up to make this an item
  832. higher up in the item hierarchy."
  833. :group 'org-cycle
  834. :type 'boolean)
  835. (defcustom org-cycle-emulate-tab t
  836. "Where should `org-cycle' emulate TAB.
  837. nil Never
  838. white Only in completely white lines
  839. whitestart Only at the beginning of lines, before the first non-white char
  840. t Everywhere except in headlines
  841. exc-hl-bol Everywhere except at the start of a headline
  842. If TAB is used in a place where it does not emulate TAB, the current subtree
  843. visibility is cycled."
  844. :group 'org-cycle
  845. :type '(choice (const :tag "Never" nil)
  846. (const :tag "Only in completely white lines" white)
  847. (const :tag "Before first char in a line" whitestart)
  848. (const :tag "Everywhere except in headlines" t)
  849. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  850. ))
  851. (defcustom org-cycle-separator-lines 2
  852. "Number of empty lines needed to keep an empty line between collapsed trees.
  853. If you leave an empty line between the end of a subtree and the following
  854. headline, this empty line is hidden when the subtree is folded.
  855. Org-mode will leave (exactly) one empty line visible if the number of
  856. empty lines is equal or larger to the number given in this variable.
  857. So the default 2 means at least 2 empty lines after the end of a subtree
  858. are needed to produce free space between a collapsed subtree and the
  859. following headline.
  860. If the number is negative, and the number of empty lines is at least -N,
  861. all empty lines are shown.
  862. Special case: when 0, never leave empty lines in collapsed view."
  863. :group 'org-cycle
  864. :type 'integer)
  865. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  866. (defcustom org-pre-cycle-hook nil
  867. "Hook that is run before visibility cycling is happening.
  868. The function(s) in this hook must accept a single argument which indicates
  869. the new state that will be set right after running this hook. The
  870. argument is a symbol. Before a global state change, it can have the values
  871. `overview', `content', or `all'. Before a local state change, it can have
  872. the values `folded', `children', or `subtree'."
  873. :group 'org-cycle
  874. :type 'hook)
  875. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  876. org-cycle-hide-drawers
  877. org-cycle-hide-inline-tasks
  878. org-cycle-show-empty-lines
  879. org-optimize-window-after-visibility-change)
  880. "Hook that is run after `org-cycle' has changed the buffer visibility.
  881. The function(s) in this hook must accept a single argument which indicates
  882. the new state that was set by the most recent `org-cycle' command. The
  883. argument is a symbol. After a global state change, it can have the values
  884. `overview', `contents', or `all'. After a local state change, it can have
  885. the values `folded', `children', or `subtree'."
  886. :group 'org-cycle
  887. :type 'hook)
  888. (defgroup org-edit-structure nil
  889. "Options concerning structure editing in Org-mode."
  890. :tag "Org Edit Structure"
  891. :group 'org-structure)
  892. (defcustom org-odd-levels-only nil
  893. "Non-nil means skip even levels and only use odd levels for the outline.
  894. This has the effect that two stars are being added/taken away in
  895. promotion/demotion commands. It also influences how levels are
  896. handled by the exporters.
  897. Changing it requires restart of `font-lock-mode' to become effective
  898. for fontification also in regions already fontified.
  899. You may also set this on a per-file basis by adding one of the following
  900. lines to the buffer:
  901. #+STARTUP: odd
  902. #+STARTUP: oddeven"
  903. :group 'org-edit-structure
  904. :group 'org-appearance
  905. :type 'boolean)
  906. (defcustom org-adapt-indentation t
  907. "Non-nil means adapt indentation to outline node level.
  908. When this variable is set, Org assumes that you write outlines by
  909. indenting text in each node to align with the headline (after the stars).
  910. The following issues are influenced by this variable:
  911. - When this is set and the *entire* text in an entry is indented, the
  912. indentation is increased by one space in a demotion command, and
  913. decreased by one in a promotion command. If any line in the entry
  914. body starts with text at column 0, indentation is not changed at all.
  915. - Property drawers and planning information is inserted indented when
  916. this variable s set. When nil, they will not be indented.
  917. - TAB indents a line relative to context. The lines below a headline
  918. will be indented when this variable is set.
  919. Note that this is all about true indentation, by adding and removing
  920. space characters. See also `org-indent.el' which does level-dependent
  921. indentation in a virtual way, i.e. at display time in Emacs."
  922. :group 'org-edit-structure
  923. :type 'boolean)
  924. (defcustom org-special-ctrl-a/e nil
  925. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  926. When t, `C-a' will bring back the cursor to the beginning of the
  927. headline text, i.e. after the stars and after a possible TODO
  928. keyword. In an item, this will be the position after bullet and
  929. check-box, if any. When the cursor is already at that position,
  930. another `C-a' will bring it to the beginning of the line.
  931. `C-e' will jump to the end of the headline, ignoring the presence
  932. of tags in the headline. A second `C-e' will then jump to the
  933. true end of the line, after any tags. This also means that, when
  934. this variable is non-nil, `C-e' also will never jump beyond the
  935. end of the heading of a folded section, i.e. not after the
  936. ellipses.
  937. When set to the symbol `reversed', the first `C-a' or `C-e' works
  938. normally, going to the true line boundary first. Only a directly
  939. following, identical keypress will bring the cursor to the
  940. special positions.
  941. This may also be a cons cell where the behavior for `C-a' and
  942. `C-e' is set separately."
  943. :group 'org-edit-structure
  944. :type '(choice
  945. (const :tag "off" nil)
  946. (const :tag "on: after stars/bullet and before tags first" t)
  947. (const :tag "reversed: true line boundary first" reversed)
  948. (cons :tag "Set C-a and C-e separately"
  949. (choice :tag "Special C-a"
  950. (const :tag "off" nil)
  951. (const :tag "on: after stars/bullet first" t)
  952. (const :tag "reversed: before stars/bullet first" reversed))
  953. (choice :tag "Special C-e"
  954. (const :tag "off" nil)
  955. (const :tag "on: before tags first" t)
  956. (const :tag "reversed: after tags first" reversed)))))
  957. (if (fboundp 'defvaralias)
  958. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  959. (defcustom org-special-ctrl-k nil
  960. "Non-nil means `C-k' will behave specially in headlines.
  961. When nil, `C-k' will call the default `kill-line' command.
  962. When t, the following will happen while the cursor is in the headline:
  963. - When the cursor is at the beginning of a headline, kill the entire
  964. line and possible the folded subtree below the line.
  965. - When in the middle of the headline text, kill the headline up to the tags.
  966. - When after the headline text, kill the tags."
  967. :group 'org-edit-structure
  968. :type 'boolean)
  969. (defcustom org-ctrl-k-protect-subtree nil
  970. "Non-nil means, do not delete a hidden subtree with C-k.
  971. When set to the symbol `error', simply throw an error when C-k is
  972. used to kill (part-of) a headline that has hidden text behind it.
  973. Any other non-nil value will result in a query to the user, if it is
  974. OK to kill that hidden subtree. When nil, kill without remorse."
  975. :group 'org-edit-structure
  976. :version "24.1"
  977. :type '(choice
  978. (const :tag "Do not protect hidden subtrees" nil)
  979. (const :tag "Protect hidden subtrees with a security query" t)
  980. (const :tag "Never kill a hidden subtree with C-k" error)))
  981. (defcustom org-catch-invisible-edits nil
  982. "Check if in invisible region before inserting or deleting a character.
  983. Valid values are:
  984. nil Do not check, so just do invisible edits.
  985. error Throw an error and do nothing.
  986. show Make point visible, and do the requested edit.
  987. show-and-error Make point visible, then throw an error and abort the edit.
  988. smart Make point visible, and do insertion/deletion if it is
  989. adjacent to visible text and the change feels predictable.
  990. Never delete a previously invisible character or add in the
  991. middle or right after an invisible region. Basically, this
  992. allows insertion and backward-delete right before ellipses.
  993. FIXME: maybe in this case we should not even show?"
  994. :group 'org-edit-structure
  995. :version "24.1"
  996. :type '(choice
  997. (const :tag "Do not check" nil)
  998. (const :tag "Throw error when trying to edit" error)
  999. (const :tag "Unhide, but do not do the edit" show-and-error)
  1000. (const :tag "Show invisible part and do the edit" show)
  1001. (const :tag "Be smart and do the right thing" smart)))
  1002. (defcustom org-yank-folded-subtrees t
  1003. "Non-nil means when yanking subtrees, fold them.
  1004. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  1005. it starts with a heading and all other headings in it are either children
  1006. or siblings, then fold all the subtrees. However, do this only if no
  1007. text after the yank would be swallowed into a folded tree by this action."
  1008. :group 'org-edit-structure
  1009. :type 'boolean)
  1010. (defcustom org-yank-adjusted-subtrees nil
  1011. "Non-nil means when yanking subtrees, adjust the level.
  1012. With this setting, `org-paste-subtree' is used to insert the subtree, see
  1013. this function for details."
  1014. :group 'org-edit-structure
  1015. :type 'boolean)
  1016. (defcustom org-M-RET-may-split-line '((default . t))
  1017. "Non-nil means M-RET will split the line at the cursor position.
  1018. When nil, it will go to the end of the line before making a
  1019. new line.
  1020. You may also set this option in a different way for different
  1021. contexts. Valid contexts are:
  1022. headline when creating a new headline
  1023. item when creating a new item
  1024. table in a table field
  1025. default the value to be used for all contexts not explicitly
  1026. customized"
  1027. :group 'org-structure
  1028. :group 'org-table
  1029. :type '(choice
  1030. (const :tag "Always" t)
  1031. (const :tag "Never" nil)
  1032. (repeat :greedy t :tag "Individual contexts"
  1033. (cons
  1034. (choice :tag "Context"
  1035. (const headline)
  1036. (const item)
  1037. (const table)
  1038. (const default))
  1039. (boolean)))))
  1040. (defcustom org-insert-heading-respect-content nil
  1041. "Non-nil means insert new headings after the current subtree.
  1042. When nil, the new heading is created directly after the current line.
  1043. The commands \\[org-insert-heading-respect-content] and
  1044. \\[org-insert-todo-heading-respect-content] turn this variable on
  1045. for the duration of the command."
  1046. :group 'org-structure
  1047. :type 'boolean)
  1048. (defcustom org-blank-before-new-entry '((heading . auto)
  1049. (plain-list-item . auto))
  1050. "Should `org-insert-heading' leave a blank line before new heading/item?
  1051. The value is an alist, with `heading' and `plain-list-item' as CAR,
  1052. and a boolean flag as CDR. The cdr may also be the symbol `auto', in
  1053. which case Org will look at the surrounding headings/items and try to
  1054. make an intelligent decision whether to insert a blank line or not.
  1055. For plain lists, if the variable `org-empty-line-terminates-plain-lists' is
  1056. set, the setting here is ignored and no empty line is inserted, to avoid
  1057. breaking the list structure."
  1058. :group 'org-edit-structure
  1059. :type '(list
  1060. (cons (const heading)
  1061. (choice (const :tag "Never" nil)
  1062. (const :tag "Always" t)
  1063. (const :tag "Auto" auto)))
  1064. (cons (const plain-list-item)
  1065. (choice (const :tag "Never" nil)
  1066. (const :tag "Always" t)
  1067. (const :tag "Auto" auto)))))
  1068. (defcustom org-insert-heading-hook nil
  1069. "Hook being run after inserting a new heading."
  1070. :group 'org-edit-structure
  1071. :type 'hook)
  1072. (defcustom org-enable-fixed-width-editor t
  1073. "Non-nil means lines starting with \":\" are treated as fixed-width.
  1074. This currently only means they are never auto-wrapped.
  1075. When nil, such lines will be treated like ordinary lines.
  1076. See also the QUOTE keyword."
  1077. :group 'org-edit-structure
  1078. :type 'boolean)
  1079. (defcustom org-goto-auto-isearch t
  1080. "Non-nil means typing characters in `org-goto' starts incremental search."
  1081. :group 'org-edit-structure
  1082. :type 'boolean)
  1083. (defgroup org-sparse-trees nil
  1084. "Options concerning sparse trees in Org-mode."
  1085. :tag "Org Sparse Trees"
  1086. :group 'org-structure)
  1087. (defcustom org-highlight-sparse-tree-matches t
  1088. "Non-nil means highlight all matches that define a sparse tree.
  1089. The highlights will automatically disappear the next time the buffer is
  1090. changed by an edit command."
  1091. :group 'org-sparse-trees
  1092. :type 'boolean)
  1093. (defcustom org-remove-highlights-with-change t
  1094. "Non-nil means any change to the buffer will remove temporary highlights.
  1095. Such highlights are created by `org-occur' and `org-clock-display'.
  1096. When nil, `C-c C-c needs to be used to get rid of the highlights.
  1097. The highlights created by `org-preview-latex-fragment' always need
  1098. `C-c C-c' to be removed."
  1099. :group 'org-sparse-trees
  1100. :group 'org-time
  1101. :type 'boolean)
  1102. (defcustom org-occur-hook '(org-first-headline-recenter)
  1103. "Hook that is run after `org-occur' has constructed a sparse tree.
  1104. This can be used to recenter the window to show as much of the structure
  1105. as possible."
  1106. :group 'org-sparse-trees
  1107. :type 'hook)
  1108. (defgroup org-imenu-and-speedbar nil
  1109. "Options concerning imenu and speedbar in Org-mode."
  1110. :tag "Org Imenu and Speedbar"
  1111. :group 'org-structure)
  1112. (defcustom org-imenu-depth 2
  1113. "The maximum level for Imenu access to Org-mode headlines.
  1114. This also applied for speedbar access."
  1115. :group 'org-imenu-and-speedbar
  1116. :type 'integer)
  1117. (defgroup org-table nil
  1118. "Options concerning tables in Org-mode."
  1119. :tag "Org Table"
  1120. :group 'org)
  1121. (defcustom org-enable-table-editor 'optimized
  1122. "Non-nil means lines starting with \"|\" are handled by the table editor.
  1123. When nil, such lines will be treated like ordinary lines.
  1124. When equal to the symbol `optimized', the table editor will be optimized to
  1125. do the following:
  1126. - Automatic overwrite mode in front of whitespace in table fields.
  1127. This makes the structure of the table stay in tact as long as the edited
  1128. field does not exceed the column width.
  1129. - Minimize the number of realigns. Normally, the table is aligned each time
  1130. TAB or RET are pressed to move to another field. With optimization this
  1131. happens only if changes to a field might have changed the column width.
  1132. Optimization requires replacing the functions `self-insert-command',
  1133. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  1134. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  1135. very good at guessing when a re-align will be necessary, but you can always
  1136. force one with \\[org-ctrl-c-ctrl-c].
  1137. If you would like to use the optimized version in Org-mode, but the
  1138. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  1139. This variable can be used to turn on and off the table editor during a session,
  1140. but in order to toggle optimization, a restart is required.
  1141. See also the variable `org-table-auto-blank-field'."
  1142. :group 'org-table
  1143. :type '(choice
  1144. (const :tag "off" nil)
  1145. (const :tag "on" t)
  1146. (const :tag "on, optimized" optimized)))
  1147. (defcustom org-self-insert-cluster-for-undo (or (featurep 'xemacs)
  1148. (version<= emacs-version "24.1"))
  1149. "Non-nil means cluster self-insert commands for undo when possible.
  1150. If this is set, then, like in the Emacs command loop, 20 consecutive
  1151. characters will be undone together.
  1152. This is configurable, because there is some impact on typing performance."
  1153. :group 'org-table
  1154. :type 'boolean)
  1155. (defcustom org-table-tab-recognizes-table.el t
  1156. "Non-nil means TAB will automatically notice a table.el table.
  1157. When it sees such a table, it moves point into it and - if necessary -
  1158. calls `table-recognize-table'."
  1159. :group 'org-table-editing
  1160. :type 'boolean)
  1161. (defgroup org-link nil
  1162. "Options concerning links in Org-mode."
  1163. :tag "Org Link"
  1164. :group 'org)
  1165. (defvar org-link-abbrev-alist-local nil
  1166. "Buffer-local version of `org-link-abbrev-alist', which see.
  1167. The value of this is taken from the #+LINK lines.")
  1168. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  1169. (defcustom org-link-abbrev-alist nil
  1170. "Alist of link abbreviations.
  1171. The car of each element is a string, to be replaced at the start of a link.
  1172. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  1173. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  1174. [[linkkey:tag][description]]
  1175. The 'linkkey' must be a word word, starting with a letter, followed
  1176. by letters, numbers, '-' or '_'.
  1177. If REPLACE is a string, the tag will simply be appended to create the link.
  1178. If the string contains \"%s\", the tag will be inserted there. If the string
  1179. contains \"%h\", it will cause a url-encoded version of the tag to be inserted
  1180. at that point (see the function `url-hexify-string'). If the string contains
  1181. the specifier \"%(my-function)\", then the custom function `my-function' will
  1182. be invoked: this function takes the tag as its only argument and must return
  1183. a string.
  1184. REPLACE may also be a function that will be called with the tag as the
  1185. only argument to create the link, which should be returned as a string.
  1186. See the manual for examples."
  1187. :group 'org-link
  1188. :type '(repeat
  1189. (cons
  1190. (string :tag "Protocol")
  1191. (choice
  1192. (string :tag "Format")
  1193. (function)))))
  1194. (defcustom org-descriptive-links t
  1195. "Non-nil means Org will display descriptive links.
  1196. E.g. [[http://orgmode.org][Org website]] will be displayed as
  1197. \"Org Website\", hiding the link itself and just displaying its
  1198. description. When set to `nil', Org will display the full links
  1199. literally.
  1200. You can interactively set the value of this variable by calling
  1201. `org-toggle-link-display' or from the menu Org>Hyperlinks menu."
  1202. :group 'org-link
  1203. :type 'boolean)
  1204. (defcustom org-link-file-path-type 'adaptive
  1205. "How the path name in file links should be stored.
  1206. Valid values are:
  1207. relative Relative to the current directory, i.e. the directory of the file
  1208. into which the link is being inserted.
  1209. absolute Absolute path, if possible with ~ for home directory.
  1210. noabbrev Absolute path, no abbreviation of home directory.
  1211. adaptive Use relative path for files in the current directory and sub-
  1212. directories of it. For other files, use an absolute path."
  1213. :group 'org-link
  1214. :type '(choice
  1215. (const relative)
  1216. (const absolute)
  1217. (const noabbrev)
  1218. (const adaptive)))
  1219. (defcustom org-activate-links '(bracket angle plain radio tag date footnote)
  1220. "Types of links that should be activated in Org-mode files.
  1221. This is a list of symbols, each leading to the activation of a certain link
  1222. type. In principle, it does not hurt to turn on most link types - there may
  1223. be a small gain when turning off unused link types. The types are:
  1224. bracket The recommended [[link][description]] or [[link]] links with hiding.
  1225. angle Links in angular brackets that may contain whitespace like
  1226. <bbdb:Carsten Dominik>.
  1227. plain Plain links in normal text, no whitespace, like http://google.com.
  1228. radio Text that is matched by a radio target, see manual for details.
  1229. tag Tag settings in a headline (link to tag search).
  1230. date Time stamps (link to calendar).
  1231. footnote Footnote labels.
  1232. Changing this variable requires a restart of Emacs to become effective."
  1233. :group 'org-link
  1234. :type '(set :greedy t
  1235. (const :tag "Double bracket links" bracket)
  1236. (const :tag "Angular bracket links" angle)
  1237. (const :tag "Plain text links" plain)
  1238. (const :tag "Radio target matches" radio)
  1239. (const :tag "Tags" tag)
  1240. (const :tag "Timestamps" date)
  1241. (const :tag "Footnotes" footnote)))
  1242. (defcustom org-make-link-description-function nil
  1243. "Function to use for generating link descriptions from links.
  1244. When nil, the link location will be used. This function must take
  1245. two parameters: the first one is the link, the second one is the
  1246. description generated by `org-insert-link'. The function should
  1247. return the description to use."
  1248. :group 'org-link
  1249. :type 'function)
  1250. (defgroup org-link-store nil
  1251. "Options concerning storing links in Org-mode."
  1252. :tag "Org Store Link"
  1253. :group 'org-link)
  1254. (defcustom org-url-hexify-p t
  1255. "When non-nil, hexify URL when creating a link."
  1256. :type 'boolean
  1257. :version "24.3"
  1258. :group 'org-link-store)
  1259. (defcustom org-email-link-description-format "Email %c: %.30s"
  1260. "Format of the description part of a link to an email or usenet message.
  1261. The following %-escapes will be replaced by corresponding information:
  1262. %F full \"From\" field
  1263. %f name, taken from \"From\" field, address if no name
  1264. %T full \"To\" field
  1265. %t first name in \"To\" field, address if no name
  1266. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  1267. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  1268. %s subject
  1269. %d date
  1270. %m message-id.
  1271. You may use normal field width specification between the % and the letter.
  1272. This is for example useful to limit the length of the subject.
  1273. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  1274. :group 'org-link-store
  1275. :type 'string)
  1276. (defcustom org-from-is-user-regexp
  1277. (let (r1 r2)
  1278. (when (and user-mail-address (not (string= user-mail-address "")))
  1279. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  1280. (when (and user-full-name (not (string= user-full-name "")))
  1281. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  1282. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  1283. "Regexp matched against the \"From:\" header of an email or usenet message.
  1284. It should match if the message is from the user him/herself."
  1285. :group 'org-link-store
  1286. :type 'regexp)
  1287. (defcustom org-context-in-file-links t
  1288. "Non-nil means file links from `org-store-link' contain context.
  1289. A search string will be added to the file name with :: as separator and
  1290. used to find the context when the link is activated by the command
  1291. `org-open-at-point'. When this option is t, the entire active region
  1292. will be placed in the search string of the file link. If set to a
  1293. positive integer, only the first n lines of context will be stored.
  1294. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1295. negates this setting for the duration of the command."
  1296. :group 'org-link-store
  1297. :type '(choice boolean integer))
  1298. (defcustom org-keep-stored-link-after-insertion nil
  1299. "Non-nil means keep link in list for entire session.
  1300. The command `org-store-link' adds a link pointing to the current
  1301. location to an internal list. These links accumulate during a session.
  1302. The command `org-insert-link' can be used to insert links into any
  1303. Org-mode file (offering completion for all stored links). When this
  1304. option is nil, every link which has been inserted once using \\[org-insert-link]
  1305. will be removed from the list, to make completing the unused links
  1306. more efficient."
  1307. :group 'org-link-store
  1308. :type 'boolean)
  1309. (defgroup org-link-follow nil
  1310. "Options concerning following links in Org-mode."
  1311. :tag "Org Follow Link"
  1312. :group 'org-link)
  1313. (defcustom org-link-translation-function nil
  1314. "Function to translate links with different syntax to Org syntax.
  1315. This can be used to translate links created for example by the Planner
  1316. or emacs-wiki packages to Org syntax.
  1317. The function must accept two parameters, a TYPE containing the link
  1318. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  1319. which is everything after the link protocol. It should return a cons
  1320. with possibly modified values of type and path.
  1321. Org contains a function for this, so if you set this variable to
  1322. `org-translate-link-from-planner', you should be able follow many
  1323. links created by planner."
  1324. :group 'org-link-follow
  1325. :type 'function)
  1326. (defcustom org-follow-link-hook nil
  1327. "Hook that is run after a link has been followed."
  1328. :group 'org-link-follow
  1329. :type 'hook)
  1330. (defcustom org-tab-follows-link nil
  1331. "Non-nil means on links TAB will follow the link.
  1332. Needs to be set before org.el is loaded.
  1333. This really should not be used, it does not make sense, and the
  1334. implementation is bad."
  1335. :group 'org-link-follow
  1336. :type 'boolean)
  1337. (defcustom org-return-follows-link nil
  1338. "Non-nil means on links RET will follow the link."
  1339. :group 'org-link-follow
  1340. :type 'boolean)
  1341. (defcustom org-mouse-1-follows-link
  1342. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1343. "Non-nil means mouse-1 on a link will follow the link.
  1344. A longer mouse click will still set point. Does not work on XEmacs.
  1345. Needs to be set before org.el is loaded."
  1346. :group 'org-link-follow
  1347. :type 'boolean)
  1348. (defcustom org-mark-ring-length 4
  1349. "Number of different positions to be recorded in the ring.
  1350. Changing this requires a restart of Emacs to work correctly."
  1351. :group 'org-link-follow
  1352. :type 'integer)
  1353. (defcustom org-link-search-must-match-exact-headline 'query-to-create
  1354. "Non-nil means internal links in Org files must exactly match a headline.
  1355. When nil, the link search tries to match a phrase with all words
  1356. in the search text."
  1357. :group 'org-link-follow
  1358. :version "24.1"
  1359. :type '(choice
  1360. (const :tag "Use fuzzy text search" nil)
  1361. (const :tag "Match only exact headline" t)
  1362. (const :tag "Match exact headline or query to create it"
  1363. query-to-create)))
  1364. (defcustom org-link-frame-setup
  1365. '((vm . vm-visit-folder-other-frame)
  1366. (vm-imap . vm-visit-imap-folder-other-frame)
  1367. (gnus . org-gnus-no-new-news)
  1368. (file . find-file-other-window)
  1369. (wl . wl-other-frame))
  1370. "Setup the frame configuration for following links.
  1371. When following a link with Emacs, it may often be useful to display
  1372. this link in another window or frame. This variable can be used to
  1373. set this up for the different types of links.
  1374. For VM, use any of
  1375. `vm-visit-folder'
  1376. `vm-visit-folder-other-window'
  1377. `vm-visit-folder-other-frame'
  1378. For Gnus, use any of
  1379. `gnus'
  1380. `gnus-other-frame'
  1381. `org-gnus-no-new-news'
  1382. For FILE, use any of
  1383. `find-file'
  1384. `find-file-other-window'
  1385. `find-file-other-frame'
  1386. For Wanderlust use any of
  1387. `wl'
  1388. `wl-other-frame'
  1389. For the calendar, use the variable `calendar-setup'.
  1390. For BBDB, it is currently only possible to display the matches in
  1391. another window."
  1392. :group 'org-link-follow
  1393. :type '(list
  1394. (cons (const vm)
  1395. (choice
  1396. (const vm-visit-folder)
  1397. (const vm-visit-folder-other-window)
  1398. (const vm-visit-folder-other-frame)))
  1399. (cons (const gnus)
  1400. (choice
  1401. (const gnus)
  1402. (const gnus-other-frame)
  1403. (const org-gnus-no-new-news)))
  1404. (cons (const file)
  1405. (choice
  1406. (const find-file)
  1407. (const find-file-other-window)
  1408. (const find-file-other-frame)))
  1409. (cons (const wl)
  1410. (choice
  1411. (const wl)
  1412. (const wl-other-frame)))))
  1413. (defcustom org-display-internal-link-with-indirect-buffer nil
  1414. "Non-nil means use indirect buffer to display infile links.
  1415. Activating internal links (from one location in a file to another location
  1416. in the same file) normally just jumps to the location. When the link is
  1417. activated with a \\[universal-argument] prefix (or with mouse-3), the link \
  1418. is displayed in
  1419. another window. When this option is set, the other window actually displays
  1420. an indirect buffer clone of the current buffer, to avoid any visibility
  1421. changes to the current buffer."
  1422. :group 'org-link-follow
  1423. :type 'boolean)
  1424. (defcustom org-open-non-existing-files nil
  1425. "Non-nil means `org-open-file' will open non-existing files.
  1426. When nil, an error will be generated.
  1427. This variable applies only to external applications because they
  1428. might choke on non-existing files. If the link is to a file that
  1429. will be opened in Emacs, the variable is ignored."
  1430. :group 'org-link-follow
  1431. :type 'boolean)
  1432. (defcustom org-open-directory-means-index-dot-org nil
  1433. "Non-nil means a link to a directory really means to index.org.
  1434. When nil, following a directory link will run dired or open a finder/explorer
  1435. window on that directory."
  1436. :group 'org-link-follow
  1437. :type 'boolean)
  1438. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1439. "Function and arguments to call for following mailto links.
  1440. This is a list with the first element being a Lisp function, and the
  1441. remaining elements being arguments to the function. In string arguments,
  1442. %a will be replaced by the address, and %s will be replaced by the subject
  1443. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1444. :group 'org-link-follow
  1445. :type '(choice
  1446. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1447. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1448. (const :tag "message-mail" (message-mail "%a" "%s"))
  1449. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1450. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1451. "Non-nil means ask for confirmation before executing shell links.
  1452. Shell links can be dangerous: just think about a link
  1453. [[shell:rm -rf ~/*][Google Search]]
  1454. This link would show up in your Org-mode document as \"Google Search\",
  1455. but really it would remove your entire home directory.
  1456. Therefore we advise against setting this variable to nil.
  1457. Just change it to `y-or-n-p' if you want to confirm with a
  1458. single keystroke rather than having to type \"yes\"."
  1459. :group 'org-link-follow
  1460. :type '(choice
  1461. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1462. (const :tag "with y-or-n (faster)" y-or-n-p)
  1463. (const :tag "no confirmation (dangerous)" nil)))
  1464. (put 'org-confirm-shell-link-function
  1465. 'safe-local-variable
  1466. #'(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1467. (defcustom org-confirm-shell-link-not-regexp ""
  1468. "A regexp to skip confirmation for shell links."
  1469. :group 'org-link-follow
  1470. :version "24.1"
  1471. :type 'regexp)
  1472. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1473. "Non-nil means ask for confirmation before executing Emacs Lisp links.
  1474. Elisp links can be dangerous: just think about a link
  1475. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1476. This link would show up in your Org-mode document as \"Google Search\",
  1477. but really it would remove your entire home directory.
  1478. Therefore we advise against setting this variable to nil.
  1479. Just change it to `y-or-n-p' if you want to confirm with a
  1480. single keystroke rather than having to type \"yes\"."
  1481. :group 'org-link-follow
  1482. :type '(choice
  1483. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1484. (const :tag "with y-or-n (faster)" y-or-n-p)
  1485. (const :tag "no confirmation (dangerous)" nil)))
  1486. (put 'org-confirm-shell-link-function
  1487. 'safe-local-variable
  1488. #'(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1489. (defcustom org-confirm-elisp-link-not-regexp ""
  1490. "A regexp to skip confirmation for Elisp links."
  1491. :group 'org-link-follow
  1492. :version "24.1"
  1493. :type 'regexp)
  1494. (defconst org-file-apps-defaults-gnu
  1495. '((remote . emacs)
  1496. (system . mailcap)
  1497. (t . mailcap))
  1498. "Default file applications on a UNIX or GNU/Linux system.
  1499. See `org-file-apps'.")
  1500. (defconst org-file-apps-defaults-macosx
  1501. '((remote . emacs)
  1502. (t . "open %s")
  1503. (system . "open %s")
  1504. ("ps.gz" . "gv %s")
  1505. ("eps.gz" . "gv %s")
  1506. ("dvi" . "xdvi %s")
  1507. ("fig" . "xfig %s"))
  1508. "Default file applications on a MacOS X system.
  1509. The system \"open\" is known as a default, but we use X11 applications
  1510. for some files for which the OS does not have a good default.
  1511. See `org-file-apps'.")
  1512. (defconst org-file-apps-defaults-windowsnt
  1513. (list
  1514. '(remote . emacs)
  1515. (cons t
  1516. (list (if (featurep 'xemacs)
  1517. 'mswindows-shell-execute
  1518. 'w32-shell-execute)
  1519. "open" 'file))
  1520. (cons 'system
  1521. (list (if (featurep 'xemacs)
  1522. 'mswindows-shell-execute
  1523. 'w32-shell-execute)
  1524. "open" 'file)))
  1525. "Default file applications on a Windows NT system.
  1526. The system \"open\" is used for most files.
  1527. See `org-file-apps'.")
  1528. (defcustom org-file-apps
  1529. '(
  1530. (auto-mode . emacs)
  1531. ("\\.mm\\'" . default)
  1532. ("\\.x?html?\\'" . default)
  1533. ("\\.pdf\\'" . default)
  1534. )
  1535. "External applications for opening `file:path' items in a document.
  1536. Org-mode uses system defaults for different file types, but
  1537. you can use this variable to set the application for a given file
  1538. extension. The entries in this list are cons cells where the car identifies
  1539. files and the cdr the corresponding command. Possible values for the
  1540. file identifier are
  1541. \"string\" A string as a file identifier can be interpreted in different
  1542. ways, depending on its contents:
  1543. - Alphanumeric characters only:
  1544. Match links with this file extension.
  1545. Example: (\"pdf\" . \"evince %s\")
  1546. to open PDFs with evince.
  1547. - Regular expression: Match links where the
  1548. filename matches the regexp. If you want to
  1549. use groups here, use shy groups.
  1550. Example: (\"\\.x?html\\'\" . \"firefox %s\")
  1551. (\"\\(?:xhtml\\|html\\)\" . \"firefox %s\")
  1552. to open *.html and *.xhtml with firefox.
  1553. - Regular expression which contains (non-shy) groups:
  1554. Match links where the whole link, including \"::\", and
  1555. anything after that, matches the regexp.
  1556. In a custom command string, %1, %2, etc. are replaced with
  1557. the parts of the link that were matched by the groups.
  1558. For backwards compatibility, if a command string is given
  1559. that does not use any of the group matches, this case is
  1560. handled identically to the second one (i.e. match against
  1561. file name only).
  1562. In a custom lisp form, you can access the group matches with
  1563. (match-string n link).
  1564. Example: (\"\\.pdf::\\(\\d+\\)\\'\" . \"evince -p %1 %s\")
  1565. to open [[file:document.pdf::5]] with evince at page 5.
  1566. `directory' Matches a directory
  1567. `remote' Matches a remote file, accessible through tramp or efs.
  1568. Remote files most likely should be visited through Emacs
  1569. because external applications cannot handle such paths.
  1570. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1571. so all files Emacs knows how to handle. Using this with
  1572. command `emacs' will open most files in Emacs. Beware that this
  1573. will also open html files inside Emacs, unless you add
  1574. (\"html\" . default) to the list as well.
  1575. t Default for files not matched by any of the other options.
  1576. `system' The system command to open files, like `open' on Windows
  1577. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1578. that will be selected if you call `C-c C-o' with a double
  1579. \\[universal-argument] \\[universal-argument] prefix.
  1580. Possible values for the command are:
  1581. `emacs' The file will be visited by the current Emacs process.
  1582. `default' Use the default application for this file type, which is the
  1583. association for t in the list, most likely in the system-specific
  1584. part.
  1585. This can be used to overrule an unwanted setting in the
  1586. system-specific variable.
  1587. `system' Use the system command for opening files, like \"open\".
  1588. This command is specified by the entry whose car is `system'.
  1589. Most likely, the system-specific version of this variable
  1590. does define this command, but you can overrule/replace it
  1591. here.
  1592. string A command to be executed by a shell; %s will be replaced
  1593. by the path to the file.
  1594. sexp A Lisp form which will be evaluated. The file path will
  1595. be available in the Lisp variable `file'.
  1596. For more examples, see the system specific constants
  1597. `org-file-apps-defaults-macosx'
  1598. `org-file-apps-defaults-windowsnt'
  1599. `org-file-apps-defaults-gnu'."
  1600. :group 'org-link-follow
  1601. :type '(repeat
  1602. (cons (choice :value ""
  1603. (string :tag "Extension")
  1604. (const :tag "System command to open files" system)
  1605. (const :tag "Default for unrecognized files" t)
  1606. (const :tag "Remote file" remote)
  1607. (const :tag "Links to a directory" directory)
  1608. (const :tag "Any files that have Emacs modes"
  1609. auto-mode))
  1610. (choice :value ""
  1611. (const :tag "Visit with Emacs" emacs)
  1612. (const :tag "Use default" default)
  1613. (const :tag "Use the system command" system)
  1614. (string :tag "Command")
  1615. (sexp :tag "Lisp form")))))
  1616. (defcustom org-doi-server-url "http://dx.doi.org/"
  1617. "The URL of the DOI server."
  1618. :type 'string
  1619. :version "24.3"
  1620. :group 'org-link-follow)
  1621. (defgroup org-refile nil
  1622. "Options concerning refiling entries in Org-mode."
  1623. :tag "Org Refile"
  1624. :group 'org)
  1625. (defcustom org-directory "~/org"
  1626. "Directory with org files.
  1627. This is just a default location to look for Org files. There is no need
  1628. at all to put your files into this directory. It is only used in the
  1629. following situations:
  1630. 1. When a capture template specifies a target file that is not an
  1631. absolute path. The path will then be interpreted relative to
  1632. `org-directory'
  1633. 2. When a capture note is filed away in an interactive way (when exiting the
  1634. note buffer with `C-1 C-c C-c'. The user is prompted for an org file,
  1635. with `org-directory' as the default path."
  1636. :group 'org-refile
  1637. :group 'org-remember
  1638. :group 'org-capture
  1639. :type 'directory)
  1640. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1641. "Default target for storing notes.
  1642. Used as a fall back file for org-remember.el and org-capture.el, for
  1643. templates that do not specify a target file."
  1644. :group 'org-refile
  1645. :group 'org-remember
  1646. :group 'org-capture
  1647. :type '(choice
  1648. (const :tag "Default from remember-data-file" nil)
  1649. file))
  1650. (defcustom org-goto-interface 'outline
  1651. "The default interface to be used for `org-goto'.
  1652. Allowed values are:
  1653. outline The interface shows an outline of the relevant file
  1654. and the correct heading is found by moving through
  1655. the outline or by searching with incremental search.
  1656. outline-path-completion Headlines in the current buffer are offered via
  1657. completion. This is the interface also used by
  1658. the refile command."
  1659. :group 'org-refile
  1660. :type '(choice
  1661. (const :tag "Outline" outline)
  1662. (const :tag "Outline-path-completion" outline-path-completion)))
  1663. (defcustom org-goto-max-level 5
  1664. "Maximum target level when running `org-goto' with refile interface."
  1665. :group 'org-refile
  1666. :type 'integer)
  1667. (defcustom org-reverse-note-order nil
  1668. "Non-nil means store new notes at the beginning of a file or entry.
  1669. When nil, new notes will be filed to the end of a file or entry.
  1670. This can also be a list with cons cells of regular expressions that
  1671. are matched against file names, and values."
  1672. :group 'org-remember
  1673. :group 'org-capture
  1674. :group 'org-refile
  1675. :type '(choice
  1676. (const :tag "Reverse always" t)
  1677. (const :tag "Reverse never" nil)
  1678. (repeat :tag "By file name regexp"
  1679. (cons regexp boolean))))
  1680. (defcustom org-log-refile nil
  1681. "Information to record when a task is refiled.
  1682. Possible values are:
  1683. nil Don't add anything
  1684. time Add a time stamp to the task
  1685. note Prompt for a note and add it with template `org-log-note-headings'
  1686. This option can also be set with on a per-file-basis with
  1687. #+STARTUP: nologrefile
  1688. #+STARTUP: logrefile
  1689. #+STARTUP: lognoterefile
  1690. You can have local logging settings for a subtree by setting the LOGGING
  1691. property to one or more of these keywords.
  1692. When bulk-refiling from the agenda, the value `note' is forbidden and
  1693. will temporarily be changed to `time'."
  1694. :group 'org-refile
  1695. :group 'org-progress
  1696. :version "24.1"
  1697. :type '(choice
  1698. (const :tag "No logging" nil)
  1699. (const :tag "Record timestamp" time)
  1700. (const :tag "Record timestamp with note." note)))
  1701. (defcustom org-refile-targets nil
  1702. "Targets for refiling entries with \\[org-refile].
  1703. This is a list of cons cells. Each cell contains:
  1704. - a specification of the files to be considered, either a list of files,
  1705. or a symbol whose function or variable value will be used to retrieve
  1706. a file name or a list of file names. If you use `org-agenda-files' for
  1707. that, all agenda files will be scanned for targets. Nil means consider
  1708. headings in the current buffer.
  1709. - A specification of how to find candidate refile targets. This may be
  1710. any of:
  1711. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1712. This tag has to be present in all target headlines, inheritance will
  1713. not be considered.
  1714. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1715. todo keyword.
  1716. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1717. headlines that are refiling targets.
  1718. - a cons cell (:level . N). Any headline of level N is considered a target.
  1719. Note that, when `org-odd-levels-only' is set, level corresponds to
  1720. order in hierarchy, not to the number of stars.
  1721. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1722. Note that, when `org-odd-levels-only' is set, level corresponds to
  1723. order in hierarchy, not to the number of stars.
  1724. Each element of this list generates a set of possible targets.
  1725. The union of these sets is presented (with completion) to
  1726. the user by `org-refile'.
  1727. You can set the variable `org-refile-target-verify-function' to a function
  1728. to verify each headline found by the simple criteria above.
  1729. When this variable is nil, all top-level headlines in the current buffer
  1730. are used, equivalent to the value `((nil . (:level . 1))'."
  1731. :group 'org-refile
  1732. :type '(repeat
  1733. (cons
  1734. (choice :value org-agenda-files
  1735. (const :tag "All agenda files" org-agenda-files)
  1736. (const :tag "Current buffer" nil)
  1737. (function) (variable) (file))
  1738. (choice :tag "Identify target headline by"
  1739. (cons :tag "Specific tag" (const :value :tag) (string))
  1740. (cons :tag "TODO keyword" (const :value :todo) (string))
  1741. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  1742. (cons :tag "Level number" (const :value :level) (integer))
  1743. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  1744. (defcustom org-refile-target-verify-function nil
  1745. "Function to verify if the headline at point should be a refile target.
  1746. The function will be called without arguments, with point at the
  1747. beginning of the headline. It should return t and leave point
  1748. where it is if the headline is a valid target for refiling.
  1749. If the target should not be selected, the function must return nil.
  1750. In addition to this, it may move point to a place from where the search
  1751. should be continued. For example, the function may decide that the entire
  1752. subtree of the current entry should be excluded and move point to the end
  1753. of the subtree."
  1754. :group 'org-refile
  1755. :type 'function)
  1756. (defcustom org-refile-use-cache nil
  1757. "Non-nil means cache refile targets to speed up the process.
  1758. The cache for a particular file will be updated automatically when
  1759. the buffer has been killed, or when any of the marker used for flagging
  1760. refile targets no longer points at a live buffer.
  1761. If you have added new entries to a buffer that might themselves be targets,
  1762. you need to clear the cache manually by pressing `C-0 C-c C-w' or, if you
  1763. find that easier, `C-u C-u C-u C-c C-w'."
  1764. :group 'org-refile
  1765. :version "24.1"
  1766. :type 'boolean)
  1767. (defcustom org-refile-use-outline-path nil
  1768. "Non-nil means provide refile targets as paths.
  1769. So a level 3 headline will be available as level1/level2/level3.
  1770. When the value is `file', also include the file name (without directory)
  1771. into the path. In this case, you can also stop the completion after
  1772. the file name, to get entries inserted as top level in the file.
  1773. When `full-file-path', include the full file path."
  1774. :group 'org-refile
  1775. :type '(choice
  1776. (const :tag "Not" nil)
  1777. (const :tag "Yes" t)
  1778. (const :tag "Start with file name" file)
  1779. (const :tag "Start with full file path" full-file-path)))
  1780. (defcustom org-outline-path-complete-in-steps t
  1781. "Non-nil means complete the outline path in hierarchical steps.
  1782. When Org-mode uses the refile interface to select an outline path
  1783. \(see variable `org-refile-use-outline-path'), the completion of
  1784. the path can be done is a single go, or if can be done in steps down
  1785. the headline hierarchy. Going in steps is probably the best if you
  1786. do not use a special completion package like `ido' or `icicles'.
  1787. However, when using these packages, going in one step can be very
  1788. fast, while still showing the whole path to the entry."
  1789. :group 'org-refile
  1790. :type 'boolean)
  1791. (defcustom org-refile-allow-creating-parent-nodes nil
  1792. "Non-nil means allow to create new nodes as refile targets.
  1793. New nodes are then created by adding \"/new node name\" to the completion
  1794. of an existing node. When the value of this variable is `confirm',
  1795. new node creation must be confirmed by the user (recommended)
  1796. When nil, the completion must match an existing entry.
  1797. Note that, if the new heading is not seen by the criteria
  1798. listed in `org-refile-targets', multiple instances of the same
  1799. heading would be created by trying again to file under the new
  1800. heading."
  1801. :group 'org-refile
  1802. :type '(choice
  1803. (const :tag "Never" nil)
  1804. (const :tag "Always" t)
  1805. (const :tag "Prompt for confirmation" confirm)))
  1806. (defcustom org-refile-active-region-within-subtree nil
  1807. "Non-nil means also refile active region within a subtree.
  1808. By default `org-refile' doesn't allow refiling regions if they
  1809. don't contain a set of subtrees, but it might be convenient to
  1810. do so sometimes: in that case, the first line of the region is
  1811. converted to a headline before refiling."
  1812. :group 'org-refile
  1813. :version "24.1"
  1814. :type 'boolean)
  1815. (defgroup org-todo nil
  1816. "Options concerning TODO items in Org-mode."
  1817. :tag "Org TODO"
  1818. :group 'org)
  1819. (defgroup org-progress nil
  1820. "Options concerning Progress logging in Org-mode."
  1821. :tag "Org Progress"
  1822. :group 'org-time)
  1823. (defvar org-todo-interpretation-widgets
  1824. '((:tag "Sequence (cycling hits every state)" sequence)
  1825. (:tag "Type (cycling directly to DONE)" type))
  1826. "The available interpretation symbols for customizing `org-todo-keywords'.
  1827. Interested libraries should add to this list.")
  1828. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1829. "List of TODO entry keyword sequences and their interpretation.
  1830. \\<org-mode-map>This is a list of sequences.
  1831. Each sequence starts with a symbol, either `sequence' or `type',
  1832. indicating if the keywords should be interpreted as a sequence of
  1833. action steps, or as different types of TODO items. The first
  1834. keywords are states requiring action - these states will select a headline
  1835. for inclusion into the global TODO list Org-mode produces. If one of
  1836. the \"keywords\" is the vertical bar, \"|\", the remaining keywords
  1837. signify that no further action is necessary. If \"|\" is not found,
  1838. the last keyword is treated as the only DONE state of the sequence.
  1839. The command \\[org-todo] cycles an entry through these states, and one
  1840. additional state where no keyword is present. For details about this
  1841. cycling, see the manual.
  1842. TODO keywords and interpretation can also be set on a per-file basis with
  1843. the special #+SEQ_TODO and #+TYP_TODO lines.
  1844. Each keyword can optionally specify a character for fast state selection
  1845. \(in combination with the variable `org-use-fast-todo-selection')
  1846. and specifiers for state change logging, using the same syntax that
  1847. is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says that
  1848. the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1849. indicates to record a time stamp each time this state is selected.
  1850. Each keyword may also specify if a timestamp or a note should be
  1851. recorded when entering or leaving the state, by adding additional
  1852. characters in the parenthesis after the keyword. This looks like this:
  1853. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1854. record only the time of the state change. With X and Y being either
  1855. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1856. Y when leaving the state if and only if the *target* state does not
  1857. define X. You may omit any of the fast-selection key or X or /Y,
  1858. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1859. For backward compatibility, this variable may also be just a list
  1860. of keywords. In this case the interpretation (sequence or type) will be
  1861. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1862. :group 'org-todo
  1863. :group 'org-keywords
  1864. :type '(choice
  1865. (repeat :tag "Old syntax, just keywords"
  1866. (string :tag "Keyword"))
  1867. (repeat :tag "New syntax"
  1868. (cons
  1869. (choice
  1870. :tag "Interpretation"
  1871. ;;Quick and dirty way to see
  1872. ;;`org-todo-interpretations'. This takes the
  1873. ;;place of item arguments
  1874. :convert-widget
  1875. (lambda (widget)
  1876. (widget-put widget
  1877. :args (mapcar
  1878. #'(lambda (x)
  1879. (widget-convert
  1880. (cons 'const x)))
  1881. org-todo-interpretation-widgets))
  1882. widget))
  1883. (repeat
  1884. (string :tag "Keyword"))))))
  1885. (defvar org-todo-keywords-1 nil
  1886. "All TODO and DONE keywords active in a buffer.")
  1887. (make-variable-buffer-local 'org-todo-keywords-1)
  1888. (defvar org-todo-keywords-for-agenda nil)
  1889. (defvar org-done-keywords-for-agenda nil)
  1890. (defvar org-drawers-for-agenda nil)
  1891. (defvar org-todo-keyword-alist-for-agenda nil)
  1892. (defvar org-tag-alist-for-agenda nil)
  1893. (defvar org-agenda-contributing-files nil)
  1894. (defvar org-not-done-keywords nil)
  1895. (make-variable-buffer-local 'org-not-done-keywords)
  1896. (defvar org-done-keywords nil)
  1897. (make-variable-buffer-local 'org-done-keywords)
  1898. (defvar org-todo-heads nil)
  1899. (make-variable-buffer-local 'org-todo-heads)
  1900. (defvar org-todo-sets nil)
  1901. (make-variable-buffer-local 'org-todo-sets)
  1902. (defvar org-todo-log-states nil)
  1903. (make-variable-buffer-local 'org-todo-log-states)
  1904. (defvar org-todo-kwd-alist nil)
  1905. (make-variable-buffer-local 'org-todo-kwd-alist)
  1906. (defvar org-todo-key-alist nil)
  1907. (make-variable-buffer-local 'org-todo-key-alist)
  1908. (defvar org-todo-key-trigger nil)
  1909. (make-variable-buffer-local 'org-todo-key-trigger)
  1910. (defcustom org-todo-interpretation 'sequence
  1911. "Controls how TODO keywords are interpreted.
  1912. This variable is in principle obsolete and is only used for
  1913. backward compatibility, if the interpretation of todo keywords is
  1914. not given already in `org-todo-keywords'. See that variable for
  1915. more information."
  1916. :group 'org-todo
  1917. :group 'org-keywords
  1918. :type '(choice (const sequence)
  1919. (const type)))
  1920. (defcustom org-use-fast-todo-selection t
  1921. "Non-nil means use the fast todo selection scheme with C-c C-t.
  1922. This variable describes if and under what circumstances the cycling
  1923. mechanism for TODO keywords will be replaced by a single-key, direct
  1924. selection scheme.
  1925. When nil, fast selection is never used.
  1926. When the symbol `prefix', it will be used when `org-todo' is called
  1927. with a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and
  1928. `C-u t' in an agenda buffer.
  1929. When t, fast selection is used by default. In this case, the prefix
  1930. argument forces cycling instead.
  1931. In all cases, the special interface is only used if access keys have
  1932. actually been assigned by the user, i.e. if keywords in the configuration
  1933. are followed by a letter in parenthesis, like TODO(t)."
  1934. :group 'org-todo
  1935. :type '(choice
  1936. (const :tag "Never" nil)
  1937. (const :tag "By default" t)
  1938. (const :tag "Only with C-u C-c C-t" prefix)))
  1939. (defcustom org-provide-todo-statistics t
  1940. "Non-nil means update todo statistics after insert and toggle.
  1941. ALL-HEADLINES means update todo statistics by including headlines
  1942. with no TODO keyword as well, counting them as not done.
  1943. A list of TODO keywords means the same, but skip keywords that are
  1944. not in this list.
  1945. When this is set, todo statistics is updated in the parent of the
  1946. current entry each time a todo state is changed."
  1947. :group 'org-todo
  1948. :type '(choice
  1949. (const :tag "Yes, only for TODO entries" t)
  1950. (const :tag "Yes, including all entries" 'all-headlines)
  1951. (repeat :tag "Yes, for TODOs in this list"
  1952. (string :tag "TODO keyword"))
  1953. (other :tag "No TODO statistics" nil)))
  1954. (defcustom org-hierarchical-todo-statistics t
  1955. "Non-nil means TODO statistics covers just direct children.
  1956. When nil, all entries in the subtree are considered.
  1957. This has only an effect if `org-provide-todo-statistics' is set.
  1958. To set this to nil for only a single subtree, use a COOKIE_DATA
  1959. property and include the word \"recursive\" into the value."
  1960. :group 'org-todo
  1961. :type 'boolean)
  1962. (defcustom org-after-todo-state-change-hook nil
  1963. "Hook which is run after the state of a TODO item was changed.
  1964. The new state (a string with a TODO keyword, or nil) is available in the
  1965. Lisp variable `org-state'."
  1966. :group 'org-todo
  1967. :type 'hook)
  1968. (defvar org-blocker-hook nil
  1969. "Hook for functions that are allowed to block a state change.
  1970. Each function gets as its single argument a property list, see
  1971. `org-trigger-hook' for more information about this list.
  1972. If any of the functions in this hook returns nil, the state change
  1973. is blocked.")
  1974. (defvar org-trigger-hook nil
  1975. "Hook for functions that are triggered by a state change.
  1976. Each function gets as its single argument a property list with at least
  1977. the following elements:
  1978. (:type type-of-change :position pos-at-entry-start
  1979. :from old-state :to new-state)
  1980. Depending on the type, more properties may be present.
  1981. This mechanism is currently implemented for:
  1982. TODO state changes
  1983. ------------------
  1984. :type todo-state-change
  1985. :from previous state (keyword as a string), or nil, or a symbol
  1986. 'todo' or 'done', to indicate the general type of state.
  1987. :to new state, like in :from")
  1988. (defcustom org-enforce-todo-dependencies nil
  1989. "Non-nil means undone TODO entries will block switching the parent to DONE.
  1990. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  1991. be blocked if any prior sibling is not yet done.
  1992. Finally, if the parent is blocked because of ordered siblings of its own,
  1993. the child will also be blocked."
  1994. :set (lambda (var val)
  1995. (set var val)
  1996. (if val
  1997. (add-hook 'org-blocker-hook
  1998. 'org-block-todo-from-children-or-siblings-or-parent)
  1999. (remove-hook 'org-blocker-hook
  2000. 'org-block-todo-from-children-or-siblings-or-parent)))
  2001. :group 'org-todo
  2002. :type 'boolean)
  2003. (defcustom org-enforce-todo-checkbox-dependencies nil
  2004. "Non-nil means unchecked boxes will block switching the parent to DONE.
  2005. When this is nil, checkboxes have no influence on switching TODO states.
  2006. When non-nil, you first need to check off all check boxes before the TODO
  2007. entry can be switched to DONE.
  2008. This variable needs to be set before org.el is loaded, and you need to
  2009. restart Emacs after a change to make the change effective. The only way
  2010. to change is while Emacs is running is through the customize interface."
  2011. :set (lambda (var val)
  2012. (set var val)
  2013. (if val
  2014. (add-hook 'org-blocker-hook
  2015. 'org-block-todo-from-checkboxes)
  2016. (remove-hook 'org-blocker-hook
  2017. 'org-block-todo-from-checkboxes)))
  2018. :group 'org-todo
  2019. :type 'boolean)
  2020. (defcustom org-treat-insert-todo-heading-as-state-change nil
  2021. "Non-nil means inserting a TODO heading is treated as state change.
  2022. So when the command \\[org-insert-todo-heading] is used, state change
  2023. logging will apply if appropriate. When nil, the new TODO item will
  2024. be inserted directly, and no logging will take place."
  2025. :group 'org-todo
  2026. :type 'boolean)
  2027. (defcustom org-treat-S-cursor-todo-selection-as-state-change t
  2028. "Non-nil means switching TODO states with S-cursor counts as state change.
  2029. This is the default behavior. However, setting this to nil allows a
  2030. convenient way to select a TODO state and bypass any logging associated
  2031. with that."
  2032. :group 'org-todo
  2033. :type 'boolean)
  2034. (defcustom org-todo-state-tags-triggers nil
  2035. "Tag changes that should be triggered by TODO state changes.
  2036. This is a list. Each entry is
  2037. (state-change (tag . flag) .......)
  2038. State-change can be a string with a state, and empty string to indicate the
  2039. state that has no TODO keyword, or it can be one of the symbols `todo'
  2040. or `done', meaning any not-done or done state, respectively."
  2041. :group 'org-todo
  2042. :group 'org-tags
  2043. :type '(repeat
  2044. (cons (choice :tag "When changing to"
  2045. (const :tag "Not-done state" todo)
  2046. (const :tag "Done state" done)
  2047. (string :tag "State"))
  2048. (repeat
  2049. (cons :tag "Tag action"
  2050. (string :tag "Tag")
  2051. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  2052. (defcustom org-log-done nil
  2053. "Information to record when a task moves to the DONE state.
  2054. Possible values are:
  2055. nil Don't add anything, just change the keyword
  2056. time Add a time stamp to the task
  2057. note Prompt for a note and add it with template `org-log-note-headings'
  2058. This option can also be set with on a per-file-basis with
  2059. #+STARTUP: nologdone
  2060. #+STARTUP: logdone
  2061. #+STARTUP: lognotedone
  2062. You can have local logging settings for a subtree by setting the LOGGING
  2063. property to one or more of these keywords."
  2064. :group 'org-todo
  2065. :group 'org-progress
  2066. :type '(choice
  2067. (const :tag "No logging" nil)
  2068. (const :tag "Record CLOSED timestamp" time)
  2069. (const :tag "Record CLOSED timestamp with note." note)))
  2070. ;; Normalize old uses of org-log-done.
  2071. (cond
  2072. ((eq org-log-done t) (setq org-log-done 'time))
  2073. ((and (listp org-log-done) (memq 'done org-log-done))
  2074. (setq org-log-done 'note)))
  2075. (defcustom org-log-reschedule nil
  2076. "Information to record when the scheduling date of a tasks is modified.
  2077. Possible values are:
  2078. nil Don't add anything, just change the date
  2079. time Add a time stamp to the task
  2080. note Prompt for a note and add it with template `org-log-note-headings'
  2081. This option can also be set with on a per-file-basis with
  2082. #+STARTUP: nologreschedule
  2083. #+STARTUP: logreschedule
  2084. #+STARTUP: lognotereschedule"
  2085. :group 'org-todo
  2086. :group 'org-progress
  2087. :type '(choice
  2088. (const :tag "No logging" nil)
  2089. (const :tag "Record timestamp" time)
  2090. (const :tag "Record timestamp with note." note)))
  2091. (defcustom org-log-redeadline nil
  2092. "Information to record when the deadline date of a tasks is modified.
  2093. Possible values are:
  2094. nil Don't add anything, just change the date
  2095. time Add a time stamp to the task
  2096. note Prompt for a note and add it with template `org-log-note-headings'
  2097. This option can also be set with on a per-file-basis with
  2098. #+STARTUP: nologredeadline
  2099. #+STARTUP: logredeadline
  2100. #+STARTUP: lognoteredeadline
  2101. You can have local logging settings for a subtree by setting the LOGGING
  2102. property to one or more of these keywords."
  2103. :group 'org-todo
  2104. :group 'org-progress
  2105. :type '(choice
  2106. (const :tag "No logging" nil)
  2107. (const :tag "Record timestamp" time)
  2108. (const :tag "Record timestamp with note." note)))
  2109. (defcustom org-log-note-clock-out nil
  2110. "Non-nil means record a note when clocking out of an item.
  2111. This can also be configured on a per-file basis by adding one of
  2112. the following lines anywhere in the buffer:
  2113. #+STARTUP: lognoteclock-out
  2114. #+STARTUP: nolognoteclock-out"
  2115. :group 'org-todo
  2116. :group 'org-progress
  2117. :type 'boolean)
  2118. (defcustom org-log-done-with-time t
  2119. "Non-nil means the CLOSED time stamp will contain date and time.
  2120. When nil, only the date will be recorded."
  2121. :group 'org-progress
  2122. :type 'boolean)
  2123. (defcustom org-log-note-headings
  2124. '((done . "CLOSING NOTE %t")
  2125. (state . "State %-12s from %-12S %t")
  2126. (note . "Note taken on %t")
  2127. (reschedule . "Rescheduled from %S on %t")
  2128. (delschedule . "Not scheduled, was %S on %t")
  2129. (redeadline . "New deadline from %S on %t")
  2130. (deldeadline . "Removed deadline, was %S on %t")
  2131. (refile . "Refiled on %t")
  2132. (clock-out . ""))
  2133. "Headings for notes added to entries.
  2134. The value is an alist, with the car being a symbol indicating the note
  2135. context, and the cdr is the heading to be used. The heading may also be the
  2136. empty string.
  2137. %t in the heading will be replaced by a time stamp.
  2138. %T will be an active time stamp instead the default inactive one
  2139. %d will be replaced by a short-format time stamp.
  2140. %D will be replaced by an active short-format time stamp.
  2141. %s will be replaced by the new TODO state, in double quotes.
  2142. %S will be replaced by the old TODO state, in double quotes.
  2143. %u will be replaced by the user name.
  2144. %U will be replaced by the full user name.
  2145. In fact, it is not a good idea to change the `state' entry, because
  2146. agenda log mode depends on the format of these entries."
  2147. :group 'org-todo
  2148. :group 'org-progress
  2149. :type '(list :greedy t
  2150. (cons (const :tag "Heading when closing an item" done) string)
  2151. (cons (const :tag
  2152. "Heading when changing todo state (todo sequence only)"
  2153. state) string)
  2154. (cons (const :tag "Heading when just taking a note" note) string)
  2155. (cons (const :tag "Heading when clocking out" clock-out) string)
  2156. (cons (const :tag "Heading when an item is no longer scheduled" delschedule) string)
  2157. (cons (const :tag "Heading when rescheduling" reschedule) string)
  2158. (cons (const :tag "Heading when changing deadline" redeadline) string)
  2159. (cons (const :tag "Heading when deleting a deadline" deldeadline) string)
  2160. (cons (const :tag "Heading when refiling" refile) string)))
  2161. (unless (assq 'note org-log-note-headings)
  2162. (push '(note . "%t") org-log-note-headings))
  2163. (defcustom org-log-into-drawer nil
  2164. "Non-nil means insert state change notes and time stamps into a drawer.
  2165. When nil, state changes notes will be inserted after the headline and
  2166. any scheduling and clock lines, but not inside a drawer.
  2167. The value of this variable should be the name of the drawer to use.
  2168. LOGBOOK is proposed as the default drawer for this purpose, you can
  2169. also set this to a string to define the drawer of your choice.
  2170. A value of t is also allowed, representing \"LOGBOOK\".
  2171. If this variable is set, `org-log-state-notes-insert-after-drawers'
  2172. will be ignored.
  2173. You can set the property LOG_INTO_DRAWER to overrule this setting for
  2174. a subtree."
  2175. :group 'org-todo
  2176. :group 'org-progress
  2177. :type '(choice
  2178. (const :tag "Not into a drawer" nil)
  2179. (const :tag "LOGBOOK" t)
  2180. (string :tag "Other")))
  2181. (if (fboundp 'defvaralias)
  2182. (defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer))
  2183. (defun org-log-into-drawer ()
  2184. "Return the value of `org-log-into-drawer', but let properties overrule.
  2185. If the current entry has or inherits a LOG_INTO_DRAWER property, it will be
  2186. used instead of the default value."
  2187. (let ((p (org-entry-get nil "LOG_INTO_DRAWER" 'inherit)))
  2188. (cond
  2189. ((or (not p) (equal p "nil")) org-log-into-drawer)
  2190. ((equal p "t") "LOGBOOK")
  2191. (t p))))
  2192. (defcustom org-log-state-notes-insert-after-drawers nil
  2193. "Non-nil means insert state change notes after any drawers in entry.
  2194. Only the drawers that *immediately* follow the headline and the
  2195. deadline/scheduled line are skipped.
  2196. When nil, insert notes right after the heading and perhaps the line
  2197. with deadline/scheduling if present.
  2198. This variable will have no effect if `org-log-into-drawer' is
  2199. set."
  2200. :group 'org-todo
  2201. :group 'org-progress
  2202. :type 'boolean)
  2203. (defcustom org-log-states-order-reversed t
  2204. "Non-nil means the latest state note will be directly after heading.
  2205. When nil, the state change notes will be ordered according to time."
  2206. :group 'org-todo
  2207. :group 'org-progress
  2208. :type 'boolean)
  2209. (defcustom org-todo-repeat-to-state nil
  2210. "The TODO state to which a repeater should return the repeating task.
  2211. By default this is the first task in a TODO sequence, or the previous state
  2212. in a TODO_TYP set. But you can specify another task here.
  2213. alternatively, set the :REPEAT_TO_STATE: property of the entry."
  2214. :group 'org-todo
  2215. :version "24.1"
  2216. :type '(choice (const :tag "Head of sequence" nil)
  2217. (string :tag "Specific state")))
  2218. (defcustom org-log-repeat 'time
  2219. "Non-nil means record moving through the DONE state when triggering repeat.
  2220. An auto-repeating task is immediately switched back to TODO when
  2221. marked DONE. If you are not logging state changes (by adding \"@\"
  2222. or \"!\" to the TODO keyword definition), or set `org-log-done' to
  2223. record a closing note, there will be no record of the task moving
  2224. through DONE. This variable forces taking a note anyway.
  2225. nil Don't force a record
  2226. time Record a time stamp
  2227. note Prompt for a note and add it with template `org-log-note-headings'
  2228. This option can also be set with on a per-file-basis with
  2229. #+STARTUP: nologrepeat
  2230. #+STARTUP: logrepeat
  2231. #+STARTUP: lognoterepeat
  2232. You can have local logging settings for a subtree by setting the LOGGING
  2233. property to one or more of these keywords."
  2234. :group 'org-todo
  2235. :group 'org-progress
  2236. :type '(choice
  2237. (const :tag "Don't force a record" nil)
  2238. (const :tag "Force recording the DONE state" time)
  2239. (const :tag "Force recording a note with the DONE state" note)))
  2240. (defgroup org-priorities nil
  2241. "Priorities in Org-mode."
  2242. :tag "Org Priorities"
  2243. :group 'org-todo)
  2244. (defcustom org-enable-priority-commands t
  2245. "Non-nil means priority commands are active.
  2246. When nil, these commands will be disabled, so that you never accidentally
  2247. set a priority."
  2248. :group 'org-priorities
  2249. :type 'boolean)
  2250. (defcustom org-highest-priority ?A
  2251. "The highest priority of TODO items. A character like ?A, ?B etc.
  2252. Must have a smaller ASCII number than `org-lowest-priority'."
  2253. :group 'org-priorities
  2254. :type 'character)
  2255. (defcustom org-lowest-priority ?C
  2256. "The lowest priority of TODO items. A character like ?A, ?B etc.
  2257. Must have a larger ASCII number than `org-highest-priority'."
  2258. :group 'org-priorities
  2259. :type 'character)
  2260. (defcustom org-default-priority ?B
  2261. "The default priority of TODO items.
  2262. This is the priority an item gets if no explicit priority is given.
  2263. When starting to cycle on an empty priority the first step in the cycle
  2264. depends on `org-priority-start-cycle-with-default'. The resulting first
  2265. step priority must not exceed the range from `org-highest-priority' to
  2266. `org-lowest-priority' which means that `org-default-priority' has to be
  2267. in this range exclusive or inclusive the range boundaries. Else the
  2268. first step refuses to set the default and the second will fall back
  2269. to (depending on the command used) the highest or lowest priority."
  2270. :group 'org-priorities
  2271. :type 'character)
  2272. (defcustom org-priority-start-cycle-with-default t
  2273. "Non-nil means start with default priority when starting to cycle.
  2274. When this is nil, the first step in the cycle will be (depending on the
  2275. command used) one higher or lower than the default priority.
  2276. See also `org-default-priority'."
  2277. :group 'org-priorities
  2278. :type 'boolean)
  2279. (defcustom org-get-priority-function nil
  2280. "Function to extract the priority from a string.
  2281. The string is normally the headline. If this is nil Org computes the
  2282. priority from the priority cookie like [#A] in the headline. It returns
  2283. an integer, increasing by 1000 for each priority level.
  2284. The user can set a different function here, which should take a string
  2285. as an argument and return the numeric priority."
  2286. :group 'org-priorities
  2287. :version "24.1"
  2288. :type 'function)
  2289. (defgroup org-time nil
  2290. "Options concerning time stamps and deadlines in Org-mode."
  2291. :tag "Org Time"
  2292. :group 'org)
  2293. (defcustom org-insert-labeled-timestamps-at-point nil
  2294. "Non-nil means SCHEDULED and DEADLINE timestamps are inserted at point.
  2295. When nil, these labeled time stamps are forces into the second line of an
  2296. entry, just after the headline. When scheduling from the global TODO list,
  2297. the time stamp will always be forced into the second line."
  2298. :group 'org-time
  2299. :type 'boolean)
  2300. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  2301. "Formats for `format-time-string' which are used for time stamps.
  2302. It is not recommended to change this constant.")
  2303. (defcustom org-time-stamp-rounding-minutes '(0 5)
  2304. "Number of minutes to round time stamps to.
  2305. These are two values, the first applies when first creating a time stamp.
  2306. The second applies when changing it with the commands `S-up' and `S-down'.
  2307. When changing the time stamp, this means that it will change in steps
  2308. of N minutes, as given by the second value.
  2309. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  2310. numbers should be factors of 60, so for example 5, 10, 15.
  2311. When this is larger than 1, you can still force an exact time stamp by using
  2312. a double prefix argument to a time stamp command like `C-c .' or `C-c !',
  2313. and by using a prefix arg to `S-up/down' to specify the exact number
  2314. of minutes to shift."
  2315. :group 'org-time
  2316. :get #'(lambda (var) ; Make sure both elements are there
  2317. (if (integerp (default-value var))
  2318. (list (default-value var) 5)
  2319. (default-value var)))
  2320. :type '(list
  2321. (integer :tag "when inserting times")
  2322. (integer :tag "when modifying times")))
  2323. ;; Normalize old customizations of this variable.
  2324. (when (integerp org-time-stamp-rounding-minutes)
  2325. (setq org-time-stamp-rounding-minutes
  2326. (list org-time-stamp-rounding-minutes
  2327. org-time-stamp-rounding-minutes)))
  2328. (defcustom org-display-custom-times nil
  2329. "Non-nil means overlay custom formats over all time stamps.
  2330. The formats are defined through the variable `org-time-stamp-custom-formats'.
  2331. To turn this on on a per-file basis, insert anywhere in the file:
  2332. #+STARTUP: customtime"
  2333. :group 'org-time
  2334. :set 'set-default
  2335. :type 'sexp)
  2336. (make-variable-buffer-local 'org-display-custom-times)
  2337. (defcustom org-time-stamp-custom-formats
  2338. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  2339. "Custom formats for time stamps. See `format-time-string' for the syntax.
  2340. These are overlaid over the default ISO format if the variable
  2341. `org-display-custom-times' is set. Time like %H:%M should be at the
  2342. end of the second format. The custom formats are also honored by export
  2343. commands, if custom time display is turned on at the time of export."
  2344. :group 'org-time
  2345. :type 'sexp)
  2346. (defun org-time-stamp-format (&optional long inactive)
  2347. "Get the right format for a time string."
  2348. (let ((f (if long (cdr org-time-stamp-formats)
  2349. (car org-time-stamp-formats))))
  2350. (if inactive
  2351. (concat "[" (substring f 1 -1) "]")
  2352. f)))
  2353. (defcustom org-time-clocksum-format "%d:%02d"
  2354. "The format string used when creating CLOCKSUM lines.
  2355. This is also used when org-mode generates a time duration."
  2356. :group 'org-time
  2357. :type 'string)
  2358. (defcustom org-time-clocksum-use-fractional nil
  2359. "If non-nil, \\[org-clock-display] uses fractional times.
  2360. org-mode generates a time duration."
  2361. :group 'org-time
  2362. :type 'boolean)
  2363. (defcustom org-time-clocksum-fractional-format "%.2f"
  2364. "The format string used when creating CLOCKSUM lines, or when
  2365. org-mode generates a time duration."
  2366. :group 'org-time
  2367. :type 'string)
  2368. (defcustom org-deadline-warning-days 14
  2369. "No. of days before expiration during which a deadline becomes active.
  2370. This variable governs the display in sparse trees and in the agenda.
  2371. When 0 or negative, it means use this number (the absolute value of it)
  2372. even if a deadline has a different individual lead time specified.
  2373. Custom commands can set this variable in the options section."
  2374. :group 'org-time
  2375. :group 'org-agenda-daily/weekly
  2376. :type 'integer)
  2377. (defcustom org-read-date-prefer-future t
  2378. "Non-nil means assume future for incomplete date input from user.
  2379. This affects the following situations:
  2380. 1. The user gives a month but not a year.
  2381. For example, if it is April and you enter \"feb 2\", this will be read
  2382. as Feb 2, *next* year. \"May 5\", however, will be this year.
  2383. 2. The user gives a day, but no month.
  2384. For example, if today is the 15th, and you enter \"3\", Org-mode will
  2385. read this as the third of *next* month. However, if you enter \"17\",
  2386. it will be considered as *this* month.
  2387. If you set this variable to the symbol `time', then also the following
  2388. will work:
  2389. 3. If the user gives a time.
  2390. If the time is before now, it will be interpreted as tomorrow.
  2391. Currently none of this works for ISO week specifications.
  2392. When this option is nil, the current day, month and year will always be
  2393. used as defaults.
  2394. See also `org-agenda-jump-prefer-future'."
  2395. :group 'org-time
  2396. :type '(choice
  2397. (const :tag "Never" nil)
  2398. (const :tag "Check month and day" t)
  2399. (const :tag "Check month, day, and time" time)))
  2400. (defcustom org-agenda-jump-prefer-future 'org-read-date-prefer-future
  2401. "Should the agenda jump command prefer the future for incomplete dates?
  2402. The default is to do the same as configured in `org-read-date-prefer-future'.
  2403. But you can also set a deviating value here.
  2404. This may t or nil, or the symbol `org-read-date-prefer-future'."
  2405. :group 'org-agenda
  2406. :group 'org-time
  2407. :version "24.1"
  2408. :type '(choice
  2409. (const :tag "Use org-read-date-prefer-future"
  2410. org-read-date-prefer-future)
  2411. (const :tag "Never" nil)
  2412. (const :tag "Always" t)))
  2413. (defcustom org-read-date-force-compatible-dates t
  2414. "Should date/time prompt force dates that are guaranteed to work in Emacs?
  2415. Depending on the system Emacs is running on, certain dates cannot
  2416. be represented with the type used internally to represent time.
  2417. Dates between 1970-1-1 and 2038-1-1 can always be represented
  2418. correctly. Some systems allow for earlier dates, some for later,
  2419. some for both. One way to find out it to insert any date into an
  2420. Org buffer, putting the cursor on the year and hitting S-up and
  2421. S-down to test the range.
  2422. When this variable is set to t, the date/time prompt will not let
  2423. you specify dates outside the 1970-2037 range, so it is certain that
  2424. these dates will work in whatever version of Emacs you are
  2425. running, and also that you can move a file from one Emacs implementation
  2426. to another. WHenever Org is forcing the year for you, it will display
  2427. a message and beep.
  2428. When this variable is nil, Org will check if the date is
  2429. representable in the specific Emacs implementation you are using.
  2430. If not, it will force a year, usually the current year, and beep
  2431. to remind you. Currently this setting is not recommended because
  2432. the likelihood that you will open your Org files in an Emacs that
  2433. has limited date range is not negligible.
  2434. A workaround for this problem is to use diary sexp dates for time
  2435. stamps outside of this range."
  2436. :group 'org-time
  2437. :version "24.1"
  2438. :type 'boolean)
  2439. (defcustom org-read-date-display-live t
  2440. "Non-nil means display current interpretation of date prompt live.
  2441. This display will be in an overlay, in the minibuffer."
  2442. :group 'org-time
  2443. :type 'boolean)
  2444. (defcustom org-read-date-popup-calendar t
  2445. "Non-nil means pop up a calendar when prompting for a date.
  2446. In the calendar, the date can be selected with mouse-1. However, the
  2447. minibuffer will also be active, and you can simply enter the date as well.
  2448. When nil, only the minibuffer will be available."
  2449. :group 'org-time
  2450. :type 'boolean)
  2451. (if (fboundp 'defvaralias)
  2452. (defvaralias 'org-popup-calendar-for-date-prompt
  2453. 'org-read-date-popup-calendar))
  2454. (defcustom org-read-date-minibuffer-setup-hook nil
  2455. "Hook to be used to set up keys for the date/time interface.
  2456. Add key definitions to `minibuffer-local-map', which will be a temporary
  2457. copy."
  2458. :group 'org-time
  2459. :type 'hook)
  2460. (defcustom org-extend-today-until 0
  2461. "The hour when your day really ends. Must be an integer.
  2462. This has influence for the following applications:
  2463. - When switching the agenda to \"today\". It it is still earlier than
  2464. the time given here, the day recognized as TODAY is actually yesterday.
  2465. - When a date is read from the user and it is still before the time given
  2466. here, the current date and time will be assumed to be yesterday, 23:59.
  2467. Also, timestamps inserted in capture templates follow this rule.
  2468. IMPORTANT: This is a feature whose implementation is and likely will
  2469. remain incomplete. Really, it is only here because past midnight seems to
  2470. be the favorite working time of John Wiegley :-)"
  2471. :group 'org-time
  2472. :type 'integer)
  2473. (defcustom org-use-effective-time nil
  2474. "If non-nil, consider `org-extend-today-until' when creating timestamps.
  2475. For example, if `org-extend-today-until' is 8, and it's 4am, then the
  2476. \"effective time\" of any timestamps between midnight and 8am will be
  2477. 23:59 of the previous day."
  2478. :group 'org-time
  2479. :version "24.1"
  2480. :type 'boolean)
  2481. (defcustom org-use-last-clock-out-time-as-effective-time nil
  2482. "When non-nil, use the last clock out time for `org-todo'.
  2483. Note that this option has precedence over the combined use of
  2484. `org-use-effective-time' and `org-extend-today-until'."
  2485. :group 'org-time
  2486. ;; :version "24.3"
  2487. :type 'boolean)
  2488. (defcustom org-edit-timestamp-down-means-later nil
  2489. "Non-nil means S-down will increase the time in a time stamp.
  2490. When nil, S-up will increase."
  2491. :group 'org-time
  2492. :type 'boolean)
  2493. (defcustom org-calendar-follow-timestamp-change t
  2494. "Non-nil means make the calendar window follow timestamp changes.
  2495. When a timestamp is modified and the calendar window is visible, it will be
  2496. moved to the new date."
  2497. :group 'org-time
  2498. :type 'boolean)
  2499. (defgroup org-tags nil
  2500. "Options concerning tags in Org-mode."
  2501. :tag "Org Tags"
  2502. :group 'org)
  2503. (defcustom org-tag-alist nil
  2504. "List of tags allowed in Org-mode files.
  2505. When this list is nil, Org-mode will base TAG input on what is already in the
  2506. buffer.
  2507. The value of this variable is an alist, the car of each entry must be a
  2508. keyword as a string, the cdr may be a character that is used to select
  2509. that tag through the fast-tag-selection interface.
  2510. See the manual for details."
  2511. :group 'org-tags
  2512. :type '(repeat
  2513. (choice
  2514. (cons (string :tag "Tag name")
  2515. (character :tag "Access char"))
  2516. (list :tag "Start radio group"
  2517. (const :startgroup)
  2518. (option (string :tag "Group description")))
  2519. (list :tag "End radio group"
  2520. (const :endgroup)
  2521. (option (string :tag "Group description")))
  2522. (const :tag "New line" (:newline)))))
  2523. (defcustom org-tag-persistent-alist nil
  2524. "List of tags that will always appear in all Org-mode files.
  2525. This is in addition to any in buffer settings or customizations
  2526. of `org-tag-alist'.
  2527. When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
  2528. The value of this variable is an alist, the car of each entry must be a
  2529. keyword as a string, the cdr may be a character that is used to select
  2530. that tag through the fast-tag-selection interface.
  2531. See the manual for details.
  2532. To disable these tags on a per-file basis, insert anywhere in the file:
  2533. #+STARTUP: noptag"
  2534. :group 'org-tags
  2535. :type '(repeat
  2536. (choice
  2537. (cons (string :tag "Tag name")
  2538. (character :tag "Access char"))
  2539. (const :tag "Start radio group" (:startgroup))
  2540. (const :tag "End radio group" (:endgroup))
  2541. (const :tag "New line" (:newline)))))
  2542. (defcustom org-complete-tags-always-offer-all-agenda-tags nil
  2543. "If non-nil, always offer completion for all tags of all agenda files.
  2544. Instead of customizing this variable directly, you might want to
  2545. set it locally for capture buffers, because there no list of
  2546. tags in that file can be created dynamically (there are none).
  2547. (add-hook 'org-capture-mode-hook
  2548. (lambda ()
  2549. (set (make-local-variable
  2550. 'org-complete-tags-always-offer-all-agenda-tags)
  2551. t)))"
  2552. :group 'org-tags
  2553. :version "24.1"
  2554. :type 'boolean)
  2555. (defvar org-file-tags nil
  2556. "List of tags that can be inherited by all entries in the file.
  2557. The tags will be inherited if the variable `org-use-tag-inheritance'
  2558. says they should be.
  2559. This variable is populated from #+FILETAGS lines.")
  2560. (defcustom org-use-fast-tag-selection 'auto
  2561. "Non-nil means use fast tag selection scheme.
  2562. This is a special interface to select and deselect tags with single keys.
  2563. When nil, fast selection is never used.
  2564. When the symbol `auto', fast selection is used if and only if selection
  2565. characters for tags have been configured, either through the variable
  2566. `org-tag-alist' or through a #+TAGS line in the buffer.
  2567. When t, fast selection is always used and selection keys are assigned
  2568. automatically if necessary."
  2569. :group 'org-tags
  2570. :type '(choice
  2571. (const :tag "Always" t)
  2572. (const :tag "Never" nil)
  2573. (const :tag "When selection characters are configured" 'auto)))
  2574. (defcustom org-fast-tag-selection-single-key nil
  2575. "Non-nil means fast tag selection exits after first change.
  2576. When nil, you have to press RET to exit it.
  2577. During fast tag selection, you can toggle this flag with `C-c'.
  2578. This variable can also have the value `expert'. In this case, the window
  2579. displaying the tags menu is not even shown, until you press C-c again."
  2580. :group 'org-tags
  2581. :type '(choice
  2582. (const :tag "No" nil)
  2583. (const :tag "Yes" t)
  2584. (const :tag "Expert" expert)))
  2585. (defvar org-fast-tag-selection-include-todo nil
  2586. "Non-nil means fast tags selection interface will also offer TODO states.
  2587. This is an undocumented feature, you should not rely on it.")
  2588. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  2589. "The column to which tags should be indented in a headline.
  2590. If this number is positive, it specifies the column. If it is negative,
  2591. it means that the tags should be flushright to that column. For example,
  2592. -80 works well for a normal 80 character screen.
  2593. When 0, place tags directly after headline text, with only one space in
  2594. between."
  2595. :group 'org-tags
  2596. :type 'integer)
  2597. (defcustom org-auto-align-tags t
  2598. "Non-nil keeps tags aligned when modifying headlines.
  2599. Some operations (i.e. demoting) change the length of a headline and
  2600. therefore shift the tags around. With this option turned on, after
  2601. each such operation the tags are again aligned to `org-tags-column'."
  2602. :group 'org-tags
  2603. :type 'boolean)
  2604. (defcustom org-use-tag-inheritance t
  2605. "Non-nil means tags in levels apply also for sublevels.
  2606. When nil, only the tags directly given in a specific line apply there.
  2607. This may also be a list of tags that should be inherited, or a regexp that
  2608. matches tags that should be inherited. Additional control is possible
  2609. with the variable `org-tags-exclude-from-inheritance' which gives an
  2610. explicit list of tags to be excluded from inheritance., even if the value of
  2611. `org-use-tag-inheritance' would select it for inheritance.
  2612. If this option is t, a match early-on in a tree can lead to a large
  2613. number of matches in the subtree when constructing the agenda or creating
  2614. a sparse tree. If you only want to see the first match in a tree during
  2615. a search, check out the variable `org-tags-match-list-sublevels'."
  2616. :group 'org-tags
  2617. :type '(choice
  2618. (const :tag "Not" nil)
  2619. (const :tag "Always" t)
  2620. (repeat :tag "Specific tags" (string :tag "Tag"))
  2621. (regexp :tag "Tags matched by regexp")))
  2622. (defcustom org-tags-exclude-from-inheritance nil
  2623. "List of tags that should never be inherited.
  2624. This is a way to exclude a few tags from inheritance. For way to do
  2625. the opposite, to actively allow inheritance for selected tags,
  2626. see the variable `org-use-tag-inheritance'."
  2627. :group 'org-tags
  2628. :type '(repeat (string :tag "Tag")))
  2629. (defun org-tag-inherit-p (tag)
  2630. "Check if TAG is one that should be inherited."
  2631. (cond
  2632. ((member tag org-tags-exclude-from-inheritance) nil)
  2633. ((eq org-use-tag-inheritance t) t)
  2634. ((not org-use-tag-inheritance) nil)
  2635. ((stringp org-use-tag-inheritance)
  2636. (string-match org-use-tag-inheritance tag))
  2637. ((listp org-use-tag-inheritance)
  2638. (member tag org-use-tag-inheritance))
  2639. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  2640. (defcustom org-tags-match-list-sublevels t
  2641. "Non-nil means list also sublevels of headlines matching a search.
  2642. This variable applies to tags/property searches, and also to stuck
  2643. projects because this search is based on a tags match as well.
  2644. When set to the symbol `indented', sublevels are indented with
  2645. leading dots.
  2646. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  2647. the sublevels of a headline matching a tag search often also match
  2648. the same search. Listing all of them can create very long lists.
  2649. Setting this variable to nil causes subtrees of a match to be skipped.
  2650. This variable is semi-obsolete and probably should always be true. It
  2651. is better to limit inheritance to certain tags using the variables
  2652. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  2653. :group 'org-tags
  2654. :type '(choice
  2655. (const :tag "No, don't list them" nil)
  2656. (const :tag "Yes, do list them" t)
  2657. (const :tag "List them, indented with leading dots" indented)))
  2658. (defcustom org-tags-sort-function nil
  2659. "When set, tags are sorted using this function as a comparator."
  2660. :group 'org-tags
  2661. :type '(choice
  2662. (const :tag "No sorting" nil)
  2663. (const :tag "Alphabetical" string<)
  2664. (const :tag "Reverse alphabetical" string>)
  2665. (function :tag "Custom function" nil)))
  2666. (defvar org-tags-history nil
  2667. "History of minibuffer reads for tags.")
  2668. (defvar org-last-tags-completion-table nil
  2669. "The last used completion table for tags.")
  2670. (defvar org-after-tags-change-hook nil
  2671. "Hook that is run after the tags in a line have changed.")
  2672. (defgroup org-properties nil
  2673. "Options concerning properties in Org-mode."
  2674. :tag "Org Properties"
  2675. :group 'org)
  2676. (defcustom org-property-format "%-10s %s"
  2677. "How property key/value pairs should be formatted by `indent-line'.
  2678. When `indent-line' hits a property definition, it will format the line
  2679. according to this format, mainly to make sure that the values are
  2680. lined-up with respect to each other."
  2681. :group 'org-properties
  2682. :type 'string)
  2683. (defcustom org-properties-postprocess-alist nil
  2684. "Alist of properties and functions to adjust inserted values.
  2685. Elements of this alist must be of the form
  2686. ([string] [function])
  2687. where [string] must be a property name and [function] must be a
  2688. lambda expression: this lambda expression must take one argument,
  2689. the value to adjust, and return the new value as a string.
  2690. For example, this element will allow the property \"Remaining\"
  2691. to be updated wrt the relation between the \"Effort\" property
  2692. and the clock summary:
  2693. ((\"Remaining\" (lambda(value)
  2694. (let ((clocksum (org-clock-sum-current-item))
  2695. (effort (org-duration-string-to-minutes
  2696. (org-entry-get (point) \"Effort\"))))
  2697. (org-minutes-to-hh:mm-string (- effort clocksum))))))"
  2698. :group 'org-properties
  2699. :version "24.1"
  2700. :type '(alist :key-type (string :tag "Property")
  2701. :value-type (function :tag "Function")))
  2702. (defcustom org-use-property-inheritance nil
  2703. "Non-nil means properties apply also for sublevels.
  2704. This setting is chiefly used during property searches. Turning it on can
  2705. cause significant overhead when doing a search, which is why it is not
  2706. on by default.
  2707. When nil, only the properties directly given in the current entry count.
  2708. When t, every property is inherited. The value may also be a list of
  2709. properties that should have inheritance, or a regular expression matching
  2710. properties that should be inherited.
  2711. However, note that some special properties use inheritance under special
  2712. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  2713. and the properties ending in \"_ALL\" when they are used as descriptor
  2714. for valid values of a property.
  2715. Note for programmers:
  2716. When querying an entry with `org-entry-get', you can control if inheritance
  2717. should be used. By default, `org-entry-get' looks only at the local
  2718. properties. You can request inheritance by setting the inherit argument
  2719. to t (to force inheritance) or to `selective' (to respect the setting
  2720. in this variable)."
  2721. :group 'org-properties
  2722. :type '(choice
  2723. (const :tag "Not" nil)
  2724. (const :tag "Always" t)
  2725. (repeat :tag "Specific properties" (string :tag "Property"))
  2726. (regexp :tag "Properties matched by regexp")))
  2727. (defun org-property-inherit-p (property)
  2728. "Check if PROPERTY is one that should be inherited."
  2729. (cond
  2730. ((eq org-use-property-inheritance t) t)
  2731. ((not org-use-property-inheritance) nil)
  2732. ((stringp org-use-property-inheritance)
  2733. (string-match org-use-property-inheritance property))
  2734. ((listp org-use-property-inheritance)
  2735. (member property org-use-property-inheritance))
  2736. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  2737. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  2738. "The default column format, if no other format has been defined.
  2739. This variable can be set on the per-file basis by inserting a line
  2740. #+COLUMNS: %25ITEM ....."
  2741. :group 'org-properties
  2742. :type 'string)
  2743. (defcustom org-columns-ellipses ".."
  2744. "The ellipses to be used when a field in column view is truncated.
  2745. When this is the empty string, as many characters as possible are shown,
  2746. but then there will be no visual indication that the field has been truncated.
  2747. When this is a string of length N, the last N characters of a truncated
  2748. field are replaced by this string. If the column is narrower than the
  2749. ellipses string, only part of the ellipses string will be shown."
  2750. :group 'org-properties
  2751. :type 'string)
  2752. (defcustom org-columns-modify-value-for-display-function nil
  2753. "Function that modifies values for display in column view.
  2754. For example, it can be used to cut out a certain part from a time stamp.
  2755. The function must take 2 arguments:
  2756. column-title The title of the column (*not* the property name)
  2757. value The value that should be modified.
  2758. The function should return the value that should be displayed,
  2759. or nil if the normal value should be used."
  2760. :group 'org-properties
  2761. :type 'function)
  2762. (defcustom org-effort-property "Effort"
  2763. "The property that is being used to keep track of effort estimates.
  2764. Effort estimates given in this property need to have the format H:MM."
  2765. :group 'org-properties
  2766. :group 'org-progress
  2767. :type '(string :tag "Property"))
  2768. (defconst org-global-properties-fixed
  2769. '(("VISIBILITY_ALL" . "folded children content all")
  2770. ("CLOCK_MODELINE_TOTAL_ALL" . "current today repeat all auto"))
  2771. "List of property/value pairs that can be inherited by any entry.
  2772. These are fixed values, for the preset properties. The user variable
  2773. that can be used to add to this list is `org-global-properties'.
  2774. The entries in this list are cons cells where the car is a property
  2775. name and cdr is a string with the value. If the value represents
  2776. multiple items like an \"_ALL\" property, separate the items by
  2777. spaces.")
  2778. (defcustom org-global-properties nil
  2779. "List of property/value pairs that can be inherited by any entry.
  2780. This list will be combined with the constant `org-global-properties-fixed'.
  2781. The entries in this list are cons cells where the car is a property
  2782. name and cdr is a string with the value.
  2783. You can set buffer-local values for the same purpose in the variable
  2784. `org-file-properties' this by adding lines like
  2785. #+PROPERTY: NAME VALUE"
  2786. :group 'org-properties
  2787. :type '(repeat
  2788. (cons (string :tag "Property")
  2789. (string :tag "Value"))))
  2790. (defvar org-file-properties nil
  2791. "List of property/value pairs that can be inherited by any entry.
  2792. Valid for the current buffer.
  2793. This variable is populated from #+PROPERTY lines.")
  2794. (make-variable-buffer-local 'org-file-properties)
  2795. (defgroup org-agenda nil
  2796. "Options concerning agenda views in Org-mode."
  2797. :tag "Org Agenda"
  2798. :group 'org)
  2799. (defvar org-category nil
  2800. "Variable used by org files to set a category for agenda display.
  2801. Such files should use a file variable to set it, for example
  2802. # -*- mode: org; org-category: \"ELisp\"
  2803. or contain a special line
  2804. #+CATEGORY: ELisp
  2805. If the file does not specify a category, then file's base name
  2806. is used instead.")
  2807. (make-variable-buffer-local 'org-category)
  2808. (put 'org-category 'safe-local-variable #'(lambda (x) (or (symbolp x) (stringp x))))
  2809. (defcustom org-agenda-files nil
  2810. "The files to be used for agenda display.
  2811. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  2812. \\[org-remove-file]. You can also use customize to edit the list.
  2813. If an entry is a directory, all files in that directory that are matched by
  2814. `org-agenda-file-regexp' will be part of the file list.
  2815. If the value of the variable is not a list but a single file name, then
  2816. the list of agenda files is actually stored and maintained in that file, one
  2817. agenda file per line. In this file paths can be given relative to
  2818. `org-directory'. Tilde expansion and environment variable substitution
  2819. are also made."
  2820. :group 'org-agenda
  2821. :type '(choice
  2822. (repeat :tag "List of files and directories" file)
  2823. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  2824. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  2825. "Regular expression to match files for `org-agenda-files'.
  2826. If any element in the list in that variable contains a directory instead
  2827. of a normal file, all files in that directory that are matched by this
  2828. regular expression will be included."
  2829. :group 'org-agenda
  2830. :type 'regexp)
  2831. (defcustom org-agenda-text-search-extra-files nil
  2832. "List of extra files to be searched by text search commands.
  2833. These files will be search in addition to the agenda files by the
  2834. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  2835. Note that these files will only be searched for text search commands,
  2836. not for the other agenda views like todo lists, tag searches or the weekly
  2837. agenda. This variable is intended to list notes and possibly archive files
  2838. that should also be searched by these two commands.
  2839. In fact, if the first element in the list is the symbol `agenda-archives',
  2840. than all archive files of all agenda files will be added to the search
  2841. scope."
  2842. :group 'org-agenda
  2843. :type '(set :greedy t
  2844. (const :tag "Agenda Archives" agenda-archives)
  2845. (repeat :inline t (file))))
  2846. (if (fboundp 'defvaralias)
  2847. (defvaralias 'org-agenda-multi-occur-extra-files
  2848. 'org-agenda-text-search-extra-files))
  2849. (defcustom org-agenda-skip-unavailable-files nil
  2850. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  2851. A nil value means to remove them, after a query, from the list."
  2852. :group 'org-agenda
  2853. :type 'boolean)
  2854. (defcustom org-calendar-to-agenda-key [?c]
  2855. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  2856. The command `org-calendar-goto-agenda' will be bound to this key. The
  2857. default is the character `c' because then `c' can be used to switch back and
  2858. forth between agenda and calendar."
  2859. :group 'org-agenda
  2860. :type 'sexp)
  2861. (defcustom org-calendar-insert-diary-entry-key [?i]
  2862. "The key to be installed in `calendar-mode-map' for adding diary entries.
  2863. This option is irrelevant until `org-agenda-diary-file' has been configured
  2864. to point to an Org-mode file. When that is the case, the command
  2865. `org-agenda-diary-entry' will be bound to the key given here, by default
  2866. `i'. In the calendar, `i' normally adds entries to `diary-file'. So
  2867. if you want to continue doing this, you need to change this to a different
  2868. key."
  2869. :group 'org-agenda
  2870. :type 'sexp)
  2871. (defcustom org-agenda-diary-file 'diary-file
  2872. "File to which to add new entries with the `i' key in agenda and calendar.
  2873. When this is the symbol `diary-file', the functionality in the Emacs
  2874. calendar will be used to add entries to the `diary-file'. But when this
  2875. points to a file, `org-agenda-diary-entry' will be used instead."
  2876. :group 'org-agenda
  2877. :type '(choice
  2878. (const :tag "The standard Emacs diary file" diary-file)
  2879. (file :tag "Special Org file diary entries")))
  2880. (eval-after-load "calendar"
  2881. '(progn
  2882. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  2883. 'org-calendar-goto-agenda)
  2884. (add-hook 'calendar-mode-hook
  2885. (lambda ()
  2886. (unless (eq org-agenda-diary-file 'diary-file)
  2887. (define-key calendar-mode-map
  2888. org-calendar-insert-diary-entry-key
  2889. 'org-agenda-diary-entry))))))
  2890. (defgroup org-latex nil
  2891. "Options for embedding LaTeX code into Org-mode."
  2892. :tag "Org LaTeX"
  2893. :group 'org)
  2894. (defcustom org-format-latex-options
  2895. '(:foreground default :background default :scale 1.0
  2896. :html-foreground "Black" :html-background "Transparent"
  2897. :html-scale 1.0 :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  2898. "Options for creating images from LaTeX fragments.
  2899. This is a property list with the following properties:
  2900. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  2901. `default' means use the foreground of the default face.
  2902. `auto' means use the foreground from the text face.
  2903. :background the background color, or \"Transparent\".
  2904. `default' means use the background of the default face.
  2905. `auto' means use the background from the text face.
  2906. :scale a scaling factor for the size of the images, to get more pixels
  2907. :html-foreground, :html-background, :html-scale
  2908. the same numbers for HTML export.
  2909. :matchers a list indicating which matchers should be used to
  2910. find LaTeX fragments. Valid members of this list are:
  2911. \"begin\" find environments
  2912. \"$1\" find single characters surrounded by $.$
  2913. \"$\" find math expressions surrounded by $...$
  2914. \"$$\" find math expressions surrounded by $$....$$
  2915. \"\\(\" find math expressions surrounded by \\(...\\)
  2916. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  2917. :group 'org-latex
  2918. :type 'plist)
  2919. (defcustom org-format-latex-signal-error t
  2920. "Non-nil means signal an error when image creation of LaTeX snippets fails.
  2921. When nil, just push out a message."
  2922. :group 'org-latex
  2923. :version "24.1"
  2924. :type 'boolean)
  2925. (defcustom org-latex-to-mathml-jar-file nil
  2926. "Value of\"%j\" in `org-latex-to-mathml-convert-command'.
  2927. Use this to specify additional executable file say a jar file.
  2928. When using MathToWeb as the converter, specify the full-path to
  2929. your mathtoweb.jar file."
  2930. :group 'org-latex
  2931. :version "24.1"
  2932. :type '(choice
  2933. (const :tag "None" nil)
  2934. (file :tag "JAR file" :must-match t)))
  2935. (defcustom org-latex-to-mathml-convert-command nil
  2936. "Command to convert LaTeX fragments to MathML.
  2937. Replace format-specifiers in the command as noted below and use
  2938. `shell-command' to convert LaTeX to MathML.
  2939. %j: Executable file in fully expanded form as specified by
  2940. `org-latex-to-mathml-jar-file'.
  2941. %I: Input LaTeX file in fully expanded form
  2942. %o: Output MathML file
  2943. This command is used by `org-create-math-formula'.
  2944. When using MathToWeb as the converter, set this to
  2945. \"java -jar %j -unicode -force -df %o %I\"."
  2946. :group 'org-latex
  2947. :version "24.1"
  2948. :type '(choice
  2949. (const :tag "None" nil)
  2950. (string :tag "\nShell command")))
  2951. (defcustom org-latex-create-formula-image-program 'dvipng
  2952. "Program to convert LaTeX fragments with.
  2953. dvipng Process the LaTeX fragments to dvi file, then convert
  2954. dvi files to png files using dvipng.
  2955. This will also include processing of non-math environments.
  2956. imagemagick Convert the LaTeX fragments to pdf files and use imagemagick
  2957. to convert pdf files to png files"
  2958. :group 'org-latex
  2959. :version "24.1"
  2960. :type '(choice
  2961. (const :tag "dvipng" dvipng)
  2962. (const :tag "imagemagick" imagemagick)))
  2963. (defcustom org-latex-preview-ltxpng-directory "ltxpng/"
  2964. "Path to store latex preview images. A relative path here creates many
  2965. directories relative to the processed org files paths. An absolute path
  2966. puts all preview images at the same place."
  2967. :group 'org-latex
  2968. :version "24.3"
  2969. :type 'string)
  2970. (defun org-format-latex-mathml-available-p ()
  2971. "Return t if `org-latex-to-mathml-convert-command' is usable."
  2972. (save-match-data
  2973. (when (and (boundp 'org-latex-to-mathml-convert-command)
  2974. org-latex-to-mathml-convert-command)
  2975. (let ((executable (car (split-string
  2976. org-latex-to-mathml-convert-command))))
  2977. (when (executable-find executable)
  2978. (if (string-match
  2979. "%j" org-latex-to-mathml-convert-command)
  2980. (file-readable-p org-latex-to-mathml-jar-file)
  2981. t))))))
  2982. (defcustom org-format-latex-header "\\documentclass{article}
  2983. \\usepackage[usenames]{color}
  2984. \\usepackage{amsmath}
  2985. \\usepackage[mathscr]{eucal}
  2986. \\pagestyle{empty} % do not remove
  2987. \[PACKAGES]
  2988. \[DEFAULT-PACKAGES]
  2989. % The settings below are copied from fullpage.sty
  2990. \\setlength{\\textwidth}{\\paperwidth}
  2991. \\addtolength{\\textwidth}{-3cm}
  2992. \\setlength{\\oddsidemargin}{1.5cm}
  2993. \\addtolength{\\oddsidemargin}{-2.54cm}
  2994. \\setlength{\\evensidemargin}{\\oddsidemargin}
  2995. \\setlength{\\textheight}{\\paperheight}
  2996. \\addtolength{\\textheight}{-\\headheight}
  2997. \\addtolength{\\textheight}{-\\headsep}
  2998. \\addtolength{\\textheight}{-\\footskip}
  2999. \\addtolength{\\textheight}{-3cm}
  3000. \\setlength{\\topmargin}{1.5cm}
  3001. \\addtolength{\\topmargin}{-2.54cm}"
  3002. "The document header used for processing LaTeX fragments.
  3003. It is imperative that this header make sure that no page number
  3004. appears on the page. The package defined in the variables
  3005. `org-export-latex-default-packages-alist' and `org-export-latex-packages-alist'
  3006. will either replace the placeholder \"[PACKAGES]\" in this header, or they
  3007. will be appended."
  3008. :group 'org-latex
  3009. :type 'string)
  3010. (defvar org-format-latex-header-extra nil)
  3011. (defun org-set-packages-alist (var val)
  3012. "Set the packages alist and make sure it has 3 elements per entry."
  3013. (set var (mapcar (lambda (x)
  3014. (if (and (consp x) (= (length x) 2))
  3015. (list (car x) (nth 1 x) t)
  3016. x))
  3017. val)))
  3018. (defun org-get-packages-alist (var)
  3019. "Get the packages alist and make sure it has 3 elements per entry."
  3020. (mapcar (lambda (x)
  3021. (if (and (consp x) (= (length x) 2))
  3022. (list (car x) (nth 1 x) t)
  3023. x))
  3024. (default-value var)))
  3025. ;; The following variables are defined here because is it also used
  3026. ;; when formatting latex fragments. Originally it was part of the
  3027. ;; LaTeX exporter, which is why the name includes "export".
  3028. (defcustom org-export-latex-default-packages-alist
  3029. '(("AUTO" "inputenc" t)
  3030. ("T1" "fontenc" t)
  3031. ("" "fixltx2e" nil)
  3032. ("" "graphicx" t)
  3033. ("" "longtable" nil)
  3034. ("" "float" nil)
  3035. ("" "wrapfig" nil)
  3036. ("" "soul" t)
  3037. ("" "textcomp" t)
  3038. ("" "marvosym" t)
  3039. ("" "wasysym" t)
  3040. ("" "latexsym" t)
  3041. ("" "amssymb" t)
  3042. ("" "hyperref" nil)
  3043. "\\tolerance=1000"
  3044. )
  3045. "Alist of default packages to be inserted in the header.
  3046. Change this only if one of the packages here causes an incompatibility
  3047. with another package you are using.
  3048. The packages in this list are needed by one part or another of Org-mode
  3049. to function properly.
  3050. - inputenc, fontenc: for basic font and character selection
  3051. - textcomp, marvosymb, wasysym, latexsym, amssym: for various symbols used
  3052. for interpreting the entities in `org-entities'. You can skip some of these
  3053. packages if you don't use any of the symbols in it.
  3054. - graphicx: for including images
  3055. - float, wrapfig: for figure placement
  3056. - longtable: for long tables
  3057. - hyperref: for cross references
  3058. Therefore you should not modify this variable unless you know what you
  3059. are doing. The one reason to change it anyway is that you might be loading
  3060. some other package that conflicts with one of the default packages.
  3061. Each cell is of the format \( \"options\" \"package\" snippet-flag\).
  3062. If SNIPPET-FLAG is t, the package also needs to be included when
  3063. compiling LaTeX snippets into images for inclusion into HTML."
  3064. :group 'org-export-latex
  3065. :set 'org-set-packages-alist
  3066. :get 'org-get-packages-alist
  3067. :version "24.1"
  3068. :type '(repeat
  3069. (choice
  3070. (list :tag "options/package pair"
  3071. (string :tag "options")
  3072. (string :tag "package")
  3073. (boolean :tag "Snippet"))
  3074. (string :tag "A line of LaTeX"))))
  3075. (defcustom org-export-latex-packages-alist nil
  3076. "Alist of packages to be inserted in every LaTeX header.
  3077. These will be inserted after `org-export-latex-default-packages-alist'.
  3078. Each cell is of the format \( \"options\" \"package\" snippet-flag \).
  3079. SNIPPET-FLAG, when t, indicates that this package is also needed when
  3080. turning LaTeX snippets into images for inclusion into HTML.
  3081. Make sure that you only list packages here which:
  3082. - you want in every file
  3083. - do not conflict with the default packages in
  3084. `org-export-latex-default-packages-alist'
  3085. - do not conflict with the setup in `org-format-latex-header'."
  3086. :group 'org-export-latex
  3087. :set 'org-set-packages-alist
  3088. :get 'org-get-packages-alist
  3089. :type '(repeat
  3090. (choice
  3091. (list :tag "options/package pair"
  3092. (string :tag "options")
  3093. (string :tag "package")
  3094. (boolean :tag "Snippet"))
  3095. (string :tag "A line of LaTeX"))))
  3096. (defgroup org-appearance nil
  3097. "Settings for Org-mode appearance."
  3098. :tag "Org Appearance"
  3099. :group 'org)
  3100. (defcustom org-level-color-stars-only nil
  3101. "Non-nil means fontify only the stars in each headline.
  3102. When nil, the entire headline is fontified.
  3103. Changing it requires restart of `font-lock-mode' to become effective
  3104. also in regions already fontified."
  3105. :group 'org-appearance
  3106. :type 'boolean)
  3107. (defcustom org-hide-leading-stars nil
  3108. "Non-nil means hide the first N-1 stars in a headline.
  3109. This works by using the face `org-hide' for these stars. This
  3110. face is white for a light background, and black for a dark
  3111. background. You may have to customize the face `org-hide' to
  3112. make this work.
  3113. Changing it requires restart of `font-lock-mode' to become effective
  3114. also in regions already fontified.
  3115. You may also set this on a per-file basis by adding one of the following
  3116. lines to the buffer:
  3117. #+STARTUP: hidestars
  3118. #+STARTUP: showstars"
  3119. :group 'org-appearance
  3120. :type 'boolean)
  3121. (defcustom org-hidden-keywords nil
  3122. "List of symbols corresponding to keywords to be hidden the org buffer.
  3123. For example, a value '(title) for this list will make the document's title
  3124. appear in the buffer without the initial #+TITLE: keyword."
  3125. :group 'org-appearance
  3126. :version "24.1"
  3127. :type '(set (const :tag "#+AUTHOR" author)
  3128. (const :tag "#+DATE" date)
  3129. (const :tag "#+EMAIL" email)
  3130. (const :tag "#+TITLE" title)))
  3131. (defcustom org-custom-properties nil
  3132. "List of properties (as strings) with a special meaning.
  3133. The default use of these custom properties is to let the user
  3134. hide them with `org-toggle-custom-properties-visibility'."
  3135. :group 'org-properties
  3136. :group 'org-appearance
  3137. :version "24.3"
  3138. :type '(repeat (string :tag "Property Name")))
  3139. (defcustom org-fontify-done-headline nil
  3140. "Non-nil means change the face of a headline if it is marked DONE.
  3141. Normally, only the TODO/DONE keyword indicates the state of a headline.
  3142. When this is non-nil, the headline after the keyword is set to the
  3143. `org-headline-done' as an additional indication."
  3144. :group 'org-appearance
  3145. :type 'boolean)
  3146. (defcustom org-fontify-emphasized-text t
  3147. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  3148. Changing this variable requires a restart of Emacs to take effect."
  3149. :group 'org-appearance
  3150. :type 'boolean)
  3151. (defcustom org-fontify-whole-heading-line nil
  3152. "Non-nil means fontify the whole line for headings.
  3153. This is useful when setting a background color for the
  3154. org-level-* faces."
  3155. :group 'org-appearance
  3156. :type 'boolean)
  3157. (defcustom org-highlight-latex-fragments-and-specials nil
  3158. "Non-nil means fontify what is treated specially by the exporters."
  3159. :group 'org-appearance
  3160. :type 'boolean)
  3161. (defcustom org-hide-emphasis-markers nil
  3162. "Non-nil mean font-lock should hide the emphasis marker characters."
  3163. :group 'org-appearance
  3164. :type 'boolean)
  3165. (defcustom org-pretty-entities nil
  3166. "Non-nil means show entities as UTF8 characters.
  3167. When nil, the \\name form remains in the buffer."
  3168. :group 'org-appearance
  3169. :version "24.1"
  3170. :type 'boolean)
  3171. (defcustom org-pretty-entities-include-sub-superscripts t
  3172. "Non-nil means, pretty entity display includes formatting sub/superscripts."
  3173. :group 'org-appearance
  3174. :version "24.1"
  3175. :type 'boolean)
  3176. (defvar org-emph-re nil
  3177. "Regular expression for matching emphasis.
  3178. After a match, the match groups contain these elements:
  3179. 0 The match of the full regular expression, including the characters
  3180. before and after the proper match
  3181. 1 The character before the proper match, or empty at beginning of line
  3182. 2 The proper match, including the leading and trailing markers
  3183. 3 The leading marker like * or /, indicating the type of highlighting
  3184. 4 The text between the emphasis markers, not including the markers
  3185. 5 The character after the match, empty at the end of a line")
  3186. (defvar org-verbatim-re nil
  3187. "Regular expression for matching verbatim text.")
  3188. (defvar org-emphasis-regexp-components) ; defined just below
  3189. (defvar org-emphasis-alist) ; defined just below
  3190. (defun org-set-emph-re (var val)
  3191. "Set variable and compute the emphasis regular expression."
  3192. (set var val)
  3193. (when (and (boundp 'org-emphasis-alist)
  3194. (boundp 'org-emphasis-regexp-components)
  3195. org-emphasis-alist org-emphasis-regexp-components)
  3196. (let* ((e org-emphasis-regexp-components)
  3197. (pre (car e))
  3198. (post (nth 1 e))
  3199. (border (nth 2 e))
  3200. (body (nth 3 e))
  3201. (nl (nth 4 e))
  3202. (body1 (concat body "*?"))
  3203. (markers (mapconcat 'car org-emphasis-alist ""))
  3204. (vmarkers (mapconcat
  3205. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  3206. org-emphasis-alist "")))
  3207. ;; make sure special characters appear at the right position in the class
  3208. (if (string-match "\\^" markers)
  3209. (setq markers (concat (replace-match "" t t markers) "^")))
  3210. (if (string-match "-" markers)
  3211. (setq markers (concat (replace-match "" t t markers) "-")))
  3212. (if (string-match "\\^" vmarkers)
  3213. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  3214. (if (string-match "-" vmarkers)
  3215. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  3216. (if (> nl 0)
  3217. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  3218. (int-to-string nl) "\\}")))
  3219. ;; Make the regexp
  3220. (setq org-emph-re
  3221. (concat "\\([" pre "]\\|^\\)"
  3222. "\\("
  3223. "\\([" markers "]\\)"
  3224. "\\("
  3225. "[^" border "]\\|"
  3226. "[^" border "]"
  3227. body1
  3228. "[^" border "]"
  3229. "\\)"
  3230. "\\3\\)"
  3231. "\\([" post "]\\|$\\)"))
  3232. (setq org-verbatim-re
  3233. (concat "\\([" pre "]\\|^\\)"
  3234. "\\("
  3235. "\\([" vmarkers "]\\)"
  3236. "\\("
  3237. "[^" border "]\\|"
  3238. "[^" border "]"
  3239. body1
  3240. "[^" border "]"
  3241. "\\)"
  3242. "\\3\\)"
  3243. "\\([" post "]\\|$\\)")))))
  3244. (defcustom org-emphasis-regexp-components
  3245. '(" \t('\"{" "- \t.,:!?;'\")}\\" " \t\r\n,\"'" "." 1)
  3246. "Components used to build the regular expression for emphasis.
  3247. This is a list with five entries. Terminology: In an emphasis string
  3248. like \" *strong word* \", we call the initial space PREMATCH, the final
  3249. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  3250. and \"trong wor\" is the body. The different components in this variable
  3251. specify what is allowed/forbidden in each part:
  3252. pre Chars allowed as prematch. Beginning of line will be allowed too.
  3253. post Chars allowed as postmatch. End of line will be allowed too.
  3254. border The chars *forbidden* as border characters.
  3255. body-regexp A regexp like \".\" to match a body character. Don't use
  3256. non-shy groups here, and don't allow newline here.
  3257. newline The maximum number of newlines allowed in an emphasis exp.
  3258. Use customize to modify this, or restart Emacs after changing it."
  3259. :group 'org-appearance
  3260. :set 'org-set-emph-re
  3261. :type '(list
  3262. (sexp :tag "Allowed chars in pre ")
  3263. (sexp :tag "Allowed chars in post ")
  3264. (sexp :tag "Forbidden chars in border ")
  3265. (sexp :tag "Regexp for body ")
  3266. (integer :tag "number of newlines allowed")
  3267. (option (boolean :tag "Please ignore this button"))))
  3268. (defcustom org-emphasis-alist
  3269. `(("*" bold "<b>" "</b>")
  3270. ("/" italic "<i>" "</i>")
  3271. ("_" underline "<span style=\"text-decoration:underline;\">" "</span>")
  3272. ("=" org-code "<code>" "</code>" verbatim)
  3273. ("~" org-verbatim "<code>" "</code>" verbatim)
  3274. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  3275. "<del>" "</del>")
  3276. )
  3277. "Special syntax for emphasized text.
  3278. Text starting and ending with a special character will be emphasized, for
  3279. example *bold*, _underlined_ and /italic/. This variable sets the marker
  3280. characters, the face to be used by font-lock for highlighting in Org-mode
  3281. Emacs buffers, and the HTML tags to be used for this.
  3282. For LaTeX export, see the variable `org-export-latex-emphasis-alist'.
  3283. For DocBook export, see the variable `org-export-docbook-emphasis-alist'.
  3284. Use customize to modify this, or restart Emacs after changing it."
  3285. :group 'org-appearance
  3286. :set 'org-set-emph-re
  3287. :type '(repeat
  3288. (list
  3289. (string :tag "Marker character")
  3290. (choice
  3291. (face :tag "Font-lock-face")
  3292. (plist :tag "Face property list"))
  3293. (string :tag "HTML start tag")
  3294. (string :tag "HTML end tag")
  3295. (option (const verbatim)))))
  3296. (defvar org-protecting-blocks
  3297. '("src" "example" "latex" "ascii" "html" "docbook" "ditaa" "dot" "r" "R")
  3298. "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
  3299. This is needed for font-lock setup.")
  3300. ;;; Miscellaneous options
  3301. (defgroup org-completion nil
  3302. "Completion in Org-mode."
  3303. :tag "Org Completion"
  3304. :group 'org)
  3305. (defcustom org-completion-use-ido nil
  3306. "Non-nil means use ido completion wherever possible.
  3307. Note that `ido-mode' must be active for this variable to be relevant.
  3308. If you decide to turn this variable on, you might well want to turn off
  3309. `org-outline-path-complete-in-steps'.
  3310. See also `org-completion-use-iswitchb'."
  3311. :group 'org-completion
  3312. :type 'boolean)
  3313. (defcustom org-completion-use-iswitchb nil
  3314. "Non-nil means use iswitchb completion wherever possible.
  3315. Note that `iswitchb-mode' must be active for this variable to be relevant.
  3316. If you decide to turn this variable on, you might well want to turn off
  3317. `org-outline-path-complete-in-steps'.
  3318. Note that this variable has only an effect if `org-completion-use-ido' is nil."
  3319. :group 'org-completion
  3320. :type 'boolean)
  3321. (defcustom org-completion-fallback-command 'hippie-expand
  3322. "The expansion command called by \\[pcomplete] in normal context.
  3323. Normal means, no org-mode-specific context."
  3324. :group 'org-completion
  3325. :type 'function)
  3326. ;;; Functions and variables from their packages
  3327. ;; Declared here to avoid compiler warnings
  3328. ;; XEmacs only
  3329. (defvar outline-mode-menu-heading)
  3330. (defvar outline-mode-menu-show)
  3331. (defvar outline-mode-menu-hide)
  3332. (defvar zmacs-regions) ; XEmacs regions
  3333. ;; Emacs only
  3334. (defvar mark-active)
  3335. ;; Various packages
  3336. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  3337. (declare-function calendar-forward-day "cal-move" (arg))
  3338. (declare-function calendar-goto-date "cal-move" (date))
  3339. (declare-function calendar-goto-today "cal-move" ())
  3340. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  3341. (defvar calc-embedded-close-formula)
  3342. (defvar calc-embedded-open-formula)
  3343. (declare-function cdlatex-tab "ext:cdlatex" ())
  3344. (declare-function cdlatex-compute-tables "ext:cdlatex" ())
  3345. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  3346. (defvar font-lock-unfontify-region-function)
  3347. (declare-function iswitchb-read-buffer "iswitchb"
  3348. (prompt &optional default require-match start matches-set))
  3349. (defvar iswitchb-temp-buflist)
  3350. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  3351. (defvar org-agenda-tags-todo-honor-ignore-options)
  3352. (declare-function org-agenda-skip "org-agenda" ())
  3353. (declare-function
  3354. org-agenda-format-item "org-agenda"
  3355. (extra txt &optional level category tags dotime noprefix remove-re habitp))
  3356. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  3357. (declare-function org-agenda-change-all-lines "org-agenda"
  3358. (newhead hdmarker &optional fixface just-this))
  3359. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  3360. (declare-function org-agenda-maybe-redo "org-agenda" ())
  3361. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  3362. (beg end))
  3363. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  3364. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  3365. "org-agenda" (&optional end))
  3366. (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
  3367. (declare-function org-inlinetask-in-task-p "org-inlinetask" ())
  3368. (declare-function org-inlinetask-goto-beginning "org-inlinetask" ())
  3369. (declare-function org-inlinetask-goto-end "org-inlinetask" ())
  3370. (declare-function org-indent-mode "org-indent" (&optional arg))
  3371. (declare-function parse-time-string "parse-time" (string))
  3372. (declare-function org-attach-reveal "org-attach" (&optional if-exists))
  3373. (declare-function org-export-latex-fix-inputenc "org-latex" ())
  3374. (declare-function orgtbl-send-table "org-table" (&optional maybe))
  3375. (defvar remember-data-file)
  3376. (defvar texmathp-why)
  3377. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  3378. (declare-function table--at-cell-p "table" (position &optional object at-column))
  3379. (defvar w3m-current-url)
  3380. (defvar w3m-current-title)
  3381. (defvar org-latex-regexps)
  3382. ;;; Autoload and prepare some org modules
  3383. ;; Some table stuff that needs to be defined here, because it is used
  3384. ;; by the functions setting up org-mode or checking for table context.
  3385. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  3386. "Detect an org-type or table-type table.")
  3387. (defconst org-table-line-regexp "^[ \t]*|"
  3388. "Detect an org-type table line.")
  3389. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  3390. "Detect an org-type table line.")
  3391. (defconst org-table-hline-regexp "^[ \t]*|-"
  3392. "Detect an org-type table hline.")
  3393. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  3394. "Detect a table-type table hline.")
  3395. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  3396. "Detect the first line outside a table when searching from within it.
  3397. This works for both table types.")
  3398. ;; Autoload the functions in org-table.el that are needed by functions here.
  3399. (eval-and-compile
  3400. (org-autoload "org-table"
  3401. '(org-table-begin org-table-blank-field org-table-end)))
  3402. ;;;###autoload
  3403. (defun turn-on-orgtbl ()
  3404. "Unconditionally turn on `orgtbl-mode'."
  3405. (require 'org-table)
  3406. (orgtbl-mode 1))
  3407. (defun org-at-table-p (&optional table-type)
  3408. "Return t if the cursor is inside an org-type table.
  3409. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  3410. (if org-enable-table-editor
  3411. (save-excursion
  3412. (beginning-of-line 1)
  3413. (looking-at (if table-type org-table-any-line-regexp
  3414. org-table-line-regexp)))
  3415. nil))
  3416. (defsubst org-table-p () (org-at-table-p))
  3417. (defun org-at-table.el-p ()
  3418. "Return t if and only if we are at a table.el table."
  3419. (and (org-at-table-p 'any)
  3420. (save-excursion
  3421. (goto-char (org-table-begin 'any))
  3422. (looking-at org-table1-hline-regexp))))
  3423. (defun org-table-recognize-table.el ()
  3424. "If there is a table.el table nearby, recognize it and move into it."
  3425. (if org-table-tab-recognizes-table.el
  3426. (if (org-at-table.el-p)
  3427. (progn
  3428. (beginning-of-line 1)
  3429. (if (looking-at org-table-dataline-regexp)
  3430. nil
  3431. (if (looking-at org-table1-hline-regexp)
  3432. (progn
  3433. (beginning-of-line 2)
  3434. (if (looking-at org-table-any-border-regexp)
  3435. (beginning-of-line -1)))))
  3436. (if (re-search-forward "|" (org-table-end t) t)
  3437. (progn
  3438. (require 'table)
  3439. (if (table--at-cell-p (point))
  3440. t
  3441. (message "recognizing table.el table...")
  3442. (table-recognize-table)
  3443. (message "recognizing table.el table...done")))
  3444. (error "This should not happen"))
  3445. t)
  3446. nil)
  3447. nil))
  3448. (defun org-at-table-hline-p ()
  3449. "Return t if the cursor is inside a hline in a table."
  3450. (if org-enable-table-editor
  3451. (save-excursion
  3452. (beginning-of-line 1)
  3453. (looking-at org-table-hline-regexp))
  3454. nil))
  3455. (defvar org-table-clean-did-remove-column nil)
  3456. (defun org-table-map-tables (function &optional quietly)
  3457. "Apply FUNCTION to the start of all tables in the buffer."
  3458. (save-excursion
  3459. (save-restriction
  3460. (widen)
  3461. (goto-char (point-min))
  3462. (while (re-search-forward org-table-any-line-regexp nil t)
  3463. (unless quietly
  3464. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size))))
  3465. (beginning-of-line 1)
  3466. (when (and (looking-at org-table-line-regexp)
  3467. ;; Exclude tables in src/example/verbatim/clocktable blocks
  3468. (not (org-in-block-p '("src" "example"))))
  3469. (save-excursion (funcall function))
  3470. (or (looking-at org-table-line-regexp)
  3471. (forward-char 1)))
  3472. (re-search-forward org-table-any-border-regexp nil 1))))
  3473. (unless quietly (message "Mapping tables: done")))
  3474. ;; Declare and autoload functions from org-exp.el & Co
  3475. (declare-function org-default-export-plist "org-exp")
  3476. (declare-function org-infile-export-plist "org-exp")
  3477. (declare-function org-get-current-options "org-exp")
  3478. ;; Declare and autoload functions from org-agenda.el
  3479. (eval-and-compile
  3480. (org-autoload "org-agenda"
  3481. '(org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
  3482. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock" (beg end))
  3483. (declare-function org-clock-update-mode-line "org-clock" ())
  3484. (declare-function org-resolve-clocks "org-clock"
  3485. (&optional also-non-dangling-p prompt last-valid))
  3486. (defvar org-clock-start-time)
  3487. (defvar org-clock-marker (make-marker)
  3488. "Marker recording the last clock-in.")
  3489. (defvar org-clock-hd-marker (make-marker)
  3490. "Marker recording the last clock-in, but the headline position.")
  3491. (defvar org-clock-heading ""
  3492. "The heading of the current clock entry.")
  3493. (defun org-clock-is-active ()
  3494. "Return non-nil if clock is currently running.
  3495. The return value is actually the clock marker."
  3496. (marker-buffer org-clock-marker))
  3497. (eval-and-compile
  3498. (org-autoload "org-clock" '(org-clock-remove-overlays
  3499. org-clock-update-time-maybe
  3500. org-clocktable-shift)))
  3501. (defun org-check-running-clock ()
  3502. "Check if the current buffer contains the running clock.
  3503. If yes, offer to stop it and to save the buffer with the changes."
  3504. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3505. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  3506. (buffer-name))))
  3507. (org-clock-out)
  3508. (when (y-or-n-p "Save changed buffer?")
  3509. (save-buffer))))
  3510. (defun org-clocktable-try-shift (dir n)
  3511. "Check if this line starts a clock table, if yes, shift the time block."
  3512. (when (org-match-line "^[ \t]*#\\+BEGIN:[ \t]+clocktable\\>")
  3513. (org-clocktable-shift dir n)))
  3514. ;;;###autoload
  3515. (defun org-clock-persistence-insinuate ()
  3516. "Set up hooks for clock persistence."
  3517. (require 'org-clock)
  3518. (add-hook 'org-mode-hook 'org-clock-load)
  3519. (add-hook 'kill-emacs-hook 'org-clock-save))
  3520. ;; Define the variable already here, to make sure we have it.
  3521. (defvar org-indent-mode nil
  3522. "Non-nil if Org-Indent mode is enabled.
  3523. Use the command `org-indent-mode' to change this variable.")
  3524. ;; Autoload archiving code
  3525. ;; The stuff that is needed for cycling and tags has to be defined here.
  3526. (defgroup org-archive nil
  3527. "Options concerning archiving in Org-mode."
  3528. :tag "Org Archive"
  3529. :group 'org-structure)
  3530. (defcustom org-archive-location "%s_archive::"
  3531. "The location where subtrees should be archived.
  3532. The value of this variable is a string, consisting of two parts,
  3533. separated by a double-colon. The first part is a filename and
  3534. the second part is a headline.
  3535. When the filename is omitted, archiving happens in the same file.
  3536. %s in the filename will be replaced by the current file
  3537. name (without the directory part). Archiving to a different file
  3538. is useful to keep archived entries from contributing to the
  3539. Org-mode Agenda.
  3540. The archived entries will be filed as subtrees of the specified
  3541. headline. When the headline is omitted, the subtrees are simply
  3542. filed away at the end of the file, as top-level entries. Also in
  3543. the heading you can use %s to represent the file name, this can be
  3544. useful when using the same archive for a number of different files.
  3545. Here are a few examples:
  3546. \"%s_archive::\"
  3547. If the current file is Projects.org, archive in file
  3548. Projects.org_archive, as top-level trees. This is the default.
  3549. \"::* Archived Tasks\"
  3550. Archive in the current file, under the top-level headline
  3551. \"* Archived Tasks\".
  3552. \"~/org/archive.org::\"
  3553. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  3554. \"~/org/archive.org::* From %s\"
  3555. Archive in file ~/org/archive.org (absolute path), under headlines
  3556. \"From FILENAME\" where file name is the current file name.
  3557. \"~/org/datetree.org::datetree/* Finished Tasks\"
  3558. The \"datetree/\" string is special, signifying to archive
  3559. items to the datetree. Items are placed in either the CLOSED
  3560. date of the item, or the current date if there is no CLOSED date.
  3561. The heading will be a subentry to the current date. There doesn't
  3562. need to be a heading, but there always needs to be a slash after
  3563. datetree. For example, to store archived items directly in the
  3564. datetree, use \"~/org/datetree.org::datetree/\".
  3565. \"basement::** Finished Tasks\"
  3566. Archive in file ./basement (relative path), as level 3 trees
  3567. below the level 2 heading \"** Finished Tasks\".
  3568. You may set this option on a per-file basis by adding to the buffer a
  3569. line like
  3570. #+ARCHIVE: basement::** Finished Tasks
  3571. You may also define it locally for a subtree by setting an ARCHIVE property
  3572. in the entry. If such a property is found in an entry, or anywhere up
  3573. the hierarchy, it will be used."
  3574. :group 'org-archive
  3575. :type 'string)
  3576. (defcustom org-archive-tag "ARCHIVE"
  3577. "The tag that marks a subtree as archived.
  3578. An archived subtree does not open during visibility cycling, and does
  3579. not contribute to the agenda listings.
  3580. After changing this, font-lock must be restarted in the relevant buffers to
  3581. get the proper fontification."
  3582. :group 'org-archive
  3583. :group 'org-keywords
  3584. :type 'string)
  3585. (defcustom org-agenda-skip-archived-trees t
  3586. "Non-nil means the agenda will skip any items located in archived trees.
  3587. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  3588. variable is no longer recommended, you should leave it at the value t.
  3589. Instead, use the key `v' to cycle the archives-mode in the agenda."
  3590. :group 'org-archive
  3591. :group 'org-agenda-skip
  3592. :type 'boolean)
  3593. (defcustom org-columns-skip-archived-trees t
  3594. "Non-nil means ignore archived trees when creating column view."
  3595. :group 'org-archive
  3596. :group 'org-properties
  3597. :type 'boolean)
  3598. (defcustom org-cycle-open-archived-trees nil
  3599. "Non-nil means `org-cycle' will open archived trees.
  3600. An archived tree is a tree marked with the tag ARCHIVE.
  3601. When nil, archived trees will stay folded. You can still open them with
  3602. normal outline commands like `show-all', but not with the cycling commands."
  3603. :group 'org-archive
  3604. :group 'org-cycle
  3605. :type 'boolean)
  3606. (defcustom org-sparse-tree-open-archived-trees nil
  3607. "Non-nil means sparse tree construction shows matches in archived trees.
  3608. When nil, matches in these trees are highlighted, but the trees are kept in
  3609. collapsed state."
  3610. :group 'org-archive
  3611. :group 'org-sparse-trees
  3612. :type 'boolean)
  3613. (defcustom org-sparse-tree-default-date-type 'scheduled-or-deadline
  3614. "The default date type when building a sparse tree.
  3615. When this is nil, a date is a scheduled or a deadline timestamp.
  3616. Otherwise, these types are allowed:
  3617. all: all timestamps
  3618. active: only active timestamps (<...>)
  3619. inactive: only inactive timestamps (<...)
  3620. scheduled: only scheduled timestamps
  3621. deadline: only deadline timestamps"
  3622. :type '(choice (const :tag "Scheduled or deadline" 'scheduled-or-deadline)
  3623. (const :tag "All timestamps" all)
  3624. (const :tag "Only active timestamps" active)
  3625. (const :tag "Only inactive timestamps" inactive)
  3626. (const :tag "Only scheduled timestamps" scheduled)
  3627. (const :tag "Only deadline timestamps" deadline))
  3628. :version "24.3"
  3629. :group 'org-sparse-trees)
  3630. (defun org-cycle-hide-archived-subtrees (state)
  3631. "Re-hide all archived subtrees after a visibility state change."
  3632. (when (and (not org-cycle-open-archived-trees)
  3633. (not (memq state '(overview folded))))
  3634. (save-excursion
  3635. (let* ((globalp (memq state '(contents all)))
  3636. (beg (if globalp (point-min) (point)))
  3637. (end (if globalp (point-max) (org-end-of-subtree t))))
  3638. (org-hide-archived-subtrees beg end)
  3639. (goto-char beg)
  3640. (if (looking-at (concat ".*:" org-archive-tag ":"))
  3641. (message "%s" (substitute-command-keys
  3642. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  3643. (defun org-force-cycle-archived ()
  3644. "Cycle subtree even if it is archived."
  3645. (interactive)
  3646. (setq this-command 'org-cycle)
  3647. (let ((org-cycle-open-archived-trees t))
  3648. (call-interactively 'org-cycle)))
  3649. (defun org-hide-archived-subtrees (beg end)
  3650. "Re-hide all archived subtrees after a visibility state change."
  3651. (save-excursion
  3652. (let* ((re (concat ":" org-archive-tag ":")))
  3653. (goto-char beg)
  3654. (while (re-search-forward re end t)
  3655. (when (org-at-heading-p)
  3656. (org-flag-subtree t)
  3657. (org-end-of-subtree t))))))
  3658. (declare-function outline-end-of-heading "outline" ())
  3659. (declare-function outline-flag-region "outline" (from to flag))
  3660. (defun org-flag-subtree (flag)
  3661. (save-excursion
  3662. (org-back-to-heading t)
  3663. (outline-end-of-heading)
  3664. (outline-flag-region (point)
  3665. (progn (org-end-of-subtree t) (point))
  3666. flag)))
  3667. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  3668. (eval-and-compile
  3669. (org-autoload "org-archive"
  3670. '(org-add-archive-files)))
  3671. ;; Autoload Column View Code
  3672. (declare-function org-columns-number-to-string "org-colview" (n fmt &optional printf))
  3673. (declare-function org-columns-get-format-and-top-level "org-colview" ())
  3674. (declare-function org-columns-compute "org-colview" (property))
  3675. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  3676. '(org-columns-number-to-string
  3677. org-columns-get-format-and-top-level
  3678. org-columns-compute
  3679. org-columns-remove-overlays))
  3680. ;; Autoload ID code
  3681. (declare-function org-id-store-link "org-id")
  3682. (declare-function org-id-locations-load "org-id")
  3683. (declare-function org-id-locations-save "org-id")
  3684. (defvar org-id-track-globally)
  3685. (org-autoload "org-id"
  3686. '(org-id-new
  3687. org-id-copy
  3688. org-id-get-with-outline-path-completion
  3689. org-id-get-with-outline-drilling))
  3690. ;;; Variables for pre-computed regular expressions, all buffer local
  3691. (defvar org-drawer-regexp "^[ \t]*:PROPERTIES:[ \t]*$"
  3692. "Matches first line of a hidden block.")
  3693. (make-variable-buffer-local 'org-drawer-regexp)
  3694. (defvar org-todo-regexp nil
  3695. "Matches any of the TODO state keywords.")
  3696. (make-variable-buffer-local 'org-todo-regexp)
  3697. (defvar org-not-done-regexp nil
  3698. "Matches any of the TODO state keywords except the last one.")
  3699. (make-variable-buffer-local 'org-not-done-regexp)
  3700. (defvar org-not-done-heading-regexp nil
  3701. "Matches a TODO headline that is not done.")
  3702. (make-variable-buffer-local 'org-not-done-regexp)
  3703. (defvar org-todo-line-regexp nil
  3704. "Matches a headline and puts TODO state into group 2 if present.")
  3705. (make-variable-buffer-local 'org-todo-line-regexp)
  3706. (defvar org-complex-heading-regexp nil
  3707. "Matches a headline and puts everything into groups:
  3708. group 1: the stars
  3709. group 2: The todo keyword, maybe
  3710. group 3: Priority cookie
  3711. group 4: True headline
  3712. group 5: Tags")
  3713. (make-variable-buffer-local 'org-complex-heading-regexp)
  3714. (defvar org-complex-heading-regexp-format nil
  3715. "Printf format to make regexp to match an exact headline.
  3716. This regexp will match the headline of any node which has the
  3717. exact headline text that is put into the format, but may have any
  3718. TODO state, priority and tags.")
  3719. (make-variable-buffer-local 'org-complex-heading-regexp-format)
  3720. (defvar org-todo-line-tags-regexp nil
  3721. "Matches a headline and puts TODO state into group 2 if present.
  3722. Also put tags into group 4 if tags are present.")
  3723. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  3724. (defvar org-ds-keyword-length 12
  3725. "Maximum length of the DEADLINE and SCHEDULED keywords.")
  3726. (make-variable-buffer-local 'org-ds-keyword-length)
  3727. (defvar org-deadline-regexp nil
  3728. "Matches the DEADLINE keyword.")
  3729. (make-variable-buffer-local 'org-deadline-regexp)
  3730. (defvar org-deadline-time-regexp nil
  3731. "Matches the DEADLINE keyword together with a time stamp.")
  3732. (make-variable-buffer-local 'org-deadline-time-regexp)
  3733. (defvar org-deadline-line-regexp nil
  3734. "Matches the DEADLINE keyword and the rest of the line.")
  3735. (make-variable-buffer-local 'org-deadline-line-regexp)
  3736. (defvar org-scheduled-regexp nil
  3737. "Matches the SCHEDULED keyword.")
  3738. (make-variable-buffer-local 'org-scheduled-regexp)
  3739. (defvar org-scheduled-time-regexp nil
  3740. "Matches the SCHEDULED keyword together with a time stamp.")
  3741. (make-variable-buffer-local 'org-scheduled-time-regexp)
  3742. (defvar org-closed-time-regexp nil
  3743. "Matches the CLOSED keyword together with a time stamp.")
  3744. (make-variable-buffer-local 'org-closed-time-regexp)
  3745. (defvar org-keyword-time-regexp nil
  3746. "Matches any of the 4 keywords, together with the time stamp.")
  3747. (make-variable-buffer-local 'org-keyword-time-regexp)
  3748. (defvar org-keyword-time-not-clock-regexp nil
  3749. "Matches any of the 3 keywords, together with the time stamp.")
  3750. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  3751. (defvar org-maybe-keyword-time-regexp nil
  3752. "Matches a timestamp, possibly preceded by a keyword.")
  3753. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  3754. (defvar org-all-time-keywords nil
  3755. "List of time keywords.")
  3756. (make-variable-buffer-local 'org-all-time-keywords)
  3757. (defconst org-plain-time-of-day-regexp
  3758. (concat
  3759. "\\(\\<[012]?[0-9]"
  3760. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3761. "\\(--?"
  3762. "\\(\\<[012]?[0-9]"
  3763. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3764. "\\)?")
  3765. "Regular expression to match a plain time or time range.
  3766. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  3767. groups carry important information:
  3768. 0 the full match
  3769. 1 the first time, range or not
  3770. 8 the second time, if it is a range.")
  3771. (defconst org-plain-time-extension-regexp
  3772. (concat
  3773. "\\(\\<[012]?[0-9]"
  3774. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3775. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  3776. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  3777. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  3778. groups carry important information:
  3779. 0 the full match
  3780. 7 hours of duration
  3781. 9 minutes of duration")
  3782. (defconst org-stamp-time-of-day-regexp
  3783. (concat
  3784. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  3785. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  3786. "\\(--?"
  3787. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  3788. "Regular expression to match a timestamp time or time range.
  3789. After a match, the following groups carry important information:
  3790. 0 the full match
  3791. 1 date plus weekday, for back referencing to make sure both times are on the same day
  3792. 2 the first time, range or not
  3793. 4 the second time, if it is a range.")
  3794. (defconst org-startup-options
  3795. '(("fold" org-startup-folded t)
  3796. ("overview" org-startup-folded t)
  3797. ("nofold" org-startup-folded nil)
  3798. ("showall" org-startup-folded nil)
  3799. ("showeverything" org-startup-folded showeverything)
  3800. ("content" org-startup-folded content)
  3801. ("indent" org-startup-indented t)
  3802. ("noindent" org-startup-indented nil)
  3803. ("hidestars" org-hide-leading-stars t)
  3804. ("showstars" org-hide-leading-stars nil)
  3805. ("odd" org-odd-levels-only t)
  3806. ("oddeven" org-odd-levels-only nil)
  3807. ("align" org-startup-align-all-tables t)
  3808. ("noalign" org-startup-align-all-tables nil)
  3809. ("inlineimages" org-startup-with-inline-images t)
  3810. ("noinlineimages" org-startup-with-inline-images nil)
  3811. ("customtime" org-display-custom-times t)
  3812. ("logdone" org-log-done time)
  3813. ("lognotedone" org-log-done note)
  3814. ("nologdone" org-log-done nil)
  3815. ("lognoteclock-out" org-log-note-clock-out t)
  3816. ("nolognoteclock-out" org-log-note-clock-out nil)
  3817. ("logrepeat" org-log-repeat state)
  3818. ("lognoterepeat" org-log-repeat note)
  3819. ("nologrepeat" org-log-repeat nil)
  3820. ("logreschedule" org-log-reschedule time)
  3821. ("lognotereschedule" org-log-reschedule note)
  3822. ("nologreschedule" org-log-reschedule nil)
  3823. ("logredeadline" org-log-redeadline time)
  3824. ("lognoteredeadline" org-log-redeadline note)
  3825. ("nologredeadline" org-log-redeadline nil)
  3826. ("logrefile" org-log-refile time)
  3827. ("lognoterefile" org-log-refile note)
  3828. ("nologrefile" org-log-refile nil)
  3829. ("fninline" org-footnote-define-inline t)
  3830. ("nofninline" org-footnote-define-inline nil)
  3831. ("fnlocal" org-footnote-section nil)
  3832. ("fnauto" org-footnote-auto-label t)
  3833. ("fnprompt" org-footnote-auto-label nil)
  3834. ("fnconfirm" org-footnote-auto-label confirm)
  3835. ("fnplain" org-footnote-auto-label plain)
  3836. ("fnadjust" org-footnote-auto-adjust t)
  3837. ("nofnadjust" org-footnote-auto-adjust nil)
  3838. ("constcgs" constants-unit-system cgs)
  3839. ("constSI" constants-unit-system SI)
  3840. ("noptag" org-tag-persistent-alist nil)
  3841. ("hideblocks" org-hide-block-startup t)
  3842. ("nohideblocks" org-hide-block-startup nil)
  3843. ("beamer" org-startup-with-beamer-mode t)
  3844. ("entitiespretty" org-pretty-entities t)
  3845. ("entitiesplain" org-pretty-entities nil))
  3846. "Variable associated with STARTUP options for org-mode.
  3847. Each element is a list of three items: the startup options (as written
  3848. in the #+STARTUP line), the corresponding variable, and the value to set
  3849. this variable to if the option is found. An optional forth element PUSH
  3850. means to push this value onto the list in the variable.")
  3851. (defun org-update-property-plist (key val props)
  3852. "Update PROPS with KEY and VAL."
  3853. (let* ((appending (string= "+" (substring key (- (length key) 1))))
  3854. (key (if appending (substring key 0 (- (length key) 1)) key))
  3855. (remainder (org-remove-if (lambda (p) (string= (car p) key)) props))
  3856. (previous (cdr (assoc key props))))
  3857. (if appending
  3858. (cons (cons key (if previous (concat previous " " val) val)) remainder)
  3859. (cons (cons key val) remainder))))
  3860. (defconst org-block-regexp
  3861. "^[ \t]*#\\+begin_?\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_?\\1[ \t]*$"
  3862. "Regular expression for hiding blocks.")
  3863. (defconst org-heading-keyword-regexp-format
  3864. "^\\(\\*+\\)\\(?: +%s\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  3865. "Printf format for a regexp matching an headline with some keyword.
  3866. This regexp will match the headline of any node which has the
  3867. exact keyword that is put into the format. The keyword isn't in
  3868. any group by default, but the stars and the body are.")
  3869. (defconst org-heading-keyword-maybe-regexp-format
  3870. "^\\(\\*+\\)\\(?: +%s\\)?\\(?: +\\(.*?\\)\\)?[ \t]*$"
  3871. "Printf format for a regexp matching an headline, possibly with some keyword.
  3872. This regexp can match any headline with the specified keyword, or
  3873. without a keyword. The keyword isn't in any group by default,
  3874. but the stars and the body are.")
  3875. (defun org-set-regexps-and-options ()
  3876. "Precompute regular expressions for current buffer."
  3877. (when (derived-mode-p 'org-mode)
  3878. (org-set-local 'org-todo-kwd-alist nil)
  3879. (org-set-local 'org-todo-key-alist nil)
  3880. (org-set-local 'org-todo-key-trigger nil)
  3881. (org-set-local 'org-todo-keywords-1 nil)
  3882. (org-set-local 'org-done-keywords nil)
  3883. (org-set-local 'org-todo-heads nil)
  3884. (org-set-local 'org-todo-sets nil)
  3885. (org-set-local 'org-todo-log-states nil)
  3886. (org-set-local 'org-file-properties nil)
  3887. (org-set-local 'org-file-tags nil)
  3888. (let ((re (org-make-options-regexp
  3889. '("CATEGORY" "TODO" "COLUMNS"
  3890. "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
  3891. "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE" "LATEX_CLASS"
  3892. "OPTIONS")
  3893. "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
  3894. (splitre "[ \t]+")
  3895. (scripts org-use-sub-superscripts)
  3896. kwds kws0 kwsa key log value cat arch tags const links hw dws
  3897. tail sep kws1 prio props ftags drawers beamer-p
  3898. ext-setup-or-nil setup-contents (start 0))
  3899. (save-excursion
  3900. (save-restriction
  3901. (widen)
  3902. (goto-char (point-min))
  3903. (while (or (and ext-setup-or-nil
  3904. (string-match re ext-setup-or-nil start)
  3905. (setq start (match-end 0)))
  3906. (and (setq ext-setup-or-nil nil start 0)
  3907. (re-search-forward re nil t)))
  3908. (setq key (upcase (match-string 1 ext-setup-or-nil))
  3909. value (org-match-string-no-properties 2 ext-setup-or-nil))
  3910. (if (stringp value) (setq value (org-trim value)))
  3911. (cond
  3912. ((equal key "CATEGORY")
  3913. (setq cat value))
  3914. ((member key '("SEQ_TODO" "TODO"))
  3915. (push (cons 'sequence (org-split-string value splitre)) kwds))
  3916. ((equal key "TYP_TODO")
  3917. (push (cons 'type (org-split-string value splitre)) kwds))
  3918. ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key)
  3919. ;; general TODO-like setup
  3920. (push (cons (intern (downcase (match-string 1 key)))
  3921. (org-split-string value splitre)) kwds))
  3922. ((equal key "TAGS")
  3923. (setq tags (append tags (if tags '("\\n") nil)
  3924. (org-split-string value splitre))))
  3925. ((equal key "COLUMNS")
  3926. (org-set-local 'org-columns-default-format value))
  3927. ((equal key "LINK")
  3928. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  3929. (push (cons (match-string 1 value)
  3930. (org-trim (match-string 2 value)))
  3931. links)))
  3932. ((equal key "PRIORITIES")
  3933. (setq prio (org-split-string value " +")))
  3934. ((equal key "PROPERTY")
  3935. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  3936. (setq props (org-update-property-plist (match-string 1 value)
  3937. (match-string 2 value)
  3938. props))))
  3939. ((equal key "FILETAGS")
  3940. (when (string-match "\\S-" value)
  3941. (setq ftags
  3942. (append
  3943. ftags
  3944. (apply 'append
  3945. (mapcar (lambda (x) (org-split-string x ":"))
  3946. (org-split-string value)))))))
  3947. ((equal key "DRAWERS")
  3948. (setq drawers (delete-dups (append org-drawers (org-split-string value splitre)))))
  3949. ((equal key "CONSTANTS")
  3950. (setq const (append const (org-split-string value splitre))))
  3951. ((equal key "STARTUP")
  3952. (let ((opts (org-split-string value splitre))
  3953. l var val)
  3954. (while (setq l (pop opts))
  3955. (when (setq l (assoc l org-startup-options))
  3956. (setq var (nth 1 l) val (nth 2 l))
  3957. (if (not (nth 3 l))
  3958. (set (make-local-variable var) val)
  3959. (if (not (listp (symbol-value var)))
  3960. (set (make-local-variable var) nil))
  3961. (set (make-local-variable var) (symbol-value var))
  3962. (add-to-list var val))))))
  3963. ((equal key "ARCHIVE")
  3964. (setq arch value)
  3965. (remove-text-properties 0 (length arch)
  3966. '(face t fontified t) arch))
  3967. ((equal key "LATEX_CLASS")
  3968. (setq beamer-p (equal value "beamer")))
  3969. ((equal key "OPTIONS")
  3970. (if (string-match "\\([ \t]\\|\\`\\)\\^:\\(t\\|nil\\|{}\\)" value)
  3971. (setq scripts (read (match-string 2 value)))))
  3972. ((equal key "SETUPFILE")
  3973. (setq setup-contents (org-file-contents
  3974. (expand-file-name
  3975. (org-remove-double-quotes value))
  3976. 'noerror))
  3977. (if (not ext-setup-or-nil)
  3978. (setq ext-setup-or-nil setup-contents start 0)
  3979. (setq ext-setup-or-nil
  3980. (concat (substring ext-setup-or-nil 0 start)
  3981. "\n" setup-contents "\n"
  3982. (substring ext-setup-or-nil start)))))))
  3983. ;; search for property blocks
  3984. (goto-char (point-min))
  3985. (while (re-search-forward org-block-regexp nil t)
  3986. (when (equal "PROPERTY" (upcase (match-string 1)))
  3987. (setq value (replace-regexp-in-string
  3988. "[\n\r]" " " (match-string 4)))
  3989. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  3990. (setq props (org-update-property-plist (match-string 1 value)
  3991. (match-string 2 value)
  3992. props)))))))
  3993. (org-set-local 'org-use-sub-superscripts scripts)
  3994. (when cat
  3995. (org-set-local 'org-category (intern cat))
  3996. (push (cons "CATEGORY" cat) props))
  3997. (when prio
  3998. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  3999. (setq prio (mapcar 'string-to-char prio))
  4000. (org-set-local 'org-highest-priority (nth 0 prio))
  4001. (org-set-local 'org-lowest-priority (nth 1 prio))
  4002. (org-set-local 'org-default-priority (nth 2 prio)))
  4003. (and props (org-set-local 'org-file-properties (nreverse props)))
  4004. (and ftags (org-set-local 'org-file-tags
  4005. (mapcar 'org-add-prop-inherited ftags)))
  4006. (and drawers (org-set-local 'org-drawers drawers))
  4007. (and arch (org-set-local 'org-archive-location arch))
  4008. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  4009. ;; Process the TODO keywords
  4010. (unless kwds
  4011. ;; Use the global values as if they had been given locally.
  4012. (setq kwds (default-value 'org-todo-keywords))
  4013. (if (stringp (car kwds))
  4014. (setq kwds (list (cons org-todo-interpretation
  4015. (default-value 'org-todo-keywords)))))
  4016. (setq kwds (reverse kwds)))
  4017. (setq kwds (nreverse kwds))
  4018. (let (inter kws kw)
  4019. (while (setq kws (pop kwds))
  4020. (let ((kws (or
  4021. (run-hook-with-args-until-success
  4022. 'org-todo-setup-filter-hook kws)
  4023. kws)))
  4024. (setq inter (pop kws) sep (member "|" kws)
  4025. kws0 (delete "|" (copy-sequence kws))
  4026. kwsa nil
  4027. kws1 (mapcar
  4028. (lambda (x)
  4029. ;; 1 2
  4030. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  4031. (progn
  4032. (setq kw (match-string 1 x)
  4033. key (and (match-end 2) (match-string 2 x))
  4034. log (org-extract-log-state-settings x))
  4035. (push (cons kw (and key (string-to-char key))) kwsa)
  4036. (and log (push log org-todo-log-states))
  4037. kw)
  4038. (error "Invalid TODO keyword %s" x)))
  4039. kws0)
  4040. kwsa (if kwsa (append '((:startgroup))
  4041. (nreverse kwsa)
  4042. '((:endgroup))))
  4043. hw (car kws1)
  4044. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  4045. tail (list inter hw (car dws) (org-last dws))))
  4046. (add-to-list 'org-todo-heads hw 'append)
  4047. (push kws1 org-todo-sets)
  4048. (setq org-done-keywords (append org-done-keywords dws nil))
  4049. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  4050. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  4051. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  4052. (setq org-todo-sets (nreverse org-todo-sets)
  4053. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  4054. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  4055. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  4056. ;; Process the constants
  4057. (when const
  4058. (let (e cst)
  4059. (while (setq e (pop const))
  4060. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  4061. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  4062. (setq org-table-formula-constants-local cst)))
  4063. ;; Process the tags.
  4064. (when tags
  4065. (let (e tgs)
  4066. (while (setq e (pop tags))
  4067. (cond
  4068. ((equal e "{") (push '(:startgroup) tgs))
  4069. ((equal e "}") (push '(:endgroup) tgs))
  4070. ((equal e "\\n") (push '(:newline) tgs))
  4071. ((string-match (org-re "^\\([[:alnum:]_@#%]+\\)(\\(.\\))$") e)
  4072. (push (cons (match-string 1 e)
  4073. (string-to-char (match-string 2 e)))
  4074. tgs))
  4075. (t (push (list e) tgs))))
  4076. (org-set-local 'org-tag-alist nil)
  4077. (while (setq e (pop tgs))
  4078. (or (and (stringp (car e))
  4079. (assoc (car e) org-tag-alist))
  4080. (push e org-tag-alist)))))
  4081. ;; Compute the regular expressions and other local variables.
  4082. ;; Using `org-outline-regexp-bol' would complicate them much,
  4083. ;; because of the fixed white space at the end of that string.
  4084. (if (not org-done-keywords)
  4085. (setq org-done-keywords (and org-todo-keywords-1
  4086. (list (org-last org-todo-keywords-1)))))
  4087. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  4088. (length org-scheduled-string)
  4089. (length org-clock-string)
  4090. (length org-closed-string)))
  4091. org-drawer-regexp
  4092. (concat "^[ \t]*:\\("
  4093. (mapconcat 'regexp-quote org-drawers "\\|")
  4094. "\\):[ \t]*$")
  4095. org-not-done-keywords
  4096. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  4097. org-todo-regexp
  4098. (concat "\\("
  4099. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  4100. "\\)")
  4101. org-not-done-regexp
  4102. (concat "\\("
  4103. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  4104. "\\)")
  4105. org-not-done-heading-regexp
  4106. (format org-heading-keyword-regexp-format org-not-done-regexp)
  4107. org-todo-line-regexp
  4108. (format org-heading-keyword-maybe-regexp-format org-todo-regexp)
  4109. org-complex-heading-regexp
  4110. (concat "^\\(\\*+\\)"
  4111. "\\(?: +" org-todo-regexp "\\)?"
  4112. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4113. "\\(?: +\\(.*?\\)\\)??"
  4114. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?")
  4115. "[ \t]*$")
  4116. org-complex-heading-regexp-format
  4117. (concat "^\\(\\*+\\)"
  4118. "\\(?: +" org-todo-regexp "\\)?"
  4119. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4120. "\\(?: +"
  4121. ;; Stats cookies can be stuck to body.
  4122. "\\(?:\\[[0-9%%/]+\\] *\\)?"
  4123. "\\(%s\\)"
  4124. "\\(?: *\\[[0-9%%/]+\\]\\)?"
  4125. "\\)"
  4126. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%%:]+:\\)\\)?")
  4127. "[ \t]*$")
  4128. org-todo-line-tags-regexp
  4129. (concat "^\\(\\*+\\)"
  4130. "\\(?: +" org-todo-regexp "\\)?"
  4131. "\\(?: +\\(.*?\\)\\)??"
  4132. (org-re "\\(?:[ \t]+\\(:[[:alnum:]:_@#%]+:\\)\\)?")
  4133. "[ \t]*$")
  4134. org-deadline-regexp (concat "\\<" org-deadline-string)
  4135. org-deadline-time-regexp
  4136. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  4137. org-deadline-line-regexp
  4138. (concat "\\<\\(" org-deadline-string "\\).*")
  4139. org-scheduled-regexp
  4140. (concat "\\<" org-scheduled-string)
  4141. org-scheduled-time-regexp
  4142. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  4143. org-closed-time-regexp
  4144. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  4145. org-keyword-time-regexp
  4146. (concat "\\<\\(" org-scheduled-string
  4147. "\\|" org-deadline-string
  4148. "\\|" org-closed-string
  4149. "\\|" org-clock-string "\\)"
  4150. " *[[<]\\([^]>]+\\)[]>]")
  4151. org-keyword-time-not-clock-regexp
  4152. (concat "\\<\\(" org-scheduled-string
  4153. "\\|" org-deadline-string
  4154. "\\|" org-closed-string
  4155. "\\)"
  4156. " *[[<]\\([^]>]+\\)[]>]")
  4157. org-maybe-keyword-time-regexp
  4158. (concat "\\(\\<\\(" org-scheduled-string
  4159. "\\|" org-deadline-string
  4160. "\\|" org-closed-string
  4161. "\\|" org-clock-string "\\)\\)?"
  4162. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  4163. org-all-time-keywords
  4164. (mapcar (lambda (w) (substring w 0 -1))
  4165. (list org-scheduled-string org-deadline-string
  4166. org-clock-string org-closed-string))
  4167. )
  4168. (org-compute-latex-and-specials-regexp)
  4169. (org-set-font-lock-defaults))))
  4170. (defun org-file-contents (file &optional noerror)
  4171. "Return the contents of FILE, as a string."
  4172. (if (or (not file)
  4173. (not (file-readable-p file)))
  4174. (if noerror
  4175. (progn
  4176. (message "Cannot read file \"%s\"" file)
  4177. (ding) (sit-for 2)
  4178. "")
  4179. (error "Cannot read file \"%s\"" file))
  4180. (with-temp-buffer
  4181. (insert-file-contents file)
  4182. (buffer-string))))
  4183. (defun org-extract-log-state-settings (x)
  4184. "Extract the log state setting from a TODO keyword string.
  4185. This will extract info from a string like \"WAIT(w@/!)\"."
  4186. (let (kw key log1 log2)
  4187. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  4188. (setq kw (match-string 1 x)
  4189. key (and (match-end 2) (match-string 2 x))
  4190. log1 (and (match-end 3) (match-string 3 x))
  4191. log2 (and (match-end 4) (match-string 4 x)))
  4192. (and (or log1 log2)
  4193. (list kw
  4194. (and log1 (if (equal log1 "!") 'time 'note))
  4195. (and log2 (if (equal log2 "!") 'time 'note)))))))
  4196. (defun org-remove-keyword-keys (list)
  4197. "Remove a pair of parenthesis at the end of each string in LIST."
  4198. (mapcar (lambda (x)
  4199. (if (string-match "(.*)$" x)
  4200. (substring x 0 (match-beginning 0))
  4201. x))
  4202. list))
  4203. (defun org-assign-fast-keys (alist)
  4204. "Assign fast keys to a keyword-key alist.
  4205. Respect keys that are already there."
  4206. (let (new e (alt ?0))
  4207. (while (setq e (pop alist))
  4208. (if (or (memq (car e) '(:newline :endgroup :startgroup))
  4209. (cdr e)) ;; Key already assigned.
  4210. (push e new)
  4211. (let ((clist (string-to-list (downcase (car e))))
  4212. (used (append new alist)))
  4213. (when (= (car clist) ?@)
  4214. (pop clist))
  4215. (while (and clist (rassoc (car clist) used))
  4216. (pop clist))
  4217. (unless clist
  4218. (while (rassoc alt used)
  4219. (incf alt)))
  4220. (push (cons (car e) (or (car clist) alt)) new))))
  4221. (nreverse new)))
  4222. ;;; Some variables used in various places
  4223. (defvar org-window-configuration nil
  4224. "Used in various places to store a window configuration.")
  4225. (defvar org-selected-window nil
  4226. "Used in various places to store a window configuration.")
  4227. (defvar org-finish-function nil
  4228. "Function to be called when `C-c C-c' is used.
  4229. This is for getting out of special buffers like capture.")
  4230. ;; FIXME: Occasionally check by commenting these, to make sure
  4231. ;; no other functions uses these, forgetting to let-bind them.
  4232. (org-no-warnings (defvar entry)) ;; unprefixed, from calendar.el
  4233. (defvar org-last-state)
  4234. (org-no-warnings (defvar date)) ;; unprefixed, from calendar.el
  4235. ;; Defined somewhere in this file, but used before definition.
  4236. (defvar org-entities) ;; defined in org-entities.el
  4237. (defvar org-struct-menu)
  4238. (defvar org-org-menu)
  4239. (defvar org-tbl-menu)
  4240. ;;;; Define the Org-mode
  4241. ;; We use a before-change function to check if a table might need
  4242. ;; an update.
  4243. (defvar org-table-may-need-update t
  4244. "Indicates that a table might need an update.
  4245. This variable is set by `org-before-change-function'.
  4246. `org-table-align' sets it back to nil.")
  4247. (defun org-before-change-function (beg end)
  4248. "Every change indicates that a table might need an update."
  4249. (setq org-table-may-need-update t))
  4250. (defvar org-mode-map)
  4251. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  4252. (defvar org-inhibit-startup-visibility-stuff nil) ; Dynamically-scoped param.
  4253. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  4254. (defvar org-inhibit-logging nil) ; Dynamically-scoped param.
  4255. (defvar org-inhibit-blocking nil) ; Dynamically-scoped param.
  4256. (defvar org-table-buffer-is-an nil)
  4257. (defvar bidi-paragraph-direction)
  4258. (defvar buffer-face-mode-face)
  4259. (require 'outline)
  4260. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  4261. (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"))
  4262. (require 'noutline "noutline" 'noerror) ;; stock XEmacs does not have it
  4263. ;; Other stuff we need.
  4264. (require 'time-date)
  4265. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  4266. (require 'easymenu)
  4267. (require 'overlay)
  4268. (require 'org-macs)
  4269. (require 'org-entities)
  4270. ;; (require 'org-compat) moved higher up in the file before it is first used
  4271. (require 'org-faces)
  4272. (require 'org-list)
  4273. (require 'org-pcomplete)
  4274. (require 'org-src)
  4275. (require 'org-footnote)
  4276. ;; babel
  4277. (require 'ob)
  4278. (require 'ob-table)
  4279. (require 'ob-lob)
  4280. (require 'ob-ref)
  4281. (require 'ob-tangle)
  4282. (require 'ob-comint)
  4283. (require 'ob-keys)
  4284. ;;;###autoload
  4285. (define-derived-mode org-mode outline-mode "Org"
  4286. "Outline-based notes management and organizer, alias
  4287. \"Carsten's outline-mode for keeping track of everything.\"
  4288. Org-mode develops organizational tasks around a NOTES file which
  4289. contains information about projects as plain text. Org-mode is
  4290. implemented on top of outline-mode, which is ideal to keep the content
  4291. of large files well structured. It supports ToDo items, deadlines and
  4292. time stamps, which magically appear in the diary listing of the Emacs
  4293. calendar. Tables are easily created with a built-in table editor.
  4294. Plain text URL-like links connect to websites, emails (VM), Usenet
  4295. messages (Gnus), BBDB entries, and any files related to the project.
  4296. For printing and sharing of notes, an Org-mode file (or a part of it)
  4297. can be exported as a structured ASCII or HTML file.
  4298. The following commands are available:
  4299. \\{org-mode-map}"
  4300. ;; Get rid of Outline menus, they are not needed
  4301. ;; Need to do this here because define-derived-mode sets up
  4302. ;; the keymap so late. Still, it is a waste to call this each time
  4303. ;; we switch another buffer into org-mode.
  4304. (if (featurep 'xemacs)
  4305. (when (boundp 'outline-mode-menu-heading)
  4306. ;; Assume this is Greg's port, it uses easymenu
  4307. (easy-menu-remove outline-mode-menu-heading)
  4308. (easy-menu-remove outline-mode-menu-show)
  4309. (easy-menu-remove outline-mode-menu-hide))
  4310. (define-key org-mode-map [menu-bar headings] 'undefined)
  4311. (define-key org-mode-map [menu-bar hide] 'undefined)
  4312. (define-key org-mode-map [menu-bar show] 'undefined))
  4313. (org-load-modules-maybe)
  4314. (easy-menu-add org-org-menu)
  4315. (easy-menu-add org-tbl-menu)
  4316. (org-install-agenda-files-menu)
  4317. (if org-descriptive-links (add-to-invisibility-spec '(org-link)))
  4318. (add-to-invisibility-spec '(org-cwidth))
  4319. (add-to-invisibility-spec '(org-hide-block . t))
  4320. (when (featurep 'xemacs)
  4321. (org-set-local 'line-move-ignore-invisible t))
  4322. (org-set-local 'outline-regexp org-outline-regexp)
  4323. (org-set-local 'outline-level 'org-outline-level)
  4324. (setq bidi-paragraph-direction 'left-to-right)
  4325. (when (and org-ellipsis
  4326. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  4327. (fboundp 'make-glyph-code))
  4328. (unless org-display-table
  4329. (setq org-display-table (make-display-table)))
  4330. (set-display-table-slot
  4331. org-display-table 4
  4332. (vconcat (mapcar
  4333. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  4334. org-ellipsis)))
  4335. (if (stringp org-ellipsis) org-ellipsis "..."))))
  4336. (setq buffer-display-table org-display-table))
  4337. (org-set-regexps-and-options)
  4338. (when (and org-tag-faces (not org-tags-special-faces-re))
  4339. ;; tag faces set outside customize.... force initialization.
  4340. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  4341. ;; Calc embedded
  4342. (org-set-local 'calc-embedded-open-mode "# ")
  4343. (modify-syntax-entry ?@ "w")
  4344. (modify-syntax-entry ?\" "\"")
  4345. (if org-startup-truncated (setq truncate-lines t))
  4346. (org-set-local 'font-lock-unfontify-region-function
  4347. 'org-unfontify-region)
  4348. ;; Activate before-change-function
  4349. (org-set-local 'org-table-may-need-update t)
  4350. (org-add-hook 'before-change-functions 'org-before-change-function nil
  4351. 'local)
  4352. ;; Check for running clock before killing a buffer
  4353. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  4354. ;; Initialize macros templates.
  4355. (org-macro-initialize-templates)
  4356. ;; Initialize radio targets.
  4357. (org-update-radio-target-regexp)
  4358. ;; Indentation.
  4359. (org-set-local 'indent-line-function 'org-indent-line)
  4360. (org-set-local 'indent-region-function 'org-indent-region)
  4361. ;; Filling and auto-filling.
  4362. (org-setup-filling)
  4363. ;; Comments.
  4364. (org-setup-comments-handling)
  4365. ;; Beginning/end of defun
  4366. (org-set-local 'beginning-of-defun-function 'org-back-to-heading)
  4367. (org-set-local 'end-of-defun-function (lambda () (interactive) (org-end-of-subtree nil t)))
  4368. ;; Next error for sparse trees
  4369. (org-set-local 'next-error-function 'org-occur-next-match)
  4370. ;; Make sure dependence stuff works reliably, even for users who set it
  4371. ;; too late :-(
  4372. (if org-enforce-todo-dependencies
  4373. (add-hook 'org-blocker-hook
  4374. 'org-block-todo-from-children-or-siblings-or-parent)
  4375. (remove-hook 'org-blocker-hook
  4376. 'org-block-todo-from-children-or-siblings-or-parent))
  4377. (if org-enforce-todo-checkbox-dependencies
  4378. (add-hook 'org-blocker-hook
  4379. 'org-block-todo-from-checkboxes)
  4380. (remove-hook 'org-blocker-hook
  4381. 'org-block-todo-from-checkboxes))
  4382. ;; Align options lines
  4383. (org-set-local
  4384. 'align-mode-rules-list
  4385. '((org-in-buffer-settings
  4386. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  4387. (modes . '(org-mode)))))
  4388. ;; Imenu
  4389. (org-set-local 'imenu-create-index-function
  4390. 'org-imenu-get-tree)
  4391. ;; Make isearch reveal context
  4392. (if (or (featurep 'xemacs)
  4393. (not (boundp 'outline-isearch-open-invisible-function)))
  4394. ;; Emacs 21 and XEmacs make use of the hook
  4395. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  4396. ;; Emacs 22 deals with this through a special variable
  4397. (org-set-local 'outline-isearch-open-invisible-function
  4398. (lambda (&rest ignore) (org-show-context 'isearch))))
  4399. ;; Turn on org-beamer-mode?
  4400. (and org-startup-with-beamer-mode (org-beamer-mode))
  4401. ;; Setup the pcomplete hooks
  4402. (set (make-local-variable 'pcomplete-command-completion-function)
  4403. 'org-pcomplete-initial)
  4404. (set (make-local-variable 'pcomplete-command-name-function)
  4405. 'org-command-at-point)
  4406. (set (make-local-variable 'pcomplete-default-completion-function)
  4407. 'ignore)
  4408. (set (make-local-variable 'pcomplete-parse-arguments-function)
  4409. 'org-parse-arguments)
  4410. (set (make-local-variable 'pcomplete-termination-string) "")
  4411. (when (>= emacs-major-version 23)
  4412. (set (make-local-variable 'buffer-face-mode-face) 'org-default))
  4413. ;; If empty file that did not turn on org-mode automatically, make it to.
  4414. (if (and org-insert-mode-line-in-empty-file
  4415. (org-called-interactively-p 'any)
  4416. (= (point-min) (point-max)))
  4417. (insert "# -*- mode: org -*-\n\n"))
  4418. (unless org-inhibit-startup
  4419. (when org-startup-align-all-tables
  4420. (let ((bmp (buffer-modified-p)))
  4421. (org-table-map-tables 'org-table-align 'quietly)
  4422. (set-buffer-modified-p bmp)))
  4423. (when org-startup-with-inline-images
  4424. (org-display-inline-images))
  4425. (when org-startup-indented
  4426. (require 'org-indent)
  4427. (org-indent-mode 1))
  4428. (unless org-inhibit-startup-visibility-stuff
  4429. (org-set-startup-visibility)))
  4430. ;; Try to set org-hide correctly
  4431. (set-face-foreground 'org-hide (org-find-invisible-foreground)))
  4432. (when (fboundp 'abbrev-table-put)
  4433. (abbrev-table-put org-mode-abbrev-table
  4434. :parents (list text-mode-abbrev-table)))
  4435. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  4436. (defun org-find-invisible-foreground ()
  4437. (let ((candidates (remove
  4438. "unspecified-bg"
  4439. (list
  4440. (face-background 'default)
  4441. (face-background 'org-default)
  4442. (cdr (assoc 'background-color default-frame-alist))
  4443. (cdr (assoc 'background-color initial-frame-alist))
  4444. (cdr (assoc 'background-color window-system-default-frame-alist))
  4445. (face-foreground 'org-hide)))))
  4446. (car (remove nil candidates))))
  4447. (defun org-current-time ()
  4448. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  4449. (if (> (car org-time-stamp-rounding-minutes) 1)
  4450. (let ((r (car org-time-stamp-rounding-minutes))
  4451. (time (decode-time)))
  4452. (apply 'encode-time
  4453. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  4454. (nthcdr 2 time))))
  4455. (current-time)))
  4456. (defun org-today ()
  4457. "Return today date, considering `org-extend-today-until'."
  4458. (time-to-days
  4459. (time-subtract (current-time)
  4460. (list 0 (* 3600 org-extend-today-until) 0))))
  4461. ;;;; Font-Lock stuff, including the activators
  4462. (defvar org-mouse-map (make-sparse-keymap))
  4463. (org-defkey org-mouse-map [mouse-2] 'org-open-at-mouse)
  4464. (org-defkey org-mouse-map [mouse-3] 'org-find-file-at-mouse)
  4465. (when org-mouse-1-follows-link
  4466. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  4467. (when org-tab-follows-link
  4468. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  4469. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  4470. (require 'font-lock)
  4471. (defconst org-non-link-chars "]\t\n\r<>")
  4472. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  4473. "shell" "elisp" "doi" "message"))
  4474. (defvar org-link-types-re nil
  4475. "Matches a link that has a url-like prefix like \"http:\"")
  4476. (defvar org-link-re-with-space nil
  4477. "Matches a link with spaces, optional angular brackets around it.")
  4478. (defvar org-link-re-with-space2 nil
  4479. "Matches a link with spaces, optional angular brackets around it.")
  4480. (defvar org-link-re-with-space3 nil
  4481. "Matches a link with spaces, only for internal part in bracket links.")
  4482. (defvar org-angle-link-re nil
  4483. "Matches link with angular brackets, spaces are allowed.")
  4484. (defvar org-plain-link-re nil
  4485. "Matches plain link, without spaces.")
  4486. (defvar org-bracket-link-regexp nil
  4487. "Matches a link in double brackets.")
  4488. (defvar org-bracket-link-analytic-regexp nil
  4489. "Regular expression used to analyze links.
  4490. Here is what the match groups contain after a match:
  4491. 1: http:
  4492. 2: http
  4493. 3: path
  4494. 4: [desc]
  4495. 5: desc")
  4496. (defvar org-bracket-link-analytic-regexp++ nil
  4497. "Like `org-bracket-link-analytic-regexp', but include coderef internal type.")
  4498. (defvar org-any-link-re nil
  4499. "Regular expression matching any link.")
  4500. (defcustom org-match-sexp-depth 3
  4501. "Number of stacked braces for sub/superscript matching.
  4502. This has to be set before loading org.el to be effective."
  4503. :group 'org-export-translation ; ??????????????????????????/
  4504. :type 'integer)
  4505. (defun org-create-multibrace-regexp (left right n)
  4506. "Create a regular expression which will match a balanced sexp.
  4507. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  4508. as single character strings.
  4509. The regexp returned will match the entire expression including the
  4510. delimiters. It will also define a single group which contains the
  4511. match except for the outermost delimiters. The maximum depth of
  4512. stacked delimiters is N. Escaping delimiters is not possible."
  4513. (let* ((nothing (concat "[^" left right "]*?"))
  4514. (or "\\|")
  4515. (re nothing)
  4516. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  4517. (while (> n 1)
  4518. (setq n (1- n)
  4519. re (concat re or next)
  4520. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  4521. (concat left "\\(" re "\\)" right)))
  4522. (defvar org-match-substring-regexp
  4523. (concat
  4524. "\\([^\\]\\|^\\)\\([_^]\\)\\("
  4525. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4526. "\\|"
  4527. "\\(" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  4528. "\\|"
  4529. "\\(\\(?:\\*\\|[-+]?[^-+*!@#$%^_ \t\r\n,:\"?<>~;./{}=()]+\\)\\)\\)")
  4530. "The regular expression matching a sub- or superscript.")
  4531. (defvar org-match-substring-with-braces-regexp
  4532. (concat
  4533. "\\([^\\]\\|^\\)\\([_^]\\)\\("
  4534. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4535. "\\)")
  4536. "The regular expression matching a sub- or superscript, forcing braces.")
  4537. (defun org-make-link-regexps ()
  4538. "Update the link regular expressions.
  4539. This should be called after the variable `org-link-types' has changed."
  4540. (setq org-link-types-re
  4541. (concat
  4542. "\\`\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):")
  4543. org-link-re-with-space
  4544. (concat
  4545. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4546. "\\([^" org-non-link-chars " ]"
  4547. "[^" org-non-link-chars "]*"
  4548. "[^" org-non-link-chars " ]\\)>?")
  4549. org-link-re-with-space2
  4550. (concat
  4551. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4552. "\\([^" org-non-link-chars " ]"
  4553. "[^\t\n\r]*"
  4554. "[^" org-non-link-chars " ]\\)>?")
  4555. org-link-re-with-space3
  4556. (concat
  4557. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4558. "\\([^" org-non-link-chars " ]"
  4559. "[^\t\n\r]*\\)")
  4560. org-angle-link-re
  4561. (concat
  4562. "<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4563. "\\([^" org-non-link-chars " ]"
  4564. "[^" org-non-link-chars "]*"
  4565. "\\)>")
  4566. org-plain-link-re
  4567. (concat
  4568. "\\<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4569. (org-re "\\([^ \t\n()<>]+\\(?:([[:word:]0-9_]+)\\|\\([^[:punct:] \t\n]\\|/\\)\\)\\)"))
  4570. ;; "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  4571. org-bracket-link-regexp
  4572. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  4573. org-bracket-link-analytic-regexp
  4574. (concat
  4575. "\\[\\["
  4576. "\\(\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):\\)?"
  4577. "\\([^]]+\\)"
  4578. "\\]"
  4579. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4580. "\\]")
  4581. org-bracket-link-analytic-regexp++
  4582. (concat
  4583. "\\[\\["
  4584. "\\(\\(" (mapconcat 'regexp-quote (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  4585. "\\([^]]+\\)"
  4586. "\\]"
  4587. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4588. "\\]")
  4589. org-any-link-re
  4590. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  4591. org-angle-link-re "\\)\\|\\("
  4592. org-plain-link-re "\\)")))
  4593. (org-make-link-regexps)
  4594. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^\r\n>]*?\\)>"
  4595. "Regular expression for fast time stamp matching.")
  4596. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?\\)[]>]"
  4597. "Regular expression for fast time stamp matching.")
  4598. (defconst org-ts-regexp0
  4599. "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\)\\( +[^]+0-9>\r\n -]+\\)?\\( +\\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4600. "Regular expression matching time strings for analysis.
  4601. This one does not require the space after the date, so it can be used
  4602. on a string that terminates immediately after the date.")
  4603. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]+0-9>\r\n -]*\\)\\( \\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4604. "Regular expression matching time strings for analysis.")
  4605. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  4606. "Regular expression matching time stamps, with groups.")
  4607. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  4608. "Regular expression matching time stamps (also [..]), with groups.")
  4609. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  4610. "Regular expression matching a time stamp range.")
  4611. (defconst org-tr-regexp-both
  4612. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  4613. "Regular expression matching a time stamp range.")
  4614. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  4615. org-ts-regexp "\\)?")
  4616. "Regular expression matching a time stamp or time stamp range.")
  4617. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  4618. org-ts-regexp-both "\\)?")
  4619. "Regular expression matching a time stamp or time stamp range.
  4620. The time stamps may be either active or inactive.")
  4621. (defvar org-emph-face nil)
  4622. (defun org-do-emphasis-faces (limit)
  4623. "Run through the buffer and add overlays to emphasized strings."
  4624. (let (rtn a)
  4625. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  4626. (if (not (= (char-after (match-beginning 3))
  4627. (char-after (match-beginning 4))))
  4628. (progn
  4629. (setq rtn t)
  4630. (setq a (assoc (match-string 3) org-emphasis-alist))
  4631. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  4632. 'face
  4633. (nth 1 a))
  4634. (and (nth 4 a)
  4635. (org-remove-flyspell-overlays-in
  4636. (match-beginning 0) (match-end 0)))
  4637. (add-text-properties (match-beginning 2) (match-end 2)
  4638. '(font-lock-multiline t org-emphasis t))
  4639. (when org-hide-emphasis-markers
  4640. (add-text-properties (match-end 4) (match-beginning 5)
  4641. '(invisible org-link))
  4642. (add-text-properties (match-beginning 3) (match-end 3)
  4643. '(invisible org-link)))))
  4644. (backward-char 1))
  4645. rtn))
  4646. (defun org-emphasize (&optional char)
  4647. "Insert or change an emphasis, i.e. a font like bold or italic.
  4648. If there is an active region, change that region to a new emphasis.
  4649. If there is no region, just insert the marker characters and position
  4650. the cursor between them.
  4651. CHAR should be either the marker character, or the first character of the
  4652. HTML tag associated with that emphasis. If CHAR is a space, the means
  4653. to remove the emphasis of the selected region.
  4654. If char is not given (for example in an interactive call) it
  4655. will be prompted for."
  4656. (interactive)
  4657. (let ((eal org-emphasis-alist) e det
  4658. (erc org-emphasis-regexp-components)
  4659. (prompt "")
  4660. (string "") beg end move tag c s)
  4661. (if (org-region-active-p)
  4662. (setq beg (region-beginning) end (region-end)
  4663. string (buffer-substring beg end))
  4664. (setq move t))
  4665. (while (setq e (pop eal))
  4666. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  4667. c (aref tag 0))
  4668. (push (cons c (string-to-char (car e))) det)
  4669. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  4670. (substring tag 1)))))
  4671. (setq det (nreverse det))
  4672. (unless char
  4673. (message "%s" (concat "Emphasis marker or tag:" prompt))
  4674. (setq char (read-char-exclusive)))
  4675. (setq char (or (cdr (assoc char det)) char))
  4676. (if (equal char ?\ )
  4677. (setq s "" move nil)
  4678. (unless (assoc (char-to-string char) org-emphasis-alist)
  4679. (error "No such emphasis marker: \"%c\"" char))
  4680. (setq s (char-to-string char)))
  4681. (while (and (> (length string) 1)
  4682. (equal (substring string 0 1) (substring string -1))
  4683. (assoc (substring string 0 1) org-emphasis-alist))
  4684. (setq string (substring string 1 -1)))
  4685. (setq string (concat s string s))
  4686. (if beg (delete-region beg end))
  4687. (unless (or (bolp)
  4688. (string-match (concat "[" (nth 0 erc) "\n]")
  4689. (char-to-string (char-before (point)))))
  4690. (insert " "))
  4691. (unless (or (eobp)
  4692. (string-match (concat "[" (nth 1 erc) "\n]")
  4693. (char-to-string (char-after (point)))))
  4694. (insert " ") (backward-char 1))
  4695. (insert string)
  4696. (and move (backward-char 1))))
  4697. (defconst org-nonsticky-props
  4698. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  4699. (defsubst org-rear-nonsticky-at (pos)
  4700. (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
  4701. (defun org-activate-plain-links (limit)
  4702. "Run through the buffer and add overlays to links."
  4703. (catch 'exit
  4704. (let (f hl)
  4705. (when (re-search-forward (concat org-plain-link-re) limit t)
  4706. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4707. (setq f (get-text-property (match-beginning 0) 'face))
  4708. (setq hl (org-match-string-no-properties 0))
  4709. (if (or (eq f 'org-tag)
  4710. (and (listp f) (memq 'org-tag f)))
  4711. nil
  4712. (add-text-properties (match-beginning 0) (match-end 0)
  4713. (list 'mouse-face 'highlight
  4714. 'face 'org-link
  4715. 'htmlize-link `(:uri ,hl)
  4716. 'keymap org-mouse-map))
  4717. (org-rear-nonsticky-at (match-end 0)))
  4718. t))))
  4719. (defun org-activate-code (limit)
  4720. (if (re-search-forward "^[ \t]*\\(:\\(?: .*\\|$\\)\n?\\)" limit t)
  4721. (progn
  4722. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4723. (remove-text-properties (match-beginning 0) (match-end 0)
  4724. '(display t invisible t intangible t))
  4725. t)))
  4726. (defcustom org-src-fontify-natively nil
  4727. "When non-nil, fontify code in code blocks."
  4728. :type 'boolean
  4729. :version "24.1"
  4730. :group 'org-appearance
  4731. :group 'org-babel)
  4732. (defcustom org-allow-promoting-top-level-subtree nil
  4733. "When non-nil, allow promoting a top level subtree.
  4734. The leading star of the top level headline will be replaced
  4735. by a #."
  4736. :type 'boolean
  4737. :version "24.1"
  4738. :group 'org-appearance)
  4739. (defun org-fontify-meta-lines-and-blocks (limit)
  4740. (condition-case nil
  4741. (org-fontify-meta-lines-and-blocks-1 limit)
  4742. (error (message "org-mode fontification error"))))
  4743. (defun org-fontify-meta-lines-and-blocks-1 (limit)
  4744. "Fontify #+ lines and blocks, in the correct ways."
  4745. (let ((case-fold-search t))
  4746. (if (re-search-forward
  4747. "^\\([ \t]*#\\(\\(\\+[a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)[ \t]*\\(\\([^ \t\n]*\\)[ \t]*\\(.*\\)\\)\\)"
  4748. limit t)
  4749. (let ((beg (match-beginning 0))
  4750. (block-start (match-end 0))
  4751. (block-end nil)
  4752. (lang (match-string 7))
  4753. (beg1 (line-beginning-position 2))
  4754. (dc1 (downcase (match-string 2)))
  4755. (dc3 (downcase (match-string 3)))
  4756. end end1 quoting block-type ovl)
  4757. (cond
  4758. ((member dc1 '("+html:" "+ascii:" "+latex:" "+docbook:"))
  4759. ;; a single line of backend-specific content
  4760. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4761. (remove-text-properties (match-beginning 0) (match-end 0)
  4762. '(display t invisible t intangible t))
  4763. (add-text-properties (match-beginning 1) (match-end 3)
  4764. '(font-lock-fontified t face org-meta-line))
  4765. (add-text-properties (match-beginning 6) (+ (match-end 6) 1)
  4766. '(font-lock-fontified t face org-block))
  4767. ; for backend-specific code
  4768. t)
  4769. ((and (match-end 4) (equal dc3 "+begin"))
  4770. ;; Truly a block
  4771. (setq block-type (downcase (match-string 5))
  4772. quoting (member block-type org-protecting-blocks))
  4773. (when (re-search-forward
  4774. (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
  4775. nil t) ;; on purpose, we look further than LIMIT
  4776. (setq end (min (point-max) (match-end 0))
  4777. end1 (min (point-max) (1- (match-beginning 0))))
  4778. (setq block-end (match-beginning 0))
  4779. (when quoting
  4780. (remove-text-properties beg end
  4781. '(display t invisible t intangible t)))
  4782. (add-text-properties
  4783. beg end
  4784. '(font-lock-fontified t font-lock-multiline t))
  4785. (add-text-properties beg beg1 '(face org-meta-line))
  4786. (add-text-properties end1 (min (point-max) (1+ end))
  4787. '(face org-meta-line)) ; for end_src
  4788. (cond
  4789. ((and lang (not (string= lang "")) org-src-fontify-natively)
  4790. (org-src-font-lock-fontify-block lang block-start block-end)
  4791. ;; remove old background overlays
  4792. (mapc (lambda (ov)
  4793. (if (eq (overlay-get ov 'face) 'org-block-background)
  4794. (delete-overlay ov)))
  4795. (overlays-at (/ (+ beg1 block-end) 2)))
  4796. ;; add a background overlay
  4797. (setq ovl (make-overlay beg1 block-end))
  4798. (overlay-put ovl 'face 'org-block-background)
  4799. (overlay-put ovl 'evaporate t)) ;; make it go away when empty
  4800. (quoting
  4801. (add-text-properties beg1 (min (point-max) (1+ end1))
  4802. '(face org-block))) ; end of source block
  4803. ((not org-fontify-quote-and-verse-blocks))
  4804. ((string= block-type "quote")
  4805. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-quote)))
  4806. ((string= block-type "verse")
  4807. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-verse))))
  4808. (add-text-properties beg beg1 '(face org-block-begin-line))
  4809. (add-text-properties (min (point-max) (1+ end)) (min (point-max) (1+ end1))
  4810. '(face org-block-end-line))
  4811. t))
  4812. ((member dc1 '("+title:" "+author:" "+email:" "+date:"))
  4813. (add-text-properties
  4814. beg (match-end 3)
  4815. (if (member (intern (substring dc1 0 -1)) org-hidden-keywords)
  4816. '(font-lock-fontified t invisible t)
  4817. '(font-lock-fontified t face org-document-info-keyword)))
  4818. (add-text-properties
  4819. (match-beginning 6) (match-end 6)
  4820. (if (string-equal dc1 "+title:")
  4821. '(font-lock-fontified t face org-document-title)
  4822. '(font-lock-fontified t face org-document-info))))
  4823. ((or (equal dc1 "+results")
  4824. (member dc1 '("+begin:" "+end:" "+caption:" "+label:"
  4825. "+orgtbl:" "+tblfm:" "+tblname:" "+results:"
  4826. "+call:" "+header:" "+headers:" "+name:"))
  4827. (and (match-end 4) (equal dc3 "+attr")))
  4828. (add-text-properties
  4829. beg (match-end 0)
  4830. '(font-lock-fontified t face org-meta-line))
  4831. t)
  4832. ((member dc3 '(" " ""))
  4833. (add-text-properties
  4834. beg (match-end 0)
  4835. '(font-lock-fontified t face font-lock-comment-face)))
  4836. ((not (member (char-after beg) '(?\ ?\t)))
  4837. ;; just any other in-buffer setting, but not indented
  4838. (add-text-properties
  4839. beg (match-end 0)
  4840. '(font-lock-fontified t face org-meta-line))
  4841. t)
  4842. (t nil))))))
  4843. (defun org-activate-angle-links (limit)
  4844. "Run through the buffer and add overlays to links."
  4845. (if (re-search-forward org-angle-link-re limit t)
  4846. (progn
  4847. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4848. (add-text-properties (match-beginning 0) (match-end 0)
  4849. (list 'mouse-face 'highlight
  4850. 'keymap org-mouse-map))
  4851. (org-rear-nonsticky-at (match-end 0))
  4852. t)))
  4853. (defun org-activate-footnote-links (limit)
  4854. "Run through the buffer and add overlays to footnotes."
  4855. (let ((fn (org-footnote-next-reference-or-definition limit)))
  4856. (when fn
  4857. (let ((beg (nth 1 fn)) (end (nth 2 fn)))
  4858. (org-remove-flyspell-overlays-in beg end)
  4859. (add-text-properties beg end
  4860. (list 'mouse-face 'highlight
  4861. 'keymap org-mouse-map
  4862. 'help-echo
  4863. (if (= (point-at-bol) beg)
  4864. "Footnote definition"
  4865. "Footnote reference")
  4866. 'font-lock-fontified t
  4867. 'font-lock-multiline t
  4868. 'face 'org-footnote))))))
  4869. (defun org-activate-bracket-links (limit)
  4870. "Run through the buffer and add overlays to bracketed links."
  4871. (if (re-search-forward org-bracket-link-regexp limit t)
  4872. (let* ((hl (org-match-string-no-properties 1))
  4873. (help (concat "LINK: " hl))
  4874. ;; FIXME: Above we should remove the escapes. But that
  4875. ;; requires another match, protecting match data, a lot
  4876. ;; of overhead for font-lock.
  4877. (ip (org-maybe-intangible
  4878. (list 'invisible 'org-link
  4879. 'keymap org-mouse-map 'mouse-face 'highlight
  4880. 'font-lock-multiline t 'help-echo help
  4881. 'htmlize-link `(:uri ,hl))))
  4882. (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
  4883. 'font-lock-multiline t 'help-echo help
  4884. 'htmlize-link `(:uri ,hl))))
  4885. ;; We need to remove the invisible property here. Table narrowing
  4886. ;; may have made some of this invisible.
  4887. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4888. (remove-text-properties (match-beginning 0) (match-end 0)
  4889. '(invisible nil))
  4890. (if (match-end 3)
  4891. (progn
  4892. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  4893. (org-rear-nonsticky-at (match-beginning 3))
  4894. (add-text-properties (match-beginning 3) (match-end 3) vp)
  4895. (org-rear-nonsticky-at (match-end 3))
  4896. (add-text-properties (match-end 3) (match-end 0) ip)
  4897. (org-rear-nonsticky-at (match-end 0)))
  4898. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  4899. (org-rear-nonsticky-at (match-beginning 1))
  4900. (add-text-properties (match-beginning 1) (match-end 1) vp)
  4901. (org-rear-nonsticky-at (match-end 1))
  4902. (add-text-properties (match-end 1) (match-end 0) ip)
  4903. (org-rear-nonsticky-at (match-end 0)))
  4904. t)))
  4905. (defun org-activate-dates (limit)
  4906. "Run through the buffer and add overlays to dates."
  4907. (if (re-search-forward org-tsr-regexp-both limit t)
  4908. (progn
  4909. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4910. (add-text-properties (match-beginning 0) (match-end 0)
  4911. (list 'mouse-face 'highlight
  4912. 'keymap org-mouse-map))
  4913. (org-rear-nonsticky-at (match-end 0))
  4914. (when org-display-custom-times
  4915. (if (match-end 3)
  4916. (org-display-custom-time (match-beginning 3) (match-end 3)))
  4917. (org-display-custom-time (match-beginning 1) (match-end 1)))
  4918. t)))
  4919. (defvar org-target-link-regexp nil
  4920. "Regular expression matching radio targets in plain text.")
  4921. (make-variable-buffer-local 'org-target-link-regexp)
  4922. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  4923. "Regular expression matching a link target.")
  4924. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  4925. "Regular expression matching a radio target.")
  4926. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  4927. "Regular expression matching any target.")
  4928. (defun org-activate-target-links (limit)
  4929. "Run through the buffer and add overlays to target matches."
  4930. (when org-target-link-regexp
  4931. (let ((case-fold-search t))
  4932. (if (re-search-forward org-target-link-regexp limit t)
  4933. (progn
  4934. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4935. (add-text-properties (match-beginning 0) (match-end 0)
  4936. (list 'mouse-face 'highlight
  4937. 'keymap org-mouse-map
  4938. 'help-echo "Radio target link"
  4939. 'org-linked-text t))
  4940. (org-rear-nonsticky-at (match-end 0))
  4941. t)))))
  4942. (defun org-update-radio-target-regexp ()
  4943. "Find all radio targets in this file and update the regular expression."
  4944. (interactive)
  4945. (when (memq 'radio org-activate-links)
  4946. (setq org-target-link-regexp
  4947. (org-make-target-link-regexp (org-all-targets 'radio)))
  4948. (org-restart-font-lock)))
  4949. (defun org-hide-wide-columns (limit)
  4950. (let (s e)
  4951. (setq s (text-property-any (point) (or limit (point-max))
  4952. 'org-cwidth t))
  4953. (when s
  4954. (setq e (next-single-property-change s 'org-cwidth))
  4955. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  4956. (goto-char e)
  4957. t)))
  4958. (defvar org-latex-and-specials-regexp nil
  4959. "Regular expression for highlighting export special stuff.")
  4960. (defvar org-match-substring-regexp)
  4961. (defvar org-match-substring-with-braces-regexp)
  4962. ;; This should be with the exporter code, but we also use if for font-locking
  4963. (defconst org-export-html-special-string-regexps
  4964. '(("\\\\-" . "&shy;")
  4965. ("---\\([^-]\\)" . "&mdash;\\1")
  4966. ("--\\([^-]\\)" . "&ndash;\\1")
  4967. ("\\.\\.\\." . "&hellip;"))
  4968. "Regular expressions for special string conversion.")
  4969. (defun org-compute-latex-and-specials-regexp ()
  4970. "Compute regular expression for stuff treated specially by exporters."
  4971. (if (not org-highlight-latex-fragments-and-specials)
  4972. (org-set-local 'org-latex-and-specials-regexp nil)
  4973. (require 'org-exp)
  4974. (let*
  4975. ((matchers (plist-get org-format-latex-options :matchers))
  4976. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  4977. org-latex-regexps)))
  4978. (org-export-allow-BIND nil)
  4979. (options (org-combine-plists (org-default-export-plist)
  4980. (org-infile-export-plist)))
  4981. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  4982. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  4983. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  4984. (org-export-html-expand (plist-get options :expand-quoted-html))
  4985. (org-export-with-special-strings (plist-get options :special-strings))
  4986. (re-sub
  4987. (cond
  4988. ((equal org-export-with-sub-superscripts '{})
  4989. (list org-match-substring-with-braces-regexp))
  4990. (org-export-with-sub-superscripts
  4991. (list org-match-substring-regexp))))
  4992. (re-latex
  4993. (if org-export-with-LaTeX-fragments
  4994. (mapcar (lambda (x) (nth 1 x)) latexs)))
  4995. (re-macros
  4996. (if org-export-with-TeX-macros
  4997. (list (concat "\\\\"
  4998. (regexp-opt
  4999. (append
  5000. (delq nil
  5001. (mapcar 'car-safe
  5002. (append org-entities-user
  5003. org-entities)))
  5004. (if (boundp 'org-latex-entities)
  5005. (mapcar (lambda (x)
  5006. (or (car-safe x) x))
  5007. org-latex-entities)
  5008. nil))
  5009. 'words))) ; FIXME
  5010. ))
  5011. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  5012. (re-special (if org-export-with-special-strings
  5013. (mapcar (lambda (x) (car x))
  5014. org-export-html-special-string-regexps)))
  5015. (re-rest
  5016. (delq nil
  5017. (list
  5018. (if org-export-html-expand "@<[^>\n]+>")
  5019. ))))
  5020. (org-set-local
  5021. 'org-latex-and-specials-regexp
  5022. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  5023. re-rest) "\\|")))))
  5024. (defun org-do-latex-and-special-faces (limit)
  5025. "Run through the buffer and add overlays to links."
  5026. (when org-latex-and-specials-regexp
  5027. (let (rtn d)
  5028. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  5029. limit t))
  5030. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  5031. 'face))
  5032. '(org-code org-verbatim underline)))
  5033. (progn
  5034. (setq rtn t
  5035. d (cond ((member (char-after (1+ (match-beginning 0)))
  5036. '(?_ ?^)) 1)
  5037. (t 0)))
  5038. (font-lock-prepend-text-property
  5039. (+ d (match-beginning 0)) (match-end 0)
  5040. 'face 'org-latex-and-export-specials)
  5041. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  5042. '(font-lock-multiline t)))))
  5043. rtn)))
  5044. (defun org-restart-font-lock ()
  5045. "Restart `font-lock-mode', to force refontification."
  5046. (when (and (boundp 'font-lock-mode) font-lock-mode)
  5047. (font-lock-mode -1)
  5048. (font-lock-mode 1)))
  5049. (defun org-all-targets (&optional radio)
  5050. "Return a list of all targets in this file.
  5051. When optional argument RADIO is non-nil, only find radio
  5052. targets."
  5053. (let ((re (if radio org-radio-target-regexp org-target-regexp)) rtn)
  5054. (save-excursion
  5055. (goto-char (point-min))
  5056. (while (re-search-forward re nil t)
  5057. (let ((obj (org-element-context)))
  5058. (when (memq (org-element-type obj) '(radio-target target))
  5059. (add-to-list 'rtn (downcase (org-element-property :value obj))))))
  5060. rtn)))
  5061. (defun org-make-target-link-regexp (targets)
  5062. "Make regular expression matching all strings in TARGETS.
  5063. The regular expression finds the targets also if there is a line break
  5064. between words."
  5065. (and targets
  5066. (concat
  5067. "\\<\\("
  5068. (mapconcat
  5069. (lambda (x)
  5070. (setq x (regexp-quote x))
  5071. (while (string-match " +" x)
  5072. (setq x (replace-match "\\s-+" t t x)))
  5073. x)
  5074. targets
  5075. "\\|")
  5076. "\\)\\>")))
  5077. (defun org-activate-tags (limit)
  5078. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \r\n]") limit t)
  5079. (progn
  5080. (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
  5081. (add-text-properties (match-beginning 1) (match-end 1)
  5082. (list 'mouse-face 'highlight
  5083. 'keymap org-mouse-map))
  5084. (org-rear-nonsticky-at (match-end 1))
  5085. t)))
  5086. (defun org-outline-level ()
  5087. "Compute the outline level of the heading at point.
  5088. If this is called at a normal headline, the level is the number of stars.
  5089. Use `org-reduced-level' to remove the effect of `org-odd-levels'."
  5090. (save-excursion
  5091. (if (not (condition-case nil
  5092. (org-back-to-heading t)
  5093. (error nil)))
  5094. 0
  5095. (looking-at org-outline-regexp)
  5096. (1- (- (match-end 0) (match-beginning 0))))))
  5097. (defvar org-font-lock-keywords nil)
  5098. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\+?\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  5099. "Regular expression matching a property line.")
  5100. (defvar org-font-lock-hook nil
  5101. "Functions to be called for special font lock stuff.")
  5102. (defvar org-font-lock-set-keywords-hook nil
  5103. "Functions that can manipulate `org-font-lock-extra-keywords'.
  5104. This is called after `org-font-lock-extra-keywords' is defined, but before
  5105. it is installed to be used by font lock. This can be useful if something
  5106. needs to be inserted at a specific position in the font-lock sequence.")
  5107. (defun org-font-lock-hook (limit)
  5108. "Run `org-font-lock-hook' within LIMIT."
  5109. (run-hook-with-args 'org-font-lock-hook limit))
  5110. (defun org-set-font-lock-defaults ()
  5111. "Set font lock defaults for the current buffer."
  5112. (let* ((em org-fontify-emphasized-text)
  5113. (lk org-activate-links)
  5114. (org-font-lock-extra-keywords
  5115. (list
  5116. ;; Call the hook
  5117. '(org-font-lock-hook)
  5118. ;; Headlines
  5119. `(,(if org-fontify-whole-heading-line
  5120. "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
  5121. "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
  5122. (1 (org-get-level-face 1))
  5123. (2 (org-get-level-face 2))
  5124. (3 (org-get-level-face 3)))
  5125. ;; Table lines
  5126. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  5127. (1 'org-table t))
  5128. ;; Table internals
  5129. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  5130. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  5131. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  5132. '("| *\\(<[lrc]?[0-9]*>\\)" (1 'org-formula t))
  5133. ;; Drawers
  5134. (list org-drawer-regexp '(0 'org-special-keyword t))
  5135. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  5136. ;; Properties
  5137. (list org-property-re
  5138. '(1 'org-special-keyword t)
  5139. '(3 'org-property-value t))
  5140. ;; Links
  5141. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  5142. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  5143. (if (memq 'plain lk) '(org-activate-plain-links))
  5144. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  5145. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  5146. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  5147. (if (memq 'footnote lk) '(org-activate-footnote-links))
  5148. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  5149. '(org-hide-wide-columns (0 nil append))
  5150. ;; TODO keyword
  5151. (list (format org-heading-keyword-regexp-format
  5152. org-todo-regexp)
  5153. '(2 (org-get-todo-face 2) t))
  5154. ;; DONE
  5155. (if org-fontify-done-headline
  5156. (list (format org-heading-keyword-regexp-format
  5157. (concat
  5158. "\\(?:"
  5159. (mapconcat 'regexp-quote org-done-keywords "\\|")
  5160. "\\)"))
  5161. '(2 'org-headline-done t))
  5162. nil)
  5163. ;; Priorities
  5164. '(org-font-lock-add-priority-faces)
  5165. ;; Tags
  5166. '(org-font-lock-add-tag-faces)
  5167. ;; Special keywords
  5168. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  5169. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  5170. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  5171. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  5172. ;; Emphasis
  5173. (if em
  5174. (if (featurep 'xemacs)
  5175. '(org-do-emphasis-faces (0 nil append))
  5176. '(org-do-emphasis-faces)))
  5177. ;; Checkboxes
  5178. '("^[ \t]*\\(?:[-+*]\\|[0-9]+[.)]\\)[ \t]+\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?\\(\\[[- X]\\]\\)"
  5179. 1 'org-checkbox prepend)
  5180. (if (cdr (assq 'checkbox org-list-automatic-rules))
  5181. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  5182. (0 (org-get-checkbox-statistics-face) t)))
  5183. ;; Description list items
  5184. '("^[ \t]*[-+*][ \t]+\\(.*?[ \t]+::\\)\\([ \t]+\\|$\\)"
  5185. 1 'org-list-dt prepend)
  5186. ;; ARCHIVEd headings
  5187. (list (concat
  5188. org-outline-regexp-bol
  5189. "\\(.*:" org-archive-tag ":.*\\)")
  5190. '(1 'org-archived prepend))
  5191. ;; Specials
  5192. '(org-do-latex-and-special-faces)
  5193. '(org-fontify-entities)
  5194. '(org-raise-scripts)
  5195. ;; Code
  5196. '(org-activate-code (1 'org-code t))
  5197. ;; COMMENT
  5198. (list (format org-heading-keyword-regexp-format
  5199. (concat "\\("
  5200. org-comment-string "\\|" org-quote-string
  5201. "\\)"))
  5202. '(2 'org-special-keyword t))
  5203. ;; Blocks and meta lines
  5204. '(org-fontify-meta-lines-and-blocks)
  5205. )))
  5206. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  5207. (run-hooks 'org-font-lock-set-keywords-hook)
  5208. ;; Now set the full font-lock-keywords
  5209. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  5210. (org-set-local 'font-lock-defaults
  5211. '(org-font-lock-keywords t nil nil backward-paragraph))
  5212. (kill-local-variable 'font-lock-keywords) nil))
  5213. (defun org-toggle-pretty-entities ()
  5214. "Toggle the composition display of entities as UTF8 characters."
  5215. (interactive)
  5216. (org-set-local 'org-pretty-entities (not org-pretty-entities))
  5217. (org-restart-font-lock)
  5218. (if org-pretty-entities
  5219. (message "Entities are displayed as UTF8 characters")
  5220. (save-restriction
  5221. (widen)
  5222. (org-decompose-region (point-min) (point-max))
  5223. (message "Entities are displayed plain"))))
  5224. (defvar org-custom-properties-overlays nil
  5225. "List of overlays used for custom properties.")
  5226. (make-variable-buffer-local 'org-custom-properties-overlays)
  5227. (defun org-toggle-custom-properties-visibility ()
  5228. "Display or hide properties in `org-custom-properties'."
  5229. (interactive)
  5230. (if org-custom-properties-overlays
  5231. (progn (mapc 'delete-overlay org-custom-properties-overlays)
  5232. (setq org-custom-properties-overlays nil))
  5233. (unless (not org-custom-properties)
  5234. (save-excursion
  5235. (save-restriction
  5236. (widen)
  5237. (goto-char (point-min))
  5238. (while (re-search-forward org-property-re nil t)
  5239. (mapc (lambda(p)
  5240. (when (equal p (substring (match-string 1) 1 -1))
  5241. (let ((o (make-overlay (match-beginning 0) (1+ (match-end 0)))))
  5242. (overlay-put o 'invisible t)
  5243. (overlay-put o 'org-custom-property t)
  5244. (push o org-custom-properties-overlays))))
  5245. org-custom-properties)))))))
  5246. (defun org-fontify-entities (limit)
  5247. "Find an entity to fontify."
  5248. (let (ee)
  5249. (when org-pretty-entities
  5250. (catch 'match
  5251. (while (re-search-forward
  5252. "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]\n]\\)"
  5253. limit t)
  5254. (if (and (not (org-in-indented-comment-line))
  5255. (setq ee (org-entity-get (match-string 1)))
  5256. (= (length (nth 6 ee)) 1))
  5257. (let*
  5258. ((end (if (equal (match-string 2) "{}")
  5259. (match-end 2)
  5260. (match-end 1))))
  5261. (add-text-properties
  5262. (match-beginning 0) end
  5263. (list 'font-lock-fontified t))
  5264. (compose-region (match-beginning 0) end
  5265. (nth 6 ee) nil)
  5266. (backward-char 1)
  5267. (throw 'match t))))
  5268. nil))))
  5269. (defun org-fontify-like-in-org-mode (s &optional odd-levels)
  5270. "Fontify string S like in Org-mode."
  5271. (with-temp-buffer
  5272. (insert s)
  5273. (let ((org-odd-levels-only odd-levels))
  5274. (org-mode)
  5275. (font-lock-fontify-buffer)
  5276. (buffer-string))))
  5277. (defvar org-m nil)
  5278. (defvar org-l nil)
  5279. (defvar org-f nil)
  5280. (defun org-get-level-face (n)
  5281. "Get the right face for match N in font-lock matching of headlines."
  5282. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  5283. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  5284. (if org-cycle-level-faces
  5285. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  5286. (setq org-f (nth (1- (min org-l org-n-level-faces)) org-level-faces)))
  5287. (cond
  5288. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  5289. ((eq n 2) org-f)
  5290. (t (if org-level-color-stars-only nil org-f))))
  5291. (defun org-get-todo-face (kwd)
  5292. "Get the right face for a TODO keyword KWD.
  5293. If KWD is a number, get the corresponding match group."
  5294. (if (numberp kwd) (setq kwd (match-string kwd)))
  5295. (or (org-face-from-face-or-color
  5296. 'todo 'org-todo (cdr (assoc kwd org-todo-keyword-faces)))
  5297. (and (member kwd org-done-keywords) 'org-done)
  5298. 'org-todo))
  5299. (defun org-face-from-face-or-color (context inherit face-or-color)
  5300. "Create a face list that inherits INHERIT, but sets the foreground color.
  5301. When FACE-OR-COLOR is not a string, just return it."
  5302. (if (stringp face-or-color)
  5303. (list :inherit inherit
  5304. (cdr (assoc context org-faces-easy-properties))
  5305. face-or-color)
  5306. face-or-color))
  5307. (defun org-font-lock-add-tag-faces (limit)
  5308. "Add the special tag faces."
  5309. (when (and org-tag-faces org-tags-special-faces-re)
  5310. (while (re-search-forward org-tags-special-faces-re limit t)
  5311. (add-text-properties (match-beginning 1) (match-end 1)
  5312. (list 'face (org-get-tag-face 1)
  5313. 'font-lock-fontified t))
  5314. (backward-char 1))))
  5315. (defun org-font-lock-add-priority-faces (limit)
  5316. "Add the special priority faces."
  5317. (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
  5318. (when (save-match-data (org-at-heading-p))
  5319. (add-text-properties
  5320. (match-beginning 0) (match-end 0)
  5321. (list 'face (or (org-face-from-face-or-color
  5322. 'priority 'org-special-keyword
  5323. (cdr (assoc (char-after (match-beginning 1))
  5324. org-priority-faces)))
  5325. 'org-special-keyword)
  5326. 'font-lock-fontified t)))))
  5327. (defun org-get-tag-face (kwd)
  5328. "Get the right face for a TODO keyword KWD.
  5329. If KWD is a number, get the corresponding match group."
  5330. (if (numberp kwd) (setq kwd (match-string kwd)))
  5331. (or (org-face-from-face-or-color
  5332. 'tag 'org-tag (cdr (assoc kwd org-tag-faces)))
  5333. 'org-tag))
  5334. (defun org-unfontify-region (beg end &optional maybe_loudly)
  5335. "Remove fontification and activation overlays from links."
  5336. (font-lock-default-unfontify-region beg end)
  5337. (let* ((buffer-undo-list t)
  5338. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  5339. (inhibit-modification-hooks t)
  5340. deactivate-mark buffer-file-name buffer-file-truename)
  5341. (org-decompose-region beg end)
  5342. (remove-text-properties beg end
  5343. '(mouse-face t keymap t org-linked-text t
  5344. invisible t intangible t
  5345. org-no-flyspell t org-emphasis t))
  5346. (org-remove-font-lock-display-properties beg end)))
  5347. (defconst org-script-display '(((raise -0.3) (height 0.7))
  5348. ((raise 0.3) (height 0.7))
  5349. ((raise -0.5))
  5350. ((raise 0.5)))
  5351. "Display properties for showing superscripts and subscripts.")
  5352. (defun org-remove-font-lock-display-properties (beg end)
  5353. "Remove specific display properties that have been added by font lock.
  5354. The will remove the raise properties that are used to show superscripts
  5355. and subscripts."
  5356. (let (next prop)
  5357. (while (< beg end)
  5358. (setq next (next-single-property-change beg 'display nil end)
  5359. prop (get-text-property beg 'display))
  5360. (if (member prop org-script-display)
  5361. (put-text-property beg next 'display nil))
  5362. (setq beg next))))
  5363. (defun org-raise-scripts (limit)
  5364. "Add raise properties to sub/superscripts."
  5365. (when (and org-pretty-entities org-pretty-entities-include-sub-superscripts)
  5366. (if (re-search-forward
  5367. (if (eq org-use-sub-superscripts t)
  5368. org-match-substring-regexp
  5369. org-match-substring-with-braces-regexp)
  5370. limit t)
  5371. (let* ((pos (point)) table-p comment-p
  5372. (mpos (match-beginning 3))
  5373. (emph-p (get-text-property mpos 'org-emphasis))
  5374. (link-p (get-text-property mpos 'mouse-face))
  5375. (keyw-p (eq 'org-special-keyword (get-text-property mpos 'face))))
  5376. (goto-char (point-at-bol))
  5377. (setq table-p (org-looking-at-p org-table-dataline-regexp)
  5378. comment-p (org-looking-at-p "[ \t]*#"))
  5379. (goto-char pos)
  5380. ;; FIXME: Should we go back one character here, for a_b^c
  5381. ;; (goto-char (1- pos)) ;????????????????????
  5382. (if (or comment-p emph-p link-p keyw-p)
  5383. t
  5384. (put-text-property (match-beginning 3) (match-end 0)
  5385. 'display
  5386. (if (equal (char-after (match-beginning 2)) ?^)
  5387. (nth (if table-p 3 1) org-script-display)
  5388. (nth (if table-p 2 0) org-script-display)))
  5389. (add-text-properties (match-beginning 2) (match-end 2)
  5390. (list 'invisible t
  5391. 'org-dwidth t 'org-dwidth-n 1))
  5392. (if (and (eq (char-after (match-beginning 3)) ?{)
  5393. (eq (char-before (match-end 3)) ?}))
  5394. (progn
  5395. (add-text-properties
  5396. (match-beginning 3) (1+ (match-beginning 3))
  5397. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))
  5398. (add-text-properties
  5399. (1- (match-end 3)) (match-end 3)
  5400. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))))
  5401. t)))))
  5402. ;;;; Visibility cycling, including org-goto and indirect buffer
  5403. ;;; Cycling
  5404. (defvar org-cycle-global-status nil)
  5405. (make-variable-buffer-local 'org-cycle-global-status)
  5406. (defvar org-cycle-subtree-status nil)
  5407. (make-variable-buffer-local 'org-cycle-subtree-status)
  5408. (defvar org-inlinetask-min-level)
  5409. ;;;###autoload
  5410. (defun org-cycle (&optional arg)
  5411. "TAB-action and visibility cycling for Org-mode.
  5412. This is the command invoked in Org-mode by the TAB key. Its main purpose
  5413. is outline visibility cycling, but it also invokes other actions
  5414. in special contexts.
  5415. - When this function is called with a prefix argument, rotate the entire
  5416. buffer through 3 states (global cycling)
  5417. 1. OVERVIEW: Show only top-level headlines.
  5418. 2. CONTENTS: Show all headlines of all levels, but no body text.
  5419. 3. SHOW ALL: Show everything.
  5420. When called with two `C-u C-u' prefixes, switch to the startup visibility,
  5421. determined by the variable `org-startup-folded', and by any VISIBILITY
  5422. properties in the buffer.
  5423. When called with three `C-u C-u C-u' prefixed, show the entire buffer,
  5424. including any drawers.
  5425. - When inside a table, re-align the table and move to the next field.
  5426. - When point is at the beginning of a headline, rotate the subtree started
  5427. by this line through 3 different states (local cycling)
  5428. 1. FOLDED: Only the main headline is shown.
  5429. 2. CHILDREN: The main headline and the direct children are shown.
  5430. From this state, you can move to one of the children
  5431. and zoom in further.
  5432. 3. SUBTREE: Show the entire subtree, including body text.
  5433. If there is no subtree, switch directly from CHILDREN to FOLDED.
  5434. - When point is at the beginning of an empty headline and the variable
  5435. `org-cycle-level-after-item/entry-creation' is set, cycle the level
  5436. of the headline by demoting and promoting it to likely levels. This
  5437. speeds up creation document structure by pressing TAB once or several
  5438. times right after creating a new headline.
  5439. - When there is a numeric prefix, go up to a heading with level ARG, do
  5440. a `show-subtree' and return to the previous cursor position. If ARG
  5441. is negative, go up that many levels.
  5442. - When point is not at the beginning of a headline, execute the global
  5443. binding for TAB, which is re-indenting the line. See the option
  5444. `org-cycle-emulate-tab' for details.
  5445. - Special case: if point is at the beginning of the buffer and there is
  5446. no headline in line 1, this function will act as if called with prefix arg
  5447. (C-u TAB, same as S-TAB) also when called without prefix arg.
  5448. But only if also the variable `org-cycle-global-at-bob' is t."
  5449. (interactive "P")
  5450. (org-load-modules-maybe)
  5451. (unless (or (run-hook-with-args-until-success 'org-tab-first-hook)
  5452. (and org-cycle-level-after-item/entry-creation
  5453. (or (org-cycle-level)
  5454. (org-cycle-item-indentation))))
  5455. (let* ((limit-level
  5456. (or org-cycle-max-level
  5457. (and (boundp 'org-inlinetask-min-level)
  5458. org-inlinetask-min-level
  5459. (1- org-inlinetask-min-level))))
  5460. (nstars (and limit-level
  5461. (if org-odd-levels-only
  5462. (and limit-level (1- (* limit-level 2)))
  5463. limit-level)))
  5464. (org-outline-regexp
  5465. (if (not (derived-mode-p 'org-mode))
  5466. outline-regexp
  5467. (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ "))))
  5468. (bob-special (and org-cycle-global-at-bob (not arg) (bobp)
  5469. (not (looking-at org-outline-regexp))))
  5470. (org-cycle-hook
  5471. (if bob-special
  5472. (delq 'org-optimize-window-after-visibility-change
  5473. (copy-sequence org-cycle-hook))
  5474. org-cycle-hook))
  5475. (pos (point)))
  5476. (if (or bob-special (equal arg '(4)))
  5477. ;; special case: use global cycling
  5478. (setq arg t))
  5479. (cond
  5480. ((equal arg '(16))
  5481. (setq last-command 'dummy)
  5482. (org-set-startup-visibility)
  5483. (message "Startup visibility, plus VISIBILITY properties"))
  5484. ((equal arg '(64))
  5485. (show-all)
  5486. (message "Entire buffer visible, including drawers"))
  5487. ;; Table: enter it or move to the next field.
  5488. ((org-at-table-p 'any)
  5489. (if (org-at-table.el-p)
  5490. (message "Use C-c ' to edit table.el tables")
  5491. (if arg (org-table-edit-field t)
  5492. (org-table-justify-field-maybe)
  5493. (call-interactively 'org-table-next-field))))
  5494. ((run-hook-with-args-until-success
  5495. 'org-tab-after-check-for-table-hook))
  5496. ;; Global cycling: delegate to `org-cycle-internal-global'.
  5497. ((eq arg t) (org-cycle-internal-global))
  5498. ;; Drawers: delegate to `org-flag-drawer'.
  5499. ((and org-drawers org-drawer-regexp
  5500. (save-excursion
  5501. (beginning-of-line 1)
  5502. (looking-at org-drawer-regexp)))
  5503. (org-flag-drawer ; toggle block visibility
  5504. (not (get-char-property (match-end 0) 'invisible))))
  5505. ;; Show-subtree, ARG levels up from here.
  5506. ((integerp arg)
  5507. (save-excursion
  5508. (org-back-to-heading)
  5509. (outline-up-heading (if (< arg 0) (- arg)
  5510. (- (funcall outline-level) arg)))
  5511. (org-show-subtree)))
  5512. ;; Inline task: delegate to `org-inlinetask-toggle-visibility'.
  5513. ((and (featurep 'org-inlinetask)
  5514. (org-inlinetask-at-task-p)
  5515. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5516. (org-inlinetask-toggle-visibility))
  5517. ((org-try-cdlatex-tab))
  5518. ;; At an item/headline: delegate to `org-cycle-internal-local'.
  5519. ((and (or (and org-cycle-include-plain-lists (org-at-item-p))
  5520. (save-excursion (beginning-of-line 1)
  5521. (looking-at org-outline-regexp)))
  5522. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5523. (org-cycle-internal-local))
  5524. ;; From there: TAB emulation and template completion.
  5525. (buffer-read-only (org-back-to-heading))
  5526. ((run-hook-with-args-until-success
  5527. 'org-tab-after-check-for-cycling-hook))
  5528. ((org-try-structure-completion))
  5529. ((run-hook-with-args-until-success
  5530. 'org-tab-before-tab-emulation-hook))
  5531. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  5532. (or (not (bolp))
  5533. (not (looking-at org-outline-regexp))))
  5534. (call-interactively (global-key-binding "\t")))
  5535. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  5536. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  5537. (or (and (eq org-cycle-emulate-tab 'white)
  5538. (= (match-end 0) (point-at-eol)))
  5539. (and (eq org-cycle-emulate-tab 'whitestart)
  5540. (>= (match-end 0) pos))))
  5541. t
  5542. (eq org-cycle-emulate-tab t))
  5543. (call-interactively (global-key-binding "\t")))
  5544. (t (save-excursion
  5545. (org-back-to-heading)
  5546. (org-cycle)))))))
  5547. (defun org-cycle-internal-global ()
  5548. "Do the global cycling action."
  5549. ;; Hack to avoid display of messages for .org attachments in Gnus
  5550. (let ((ga (string-match "\\*fontification" (buffer-name))))
  5551. (cond
  5552. ((and (eq last-command this-command)
  5553. (eq org-cycle-global-status 'overview))
  5554. ;; We just created the overview - now do table of contents
  5555. ;; This can be slow in very large buffers, so indicate action
  5556. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  5557. (unless ga (message "CONTENTS..."))
  5558. (org-content)
  5559. (unless ga (message "CONTENTS...done"))
  5560. (setq org-cycle-global-status 'contents)
  5561. (run-hook-with-args 'org-cycle-hook 'contents))
  5562. ((and (eq last-command this-command)
  5563. (eq org-cycle-global-status 'contents))
  5564. ;; We just showed the table of contents - now show everything
  5565. (run-hook-with-args 'org-pre-cycle-hook 'all)
  5566. (show-all)
  5567. (unless ga (message "SHOW ALL"))
  5568. (setq org-cycle-global-status 'all)
  5569. (run-hook-with-args 'org-cycle-hook 'all))
  5570. (t
  5571. ;; Default action: go to overview
  5572. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  5573. (org-overview)
  5574. (unless ga (message "OVERVIEW"))
  5575. (setq org-cycle-global-status 'overview)
  5576. (run-hook-with-args 'org-cycle-hook 'overview)))))
  5577. (defun org-cycle-internal-local ()
  5578. "Do the local cycling action."
  5579. (let ((goal-column 0) eoh eol eos has-children children-skipped struct)
  5580. ;; First, determine end of headline (EOH), end of subtree or item
  5581. ;; (EOS), and if item or heading has children (HAS-CHILDREN).
  5582. (save-excursion
  5583. (if (org-at-item-p)
  5584. (progn
  5585. (beginning-of-line)
  5586. (setq struct (org-list-struct))
  5587. (setq eoh (point-at-eol))
  5588. (setq eos (org-list-get-item-end-before-blank (point) struct))
  5589. (setq has-children (org-list-has-child-p (point) struct)))
  5590. (org-back-to-heading)
  5591. (setq eoh (save-excursion (outline-end-of-heading) (point)))
  5592. (setq eos (save-excursion
  5593. (org-end-of-subtree t)
  5594. (unless (eobp)
  5595. (skip-chars-forward " \t\n"))
  5596. (if (eobp) (point) (1- (point)))))
  5597. (setq has-children
  5598. (or (save-excursion
  5599. (let ((level (funcall outline-level)))
  5600. (outline-next-heading)
  5601. (and (org-at-heading-p t)
  5602. (> (funcall outline-level) level))))
  5603. (save-excursion
  5604. (org-list-search-forward (org-item-beginning-re) eos t)))))
  5605. ;; Determine end invisible part of buffer (EOL)
  5606. (beginning-of-line 2)
  5607. ;; XEmacs doesn't have `next-single-char-property-change'
  5608. (if (featurep 'xemacs)
  5609. (while (and (not (eobp)) ;; this is like `next-line'
  5610. (get-char-property (1- (point)) 'invisible))
  5611. (beginning-of-line 2))
  5612. (while (and (not (eobp)) ;; this is like `next-line'
  5613. (get-char-property (1- (point)) 'invisible))
  5614. (goto-char (next-single-char-property-change (point) 'invisible))
  5615. (and (eolp) (beginning-of-line 2))))
  5616. (setq eol (point)))
  5617. ;; Find out what to do next and set `this-command'
  5618. (cond
  5619. ((= eos eoh)
  5620. ;; Nothing is hidden behind this heading
  5621. (run-hook-with-args 'org-pre-cycle-hook 'empty)
  5622. (message "EMPTY ENTRY")
  5623. (setq org-cycle-subtree-status nil)
  5624. (save-excursion
  5625. (goto-char eos)
  5626. (outline-next-heading)
  5627. (if (outline-invisible-p) (org-flag-heading nil))))
  5628. ((and (or (>= eol eos)
  5629. (not (string-match "\\S-" (buffer-substring eol eos))))
  5630. (or has-children
  5631. (not (setq children-skipped
  5632. org-cycle-skip-children-state-if-no-children))))
  5633. ;; Entire subtree is hidden in one line: children view
  5634. (run-hook-with-args 'org-pre-cycle-hook 'children)
  5635. (if (org-at-item-p)
  5636. (org-list-set-item-visibility (point-at-bol) struct 'children)
  5637. (org-show-entry)
  5638. (org-with-limited-levels (show-children))
  5639. ;; FIXME: This slows down the func way too much.
  5640. ;; How keep drawers hidden in subtree anyway?
  5641. ;; (when (memq 'org-cycle-hide-drawers org-cycle-hook)
  5642. ;; (org-cycle-hide-drawers 'subtree))
  5643. ;; Fold every list in subtree to top-level items.
  5644. (when (eq org-cycle-include-plain-lists 'integrate)
  5645. (save-excursion
  5646. (org-back-to-heading)
  5647. (while (org-list-search-forward (org-item-beginning-re) eos t)
  5648. (beginning-of-line 1)
  5649. (let* ((struct (org-list-struct))
  5650. (prevs (org-list-prevs-alist struct))
  5651. (end (org-list-get-bottom-point struct)))
  5652. (mapc (lambda (e) (org-list-set-item-visibility e struct 'folded))
  5653. (org-list-get-all-items (point) struct prevs))
  5654. (goto-char end))))))
  5655. (message "CHILDREN")
  5656. (save-excursion
  5657. (goto-char eos)
  5658. (outline-next-heading)
  5659. (if (outline-invisible-p) (org-flag-heading nil)))
  5660. (setq org-cycle-subtree-status 'children)
  5661. (run-hook-with-args 'org-cycle-hook 'children))
  5662. ((or children-skipped
  5663. (and (eq last-command this-command)
  5664. (eq org-cycle-subtree-status 'children)))
  5665. ;; We just showed the children, or no children are there,
  5666. ;; now show everything.
  5667. (run-hook-with-args 'org-pre-cycle-hook 'subtree)
  5668. (outline-flag-region eoh eos nil)
  5669. (message (if children-skipped "SUBTREE (NO CHILDREN)" "SUBTREE"))
  5670. (setq org-cycle-subtree-status 'subtree)
  5671. (run-hook-with-args 'org-cycle-hook 'subtree))
  5672. (t
  5673. ;; Default action: hide the subtree.
  5674. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  5675. (outline-flag-region eoh eos t)
  5676. (message "FOLDED")
  5677. (setq org-cycle-subtree-status 'folded)
  5678. (run-hook-with-args 'org-cycle-hook 'folded)))))
  5679. ;;;###autoload
  5680. (defun org-global-cycle (&optional arg)
  5681. "Cycle the global visibility. For details see `org-cycle'.
  5682. With \\[universal-argument] prefix arg, switch to startup visibility.
  5683. With a numeric prefix, show all headlines up to that level."
  5684. (interactive "P")
  5685. (let ((org-cycle-include-plain-lists
  5686. (if (derived-mode-p 'org-mode) org-cycle-include-plain-lists nil)))
  5687. (cond
  5688. ((integerp arg)
  5689. (show-all)
  5690. (hide-sublevels arg)
  5691. (setq org-cycle-global-status 'contents))
  5692. ((equal arg '(4))
  5693. (org-set-startup-visibility)
  5694. (message "Startup visibility, plus VISIBILITY properties."))
  5695. (t
  5696. (org-cycle '(4))))))
  5697. (defun org-set-startup-visibility ()
  5698. "Set the visibility required by startup options and properties."
  5699. (cond
  5700. ((eq org-startup-folded t)
  5701. (org-cycle '(4)))
  5702. ((eq org-startup-folded 'content)
  5703. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  5704. (org-cycle '(4)) (org-cycle '(4)))))
  5705. (unless (eq org-startup-folded 'showeverything)
  5706. (if org-hide-block-startup (org-hide-block-all))
  5707. (org-set-visibility-according-to-property 'no-cleanup)
  5708. (org-cycle-hide-archived-subtrees 'all)
  5709. (org-cycle-hide-drawers 'all)
  5710. (org-cycle-show-empty-lines t)))
  5711. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  5712. "Switch subtree visibilities according to :VISIBILITY: property."
  5713. (interactive)
  5714. (let (org-show-entry-below state)
  5715. (save-excursion
  5716. (goto-char (point-min))
  5717. (while (re-search-forward
  5718. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  5719. nil t)
  5720. (setq state (match-string 1))
  5721. (save-excursion
  5722. (org-back-to-heading t)
  5723. (hide-subtree)
  5724. (org-reveal)
  5725. (cond
  5726. ((equal state '("fold" "folded"))
  5727. (hide-subtree))
  5728. ((equal state "children")
  5729. (org-show-hidden-entry)
  5730. (show-children))
  5731. ((equal state "content")
  5732. (save-excursion
  5733. (save-restriction
  5734. (org-narrow-to-subtree)
  5735. (org-content))))
  5736. ((member state '("all" "showall"))
  5737. (show-subtree)))))
  5738. (unless no-cleanup
  5739. (org-cycle-hide-archived-subtrees 'all)
  5740. (org-cycle-hide-drawers 'all)
  5741. (org-cycle-show-empty-lines 'all)))))
  5742. ;; This function uses outline-regexp instead of the more fundamental
  5743. ;; org-outline-regexp so that org-cycle-global works outside of Org
  5744. ;; buffers, where outline-regexp is needed.
  5745. (defun org-overview ()
  5746. "Switch to overview mode, showing only top-level headlines.
  5747. Really, this shows all headlines with level equal or greater than the level
  5748. of the first headline in the buffer. This is important, because if the
  5749. first headline is not level one, then (hide-sublevels 1) gives confusing
  5750. results."
  5751. (interactive)
  5752. (let ((level (save-excursion
  5753. (goto-char (point-min))
  5754. (if (re-search-forward (concat "^" outline-regexp) nil t)
  5755. (progn
  5756. (goto-char (match-beginning 0))
  5757. (funcall outline-level))))))
  5758. (and level (hide-sublevels level))))
  5759. (defun org-content (&optional arg)
  5760. "Show all headlines in the buffer, like a table of contents.
  5761. With numerical argument N, show content up to level N."
  5762. (interactive "P")
  5763. (save-excursion
  5764. ;; Visit all headings and show their offspring
  5765. (and (integerp arg) (org-overview))
  5766. (goto-char (point-max))
  5767. (catch 'exit
  5768. (while (and (progn (condition-case nil
  5769. (outline-previous-visible-heading 1)
  5770. (error (goto-char (point-min))))
  5771. t)
  5772. (looking-at org-outline-regexp))
  5773. (if (integerp arg)
  5774. (show-children (1- arg))
  5775. (show-branches))
  5776. (if (bobp) (throw 'exit nil))))))
  5777. (defun org-optimize-window-after-visibility-change (state)
  5778. "Adjust the window after a change in outline visibility.
  5779. This function is the default value of the hook `org-cycle-hook'."
  5780. (when (get-buffer-window (current-buffer))
  5781. (cond
  5782. ((eq state 'content) nil)
  5783. ((eq state 'all) nil)
  5784. ((eq state 'folded) nil)
  5785. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  5786. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  5787. (defun org-remove-empty-overlays-at (pos)
  5788. "Remove outline overlays that do not contain non-white stuff."
  5789. (mapc
  5790. (lambda (o)
  5791. (and (eq 'outline (overlay-get o 'invisible))
  5792. (not (string-match "\\S-" (buffer-substring (overlay-start o)
  5793. (overlay-end o))))
  5794. (delete-overlay o)))
  5795. (overlays-at pos)))
  5796. (defun org-clean-visibility-after-subtree-move ()
  5797. "Fix visibility issues after moving a subtree."
  5798. ;; First, find a reasonable region to look at:
  5799. ;; Start two siblings above, end three below
  5800. (let* ((beg (save-excursion
  5801. (and (org-get-last-sibling)
  5802. (org-get-last-sibling))
  5803. (point)))
  5804. (end (save-excursion
  5805. (and (org-get-next-sibling)
  5806. (org-get-next-sibling)
  5807. (org-get-next-sibling))
  5808. (if (org-at-heading-p)
  5809. (point-at-eol)
  5810. (point))))
  5811. (level (looking-at "\\*+"))
  5812. (re (if level (concat "^" (regexp-quote (match-string 0)) " "))))
  5813. (save-excursion
  5814. (save-restriction
  5815. (narrow-to-region beg end)
  5816. (when re
  5817. ;; Properly fold already folded siblings
  5818. (goto-char (point-min))
  5819. (while (re-search-forward re nil t)
  5820. (if (and (not (outline-invisible-p))
  5821. (save-excursion
  5822. (goto-char (point-at-eol)) (outline-invisible-p)))
  5823. (hide-entry))))
  5824. (org-cycle-show-empty-lines 'overview)
  5825. (org-cycle-hide-drawers 'overview)))))
  5826. (defun org-cycle-show-empty-lines (state)
  5827. "Show empty lines above all visible headlines.
  5828. The region to be covered depends on STATE when called through
  5829. `org-cycle-hook'. Lisp program can use t for STATE to get the
  5830. entire buffer covered. Note that an empty line is only shown if there
  5831. are at least `org-cycle-separator-lines' empty lines before the headline."
  5832. (when (not (= org-cycle-separator-lines 0))
  5833. (save-excursion
  5834. (let* ((n (abs org-cycle-separator-lines))
  5835. (re (cond
  5836. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  5837. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  5838. (t (let ((ns (number-to-string (- n 2))))
  5839. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  5840. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  5841. beg end b e)
  5842. (cond
  5843. ((memq state '(overview contents t))
  5844. (setq beg (point-min) end (point-max)))
  5845. ((memq state '(children folded))
  5846. (setq beg (point) end (progn (org-end-of-subtree t t)
  5847. (beginning-of-line 2)
  5848. (point)))))
  5849. (when beg
  5850. (goto-char beg)
  5851. (while (re-search-forward re end t)
  5852. (unless (get-char-property (match-end 1) 'invisible)
  5853. (setq e (match-end 1))
  5854. (if (< org-cycle-separator-lines 0)
  5855. (setq b (save-excursion
  5856. (goto-char (match-beginning 0))
  5857. (org-back-over-empty-lines)
  5858. (if (save-excursion
  5859. (goto-char (max (point-min) (1- (point))))
  5860. (org-at-heading-p))
  5861. (1- (point))
  5862. (point))))
  5863. (setq b (match-beginning 1)))
  5864. (outline-flag-region b e nil)))))))
  5865. ;; Never hide empty lines at the end of the file.
  5866. (save-excursion
  5867. (goto-char (point-max))
  5868. (outline-previous-heading)
  5869. (outline-end-of-heading)
  5870. (if (and (looking-at "[ \t\n]+")
  5871. (= (match-end 0) (point-max)))
  5872. (outline-flag-region (point) (match-end 0) nil))))
  5873. (defun org-show-empty-lines-in-parent ()
  5874. "Move to the parent and re-show empty lines before visible headlines."
  5875. (save-excursion
  5876. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  5877. (org-cycle-show-empty-lines context))))
  5878. (defun org-files-list ()
  5879. "Return `org-agenda-files' list, plus all open org-mode files.
  5880. This is useful for operations that need to scan all of a user's
  5881. open and agenda-wise Org files."
  5882. (let ((files (mapcar 'expand-file-name (org-agenda-files))))
  5883. (dolist (buf (buffer-list))
  5884. (with-current-buffer buf
  5885. (if (and (derived-mode-p 'org-mode) (buffer-file-name))
  5886. (let ((file (expand-file-name (buffer-file-name))))
  5887. (unless (member file files)
  5888. (push file files))))))
  5889. files))
  5890. (defsubst org-entry-beginning-position ()
  5891. "Return the beginning position of the current entry."
  5892. (save-excursion (outline-back-to-heading t) (point)))
  5893. (defsubst org-entry-end-position ()
  5894. "Return the end position of the current entry."
  5895. (save-excursion (outline-next-heading) (point)))
  5896. (defun org-cycle-hide-drawers (state)
  5897. "Re-hide all drawers after a visibility state change."
  5898. (when (and (derived-mode-p 'org-mode)
  5899. (not (memq state '(overview folded contents))))
  5900. (save-excursion
  5901. (let* ((globalp (memq state '(contents all)))
  5902. (beg (if globalp (point-min) (point)))
  5903. (end (if globalp (point-max)
  5904. (if (eq state 'children)
  5905. (save-excursion (outline-next-heading) (point))
  5906. (org-end-of-subtree t)))))
  5907. (goto-char beg)
  5908. (while (re-search-forward org-drawer-regexp end t)
  5909. (org-flag-drawer t))))))
  5910. (defun org-cycle-hide-inline-tasks (state)
  5911. "Re-hide inline task when switching to 'contents visibility state."
  5912. (when (and (eq state 'contents)
  5913. (boundp 'org-inlinetask-min-level)
  5914. org-inlinetask-min-level)
  5915. (hide-sublevels (1- org-inlinetask-min-level))))
  5916. (defun org-flag-drawer (flag)
  5917. "When FLAG is non-nil, hide the drawer we are within.
  5918. Otherwise make it visible."
  5919. (save-excursion
  5920. (beginning-of-line 1)
  5921. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  5922. (let ((b (match-end 0)))
  5923. (if (re-search-forward
  5924. "^[ \t]*:END:"
  5925. (save-excursion (outline-next-heading) (point)) t)
  5926. (outline-flag-region b (point-at-eol) flag)
  5927. (error ":END: line missing at position %s" b))))))
  5928. (defun org-subtree-end-visible-p ()
  5929. "Is the end of the current subtree visible?"
  5930. (pos-visible-in-window-p
  5931. (save-excursion (org-end-of-subtree t) (point))))
  5932. (defun org-first-headline-recenter (&optional N)
  5933. "Move cursor to the first headline and recenter the headline.
  5934. Optional argument N means put the headline into the Nth line of the window."
  5935. (goto-char (point-min))
  5936. (when (re-search-forward (concat "^\\(" org-outline-regexp "\\)") nil t)
  5937. (beginning-of-line)
  5938. (recenter (prefix-numeric-value N))))
  5939. ;;; Saving and restoring visibility
  5940. (defun org-outline-overlay-data (&optional use-markers)
  5941. "Return a list of the locations of all outline overlays.
  5942. These are overlays with the `invisible' property value `outline'.
  5943. The return value is a list of cons cells, with start and stop
  5944. positions for each overlay.
  5945. If USE-MARKERS is set, return the positions as markers."
  5946. (let (beg end)
  5947. (save-excursion
  5948. (save-restriction
  5949. (widen)
  5950. (delq nil
  5951. (mapcar (lambda (o)
  5952. (when (eq (overlay-get o 'invisible) 'outline)
  5953. (setq beg (overlay-start o)
  5954. end (overlay-end o))
  5955. (and beg end (> end beg)
  5956. (if use-markers
  5957. (cons (move-marker (make-marker) beg)
  5958. (move-marker (make-marker) end))
  5959. (cons beg end)))))
  5960. (overlays-in (point-min) (point-max))))))))
  5961. (defun org-set-outline-overlay-data (data)
  5962. "Create visibility overlays for all positions in DATA.
  5963. DATA should have been made by `org-outline-overlay-data'."
  5964. (let (o)
  5965. (save-excursion
  5966. (save-restriction
  5967. (widen)
  5968. (show-all)
  5969. (mapc (lambda (c)
  5970. (outline-flag-region (car c) (cdr c) t))
  5971. data)))))
  5972. ;;; Folding of blocks
  5973. (defvar org-hide-block-overlays nil
  5974. "Overlays hiding blocks.")
  5975. (make-variable-buffer-local 'org-hide-block-overlays)
  5976. (defun org-block-map (function &optional start end)
  5977. "Call FUNCTION at the head of all source blocks in the current buffer.
  5978. Optional arguments START and END can be used to limit the range."
  5979. (let ((start (or start (point-min)))
  5980. (end (or end (point-max))))
  5981. (save-excursion
  5982. (goto-char start)
  5983. (while (and (< (point) end) (re-search-forward org-block-regexp end t))
  5984. (save-excursion
  5985. (save-match-data
  5986. (goto-char (match-beginning 0))
  5987. (funcall function)))))))
  5988. (defun org-hide-block-toggle-all ()
  5989. "Toggle the visibility of all blocks in the current buffer."
  5990. (org-block-map #'org-hide-block-toggle))
  5991. (defun org-hide-block-all ()
  5992. "Fold all blocks in the current buffer."
  5993. (interactive)
  5994. (org-show-block-all)
  5995. (org-block-map #'org-hide-block-toggle-maybe))
  5996. (defun org-show-block-all ()
  5997. "Unfold all blocks in the current buffer."
  5998. (interactive)
  5999. (mapc 'delete-overlay org-hide-block-overlays)
  6000. (setq org-hide-block-overlays nil))
  6001. (defun org-hide-block-toggle-maybe ()
  6002. "Toggle visibility of block at point."
  6003. (interactive)
  6004. (let ((case-fold-search t))
  6005. (if (save-excursion
  6006. (beginning-of-line 1)
  6007. (looking-at org-block-regexp))
  6008. (progn (org-hide-block-toggle)
  6009. t) ;; to signal that we took action
  6010. nil))) ;; to signal that we did not
  6011. (defun org-hide-block-toggle (&optional force)
  6012. "Toggle the visibility of the current block."
  6013. (interactive)
  6014. (save-excursion
  6015. (beginning-of-line)
  6016. (if (re-search-forward org-block-regexp nil t)
  6017. (let ((start (- (match-beginning 4) 1)) ;; beginning of body
  6018. (end (match-end 0)) ;; end of entire body
  6019. ov)
  6020. (if (memq t (mapcar (lambda (overlay)
  6021. (eq (overlay-get overlay 'invisible)
  6022. 'org-hide-block))
  6023. (overlays-at start)))
  6024. (if (or (not force) (eq force 'off))
  6025. (mapc (lambda (ov)
  6026. (when (member ov org-hide-block-overlays)
  6027. (setq org-hide-block-overlays
  6028. (delq ov org-hide-block-overlays)))
  6029. (when (eq (overlay-get ov 'invisible)
  6030. 'org-hide-block)
  6031. (delete-overlay ov)))
  6032. (overlays-at start)))
  6033. (setq ov (make-overlay start end))
  6034. (overlay-put ov 'invisible 'org-hide-block)
  6035. ;; make the block accessible to isearch
  6036. (overlay-put
  6037. ov 'isearch-open-invisible
  6038. (lambda (ov)
  6039. (when (member ov org-hide-block-overlays)
  6040. (setq org-hide-block-overlays
  6041. (delq ov org-hide-block-overlays)))
  6042. (when (eq (overlay-get ov 'invisible)
  6043. 'org-hide-block)
  6044. (delete-overlay ov))))
  6045. (push ov org-hide-block-overlays)))
  6046. (error "Not looking at a source block"))))
  6047. ;; org-tab-after-check-for-cycling-hook
  6048. (add-hook 'org-tab-first-hook 'org-hide-block-toggle-maybe)
  6049. ;; Remove overlays when changing major mode
  6050. (add-hook 'org-mode-hook
  6051. (lambda () (org-add-hook 'change-major-mode-hook
  6052. 'org-show-block-all 'append 'local)))
  6053. ;;; Org-goto
  6054. (defvar org-goto-window-configuration nil)
  6055. (defvar org-goto-marker nil)
  6056. (defvar org-goto-map
  6057. (let ((map (make-sparse-keymap)))
  6058. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  6059. (while (setq cmd (pop cmds))
  6060. (substitute-key-definition cmd cmd map global-map)))
  6061. (suppress-keymap map)
  6062. (org-defkey map "\C-m" 'org-goto-ret)
  6063. (org-defkey map [(return)] 'org-goto-ret)
  6064. (org-defkey map [(left)] 'org-goto-left)
  6065. (org-defkey map [(right)] 'org-goto-right)
  6066. (org-defkey map [(control ?g)] 'org-goto-quit)
  6067. (org-defkey map "\C-i" 'org-cycle)
  6068. (org-defkey map [(tab)] 'org-cycle)
  6069. (org-defkey map [(down)] 'outline-next-visible-heading)
  6070. (org-defkey map [(up)] 'outline-previous-visible-heading)
  6071. (if org-goto-auto-isearch
  6072. (if (fboundp 'define-key-after)
  6073. (define-key-after map [t] 'org-goto-local-auto-isearch)
  6074. nil)
  6075. (org-defkey map "q" 'org-goto-quit)
  6076. (org-defkey map "n" 'outline-next-visible-heading)
  6077. (org-defkey map "p" 'outline-previous-visible-heading)
  6078. (org-defkey map "f" 'outline-forward-same-level)
  6079. (org-defkey map "b" 'outline-backward-same-level)
  6080. (org-defkey map "u" 'outline-up-heading))
  6081. (org-defkey map "/" 'org-occur)
  6082. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  6083. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  6084. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  6085. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  6086. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  6087. map))
  6088. (defconst org-goto-help
  6089. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  6090. RET=jump to location [Q]uit and return to previous location
  6091. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  6092. (defvar org-goto-start-pos) ; dynamically scoped parameter
  6093. ;; FIXME: Docstring does not mention both interfaces
  6094. (defun org-goto (&optional alternative-interface)
  6095. "Look up a different location in the current file, keeping current visibility.
  6096. When you want look-up or go to a different location in a
  6097. document, the fastest way is often to fold the entire buffer and
  6098. then dive into the tree. This method has the disadvantage, that
  6099. the previous location will be folded, which may not be what you
  6100. want.
  6101. This command works around this by showing a copy of the current
  6102. buffer in an indirect buffer, in overview mode. You can dive
  6103. into the tree in that copy, use org-occur and incremental search
  6104. to find a location. When pressing RET or `Q', the command
  6105. returns to the original buffer in which the visibility is still
  6106. unchanged. After RET it will also jump to the location selected
  6107. in the indirect buffer and expose the headline hierarchy above.
  6108. With a prefix argument, use the alternative interface: e.g. if
  6109. `org-goto-interface' is 'outline use 'outline-path-completion."
  6110. (interactive "P")
  6111. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  6112. (org-refile-use-outline-path t)
  6113. (org-refile-target-verify-function nil)
  6114. (interface
  6115. (if (not alternative-interface)
  6116. org-goto-interface
  6117. (if (eq org-goto-interface 'outline)
  6118. 'outline-path-completion
  6119. 'outline)))
  6120. (org-goto-start-pos (point))
  6121. (selected-point
  6122. (if (eq interface 'outline)
  6123. (car (org-get-location (current-buffer) org-goto-help))
  6124. (let ((pa (org-refile-get-location "Goto" nil nil t)))
  6125. (org-refile-check-position pa)
  6126. (nth 3 pa)))))
  6127. (if selected-point
  6128. (progn
  6129. (org-mark-ring-push org-goto-start-pos)
  6130. (goto-char selected-point)
  6131. (if (or (outline-invisible-p) (org-invisible-p2))
  6132. (org-show-context 'org-goto)))
  6133. (message "Quit"))))
  6134. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  6135. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  6136. (defvar org-goto-local-auto-isearch-map) ; defined below
  6137. (defun org-get-location (buf help)
  6138. "Let the user select a location in the Org-mode buffer BUF.
  6139. This function uses a recursive edit. It returns the selected position
  6140. or nil."
  6141. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  6142. (isearch-hide-immediately nil)
  6143. (isearch-search-fun-function
  6144. (lambda () 'org-goto-local-search-headings))
  6145. (org-goto-selected-point org-goto-exit-command)
  6146. (pop-up-frames nil)
  6147. (special-display-buffer-names nil)
  6148. (special-display-regexps nil)
  6149. (special-display-function nil))
  6150. (save-excursion
  6151. (save-window-excursion
  6152. (delete-other-windows)
  6153. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  6154. (org-pop-to-buffer-same-window
  6155. (condition-case nil
  6156. (make-indirect-buffer (current-buffer) "*org-goto*")
  6157. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  6158. (with-output-to-temp-buffer "*Help*"
  6159. (princ help))
  6160. (org-fit-window-to-buffer (get-buffer-window "*Help*"))
  6161. (setq buffer-read-only nil)
  6162. (let ((org-startup-truncated t)
  6163. (org-startup-folded nil)
  6164. (org-startup-align-all-tables nil))
  6165. (org-mode)
  6166. (org-overview))
  6167. (setq buffer-read-only t)
  6168. (if (and (boundp 'org-goto-start-pos)
  6169. (integer-or-marker-p org-goto-start-pos))
  6170. (let ((org-show-hierarchy-above t)
  6171. (org-show-siblings t)
  6172. (org-show-following-heading t))
  6173. (goto-char org-goto-start-pos)
  6174. (and (outline-invisible-p) (org-show-context)))
  6175. (goto-char (point-min)))
  6176. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  6177. (message "Select location and press RET")
  6178. (use-local-map org-goto-map)
  6179. (recursive-edit)
  6180. ))
  6181. (kill-buffer "*org-goto*")
  6182. (cons org-goto-selected-point org-goto-exit-command)))
  6183. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  6184. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  6185. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  6186. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  6187. (defun org-goto-local-search-headings (string bound noerror)
  6188. "Search and make sure that any matches are in headlines."
  6189. (catch 'return
  6190. (while (if isearch-forward
  6191. (search-forward string bound noerror)
  6192. (search-backward string bound noerror))
  6193. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  6194. (and (member :headline context)
  6195. (not (member :tags context))))
  6196. (throw 'return (point))))))
  6197. (defun org-goto-local-auto-isearch ()
  6198. "Start isearch."
  6199. (interactive)
  6200. (goto-char (point-min))
  6201. (let ((keys (this-command-keys)))
  6202. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  6203. (isearch-mode t)
  6204. (isearch-process-search-char (string-to-char keys)))))
  6205. (defun org-goto-ret (&optional arg)
  6206. "Finish `org-goto' by going to the new location."
  6207. (interactive "P")
  6208. (setq org-goto-selected-point (point)
  6209. org-goto-exit-command 'return)
  6210. (throw 'exit nil))
  6211. (defun org-goto-left ()
  6212. "Finish `org-goto' by going to the new location."
  6213. (interactive)
  6214. (if (org-at-heading-p)
  6215. (progn
  6216. (beginning-of-line 1)
  6217. (setq org-goto-selected-point (point)
  6218. org-goto-exit-command 'left)
  6219. (throw 'exit nil))
  6220. (error "Not on a heading")))
  6221. (defun org-goto-right ()
  6222. "Finish `org-goto' by going to the new location."
  6223. (interactive)
  6224. (if (org-at-heading-p)
  6225. (progn
  6226. (setq org-goto-selected-point (point)
  6227. org-goto-exit-command 'right)
  6228. (throw 'exit nil))
  6229. (error "Not on a heading")))
  6230. (defun org-goto-quit ()
  6231. "Finish `org-goto' without cursor motion."
  6232. (interactive)
  6233. (setq org-goto-selected-point nil)
  6234. (setq org-goto-exit-command 'quit)
  6235. (throw 'exit nil))
  6236. ;;; Indirect buffer display of subtrees
  6237. (defvar org-indirect-dedicated-frame nil
  6238. "This is the frame being used for indirect tree display.")
  6239. (defvar org-last-indirect-buffer nil)
  6240. (defun org-tree-to-indirect-buffer (&optional arg)
  6241. "Create indirect buffer and narrow it to current subtree.
  6242. With a numerical prefix ARG, go up to this level and then take that tree.
  6243. If ARG is negative, go up that many levels.
  6244. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  6245. indirect buffer previously made with this command, to avoid proliferation of
  6246. indirect buffers. However, when you call the command with a \
  6247. \\[universal-argument] prefix, or
  6248. when `org-indirect-buffer-display' is `new-frame', the last buffer
  6249. is kept so that you can work with several indirect buffers at the same time.
  6250. If `org-indirect-buffer-display' is `dedicated-frame', the \
  6251. \\[universal-argument] prefix also
  6252. requests that a new frame be made for the new buffer, so that the dedicated
  6253. frame is not changed."
  6254. (interactive "P")
  6255. (let ((cbuf (current-buffer))
  6256. (cwin (selected-window))
  6257. (pos (point))
  6258. beg end level heading ibuf)
  6259. (save-excursion
  6260. (org-back-to-heading t)
  6261. (when (numberp arg)
  6262. (setq level (org-outline-level))
  6263. (if (< arg 0) (setq arg (+ level arg)))
  6264. (while (> (setq level (org-outline-level)) arg)
  6265. (org-up-heading-safe)))
  6266. (setq beg (point)
  6267. heading (org-get-heading))
  6268. (org-end-of-subtree t t)
  6269. (if (org-at-heading-p) (backward-char 1))
  6270. (setq end (point)))
  6271. (if (and (buffer-live-p org-last-indirect-buffer)
  6272. (not (eq org-indirect-buffer-display 'new-frame))
  6273. (not arg))
  6274. (kill-buffer org-last-indirect-buffer))
  6275. (setq ibuf (org-get-indirect-buffer cbuf)
  6276. org-last-indirect-buffer ibuf)
  6277. (cond
  6278. ((or (eq org-indirect-buffer-display 'new-frame)
  6279. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  6280. (select-frame (make-frame))
  6281. (delete-other-windows)
  6282. (org-pop-to-buffer-same-window ibuf)
  6283. (org-set-frame-title heading))
  6284. ((eq org-indirect-buffer-display 'dedicated-frame)
  6285. (raise-frame
  6286. (select-frame (or (and org-indirect-dedicated-frame
  6287. (frame-live-p org-indirect-dedicated-frame)
  6288. org-indirect-dedicated-frame)
  6289. (setq org-indirect-dedicated-frame (make-frame)))))
  6290. (delete-other-windows)
  6291. (org-pop-to-buffer-same-window ibuf)
  6292. (org-set-frame-title (concat "Indirect: " heading)))
  6293. ((eq org-indirect-buffer-display 'current-window)
  6294. (org-pop-to-buffer-same-window ibuf))
  6295. ((eq org-indirect-buffer-display 'other-window)
  6296. (pop-to-buffer ibuf))
  6297. (t (error "Invalid value")))
  6298. (if (featurep 'xemacs)
  6299. (save-excursion (org-mode) (turn-on-font-lock)))
  6300. (narrow-to-region beg end)
  6301. (show-all)
  6302. (goto-char pos)
  6303. (run-hook-with-args 'org-cycle-hook 'all)
  6304. (and (window-live-p cwin) (select-window cwin))))
  6305. (defun org-get-indirect-buffer (&optional buffer)
  6306. (setq buffer (or buffer (current-buffer)))
  6307. (let ((n 1) (base (buffer-name buffer)) bname)
  6308. (while (buffer-live-p
  6309. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  6310. (setq n (1+ n)))
  6311. (condition-case nil
  6312. (make-indirect-buffer buffer bname 'clone)
  6313. (error (make-indirect-buffer buffer bname)))))
  6314. (defun org-set-frame-title (title)
  6315. "Set the title of the current frame to the string TITLE."
  6316. ;; FIXME: how to name a single frame in XEmacs???
  6317. (unless (featurep 'xemacs)
  6318. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  6319. ;;;; Structure editing
  6320. ;;; Inserting headlines
  6321. (defun org-previous-line-empty-p ()
  6322. (save-excursion
  6323. (and (not (bobp))
  6324. (or (beginning-of-line 0) t)
  6325. (save-match-data
  6326. (looking-at "[ \t]*$")))))
  6327. (defun org-insert-heading (&optional force-heading invisible-ok)
  6328. "Insert a new heading or item with same depth at point.
  6329. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  6330. If point is at the beginning of a headline, insert a sibling before the
  6331. current headline. If point is not at the beginning, split the line,
  6332. create the new headline with the text in the current line after point
  6333. \(but see also the variable `org-M-RET-may-split-line').
  6334. When INVISIBLE-OK is set, stop at invisible headlines when going back.
  6335. This is important for non-interactive uses of the command."
  6336. (interactive "P")
  6337. (if (or (= (buffer-size) 0)
  6338. (and (not (save-excursion
  6339. (and (ignore-errors (org-back-to-heading invisible-ok))
  6340. (org-at-heading-p))))
  6341. (or force-heading (not (org-in-item-p)))))
  6342. (progn
  6343. (insert "\n* ")
  6344. (run-hooks 'org-insert-heading-hook))
  6345. (when (or force-heading (not (org-insert-item)))
  6346. (let* ((empty-line-p nil)
  6347. (level nil)
  6348. (on-heading (org-at-heading-p))
  6349. (head (save-excursion
  6350. (condition-case nil
  6351. (progn
  6352. (org-back-to-heading invisible-ok)
  6353. (when (and (not on-heading)
  6354. (featurep 'org-inlinetask)
  6355. (integerp org-inlinetask-min-level)
  6356. (>= (length (match-string 0))
  6357. org-inlinetask-min-level))
  6358. ;; Find a heading level before the inline task
  6359. (while (and (setq level (org-up-heading-safe))
  6360. (>= level org-inlinetask-min-level)))
  6361. (if (org-at-heading-p)
  6362. (org-back-to-heading invisible-ok)
  6363. (error "This should not happen")))
  6364. (setq empty-line-p (org-previous-line-empty-p))
  6365. (match-string 0))
  6366. (error "*"))))
  6367. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  6368. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  6369. pos hide-previous previous-pos)
  6370. (cond
  6371. ((and (org-at-heading-p) (bolp)
  6372. (or (bobp)
  6373. (save-excursion (backward-char 1) (not (outline-invisible-p)))))
  6374. ;; insert before the current line
  6375. (open-line (if blank 2 1)))
  6376. ((and (bolp)
  6377. (not org-insert-heading-respect-content)
  6378. (or (bobp)
  6379. (save-excursion
  6380. (backward-char 1) (not (outline-invisible-p)))))
  6381. ;; insert right here
  6382. nil)
  6383. (t
  6384. ;; somewhere in the line
  6385. (save-excursion
  6386. (setq previous-pos (point-at-bol))
  6387. (end-of-line)
  6388. (setq hide-previous (outline-invisible-p)))
  6389. (and org-insert-heading-respect-content (org-show-subtree))
  6390. (let ((split
  6391. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  6392. (save-excursion
  6393. (let ((p (point)))
  6394. (goto-char (point-at-bol))
  6395. (and (looking-at org-complex-heading-regexp)
  6396. (match-beginning 4)
  6397. (> p (match-beginning 4)))))))
  6398. tags pos)
  6399. (cond
  6400. (org-insert-heading-respect-content
  6401. (org-end-of-subtree nil t)
  6402. (when (featurep 'org-inlinetask)
  6403. (while (and (not (eobp))
  6404. (looking-at "\\(\\*+\\)[ \t]+")
  6405. (>= (length (match-string 1))
  6406. org-inlinetask-min-level))
  6407. (org-end-of-subtree nil t)))
  6408. (or (bolp) (newline))
  6409. (or (org-previous-line-empty-p)
  6410. (and blank (newline)))
  6411. (open-line 1))
  6412. ((org-at-heading-p)
  6413. (when hide-previous
  6414. (show-children)
  6415. (org-show-entry))
  6416. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?[ \t]*$")
  6417. (setq tags (and (match-end 2) (match-string 2)))
  6418. (and (match-end 1)
  6419. (delete-region (match-beginning 1) (match-end 1)))
  6420. (setq pos (point-at-bol))
  6421. (or split (end-of-line 1))
  6422. (delete-horizontal-space)
  6423. (if (string-match "\\`\\*+\\'"
  6424. (buffer-substring (point-at-bol) (point)))
  6425. (insert " "))
  6426. (newline (if blank 2 1))
  6427. (when tags
  6428. (save-excursion
  6429. (goto-char pos)
  6430. (end-of-line 1)
  6431. (insert " " tags)
  6432. (org-set-tags nil 'align))))
  6433. (t
  6434. (or split (end-of-line 1))
  6435. (newline (if blank 2 1)))))))
  6436. (insert head) (just-one-space)
  6437. (setq pos (point))
  6438. (end-of-line 1)
  6439. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  6440. (when (and org-insert-heading-respect-content hide-previous)
  6441. (save-excursion
  6442. (goto-char previous-pos)
  6443. (hide-subtree)))
  6444. (run-hooks 'org-insert-heading-hook)))))
  6445. (defun org-get-heading (&optional no-tags no-todo)
  6446. "Return the heading of the current entry, without the stars.
  6447. When NO-TAGS is non-nil, don't include tags.
  6448. When NO-TODO is non-nil, don't include TODO keywords."
  6449. (save-excursion
  6450. (org-back-to-heading t)
  6451. (cond
  6452. ((and no-tags no-todo)
  6453. (looking-at org-complex-heading-regexp)
  6454. (match-string 4))
  6455. (no-tags
  6456. (looking-at (concat org-outline-regexp
  6457. "\\(.*?\\)"
  6458. "\\(?:[ \t]+:[[:alnum:]:_@#%]+:\\)?[ \t]*$"))
  6459. (match-string 1))
  6460. (no-todo
  6461. (looking-at org-todo-line-regexp)
  6462. (match-string 3))
  6463. (t (looking-at org-heading-regexp)
  6464. (match-string 2)))))
  6465. (defun org-heading-components ()
  6466. "Return the components of the current heading.
  6467. This is a list with the following elements:
  6468. - the level as an integer
  6469. - the reduced level, different if `org-odd-levels-only' is set.
  6470. - the TODO keyword, or nil
  6471. - the priority character, like ?A, or nil if no priority is given
  6472. - the headline text itself, or the tags string if no headline text
  6473. - the tags string, or nil."
  6474. (save-excursion
  6475. (org-back-to-heading t)
  6476. (if (let (case-fold-search) (looking-at org-complex-heading-regexp))
  6477. (list (length (match-string 1))
  6478. (org-reduced-level (length (match-string 1)))
  6479. (org-match-string-no-properties 2)
  6480. (and (match-end 3) (aref (match-string 3) 2))
  6481. (org-match-string-no-properties 4)
  6482. (org-match-string-no-properties 5)))))
  6483. (defun org-get-entry ()
  6484. "Get the entry text, after heading, entire subtree."
  6485. (save-excursion
  6486. (org-back-to-heading t)
  6487. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  6488. (defun org-insert-heading-after-current ()
  6489. "Insert a new heading with same level as current, after current subtree."
  6490. (interactive)
  6491. (org-back-to-heading)
  6492. (org-insert-heading)
  6493. (org-move-subtree-down)
  6494. (end-of-line 1))
  6495. (defun org-insert-heading-respect-content ()
  6496. (interactive)
  6497. (let ((org-insert-heading-respect-content t))
  6498. (org-insert-heading t)))
  6499. (defun org-insert-todo-heading-respect-content (&optional force-state)
  6500. (interactive "P")
  6501. (let ((org-insert-heading-respect-content t))
  6502. (org-insert-todo-heading force-state t)))
  6503. (defun org-insert-todo-heading (arg &optional force-heading)
  6504. "Insert a new heading with the same level and TODO state as current heading.
  6505. If the heading has no TODO state, or if the state is DONE, use the first
  6506. state (TODO by default). Also with prefix arg, force first state."
  6507. (interactive "P")
  6508. (when (or force-heading (not (org-insert-item 'checkbox)))
  6509. (org-insert-heading force-heading)
  6510. (save-excursion
  6511. (org-back-to-heading)
  6512. (outline-previous-heading)
  6513. (looking-at org-todo-line-regexp))
  6514. (let*
  6515. ((new-mark-x
  6516. (if (or arg
  6517. (not (match-beginning 2))
  6518. (member (match-string 2) org-done-keywords))
  6519. (car org-todo-keywords-1)
  6520. (match-string 2)))
  6521. (new-mark
  6522. (or
  6523. (run-hook-with-args-until-success
  6524. 'org-todo-get-default-hook new-mark-x nil)
  6525. new-mark-x)))
  6526. (beginning-of-line 1)
  6527. (and (looking-at org-outline-regexp) (goto-char (match-end 0))
  6528. (if org-treat-insert-todo-heading-as-state-change
  6529. (org-todo new-mark)
  6530. (insert new-mark " "))))
  6531. (when org-provide-todo-statistics
  6532. (org-update-parent-todo-statistics))))
  6533. (defun org-insert-subheading (arg)
  6534. "Insert a new subheading and demote it.
  6535. Works for outline headings and for plain lists alike."
  6536. (interactive "P")
  6537. (org-insert-heading arg)
  6538. (cond
  6539. ((org-at-heading-p) (org-do-demote))
  6540. ((org-at-item-p) (org-indent-item))))
  6541. (defun org-insert-todo-subheading (arg)
  6542. "Insert a new subheading with TODO keyword or checkbox and demote it.
  6543. Works for outline headings and for plain lists alike."
  6544. (interactive "P")
  6545. (org-insert-todo-heading arg)
  6546. (cond
  6547. ((org-at-heading-p) (org-do-demote))
  6548. ((org-at-item-p) (org-indent-item))))
  6549. ;;; Promotion and Demotion
  6550. (defvar org-after-demote-entry-hook nil
  6551. "Hook run after an entry has been demoted.
  6552. The cursor will be at the beginning of the entry.
  6553. When a subtree is being demoted, the hook will be called for each node.")
  6554. (defvar org-after-promote-entry-hook nil
  6555. "Hook run after an entry has been promoted.
  6556. The cursor will be at the beginning of the entry.
  6557. When a subtree is being promoted, the hook will be called for each node.")
  6558. (defun org-promote-subtree ()
  6559. "Promote the entire subtree.
  6560. See also `org-promote'."
  6561. (interactive)
  6562. (save-excursion
  6563. (org-with-limited-levels (org-map-tree 'org-promote)))
  6564. (org-fix-position-after-promote))
  6565. (defun org-demote-subtree ()
  6566. "Demote the entire subtree. See `org-demote'.
  6567. See also `org-promote'."
  6568. (interactive)
  6569. (save-excursion
  6570. (org-with-limited-levels (org-map-tree 'org-demote)))
  6571. (org-fix-position-after-promote))
  6572. (defun org-do-promote ()
  6573. "Promote the current heading higher up the tree.
  6574. If the region is active in `transient-mark-mode', promote all headings
  6575. in the region."
  6576. (interactive)
  6577. (save-excursion
  6578. (if (org-region-active-p)
  6579. (org-map-region 'org-promote (region-beginning) (region-end))
  6580. (org-promote)))
  6581. (org-fix-position-after-promote))
  6582. (defun org-do-demote ()
  6583. "Demote the current heading lower down the tree.
  6584. If the region is active in `transient-mark-mode', demote all headings
  6585. in the region."
  6586. (interactive)
  6587. (save-excursion
  6588. (if (org-region-active-p)
  6589. (org-map-region 'org-demote (region-beginning) (region-end))
  6590. (org-demote)))
  6591. (org-fix-position-after-promote))
  6592. (defun org-fix-position-after-promote ()
  6593. "Make sure that after pro/demotion cursor position is right."
  6594. (let ((pos (point)))
  6595. (when (save-excursion
  6596. (beginning-of-line 1)
  6597. (looking-at org-todo-line-regexp)
  6598. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  6599. (cond ((eobp) (insert " "))
  6600. ((eolp) (insert " "))
  6601. ((equal (char-after) ?\ ) (forward-char 1))))))
  6602. (defun org-current-level ()
  6603. "Return the level of the current entry, or nil if before the first headline.
  6604. The level is the number of stars at the beginning of the headline."
  6605. (save-excursion
  6606. (org-with-limited-levels
  6607. (if (ignore-errors (org-back-to-heading t))
  6608. (funcall outline-level)))))
  6609. (defun org-get-previous-line-level ()
  6610. "Return the outline depth of the last headline before the current line.
  6611. Returns 0 for the first headline in the buffer, and nil if before the
  6612. first headline."
  6613. (let ((current-level (org-current-level))
  6614. (prev-level (when (> (line-number-at-pos) 1)
  6615. (save-excursion
  6616. (beginning-of-line 0)
  6617. (org-current-level)))))
  6618. (cond ((null current-level) nil) ; Before first headline
  6619. ((null prev-level) 0) ; At first headline
  6620. (prev-level))))
  6621. (defun org-reduced-level (l)
  6622. "Compute the effective level of a heading.
  6623. This takes into account the setting of `org-odd-levels-only'."
  6624. (cond
  6625. ((zerop l) 0)
  6626. (org-odd-levels-only (1+ (floor (/ l 2))))
  6627. (t l)))
  6628. (defun org-level-increment ()
  6629. "Return the number of stars that will be added or removed at a
  6630. time to headlines when structure editing, based on the value of
  6631. `org-odd-levels-only'."
  6632. (if org-odd-levels-only 2 1))
  6633. (defun org-get-valid-level (level &optional change)
  6634. "Rectify a level change under the influence of `org-odd-levels-only'
  6635. LEVEL is a current level, CHANGE is by how much the level should be
  6636. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  6637. even level numbers will become the next higher odd number."
  6638. (if org-odd-levels-only
  6639. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  6640. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  6641. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  6642. (max 1 (+ level (or change 0)))))
  6643. (if (boundp 'define-obsolete-function-alias)
  6644. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  6645. (define-obsolete-function-alias 'org-get-legal-level
  6646. 'org-get-valid-level)
  6647. (define-obsolete-function-alias 'org-get-legal-level
  6648. 'org-get-valid-level "23.1")))
  6649. (defvar org-called-with-limited-levels nil) ;; Dynamically bound in
  6650. ;; ̀org-with-limited-levels'
  6651. (defun org-promote ()
  6652. "Promote the current heading higher up the tree.
  6653. If the region is active in `transient-mark-mode', promote all headings
  6654. in the region."
  6655. (org-back-to-heading t)
  6656. (let* ((level (save-match-data (funcall outline-level)))
  6657. (after-change-functions (remove 'flyspell-after-change-function
  6658. after-change-functions))
  6659. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  6660. (diff (abs (- level (length up-head) -1))))
  6661. (cond ((and (= level 1) org-called-with-limited-levels
  6662. org-allow-promoting-top-level-subtree)
  6663. (replace-match "# " nil t))
  6664. ((= level 1)
  6665. (error "Cannot promote to level 0. UNDO to recover if necessary"))
  6666. (t (replace-match up-head nil t)))
  6667. ;; Fixup tag positioning
  6668. (unless (= level 1)
  6669. (and org-auto-align-tags (org-set-tags nil t))
  6670. (if org-adapt-indentation (org-fixup-indentation (- diff))))
  6671. (run-hooks 'org-after-promote-entry-hook)))
  6672. (defun org-demote ()
  6673. "Demote the current heading lower down the tree.
  6674. If the region is active in `transient-mark-mode', demote all headings
  6675. in the region."
  6676. (org-back-to-heading t)
  6677. (let* ((level (save-match-data (funcall outline-level)))
  6678. (after-change-functions (remove 'flyspell-after-change-function
  6679. after-change-functions))
  6680. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  6681. (diff (abs (- level (length down-head) -1))))
  6682. (replace-match down-head nil t)
  6683. ;; Fixup tag positioning
  6684. (and org-auto-align-tags (org-set-tags nil t))
  6685. (if org-adapt-indentation (org-fixup-indentation diff))
  6686. (run-hooks 'org-after-demote-entry-hook)))
  6687. (defun org-cycle-level ()
  6688. "Cycle the level of an empty headline through possible states.
  6689. This goes first to child, then to parent, level, then up the hierarchy.
  6690. After top level, it switches back to sibling level."
  6691. (interactive)
  6692. (let ((org-adapt-indentation nil))
  6693. (when (org-point-at-end-of-empty-headline)
  6694. (setq this-command 'org-cycle-level) ; Only needed for caching
  6695. (let ((cur-level (org-current-level))
  6696. (prev-level (org-get-previous-line-level)))
  6697. (cond
  6698. ;; If first headline in file, promote to top-level.
  6699. ((= prev-level 0)
  6700. (loop repeat (/ (- cur-level 1) (org-level-increment))
  6701. do (org-do-promote)))
  6702. ;; If same level as prev, demote one.
  6703. ((= prev-level cur-level)
  6704. (org-do-demote))
  6705. ;; If parent is top-level, promote to top level if not already.
  6706. ((= prev-level 1)
  6707. (loop repeat (/ (- cur-level 1) (org-level-increment))
  6708. do (org-do-promote)))
  6709. ;; If top-level, return to prev-level.
  6710. ((= cur-level 1)
  6711. (loop repeat (/ (- prev-level 1) (org-level-increment))
  6712. do (org-do-demote)))
  6713. ;; If less than prev-level, promote one.
  6714. ((< cur-level prev-level)
  6715. (org-do-promote))
  6716. ;; If deeper than prev-level, promote until higher than
  6717. ;; prev-level.
  6718. ((> cur-level prev-level)
  6719. (loop repeat (+ 1 (/ (- cur-level prev-level) (org-level-increment)))
  6720. do (org-do-promote))))
  6721. t))))
  6722. (defun org-map-tree (fun)
  6723. "Call FUN for every heading underneath the current one."
  6724. (org-back-to-heading)
  6725. (let ((level (funcall outline-level)))
  6726. (save-excursion
  6727. (funcall fun)
  6728. (while (and (progn
  6729. (outline-next-heading)
  6730. (> (funcall outline-level) level))
  6731. (not (eobp)))
  6732. (funcall fun)))))
  6733. (defun org-map-region (fun beg end)
  6734. "Call FUN for every heading between BEG and END."
  6735. (let ((org-ignore-region t))
  6736. (save-excursion
  6737. (setq end (copy-marker end))
  6738. (goto-char beg)
  6739. (if (and (re-search-forward org-outline-regexp-bol nil t)
  6740. (< (point) end))
  6741. (funcall fun))
  6742. (while (and (progn
  6743. (outline-next-heading)
  6744. (< (point) end))
  6745. (not (eobp)))
  6746. (funcall fun)))))
  6747. (defvar org-property-end-re) ; silence byte-compiler
  6748. (defun org-fixup-indentation (diff)
  6749. "Change the indentation in the current entry by DIFF.
  6750. However, if any line in the current entry has no indentation, or if it
  6751. would end up with no indentation after the change, nothing at all is done."
  6752. (save-excursion
  6753. (let ((end (save-excursion (outline-next-heading)
  6754. (point-marker)))
  6755. (prohibit (if (> diff 0)
  6756. "^\\S-"
  6757. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  6758. col)
  6759. (unless (save-excursion (end-of-line 1)
  6760. (re-search-forward prohibit end t))
  6761. (while (and (< (point) end)
  6762. (re-search-forward "^[ \t]+" end t))
  6763. (goto-char (match-end 0))
  6764. (setq col (current-column))
  6765. (if (< diff 0) (replace-match ""))
  6766. (org-indent-to-column (+ diff col))))
  6767. (move-marker end nil))))
  6768. (defun org-convert-to-odd-levels ()
  6769. "Convert an org-mode file with all levels allowed to one with odd levels.
  6770. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  6771. level 5 etc."
  6772. (interactive)
  6773. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  6774. (let ((outline-level 'org-outline-level)
  6775. (org-odd-levels-only nil) n)
  6776. (save-excursion
  6777. (goto-char (point-min))
  6778. (while (re-search-forward "^\\*\\*+ " nil t)
  6779. (setq n (- (length (match-string 0)) 2))
  6780. (while (>= (setq n (1- n)) 0)
  6781. (org-demote))
  6782. (end-of-line 1))))))
  6783. (defun org-convert-to-oddeven-levels ()
  6784. "Convert an org-mode file with only odd levels to one with odd/even levels.
  6785. This promotes level 3 to level 2, level 5 to level 3 etc. If the
  6786. file contains a section with an even level, conversion would
  6787. destroy the structure of the file. An error is signaled in this
  6788. case."
  6789. (interactive)
  6790. (goto-char (point-min))
  6791. ;; First check if there are no even levels
  6792. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  6793. (org-show-context t)
  6794. (error "Not all levels are odd in this file. Conversion not possible"))
  6795. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  6796. (let ((outline-regexp org-outline-regexp)
  6797. (outline-level 'org-outline-level)
  6798. (org-odd-levels-only nil) n)
  6799. (save-excursion
  6800. (goto-char (point-min))
  6801. (while (re-search-forward "^\\*\\*+ " nil t)
  6802. (setq n (/ (1- (length (match-string 0))) 2))
  6803. (while (>= (setq n (1- n)) 0)
  6804. (org-promote))
  6805. (end-of-line 1))))))
  6806. (defun org-tr-level (n)
  6807. "Make N odd if required."
  6808. (if org-odd-levels-only (1+ (/ n 2)) n))
  6809. ;;; Vertical tree motion, cutting and pasting of subtrees
  6810. (defun org-move-subtree-up (&optional arg)
  6811. "Move the current subtree up past ARG headlines of the same level."
  6812. (interactive "p")
  6813. (org-move-subtree-down (- (prefix-numeric-value arg))))
  6814. (defun org-move-subtree-down (&optional arg)
  6815. "Move the current subtree down past ARG headlines of the same level."
  6816. (interactive "p")
  6817. (setq arg (prefix-numeric-value arg))
  6818. (let ((movfunc (if (> arg 0) 'org-get-next-sibling
  6819. 'org-get-last-sibling))
  6820. (ins-point (make-marker))
  6821. (cnt (abs arg))
  6822. (col (current-column))
  6823. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  6824. ;; Select the tree
  6825. (org-back-to-heading)
  6826. (setq beg0 (point))
  6827. (save-excursion
  6828. (setq ne-beg (org-back-over-empty-lines))
  6829. (setq beg (point)))
  6830. (save-match-data
  6831. (save-excursion (outline-end-of-heading)
  6832. (setq folded (outline-invisible-p)))
  6833. (outline-end-of-subtree))
  6834. (outline-next-heading)
  6835. (setq ne-end (org-back-over-empty-lines))
  6836. (setq end (point))
  6837. (goto-char beg0)
  6838. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  6839. ;; include less whitespace
  6840. (save-excursion
  6841. (goto-char beg)
  6842. (forward-line (- ne-beg ne-end))
  6843. (setq beg (point))))
  6844. ;; Find insertion point, with error handling
  6845. (while (> cnt 0)
  6846. (or (and (funcall movfunc) (looking-at org-outline-regexp))
  6847. (progn (goto-char beg0)
  6848. (error "Cannot move past superior level or buffer limit")))
  6849. (setq cnt (1- cnt)))
  6850. (if (> arg 0)
  6851. ;; Moving forward - still need to move over subtree
  6852. (progn (org-end-of-subtree t t)
  6853. (save-excursion
  6854. (org-back-over-empty-lines)
  6855. (or (bolp) (newline)))))
  6856. (setq ne-ins (org-back-over-empty-lines))
  6857. (move-marker ins-point (point))
  6858. (setq txt (buffer-substring beg end))
  6859. (org-save-markers-in-region beg end)
  6860. (delete-region beg end)
  6861. (org-remove-empty-overlays-at beg)
  6862. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  6863. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  6864. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  6865. (let ((bbb (point)))
  6866. (insert-before-markers txt)
  6867. (org-reinstall-markers-in-region bbb)
  6868. (move-marker ins-point bbb))
  6869. (or (bolp) (insert "\n"))
  6870. (setq ins-end (point))
  6871. (goto-char ins-point)
  6872. (org-skip-whitespace)
  6873. (when (and (< arg 0)
  6874. (org-first-sibling-p)
  6875. (> ne-ins ne-beg))
  6876. ;; Move whitespace back to beginning
  6877. (save-excursion
  6878. (goto-char ins-end)
  6879. (let ((kill-whole-line t))
  6880. (kill-line (- ne-ins ne-beg)) (point)))
  6881. (insert (make-string (- ne-ins ne-beg) ?\n)))
  6882. (move-marker ins-point nil)
  6883. (if folded
  6884. (hide-subtree)
  6885. (org-show-entry)
  6886. (show-children)
  6887. (org-cycle-hide-drawers 'children))
  6888. (org-clean-visibility-after-subtree-move)
  6889. ;; move back to the initial column we were at
  6890. (move-to-column col)))
  6891. (defvar org-subtree-clip ""
  6892. "Clipboard for cut and paste of subtrees.
  6893. This is actually only a copy of the kill, because we use the normal kill
  6894. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  6895. (defvar org-subtree-clip-folded nil
  6896. "Was the last copied subtree folded?
  6897. This is used to fold the tree back after pasting.")
  6898. (defun org-cut-subtree (&optional n)
  6899. "Cut the current subtree into the clipboard.
  6900. With prefix arg N, cut this many sequential subtrees.
  6901. This is a short-hand for marking the subtree and then cutting it."
  6902. (interactive "p")
  6903. (org-copy-subtree n 'cut))
  6904. (defun org-copy-subtree (&optional n cut force-store-markers)
  6905. "Cut the current subtree into the clipboard.
  6906. With prefix arg N, cut this many sequential subtrees.
  6907. This is a short-hand for marking the subtree and then copying it.
  6908. If CUT is non-nil, actually cut the subtree.
  6909. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  6910. of some markers in the region, even if CUT is non-nil. This is
  6911. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  6912. (interactive "p")
  6913. (let (beg end folded (beg0 (point)))
  6914. (if (org-called-interactively-p 'any)
  6915. (org-back-to-heading nil) ; take what looks like a subtree
  6916. (org-back-to-heading t)) ; take what is really there
  6917. (org-back-over-empty-lines)
  6918. (setq beg (point))
  6919. (skip-chars-forward " \t\r\n")
  6920. (save-match-data
  6921. (save-excursion (outline-end-of-heading)
  6922. (setq folded (outline-invisible-p)))
  6923. (condition-case nil
  6924. (org-forward-heading-same-level (1- n) t)
  6925. (error nil))
  6926. (org-end-of-subtree t t))
  6927. (org-back-over-empty-lines)
  6928. (setq end (point))
  6929. (goto-char beg0)
  6930. (when (> end beg)
  6931. (setq org-subtree-clip-folded folded)
  6932. (when (or cut force-store-markers)
  6933. (org-save-markers-in-region beg end))
  6934. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  6935. (setq org-subtree-clip (current-kill 0))
  6936. (message "%s: Subtree(s) with %d characters"
  6937. (if cut "Cut" "Copied")
  6938. (length org-subtree-clip)))))
  6939. (defun org-paste-subtree (&optional level tree for-yank)
  6940. "Paste the clipboard as a subtree, with modification of headline level.
  6941. The entire subtree is promoted or demoted in order to match a new headline
  6942. level.
  6943. If the cursor is at the beginning of a headline, the same level as
  6944. that headline is used to paste the tree
  6945. If not, the new level is derived from the *visible* headings
  6946. before and after the insertion point, and taken to be the inferior headline
  6947. level of the two. So if the previous visible heading is level 3 and the
  6948. next is level 4 (or vice versa), level 4 will be used for insertion.
  6949. This makes sure that the subtree remains an independent subtree and does
  6950. not swallow low level entries.
  6951. You can also force a different level, either by using a numeric prefix
  6952. argument, or by inserting the heading marker by hand. For example, if the
  6953. cursor is after \"*****\", then the tree will be shifted to level 5.
  6954. If optional TREE is given, use this text instead of the kill ring.
  6955. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  6956. move back over whitespace before inserting, and move point to the end of
  6957. the inserted text when done."
  6958. (interactive "P")
  6959. (setq tree (or tree (and kill-ring (current-kill 0))))
  6960. (unless (org-kill-is-subtree-p tree)
  6961. (error "%s"
  6962. (substitute-command-keys
  6963. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  6964. (org-with-limited-levels
  6965. (let* ((visp (not (outline-invisible-p)))
  6966. (txt tree)
  6967. (^re_ "\\(\\*+\\)[ \t]*")
  6968. (old-level (if (string-match org-outline-regexp-bol txt)
  6969. (- (match-end 0) (match-beginning 0) 1)
  6970. -1))
  6971. (force-level (cond (level (prefix-numeric-value level))
  6972. ((and (looking-at "[ \t]*$")
  6973. (string-match
  6974. "^\\*+$" (buffer-substring
  6975. (point-at-bol) (point))))
  6976. (- (match-end 1) (match-beginning 1)))
  6977. ((and (bolp)
  6978. (looking-at org-outline-regexp))
  6979. (- (match-end 0) (point) 1))))
  6980. (previous-level (save-excursion
  6981. (condition-case nil
  6982. (progn
  6983. (outline-previous-visible-heading 1)
  6984. (if (looking-at ^re_)
  6985. (- (match-end 0) (match-beginning 0) 1)
  6986. 1))
  6987. (error 1))))
  6988. (next-level (save-excursion
  6989. (condition-case nil
  6990. (progn
  6991. (or (looking-at org-outline-regexp)
  6992. (outline-next-visible-heading 1))
  6993. (if (looking-at ^re_)
  6994. (- (match-end 0) (match-beginning 0) 1)
  6995. 1))
  6996. (error 1))))
  6997. (new-level (or force-level (max previous-level next-level)))
  6998. (shift (if (or (= old-level -1)
  6999. (= new-level -1)
  7000. (= old-level new-level))
  7001. 0
  7002. (- new-level old-level)))
  7003. (delta (if (> shift 0) -1 1))
  7004. (func (if (> shift 0) 'org-demote 'org-promote))
  7005. (org-odd-levels-only nil)
  7006. beg end newend)
  7007. ;; Remove the forced level indicator
  7008. (if force-level
  7009. (delete-region (point-at-bol) (point)))
  7010. ;; Paste
  7011. (beginning-of-line (if (bolp) 1 2))
  7012. (unless for-yank (org-back-over-empty-lines))
  7013. (setq beg (point))
  7014. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  7015. (insert-before-markers txt)
  7016. (unless (string-match "\n\\'" txt) (insert "\n"))
  7017. (setq newend (point))
  7018. (org-reinstall-markers-in-region beg)
  7019. (setq end (point))
  7020. (goto-char beg)
  7021. (skip-chars-forward " \t\n\r")
  7022. (setq beg (point))
  7023. (if (and (outline-invisible-p) visp)
  7024. (save-excursion (outline-show-heading)))
  7025. ;; Shift if necessary
  7026. (unless (= shift 0)
  7027. (save-restriction
  7028. (narrow-to-region beg end)
  7029. (while (not (= shift 0))
  7030. (org-map-region func (point-min) (point-max))
  7031. (setq shift (+ delta shift)))
  7032. (goto-char (point-min))
  7033. (setq newend (point-max))))
  7034. (when (or (org-called-interactively-p 'interactive) for-yank)
  7035. (message "Clipboard pasted as level %d subtree" new-level))
  7036. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  7037. kill-ring
  7038. (eq org-subtree-clip (current-kill 0))
  7039. org-subtree-clip-folded)
  7040. ;; The tree was folded before it was killed/copied
  7041. (hide-subtree))
  7042. (and for-yank (goto-char newend)))))
  7043. (defun org-kill-is-subtree-p (&optional txt)
  7044. "Check if the current kill is an outline subtree, or a set of trees.
  7045. Returns nil if kill does not start with a headline, or if the first
  7046. headline level is not the largest headline level in the tree.
  7047. So this will actually accept several entries of equal levels as well,
  7048. which is OK for `org-paste-subtree'.
  7049. If optional TXT is given, check this string instead of the current kill."
  7050. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  7051. (re (org-get-limited-outline-regexp))
  7052. (^re (concat "^" re))
  7053. (start-level (and kill
  7054. (string-match
  7055. (concat "\\`\\([ \t\n\r]*?\n\\)?\\(" re "\\)")
  7056. kill)
  7057. (- (match-end 2) (match-beginning 2) 1)))
  7058. (start (1+ (or (match-beginning 2) -1))))
  7059. (if (not start-level)
  7060. (progn
  7061. nil) ;; does not even start with a heading
  7062. (catch 'exit
  7063. (while (setq start (string-match ^re kill (1+ start)))
  7064. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  7065. (throw 'exit nil)))
  7066. t))))
  7067. (defvar org-markers-to-move nil
  7068. "Markers that should be moved with a cut-and-paste operation.
  7069. Those markers are stored together with their positions relative to
  7070. the start of the region.")
  7071. (defun org-save-markers-in-region (beg end)
  7072. "Check markers in region.
  7073. If these markers are between BEG and END, record their position relative
  7074. to BEG, so that after moving the block of text, we can put the markers back
  7075. into place.
  7076. This function gets called just before an entry or tree gets cut from the
  7077. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  7078. called immediately, to move the markers with the entries."
  7079. (setq org-markers-to-move nil)
  7080. (when (featurep 'org-clock)
  7081. (org-clock-save-markers-for-cut-and-paste beg end))
  7082. (when (featurep 'org-agenda)
  7083. (org-agenda-save-markers-for-cut-and-paste beg end)))
  7084. (defun org-check-and-save-marker (marker beg end)
  7085. "Check if MARKER is between BEG and END.
  7086. If yes, remember the marker and the distance to BEG."
  7087. (when (and (marker-buffer marker)
  7088. (equal (marker-buffer marker) (current-buffer)))
  7089. (if (and (>= marker beg) (< marker end))
  7090. (push (cons marker (- marker beg)) org-markers-to-move))))
  7091. (defun org-reinstall-markers-in-region (beg)
  7092. "Move all remembered markers to their position relative to BEG."
  7093. (mapc (lambda (x)
  7094. (move-marker (car x) (+ beg (cdr x))))
  7095. org-markers-to-move)
  7096. (setq org-markers-to-move nil))
  7097. (defun org-narrow-to-subtree ()
  7098. "Narrow buffer to the current subtree."
  7099. (interactive)
  7100. (save-excursion
  7101. (save-match-data
  7102. (org-with-limited-levels
  7103. (narrow-to-region
  7104. (progn (org-back-to-heading t) (point))
  7105. (progn (org-end-of-subtree t t)
  7106. (if (and (org-at-heading-p) (not (eobp))) (backward-char 1))
  7107. (point)))))))
  7108. (defun org-narrow-to-block ()
  7109. "Narrow buffer to the current block."
  7110. (interactive)
  7111. (let* ((case-fold-search t)
  7112. (blockp (org-between-regexps-p "^[ \t]*#\\+begin_.*"
  7113. "^[ \t]*#\\+end_.*")))
  7114. (if blockp
  7115. (narrow-to-region (car blockp) (cdr blockp))
  7116. (error "Not in a block"))))
  7117. (eval-when-compile
  7118. (defvar org-property-drawer-re))
  7119. (defvar org-property-start-re) ;; defined below
  7120. (defun org-clone-subtree-with-time-shift (n &optional shift)
  7121. "Clone the task (subtree) at point N times.
  7122. The clones will be inserted as siblings.
  7123. In interactive use, the user will be prompted for the number of
  7124. clones to be produced, and for a time SHIFT, which may be a
  7125. repeater as used in time stamps, for example `+3d'.
  7126. When a valid repeater is given and the entry contains any time
  7127. stamps, the clones will become a sequence in time, with time
  7128. stamps in the subtree shifted for each clone produced. If SHIFT
  7129. is nil or the empty string, time stamps will be left alone. The
  7130. ID property of the original subtree is removed.
  7131. If the original subtree did contain time stamps with a repeater,
  7132. the following will happen:
  7133. - the repeater will be removed in each clone
  7134. - an additional clone will be produced, with the current, unshifted
  7135. date(s) in the entry.
  7136. - the original entry will be placed *after* all the clones, with
  7137. repeater intact.
  7138. - the start days in the repeater in the original entry will be shifted
  7139. to past the last clone.
  7140. In this way you can spell out a number of instances of a repeating task,
  7141. and still retain the repeater to cover future instances of the task."
  7142. (interactive "nNumber of clones to produce: \nsDate shift per clone (e.g. +1w, empty to copy unchanged): ")
  7143. (let (beg end template task idprop
  7144. shift-n shift-what doshift nmin nmax (n-no-remove -1)
  7145. (drawer-re org-drawer-regexp))
  7146. (if (not (and (integerp n) (> n 0)))
  7147. (error "Invalid number of replications %s" n))
  7148. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  7149. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([hdwmy]\\)[ \t]*\\'"
  7150. shift)))
  7151. (error "Invalid shift specification %s" shift))
  7152. (when doshift
  7153. (setq shift-n (string-to-number (match-string 1 shift))
  7154. shift-what (cdr (assoc (match-string 2 shift)
  7155. '(("d" . day) ("w" . week)
  7156. ("m" . month) ("y" . year))))))
  7157. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  7158. (setq nmin 1 nmax n)
  7159. (org-back-to-heading t)
  7160. (setq beg (point))
  7161. (setq idprop (org-entry-get nil "ID"))
  7162. (org-end-of-subtree t t)
  7163. (or (bolp) (insert "\n"))
  7164. (setq end (point))
  7165. (setq template (buffer-substring beg end))
  7166. (when (and doshift
  7167. (string-match "<[^<>\n]+ [.+]?\\+[0-9]+[hdwmy][^<>\n]*>" template))
  7168. (delete-region beg end)
  7169. (setq end beg)
  7170. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  7171. (goto-char end)
  7172. (loop for n from nmin to nmax do
  7173. ;; prepare clone
  7174. (with-temp-buffer
  7175. (insert template)
  7176. (org-mode)
  7177. (goto-char (point-min))
  7178. (org-show-subtree)
  7179. (and idprop (if org-clone-delete-id
  7180. (org-entry-delete nil "ID")
  7181. (org-id-get-create t)))
  7182. (unless (= n 0)
  7183. (while (re-search-forward "^[ \t]*CLOCK:.*$" nil t)
  7184. (kill-whole-line))
  7185. (goto-char (point-min))
  7186. (while (re-search-forward drawer-re nil t)
  7187. (mapc (lambda (d)
  7188. (org-remove-empty-drawer-at d (point))) org-drawers)))
  7189. (goto-char (point-min))
  7190. (when doshift
  7191. (while (re-search-forward org-ts-regexp-both nil t)
  7192. (org-timestamp-change (* n shift-n) shift-what))
  7193. (unless (= n n-no-remove)
  7194. (goto-char (point-min))
  7195. (while (re-search-forward org-ts-regexp nil t)
  7196. (save-excursion
  7197. (goto-char (match-beginning 0))
  7198. (if (looking-at "<[^<>\n]+\\( +[.+]?\\+[0-9]+[hdwmy]\\)")
  7199. (delete-region (match-beginning 1) (match-end 1)))))))
  7200. (setq task (buffer-string)))
  7201. (insert task))
  7202. (goto-char beg)))
  7203. ;;; Outline Sorting
  7204. (defun org-sort (with-case)
  7205. "Call `org-sort-entries', `org-table-sort-lines' or `org-sort-list'.
  7206. Optional argument WITH-CASE means sort case-sensitively."
  7207. (interactive "P")
  7208. (cond
  7209. ((org-at-table-p) (org-call-with-arg 'org-table-sort-lines with-case))
  7210. ((org-at-item-p) (org-call-with-arg 'org-sort-list with-case))
  7211. (t
  7212. (org-call-with-arg 'org-sort-entries with-case))))
  7213. (defun org-sort-remove-invisible (s)
  7214. (remove-text-properties 0 (length s) org-rm-props s)
  7215. (while (string-match org-bracket-link-regexp s)
  7216. (setq s (replace-match (if (match-end 2)
  7217. (match-string 3 s)
  7218. (match-string 1 s)) t t s)))
  7219. s)
  7220. (defvar org-priority-regexp) ; defined later in the file
  7221. (defvar org-after-sorting-entries-or-items-hook nil
  7222. "Hook that is run after a bunch of entries or items have been sorted.
  7223. When children are sorted, the cursor is in the parent line when this
  7224. hook gets called. When a region or a plain list is sorted, the cursor
  7225. will be in the first entry of the sorted region/list.")
  7226. (defun org-sort-entries
  7227. (&optional with-case sorting-type getkey-func compare-func property)
  7228. "Sort entries on a certain level of an outline tree.
  7229. If there is an active region, the entries in the region are sorted.
  7230. Else, if the cursor is before the first entry, sort the top-level items.
  7231. Else, the children of the entry at point are sorted.
  7232. Sorting can be alphabetically, numerically, by date/time as given by
  7233. a time stamp, by a property or by priority.
  7234. The command prompts for the sorting type unless it has been given to the
  7235. function through the SORTING-TYPE argument, which needs to be a character,
  7236. \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?r ?R ?f ?F). Here is the
  7237. precise meaning of each character:
  7238. n Numerically, by converting the beginning of the entry/item to a number.
  7239. a Alphabetically, ignoring the TODO keyword and the priority, if any.
  7240. t By date/time, either the first active time stamp in the entry, or, if
  7241. none exist, by the first inactive one.
  7242. s By the scheduled date/time.
  7243. d By deadline date/time.
  7244. c By creation time, which is assumed to be the first inactive time stamp
  7245. at the beginning of a line.
  7246. p By priority according to the cookie.
  7247. r By the value of a property.
  7248. Capital letters will reverse the sort order.
  7249. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  7250. called with point at the beginning of the record. It must return either
  7251. a string or a number that should serve as the sorting key for that record.
  7252. Comparing entries ignores case by default. However, with an optional argument
  7253. WITH-CASE, the sorting considers case as well."
  7254. (interactive "P")
  7255. (let ((case-func (if with-case 'identity 'downcase))
  7256. start beg end stars re re2
  7257. txt what tmp)
  7258. ;; Find beginning and end of region to sort
  7259. (cond
  7260. ((org-region-active-p)
  7261. ;; we will sort the region
  7262. (setq end (region-end)
  7263. what "region")
  7264. (goto-char (region-beginning))
  7265. (if (not (org-at-heading-p)) (outline-next-heading))
  7266. (setq start (point)))
  7267. ((or (org-at-heading-p)
  7268. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  7269. ;; we will sort the children of the current headline
  7270. (org-back-to-heading)
  7271. (setq start (point)
  7272. end (progn (org-end-of-subtree t t)
  7273. (or (bolp) (insert "\n"))
  7274. (org-back-over-empty-lines)
  7275. (point))
  7276. what "children")
  7277. (goto-char start)
  7278. (show-subtree)
  7279. (outline-next-heading))
  7280. (t
  7281. ;; we will sort the top-level entries in this file
  7282. (goto-char (point-min))
  7283. (or (org-at-heading-p) (outline-next-heading))
  7284. (setq start (point))
  7285. (goto-char (point-max))
  7286. (beginning-of-line 1)
  7287. (when (looking-at ".*?\\S-")
  7288. ;; File ends in a non-white line
  7289. (end-of-line 1)
  7290. (insert "\n"))
  7291. (setq end (point-max))
  7292. (setq what "top-level")
  7293. (goto-char start)
  7294. (show-all)))
  7295. (setq beg (point))
  7296. (if (>= beg end) (error "Nothing to sort"))
  7297. (looking-at "\\(\\*+\\)")
  7298. (setq stars (match-string 1)
  7299. re (concat "^" (regexp-quote stars) " +")
  7300. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[ \t\n]")
  7301. txt (buffer-substring beg end))
  7302. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  7303. (if (and (not (equal stars "*")) (string-match re2 txt))
  7304. (error "Region to sort contains a level above the first entry"))
  7305. (unless sorting-type
  7306. (message
  7307. "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
  7308. [t]ime [s]cheduled [d]eadline [c]reated
  7309. A/N/T/S/D/C/P/O/F means reversed:"
  7310. what)
  7311. (setq sorting-type (read-char-exclusive))
  7312. (unless getkey-func
  7313. (and (= (downcase sorting-type) ?f)
  7314. (setq getkey-func
  7315. (org-icompleting-read "Sort using function: "
  7316. obarray 'fboundp t nil nil))
  7317. (setq getkey-func (intern getkey-func))))
  7318. (and (= (downcase sorting-type) ?r)
  7319. (not property)
  7320. (setq property
  7321. (org-icompleting-read "Property: "
  7322. (mapcar 'list (org-buffer-property-keys t))
  7323. nil t))))
  7324. (message "Sorting entries...")
  7325. (save-restriction
  7326. (narrow-to-region start end)
  7327. (let ((dcst (downcase sorting-type))
  7328. (case-fold-search nil)
  7329. (now (current-time)))
  7330. (sort-subr
  7331. (/= dcst sorting-type)
  7332. ;; This function moves to the beginning character of the "record" to
  7333. ;; be sorted.
  7334. (lambda nil
  7335. (if (re-search-forward re nil t)
  7336. (goto-char (match-beginning 0))
  7337. (goto-char (point-max))))
  7338. ;; This function moves to the last character of the "record" being
  7339. ;; sorted.
  7340. (lambda nil
  7341. (save-match-data
  7342. (condition-case nil
  7343. (outline-forward-same-level 1)
  7344. (error
  7345. (goto-char (point-max))))))
  7346. ;; This function returns the value that gets sorted against.
  7347. (lambda nil
  7348. (cond
  7349. ((= dcst ?n)
  7350. (if (looking-at org-complex-heading-regexp)
  7351. (string-to-number (match-string 4))
  7352. nil))
  7353. ((= dcst ?a)
  7354. (if (looking-at org-complex-heading-regexp)
  7355. (funcall case-func (match-string 4))
  7356. nil))
  7357. ((= dcst ?t)
  7358. (let ((end (save-excursion (outline-next-heading) (point))))
  7359. (if (or (re-search-forward org-ts-regexp end t)
  7360. (re-search-forward org-ts-regexp-both end t))
  7361. (org-time-string-to-seconds (match-string 0))
  7362. (org-float-time now))))
  7363. ((= dcst ?c)
  7364. (let ((end (save-excursion (outline-next-heading) (point))))
  7365. (if (re-search-forward
  7366. (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
  7367. end t)
  7368. (org-time-string-to-seconds (match-string 0))
  7369. (org-float-time now))))
  7370. ((= dcst ?s)
  7371. (let ((end (save-excursion (outline-next-heading) (point))))
  7372. (if (re-search-forward org-scheduled-time-regexp end t)
  7373. (org-time-string-to-seconds (match-string 1))
  7374. (org-float-time now))))
  7375. ((= dcst ?d)
  7376. (let ((end (save-excursion (outline-next-heading) (point))))
  7377. (if (re-search-forward org-deadline-time-regexp end t)
  7378. (org-time-string-to-seconds (match-string 1))
  7379. (org-float-time now))))
  7380. ((= dcst ?p)
  7381. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  7382. (string-to-char (match-string 2))
  7383. org-default-priority))
  7384. ((= dcst ?r)
  7385. (or (org-entry-get nil property) ""))
  7386. ((= dcst ?o)
  7387. (if (looking-at org-complex-heading-regexp)
  7388. (- 9999 (length (member (match-string 2)
  7389. org-todo-keywords-1)))))
  7390. ((= dcst ?f)
  7391. (if getkey-func
  7392. (progn
  7393. (setq tmp (funcall getkey-func))
  7394. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  7395. tmp)
  7396. (error "Invalid key function `%s'" getkey-func)))
  7397. (t (error "Invalid sorting type `%c'" sorting-type))))
  7398. nil
  7399. (cond
  7400. ((= dcst ?a) 'string<)
  7401. ((= dcst ?f) compare-func)
  7402. ((member dcst '(?p ?t ?s ?d ?c)) '<)))))
  7403. (run-hooks 'org-after-sorting-entries-or-items-hook)
  7404. (message "Sorting entries...done")))
  7405. (defun org-do-sort (table what &optional with-case sorting-type)
  7406. "Sort TABLE of WHAT according to SORTING-TYPE.
  7407. The user will be prompted for the SORTING-TYPE if the call to this
  7408. function does not specify it. WHAT is only for the prompt, to indicate
  7409. what is being sorted. The sorting key will be extracted from
  7410. the car of the elements of the table.
  7411. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  7412. (unless sorting-type
  7413. (message
  7414. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  7415. what)
  7416. (setq sorting-type (read-char-exclusive)))
  7417. (let ((dcst (downcase sorting-type))
  7418. extractfun comparefun)
  7419. ;; Define the appropriate functions
  7420. (cond
  7421. ((= dcst ?n)
  7422. (setq extractfun 'string-to-number
  7423. comparefun (if (= dcst sorting-type) '< '>)))
  7424. ((= dcst ?a)
  7425. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  7426. (lambda(x) (downcase (org-sort-remove-invisible x))))
  7427. comparefun (if (= dcst sorting-type)
  7428. 'string<
  7429. (lambda (a b) (and (not (string< a b))
  7430. (not (string= a b)))))))
  7431. ((= dcst ?t)
  7432. (setq extractfun
  7433. (lambda (x)
  7434. (if (or (string-match org-ts-regexp x)
  7435. (string-match org-ts-regexp-both x))
  7436. (org-float-time
  7437. (org-time-string-to-time (match-string 0 x)))
  7438. 0))
  7439. comparefun (if (= dcst sorting-type) '< '>)))
  7440. (t (error "Invalid sorting type `%c'" sorting-type)))
  7441. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  7442. table)
  7443. (lambda (a b) (funcall comparefun (car a) (car b))))))
  7444. ;;; The orgstruct minor mode
  7445. ;; Define a minor mode which can be used in other modes in order to
  7446. ;; integrate the org-mode structure editing commands.
  7447. ;; This is really a hack, because the org-mode structure commands use
  7448. ;; keys which normally belong to the major mode. Here is how it
  7449. ;; works: The minor mode defines all the keys necessary to operate the
  7450. ;; structure commands, but wraps the commands into a function which
  7451. ;; tests if the cursor is currently at a headline or a plain list
  7452. ;; item. If that is the case, the structure command is used,
  7453. ;; temporarily setting many Org-mode variables like regular
  7454. ;; expressions for filling etc. However, when any of those keys is
  7455. ;; used at a different location, function uses `key-binding' to look
  7456. ;; up if the key has an associated command in another currently active
  7457. ;; keymap (minor modes, major mode, global), and executes that
  7458. ;; command. There might be problems if any of the keys is otherwise
  7459. ;; used as a prefix key.
  7460. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  7461. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  7462. ;; addresses this by checking explicitly for both bindings.
  7463. (defvar orgstruct-mode-map (make-sparse-keymap)
  7464. "Keymap for the minor `orgstruct-mode'.")
  7465. (defvar org-local-vars nil
  7466. "List of local variables, for use by `orgstruct-mode'.")
  7467. ;;;###autoload
  7468. (define-minor-mode orgstruct-mode
  7469. "Toggle the minor mode `orgstruct-mode'.
  7470. This mode is for using Org-mode structure commands in other
  7471. modes. The following keys behave as if Org-mode were active, if
  7472. the cursor is on a headline, or on a plain list item (both as
  7473. defined by Org-mode).
  7474. M-up Move entry/item up
  7475. M-down Move entry/item down
  7476. M-left Promote
  7477. M-right Demote
  7478. M-S-up Move entry/item up
  7479. M-S-down Move entry/item down
  7480. M-S-left Promote subtree
  7481. M-S-right Demote subtree
  7482. M-q Fill paragraph and items like in Org-mode
  7483. C-c ^ Sort entries
  7484. C-c - Cycle list bullet
  7485. TAB Cycle item visibility
  7486. M-RET Insert new heading/item
  7487. S-M-RET Insert new TODO heading / Checkbox item
  7488. C-c C-c Set tags / toggle checkbox"
  7489. nil " OrgStruct" nil
  7490. (org-load-modules-maybe)
  7491. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  7492. ;;;###autoload
  7493. (defun turn-on-orgstruct ()
  7494. "Unconditionally turn on `orgstruct-mode'."
  7495. (orgstruct-mode 1))
  7496. (defvar org-fb-vars nil)
  7497. (make-variable-buffer-local 'org-fb-vars)
  7498. (defun orgstruct++-mode (&optional arg)
  7499. "Toggle `orgstruct-mode', the enhanced version of it.
  7500. In addition to setting orgstruct-mode, this also exports all
  7501. indentation and autofilling variables from org-mode into the
  7502. buffer. It will also recognize item context in multiline items."
  7503. (interactive "P")
  7504. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  7505. (if (< arg 1)
  7506. (progn (orgstruct-mode -1)
  7507. (mapc (lambda(v)
  7508. (org-set-local (car v)
  7509. (if (eq (car-safe (cadr v)) 'quote) (cadadr v) (cadr v))))
  7510. org-fb-vars))
  7511. (orgstruct-mode 1)
  7512. (setq org-fb-vars nil)
  7513. (let (var val)
  7514. (mapc
  7515. (lambda (x)
  7516. (when (string-match
  7517. "^\\(paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|fill-prefix\\|indent-\\)"
  7518. (symbol-name (car x)))
  7519. (setq var (car x) val (nth 1 x))
  7520. (push (list var `(quote ,(eval var))) org-fb-vars)
  7521. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  7522. org-local-vars)
  7523. (org-set-local 'orgstruct-is-++ t))))
  7524. (defvar orgstruct-is-++ nil
  7525. "Is `orgstruct-mode' in ++ version in the current-buffer?")
  7526. (make-variable-buffer-local 'orgstruct-is-++)
  7527. ;;;###autoload
  7528. (defun turn-on-orgstruct++ ()
  7529. "Unconditionally turn on `orgstruct++-mode'."
  7530. (orgstruct++-mode 1))
  7531. (defun orgstruct-error ()
  7532. "Error when there is no default binding for a structure key."
  7533. (interactive)
  7534. (error "This key has no function outside structure elements"))
  7535. (defun orgstruct-setup ()
  7536. "Setup orgstruct keymaps."
  7537. (let ((nfunc 0)
  7538. (bindings
  7539. (list
  7540. '([(meta up)] org-metaup)
  7541. '([(meta down)] org-metadown)
  7542. '([(meta left)] org-metaleft)
  7543. '([(meta right)] org-metaright)
  7544. '([(meta shift up)] org-shiftmetaup)
  7545. '([(meta shift down)] org-shiftmetadown)
  7546. '([(meta shift left)] org-shiftmetaleft)
  7547. '([(meta shift right)] org-shiftmetaright)
  7548. '([?\e (up)] org-metaup)
  7549. '([?\e (down)] org-metadown)
  7550. '([?\e (left)] org-metaleft)
  7551. '([?\e (right)] org-metaright)
  7552. '([?\e (shift up)] org-shiftmetaup)
  7553. '([?\e (shift down)] org-shiftmetadown)
  7554. '([?\e (shift left)] org-shiftmetaleft)
  7555. '([?\e (shift right)] org-shiftmetaright)
  7556. '([(shift up)] org-shiftup)
  7557. '([(shift down)] org-shiftdown)
  7558. '([(shift left)] org-shiftleft)
  7559. '([(shift right)] org-shiftright)
  7560. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  7561. '("\M-q" fill-paragraph)
  7562. '("\C-c^" org-sort)
  7563. '("\C-c-" org-cycle-list-bullet)))
  7564. elt key fun cmd)
  7565. (while (setq elt (pop bindings))
  7566. (setq nfunc (1+ nfunc))
  7567. (setq key (org-key (car elt))
  7568. fun (nth 1 elt)
  7569. cmd (orgstruct-make-binding fun nfunc key))
  7570. (org-defkey orgstruct-mode-map key cmd))
  7571. ;; Prevent an error for users who forgot to make autoloads
  7572. (require 'org-element)
  7573. ;; Special treatment needed for TAB and RET
  7574. (org-defkey orgstruct-mode-map [(tab)]
  7575. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  7576. (org-defkey orgstruct-mode-map "\C-i"
  7577. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  7578. (org-defkey orgstruct-mode-map "\M-\C-m"
  7579. (orgstruct-make-binding 'org-insert-heading 105
  7580. "\M-\C-m" [(meta return)]))
  7581. (org-defkey orgstruct-mode-map [(meta return)]
  7582. (orgstruct-make-binding 'org-insert-heading 106
  7583. [(meta return)] "\M-\C-m"))
  7584. (org-defkey orgstruct-mode-map [(shift meta return)]
  7585. (orgstruct-make-binding 'org-insert-todo-heading 107
  7586. [(meta return)] "\M-\C-m"))
  7587. (org-defkey orgstruct-mode-map "\e\C-m"
  7588. (orgstruct-make-binding 'org-insert-heading 108
  7589. "\e\C-m" [?\e (return)]))
  7590. (org-defkey orgstruct-mode-map [?\e (return)]
  7591. (orgstruct-make-binding 'org-insert-heading 109
  7592. [?\e (return)] "\e\C-m"))
  7593. (org-defkey orgstruct-mode-map [?\e (shift return)]
  7594. (orgstruct-make-binding 'org-insert-todo-heading 110
  7595. [?\e (return)] "\e\C-m"))
  7596. (unless org-local-vars
  7597. (setq org-local-vars (org-get-local-variables)))
  7598. t))
  7599. (defun orgstruct-make-binding (fun n &rest keys)
  7600. "Create a function for binding in the structure minor mode.
  7601. FUN is the command to call inside a table. N is used to create a unique
  7602. command name. KEYS are keys that should be checked in for a command
  7603. to execute outside of tables."
  7604. (eval
  7605. (list 'defun
  7606. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  7607. '(arg)
  7608. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  7609. "Outside of structure, run the binding of `"
  7610. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  7611. "'.")
  7612. '(interactive "p")
  7613. (list 'if
  7614. `(org-context-p 'headline 'item
  7615. (and orgstruct-is-++
  7616. ,(and (memq fun '(org-insert-heading org-insert-todo-heading)) t)
  7617. 'item-body))
  7618. (list 'org-run-like-in-org-mode (list 'quote fun))
  7619. (list 'let '(orgstruct-mode)
  7620. (list 'call-interactively
  7621. (append '(or)
  7622. (mapcar (lambda (k)
  7623. (list 'key-binding k))
  7624. keys)
  7625. '('orgstruct-error))))))))
  7626. (defun org-contextualize-keys (alist contexts)
  7627. "Return valid elements in ALIST depending on CONTEXTS.
  7628. `org-agenda-custom-commands' or `org-capture-templates' are the
  7629. values used for ALIST, and `org-agenda-custom-commands-contexts'
  7630. or `org-capture-templates-contexts' are the associated contexts
  7631. definitions."
  7632. (let ((contexts
  7633. ;; normalize contexts
  7634. (mapcar
  7635. (lambda(c) (cond ((listp (cadr c))
  7636. (list (car c) (car c) (cadr c)))
  7637. ((string= "" (cadr c))
  7638. (list (car c) (car c) (caddr c)))
  7639. (t c))) contexts))
  7640. (a alist) c r s)
  7641. ;; loop over all commands or templates
  7642. (while (setq c (pop a))
  7643. (let (vrules repl)
  7644. (cond
  7645. ((not (assoc (car c) contexts))
  7646. (push c r))
  7647. ((and (assoc (car c) contexts)
  7648. (setq vrules (org-contextualize-validate-key
  7649. (car c) contexts)))
  7650. (mapc (lambda (vr)
  7651. (when (not (equal (car vr) (cadr vr)))
  7652. (setq repl vr))) vrules)
  7653. (if (not repl) (push c r)
  7654. (push (cadr repl) s)
  7655. (push
  7656. (cons (car c)
  7657. (cdr (or (assoc (cadr repl) alist)
  7658. (error "Undefined key `%s' as contextual replacement for `%s'"
  7659. (cadr repl) (car c)))))
  7660. r))))))
  7661. ;; Return limited ALIST, possibly with keys modified, and deduplicated
  7662. (delq
  7663. nil
  7664. (delete-dups
  7665. (mapcar (lambda (x)
  7666. (let ((tpl (car x)))
  7667. (when (not (delq
  7668. nil
  7669. (mapcar (lambda(y)
  7670. (equal y tpl)) s))) x)))
  7671. (reverse r))))))
  7672. (defun org-contextualize-validate-key (key contexts)
  7673. "Check CONTEXTS for agenda or capture KEY."
  7674. (let (r rr res)
  7675. (while (setq r (pop contexts))
  7676. (mapc
  7677. (lambda (rr)
  7678. (when
  7679. (and (equal key (car r))
  7680. (if (functionp rr) (funcall rr)
  7681. (or (and (eq (car rr) 'in-file)
  7682. (buffer-file-name)
  7683. (string-match (cdr rr) (buffer-file-name)))
  7684. (and (eq (car rr) 'in-mode)
  7685. (string-match (cdr rr) (symbol-name major-mode)))
  7686. (when (and (eq (car rr) 'not-in-file)
  7687. (buffer-file-name))
  7688. (not (string-match (cdr rr) (buffer-file-name))))
  7689. (when (eq (car rr) 'not-in-mode)
  7690. (not (string-match (cdr rr) (symbol-name major-mode)))))))
  7691. (push r res)))
  7692. (car (last r))))
  7693. (delete-dups (delq nil res))))
  7694. (defun org-context-p (&rest contexts)
  7695. "Check if local context is any of CONTEXTS.
  7696. Possible values in the list of contexts are `table', `headline', and `item'."
  7697. (let ((pos (point)))
  7698. (goto-char (point-at-bol))
  7699. (prog1 (or (and (memq 'table contexts)
  7700. (looking-at "[ \t]*|"))
  7701. (and (memq 'headline contexts)
  7702. (looking-at org-outline-regexp))
  7703. (and (memq 'item contexts)
  7704. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  7705. (and (memq 'item-body contexts)
  7706. (org-in-item-p)))
  7707. (goto-char pos))))
  7708. (defun org-get-local-variables ()
  7709. "Return a list of all local variables in an Org mode buffer."
  7710. (let (varlist)
  7711. (with-current-buffer (get-buffer-create "*Org tmp*")
  7712. (erase-buffer)
  7713. (org-mode)
  7714. (setq varlist (buffer-local-variables)))
  7715. (kill-buffer "*Org tmp*")
  7716. (delq nil
  7717. (mapcar
  7718. (lambda (x)
  7719. (setq x
  7720. (if (symbolp x)
  7721. (list x)
  7722. (list (car x) (list 'quote (cdr x)))))
  7723. (if (string-match
  7724. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|indent-\\)"
  7725. (symbol-name (car x)))
  7726. x nil))
  7727. varlist))))
  7728. (defun org-clone-local-variables (from-buffer &optional regexp)
  7729. "Clone local variables from FROM-BUFFER.
  7730. Optional argument REGEXP selects variables to clone."
  7731. (mapc
  7732. (lambda (pair)
  7733. (and (symbolp (car pair))
  7734. (or (null regexp)
  7735. (string-match regexp (symbol-name (car pair))))
  7736. (set (make-local-variable (car pair))
  7737. (cdr pair))))
  7738. (buffer-local-variables from-buffer)))
  7739. ;;;###autoload
  7740. (defun org-run-like-in-org-mode (cmd)
  7741. "Run a command, pretending that the current buffer is in Org-mode.
  7742. This will temporarily bind local variables that are typically bound in
  7743. Org-mode to the values they have in Org-mode, and then interactively
  7744. call CMD."
  7745. (org-load-modules-maybe)
  7746. (unless org-local-vars
  7747. (setq org-local-vars (org-get-local-variables)))
  7748. (eval (list 'let org-local-vars
  7749. (list 'call-interactively (list 'quote cmd)))))
  7750. ;;;; Archiving
  7751. (defun org-get-category (&optional pos force-refresh)
  7752. "Get the category applying to position POS."
  7753. (save-match-data
  7754. (if force-refresh (org-refresh-category-properties))
  7755. (let ((pos (or pos (point))))
  7756. (or (get-text-property pos 'org-category)
  7757. (progn (org-refresh-category-properties)
  7758. (get-text-property pos 'org-category))))))
  7759. (defun org-refresh-category-properties ()
  7760. "Refresh category text properties in the buffer."
  7761. (let ((case-fold-search t)
  7762. (inhibit-read-only t)
  7763. (def-cat (cond
  7764. ((null org-category)
  7765. (if buffer-file-name
  7766. (file-name-sans-extension
  7767. (file-name-nondirectory buffer-file-name))
  7768. "???"))
  7769. ((symbolp org-category) (symbol-name org-category))
  7770. (t org-category)))
  7771. beg end cat pos optionp)
  7772. (org-unmodified
  7773. (save-excursion
  7774. (save-restriction
  7775. (widen)
  7776. (goto-char (point-min))
  7777. (put-text-property (point) (point-max) 'org-category def-cat)
  7778. (while (re-search-forward
  7779. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  7780. (setq pos (match-end 0)
  7781. optionp (equal (char-after (match-beginning 0)) ?#)
  7782. cat (org-trim (match-string 2)))
  7783. (if optionp
  7784. (setq beg (point-at-bol) end (point-max))
  7785. (org-back-to-heading t)
  7786. (setq beg (point) end (org-end-of-subtree t t)))
  7787. (put-text-property beg end 'org-category cat)
  7788. (put-text-property beg end 'org-category-position beg)
  7789. (goto-char pos)))))))
  7790. ;;;; Link Stuff
  7791. ;;; Link abbreviations
  7792. (defun org-link-expand-abbrev (link)
  7793. "Apply replacements as defined in `org-link-abbrev-alist'."
  7794. (if (string-match "^\\([^:]*\\)\\(::?\\(.*\\)\\)?$" link)
  7795. (let* ((key (match-string 1 link))
  7796. (as (or (assoc key org-link-abbrev-alist-local)
  7797. (assoc key org-link-abbrev-alist)))
  7798. (tag (and (match-end 2) (match-string 3 link)))
  7799. rpl)
  7800. (if (not as)
  7801. link
  7802. (setq rpl (cdr as))
  7803. (cond
  7804. ((symbolp rpl) (funcall rpl tag))
  7805. ((string-match "%(\\([^)]+\\))" rpl)
  7806. (replace-match (funcall (intern-soft (match-string 1 rpl)) tag) t t rpl))
  7807. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  7808. ((string-match "%h" rpl)
  7809. (replace-match (url-hexify-string (or tag "")) t t rpl))
  7810. (t (concat rpl tag)))))
  7811. link))
  7812. ;;; Storing and inserting links
  7813. (defvar org-insert-link-history nil
  7814. "Minibuffer history for links inserted with `org-insert-link'.")
  7815. (defvar org-stored-links nil
  7816. "Contains the links stored with `org-store-link'.")
  7817. (defvar org-store-link-plist nil
  7818. "Plist with info about the most recently link created with `org-store-link'.")
  7819. (defvar org-link-protocols nil
  7820. "Link protocols added to Org-mode using `org-add-link-type'.")
  7821. (defvar org-store-link-functions nil
  7822. "List of functions that are called to create and store a link.
  7823. Each function will be called in turn until one returns a non-nil
  7824. value. Each function should check if it is responsible for creating
  7825. this link (for example by looking at the major mode).
  7826. If not, it must exit and return nil.
  7827. If yes, it should return a non-nil value after a calling
  7828. `org-store-link-props' with a list of properties and values.
  7829. Special properties are:
  7830. :type The link prefix, like \"http\". This must be given.
  7831. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  7832. This is obligatory as well.
  7833. :description Optional default description for the second pair
  7834. of brackets in an Org-mode link. The user can still change
  7835. this when inserting this link into an Org-mode buffer.
  7836. In addition to these, any additional properties can be specified
  7837. and then used in capture templates.")
  7838. (defun org-add-link-type (type &optional follow export)
  7839. "Add TYPE to the list of `org-link-types'.
  7840. Re-compute all regular expressions depending on `org-link-types'
  7841. FOLLOW and EXPORT are two functions.
  7842. FOLLOW should take the link path as the single argument and do whatever
  7843. is necessary to follow the link, for example find a file or display
  7844. a mail message.
  7845. EXPORT should format the link path for export to one of the export formats.
  7846. It should be a function accepting three arguments:
  7847. path the path of the link, the text after the prefix (like \"http:\")
  7848. desc the description of the link, if any, or a description added by
  7849. org-export-normalize-links if there is none
  7850. format the export format, a symbol like `html' or `latex' or `ascii'..
  7851. The function may use the FORMAT information to return different values
  7852. depending on the format. The return value will be put literally into
  7853. the exported file. If the return value is nil, this means Org should
  7854. do what it normally does with links which do not have EXPORT defined.
  7855. Org-mode has a built-in default for exporting links. If you are happy with
  7856. this default, there is no need to define an export function for the link
  7857. type. For a simple example of an export function, see `org-bbdb.el'."
  7858. (add-to-list 'org-link-types type t)
  7859. (org-make-link-regexps)
  7860. (if (assoc type org-link-protocols)
  7861. (setcdr (assoc type org-link-protocols) (list follow export))
  7862. (push (list type follow export) org-link-protocols)))
  7863. (defvar org-agenda-buffer-name) ; Defined in org-agenda.el
  7864. (defvar org-link-to-org-use-id) ; Defined in org-id.el
  7865. ;;;###autoload
  7866. (defun org-store-link (arg)
  7867. "\\<org-mode-map>Store an org-link to the current location.
  7868. This link is added to `org-stored-links' and can later be inserted
  7869. into an org-buffer with \\[org-insert-link].
  7870. For some link types, a prefix arg is interpreted:
  7871. For links to usenet articles, arg negates `org-gnus-prefer-web-links'.
  7872. For file links, arg negates `org-context-in-file-links'."
  7873. (interactive "P")
  7874. (org-load-modules-maybe)
  7875. (setq org-store-link-plist nil) ; reset
  7876. (org-with-limited-levels
  7877. (let (link cpltxt desc description search txt custom-id agenda-link)
  7878. (cond
  7879. ((run-hook-with-args-until-success 'org-store-link-functions)
  7880. (setq link (plist-get org-store-link-plist :link)
  7881. desc (or (plist-get org-store-link-plist :description) link)))
  7882. ((org-src-edit-buffer-p)
  7883. (let (label gc)
  7884. (while (or (not label)
  7885. (save-excursion
  7886. (save-restriction
  7887. (widen)
  7888. (goto-char (point-min))
  7889. (re-search-forward
  7890. (regexp-quote (format org-coderef-label-format label))
  7891. nil t))))
  7892. (when label (message "Label exists already") (sit-for 2))
  7893. (setq label (read-string "Code line label: " label)))
  7894. (end-of-line 1)
  7895. (setq link (format org-coderef-label-format label))
  7896. (setq gc (- 79 (length link)))
  7897. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  7898. (insert link)
  7899. (setq link (concat "(" label ")") desc nil)))
  7900. ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
  7901. ;; We are in the agenda, link to referenced location
  7902. (let ((m (or (get-text-property (point) 'org-hd-marker)
  7903. (get-text-property (point) 'org-marker))))
  7904. (when m
  7905. (org-with-point-at m
  7906. (setq agenda-link
  7907. (if (org-called-interactively-p 'any)
  7908. (call-interactively 'org-store-link)
  7909. (org-store-link nil)))))))
  7910. ((eq major-mode 'calendar-mode)
  7911. (let ((cd (calendar-cursor-to-date)))
  7912. (setq link
  7913. (format-time-string
  7914. (car org-time-stamp-formats)
  7915. (apply 'encode-time
  7916. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  7917. nil nil nil))))
  7918. (org-store-link-props :type "calendar" :date cd)))
  7919. ((eq major-mode 'help-mode)
  7920. (setq link (concat "help:" (save-excursion
  7921. (goto-char (point-min))
  7922. (looking-at "^[^ ]+")
  7923. (match-string 0))))
  7924. (org-store-link-props :type "help"))
  7925. ((eq major-mode 'w3-mode)
  7926. (setq cpltxt (if (and (buffer-name)
  7927. (not (string-match "Untitled" (buffer-name))))
  7928. (buffer-name)
  7929. (url-view-url t))
  7930. link (url-view-url t))
  7931. (org-store-link-props :type "w3" :url (url-view-url t)))
  7932. ((eq major-mode 'w3m-mode)
  7933. (setq cpltxt (or w3m-current-title w3m-current-url)
  7934. link w3m-current-url)
  7935. (org-store-link-props :type "w3m" :url (url-view-url t)))
  7936. ((setq search (run-hook-with-args-until-success
  7937. 'org-create-file-search-functions))
  7938. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  7939. "::" search))
  7940. (setq cpltxt (or description link)))
  7941. ((eq major-mode 'image-mode)
  7942. (setq cpltxt (concat "file:"
  7943. (abbreviate-file-name buffer-file-name))
  7944. link cpltxt)
  7945. (org-store-link-props :type "image" :file buffer-file-name))
  7946. ((eq major-mode 'dired-mode)
  7947. ;; link to the file in the current line
  7948. (let ((file (dired-get-filename nil t)))
  7949. (setq file (if file
  7950. (abbreviate-file-name
  7951. (expand-file-name (dired-get-filename nil t)))
  7952. ;; otherwise, no file so use current directory.
  7953. default-directory))
  7954. (setq cpltxt (concat "file:" file)
  7955. link cpltxt)))
  7956. ((and (buffer-file-name (buffer-base-buffer)) (derived-mode-p 'org-mode))
  7957. (setq custom-id (org-entry-get nil "CUSTOM_ID"))
  7958. (cond
  7959. ((org-in-regexp "<<\\(.*?\\)>>")
  7960. (setq cpltxt
  7961. (concat "file:"
  7962. (abbreviate-file-name
  7963. (buffer-file-name (buffer-base-buffer)))
  7964. "::" (match-string 1))
  7965. link cpltxt))
  7966. ((and (featurep 'org-id)
  7967. (or (eq org-link-to-org-use-id t)
  7968. (and (org-called-interactively-p 'any)
  7969. (or (eq org-link-to-org-use-id 'create-if-interactive)
  7970. (and (eq org-link-to-org-use-id
  7971. 'create-if-interactive-and-no-custom-id)
  7972. (not custom-id))))
  7973. (and org-link-to-org-use-id (org-entry-get nil "ID"))))
  7974. ;; We can make a link using the ID.
  7975. (setq link (condition-case nil
  7976. (prog1 (org-id-store-link)
  7977. (setq desc (plist-get org-store-link-plist :description)))
  7978. (error
  7979. ;; probably before first headline, link to file only
  7980. (concat "file:"
  7981. (abbreviate-file-name
  7982. (buffer-file-name (buffer-base-buffer))))))))
  7983. (t
  7984. ;; Just link to current headline
  7985. (setq cpltxt (concat "file:"
  7986. (abbreviate-file-name
  7987. (buffer-file-name (buffer-base-buffer)))))
  7988. ;; Add a context search string
  7989. (when (org-xor org-context-in-file-links arg)
  7990. (setq txt (cond
  7991. ((org-at-heading-p) nil)
  7992. ((org-region-active-p)
  7993. (buffer-substring (region-beginning) (region-end)))))
  7994. (when (or (null txt) (string-match "\\S-" txt))
  7995. (setq cpltxt
  7996. (concat cpltxt "::"
  7997. (condition-case nil
  7998. (org-make-org-heading-search-string txt)
  7999. (error "")))
  8000. desc (or (nth 4 (ignore-errors
  8001. (org-heading-components))) "NONE"))))
  8002. (if (string-match "::\\'" cpltxt)
  8003. (setq cpltxt (substring cpltxt 0 -2)))
  8004. (setq link cpltxt))))
  8005. ((buffer-file-name (buffer-base-buffer))
  8006. ;; Just link to this file here.
  8007. (setq cpltxt (concat "file:"
  8008. (abbreviate-file-name
  8009. (buffer-file-name (buffer-base-buffer)))))
  8010. ;; Add a context string
  8011. (when (org-xor org-context-in-file-links arg)
  8012. (setq txt (if (org-region-active-p)
  8013. (buffer-substring (region-beginning) (region-end))
  8014. (buffer-substring (point-at-bol) (point-at-eol))))
  8015. ;; Only use search option if there is some text.
  8016. (when (string-match "\\S-" txt)
  8017. (setq cpltxt
  8018. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  8019. desc "NONE")))
  8020. (setq link cpltxt))
  8021. ((org-called-interactively-p 'interactive)
  8022. (error "Cannot link to a buffer which is not visiting a file"))
  8023. (t (setq link nil)))
  8024. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  8025. (setq link (or link cpltxt)
  8026. desc (or desc cpltxt))
  8027. (if (equal desc "NONE") (setq desc nil))
  8028. (if (and (or (org-called-interactively-p 'any) executing-kbd-macro) link)
  8029. (progn
  8030. (setq org-stored-links
  8031. (cons (list link desc) org-stored-links))
  8032. (message "Stored: %s" (or desc link))
  8033. (when custom-id
  8034. (setq link (concat "file:" (abbreviate-file-name (buffer-file-name))
  8035. "::#" custom-id))
  8036. (setq org-stored-links
  8037. (cons (list link desc) org-stored-links))))
  8038. (or agenda-link (and link (org-make-link-string link desc)))))))
  8039. (defun org-store-link-props (&rest plist)
  8040. "Store link properties, extract names and addresses."
  8041. (let (x adr)
  8042. (when (setq x (plist-get plist :from))
  8043. (setq adr (mail-extract-address-components x))
  8044. (setq plist (plist-put plist :fromname (car adr)))
  8045. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  8046. (when (setq x (plist-get plist :to))
  8047. (setq adr (mail-extract-address-components x))
  8048. (setq plist (plist-put plist :toname (car adr)))
  8049. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  8050. (let ((from (plist-get plist :from))
  8051. (to (plist-get plist :to)))
  8052. (when (and from to org-from-is-user-regexp)
  8053. (setq plist
  8054. (plist-put plist :fromto
  8055. (if (string-match org-from-is-user-regexp from)
  8056. (concat "to %t")
  8057. (concat "from %f"))))))
  8058. (setq org-store-link-plist plist))
  8059. (defun org-add-link-props (&rest plist)
  8060. "Add these properties to the link property list."
  8061. (let (key value)
  8062. (while plist
  8063. (setq key (pop plist) value (pop plist))
  8064. (setq org-store-link-plist
  8065. (plist-put org-store-link-plist key value)))))
  8066. (defun org-email-link-description (&optional fmt)
  8067. "Return the description part of an email link.
  8068. This takes information from `org-store-link-plist' and formats it
  8069. according to FMT (default from `org-email-link-description-format')."
  8070. (setq fmt (or fmt org-email-link-description-format))
  8071. (let* ((p org-store-link-plist)
  8072. (to (plist-get p :toaddress))
  8073. (from (plist-get p :fromaddress))
  8074. (table
  8075. (list
  8076. (cons "%c" (plist-get p :fromto))
  8077. (cons "%F" (plist-get p :from))
  8078. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  8079. (cons "%T" (plist-get p :to))
  8080. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  8081. (cons "%s" (plist-get p :subject))
  8082. (cons "%d" (plist-get p :date))
  8083. (cons "%m" (plist-get p :message-id)))))
  8084. (when (string-match "%c" fmt)
  8085. ;; Check if the user wrote this message
  8086. (if (and org-from-is-user-regexp from to
  8087. (save-match-data (string-match org-from-is-user-regexp from)))
  8088. (setq fmt (replace-match "to %t" t t fmt))
  8089. (setq fmt (replace-match "from %f" t t fmt))))
  8090. (org-replace-escapes fmt table)))
  8091. (defun org-make-org-heading-search-string (&optional string heading)
  8092. "Make search string for STRING or current headline."
  8093. (interactive)
  8094. (let ((s (or string (org-get-heading)))
  8095. (lines org-context-in-file-links))
  8096. (unless (and string (not heading))
  8097. ;; We are using a headline, clean up garbage in there.
  8098. (if (string-match org-todo-regexp s)
  8099. (setq s (replace-match "" t t s)))
  8100. (if (string-match (org-re ":[[:alnum:]_@#%:]+:[ \t]*$") s)
  8101. (setq s (replace-match "" t t s)))
  8102. (setq s (org-trim s))
  8103. (if (string-match (concat "^\\(" org-quote-string "\\|"
  8104. org-comment-string "\\)") s)
  8105. (setq s (replace-match "" t t s)))
  8106. (while (string-match org-ts-regexp s)
  8107. (setq s (replace-match "" t t s))))
  8108. (or string (setq s (concat "*" s))) ; Add * for headlines
  8109. (when (and string (integerp lines) (> lines 0))
  8110. (let ((slines (org-split-string s "\n")))
  8111. (when (< lines (length slines))
  8112. (setq s (mapconcat
  8113. 'identity
  8114. (reverse (nthcdr (- (length slines) lines)
  8115. (reverse slines))) "\n")))))
  8116. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  8117. (defun org-make-link-string (link &optional description)
  8118. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  8119. (unless (string-match "\\S-" link)
  8120. (error "Empty link"))
  8121. (when (and description
  8122. (stringp description)
  8123. (not (string-match "\\S-" description)))
  8124. (setq description nil))
  8125. (when (stringp description)
  8126. ;; Remove brackets from the description, they are fatal.
  8127. (while (string-match "\\[" description)
  8128. (setq description (replace-match "{" t t description)))
  8129. (while (string-match "\\]" description)
  8130. (setq description (replace-match "}" t t description))))
  8131. (when (equal link description)
  8132. ;; No description needed, it is identical
  8133. (setq description nil))
  8134. (when (and (not description)
  8135. (not (string-match (org-image-file-name-regexp) link))
  8136. (not (equal link (org-link-escape link))))
  8137. (setq description (org-extract-attributes link)))
  8138. (setq link
  8139. (cond ((string-match (org-image-file-name-regexp) link) link)
  8140. ((string-match org-link-types-re link)
  8141. (concat (match-string 1 link)
  8142. (org-link-escape (substring link (match-end 1)))))
  8143. (t (org-link-escape link))))
  8144. (concat "[[" link "]"
  8145. (if description (concat "[" description "]") "")
  8146. "]"))
  8147. (defconst org-link-escape-chars
  8148. '(?\ ?\[ ?\] ?\; ?\= ?\+)
  8149. "List of characters that should be escaped in link.
  8150. This is the list that is used for internal purposes.")
  8151. (defconst org-link-escape-chars-browser
  8152. '(?\ )
  8153. "List of escapes for characters that are problematic in links.
  8154. This is the list that is used before handing over to the browser.")
  8155. (defun org-link-escape (text &optional table merge)
  8156. "Return percent escaped representation of TEXT.
  8157. TEXT is a string with the text to escape.
  8158. Optional argument TABLE is a list with characters that should be
  8159. escaped. When nil, `org-link-escape-chars' is used.
  8160. If optional argument MERGE is set, merge TABLE into
  8161. `org-link-escape-chars'."
  8162. (cond
  8163. ((and table merge)
  8164. (mapc (lambda (defchr)
  8165. (unless (member defchr table)
  8166. (setq table (cons defchr table)))) org-link-escape-chars))
  8167. ((null table)
  8168. (setq table org-link-escape-chars)))
  8169. (mapconcat
  8170. (lambda (char)
  8171. (if (or (member char table)
  8172. (and (or (< char 32) (= char 37) (> char 126))
  8173. org-url-hexify-p))
  8174. (mapconcat (lambda (sequence-element)
  8175. (format "%%%.2X" sequence-element))
  8176. (or (encode-coding-char char 'utf-8)
  8177. (error "Unable to percent escape character: %s"
  8178. (char-to-string char))) "")
  8179. (char-to-string char))) text ""))
  8180. (defun org-link-unescape (str)
  8181. "Unhex hexified Unicode strings as returned from the JavaScript function
  8182. encodeURIComponent. E.g. `%C3%B6' is the german o-Umlaut."
  8183. (unless (and (null str) (string= "" str))
  8184. (let ((pos 0) (case-fold-search t) unhexed)
  8185. (while (setq pos (string-match "\\(%[0-9a-f][0-9a-f]\\)+" str pos))
  8186. (setq unhexed (org-link-unescape-compound (match-string 0 str)))
  8187. (setq str (replace-match unhexed t t str))
  8188. (setq pos (+ pos (length unhexed))))))
  8189. str)
  8190. (defun org-link-unescape-compound (hex)
  8191. "Unhexify Unicode hex-chars. E.g. `%C3%B6' is the German o-Umlaut.
  8192. Note: this function also decodes single byte encodings like
  8193. `%E1' (a-acute) if not followed by another `%[A-F0-9]{2}' group."
  8194. (save-match-data
  8195. (let* ((bytes (cdr (split-string hex "%")))
  8196. (ret "")
  8197. (eat 0)
  8198. (sum 0))
  8199. (while bytes
  8200. (let* ((val (string-to-number (pop bytes) 16))
  8201. (shift-xor
  8202. (if (= 0 eat)
  8203. (cond
  8204. ((>= val 252) (cons 6 252))
  8205. ((>= val 248) (cons 5 248))
  8206. ((>= val 240) (cons 4 240))
  8207. ((>= val 224) (cons 3 224))
  8208. ((>= val 192) (cons 2 192))
  8209. (t (cons 0 0)))
  8210. (cons 6 128))))
  8211. (if (>= val 192) (setq eat (car shift-xor)))
  8212. (setq val (logxor val (cdr shift-xor)))
  8213. (setq sum (+ (lsh sum (car shift-xor)) val))
  8214. (if (> eat 0) (setq eat (- eat 1)))
  8215. (cond
  8216. ((= 0 eat) ;multi byte
  8217. (setq ret (concat ret (org-char-to-string sum)))
  8218. (setq sum 0))
  8219. ((not bytes) ; single byte(s)
  8220. (setq ret (org-link-unescape-single-byte-sequence hex))))
  8221. )) ;; end (while bytes
  8222. ret )))
  8223. (defun org-link-unescape-single-byte-sequence (hex)
  8224. "Unhexify hex-encoded single byte character sequences."
  8225. (mapconcat (lambda (byte)
  8226. (char-to-string (string-to-number byte 16)))
  8227. (cdr (split-string hex "%")) ""))
  8228. (defun org-xor (a b)
  8229. "Exclusive or."
  8230. (if a (not b) b))
  8231. (defun org-fixup-message-id-for-http (s)
  8232. "Replace special characters in a message id, so it can be used in an http query."
  8233. (when (string-match "%" s)
  8234. (setq s (mapconcat (lambda (c)
  8235. (if (eq c ?%)
  8236. "%25"
  8237. (char-to-string c)))
  8238. s "")))
  8239. (while (string-match "<" s)
  8240. (setq s (replace-match "%3C" t t s)))
  8241. (while (string-match ">" s)
  8242. (setq s (replace-match "%3E" t t s)))
  8243. (while (string-match "@" s)
  8244. (setq s (replace-match "%40" t t s)))
  8245. s)
  8246. (defun org-link-prettify (link)
  8247. "Return a human-readable representation of LINK.
  8248. The car of LINK must be a raw link the cdr of LINK must be either
  8249. a link description or nil."
  8250. (let ((desc (or (cadr link) "<no description>")))
  8251. (concat (format "%-45s" (substring desc 0 (min (length desc) 40)))
  8252. "<" (car link) ">")))
  8253. ;;;###autoload
  8254. (defun org-insert-link-global ()
  8255. "Insert a link like Org-mode does.
  8256. This command can be called in any mode to insert a link in Org-mode syntax."
  8257. (interactive)
  8258. (org-load-modules-maybe)
  8259. (org-run-like-in-org-mode 'org-insert-link))
  8260. (defun org-insert-all-links (&optional keep)
  8261. "Insert all links in `org-stored-links'."
  8262. (interactive "P")
  8263. (let ((links (copy-sequence org-stored-links)) l)
  8264. (while (setq l (if keep (pop links) (pop org-stored-links)))
  8265. (insert "- ")
  8266. (org-insert-link nil (car l) (cadr l))
  8267. (insert "\n"))))
  8268. (defun org-link-fontify-links-to-this-file ()
  8269. "Fontify links to the current file in `org-stored-links'."
  8270. (let ((f (buffer-file-name)) a b)
  8271. (setq a (mapcar (lambda(l)
  8272. (let ((ll (car l)))
  8273. (when (and (string-match "^file:\\(.+\\)::" ll)
  8274. (equal f (expand-file-name (match-string 1 ll))))
  8275. ll)))
  8276. org-stored-links))
  8277. (when (featurep 'org-id)
  8278. (setq b (mapcar (lambda(l)
  8279. (let ((ll (car l)))
  8280. (when (and (string-match "^id:\\(.+\\)$" ll)
  8281. (equal f (expand-file-name
  8282. (or (org-id-find-id-file
  8283. (match-string 1 ll)) ""))))
  8284. ll)))
  8285. org-stored-links)))
  8286. (mapcar (lambda(l)
  8287. (put-text-property 0 (length l) 'face 'font-lock-comment-face l))
  8288. (delq nil (append a b)))))
  8289. (defvar org-link-links-in-this-file nil)
  8290. (defun org-insert-link (&optional complete-file link-location default-description)
  8291. "Insert a link. At the prompt, enter the link.
  8292. Completion can be used to insert any of the link protocol prefixes like
  8293. http or ftp in use.
  8294. The history can be used to select a link previously stored with
  8295. `org-store-link'. When the empty string is entered (i.e. if you just
  8296. press RET at the prompt), the link defaults to the most recently
  8297. stored link. As SPC triggers completion in the minibuffer, you need to
  8298. use M-SPC or C-q SPC to force the insertion of a space character.
  8299. You will also be prompted for a description, and if one is given, it will
  8300. be displayed in the buffer instead of the link.
  8301. If there is already a link at point, this command will allow you to edit link
  8302. and description parts.
  8303. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  8304. be selected using completion. The path to the file will be relative to the
  8305. current directory if the file is in the current directory or a subdirectory.
  8306. Otherwise, the link will be the absolute path as completed in the minibuffer
  8307. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  8308. option `org-link-file-path-type'.
  8309. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  8310. the current directory or below.
  8311. With three \\[universal-argument] prefixes, negate the meaning of
  8312. `org-keep-stored-link-after-insertion'.
  8313. If `org-make-link-description-function' is non-nil, this function will be
  8314. called with the link target, and the result will be the default
  8315. link description.
  8316. If the LINK-LOCATION parameter is non-nil, this value will be
  8317. used as the link location instead of reading one interactively.
  8318. If the DEFAULT-DESCRIPTION parameter is non-nil, this value will
  8319. be used as the default description."
  8320. (interactive "P")
  8321. (let* ((wcf (current-window-configuration))
  8322. (origbuf (current-buffer))
  8323. (region (if (org-region-active-p)
  8324. (buffer-substring (region-beginning) (region-end))))
  8325. (remove (and region (list (region-beginning) (region-end))))
  8326. (desc region)
  8327. tmphist ; byte-compile incorrectly complains about this
  8328. (link link-location)
  8329. (abbrevs org-link-abbrev-alist-local)
  8330. entry file all-prefixes auto-desc)
  8331. (cond
  8332. (link-location) ; specified by arg, just use it.
  8333. ((org-in-regexp org-bracket-link-regexp 1)
  8334. ;; We do have a link at point, and we are going to edit it.
  8335. (setq remove (list (match-beginning 0) (match-end 0)))
  8336. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  8337. (setq link (read-string "Link: "
  8338. (org-link-unescape
  8339. (org-match-string-no-properties 1)))))
  8340. ((or (org-in-regexp org-angle-link-re)
  8341. (org-in-regexp org-plain-link-re))
  8342. ;; Convert to bracket link
  8343. (setq remove (list (match-beginning 0) (match-end 0))
  8344. link (read-string "Link: "
  8345. (org-remove-angle-brackets (match-string 0)))))
  8346. ((member complete-file '((4) (16)))
  8347. ;; Completing read for file names.
  8348. (setq link (org-file-complete-link complete-file)))
  8349. (t
  8350. ;; Read link, with completion for stored links.
  8351. (org-link-fontify-links-to-this-file)
  8352. (org-switch-to-buffer-other-window "*Org Links*")
  8353. (with-current-buffer "*Org Links*"
  8354. (erase-buffer)
  8355. (insert "Insert a link.
  8356. Use TAB to complete link prefixes, then RET for type-specific completion support\n")
  8357. (when org-stored-links
  8358. (insert "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  8359. (insert (mapconcat 'org-link-prettify
  8360. (reverse org-stored-links) "\n")))
  8361. (goto-char (point-min)))
  8362. (let ((cw (selected-window)))
  8363. (select-window (get-buffer-window "*Org Links*" 'visible))
  8364. (with-current-buffer "*Org Links*" (setq truncate-lines t))
  8365. (unless (pos-visible-in-window-p (point-max))
  8366. (org-fit-window-to-buffer))
  8367. (and (window-live-p cw) (select-window cw)))
  8368. ;; Fake a link history, containing the stored links.
  8369. (setq tmphist (append (mapcar 'car org-stored-links)
  8370. org-insert-link-history))
  8371. (setq all-prefixes (append (mapcar 'car abbrevs)
  8372. (mapcar 'car org-link-abbrev-alist)
  8373. org-link-types))
  8374. (unwind-protect
  8375. (progn
  8376. (setq link
  8377. (let ((org-completion-use-ido nil)
  8378. (org-completion-use-iswitchb nil))
  8379. (org-completing-read
  8380. "Link: "
  8381. (append
  8382. (mapcar (lambda (x) (list (concat x ":")))
  8383. all-prefixes)
  8384. (mapcar 'car org-stored-links)
  8385. (mapcar 'cadr org-stored-links))
  8386. nil nil nil
  8387. 'tmphist
  8388. (caar org-stored-links))))
  8389. (if (not (string-match "\\S-" link))
  8390. (error "No link selected"))
  8391. (mapc (lambda(l)
  8392. (when (equal link (cadr l)) (setq link (car l) auto-desc t)))
  8393. org-stored-links)
  8394. (if (or (member link all-prefixes)
  8395. (and (equal ":" (substring link -1))
  8396. (member (substring link 0 -1) all-prefixes)
  8397. (setq link (substring link 0 -1))))
  8398. (setq link (with-current-buffer origbuf
  8399. (org-link-try-special-completion link)))))
  8400. (set-window-configuration wcf)
  8401. (kill-buffer "*Org Links*"))
  8402. (setq entry (assoc link org-stored-links))
  8403. (or entry (push link org-insert-link-history))
  8404. (setq desc (or desc (nth 1 entry)))))
  8405. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  8406. (not org-keep-stored-link-after-insertion))
  8407. (setq org-stored-links (delq (assoc link org-stored-links)
  8408. org-stored-links)))
  8409. (if (string-match org-plain-link-re link)
  8410. ;; URL-like link, normalize the use of angular brackets.
  8411. (setq link (org-remove-angle-brackets link)))
  8412. ;; Check if we are linking to the current file with a search option
  8413. ;; If yes, simplify the link by using only the search option.
  8414. (when (and buffer-file-name
  8415. (string-match "^file:\\(.+?\\)::\\([^>]+\\)" link))
  8416. (let* ((path (match-string 1 link))
  8417. (case-fold-search nil)
  8418. (search (match-string 2 link)))
  8419. (save-match-data
  8420. (if (equal (file-truename buffer-file-name) (file-truename path))
  8421. ;; We are linking to this same file, with a search option
  8422. (setq link search)))))
  8423. ;; Check if we can/should use a relative path. If yes, simplify the link
  8424. (when (string-match "^\\(file:\\|docview:\\)\\(.*\\)" link)
  8425. (let* ((type (match-string 1 link))
  8426. (path (match-string 2 link))
  8427. (origpath path)
  8428. (case-fold-search nil))
  8429. (cond
  8430. ((or (eq org-link-file-path-type 'absolute)
  8431. (equal complete-file '(16)))
  8432. (setq path (abbreviate-file-name (expand-file-name path))))
  8433. ((eq org-link-file-path-type 'noabbrev)
  8434. (setq path (expand-file-name path)))
  8435. ((eq org-link-file-path-type 'relative)
  8436. (setq path (file-relative-name path)))
  8437. (t
  8438. (save-match-data
  8439. (if (string-match (concat "^" (regexp-quote
  8440. (expand-file-name
  8441. (file-name-as-directory
  8442. default-directory))))
  8443. (expand-file-name path))
  8444. ;; We are linking a file with relative path name.
  8445. (setq path (substring (expand-file-name path)
  8446. (match-end 0)))
  8447. (setq path (abbreviate-file-name (expand-file-name path)))))))
  8448. (setq link (concat type path))
  8449. (if (equal desc origpath)
  8450. (setq desc path))))
  8451. (if org-make-link-description-function
  8452. (setq desc
  8453. (or (condition-case nil
  8454. (funcall org-make-link-description-function link desc)
  8455. (error (progn (message "Can't get link description from `%s'"
  8456. (symbol-name org-make-link-description-function))
  8457. (sit-for 2) nil)))
  8458. (read-string "Description: " default-description)))
  8459. (if default-description (setq desc default-description)
  8460. (setq desc (or (and auto-desc desc)
  8461. (read-string "Description: " desc)))))
  8462. (unless (string-match "\\S-" desc) (setq desc nil))
  8463. (if remove (apply 'delete-region remove))
  8464. (insert (org-make-link-string link desc))))
  8465. (defun org-link-try-special-completion (type)
  8466. "If there is completion support for link type TYPE, offer it."
  8467. (let ((fun (intern (concat "org-" type "-complete-link"))))
  8468. (if (functionp fun)
  8469. (funcall fun)
  8470. (read-string "Link (no completion support): " (concat type ":")))))
  8471. (defun org-file-complete-link (&optional arg)
  8472. "Create a file link using completion."
  8473. (let (file link)
  8474. (setq file (read-file-name "File: "))
  8475. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  8476. (pwd1 (file-name-as-directory (abbreviate-file-name
  8477. (expand-file-name ".")))))
  8478. (cond
  8479. ((equal arg '(16))
  8480. (setq link (concat
  8481. "file:"
  8482. (abbreviate-file-name (expand-file-name file)))))
  8483. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  8484. (setq link (concat "file:" (match-string 1 file))))
  8485. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  8486. (expand-file-name file))
  8487. (setq link (concat
  8488. "file:" (match-string 1 (expand-file-name file)))))
  8489. (t (setq link (concat "file:" file)))))
  8490. link))
  8491. (defun org-completing-read (&rest args)
  8492. "Completing-read with SPACE being a normal character."
  8493. (let ((enable-recursive-minibuffers t)
  8494. (minibuffer-local-completion-map
  8495. (copy-keymap minibuffer-local-completion-map)))
  8496. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  8497. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  8498. (org-defkey minibuffer-local-completion-map (kbd "C-c !") 'org-time-stamp-inactive)
  8499. (apply 'org-icompleting-read args)))
  8500. (defun org-completing-read-no-i (&rest args)
  8501. (let (org-completion-use-ido org-completion-use-iswitchb)
  8502. (apply 'org-completing-read args)))
  8503. (defun org-iswitchb-completing-read (prompt choices &rest args)
  8504. "Use iswitch as a completing-read replacement to choose from choices.
  8505. PROMPT is a string to prompt with. CHOICES is a list of strings to choose
  8506. from."
  8507. (let* ((iswitchb-use-virtual-buffers nil)
  8508. (iswitchb-make-buflist-hook
  8509. (lambda ()
  8510. (setq iswitchb-temp-buflist choices))))
  8511. (iswitchb-read-buffer prompt)))
  8512. (defun org-icompleting-read (&rest args)
  8513. "Completing-read using `ido-mode' or `iswitchb' speedups if available."
  8514. (org-without-partial-completion
  8515. (if (and org-completion-use-ido
  8516. (fboundp 'ido-completing-read)
  8517. (boundp 'ido-mode) ido-mode
  8518. (listp (second args)))
  8519. (let ((ido-enter-matching-directory nil))
  8520. (apply 'ido-completing-read (concat (car args))
  8521. (if (consp (car (nth 1 args)))
  8522. (mapcar 'car (nth 1 args))
  8523. (nth 1 args))
  8524. (cddr args)))
  8525. (if (and org-completion-use-iswitchb
  8526. (boundp 'iswitchb-mode) iswitchb-mode
  8527. (listp (second args)))
  8528. (apply 'org-iswitchb-completing-read (concat (car args))
  8529. (if (consp (car (nth 1 args)))
  8530. (mapcar 'car (nth 1 args))
  8531. (nth 1 args))
  8532. (cddr args))
  8533. (apply 'completing-read args)))))
  8534. (defun org-extract-attributes (s)
  8535. "Extract the attributes cookie from a string and set as text property."
  8536. (let (a attr (start 0) key value)
  8537. (save-match-data
  8538. (when (string-match "{{\\([^}]+\\)}}$" s)
  8539. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  8540. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  8541. (setq key (match-string 1 a) value (match-string 2 a)
  8542. start (match-end 0)
  8543. attr (plist-put attr (intern key) value))))
  8544. (org-add-props s nil 'org-attr attr))
  8545. s))
  8546. (defun org-extract-attributes-from-string (tag)
  8547. (let (key value attr)
  8548. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"\\s-?" tag)
  8549. (setq key (match-string 1 tag) value (match-string 2 tag)
  8550. tag (replace-match "" t t tag)
  8551. attr (plist-put attr (intern key) value)))
  8552. (cons tag attr)))
  8553. (defun org-attributes-to-string (plist)
  8554. "Format a property list into an HTML attribute list."
  8555. (let ((s "") key value)
  8556. (while plist
  8557. (setq key (pop plist) value (pop plist))
  8558. (and value
  8559. (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
  8560. s))
  8561. ;;; Opening/following a link
  8562. (defvar org-link-search-failed nil)
  8563. (defvar org-open-link-functions nil
  8564. "Hook for functions finding a plain text link.
  8565. These functions must take a single argument, the link content.
  8566. They will be called for links that look like [[link text][description]]
  8567. when LINK TEXT does not have a protocol like \"http:\" and does not look
  8568. like a filename (e.g. \"./blue.png\").
  8569. These functions will be called *before* Org attempts to resolve the
  8570. link by doing text searches in the current buffer - so if you want a
  8571. link \"[[target]]\" to still find \"<<target>>\", your function should
  8572. handle this as a special case.
  8573. When the function does handle the link, it must return a non-nil value.
  8574. If it decides that it is not responsible for this link, it must return
  8575. nil to indicate that that Org-mode can continue with other options
  8576. like exact and fuzzy text search.")
  8577. (defun org-next-link ()
  8578. "Move forward to the next link.
  8579. If the link is in hidden text, expose it."
  8580. (interactive)
  8581. (when (and org-link-search-failed (eq this-command last-command))
  8582. (goto-char (point-min))
  8583. (message "Link search wrapped back to beginning of buffer"))
  8584. (setq org-link-search-failed nil)
  8585. (let* ((pos (point))
  8586. (ct (org-context))
  8587. (a (assoc :link ct)))
  8588. (if a (goto-char (nth 2 a)))
  8589. (if (re-search-forward org-any-link-re nil t)
  8590. (progn
  8591. (goto-char (match-beginning 0))
  8592. (if (outline-invisible-p) (org-show-context)))
  8593. (goto-char pos)
  8594. (setq org-link-search-failed t)
  8595. (error "No further link found"))))
  8596. (defun org-previous-link ()
  8597. "Move backward to the previous link.
  8598. If the link is in hidden text, expose it."
  8599. (interactive)
  8600. (when (and org-link-search-failed (eq this-command last-command))
  8601. (goto-char (point-max))
  8602. (message "Link search wrapped back to end of buffer"))
  8603. (setq org-link-search-failed nil)
  8604. (let* ((pos (point))
  8605. (ct (org-context))
  8606. (a (assoc :link ct)))
  8607. (if a (goto-char (nth 1 a)))
  8608. (if (re-search-backward org-any-link-re nil t)
  8609. (progn
  8610. (goto-char (match-beginning 0))
  8611. (if (outline-invisible-p) (org-show-context)))
  8612. (goto-char pos)
  8613. (setq org-link-search-failed t)
  8614. (error "No further link found"))))
  8615. (defun org-translate-link (s)
  8616. "Translate a link string if a translation function has been defined."
  8617. (if (and org-link-translation-function
  8618. (fboundp org-link-translation-function)
  8619. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  8620. (progn
  8621. (setq s (funcall org-link-translation-function
  8622. (match-string 1 s) (match-string 2 s)))
  8623. (concat (car s) ":" (cdr s)))
  8624. s))
  8625. (defun org-translate-link-from-planner (type path)
  8626. "Translate a link from Emacs Planner syntax so that Org can follow it.
  8627. This is still an experimental function, your mileage may vary."
  8628. (cond
  8629. ((member type '("http" "https" "news" "ftp"))
  8630. ;; standard Internet links are the same.
  8631. nil)
  8632. ((and (equal type "irc") (string-match "^//" path))
  8633. ;; Planner has two / at the beginning of an irc link, we have 1.
  8634. ;; We should have zero, actually....
  8635. (setq path (substring path 1)))
  8636. ((and (equal type "lisp") (string-match "^/" path))
  8637. ;; Planner has a slash, we do not.
  8638. (setq type "elisp" path (substring path 1)))
  8639. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  8640. ;; A typical message link. Planner has the id after the final slash,
  8641. ;; we separate it with a hash mark
  8642. (setq path (concat (match-string 1 path) "#"
  8643. (org-remove-angle-brackets (match-string 2 path)))))
  8644. )
  8645. (cons type path))
  8646. (defun org-find-file-at-mouse (ev)
  8647. "Open file link or URL at mouse."
  8648. (interactive "e")
  8649. (mouse-set-point ev)
  8650. (org-open-at-point 'in-emacs))
  8651. (defun org-open-at-mouse (ev)
  8652. "Open file link or URL at mouse.
  8653. See the docstring of `org-open-file' for details."
  8654. (interactive "e")
  8655. (mouse-set-point ev)
  8656. (if (eq major-mode 'org-agenda-mode)
  8657. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  8658. (org-open-at-point))
  8659. (defvar org-window-config-before-follow-link nil
  8660. "The window configuration before following a link.
  8661. This is saved in case the need arises to restore it.")
  8662. (defvar org-open-link-marker (make-marker)
  8663. "Marker pointing to the location where `org-open-at-point; was called.")
  8664. ;;;###autoload
  8665. (defun org-open-at-point-global ()
  8666. "Follow a link like Org-mode does.
  8667. This command can be called in any mode to follow a link that has
  8668. Org-mode syntax."
  8669. (interactive)
  8670. (org-run-like-in-org-mode 'org-open-at-point))
  8671. ;;;###autoload
  8672. (defun org-open-link-from-string (s &optional arg reference-buffer)
  8673. "Open a link in the string S, as if it was in Org-mode."
  8674. (interactive "sLink: \nP")
  8675. (let ((reference-buffer (or reference-buffer (current-buffer))))
  8676. (with-temp-buffer
  8677. (let ((org-inhibit-startup (not reference-buffer)))
  8678. (org-mode)
  8679. (insert s)
  8680. (goto-char (point-min))
  8681. (when reference-buffer
  8682. (setq org-link-abbrev-alist-local
  8683. (with-current-buffer reference-buffer
  8684. org-link-abbrev-alist-local)))
  8685. (org-open-at-point arg reference-buffer)))))
  8686. (defvar org-open-at-point-functions nil
  8687. "Hook that is run when following a link at point.
  8688. Functions in this hook must return t if they identify and follow
  8689. a link at point. If they don't find anything interesting at point,
  8690. they must return nil.")
  8691. (defvar clean-buffer-list-kill-buffer-names) ; Defined in midnight.el
  8692. (defun org-open-at-point (&optional arg reference-buffer)
  8693. "Open link at or after point.
  8694. If there is no link at point, this function will search forward up to
  8695. the end of the current line.
  8696. Normally, files will be opened by an appropriate application. If the
  8697. optional prefix argument ARG is non-nil, Emacs will visit the file.
  8698. With a double prefix argument, try to open outside of Emacs, in the
  8699. application the system uses for this file type."
  8700. (interactive "P")
  8701. ;; if in a code block, then open the block's results
  8702. (unless (call-interactively #'org-babel-open-src-block-result)
  8703. (org-load-modules-maybe)
  8704. (move-marker org-open-link-marker (point))
  8705. (setq org-window-config-before-follow-link (current-window-configuration))
  8706. (org-remove-occur-highlights nil nil t)
  8707. (cond
  8708. ((and (org-at-heading-p)
  8709. (not (org-at-timestamp-p t))
  8710. (not (org-in-regexp
  8711. (concat org-plain-link-re "\\|"
  8712. org-bracket-link-regexp "\\|"
  8713. org-angle-link-re "\\|"
  8714. "[ \t]:[^ \t\n]+:[ \t]*$")))
  8715. (not (get-text-property (point) 'org-linked-text)))
  8716. (or (org-offer-links-in-entry arg)
  8717. (progn (require 'org-attach) (org-attach-reveal 'if-exists))))
  8718. ((run-hook-with-args-until-success 'org-open-at-point-functions))
  8719. ((and (org-at-timestamp-p t)
  8720. (not (org-in-regexp org-bracket-link-regexp)))
  8721. (org-follow-timestamp-link))
  8722. ((and (or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  8723. (not (org-in-regexp org-bracket-link-regexp)))
  8724. (org-footnote-action))
  8725. (t
  8726. (let (type path link line search (pos (point)))
  8727. (catch 'match
  8728. (save-excursion
  8729. (skip-chars-forward "^]\n\r")
  8730. (when (org-in-regexp org-bracket-link-regexp 1)
  8731. (setq link (org-extract-attributes
  8732. (org-link-unescape (org-match-string-no-properties 1))))
  8733. (while (string-match " *\n *" link)
  8734. (setq link (replace-match " " t t link)))
  8735. (setq link (org-link-expand-abbrev link))
  8736. (cond
  8737. ((or (file-name-absolute-p link)
  8738. (string-match "^\\.\\.?/" link))
  8739. (setq type "file" path link))
  8740. ((string-match org-link-re-with-space3 link)
  8741. (setq type (match-string 1 link) path (match-string 2 link)))
  8742. ((string-match "^help:+\\(.+\\)" link)
  8743. (setq type "help" path (match-string 1 link)))
  8744. (t (setq type "thisfile" path link)))
  8745. (throw 'match t)))
  8746. (when (get-text-property (point) 'org-linked-text)
  8747. (setq type "thisfile"
  8748. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  8749. (1+ (point)) (point))
  8750. path (buffer-substring
  8751. (or (previous-single-property-change pos 'org-linked-text)
  8752. (point-min))
  8753. (or (next-single-property-change pos 'org-linked-text)
  8754. (point-max))))
  8755. (throw 'match t))
  8756. (save-excursion
  8757. (when (or (org-in-regexp org-angle-link-re)
  8758. (and (goto-char (car (org-in-regexp org-plain-link-re)))
  8759. (save-match-data (not (looking-back "\\[\\[")))))
  8760. (setq type (match-string 1)
  8761. path (org-link-unescape (match-string 2)))
  8762. (throw 'match t)))
  8763. (save-excursion
  8764. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@#%:]+\\):[ \t]*$"))
  8765. (setq type "tags"
  8766. path (match-string 1))
  8767. (while (string-match ":" path)
  8768. (setq path (replace-match "+" t t path)))
  8769. (throw 'match t)))
  8770. (when (org-in-regexp "<\\([^><\n]+\\)>")
  8771. (setq type "tree-match"
  8772. path (match-string 1))
  8773. (throw 'match t)))
  8774. (unless path
  8775. (error "No link found"))
  8776. ;; switch back to reference buffer
  8777. ;; needed when if called in a temporary buffer through
  8778. ;; org-open-link-from-string
  8779. (with-current-buffer (or reference-buffer (current-buffer))
  8780. ;; Remove any trailing spaces in path
  8781. (if (string-match " +\\'" path)
  8782. (setq path (replace-match "" t t path)))
  8783. (if (and org-link-translation-function
  8784. (fboundp org-link-translation-function))
  8785. ;; Check if we need to translate the link
  8786. (let ((tmp (funcall org-link-translation-function type path)))
  8787. (setq type (car tmp) path (cdr tmp))))
  8788. (cond
  8789. ((assoc type org-link-protocols)
  8790. (funcall (nth 1 (assoc type org-link-protocols)) path))
  8791. ((equal type "help")
  8792. (let ((f-or-v (intern path)))
  8793. (cond ((fboundp f-or-v)
  8794. (describe-function f-or-v))
  8795. ((boundp f-or-v)
  8796. (describe-variable f-or-v))
  8797. (t (error "Not a known function or variable")))))
  8798. ((equal type "mailto")
  8799. (let ((cmd (car org-link-mailto-program))
  8800. (args (cdr org-link-mailto-program)) args1
  8801. (address path) (subject "") a)
  8802. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  8803. (setq address (match-string 1 path)
  8804. subject (org-link-escape (match-string 2 path))))
  8805. (while args
  8806. (cond
  8807. ((not (stringp (car args))) (push (pop args) args1))
  8808. (t (setq a (pop args))
  8809. (if (string-match "%a" a)
  8810. (setq a (replace-match address t t a)))
  8811. (if (string-match "%s" a)
  8812. (setq a (replace-match subject t t a)))
  8813. (push a args1))))
  8814. (apply cmd (nreverse args1))))
  8815. ((member type '("http" "https" "ftp" "news"))
  8816. (browse-url (concat type ":" (if (org-string-match-p "[[:nonascii:] ]" path)
  8817. (org-link-escape
  8818. path org-link-escape-chars-browser)
  8819. path))))
  8820. ((string= type "doi")
  8821. (browse-url (concat org-doi-server-url (if (org-string-match-p "[[:nonascii:] ]" path)
  8822. (org-link-escape
  8823. path org-link-escape-chars-browser)
  8824. path))))
  8825. ((member type '("message"))
  8826. (browse-url (concat type ":" path)))
  8827. ((string= type "tags")
  8828. (org-tags-view arg path))
  8829. ((string= type "tree-match")
  8830. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  8831. ((string= type "file")
  8832. (if (string-match "::\\([0-9]+\\)\\'" path)
  8833. (setq line (string-to-number (match-string 1 path))
  8834. path (substring path 0 (match-beginning 0)))
  8835. (if (string-match "::\\(.+\\)\\'" path)
  8836. (setq search (match-string 1 path)
  8837. path (substring path 0 (match-beginning 0)))))
  8838. (if (string-match "[*?{]" (file-name-nondirectory path))
  8839. (dired path)
  8840. (org-open-file path arg line search)))
  8841. ((string= type "shell")
  8842. (let ((buf (generate-new-buffer "*Org Shell Output"))
  8843. (cmd path))
  8844. (if (or (and (not (string= org-confirm-shell-link-not-regexp ""))
  8845. (string-match org-confirm-shell-link-not-regexp cmd))
  8846. (not org-confirm-shell-link-function)
  8847. (funcall org-confirm-shell-link-function
  8848. (format "Execute \"%s\" in shell? "
  8849. (org-add-props cmd nil
  8850. 'face 'org-warning))))
  8851. (progn
  8852. (message "Executing %s" cmd)
  8853. (shell-command cmd buf)
  8854. (if (featurep 'midnight)
  8855. (setq clean-buffer-list-kill-buffer-names
  8856. (cons buf clean-buffer-list-kill-buffer-names))))
  8857. (error "Abort"))))
  8858. ((string= type "elisp")
  8859. (let ((cmd path))
  8860. (if (or (and (not (string= org-confirm-elisp-link-not-regexp ""))
  8861. (string-match org-confirm-elisp-link-not-regexp cmd))
  8862. (not org-confirm-elisp-link-function)
  8863. (funcall org-confirm-elisp-link-function
  8864. (format "Execute \"%s\" as elisp? "
  8865. (org-add-props cmd nil
  8866. 'face 'org-warning))))
  8867. (message "%s => %s" cmd
  8868. (if (equal (string-to-char cmd) ?\()
  8869. (eval (read cmd))
  8870. (call-interactively (read cmd))))
  8871. (error "Abort"))))
  8872. ((and (string= type "thisfile")
  8873. (run-hook-with-args-until-success
  8874. 'org-open-link-functions path)))
  8875. ((string= type "thisfile")
  8876. (if arg
  8877. (switch-to-buffer-other-window
  8878. (org-get-buffer-for-internal-link (current-buffer)))
  8879. (org-mark-ring-push))
  8880. (let ((cmd `(org-link-search
  8881. ,path
  8882. ,(cond ((equal arg '(4)) ''occur)
  8883. ((equal arg '(16)) ''org-occur))
  8884. ,pos)))
  8885. (condition-case nil (let ((org-link-search-inhibit-query t))
  8886. (eval cmd))
  8887. (error (progn (widen) (eval cmd))))))
  8888. (t (browse-url-at-point)))))))
  8889. (move-marker org-open-link-marker nil)
  8890. (run-hook-with-args 'org-follow-link-hook)))
  8891. (defun org-offer-links-in-entry (&optional nth zero)
  8892. "Offer links in the current entry and follow the selected link.
  8893. If there is only one link, follow it immediately as well.
  8894. If NTH is an integer, immediately pick the NTH link found.
  8895. If ZERO is a string, check also this string for a link, and if
  8896. there is one, offer it as link number zero."
  8897. (let ((re (concat "\\(" org-bracket-link-regexp "\\)\\|"
  8898. "\\(" org-angle-link-re "\\)\\|"
  8899. "\\(" org-plain-link-re "\\)"))
  8900. (cnt ?0)
  8901. (in-emacs (if (integerp nth) nil nth))
  8902. have-zero end links link c)
  8903. (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
  8904. (push (match-string 0 zero) links)
  8905. (setq cnt (1- cnt) have-zero t))
  8906. (save-excursion
  8907. (org-back-to-heading t)
  8908. (setq end (save-excursion (outline-next-heading) (point)))
  8909. (while (re-search-forward re end t)
  8910. (push (match-string 0) links))
  8911. (setq links (org-uniquify (reverse links))))
  8912. (cond
  8913. ((null links)
  8914. (message "No links"))
  8915. ((equal (length links) 1)
  8916. (setq link (list (car links))))
  8917. ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
  8918. (setq link (list (nth (if have-zero nth (1- nth)) links))))
  8919. (t ; we have to select a link
  8920. (save-excursion
  8921. (save-window-excursion
  8922. (delete-other-windows)
  8923. (with-output-to-temp-buffer "*Select Link*"
  8924. (mapc (lambda (l)
  8925. (if (not (string-match org-bracket-link-regexp l))
  8926. (princ (format "[%c] %s\n" (incf cnt)
  8927. (org-remove-angle-brackets l)))
  8928. (if (match-end 3)
  8929. (princ (format "[%c] %s (%s)\n" (incf cnt)
  8930. (match-string 3 l) (match-string 1 l)))
  8931. (princ (format "[%c] %s\n" (incf cnt)
  8932. (match-string 1 l))))))
  8933. links))
  8934. (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
  8935. (message "Select link to open, RET to open all:")
  8936. (setq c (read-char-exclusive))
  8937. (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
  8938. (when (equal c ?q) (error "Abort"))
  8939. (if (equal c ?\C-m)
  8940. (setq link links)
  8941. (setq nth (- c ?0))
  8942. (if have-zero (setq nth (1+ nth)))
  8943. (unless (and (integerp nth) (>= (length links) nth))
  8944. (error "Invalid link selection"))
  8945. (setq link (list (nth (1- nth) links))))))
  8946. (if link
  8947. (let ((buf (current-buffer)))
  8948. (dolist (l link)
  8949. (org-open-link-from-string l in-emacs buf))
  8950. t)
  8951. nil)))
  8952. ;; Add special file links that specify the way of opening
  8953. (org-add-link-type "file+sys" 'org-open-file-with-system)
  8954. (org-add-link-type "file+emacs" 'org-open-file-with-emacs)
  8955. (defun org-open-file-with-system (path)
  8956. "Open file at PATH using the system way of opening it."
  8957. (org-open-file path 'system))
  8958. (defun org-open-file-with-emacs (path)
  8959. "Open file at PATH in Emacs."
  8960. (org-open-file path 'emacs))
  8961. (defun org-remove-file-link-modifiers ()
  8962. "Remove the file link modifiers in `file+sys:' and `file+emacs:' links."
  8963. (goto-char (point-min))
  8964. (while (re-search-forward "\\<file\\+\\(sys\\|emacs\\):" nil t)
  8965. (org-if-unprotected
  8966. (replace-match "file:" t t))))
  8967. (eval-after-load "org-exp"
  8968. '(add-hook 'org-export-preprocess-before-normalizing-links-hook
  8969. 'org-remove-file-link-modifiers))
  8970. ;;;; Time estimates
  8971. (defun org-get-effort (&optional pom)
  8972. "Get the effort estimate for the current entry."
  8973. (org-entry-get pom org-effort-property))
  8974. ;;; File search
  8975. (defvar org-create-file-search-functions nil
  8976. "List of functions to construct the right search string for a file link.
  8977. These functions are called in turn with point at the location to
  8978. which the link should point.
  8979. A function in the hook should first test if it would like to
  8980. handle this file type, for example by checking the `major-mode'
  8981. or the file extension. If it decides not to handle this file, it
  8982. should just return nil to give other functions a chance. If it
  8983. does handle the file, it must return the search string to be used
  8984. when following the link. The search string will be part of the
  8985. file link, given after a double colon, and `org-open-at-point'
  8986. will automatically search for it. If special measures must be
  8987. taken to make the search successful, another function should be
  8988. added to the companion hook `org-execute-file-search-functions',
  8989. which see.
  8990. A function in this hook may also use `setq' to set the variable
  8991. `description' to provide a suggestion for the descriptive text to
  8992. be used for this link when it gets inserted into an Org-mode
  8993. buffer with \\[org-insert-link].")
  8994. (defvar org-execute-file-search-functions nil
  8995. "List of functions to execute a file search triggered by a link.
  8996. Functions added to this hook must accept a single argument, the
  8997. search string that was part of the file link, the part after the
  8998. double colon. The function must first check if it would like to
  8999. handle this search, for example by checking the `major-mode' or
  9000. the file extension. If it decides not to handle this search, it
  9001. should just return nil to give other functions a chance. If it
  9002. does handle the search, it must return a non-nil value to keep
  9003. other functions from trying.
  9004. Each function can access the current prefix argument through the
  9005. variable `current-prefix-argument'. Note that a single prefix is
  9006. used to force opening a link in Emacs, so it may be good to only
  9007. use a numeric or double prefix to guide the search function.
  9008. In case this is needed, a function in this hook can also restore
  9009. the window configuration before `org-open-at-point' was called using:
  9010. (set-window-configuration org-window-config-before-follow-link)")
  9011. (defvar org-link-search-inhibit-query nil) ;; dynamically scoped
  9012. (defun org-link-search (s &optional type avoid-pos stealth)
  9013. "Search for a link search option.
  9014. If S is surrounded by forward slashes, it is interpreted as a
  9015. regular expression. In org-mode files, this will create an `org-occur'
  9016. sparse tree. In ordinary files, `occur' will be used to list matches.
  9017. If the current buffer is in `dired-mode', grep will be used to search
  9018. in all files. If AVOID-POS is given, ignore matches near that position.
  9019. When optional argument STEALTH is non-nil, do not modify
  9020. visibility around point, thus ignoring
  9021. `org-show-hierarchy-above', `org-show-following-heading' and
  9022. `org-show-siblings' variables."
  9023. (let ((case-fold-search t)
  9024. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  9025. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  9026. (append '(("") (" ") ("\t") ("\n"))
  9027. org-emphasis-alist)
  9028. "\\|") "\\)"))
  9029. (pos (point))
  9030. (pre nil) (post nil)
  9031. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  9032. (cond
  9033. ;; First check if there are any special search functions
  9034. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  9035. ;; Now try the builtin stuff
  9036. ((and (equal (string-to-char s0) ?#)
  9037. (> (length s0) 1)
  9038. (save-excursion
  9039. (goto-char (point-min))
  9040. (and
  9041. (re-search-forward
  9042. (concat "^[ \t]*:CUSTOM_ID:[ \t]+" (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
  9043. (setq type 'dedicated
  9044. pos (match-beginning 0))))
  9045. ;; There is an exact target for this
  9046. (goto-char pos)
  9047. (org-back-to-heading t)))
  9048. ((save-excursion
  9049. (goto-char (point-min))
  9050. (and
  9051. (re-search-forward
  9052. (concat "<<" (regexp-quote s0) ">>") nil t)
  9053. (setq type 'dedicated
  9054. pos (match-beginning 0))))
  9055. ;; There is an exact target for this
  9056. (goto-char pos))
  9057. ((save-excursion
  9058. (goto-char (point-min))
  9059. (and
  9060. (re-search-forward
  9061. (format "^[ \t]*#\\+TARGET: %s" (regexp-quote s0)) nil t)
  9062. (setq type 'dedicated pos (match-beginning 0))))
  9063. ;; Found an invisible target.
  9064. (goto-char pos))
  9065. ((save-excursion
  9066. (goto-char (point-min))
  9067. (and
  9068. (re-search-forward
  9069. (format "^[ \t]*#\\+NAME: %s" (regexp-quote s0)) nil t)
  9070. (setq type 'dedicated pos (match-beginning 0))))
  9071. ;; Found an element with a matching #+name affiliated keyword.
  9072. (goto-char pos))
  9073. ((and (string-match "^(\\(.*\\))$" s0)
  9074. (save-excursion
  9075. (goto-char (point-min))
  9076. (and
  9077. (re-search-forward
  9078. (concat "[^[]" (regexp-quote
  9079. (format org-coderef-label-format
  9080. (match-string 1 s0))))
  9081. nil t)
  9082. (setq type 'dedicated
  9083. pos (1+ (match-beginning 0))))))
  9084. ;; There is a coderef target for this
  9085. (goto-char pos))
  9086. ((string-match "^/\\(.*\\)/$" s)
  9087. ;; A regular expression
  9088. (cond
  9089. ((derived-mode-p 'org-mode)
  9090. (org-occur (match-string 1 s)))
  9091. ;;((eq major-mode 'dired-mode)
  9092. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  9093. (t (org-do-occur (match-string 1 s)))))
  9094. ((and (derived-mode-p 'org-mode) org-link-search-must-match-exact-headline)
  9095. (and (equal (string-to-char s) ?*) (setq s (substring s 1)))
  9096. (goto-char (point-min))
  9097. (cond
  9098. ((let (case-fold-search)
  9099. (re-search-forward (format org-complex-heading-regexp-format
  9100. (regexp-quote s))
  9101. nil t))
  9102. ;; OK, found a match
  9103. (setq type 'dedicated)
  9104. (goto-char (match-beginning 0)))
  9105. ((and (not org-link-search-inhibit-query)
  9106. (eq org-link-search-must-match-exact-headline 'query-to-create)
  9107. (y-or-n-p "No match - create this as a new heading? "))
  9108. (goto-char (point-max))
  9109. (or (bolp) (newline))
  9110. (insert "* " s "\n")
  9111. (beginning-of-line 0))
  9112. (t
  9113. (goto-char pos)
  9114. (error "No match"))))
  9115. (t
  9116. ;; A normal search string
  9117. (when (equal (string-to-char s) ?*)
  9118. ;; Anchor on headlines, post may include tags.
  9119. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  9120. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@#%:+]:[ \t]*\\)?$")
  9121. s (substring s 1)))
  9122. (remove-text-properties
  9123. 0 (length s)
  9124. '(face nil mouse-face nil keymap nil fontified nil) s)
  9125. ;; Make a series of regular expressions to find a match
  9126. (setq words (org-split-string s "[ \n\r\t]+")
  9127. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  9128. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  9129. "\\)" markers)
  9130. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  9131. re2a (concat "[ \t\r\n]" re2a_)
  9132. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  9133. re4 (concat "[^a-zA-Z_]" re4_)
  9134. re1 (concat pre re2 post)
  9135. re3 (concat pre (if pre re4_ re4) post)
  9136. re5 (concat pre ".*" re4)
  9137. re2 (concat pre re2)
  9138. re2a (concat pre (if pre re2a_ re2a))
  9139. re4 (concat pre (if pre re4_ re4))
  9140. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  9141. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  9142. re5 "\\)"
  9143. ))
  9144. (cond
  9145. ((eq type 'org-occur) (org-occur reall))
  9146. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  9147. (t (goto-char (point-min))
  9148. (setq type 'fuzzy)
  9149. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  9150. (org-search-not-self 1 re1 nil t)
  9151. (org-search-not-self 1 re2 nil t)
  9152. (org-search-not-self 1 re2a nil t)
  9153. (org-search-not-self 1 re3 nil t)
  9154. (org-search-not-self 1 re4 nil t)
  9155. (org-search-not-self 1 re5 nil t)
  9156. )
  9157. (goto-char (match-beginning 1))
  9158. (goto-char pos)
  9159. (error "No match"))))))
  9160. (and (derived-mode-p 'org-mode)
  9161. (not stealth)
  9162. (org-show-context 'link-search))
  9163. type))
  9164. (defun org-search-not-self (group &rest args)
  9165. "Execute `re-search-forward', but only accept matches that do not
  9166. enclose the position of `org-open-link-marker'."
  9167. (let ((m org-open-link-marker))
  9168. (catch 'exit
  9169. (while (apply 're-search-forward args)
  9170. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  9171. (goto-char (match-end group))
  9172. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  9173. (> (match-beginning 0) (marker-position m))
  9174. (< (match-end 0) (marker-position m)))
  9175. (save-match-data
  9176. (or (not (org-in-regexp
  9177. org-bracket-link-analytic-regexp 1))
  9178. (not (match-end 4)) ; no description
  9179. (and (<= (match-beginning 4) (point))
  9180. (>= (match-end 4) (point))))))
  9181. (throw 'exit (point))))))))
  9182. (defun org-get-buffer-for-internal-link (buffer)
  9183. "Return a buffer to be used for displaying the link target of internal links."
  9184. (cond
  9185. ((not org-display-internal-link-with-indirect-buffer)
  9186. buffer)
  9187. ((string-match "(Clone)$" (buffer-name buffer))
  9188. (message "Buffer is already a clone, not making another one")
  9189. ;; we also do not modify visibility in this case
  9190. buffer)
  9191. (t ; make a new indirect buffer for displaying the link
  9192. (let* ((bn (buffer-name buffer))
  9193. (ibn (concat bn "(Clone)"))
  9194. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  9195. (with-current-buffer ib (org-overview))
  9196. ib))))
  9197. (defun org-do-occur (regexp &optional cleanup)
  9198. "Call the Emacs command `occur'.
  9199. If CLEANUP is non-nil, remove the printout of the regular expression
  9200. in the *Occur* buffer. This is useful if the regex is long and not useful
  9201. to read."
  9202. (occur regexp)
  9203. (when cleanup
  9204. (let ((cwin (selected-window)) win beg end)
  9205. (when (setq win (get-buffer-window "*Occur*"))
  9206. (select-window win))
  9207. (goto-char (point-min))
  9208. (when (re-search-forward "match[a-z]+" nil t)
  9209. (setq beg (match-end 0))
  9210. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  9211. (setq end (1- (match-beginning 0)))))
  9212. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  9213. (goto-char (point-min))
  9214. (select-window cwin))))
  9215. ;;; The mark ring for links jumps
  9216. (defvar org-mark-ring nil
  9217. "Mark ring for positions before jumps in Org-mode.")
  9218. (defvar org-mark-ring-last-goto nil
  9219. "Last position in the mark ring used to go back.")
  9220. ;; Fill and close the ring
  9221. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  9222. (loop for i from 1 to org-mark-ring-length do
  9223. (push (make-marker) org-mark-ring))
  9224. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  9225. org-mark-ring)
  9226. (defun org-mark-ring-push (&optional pos buffer)
  9227. "Put the current position or POS into the mark ring and rotate it."
  9228. (interactive)
  9229. (setq pos (or pos (point)))
  9230. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  9231. (move-marker (car org-mark-ring)
  9232. (or pos (point))
  9233. (or buffer (current-buffer)))
  9234. (message "%s"
  9235. (substitute-command-keys
  9236. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  9237. (defun org-mark-ring-goto (&optional n)
  9238. "Jump to the previous position in the mark ring.
  9239. With prefix arg N, jump back that many stored positions. When
  9240. called several times in succession, walk through the entire ring.
  9241. Org-mode commands jumping to a different position in the current file,
  9242. or to another Org-mode file, automatically push the old position
  9243. onto the ring."
  9244. (interactive "p")
  9245. (let (p m)
  9246. (if (eq last-command this-command)
  9247. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  9248. (setq p org-mark-ring))
  9249. (setq org-mark-ring-last-goto p)
  9250. (setq m (car p))
  9251. (org-pop-to-buffer-same-window (marker-buffer m))
  9252. (goto-char m)
  9253. (if (or (outline-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  9254. (defun org-remove-angle-brackets (s)
  9255. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  9256. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  9257. s)
  9258. (defun org-add-angle-brackets (s)
  9259. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  9260. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  9261. s)
  9262. (defun org-remove-double-quotes (s)
  9263. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  9264. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  9265. s)
  9266. ;;; Following specific links
  9267. (defun org-follow-timestamp-link ()
  9268. "Open an agenda view for the time-stamp date/range at point."
  9269. (cond
  9270. ((org-at-date-range-p t)
  9271. (let ((org-agenda-start-on-weekday)
  9272. (t1 (match-string 1))
  9273. (t2 (match-string 2)) tt1 tt2)
  9274. (setq tt1 (time-to-days (org-time-string-to-time t1))
  9275. tt2 (time-to-days (org-time-string-to-time t2)))
  9276. (let ((org-agenda-buffer-tmp-name
  9277. (format "*Org Agenda(a:%s)"
  9278. (concat (substring t1 0 10) "--" (substring t2 0 10)))))
  9279. (org-agenda-list nil tt1 (1+ (- tt2 tt1))))))
  9280. ((org-at-timestamp-p t)
  9281. (let ((org-agenda-buffer-tmp-name
  9282. (format "*Org Agenda(a:%s)" (substring (match-string 1) 0 10))))
  9283. (org-agenda-list nil (time-to-days (org-time-string-to-time
  9284. (substring (match-string 1) 0 10)))
  9285. 1)))
  9286. (t (error "This should not happen"))))
  9287. ;;; Following file links
  9288. (declare-function mailcap-parse-mailcaps "mailcap" (&optional path force))
  9289. (declare-function mailcap-extension-to-mime "mailcap" (extn))
  9290. (declare-function mailcap-mime-info
  9291. "mailcap" (string &optional request no-decode))
  9292. (defvar org-wait nil)
  9293. (defun org-open-file (path &optional in-emacs line search)
  9294. "Open the file at PATH.
  9295. First, this expands any special file name abbreviations. Then the
  9296. configuration variable `org-file-apps' is checked if it contains an
  9297. entry for this file type, and if yes, the corresponding command is launched.
  9298. If no application is found, Emacs simply visits the file.
  9299. With optional prefix argument IN-EMACS, Emacs will visit the file.
  9300. With a double \\[universal-argument] \\[universal-argument] \
  9301. prefix arg, Org tries to avoid opening in Emacs
  9302. and to use an external application to visit the file.
  9303. Optional LINE specifies a line to go to, optional SEARCH a string
  9304. to search for. If LINE or SEARCH is given, the file will be
  9305. opened in Emacs, unless an entry from org-file-apps that makes
  9306. use of groups in a regexp matches.
  9307. If you want to change the way frames are used when following a
  9308. link, please customize `org-link-frame-setup'.
  9309. If the file does not exist, an error is thrown."
  9310. (let* ((file (if (equal path "")
  9311. buffer-file-name
  9312. (substitute-in-file-name (expand-file-name path))))
  9313. (file-apps (append org-file-apps (org-default-apps)))
  9314. (apps (org-remove-if
  9315. 'org-file-apps-entry-match-against-dlink-p file-apps))
  9316. (apps-dlink (org-remove-if-not
  9317. 'org-file-apps-entry-match-against-dlink-p file-apps))
  9318. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  9319. (dirp (if remp nil (file-directory-p file)))
  9320. (file (if (and dirp org-open-directory-means-index-dot-org)
  9321. (concat (file-name-as-directory file) "index.org")
  9322. file))
  9323. (a-m-a-p (assq 'auto-mode apps))
  9324. (dfile (downcase file))
  9325. ;; reconstruct the original file: link from the PATH, LINE and SEARCH args
  9326. (link (cond ((and (eq line nil)
  9327. (eq search nil))
  9328. file)
  9329. (line
  9330. (concat file "::" (number-to-string line)))
  9331. (search
  9332. (concat file "::" search))))
  9333. (dlink (downcase link))
  9334. (old-buffer (current-buffer))
  9335. (old-pos (point))
  9336. (old-mode major-mode)
  9337. ext cmd link-match-data)
  9338. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  9339. (setq ext (match-string 1 dfile))
  9340. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  9341. (setq ext (match-string 1 dfile))))
  9342. (cond
  9343. ((member in-emacs '((16) system))
  9344. (setq cmd (cdr (assoc 'system apps))))
  9345. (in-emacs (setq cmd 'emacs))
  9346. (t
  9347. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  9348. (and dirp (cdr (assoc 'directory apps)))
  9349. ; first, try matching against apps-dlink
  9350. ; if we get a match here, store the match data for later
  9351. (let ((match (assoc-default dlink apps-dlink
  9352. 'string-match)))
  9353. (if match
  9354. (progn (setq link-match-data (match-data))
  9355. match)
  9356. (progn (setq in-emacs (or in-emacs line search))
  9357. nil))) ; if we have no match in apps-dlink,
  9358. ; always open the file in emacs if line or search
  9359. ; is given (for backwards compatibility)
  9360. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  9361. 'string-match)
  9362. (cdr (assoc ext apps))
  9363. (cdr (assoc t apps))))))
  9364. (when (eq cmd 'system)
  9365. (setq cmd (cdr (assoc 'system apps))))
  9366. (when (eq cmd 'default)
  9367. (setq cmd (cdr (assoc t apps))))
  9368. (when (eq cmd 'mailcap)
  9369. (require 'mailcap)
  9370. (mailcap-parse-mailcaps)
  9371. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  9372. (command (mailcap-mime-info mime-type)))
  9373. (if (stringp command)
  9374. (setq cmd command)
  9375. (setq cmd 'emacs))))
  9376. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  9377. (not (file-exists-p file))
  9378. (not org-open-non-existing-files))
  9379. (error "No such file: %s" file))
  9380. (cond
  9381. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  9382. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  9383. (while (string-match "['\"]%s['\"]" cmd)
  9384. (setq cmd (replace-match "%s" t t cmd)))
  9385. (while (string-match "%s" cmd)
  9386. (setq cmd (replace-match
  9387. (save-match-data
  9388. (shell-quote-argument
  9389. (convert-standard-filename file)))
  9390. t t cmd)))
  9391. ;; Replace "%1", "%2" etc. in command with group matches from regex
  9392. (save-match-data
  9393. (let ((match-index 1)
  9394. (number-of-groups (- (/ (length link-match-data) 2) 1)))
  9395. (set-match-data link-match-data)
  9396. (while (<= match-index number-of-groups)
  9397. (let ((regex (concat "%" (number-to-string match-index)))
  9398. (replace-with (match-string match-index dlink)))
  9399. (while (string-match regex cmd)
  9400. (setq cmd (replace-match replace-with t t cmd))))
  9401. (setq match-index (+ match-index 1)))))
  9402. (save-window-excursion
  9403. (message "Running %s...done" cmd)
  9404. (start-process-shell-command cmd nil cmd)
  9405. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))))
  9406. ((or (stringp cmd)
  9407. (eq cmd 'emacs))
  9408. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  9409. (widen)
  9410. (if line (org-goto-line line)
  9411. (if search (org-link-search search))))
  9412. ((consp cmd)
  9413. (let ((file (convert-standard-filename file)))
  9414. (save-match-data
  9415. (set-match-data link-match-data)
  9416. (eval cmd))))
  9417. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  9418. (and (derived-mode-p 'org-mode) (eq old-mode 'org-mode)
  9419. (or (not (equal old-buffer (current-buffer)))
  9420. (not (equal old-pos (point))))
  9421. (org-mark-ring-push old-pos old-buffer))))
  9422. (defun org-file-apps-entry-match-against-dlink-p (entry)
  9423. "This function returns non-nil if `entry' uses a regular
  9424. expression which should be matched against the whole link by
  9425. org-open-file.
  9426. It assumes that is the case when the entry uses a regular
  9427. expression which has at least one grouping construct and the
  9428. action is either a lisp form or a command string containing
  9429. '%1', i.e. using at least one subexpression match as a
  9430. parameter."
  9431. (let ((selector (car entry))
  9432. (action (cdr entry)))
  9433. (if (stringp selector)
  9434. (and (> (regexp-opt-depth selector) 0)
  9435. (or (and (stringp action)
  9436. (string-match "%[0-9]" action))
  9437. (consp action)))
  9438. nil)))
  9439. (defun org-default-apps ()
  9440. "Return the default applications for this operating system."
  9441. (cond
  9442. ((eq system-type 'darwin)
  9443. org-file-apps-defaults-macosx)
  9444. ((eq system-type 'windows-nt)
  9445. org-file-apps-defaults-windowsnt)
  9446. (t org-file-apps-defaults-gnu)))
  9447. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  9448. "Convert extensions to regular expressions in the cars of LIST.
  9449. Also, weed out any non-string entries, because the return value is used
  9450. only for regexp matching.
  9451. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  9452. point to the symbol `emacs', indicating that the file should
  9453. be opened in Emacs."
  9454. (append
  9455. (delq nil
  9456. (mapcar (lambda (x)
  9457. (if (not (stringp (car x)))
  9458. nil
  9459. (if (string-match "\\W" (car x))
  9460. x
  9461. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  9462. list))
  9463. (if add-auto-mode
  9464. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  9465. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  9466. (defun org-file-remote-p (file)
  9467. "Test whether FILE specifies a location on a remote system.
  9468. Return non-nil if the location is indeed remote.
  9469. For example, the filename \"/user@host:/foo\" specifies a location
  9470. on the system \"/user@host:\"."
  9471. (cond ((fboundp 'file-remote-p)
  9472. (file-remote-p file))
  9473. ((fboundp 'tramp-handle-file-remote-p)
  9474. (tramp-handle-file-remote-p file))
  9475. ((and (boundp 'ange-ftp-name-format)
  9476. (string-match (car ange-ftp-name-format) file))
  9477. t)))
  9478. ;;;; Refiling
  9479. (defun org-get-org-file ()
  9480. "Read a filename, with default directory `org-directory'."
  9481. (let ((default (or org-default-notes-file remember-data-file)))
  9482. (read-file-name (format "File name [%s]: " default)
  9483. (file-name-as-directory org-directory)
  9484. default)))
  9485. (defun org-notes-order-reversed-p ()
  9486. "Check if the current file should receive notes in reversed order."
  9487. (cond
  9488. ((not org-reverse-note-order) nil)
  9489. ((eq t org-reverse-note-order) t)
  9490. ((not (listp org-reverse-note-order)) nil)
  9491. (t (catch 'exit
  9492. (let ((all org-reverse-note-order)
  9493. entry)
  9494. (while (setq entry (pop all))
  9495. (if (string-match (car entry) buffer-file-name)
  9496. (throw 'exit (cdr entry))))
  9497. nil)))))
  9498. (defvar org-refile-target-table nil
  9499. "The list of refile targets, created by `org-refile'.")
  9500. (defvar org-agenda-new-buffers nil
  9501. "Buffers created to visit agenda files.")
  9502. (defvar org-refile-cache nil
  9503. "Cache for refile targets.")
  9504. (defvar org-refile-markers nil
  9505. "All the markers used for caching refile locations.")
  9506. (defun org-refile-marker (pos)
  9507. "Get a new refile marker, but only if caching is in use."
  9508. (if (not org-refile-use-cache)
  9509. pos
  9510. (let ((m (make-marker)))
  9511. (move-marker m pos)
  9512. (push m org-refile-markers)
  9513. m)))
  9514. (defun org-refile-cache-clear ()
  9515. "Clear the refile cache and disable all the markers."
  9516. (mapc (lambda (m) (move-marker m nil)) org-refile-markers)
  9517. (setq org-refile-markers nil)
  9518. (setq org-refile-cache nil)
  9519. (message "Refile cache has been cleared"))
  9520. (defun org-refile-cache-check-set (set)
  9521. "Check if all the markers in the cache still have live buffers."
  9522. (let (marker)
  9523. (catch 'exit
  9524. (while (and set (setq marker (nth 3 (pop set))))
  9525. ;; if org-refile-use-outline-path is 'file, marker may be nil
  9526. (when (and marker (null (marker-buffer marker)))
  9527. (message "not found") (sit-for 3)
  9528. (throw 'exit nil)))
  9529. t)))
  9530. (defun org-refile-cache-put (set &rest identifiers)
  9531. "Push the refile targets SET into the cache, under IDENTIFIERS."
  9532. (let* ((key (sha1 (prin1-to-string identifiers)))
  9533. (entry (assoc key org-refile-cache)))
  9534. (if entry
  9535. (setcdr entry set)
  9536. (push (cons key set) org-refile-cache))))
  9537. (defun org-refile-cache-get (&rest identifiers)
  9538. "Retrieve the cached value for refile targets given by IDENTIFIERS."
  9539. (cond
  9540. ((not org-refile-cache) nil)
  9541. ((not org-refile-use-cache) (org-refile-cache-clear) nil)
  9542. (t
  9543. (let ((set (cdr (assoc (sha1 (prin1-to-string identifiers))
  9544. org-refile-cache))))
  9545. (and set (org-refile-cache-check-set set) set)))))
  9546. (defun org-refile-get-targets (&optional default-buffer excluded-entries)
  9547. "Produce a table with refile targets."
  9548. (let ((case-fold-search nil)
  9549. ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
  9550. (entries (or org-refile-targets '((nil . (:level . 1)))))
  9551. targets tgs txt re files f desc descre fast-path-p level pos0)
  9552. (message "Getting targets...")
  9553. (with-current-buffer (or default-buffer (current-buffer))
  9554. (while (setq entry (pop entries))
  9555. (setq files (car entry) desc (cdr entry))
  9556. (setq fast-path-p nil)
  9557. (cond
  9558. ((null files) (setq files (list (current-buffer))))
  9559. ((eq files 'org-agenda-files)
  9560. (setq files (org-agenda-files 'unrestricted)))
  9561. ((and (symbolp files) (fboundp files))
  9562. (setq files (funcall files)))
  9563. ((and (symbolp files) (boundp files))
  9564. (setq files (symbol-value files))))
  9565. (if (stringp files) (setq files (list files)))
  9566. (cond
  9567. ((eq (car desc) :tag)
  9568. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  9569. ((eq (car desc) :todo)
  9570. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  9571. ((eq (car desc) :regexp)
  9572. (setq descre (cdr desc)))
  9573. ((eq (car desc) :level)
  9574. (setq descre (concat "^\\*\\{" (number-to-string
  9575. (if org-odd-levels-only
  9576. (1- (* 2 (cdr desc)))
  9577. (cdr desc)))
  9578. "\\}[ \t]")))
  9579. ((eq (car desc) :maxlevel)
  9580. (setq fast-path-p t)
  9581. (setq descre (concat "^\\*\\{1," (number-to-string
  9582. (if org-odd-levels-only
  9583. (1- (* 2 (cdr desc)))
  9584. (cdr desc)))
  9585. "\\}[ \t]")))
  9586. (t (error "Bad refiling target description %s" desc)))
  9587. (while (setq f (pop files))
  9588. (with-current-buffer
  9589. (if (bufferp f) f (org-get-agenda-file-buffer f))
  9590. (or
  9591. (setq tgs (org-refile-cache-get (buffer-file-name) descre))
  9592. (progn
  9593. (if (bufferp f) (setq f (buffer-file-name
  9594. (buffer-base-buffer f))))
  9595. (setq f (and f (expand-file-name f)))
  9596. (if (eq org-refile-use-outline-path 'file)
  9597. (push (list (file-name-nondirectory f) f nil nil) tgs))
  9598. (save-excursion
  9599. (save-restriction
  9600. (widen)
  9601. (goto-char (point-min))
  9602. (while (re-search-forward descre nil t)
  9603. (goto-char (setq pos0 (point-at-bol)))
  9604. (catch 'next
  9605. (when org-refile-target-verify-function
  9606. (save-match-data
  9607. (or (funcall org-refile-target-verify-function)
  9608. (throw 'next t))))
  9609. (when (and (looking-at org-complex-heading-regexp)
  9610. (not (member (match-string 4) excluded-entries))
  9611. (match-string 4))
  9612. (setq level (org-reduced-level
  9613. (- (match-end 1) (match-beginning 1)))
  9614. txt (org-link-display-format (match-string 4))
  9615. txt (replace-regexp-in-string "\\( *\[[0-9]+/?[0-9]*%?\]\\)+$" "" txt)
  9616. re (format org-complex-heading-regexp-format
  9617. (regexp-quote (match-string 4))))
  9618. (when org-refile-use-outline-path
  9619. (setq txt (mapconcat
  9620. 'org-protect-slash
  9621. (append
  9622. (if (eq org-refile-use-outline-path
  9623. 'file)
  9624. (list (file-name-nondirectory
  9625. (buffer-file-name
  9626. (buffer-base-buffer))))
  9627. (if (eq org-refile-use-outline-path
  9628. 'full-file-path)
  9629. (list (buffer-file-name
  9630. (buffer-base-buffer)))))
  9631. (org-get-outline-path fast-path-p
  9632. level txt)
  9633. (list txt))
  9634. "/")))
  9635. (push (list txt f re (org-refile-marker (point)))
  9636. tgs)))
  9637. (when (= (point) pos0)
  9638. ;; verification function has not moved point
  9639. (goto-char (point-at-eol))))))))
  9640. (when org-refile-use-cache
  9641. (org-refile-cache-put tgs (buffer-file-name) descre))
  9642. (setq targets (append tgs targets))
  9643. ))))
  9644. (message "Getting targets...done")
  9645. (nreverse targets)))
  9646. (defun org-protect-slash (s)
  9647. (while (string-match "/" s)
  9648. (setq s (replace-match "\\" t t s)))
  9649. s)
  9650. (defvar org-olpa (make-vector 20 nil))
  9651. (defun org-get-outline-path (&optional fastp level heading)
  9652. "Return the outline path to the current entry, as a list.
  9653. The parameters FASTP, LEVEL, and HEADING are for use by a scanner
  9654. routine which makes outline path derivations for an entire file,
  9655. avoiding backtracing. Refile target collection makes use of that."
  9656. (if fastp
  9657. (progn
  9658. (if (> level 19)
  9659. (error "Outline path failure, more than 19 levels"))
  9660. (loop for i from level upto 19 do
  9661. (aset org-olpa i nil))
  9662. (prog1
  9663. (delq nil (append org-olpa nil))
  9664. (aset org-olpa level heading)))
  9665. (let (rtn case-fold-search)
  9666. (save-excursion
  9667. (save-restriction
  9668. (widen)
  9669. (while (org-up-heading-safe)
  9670. (when (looking-at org-complex-heading-regexp)
  9671. (push (org-match-string-no-properties 4) rtn)))
  9672. rtn)))))
  9673. (defun org-format-outline-path (path &optional width prefix separator)
  9674. "Format the outline path PATH for display.
  9675. WIDTH is the maximum number of characters that is available.
  9676. PREFIX is a prefix to be included in the returned string,
  9677. such as the file name.
  9678. SEPARATOR is inserted between the different parts of the path,
  9679. the default is \"/\"."
  9680. (setq width (or width 79))
  9681. (if prefix (setq width (- width (length prefix))))
  9682. (if (not path)
  9683. (or prefix "")
  9684. (let* ((nsteps (length path))
  9685. (total-width (+ nsteps (apply '+ (mapcar 'length path))))
  9686. (maxwidth (if (<= total-width width)
  9687. 10000 ;; everything fits
  9688. ;; we need to shorten the level headings
  9689. (/ (- width nsteps) nsteps)))
  9690. (org-odd-levels-only nil)
  9691. (n 0)
  9692. (total (1+ (length prefix))))
  9693. (setq maxwidth (max maxwidth 10))
  9694. (concat prefix
  9695. (if prefix (or separator "/"))
  9696. (mapconcat
  9697. (lambda (h)
  9698. (setq n (1+ n))
  9699. (if (and (= n nsteps) (< maxwidth 10000))
  9700. (setq maxwidth (- total-width total)))
  9701. (if (< (length h) maxwidth)
  9702. (progn (setq total (+ total (length h) 1)) h)
  9703. (setq h (substring h 0 (- maxwidth 2))
  9704. total (+ total maxwidth 1))
  9705. (if (string-match "[ \t]+\\'" h)
  9706. (setq h (substring h 0 (match-beginning 0))))
  9707. (setq h (concat h "..")))
  9708. (org-add-props h nil 'face
  9709. (nth (% (1- n) org-n-level-faces)
  9710. org-level-faces))
  9711. h)
  9712. path (or separator "/"))))))
  9713. (defun org-display-outline-path (&optional file current separator just-return-string)
  9714. "Display the current outline path in the echo area.
  9715. If FILE is non-nil, prepend the output with the file name.
  9716. If CURRENT is non-nil, append the current heading to the output.
  9717. SEPARATOR is passed through to `org-format-outline-path'. It separates
  9718. the different parts of the path and defaults to \"/\".
  9719. If JUST-RETURN-STRING is non-nil, return a string, don't display a message."
  9720. (interactive "P")
  9721. (let* ((bfn (buffer-file-name (buffer-base-buffer)))
  9722. (case-fold-search nil)
  9723. (path (and (derived-mode-p 'org-mode) (org-get-outline-path)))
  9724. res)
  9725. (if current (setq path (append path
  9726. (save-excursion
  9727. (org-back-to-heading t)
  9728. (if (looking-at org-complex-heading-regexp)
  9729. (list (match-string 4)))))))
  9730. (setq res
  9731. (org-format-outline-path
  9732. path
  9733. (1- (frame-width))
  9734. (and file bfn (concat (file-name-nondirectory bfn) separator))
  9735. separator))
  9736. (if just-return-string
  9737. (org-no-properties res)
  9738. (message "%s" res))))
  9739. (defvar org-refile-history nil
  9740. "History for refiling operations.")
  9741. (defvar org-after-refile-insert-hook nil
  9742. "Hook run after `org-refile' has inserted its stuff at the new location.
  9743. Note that this is still *before* the stuff will be removed from
  9744. the *old* location.")
  9745. (defvar org-capture-last-stored-marker)
  9746. (defvar org-refile-keep nil
  9747. "Non-nil means `org-refile' will copy instead of refile.")
  9748. (defun org-copy ()
  9749. "Like `org-refile', but copy."
  9750. (interactive)
  9751. (let ((org-refile-keep t))
  9752. (funcall 'org-refile nil nil nil "Copy")))
  9753. (defun org-refile (&optional goto default-buffer rfloc msg)
  9754. "Move the entry or entries at point to another heading.
  9755. The list of target headings is compiled using the information in
  9756. `org-refile-targets', which see.
  9757. At the target location, the entry is filed as a subitem of the target
  9758. heading. Depending on `org-reverse-note-order', the new subitem will
  9759. either be the first or the last subitem.
  9760. If there is an active region, all entries in that region will be moved.
  9761. However, the region must fulfill the requirement that the first heading
  9762. is the first one sets the top-level of the moved text - at most siblings
  9763. below it are allowed.
  9764. With prefix arg GOTO, the command will only visit the target location
  9765. and not actually move anything.
  9766. With a double prefix arg \\[universal-argument] \\[universal-argument], \
  9767. go to the location where the last refiling operation has put the subtree.
  9768. With a prefix argument of `2', refile to the running clock.
  9769. RFLOC can be a refile location obtained in a different way.
  9770. MSG is a string to replace \"Refile\" in the default prompt with
  9771. another verb. E.g. `org-copy' sets this parameter to \"Copy\".
  9772. See also `org-refile-use-outline-path' and `org-completion-use-ido'.
  9773. If you are using target caching (see `org-refile-use-cache'),
  9774. you have to clear the target cache in order to find new targets.
  9775. This can be done with a 0 prefix (`C-0 C-c C-w') or a triple
  9776. prefix argument (`C-u C-u C-u C-c C-w')."
  9777. (interactive "P")
  9778. (if (member goto '(0 (64)))
  9779. (org-refile-cache-clear)
  9780. (let* ((actionmsg (or msg "Refile"))
  9781. (cbuf (current-buffer))
  9782. (regionp (org-region-active-p))
  9783. (region-start (and regionp (region-beginning)))
  9784. (region-end (and regionp (region-end)))
  9785. (region-length (and regionp (- region-end region-start)))
  9786. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  9787. pos it nbuf file re level reversed)
  9788. (setq last-command nil)
  9789. (when regionp
  9790. (goto-char region-start)
  9791. (or (bolp) (goto-char (point-at-bol)))
  9792. (setq region-start (point))
  9793. (unless (or (org-kill-is-subtree-p
  9794. (buffer-substring region-start region-end))
  9795. (prog1 org-refile-active-region-within-subtree
  9796. (org-toggle-heading)))
  9797. (error "The region is not a (sequence of) subtree(s)")))
  9798. (if (equal goto '(16))
  9799. (org-refile-goto-last-stored)
  9800. (when (or
  9801. (and (equal goto 2)
  9802. org-clock-hd-marker (marker-buffer org-clock-hd-marker)
  9803. (prog1
  9804. (setq it (list (or org-clock-heading "running clock")
  9805. (buffer-file-name
  9806. (marker-buffer org-clock-hd-marker))
  9807. ""
  9808. (marker-position org-clock-hd-marker)))
  9809. (setq goto nil)))
  9810. (setq it (or rfloc
  9811. (let (heading-text)
  9812. (save-excursion
  9813. (unless goto
  9814. (org-back-to-heading t)
  9815. (setq heading-text
  9816. (nth 4 (org-heading-components))))
  9817. (org-refile-get-location
  9818. (cond (goto "Goto")
  9819. (regionp (concat actionmsg " region to"))
  9820. (t (concat actionmsg " subtree \""
  9821. heading-text "\" to")))
  9822. default-buffer
  9823. (and (not (equal '(4) goto))
  9824. org-refile-allow-creating-parent-nodes)
  9825. goto))))))
  9826. (setq file (nth 1 it)
  9827. re (nth 2 it)
  9828. pos (nth 3 it))
  9829. (if (and (not goto)
  9830. pos
  9831. (equal (buffer-file-name) file)
  9832. (if regionp
  9833. (and (>= pos region-start)
  9834. (<= pos region-end))
  9835. (and (>= pos (point))
  9836. (< pos (save-excursion
  9837. (org-end-of-subtree t t))))))
  9838. (error "Cannot refile to position inside the tree or region"))
  9839. (setq nbuf (or (find-buffer-visiting file)
  9840. (find-file-noselect file)))
  9841. (if goto
  9842. (progn
  9843. (org-pop-to-buffer-same-window nbuf)
  9844. (goto-char pos)
  9845. (org-show-context 'org-goto))
  9846. (if regionp
  9847. (progn
  9848. (org-kill-new (buffer-substring region-start region-end))
  9849. (org-save-markers-in-region region-start region-end))
  9850. (org-copy-subtree 1 nil t))
  9851. (with-current-buffer (setq nbuf (or (find-buffer-visiting file)
  9852. (find-file-noselect file)))
  9853. (setq reversed (org-notes-order-reversed-p))
  9854. (save-excursion
  9855. (save-restriction
  9856. (widen)
  9857. (if pos
  9858. (progn
  9859. (goto-char pos)
  9860. (looking-at org-outline-regexp)
  9861. (setq level (org-get-valid-level (funcall outline-level) 1))
  9862. (goto-char
  9863. (if reversed
  9864. (or (outline-next-heading) (point-max))
  9865. (or (save-excursion (org-get-next-sibling))
  9866. (org-end-of-subtree t t)
  9867. (point-max)))))
  9868. (setq level 1)
  9869. (if (not reversed)
  9870. (goto-char (point-max))
  9871. (goto-char (point-min))
  9872. (or (outline-next-heading) (goto-char (point-max)))))
  9873. (if (not (bolp)) (newline))
  9874. (org-paste-subtree level)
  9875. (when org-log-refile
  9876. (org-add-log-setup 'refile nil nil 'findpos
  9877. org-log-refile)
  9878. (unless (eq org-log-refile 'note)
  9879. (save-excursion (org-add-log-note))))
  9880. (and org-auto-align-tags
  9881. (let ((org-loop-over-headlines-in-active-region nil))
  9882. (org-set-tags nil t)))
  9883. (bookmark-set "org-refile-last-stored")
  9884. ;; If we are refiling for capture, make sure that the
  9885. ;; last-capture pointers point here
  9886. (when (org-bound-and-true-p org-refile-for-capture)
  9887. (bookmark-set "org-capture-last-stored-marker")
  9888. (move-marker org-capture-last-stored-marker (point)))
  9889. (if (fboundp 'deactivate-mark) (deactivate-mark))
  9890. (run-hooks 'org-after-refile-insert-hook))))
  9891. (unless org-refile-keep
  9892. (if regionp
  9893. (delete-region (point) (+ (point) region-length))
  9894. (org-cut-subtree)))
  9895. (when (featurep 'org-inlinetask)
  9896. (org-inlinetask-remove-END-maybe))
  9897. (setq org-markers-to-move nil)
  9898. (message (concat actionmsg " to \"%s\" in file %s: done") (car it) file)))))))
  9899. (defun org-refile-goto-last-stored ()
  9900. "Go to the location where the last refile was stored."
  9901. (interactive)
  9902. (bookmark-jump "org-refile-last-stored")
  9903. (message "This is the location of the last refile"))
  9904. (defun org-refile-get-location (&optional prompt default-buffer new-nodes
  9905. no-exclude)
  9906. "Prompt the user for a refile location, using PROMPT.
  9907. PROMPT should not be suffixed with a colon and a space, because
  9908. this function appends the default value from
  9909. `org-refile-history' automatically, if that is not empty.
  9910. When NO-EXCLUDE is set, do not exclude headlines in the current subtree,
  9911. this is used for the GOTO interface."
  9912. (let ((org-refile-targets org-refile-targets)
  9913. (org-refile-use-outline-path org-refile-use-outline-path)
  9914. excluded-entries)
  9915. (when (and (derived-mode-p 'org-mode)
  9916. (not org-refile-use-cache)
  9917. (not no-exclude))
  9918. (org-map-tree
  9919. (lambda()
  9920. (setq excluded-entries
  9921. (append excluded-entries (list (org-get-heading t t)))))))
  9922. (setq org-refile-target-table
  9923. (org-refile-get-targets default-buffer excluded-entries)))
  9924. (unless org-refile-target-table
  9925. (error "No refile targets"))
  9926. (let* ((cbuf (current-buffer))
  9927. (partial-completion-mode nil)
  9928. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  9929. (cfunc (if (and org-refile-use-outline-path
  9930. org-outline-path-complete-in-steps)
  9931. 'org-olpath-completing-read
  9932. 'org-icompleting-read))
  9933. (extra (if org-refile-use-outline-path "/" ""))
  9934. (cbnex (concat (buffer-name) extra))
  9935. (filename (and cfn (expand-file-name cfn)))
  9936. (tbl (mapcar
  9937. (lambda (x)
  9938. (if (and (not (member org-refile-use-outline-path
  9939. '(file full-file-path)))
  9940. (not (equal filename (nth 1 x))))
  9941. (cons (concat (car x) extra " ("
  9942. (file-name-nondirectory (nth 1 x)) ")")
  9943. (cdr x))
  9944. (cons (concat (car x) extra) (cdr x))))
  9945. org-refile-target-table))
  9946. (completion-ignore-case t)
  9947. cdef
  9948. (prompt (concat prompt
  9949. (or (and (car org-refile-history)
  9950. (concat " (default " (car org-refile-history) ")"))
  9951. (and (assoc cbnex tbl) (setq cdef cbnex)
  9952. (concat " (default " cbnex ")"))) ": "))
  9953. pa answ parent-target child parent old-hist)
  9954. (setq old-hist org-refile-history)
  9955. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  9956. nil 'org-refile-history (or cdef (car org-refile-history))))
  9957. (setq pa (or (assoc answ tbl) (assoc (concat answ "/") tbl)))
  9958. (org-refile-check-position pa)
  9959. (if pa
  9960. (progn
  9961. (when (or (not org-refile-history)
  9962. (not (eq old-hist org-refile-history))
  9963. (not (equal (car pa) (car org-refile-history))))
  9964. (setq org-refile-history
  9965. (cons (car pa) (if (assoc (car org-refile-history) tbl)
  9966. org-refile-history
  9967. (cdr org-refile-history))))
  9968. (if (equal (car org-refile-history) (nth 1 org-refile-history))
  9969. (pop org-refile-history)))
  9970. pa)
  9971. (if (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  9972. (progn
  9973. (setq parent (match-string 1 answ)
  9974. child (match-string 2 answ))
  9975. (setq parent-target (or (assoc parent tbl)
  9976. (assoc (concat parent "/") tbl)))
  9977. (when (and parent-target
  9978. (or (eq new-nodes t)
  9979. (and (eq new-nodes 'confirm)
  9980. (y-or-n-p (format "Create new node \"%s\"? "
  9981. child)))))
  9982. (org-refile-new-child parent-target child)))
  9983. (error "Invalid target location")))))
  9984. (declare-function org-string-nw-p "org-macs" (s))
  9985. (defun org-refile-check-position (refile-pointer)
  9986. "Check if the refile pointer matches the readline to which it points."
  9987. (let* ((file (nth 1 refile-pointer))
  9988. (re (nth 2 refile-pointer))
  9989. (pos (nth 3 refile-pointer))
  9990. buffer)
  9991. (when (org-string-nw-p re)
  9992. (setq buffer (if (markerp pos)
  9993. (marker-buffer pos)
  9994. (or (find-buffer-visiting file)
  9995. (find-file-noselect file))))
  9996. (with-current-buffer buffer
  9997. (save-excursion
  9998. (save-restriction
  9999. (widen)
  10000. (goto-char pos)
  10001. (beginning-of-line 1)
  10002. (unless (org-looking-at-p re)
  10003. (error "Invalid refile position, please clear the cache with `C-0 C-c C-w' before refiling"))))))))
  10004. (defun org-refile-new-child (parent-target child)
  10005. "Use refile target PARENT-TARGET to add new CHILD below it."
  10006. (unless parent-target
  10007. (error "Cannot find parent for new node"))
  10008. (let ((file (nth 1 parent-target))
  10009. (pos (nth 3 parent-target))
  10010. level)
  10011. (with-current-buffer (or (find-buffer-visiting file)
  10012. (find-file-noselect file))
  10013. (save-excursion
  10014. (save-restriction
  10015. (widen)
  10016. (if pos
  10017. (goto-char pos)
  10018. (goto-char (point-max))
  10019. (if (not (bolp)) (newline)))
  10020. (when (looking-at org-outline-regexp)
  10021. (setq level (funcall outline-level))
  10022. (org-end-of-subtree t t))
  10023. (org-back-over-empty-lines)
  10024. (insert "\n" (make-string
  10025. (if pos (org-get-valid-level level 1) 1) ?*)
  10026. " " child "\n")
  10027. (beginning-of-line 0)
  10028. (list (concat (car parent-target) "/" child) file "" (point)))))))
  10029. (defun org-olpath-completing-read (prompt collection &rest args)
  10030. "Read an outline path like a file name."
  10031. (let ((thetable collection)
  10032. (org-completion-use-ido nil) ; does not work with ido.
  10033. (org-completion-use-iswitchb nil)) ; or iswitchb
  10034. (apply
  10035. 'org-icompleting-read prompt
  10036. (lambda (string predicate &optional flag)
  10037. (let (rtn r f (l (length string)))
  10038. (cond
  10039. ((eq flag nil)
  10040. ;; try completion
  10041. (try-completion string thetable))
  10042. ((eq flag t)
  10043. ;; all-completions
  10044. (setq rtn (all-completions string thetable predicate))
  10045. (mapcar
  10046. (lambda (x)
  10047. (setq r (substring x l))
  10048. (if (string-match " ([^)]*)$" x)
  10049. (setq f (match-string 0 x))
  10050. (setq f ""))
  10051. (if (string-match "/" r)
  10052. (concat string (substring r 0 (match-end 0)) f)
  10053. x))
  10054. rtn))
  10055. ((eq flag 'lambda)
  10056. ;; exact match?
  10057. (assoc string thetable)))))
  10058. args)))
  10059. ;;;; Dynamic blocks
  10060. (defun org-find-dblock (name)
  10061. "Find the first dynamic block with name NAME in the buffer.
  10062. If not found, stay at current position and return nil."
  10063. (let ((case-fold-search t) pos)
  10064. (save-excursion
  10065. (goto-char (point-min))
  10066. (setq pos (and (re-search-forward
  10067. (concat "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+" name "\\>") nil t)
  10068. (match-beginning 0))))
  10069. (if pos (goto-char pos))
  10070. pos))
  10071. (defconst org-dblock-start-re
  10072. "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  10073. "Matches the start line of a dynamic block, with parameters.")
  10074. (defconst org-dblock-end-re "^[ \t]*#\\+\\(?:END\\|end\\)\\([: \t\r\n]\\|$\\)"
  10075. "Matches the end of a dynamic block.")
  10076. (defun org-create-dblock (plist)
  10077. "Create a dynamic block section, with parameters taken from PLIST.
  10078. PLIST must contain a :name entry which is used as name of the block."
  10079. (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
  10080. (end-of-line 1)
  10081. (newline))
  10082. (let ((col (current-column))
  10083. (name (plist-get plist :name)))
  10084. (insert "#+BEGIN: " name)
  10085. (while plist
  10086. (if (eq (car plist) :name)
  10087. (setq plist (cddr plist))
  10088. (insert " " (prin1-to-string (pop plist)))))
  10089. (insert "\n\n" (make-string col ?\ ) "#+END:\n")
  10090. (beginning-of-line -2)))
  10091. (defun org-prepare-dblock ()
  10092. "Prepare dynamic block for refresh.
  10093. This empties the block, puts the cursor at the insert position and returns
  10094. the property list including an extra property :name with the block name."
  10095. (unless (looking-at org-dblock-start-re)
  10096. (error "Not at a dynamic block"))
  10097. (let* ((begdel (1+ (match-end 0)))
  10098. (name (org-no-properties (match-string 1)))
  10099. (params (append (list :name name)
  10100. (read (concat "(" (match-string 3) ")")))))
  10101. (save-excursion
  10102. (beginning-of-line 1)
  10103. (skip-chars-forward " \t")
  10104. (setq params (plist-put params :indentation-column (current-column))))
  10105. (unless (re-search-forward org-dblock-end-re nil t)
  10106. (error "Dynamic block not terminated"))
  10107. (setq params
  10108. (append params
  10109. (list :content (buffer-substring
  10110. begdel (match-beginning 0)))))
  10111. (delete-region begdel (match-beginning 0))
  10112. (goto-char begdel)
  10113. (open-line 1)
  10114. params))
  10115. (defun org-map-dblocks (&optional command)
  10116. "Apply COMMAND to all dynamic blocks in the current buffer.
  10117. If COMMAND is not given, use `org-update-dblock'."
  10118. (let ((cmd (or command 'org-update-dblock)))
  10119. (save-excursion
  10120. (goto-char (point-min))
  10121. (while (re-search-forward org-dblock-start-re nil t)
  10122. (goto-char (match-beginning 0))
  10123. (save-excursion
  10124. (condition-case nil
  10125. (funcall cmd)
  10126. (error (message "Error during update of dynamic block"))))
  10127. (unless (re-search-forward org-dblock-end-re nil t)
  10128. (error "Dynamic block not terminated"))))))
  10129. (defun org-dblock-update (&optional arg)
  10130. "User command for updating dynamic blocks.
  10131. Update the dynamic block at point. With prefix ARG, update all dynamic
  10132. blocks in the buffer."
  10133. (interactive "P")
  10134. (if arg
  10135. (org-update-all-dblocks)
  10136. (or (looking-at org-dblock-start-re)
  10137. (org-beginning-of-dblock))
  10138. (org-update-dblock)))
  10139. (defun org-update-dblock ()
  10140. "Update the dynamic block at point.
  10141. This means to empty the block, parse for parameters and then call
  10142. the correct writing function."
  10143. (interactive)
  10144. (save-window-excursion
  10145. (let* ((pos (point))
  10146. (line (org-current-line))
  10147. (params (org-prepare-dblock))
  10148. (name (plist-get params :name))
  10149. (indent (plist-get params :indentation-column))
  10150. (cmd (intern (concat "org-dblock-write:" name))))
  10151. (message "Updating dynamic block `%s' at line %d..." name line)
  10152. (funcall cmd params)
  10153. (message "Updating dynamic block `%s' at line %d...done" name line)
  10154. (goto-char pos)
  10155. (when (and indent (> indent 0))
  10156. (setq indent (make-string indent ?\ ))
  10157. (save-excursion
  10158. (org-beginning-of-dblock)
  10159. (forward-line 1)
  10160. (while (not (looking-at org-dblock-end-re))
  10161. (insert indent)
  10162. (beginning-of-line 2))
  10163. (when (looking-at org-dblock-end-re)
  10164. (and (looking-at "[ \t]+")
  10165. (replace-match ""))
  10166. (insert indent)))))))
  10167. (defun org-beginning-of-dblock ()
  10168. "Find the beginning of the dynamic block at point.
  10169. Error if there is no such block at point."
  10170. (let ((pos (point))
  10171. beg)
  10172. (end-of-line 1)
  10173. (if (and (re-search-backward org-dblock-start-re nil t)
  10174. (setq beg (match-beginning 0))
  10175. (re-search-forward org-dblock-end-re nil t)
  10176. (> (match-end 0) pos))
  10177. (goto-char beg)
  10178. (goto-char pos)
  10179. (error "Not in a dynamic block"))))
  10180. (defun org-update-all-dblocks ()
  10181. "Update all dynamic blocks in the buffer.
  10182. This function can be used in a hook."
  10183. (interactive)
  10184. (when (derived-mode-p 'org-mode)
  10185. (org-map-dblocks 'org-update-dblock)))
  10186. ;;;; Completion
  10187. (defconst org-additional-option-like-keywords
  10188. '("BEGIN_HTML" "END_HTML" "HTML:" "ATTR_HTML:"
  10189. "BEGIN_DocBook" "END_DocBook" "DocBook:" "ATTR_DocBook:"
  10190. "BEGIN_LaTeX" "END_LaTeX" "LaTeX:" "LATEX_HEADER:"
  10191. "LATEX_CLASS:" "LATEX_CLASS_OPTIONS:" "ATTR_LaTeX:"
  10192. "BEGIN:" "END:"
  10193. "ORGTBL" "TBLFM:" "TBLNAME:"
  10194. "BEGIN_EXAMPLE" "END_EXAMPLE"
  10195. "BEGIN_VERBATIM" "END_VERBATIM"
  10196. "BEGIN_QUOTE" "END_QUOTE"
  10197. "BEGIN_VERSE" "END_VERSE"
  10198. "BEGIN_CENTER" "END_CENTER"
  10199. "BEGIN_SRC" "END_SRC"
  10200. "BEGIN_RESULT" "END_RESULT"
  10201. "BEGIN_lstlisting" "END_lstlisting"
  10202. "NAME:" "RESULTS:"
  10203. "HEADER:" "HEADERS:"
  10204. "COLUMNS:" "PROPERTY:"
  10205. "CAPTION:" "LABEL:"
  10206. "SETUPFILE:"
  10207. "INCLUDE:"
  10208. "BIND:"
  10209. "MACRO:"))
  10210. (defconst org-options-keywords
  10211. '("TITLE:" "AUTHOR:" "EMAIL:" "DATE:"
  10212. "DESCRIPTION:" "KEYWORDS:" "LANGUAGE:" "OPTIONS:"
  10213. "EXPORT_SELECT_TAGS:" "EXPORT_EXCLUDE_TAGS:"
  10214. "LINK_UP:" "LINK_HOME:" "LINK:" "TODO:"
  10215. "XSLT:" "MATHJAX:" "CATEGORY:" "SEQ_TODO:" "TYP_TODO:"
  10216. "PRIORITIES:" "DRAWERS:" "STARTUP:" "TAGS:" "STYLE:"
  10217. "FILETAGS:" "ARCHIVE:" "INFOJS_OPT:"))
  10218. (defconst org-additional-option-like-keywords-for-flyspell
  10219. (delete-dups
  10220. (split-string
  10221. (mapconcat (lambda(k)
  10222. (replace-regexp-in-string
  10223. "_\\|:" " "
  10224. (concat k " " (downcase k) " " (upcase k))))
  10225. (append org-options-keywords org-additional-option-like-keywords)
  10226. " ")
  10227. " +" t)))
  10228. (defcustom org-structure-template-alist
  10229. '(
  10230. ("s" "#+BEGIN_SRC ?\n\n#+END_SRC"
  10231. "<src lang=\"?\">\n\n</src>")
  10232. ("e" "#+BEGIN_EXAMPLE\n?\n#+END_EXAMPLE"
  10233. "<example>\n?\n</example>")
  10234. ("q" "#+BEGIN_QUOTE\n?\n#+END_QUOTE"
  10235. "<quote>\n?\n</quote>")
  10236. ("v" "#+BEGIN_VERSE\n?\n#+END_VERSE"
  10237. "<verse>\n?\n</verse>")
  10238. ("c" "#+BEGIN_CENTER\n?\n#+END_CENTER"
  10239. "<center>\n?\n</center>")
  10240. ("l" "#+BEGIN_LaTeX\n?\n#+END_LaTeX"
  10241. "<literal style=\"latex\">\n?\n</literal>")
  10242. ("L" "#+LaTeX: "
  10243. "<literal style=\"latex\">?</literal>")
  10244. ("h" "#+BEGIN_HTML\n?\n#+END_HTML"
  10245. "<literal style=\"html\">\n?\n</literal>")
  10246. ("H" "#+HTML: "
  10247. "<literal style=\"html\">?</literal>")
  10248. ("a" "#+BEGIN_ASCII\n?\n#+END_ASCII")
  10249. ("A" "#+ASCII: ")
  10250. ("i" "#+INDEX: ?"
  10251. "#+INDEX: ?")
  10252. ("I" "#+INCLUDE: %file ?"
  10253. "<include file=%file markup=\"?\">")
  10254. )
  10255. "Structure completion elements.
  10256. This is a list of abbreviation keys and values. The value gets inserted
  10257. if you type `<' followed by the key and then press the completion key,
  10258. usually `M-TAB'. %file will be replaced by a file name after prompting
  10259. for the file using completion. The cursor will be placed at the position
  10260. of the `?` in the template.
  10261. There are two templates for each key, the first uses the original Org syntax,
  10262. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  10263. the default when the /org-mtags.el/ module has been loaded. See also the
  10264. variable `org-mtags-prefer-muse-templates'."
  10265. :group 'org-completion
  10266. :type '(repeat
  10267. (string :tag "Key")
  10268. (string :tag "Template")
  10269. (string :tag "Muse Template")))
  10270. (defun org-try-structure-completion ()
  10271. "Try to complete a structure template before point.
  10272. This looks for strings like \"<e\" on an otherwise empty line and
  10273. expands them."
  10274. (let ((l (buffer-substring (point-at-bol) (point)))
  10275. a)
  10276. (when (and (looking-at "[ \t]*$")
  10277. (string-match "^[ \t]*<\\([a-zA-Z]+\\)$" l)
  10278. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  10279. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  10280. (match-beginning 1)) a)
  10281. t)))
  10282. (defun org-complete-expand-structure-template (start cell)
  10283. "Expand a structure template."
  10284. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  10285. (rpl (nth (if musep 2 1) cell))
  10286. (ind ""))
  10287. (delete-region start (point))
  10288. (when (string-match "\\`#\\+" rpl)
  10289. (cond
  10290. ((bolp))
  10291. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  10292. (setq ind (buffer-substring (point-at-bol) (point))))
  10293. (t (newline))))
  10294. (setq start (point))
  10295. (if (string-match "%file" rpl)
  10296. (setq rpl (replace-match
  10297. (concat
  10298. "\""
  10299. (save-match-data
  10300. (abbreviate-file-name (read-file-name "Include file: ")))
  10301. "\"")
  10302. t t rpl)))
  10303. (setq rpl (mapconcat 'identity (split-string rpl "\n")
  10304. (concat "\n" ind)))
  10305. (insert rpl)
  10306. (if (re-search-backward "\\?" start t) (delete-char 1))))
  10307. ;;;; TODO, DEADLINE, Comments
  10308. (defun org-toggle-comment ()
  10309. "Change the COMMENT state of an entry."
  10310. (interactive)
  10311. (save-excursion
  10312. (org-back-to-heading)
  10313. (let (case-fold-search)
  10314. (cond
  10315. ((looking-at (format org-heading-keyword-regexp-format
  10316. org-comment-string))
  10317. (goto-char (match-end 1))
  10318. (looking-at (concat " +" org-comment-string))
  10319. (replace-match "" t t)
  10320. (when (eolp) (insert " ")))
  10321. ((looking-at org-outline-regexp)
  10322. (goto-char (match-end 0))
  10323. (insert org-comment-string " "))))))
  10324. (defvar org-last-todo-state-is-todo nil
  10325. "This is non-nil when the last TODO state change led to a TODO state.
  10326. If the last change removed the TODO tag or switched to DONE, then
  10327. this is nil.")
  10328. (defvar org-setting-tags nil) ; dynamically skipped
  10329. (defvar org-todo-setup-filter-hook nil
  10330. "Hook for functions that pre-filter todo specs.
  10331. Each function takes a todo spec and returns either nil or the spec
  10332. transformed into canonical form." )
  10333. (defvar org-todo-get-default-hook nil
  10334. "Hook for functions that get a default item for todo.
  10335. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  10336. nil or a string to be used for the todo mark." )
  10337. (defvar org-agenda-headline-snapshot-before-repeat)
  10338. (defun org-current-effective-time ()
  10339. "Return current time adjusted for `org-extend-today-until' variable."
  10340. (let* ((ct (org-current-time))
  10341. (dct (decode-time ct))
  10342. (ct1
  10343. (cond
  10344. (org-use-last-clock-out-time-as-effective-time
  10345. (or (org-clock-get-last-clock-out-time) ct))
  10346. ((and org-use-effective-time (< (nth 2 dct) org-extend-today-until))
  10347. (encode-time 0 59 23 (1- (nth 3 dct)) (nth 4 dct) (nth 5 dct)))
  10348. (t ct))))
  10349. ct1))
  10350. (defun org-todo-yesterday (&optional arg)
  10351. "Like `org-todo' but the time of change will be 23:59 of yesterday."
  10352. (interactive "P")
  10353. (if (eq major-mode 'org-agenda-mode)
  10354. (apply 'org-agenda-todo-yesterday arg)
  10355. (let* ((hour (third (decode-time
  10356. (org-current-time))))
  10357. (org-extend-today-until (1+ hour)))
  10358. (org-todo arg))))
  10359. (defun org-todo (&optional arg)
  10360. "Change the TODO state of an item.
  10361. The state of an item is given by a keyword at the start of the heading,
  10362. like
  10363. *** TODO Write paper
  10364. *** DONE Call mom
  10365. The different keywords are specified in the variable `org-todo-keywords'.
  10366. By default the available states are \"TODO\" and \"DONE\".
  10367. So for this example: when the item starts with TODO, it is changed to DONE.
  10368. When it starts with DONE, the DONE is removed. And when neither TODO nor
  10369. DONE are present, add TODO at the beginning of the heading.
  10370. With \\[universal-argument] prefix arg, use completion to determine the new \
  10371. state.
  10372. With numeric prefix arg, switch to that state.
  10373. With a double \\[universal-argument] prefix, switch to the next set of TODO \
  10374. keywords (nextset).
  10375. With a triple \\[universal-argument] prefix, circumvent any state blocking.
  10376. With a numeric prefix arg of 0, inhibit note taking for the change.
  10377. For calling through lisp, arg is also interpreted in the following way:
  10378. 'none -> empty state
  10379. \"\"(empty string) -> switch to empty state
  10380. 'done -> switch to DONE
  10381. 'nextset -> switch to the next set of keywords
  10382. 'previousset -> switch to the previous set of keywords
  10383. \"WAITING\" -> switch to the specified keyword, but only if it
  10384. really is a member of `org-todo-keywords'."
  10385. (interactive "P")
  10386. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  10387. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  10388. 'region-start-level 'region))
  10389. org-loop-over-headlines-in-active-region)
  10390. (org-map-entries
  10391. `(org-todo ,arg)
  10392. org-loop-over-headlines-in-active-region
  10393. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  10394. (if (equal arg '(16)) (setq arg 'nextset))
  10395. (let ((org-blocker-hook org-blocker-hook)
  10396. (case-fold-search nil))
  10397. (when (equal arg '(64))
  10398. (setq arg nil org-blocker-hook nil))
  10399. (when (and org-blocker-hook
  10400. (or org-inhibit-blocking
  10401. (org-entry-get nil "NOBLOCKING")))
  10402. (setq org-blocker-hook nil))
  10403. (save-excursion
  10404. (catch 'exit
  10405. (org-back-to-heading t)
  10406. (if (looking-at org-outline-regexp) (goto-char (1- (match-end 0))))
  10407. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|[ \t]*$\\)"))
  10408. (looking-at "\\(?: *\\|[ \t]*$\\)"))
  10409. (let* ((match-data (match-data))
  10410. (startpos (point-at-bol))
  10411. (logging (save-match-data (org-entry-get nil "LOGGING" t t)))
  10412. (org-log-done org-log-done)
  10413. (org-log-repeat org-log-repeat)
  10414. (org-todo-log-states org-todo-log-states)
  10415. (org-inhibit-logging
  10416. (if (equal arg 0)
  10417. (progn (setq arg nil) 'note) org-inhibit-logging))
  10418. (this (match-string 1))
  10419. (hl-pos (match-beginning 0))
  10420. (head (org-get-todo-sequence-head this))
  10421. (ass (assoc head org-todo-kwd-alist))
  10422. (interpret (nth 1 ass))
  10423. (done-word (nth 3 ass))
  10424. (final-done-word (nth 4 ass))
  10425. (org-last-state (or this ""))
  10426. (completion-ignore-case t)
  10427. (member (member this org-todo-keywords-1))
  10428. (tail (cdr member))
  10429. (org-state (cond
  10430. ((and org-todo-key-trigger
  10431. (or (and (equal arg '(4))
  10432. (eq org-use-fast-todo-selection 'prefix))
  10433. (and (not arg) org-use-fast-todo-selection
  10434. (not (eq org-use-fast-todo-selection
  10435. 'prefix)))))
  10436. ;; Use fast selection
  10437. (org-fast-todo-selection))
  10438. ((and (equal arg '(4))
  10439. (or (not org-use-fast-todo-selection)
  10440. (not org-todo-key-trigger)))
  10441. ;; Read a state with completion
  10442. (org-icompleting-read
  10443. "State: " (mapcar (lambda(x) (list x))
  10444. org-todo-keywords-1)
  10445. nil t))
  10446. ((eq arg 'right)
  10447. (if this
  10448. (if tail (car tail) nil)
  10449. (car org-todo-keywords-1)))
  10450. ((eq arg 'left)
  10451. (if (equal member org-todo-keywords-1)
  10452. nil
  10453. (if this
  10454. (nth (- (length org-todo-keywords-1)
  10455. (length tail) 2)
  10456. org-todo-keywords-1)
  10457. (org-last org-todo-keywords-1))))
  10458. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  10459. (setq arg nil))) ; hack to fall back to cycling
  10460. (arg
  10461. ;; user or caller requests a specific state
  10462. (cond
  10463. ((equal arg "") nil)
  10464. ((eq arg 'none) nil)
  10465. ((eq arg 'done) (or done-word (car org-done-keywords)))
  10466. ((eq arg 'nextset)
  10467. (or (car (cdr (member head org-todo-heads)))
  10468. (car org-todo-heads)))
  10469. ((eq arg 'previousset)
  10470. (let ((org-todo-heads (reverse org-todo-heads)))
  10471. (or (car (cdr (member head org-todo-heads)))
  10472. (car org-todo-heads))))
  10473. ((car (member arg org-todo-keywords-1)))
  10474. ((stringp arg)
  10475. (error "State `%s' not valid in this file" arg))
  10476. ((nth (1- (prefix-numeric-value arg))
  10477. org-todo-keywords-1))))
  10478. ((null member) (or head (car org-todo-keywords-1)))
  10479. ((equal this final-done-word) nil) ;; -> make empty
  10480. ((null tail) nil) ;; -> first entry
  10481. ((memq interpret '(type priority))
  10482. (if (eq this-command last-command)
  10483. (car tail)
  10484. (if (> (length tail) 0)
  10485. (or done-word (car org-done-keywords))
  10486. nil)))
  10487. (t
  10488. (car tail))))
  10489. (org-state (or
  10490. (run-hook-with-args-until-success
  10491. 'org-todo-get-default-hook org-state org-last-state)
  10492. org-state))
  10493. (next (if org-state (concat " " org-state " ") " "))
  10494. (change-plist (list :type 'todo-state-change :from this :to org-state
  10495. :position startpos))
  10496. dolog now-done-p)
  10497. (when org-blocker-hook
  10498. (setq org-last-todo-state-is-todo
  10499. (not (member this org-done-keywords)))
  10500. (unless (save-excursion
  10501. (save-match-data
  10502. (org-with-wide-buffer
  10503. (run-hook-with-args-until-failure
  10504. 'org-blocker-hook change-plist))))
  10505. (if (org-called-interactively-p 'interactive)
  10506. (error "TODO state change from %s to %s blocked" this org-state)
  10507. ;; fail silently
  10508. (message "TODO state change from %s to %s blocked" this org-state)
  10509. (throw 'exit nil))))
  10510. (store-match-data match-data)
  10511. (replace-match next t t)
  10512. (unless (pos-visible-in-window-p hl-pos)
  10513. (message "TODO state changed to %s" (org-trim next)))
  10514. (unless head
  10515. (setq head (org-get-todo-sequence-head org-state)
  10516. ass (assoc head org-todo-kwd-alist)
  10517. interpret (nth 1 ass)
  10518. done-word (nth 3 ass)
  10519. final-done-word (nth 4 ass)))
  10520. (when (memq arg '(nextset previousset))
  10521. (message "Keyword-Set %d/%d: %s"
  10522. (- (length org-todo-sets) -1
  10523. (length (memq (assoc org-state org-todo-sets) org-todo-sets)))
  10524. (length org-todo-sets)
  10525. (mapconcat 'identity (assoc org-state org-todo-sets) " ")))
  10526. (setq org-last-todo-state-is-todo
  10527. (not (member org-state org-done-keywords)))
  10528. (setq now-done-p (and (member org-state org-done-keywords)
  10529. (not (member this org-done-keywords))))
  10530. (and logging (org-local-logging logging))
  10531. (when (and (or org-todo-log-states org-log-done)
  10532. (not (eq org-inhibit-logging t))
  10533. (not (memq arg '(nextset previousset))))
  10534. ;; we need to look at recording a time and note
  10535. (setq dolog (or (nth 1 (assoc org-state org-todo-log-states))
  10536. (nth 2 (assoc this org-todo-log-states))))
  10537. (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
  10538. (setq dolog 'time))
  10539. (when (and org-state
  10540. (member org-state org-not-done-keywords)
  10541. (not (member this org-not-done-keywords)))
  10542. ;; This is now a todo state and was not one before
  10543. ;; If there was a CLOSED time stamp, get rid of it.
  10544. (org-add-planning-info nil nil 'closed))
  10545. (when (and now-done-p org-log-done)
  10546. ;; It is now done, and it was not done before
  10547. (org-add-planning-info 'closed (org-current-effective-time))
  10548. (if (and (not dolog) (eq 'note org-log-done))
  10549. (org-add-log-setup 'done org-state this 'findpos 'note)))
  10550. (when (and org-state dolog)
  10551. ;; This is a non-nil state, and we need to log it
  10552. (org-add-log-setup 'state org-state this 'findpos dolog)))
  10553. ;; Fixup tag positioning
  10554. (org-todo-trigger-tag-changes org-state)
  10555. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  10556. (when org-provide-todo-statistics
  10557. (org-update-parent-todo-statistics))
  10558. (run-hooks 'org-after-todo-state-change-hook)
  10559. (if (and arg (not (member org-state org-done-keywords)))
  10560. (setq head (org-get-todo-sequence-head org-state)))
  10561. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  10562. ;; Do we need to trigger a repeat?
  10563. (when now-done-p
  10564. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  10565. ;; This is for the agenda, take a snapshot of the headline.
  10566. (save-match-data
  10567. (setq org-agenda-headline-snapshot-before-repeat
  10568. (org-get-heading))))
  10569. (org-auto-repeat-maybe org-state))
  10570. ;; Fixup cursor location if close to the keyword
  10571. (if (and (outline-on-heading-p)
  10572. (not (bolp))
  10573. (save-excursion (beginning-of-line 1)
  10574. (looking-at org-todo-line-regexp))
  10575. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  10576. (progn
  10577. (goto-char (or (match-end 2) (match-end 1)))
  10578. (and (looking-at " ") (just-one-space))))
  10579. (when org-trigger-hook
  10580. (save-excursion
  10581. (run-hook-with-args 'org-trigger-hook change-plist)))))))))
  10582. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  10583. "Block turning an entry into a TODO, using the hierarchy.
  10584. This checks whether the current task should be blocked from state
  10585. changes. Such blocking occurs when:
  10586. 1. The task has children which are not all in a completed state.
  10587. 2. A task has a parent with the property :ORDERED:, and there
  10588. are siblings prior to the current task with incomplete
  10589. status.
  10590. 3. The parent of the task is blocked because it has siblings that should
  10591. be done first, or is child of a block grandparent TODO entry."
  10592. (if (not org-enforce-todo-dependencies)
  10593. t ; if locally turned off don't block
  10594. (catch 'dont-block
  10595. ;; If this is not a todo state change, or if this entry is already DONE,
  10596. ;; do not block
  10597. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  10598. (member (plist-get change-plist :from)
  10599. (cons 'done org-done-keywords))
  10600. (member (plist-get change-plist :to)
  10601. (cons 'todo org-not-done-keywords))
  10602. (not (plist-get change-plist :to)))
  10603. (throw 'dont-block t))
  10604. ;; If this task has children, and any are undone, it's blocked
  10605. (save-excursion
  10606. (org-back-to-heading t)
  10607. (let ((this-level (funcall outline-level)))
  10608. (outline-next-heading)
  10609. (let ((child-level (funcall outline-level)))
  10610. (while (and (not (eobp))
  10611. (> child-level this-level))
  10612. ;; this todo has children, check whether they are all
  10613. ;; completed
  10614. (if (and (not (org-entry-is-done-p))
  10615. (org-entry-is-todo-p))
  10616. (throw 'dont-block nil))
  10617. (outline-next-heading)
  10618. (setq child-level (funcall outline-level))))))
  10619. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  10620. ;; any previous siblings are undone, it's blocked
  10621. (save-excursion
  10622. (org-back-to-heading t)
  10623. (let* ((pos (point))
  10624. (parent-pos (and (org-up-heading-safe) (point))))
  10625. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  10626. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  10627. (forward-line 1)
  10628. (re-search-forward org-not-done-heading-regexp pos t))
  10629. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  10630. ;; Search further up the hierarchy, to see if an ancestor is blocked
  10631. (while t
  10632. (goto-char parent-pos)
  10633. (if (not (looking-at org-not-done-heading-regexp))
  10634. (throw 'dont-block t)) ; do not block, parent is not a TODO
  10635. (setq pos (point))
  10636. (setq parent-pos (and (org-up-heading-safe) (point)))
  10637. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  10638. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  10639. (forward-line 1)
  10640. (re-search-forward org-not-done-heading-regexp pos t))
  10641. (throw 'dont-block nil)))))))) ; block, older sibling not done.
  10642. (defcustom org-track-ordered-property-with-tag nil
  10643. "Should the ORDERED property also be shown as a tag?
  10644. The ORDERED property decides if an entry should require subtasks to be
  10645. completed in sequence. Since a property is not very visible, setting
  10646. this option means that toggling the ORDERED property with the command
  10647. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  10648. not relevant for the behavior, but it makes things more visible.
  10649. Note that toggling the tag with tags commands will not change the property
  10650. and therefore not influence behavior!
  10651. This can be t, meaning the tag ORDERED should be used, It can also be a
  10652. string to select a different tag for this task."
  10653. :group 'org-todo
  10654. :type '(choice
  10655. (const :tag "No tracking" nil)
  10656. (const :tag "Track with ORDERED tag" t)
  10657. (string :tag "Use other tag")))
  10658. (defun org-toggle-ordered-property ()
  10659. "Toggle the ORDERED property of the current entry.
  10660. For better visibility, you can track the value of this property with a tag.
  10661. See variable `org-track-ordered-property-with-tag'."
  10662. (interactive)
  10663. (let* ((t1 org-track-ordered-property-with-tag)
  10664. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  10665. (save-excursion
  10666. (org-back-to-heading)
  10667. (if (org-entry-get nil "ORDERED")
  10668. (progn
  10669. (org-delete-property "ORDERED")
  10670. (and tag (org-toggle-tag tag 'off))
  10671. (message "Subtasks can be completed in arbitrary order"))
  10672. (org-entry-put nil "ORDERED" "t")
  10673. (and tag (org-toggle-tag tag 'on))
  10674. (message "Subtasks must be completed in sequence")))))
  10675. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  10676. (defun org-block-todo-from-checkboxes (change-plist)
  10677. "Block turning an entry into a TODO, using checkboxes.
  10678. This checks whether the current task should be blocked from state
  10679. changes because there are unchecked boxes in this entry."
  10680. (if (not org-enforce-todo-checkbox-dependencies)
  10681. t ; if locally turned off don't block
  10682. (catch 'dont-block
  10683. ;; If this is not a todo state change, or if this entry is already DONE,
  10684. ;; do not block
  10685. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  10686. (member (plist-get change-plist :from)
  10687. (cons 'done org-done-keywords))
  10688. (member (plist-get change-plist :to)
  10689. (cons 'todo org-not-done-keywords))
  10690. (not (plist-get change-plist :to)))
  10691. (throw 'dont-block t))
  10692. ;; If this task has checkboxes that are not checked, it's blocked
  10693. (save-excursion
  10694. (org-back-to-heading t)
  10695. (let ((beg (point)) end)
  10696. (outline-next-heading)
  10697. (setq end (point))
  10698. (goto-char beg)
  10699. (if (org-list-search-forward
  10700. (concat (org-item-beginning-re)
  10701. "\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?"
  10702. "\\[[- ]\\]")
  10703. end t)
  10704. (progn
  10705. (if (boundp 'org-blocked-by-checkboxes)
  10706. (setq org-blocked-by-checkboxes t))
  10707. (throw 'dont-block nil)))))
  10708. t))) ; do not block
  10709. (defun org-entry-blocked-p ()
  10710. "Is the current entry blocked?"
  10711. (if (org-entry-get nil "NOBLOCKING")
  10712. nil ;; Never block this entry
  10713. (not
  10714. (run-hook-with-args-until-failure
  10715. 'org-blocker-hook
  10716. (list :type 'todo-state-change
  10717. :position (point)
  10718. :from 'todo
  10719. :to 'done)))))
  10720. (defun org-update-statistics-cookies (all)
  10721. "Update the statistics cookie, either from TODO or from checkboxes.
  10722. This should be called with the cursor in a line with a statistics cookie."
  10723. (interactive "P")
  10724. (if all
  10725. (progn
  10726. (org-update-checkbox-count 'all)
  10727. (org-map-entries 'org-update-parent-todo-statistics))
  10728. (if (not (org-at-heading-p))
  10729. (org-update-checkbox-count)
  10730. (let ((pos (move-marker (make-marker) (point)))
  10731. end l1 l2)
  10732. (ignore-errors (org-back-to-heading t))
  10733. (if (not (org-at-heading-p))
  10734. (org-update-checkbox-count)
  10735. (setq l1 (org-outline-level))
  10736. (setq end (save-excursion
  10737. (outline-next-heading)
  10738. (if (org-at-heading-p) (setq l2 (org-outline-level)))
  10739. (point)))
  10740. (if (and (save-excursion
  10741. (re-search-forward
  10742. "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) \\[[- X]\\]" end t))
  10743. (not (save-excursion (re-search-forward
  10744. ":COOKIE_DATA:.*\\<todo\\>" end t))))
  10745. (org-update-checkbox-count)
  10746. (if (and l2 (> l2 l1))
  10747. (progn
  10748. (goto-char end)
  10749. (org-update-parent-todo-statistics))
  10750. (goto-char pos)
  10751. (beginning-of-line 1)
  10752. (while (re-search-forward
  10753. "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)"
  10754. (point-at-eol) t)
  10755. (replace-match (if (match-end 2) "[100%]" "[0/0]") t t)))))
  10756. (goto-char pos)
  10757. (move-marker pos nil)))))
  10758. (defvar org-entry-property-inherited-from) ;; defined below
  10759. (defun org-update-parent-todo-statistics ()
  10760. "Update any statistics cookie in the parent of the current headline.
  10761. When `org-hierarchical-todo-statistics' is nil, statistics will cover
  10762. the entire subtree and this will travel up the hierarchy and update
  10763. statistics everywhere."
  10764. (let* ((prop (save-excursion (org-up-heading-safe)
  10765. (org-entry-get nil "COOKIE_DATA" 'inherit)))
  10766. (recursive (or (not org-hierarchical-todo-statistics)
  10767. (and prop (string-match "\\<recursive\\>" prop))))
  10768. (lim (or (and prop (marker-position org-entry-property-inherited-from))
  10769. 0))
  10770. (first t)
  10771. (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  10772. level ltoggle l1 new ndel
  10773. (cnt-all 0) (cnt-done 0) is-percent kwd
  10774. checkbox-beg ov ovs ove cookie-present)
  10775. (catch 'exit
  10776. (save-excursion
  10777. (beginning-of-line 1)
  10778. (setq ltoggle (funcall outline-level))
  10779. ;; Three situations are to consider:
  10780. ;; 1. if `org-hierarchical-todo-statistics' is nil, repeat up
  10781. ;; to the top-level ancestor on the headline;
  10782. ;; 2. If parent has "recursive" property, repeat up to the
  10783. ;; headline setting that property, taking inheritance into
  10784. ;; account;
  10785. ;; 3. Else, move up to direct parent and proceed only once.
  10786. (while (and (setq level (org-up-heading-safe))
  10787. (or recursive first)
  10788. (>= (point) lim))
  10789. (setq first nil cookie-present nil)
  10790. (unless (and level
  10791. (not (string-match
  10792. "\\<checkbox\\>"
  10793. (downcase (or (org-entry-get nil "COOKIE_DATA")
  10794. "")))))
  10795. (throw 'exit nil))
  10796. (while (re-search-forward box-re (point-at-eol) t)
  10797. (setq cnt-all 0 cnt-done 0 cookie-present t)
  10798. (setq is-percent (match-end 2) checkbox-beg (match-beginning 0))
  10799. (save-match-data
  10800. (unless (outline-next-heading) (throw 'exit nil))
  10801. (while (and (looking-at org-complex-heading-regexp)
  10802. (> (setq l1 (length (match-string 1))) level))
  10803. (setq kwd (and (or recursive (= l1 ltoggle))
  10804. (match-string 2)))
  10805. (if (or (eq org-provide-todo-statistics 'all-headlines)
  10806. (and (listp org-provide-todo-statistics)
  10807. (or (member kwd org-provide-todo-statistics)
  10808. (member kwd org-done-keywords))))
  10809. (setq cnt-all (1+ cnt-all))
  10810. (if (eq org-provide-todo-statistics t)
  10811. (and kwd (setq cnt-all (1+ cnt-all)))))
  10812. (and (member kwd org-done-keywords)
  10813. (setq cnt-done (1+ cnt-done)))
  10814. (outline-next-heading)))
  10815. (setq new
  10816. (if is-percent
  10817. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  10818. (format "[%d/%d]" cnt-done cnt-all))
  10819. ndel (- (match-end 0) checkbox-beg))
  10820. ;; handle overlays when updating cookie from column view
  10821. (when (setq ov (car (overlays-at checkbox-beg)))
  10822. (setq ovs (overlay-start ov) ove (overlay-end ov))
  10823. (delete-overlay ov))
  10824. (goto-char checkbox-beg)
  10825. (insert new)
  10826. (delete-region (point) (+ (point) ndel))
  10827. (when org-auto-align-tags (org-fix-tags-on-the-fly))
  10828. (when ov (move-overlay ov ovs ove)))
  10829. (when cookie-present
  10830. (run-hook-with-args 'org-after-todo-statistics-hook
  10831. cnt-done (- cnt-all cnt-done))))))
  10832. (run-hooks 'org-todo-statistics-hook)))
  10833. (defvar org-after-todo-statistics-hook nil
  10834. "Hook that is called after a TODO statistics cookie has been updated.
  10835. Each function is called with two arguments: the number of not-done entries
  10836. and the number of done entries.
  10837. For example, the following function, when added to this hook, will switch
  10838. an entry to DONE when all children are done, and back to TODO when new
  10839. entries are set to a TODO status. Note that this hook is only called
  10840. when there is a statistics cookie in the headline!
  10841. (defun org-summary-todo (n-done n-not-done)
  10842. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  10843. (let (org-log-done org-log-states) ; turn off logging
  10844. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  10845. ")
  10846. (defvar org-todo-statistics-hook nil
  10847. "Hook that is run whenever Org thinks TODO statistics should be updated.
  10848. This hook runs even if there is no statistics cookie present, in which case
  10849. `org-after-todo-statistics-hook' would not run.")
  10850. (defun org-todo-trigger-tag-changes (state)
  10851. "Apply the changes defined in `org-todo-state-tags-triggers'."
  10852. (let ((l org-todo-state-tags-triggers)
  10853. changes)
  10854. (when (or (not state) (equal state ""))
  10855. (setq changes (append changes (cdr (assoc "" l)))))
  10856. (when (and (stringp state) (> (length state) 0))
  10857. (setq changes (append changes (cdr (assoc state l)))))
  10858. (when (member state org-not-done-keywords)
  10859. (setq changes (append changes (cdr (assoc 'todo l)))))
  10860. (when (member state org-done-keywords)
  10861. (setq changes (append changes (cdr (assoc 'done l)))))
  10862. (dolist (c changes)
  10863. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  10864. (defun org-local-logging (value)
  10865. "Get logging settings from a property VALUE."
  10866. (let* (words w a)
  10867. ;; directly set the variables, they are already local.
  10868. (setq org-log-done nil
  10869. org-log-repeat nil
  10870. org-todo-log-states nil)
  10871. (setq words (org-split-string value))
  10872. (while (setq w (pop words))
  10873. (cond
  10874. ((setq a (assoc w org-startup-options))
  10875. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  10876. (set (nth 1 a) (nth 2 a))))
  10877. ((setq a (org-extract-log-state-settings w))
  10878. (and (member (car a) org-todo-keywords-1)
  10879. (push a org-todo-log-states)))))))
  10880. (defun org-get-todo-sequence-head (kwd)
  10881. "Return the head of the TODO sequence to which KWD belongs.
  10882. If KWD is not set, check if there is a text property remembering the
  10883. right sequence."
  10884. (let (p)
  10885. (cond
  10886. ((not kwd)
  10887. (or (get-text-property (point-at-bol) 'org-todo-head)
  10888. (progn
  10889. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  10890. nil (point-at-eol)))
  10891. (get-text-property p 'org-todo-head))))
  10892. ((not (member kwd org-todo-keywords-1))
  10893. (car org-todo-keywords-1))
  10894. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  10895. (defun org-fast-todo-selection ()
  10896. "Fast TODO keyword selection with single keys.
  10897. Returns the new TODO keyword, or nil if no state change should occur."
  10898. (let* ((fulltable org-todo-key-alist)
  10899. (done-keywords org-done-keywords) ;; needed for the faces.
  10900. (maxlen (apply 'max (mapcar
  10901. (lambda (x)
  10902. (if (stringp (car x)) (string-width (car x)) 0))
  10903. fulltable)))
  10904. (expert nil)
  10905. (fwidth (+ maxlen 3 1 3))
  10906. (ncol (/ (- (window-width) 4) fwidth))
  10907. tg cnt e c tbl
  10908. groups ingroup)
  10909. (save-excursion
  10910. (save-window-excursion
  10911. (if expert
  10912. (set-buffer (get-buffer-create " *Org todo*"))
  10913. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  10914. (erase-buffer)
  10915. (org-set-local 'org-done-keywords done-keywords)
  10916. (setq tbl fulltable cnt 0)
  10917. (while (setq e (pop tbl))
  10918. (cond
  10919. ((equal e '(:startgroup))
  10920. (push '() groups) (setq ingroup t)
  10921. (when (not (= cnt 0))
  10922. (setq cnt 0)
  10923. (insert "\n"))
  10924. (insert "{ "))
  10925. ((equal e '(:endgroup))
  10926. (setq ingroup nil cnt 0)
  10927. (insert "}\n"))
  10928. ((equal e '(:newline))
  10929. (when (not (= cnt 0))
  10930. (setq cnt 0)
  10931. (insert "\n")
  10932. (setq e (car tbl))
  10933. (while (equal (car tbl) '(:newline))
  10934. (insert "\n")
  10935. (setq tbl (cdr tbl)))))
  10936. (t
  10937. (setq tg (car e) c (cdr e))
  10938. (if ingroup (push tg (car groups)))
  10939. (setq tg (org-add-props tg nil 'face
  10940. (org-get-todo-face tg)))
  10941. (if (and (= cnt 0) (not ingroup)) (insert " "))
  10942. (insert "[" c "] " tg (make-string
  10943. (- fwidth 4 (length tg)) ?\ ))
  10944. (when (= (setq cnt (1+ cnt)) ncol)
  10945. (insert "\n")
  10946. (if ingroup (insert " "))
  10947. (setq cnt 0)))))
  10948. (insert "\n")
  10949. (goto-char (point-min))
  10950. (if (not expert) (org-fit-window-to-buffer))
  10951. (message "[a-z..]:Set [SPC]:clear")
  10952. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  10953. (cond
  10954. ((or (= c ?\C-g)
  10955. (and (= c ?q) (not (rassoc c fulltable))))
  10956. (setq quit-flag t))
  10957. ((= c ?\ ) nil)
  10958. ((setq e (rassoc c fulltable) tg (car e))
  10959. tg)
  10960. (t (setq quit-flag t)))))))
  10961. (defun org-entry-is-todo-p ()
  10962. (member (org-get-todo-state) org-not-done-keywords))
  10963. (defun org-entry-is-done-p ()
  10964. (member (org-get-todo-state) org-done-keywords))
  10965. (defun org-get-todo-state ()
  10966. (save-excursion
  10967. (org-back-to-heading t)
  10968. (and (looking-at org-todo-line-regexp)
  10969. (match-end 2)
  10970. (match-string 2))))
  10971. (defun org-at-date-range-p (&optional inactive-ok)
  10972. "Is the cursor inside a date range?"
  10973. (interactive)
  10974. (save-excursion
  10975. (catch 'exit
  10976. (let ((pos (point)))
  10977. (skip-chars-backward "^[<\r\n")
  10978. (skip-chars-backward "<[")
  10979. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  10980. (>= (match-end 0) pos)
  10981. (throw 'exit t))
  10982. (skip-chars-backward "^<[\r\n")
  10983. (skip-chars-backward "<[")
  10984. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  10985. (>= (match-end 0) pos)
  10986. (throw 'exit t)))
  10987. nil)))
  10988. (defun org-get-repeat (&optional tagline)
  10989. "Check if there is a deadline/schedule with repeater in this entry."
  10990. (save-match-data
  10991. (save-excursion
  10992. (org-back-to-heading t)
  10993. (and (re-search-forward (if tagline
  10994. (concat tagline "\\s-*" org-repeat-re)
  10995. org-repeat-re)
  10996. (org-entry-end-position) t)
  10997. (match-string-no-properties 1)))))
  10998. (defvar org-last-changed-timestamp)
  10999. (defvar org-last-inserted-timestamp)
  11000. (defvar org-log-post-message)
  11001. (defvar org-log-note-purpose)
  11002. (defvar org-log-note-how)
  11003. (defvar org-log-note-extra)
  11004. (defun org-auto-repeat-maybe (done-word)
  11005. "Check if the current headline contains a repeated deadline/schedule.
  11006. If yes, set TODO state back to what it was and change the base date
  11007. of repeating deadline/scheduled time stamps to new date.
  11008. This function is run automatically after each state change to a DONE state."
  11009. ;; last-state is dynamically scoped into this function
  11010. (let* ((repeat (org-get-repeat))
  11011. (aa (assoc org-last-state org-todo-kwd-alist))
  11012. (interpret (nth 1 aa))
  11013. (head (nth 2 aa))
  11014. (whata '(("h" . hour) ("d" . day) ("m" . month) ("y" . year)))
  11015. (msg "Entry repeats: ")
  11016. (org-log-done nil)
  11017. (org-todo-log-states nil)
  11018. re type n what ts time to-state)
  11019. (when repeat
  11020. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  11021. (setq to-state (or (org-entry-get nil "REPEAT_TO_STATE")
  11022. org-todo-repeat-to-state))
  11023. (unless (and to-state (member to-state org-todo-keywords-1))
  11024. (setq to-state (if (eq interpret 'type) org-last-state head)))
  11025. (org-todo to-state)
  11026. (when (or org-log-repeat (org-entry-get nil "CLOCK"))
  11027. (org-entry-put nil "LAST_REPEAT" (format-time-string
  11028. (org-time-stamp-format t t))))
  11029. (when org-log-repeat
  11030. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  11031. (memq 'org-add-log-note post-command-hook))
  11032. ;; OK, we are already setup for some record
  11033. (if (eq org-log-repeat 'note)
  11034. ;; make sure we take a note, not only a time stamp
  11035. (setq org-log-note-how 'note))
  11036. ;; Set up for taking a record
  11037. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  11038. org-last-state
  11039. 'findpos org-log-repeat)))
  11040. (org-back-to-heading t)
  11041. (org-add-planning-info nil nil 'closed)
  11042. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  11043. org-deadline-time-regexp "\\)\\|\\("
  11044. org-ts-regexp "\\)"))
  11045. (while (re-search-forward
  11046. re (save-excursion (outline-next-heading) (point)) t)
  11047. (setq type (if (match-end 1) org-scheduled-string
  11048. (if (match-end 3) org-deadline-string "Plain:"))
  11049. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  11050. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts)
  11051. (setq n (string-to-number (match-string 2 ts))
  11052. what (match-string 3 ts))
  11053. (if (equal what "w") (setq n (* n 7) what "d"))
  11054. (if (and (equal what "h") (not (string-match "[0-9]\\{1,2\\}:[0-9]\\{2\\}" ts)))
  11055. (error "Cannot repeat in Repeat in %d hour(s) because no hour has been set" n))
  11056. ;; Preparation, see if we need to modify the start date for the change
  11057. (when (match-end 1)
  11058. (setq time (save-match-data (org-time-string-to-time ts)))
  11059. (cond
  11060. ((equal (match-string 1 ts) ".")
  11061. ;; Shift starting date to today
  11062. (org-timestamp-change
  11063. (- (org-today) (time-to-days time))
  11064. 'day))
  11065. ((equal (match-string 1 ts) "+")
  11066. (let ((nshiftmax 10) (nshift 0))
  11067. (while (or (= nshift 0)
  11068. (<= (time-to-days time)
  11069. (time-to-days (current-time))))
  11070. (when (= (incf nshift) nshiftmax)
  11071. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  11072. (error "Abort")))
  11073. (org-timestamp-change n (cdr (assoc what whata)))
  11074. (org-at-timestamp-p t)
  11075. (setq ts (match-string 1))
  11076. (setq time (save-match-data (org-time-string-to-time ts)))))
  11077. (org-timestamp-change (- n) (cdr (assoc what whata)))
  11078. ;; rematch, so that we have everything in place for the real shift
  11079. (org-at-timestamp-p t)
  11080. (setq ts (match-string 1))
  11081. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts))))
  11082. (org-timestamp-change n (cdr (assoc what whata)))
  11083. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  11084. (setq org-log-post-message msg)
  11085. (message "%s" msg))))
  11086. (defun org-show-todo-tree (arg)
  11087. "Make a compact tree which shows all headlines marked with TODO.
  11088. The tree will show the lines where the regexp matches, and all higher
  11089. headlines above the match.
  11090. With a \\[universal-argument] prefix, prompt for a regexp to match.
  11091. With a numeric prefix N, construct a sparse tree for the Nth element
  11092. of `org-todo-keywords-1'."
  11093. (interactive "P")
  11094. (let ((case-fold-search nil)
  11095. (kwd-re
  11096. (cond ((null arg) org-not-done-regexp)
  11097. ((equal arg '(4))
  11098. (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
  11099. (mapcar 'list org-todo-keywords-1))))
  11100. (concat "\\("
  11101. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  11102. "\\)\\>")))
  11103. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  11104. (regexp-quote (nth (1- (prefix-numeric-value arg))
  11105. org-todo-keywords-1)))
  11106. (t (error "Invalid prefix argument: %s" arg)))))
  11107. (message "%d TODO entries found"
  11108. (org-occur (concat "^" org-outline-regexp " *" kwd-re )))))
  11109. (defun org-deadline (&optional remove time)
  11110. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  11111. With argument REMOVE, remove any deadline from the item.
  11112. With argument TIME, set the deadline at the corresponding date. TIME
  11113. can either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  11114. (interactive "P")
  11115. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  11116. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  11117. 'region-start-level 'region))
  11118. org-loop-over-headlines-in-active-region)
  11119. (org-map-entries
  11120. `(org-deadline ',remove ,time)
  11121. org-loop-over-headlines-in-active-region
  11122. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  11123. (let* ((old-date (org-entry-get nil "DEADLINE"))
  11124. (repeater (and old-date
  11125. (string-match
  11126. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  11127. old-date)
  11128. (match-string 1 old-date))))
  11129. (if remove
  11130. (progn
  11131. (when (and old-date org-log-redeadline)
  11132. (org-add-log-setup 'deldeadline nil old-date 'findpos
  11133. org-log-redeadline))
  11134. (org-remove-timestamp-with-keyword org-deadline-string)
  11135. (message "Item no longer has a deadline."))
  11136. (org-add-planning-info 'deadline time 'closed)
  11137. (when (and old-date org-log-redeadline
  11138. (not (equal old-date
  11139. (substring org-last-inserted-timestamp 1 -1))))
  11140. (org-add-log-setup 'redeadline nil old-date 'findpos
  11141. org-log-redeadline))
  11142. (when repeater
  11143. (save-excursion
  11144. (org-back-to-heading t)
  11145. (when (re-search-forward (concat org-deadline-string " "
  11146. org-last-inserted-timestamp)
  11147. (save-excursion
  11148. (outline-next-heading) (point)) t)
  11149. (goto-char (1- (match-end 0)))
  11150. (insert " " repeater)
  11151. (setq org-last-inserted-timestamp
  11152. (concat (substring org-last-inserted-timestamp 0 -1)
  11153. " " repeater
  11154. (substring org-last-inserted-timestamp -1))))))
  11155. (message "Deadline on %s" org-last-inserted-timestamp)))))
  11156. (defun org-schedule (&optional remove time)
  11157. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  11158. With argument REMOVE, remove any scheduling date from the item.
  11159. With argument TIME, scheduled at the corresponding date. TIME can
  11160. either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  11161. (interactive "P")
  11162. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  11163. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  11164. 'region-start-level 'region))
  11165. org-loop-over-headlines-in-active-region)
  11166. (org-map-entries
  11167. `(org-schedule ',remove ,time)
  11168. org-loop-over-headlines-in-active-region
  11169. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  11170. (let* ((old-date (org-entry-get nil "SCHEDULED"))
  11171. (repeater (and old-date
  11172. (string-match
  11173. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  11174. old-date)
  11175. (match-string 1 old-date))))
  11176. (if remove
  11177. (progn
  11178. (when (and old-date org-log-reschedule)
  11179. (org-add-log-setup 'delschedule nil old-date 'findpos
  11180. org-log-reschedule))
  11181. (org-remove-timestamp-with-keyword org-scheduled-string)
  11182. (message "Item is no longer scheduled."))
  11183. (org-add-planning-info 'scheduled time 'closed)
  11184. (when (and old-date org-log-reschedule
  11185. (not (equal old-date
  11186. (substring org-last-inserted-timestamp 1 -1))))
  11187. (org-add-log-setup 'reschedule nil old-date 'findpos
  11188. org-log-reschedule))
  11189. (when repeater
  11190. (save-excursion
  11191. (org-back-to-heading t)
  11192. (when (re-search-forward (concat org-scheduled-string " "
  11193. org-last-inserted-timestamp)
  11194. (save-excursion
  11195. (outline-next-heading) (point)) t)
  11196. (goto-char (1- (match-end 0)))
  11197. (insert " " repeater)
  11198. (setq org-last-inserted-timestamp
  11199. (concat (substring org-last-inserted-timestamp 0 -1)
  11200. " " repeater
  11201. (substring org-last-inserted-timestamp -1))))))
  11202. (message "Scheduled to %s" org-last-inserted-timestamp)))))
  11203. (defun org-get-scheduled-time (pom &optional inherit)
  11204. "Get the scheduled time as a time tuple, of a format suitable
  11205. for calling org-schedule with, or if there is no scheduling,
  11206. returns nil."
  11207. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  11208. (when time
  11209. (apply 'encode-time (org-parse-time-string time)))))
  11210. (defun org-get-deadline-time (pom &optional inherit)
  11211. "Get the deadline as a time tuple, of a format suitable for
  11212. calling org-deadline with, or if there is no scheduling, returns
  11213. nil."
  11214. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  11215. (when time
  11216. (apply 'encode-time (org-parse-time-string time)))))
  11217. (defun org-remove-timestamp-with-keyword (keyword)
  11218. "Remove all time stamps with KEYWORD in the current entry."
  11219. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  11220. beg)
  11221. (save-excursion
  11222. (org-back-to-heading t)
  11223. (setq beg (point))
  11224. (outline-next-heading)
  11225. (while (re-search-backward re beg t)
  11226. (replace-match "")
  11227. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  11228. (equal (char-before) ?\ ))
  11229. (backward-delete-char 1)
  11230. (if (string-match "^[ \t]*$" (buffer-substring
  11231. (point-at-bol) (point-at-eol)))
  11232. (delete-region (point-at-bol)
  11233. (min (point-max) (1+ (point-at-eol))))))))))
  11234. (defun org-add-planning-info (what &optional time &rest remove)
  11235. "Insert new timestamp with keyword in the line directly after the headline.
  11236. WHAT indicates what kind of time stamp to add. TIME indicates the time to use.
  11237. If non is given, the user is prompted for a date.
  11238. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  11239. be removed."
  11240. (interactive)
  11241. (let (org-time-was-given org-end-time-was-given ts
  11242. end default-time default-input)
  11243. (catch 'exit
  11244. (when (and (memq what '(scheduled deadline))
  11245. (or (not time)
  11246. (and (stringp time)
  11247. (string-match "^[-+]+[0-9]" time))))
  11248. ;; Try to get a default date/time from existing timestamp
  11249. (save-excursion
  11250. (org-back-to-heading t)
  11251. (setq end (save-excursion (outline-next-heading) (point)))
  11252. (when (re-search-forward (if (eq what 'scheduled)
  11253. org-scheduled-time-regexp
  11254. org-deadline-time-regexp)
  11255. end t)
  11256. (setq ts (match-string 1)
  11257. default-time
  11258. (apply 'encode-time (org-parse-time-string ts))
  11259. default-input (and ts (org-get-compact-tod ts))))))
  11260. (when what
  11261. (setq time
  11262. (if (stringp time)
  11263. ;; This is a string (relative or absolute), set proper date
  11264. (apply 'encode-time
  11265. (org-read-date-analyze
  11266. time default-time (decode-time default-time)))
  11267. ;; If necessary, get the time from the user
  11268. (or time (org-read-date nil 'to-time nil nil
  11269. default-time default-input)))))
  11270. (when (and org-insert-labeled-timestamps-at-point
  11271. (member what '(scheduled deadline)))
  11272. (insert
  11273. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  11274. (org-insert-time-stamp time org-time-was-given
  11275. nil nil nil (list org-end-time-was-given))
  11276. (setq what nil))
  11277. (save-excursion
  11278. (save-restriction
  11279. (let (col list elt ts buffer-invisibility-spec)
  11280. (org-back-to-heading t)
  11281. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"))
  11282. (goto-char (match-end 1))
  11283. (setq col (current-column))
  11284. (goto-char (match-end 0))
  11285. (if (eobp) (insert "\n") (forward-char 1))
  11286. (when (and (not what)
  11287. (not (looking-at
  11288. (concat "[ \t]*"
  11289. org-keyword-time-not-clock-regexp))))
  11290. ;; Nothing to add, nothing to remove...... :-)
  11291. (throw 'exit nil))
  11292. (if (and (not (looking-at org-outline-regexp))
  11293. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  11294. "[^\r\n]*"))
  11295. (not (equal (match-string 1) org-clock-string)))
  11296. (narrow-to-region (match-beginning 0) (match-end 0))
  11297. (insert-before-markers "\n")
  11298. (backward-char 1)
  11299. (narrow-to-region (point) (point))
  11300. (and org-adapt-indentation (org-indent-to-column col)))
  11301. ;; Check if we have to remove something.
  11302. (setq list (cons what remove))
  11303. (while list
  11304. (setq elt (pop list))
  11305. (when (or (and (eq elt 'scheduled)
  11306. (re-search-forward org-scheduled-time-regexp nil t))
  11307. (and (eq elt 'deadline)
  11308. (re-search-forward org-deadline-time-regexp nil t))
  11309. (and (eq elt 'closed)
  11310. (re-search-forward org-closed-time-regexp nil t)))
  11311. (replace-match "")
  11312. (if (looking-at "--+<[^>]+>") (replace-match ""))))
  11313. (and (looking-at "[ \t]+") (replace-match ""))
  11314. (and org-adapt-indentation (bolp) (org-indent-to-column col))
  11315. (when what
  11316. (insert
  11317. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  11318. (cond ((eq what 'scheduled) org-scheduled-string)
  11319. ((eq what 'deadline) org-deadline-string)
  11320. ((eq what 'closed) org-closed-string))
  11321. " ")
  11322. (setq ts (org-insert-time-stamp
  11323. time
  11324. (or org-time-was-given
  11325. (and (eq what 'closed) org-log-done-with-time))
  11326. (eq what 'closed)
  11327. nil nil (list org-end-time-was-given)))
  11328. (insert
  11329. (if (not (or (bolp) (eq (char-before) ?\ )
  11330. (memq (char-after) '(32 10))
  11331. (eobp))) " " ""))
  11332. (end-of-line 1))
  11333. (goto-char (point-min))
  11334. (widen)
  11335. (if (and (looking-at "[ \t]*\n")
  11336. (equal (char-before) ?\n))
  11337. (delete-region (1- (point)) (point-at-eol)))
  11338. ts))))))
  11339. (defvar org-log-note-marker (make-marker))
  11340. (defvar org-log-note-purpose nil)
  11341. (defvar org-log-note-state nil)
  11342. (defvar org-log-note-previous-state nil)
  11343. (defvar org-log-note-how nil)
  11344. (defvar org-log-note-extra nil)
  11345. (defvar org-log-note-window-configuration nil)
  11346. (defvar org-log-note-return-to (make-marker))
  11347. (defvar org-log-note-effective-time nil
  11348. "Remembered current time so that dynamically scoped
  11349. `org-extend-today-until' affects tha timestamps in state change
  11350. log")
  11351. (defvar org-log-post-message nil
  11352. "Message to be displayed after a log note has been stored.
  11353. The auto-repeater uses this.")
  11354. (defun org-add-note ()
  11355. "Add a note to the current entry.
  11356. This is done in the same way as adding a state change note."
  11357. (interactive)
  11358. (org-add-log-setup 'note nil nil 'findpos nil))
  11359. (defvar org-property-end-re)
  11360. (defun org-add-log-setup (&optional purpose state prev-state
  11361. findpos how extra)
  11362. "Set up the post command hook to take a note.
  11363. If this is about to TODO state change, the new state is expected in STATE.
  11364. When FINDPOS is non-nil, find the correct position for the note in
  11365. the current entry. If not, assume that it can be inserted at point.
  11366. HOW is an indicator what kind of note should be created.
  11367. EXTRA is additional text that will be inserted into the notes buffer."
  11368. (let* ((org-log-into-drawer (org-log-into-drawer))
  11369. (drawer (cond ((stringp org-log-into-drawer)
  11370. org-log-into-drawer)
  11371. (org-log-into-drawer "LOGBOOK"))))
  11372. (save-restriction
  11373. (save-excursion
  11374. (when findpos
  11375. (org-back-to-heading t)
  11376. (narrow-to-region (point) (save-excursion
  11377. (outline-next-heading) (point)))
  11378. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"
  11379. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  11380. "[^\r\n]*\\)?"))
  11381. (goto-char (match-end 0))
  11382. (cond
  11383. (drawer
  11384. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  11385. nil t)
  11386. (progn
  11387. (goto-char (match-end 0))
  11388. (or org-log-states-order-reversed
  11389. (and (re-search-forward org-property-end-re nil t)
  11390. (goto-char (1- (match-beginning 0))))))
  11391. (insert "\n:" drawer ":\n:END:")
  11392. (beginning-of-line 0)
  11393. (org-indent-line)
  11394. (beginning-of-line 2)
  11395. (org-indent-line)
  11396. (end-of-line 0)))
  11397. ((and org-log-state-notes-insert-after-drawers
  11398. (save-excursion
  11399. (forward-line) (looking-at org-drawer-regexp)))
  11400. (forward-line)
  11401. (while (looking-at org-drawer-regexp)
  11402. (goto-char (match-end 0))
  11403. (re-search-forward org-property-end-re (point-max) t)
  11404. (forward-line))
  11405. (forward-line -1)))
  11406. (unless org-log-states-order-reversed
  11407. (and (= (char-after) ?\n) (forward-char 1))
  11408. (org-skip-over-state-notes)
  11409. (skip-chars-backward " \t\n\r")))
  11410. (move-marker org-log-note-marker (point))
  11411. (setq org-log-note-purpose purpose
  11412. org-log-note-state state
  11413. org-log-note-previous-state prev-state
  11414. org-log-note-how how
  11415. org-log-note-extra extra
  11416. org-log-note-effective-time (org-current-effective-time))
  11417. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  11418. (defun org-skip-over-state-notes ()
  11419. "Skip past the list of State notes in an entry."
  11420. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  11421. (when (ignore-errors (goto-char (org-in-item-p)))
  11422. (let* ((struct (org-list-struct))
  11423. (prevs (org-list-prevs-alist struct)))
  11424. (while (looking-at "[ \t]*- State")
  11425. (goto-char (or (org-list-get-next-item (point) struct prevs)
  11426. (org-list-get-item-end (point) struct)))))))
  11427. (defun org-add-log-note (&optional purpose)
  11428. "Pop up a window for taking a note, and add this note later at point."
  11429. (remove-hook 'post-command-hook 'org-add-log-note)
  11430. (setq org-log-note-window-configuration (current-window-configuration))
  11431. (delete-other-windows)
  11432. (move-marker org-log-note-return-to (point))
  11433. (org-pop-to-buffer-same-window (marker-buffer org-log-note-marker))
  11434. (goto-char org-log-note-marker)
  11435. (org-switch-to-buffer-other-window "*Org Note*")
  11436. (erase-buffer)
  11437. (if (memq org-log-note-how '(time state))
  11438. (let (current-prefix-arg) (org-store-log-note))
  11439. (let ((org-inhibit-startup t)) (org-mode))
  11440. (insert (format "# Insert note for %s.
  11441. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  11442. (cond
  11443. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  11444. ((eq org-log-note-purpose 'done) "closed todo item")
  11445. ((eq org-log-note-purpose 'state)
  11446. (format "state change from \"%s\" to \"%s\""
  11447. (or org-log-note-previous-state "")
  11448. (or org-log-note-state "")))
  11449. ((eq org-log-note-purpose 'reschedule)
  11450. "rescheduling")
  11451. ((eq org-log-note-purpose 'delschedule)
  11452. "no longer scheduled")
  11453. ((eq org-log-note-purpose 'redeadline)
  11454. "changing deadline")
  11455. ((eq org-log-note-purpose 'deldeadline)
  11456. "removing deadline")
  11457. ((eq org-log-note-purpose 'refile)
  11458. "refiling")
  11459. ((eq org-log-note-purpose 'note)
  11460. "this entry")
  11461. (t (error "This should not happen")))))
  11462. (if org-log-note-extra (insert org-log-note-extra))
  11463. (org-set-local 'org-finish-function 'org-store-log-note)
  11464. (run-hooks 'org-log-buffer-setup-hook)))
  11465. (defvar org-note-abort nil) ; dynamically scoped
  11466. (defun org-store-log-note ()
  11467. "Finish taking a log note, and insert it to where it belongs."
  11468. (let ((txt (buffer-string))
  11469. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  11470. lines ind bul)
  11471. (kill-buffer (current-buffer))
  11472. (while (string-match "\\`# .*\n[ \t\n]*" txt)
  11473. (setq txt (replace-match "" t t txt)))
  11474. (if (string-match "\\s-+\\'" txt)
  11475. (setq txt (replace-match "" t t txt)))
  11476. (setq lines (org-split-string txt "\n"))
  11477. (when (and note (string-match "\\S-" note))
  11478. (setq note
  11479. (org-replace-escapes
  11480. note
  11481. (list (cons "%u" (user-login-name))
  11482. (cons "%U" user-full-name)
  11483. (cons "%t" (format-time-string
  11484. (org-time-stamp-format 'long 'inactive)
  11485. org-log-note-effective-time))
  11486. (cons "%T" (format-time-string
  11487. (org-time-stamp-format 'long nil)
  11488. org-log-note-effective-time))
  11489. (cons "%d" (format-time-string
  11490. (org-time-stamp-format nil 'inactive)
  11491. org-log-note-effective-time))
  11492. (cons "%D" (format-time-string
  11493. (org-time-stamp-format nil nil)
  11494. org-log-note-effective-time))
  11495. (cons "%s" (if org-log-note-state
  11496. (concat "\"" org-log-note-state "\"")
  11497. ""))
  11498. (cons "%S" (if org-log-note-previous-state
  11499. (concat "\"" org-log-note-previous-state "\"")
  11500. "\"\"")))))
  11501. (if lines (setq note (concat note " \\\\")))
  11502. (push note lines))
  11503. (when (or current-prefix-arg org-note-abort)
  11504. (when org-log-into-drawer
  11505. (org-remove-empty-drawer-at
  11506. (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
  11507. org-log-note-marker))
  11508. (setq lines nil))
  11509. (when lines
  11510. (with-current-buffer (marker-buffer org-log-note-marker)
  11511. (save-excursion
  11512. (goto-char org-log-note-marker)
  11513. (move-marker org-log-note-marker nil)
  11514. (end-of-line 1)
  11515. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  11516. (setq ind (save-excursion
  11517. (if (ignore-errors (goto-char (org-in-item-p)))
  11518. (let ((struct (org-list-struct)))
  11519. (org-list-get-ind
  11520. (org-list-get-top-point struct) struct))
  11521. (skip-chars-backward " \r\t\n")
  11522. (cond
  11523. ((and (org-at-heading-p)
  11524. org-adapt-indentation)
  11525. (1+ (org-current-level)))
  11526. ((org-at-heading-p) 0)
  11527. (t (org-get-indentation))))))
  11528. (setq bul (org-list-bullet-string "-"))
  11529. (org-indent-line-to ind)
  11530. (insert bul (pop lines))
  11531. (let ((ind-body (+ (length bul) ind)))
  11532. (while lines
  11533. (insert "\n")
  11534. (org-indent-line-to ind-body)
  11535. (insert (pop lines))))
  11536. (message "Note stored")
  11537. (org-back-to-heading t)
  11538. (org-cycle-hide-drawers 'children)))))
  11539. (set-window-configuration org-log-note-window-configuration)
  11540. (with-current-buffer (marker-buffer org-log-note-return-to)
  11541. (goto-char org-log-note-return-to))
  11542. (move-marker org-log-note-return-to nil)
  11543. (and org-log-post-message (message "%s" org-log-post-message)))
  11544. (defun org-remove-empty-drawer-at (drawer pos)
  11545. "Remove an empty drawer DRAWER at position POS.
  11546. POS may also be a marker."
  11547. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  11548. (save-excursion
  11549. (save-restriction
  11550. (widen)
  11551. (goto-char pos)
  11552. (if (org-in-regexp
  11553. (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
  11554. (replace-match ""))))))
  11555. (defvar org-ts-type nil)
  11556. (defun org-sparse-tree (&optional arg type)
  11557. "Create a sparse tree, prompt for the details.
  11558. This command can create sparse trees. You first need to select the type
  11559. of match used to create the tree:
  11560. t Show all TODO entries.
  11561. T Show entries with a specific TODO keyword.
  11562. m Show entries selected by a tags/property match.
  11563. p Enter a property name and its value (both with completion on existing
  11564. names/values) and show entries with that property.
  11565. r Show entries matching a regular expression (`/' can be used as well).
  11566. b Show deadlines and scheduled items before a date.
  11567. a Show deadlines and scheduled items after a date.
  11568. d Show deadlines due within `org-deadline-warning-days'.
  11569. D Show deadlines and scheduled items between a date range."
  11570. (interactive "P")
  11571. (let (ans kwd value ts-type)
  11572. (setq type (or type org-sparse-tree-default-date-type))
  11573. (setq org-ts-type type)
  11574. (message "Sparse tree: [r]egexp [/]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"
  11575. (cond ((eq type 'all) "all timestamps")
  11576. ((eq type 'scheduled) "only scheduled")
  11577. ((eq type 'deadline) "only deadline")
  11578. ((eq type 'active) "only active timestamps")
  11579. ((eq type 'inactive) "only inactive timestamps")
  11580. ((eq type 'scheduled-or-deadline) "scheduled/deadline")
  11581. (t "scheduled/deadline")))
  11582. (setq ans (read-char-exclusive))
  11583. (cond
  11584. ((equal ans ?c)
  11585. (org-sparse-tree arg (cadr (member type '(scheduled-or-deadline all scheduled deadline active inactive)))))
  11586. ((equal ans ?d)
  11587. (call-interactively 'org-check-deadlines))
  11588. ((equal ans ?b)
  11589. (call-interactively 'org-check-before-date))
  11590. ((equal ans ?a)
  11591. (call-interactively 'org-check-after-date))
  11592. ((equal ans ?D)
  11593. (call-interactively 'org-check-dates-range))
  11594. ((equal ans ?t)
  11595. (org-show-todo-tree nil))
  11596. ((equal ans ?T)
  11597. (org-show-todo-tree '(4)))
  11598. ((member ans '(?T ?m))
  11599. (call-interactively 'org-match-sparse-tree))
  11600. ((member ans '(?p ?P))
  11601. (setq kwd (org-icompleting-read "Property: "
  11602. (mapcar 'list (org-buffer-property-keys))))
  11603. (setq value (org-icompleting-read "Value: "
  11604. (mapcar 'list (org-property-values kwd))))
  11605. (unless (string-match "\\`{.*}\\'" value)
  11606. (setq value (concat "\"" value "\"")))
  11607. (org-match-sparse-tree arg (concat kwd "=" value)))
  11608. ((member ans '(?r ?R ?/))
  11609. (call-interactively 'org-occur))
  11610. (t (error "No such sparse tree command \"%c\"" ans)))))
  11611. (defvar org-occur-highlights nil
  11612. "List of overlays used for occur matches.")
  11613. (make-variable-buffer-local 'org-occur-highlights)
  11614. (defvar org-occur-parameters nil
  11615. "Parameters of the active org-occur calls.
  11616. This is a list, each call to org-occur pushes as cons cell,
  11617. containing the regular expression and the callback, onto the list.
  11618. The list can contain several entries if `org-occur' has been called
  11619. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  11620. will only contain one set of parameters. When the highlights are
  11621. removed (for example with `C-c C-c', or with the next edit (depending
  11622. on `org-remove-highlights-with-change'), this variable is emptied
  11623. as well.")
  11624. (make-variable-buffer-local 'org-occur-parameters)
  11625. (defun org-occur (regexp &optional keep-previous callback)
  11626. "Make a compact tree which shows all matches of REGEXP.
  11627. The tree will show the lines where the regexp matches, and all higher
  11628. headlines above the match. It will also show the heading after the match,
  11629. to make sure editing the matching entry is easy.
  11630. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  11631. call to `org-occur' will be kept, to allow stacking of calls to this
  11632. command.
  11633. If CALLBACK is non-nil, it is a function which is called to confirm
  11634. that the match should indeed be shown."
  11635. (interactive "sRegexp: \nP")
  11636. (when (equal regexp "")
  11637. (error "Regexp cannot be empty"))
  11638. (unless keep-previous
  11639. (org-remove-occur-highlights nil nil t))
  11640. (push (cons regexp callback) org-occur-parameters)
  11641. (let ((cnt 0))
  11642. (save-excursion
  11643. (goto-char (point-min))
  11644. (if (or (not keep-previous) ; do not want to keep
  11645. (not org-occur-highlights)) ; no previous matches
  11646. ;; hide everything
  11647. (org-overview))
  11648. (while (re-search-forward regexp nil t)
  11649. (when (or (not callback)
  11650. (save-match-data (funcall callback)))
  11651. (setq cnt (1+ cnt))
  11652. (when org-highlight-sparse-tree-matches
  11653. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  11654. (org-show-context 'occur-tree))))
  11655. (when org-remove-highlights-with-change
  11656. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  11657. nil 'local))
  11658. (unless org-sparse-tree-open-archived-trees
  11659. (org-hide-archived-subtrees (point-min) (point-max)))
  11660. (run-hooks 'org-occur-hook)
  11661. (if (org-called-interactively-p 'interactive)
  11662. (message "%d match(es) for regexp %s" cnt regexp))
  11663. cnt))
  11664. (defun org-occur-next-match (&optional n reset)
  11665. "Function for `next-error-function' to find sparse tree matches.
  11666. N is the number of matches to move, when negative move backwards.
  11667. RESET is entirely ignored - this function always goes back to the
  11668. starting point when no match is found."
  11669. (let* ((limit (if (< n 0) (point-min) (point-max)))
  11670. (search-func (if (< n 0)
  11671. 'previous-single-char-property-change
  11672. 'next-single-char-property-change))
  11673. (n (abs n))
  11674. (pos (point))
  11675. p1)
  11676. (catch 'exit
  11677. (while (setq p1 (funcall search-func (point) 'org-type))
  11678. (when (equal p1 limit)
  11679. (goto-char pos)
  11680. (error "No more matches"))
  11681. (when (equal (get-char-property p1 'org-type) 'org-occur)
  11682. (setq n (1- n))
  11683. (when (= n 0)
  11684. (goto-char p1)
  11685. (throw 'exit (point))))
  11686. (goto-char p1))
  11687. (goto-char p1)
  11688. (error "No more matches"))))
  11689. (defun org-show-context (&optional key)
  11690. "Make sure point and context are visible.
  11691. How much context is shown depends upon the variables
  11692. `org-show-hierarchy-above', `org-show-following-heading',
  11693. `org-show-entry-below' and `org-show-siblings'."
  11694. (let ((heading-p (org-at-heading-p t))
  11695. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  11696. (following-p (org-get-alist-option org-show-following-heading key))
  11697. (entry-p (org-get-alist-option org-show-entry-below key))
  11698. (siblings-p (org-get-alist-option org-show-siblings key)))
  11699. (catch 'exit
  11700. ;; Show heading or entry text
  11701. (if (and heading-p (not entry-p))
  11702. (org-flag-heading nil) ; only show the heading
  11703. (and (or entry-p (outline-invisible-p) (org-invisible-p2))
  11704. (org-show-hidden-entry))) ; show entire entry
  11705. (when following-p
  11706. ;; Show next sibling, or heading below text
  11707. (save-excursion
  11708. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  11709. (org-flag-heading nil))))
  11710. (when siblings-p (org-show-siblings))
  11711. (when hierarchy-p
  11712. ;; show all higher headings, possibly with siblings
  11713. (save-excursion
  11714. (while (and (condition-case nil
  11715. (progn (org-up-heading-all 1) t)
  11716. (error nil))
  11717. (not (bobp)))
  11718. (org-flag-heading nil)
  11719. (when siblings-p (org-show-siblings))))))))
  11720. (defvar org-reveal-start-hook nil
  11721. "Hook run before revealing a location.")
  11722. (defun org-reveal (&optional siblings)
  11723. "Show current entry, hierarchy above it, and the following headline.
  11724. This can be used to show a consistent set of context around locations
  11725. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  11726. not t for the search context.
  11727. With optional argument SIBLINGS, on each level of the hierarchy all
  11728. siblings are shown. This repairs the tree structure to what it would
  11729. look like when opened with hierarchical calls to `org-cycle'.
  11730. With double optional argument \\[universal-argument] \\[universal-argument], \
  11731. go to the parent and show the
  11732. entire tree."
  11733. (interactive "P")
  11734. (run-hooks 'org-reveal-start-hook)
  11735. (let ((org-show-hierarchy-above t)
  11736. (org-show-following-heading t)
  11737. (org-show-siblings (if siblings t org-show-siblings)))
  11738. (org-show-context nil))
  11739. (when (equal siblings '(16))
  11740. (save-excursion
  11741. (when (org-up-heading-safe)
  11742. (org-show-subtree)
  11743. (run-hook-with-args 'org-cycle-hook 'subtree)))))
  11744. (defun org-highlight-new-match (beg end)
  11745. "Highlight from BEG to END and mark the highlight is an occur headline."
  11746. (let ((ov (make-overlay beg end)))
  11747. (overlay-put ov 'face 'secondary-selection)
  11748. (overlay-put ov 'org-type 'org-occur)
  11749. (push ov org-occur-highlights)))
  11750. (defun org-remove-occur-highlights (&optional beg end noremove)
  11751. "Remove the occur highlights from the buffer.
  11752. BEG and END are ignored. If NOREMOVE is nil, remove this function
  11753. from the `before-change-functions' in the current buffer."
  11754. (interactive)
  11755. (unless org-inhibit-highlight-removal
  11756. (mapc 'delete-overlay org-occur-highlights)
  11757. (setq org-occur-highlights nil)
  11758. (setq org-occur-parameters nil)
  11759. (unless noremove
  11760. (remove-hook 'before-change-functions
  11761. 'org-remove-occur-highlights 'local))))
  11762. ;;;; Priorities
  11763. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  11764. "Regular expression matching the priority indicator.")
  11765. (defvar org-remove-priority-next-time nil)
  11766. (defun org-priority-up ()
  11767. "Increase the priority of the current item."
  11768. (interactive)
  11769. (org-priority 'up))
  11770. (defun org-priority-down ()
  11771. "Decrease the priority of the current item."
  11772. (interactive)
  11773. (org-priority 'down))
  11774. (defun org-priority (&optional action show)
  11775. "Change the priority of an item.
  11776. ACTION can be `set', `up', `down', or a character."
  11777. (interactive "P")
  11778. (if (equal action '(4))
  11779. (org-show-priority)
  11780. (unless org-enable-priority-commands
  11781. (error "Priority commands are disabled"))
  11782. (setq action (or action 'set))
  11783. (let (current new news have remove)
  11784. (save-excursion
  11785. (org-back-to-heading t)
  11786. (if (looking-at org-priority-regexp)
  11787. (setq current (string-to-char (match-string 2))
  11788. have t))
  11789. (cond
  11790. ((eq action 'remove)
  11791. (setq remove t new ?\ ))
  11792. ((or (eq action 'set)
  11793. (if (featurep 'xemacs) (characterp action) (integerp action)))
  11794. (if (not (eq action 'set))
  11795. (setq new action)
  11796. (message "Priority %c-%c, SPC to remove: "
  11797. org-highest-priority org-lowest-priority)
  11798. (save-match-data
  11799. (setq new (read-char-exclusive))))
  11800. (if (and (= (upcase org-highest-priority) org-highest-priority)
  11801. (= (upcase org-lowest-priority) org-lowest-priority))
  11802. (setq new (upcase new)))
  11803. (cond ((equal new ?\ ) (setq remove t))
  11804. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  11805. (error "Priority must be between `%c' and `%c'"
  11806. org-highest-priority org-lowest-priority))))
  11807. ((eq action 'up)
  11808. (setq new (if have
  11809. (1- current) ; normal cycling
  11810. ;; last priority was empty
  11811. (if (eq last-command this-command)
  11812. org-lowest-priority ; wrap around empty to lowest
  11813. ;; default
  11814. (if org-priority-start-cycle-with-default
  11815. org-default-priority
  11816. (1- org-default-priority))))))
  11817. ((eq action 'down)
  11818. (setq new (if have
  11819. (1+ current) ; normal cycling
  11820. ;; last priority was empty
  11821. (if (eq last-command this-command)
  11822. org-highest-priority ; wrap around empty to highest
  11823. ;; default
  11824. (if org-priority-start-cycle-with-default
  11825. org-default-priority
  11826. (1+ org-default-priority))))))
  11827. (t (error "Invalid action")))
  11828. (if (or (< (upcase new) org-highest-priority)
  11829. (> (upcase new) org-lowest-priority))
  11830. (if (and (memq action '(up down))
  11831. (not have) (not (eq last-command this-command)))
  11832. ;; `new' is from default priority
  11833. (error
  11834. "The default can not be set, see `org-default-priority' why")
  11835. ;; normal cycling: `new' is beyond highest/lowest priority
  11836. ;; and is wrapped around to the empty priority
  11837. (setq remove t)))
  11838. (setq news (format "%c" new))
  11839. (if have
  11840. (if remove
  11841. (replace-match "" t t nil 1)
  11842. (replace-match news t t nil 2))
  11843. (if remove
  11844. (error "No priority cookie found in line")
  11845. (let ((case-fold-search nil))
  11846. (looking-at org-todo-line-regexp))
  11847. (if (match-end 2)
  11848. (progn
  11849. (goto-char (match-end 2))
  11850. (insert " [#" news "]"))
  11851. (goto-char (match-beginning 3))
  11852. (insert "[#" news "] "))))
  11853. (org-preserve-lc (org-set-tags nil 'align)))
  11854. (if remove
  11855. (message "Priority removed")
  11856. (message "Priority of current item set to %s" news)))))
  11857. (defun org-show-priority ()
  11858. "Show the priority of the current item.
  11859. This priority is composed of the main priority given with the [#A] cookies,
  11860. and by additional input from the age of a schedules or deadline entry."
  11861. (interactive)
  11862. (let ((pri (if (eq major-mode 'org-agenda-mode)
  11863. (org-get-at-bol 'priority)
  11864. (save-excursion
  11865. (save-match-data
  11866. (beginning-of-line)
  11867. (and (looking-at org-heading-regexp)
  11868. (org-get-priority (match-string 0))))))))
  11869. (message "Priority is %d" (if pri pri -1000))))
  11870. (defun org-get-priority (s)
  11871. "Find priority cookie and return priority."
  11872. (if (functionp org-get-priority-function)
  11873. (funcall org-get-priority-function)
  11874. (save-match-data
  11875. (if (not (string-match org-priority-regexp s))
  11876. (* 1000 (- org-lowest-priority org-default-priority))
  11877. (* 1000 (- org-lowest-priority
  11878. (string-to-char (match-string 2 s))))))))
  11879. ;;;; Tags
  11880. (defvar org-agenda-archives-mode)
  11881. (defvar org-map-continue-from nil
  11882. "Position from where mapping should continue.
  11883. Can be set by the action argument to `org-scan-tags' and `org-map-entries'.")
  11884. (defvar org-scanner-tags nil
  11885. "The current tag list while the tags scanner is running.")
  11886. (defvar org-trust-scanner-tags nil
  11887. "Should `org-get-tags-at' use the tags for the scanner.
  11888. This is for internal dynamical scoping only.
  11889. When this is non-nil, the function `org-get-tags-at' will return the value
  11890. of `org-scanner-tags' instead of building the list by itself. This
  11891. can lead to large speed-ups when the tags scanner is used in a file with
  11892. many entries, and when the list of tags is retrieved, for example to
  11893. obtain a list of properties. Building the tags list for each entry in such
  11894. a file becomes an N^2 operation - but with this variable set, it scales
  11895. as N.")
  11896. (defun org-scan-tags (action matcher todo-only &optional start-level)
  11897. "Scan headline tags with inheritance and produce output ACTION.
  11898. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  11899. or `agenda' to produce an entry list for an agenda view. It can also be
  11900. a Lisp form or a function that should be called at each matched headline, in
  11901. this case the return value is a list of all return values from these calls.
  11902. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  11903. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  11904. only lines with a not-done TODO keyword are included in the output.
  11905. This should be the same variable that was scoped into
  11906. and set by `org-make-tags-matcher' when it constructed MATCHER.
  11907. START-LEVEL can be a string with asterisks, reducing the scope to
  11908. headlines matching this string."
  11909. (require 'org-agenda)
  11910. (let* ((re (concat "^"
  11911. (if start-level
  11912. ;; Get the correct level to match
  11913. (concat "\\*\\{" (number-to-string start-level) "\\} ")
  11914. org-outline-regexp)
  11915. " *\\(\\<\\("
  11916. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  11917. (org-re "\\)\\>\\)? *\\(.*?\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*$")))
  11918. (props (list 'face 'default
  11919. 'done-face 'org-agenda-done
  11920. 'undone-face 'default
  11921. 'mouse-face 'highlight
  11922. 'org-not-done-regexp org-not-done-regexp
  11923. 'org-todo-regexp org-todo-regexp
  11924. 'org-complex-heading-regexp org-complex-heading-regexp
  11925. 'help-echo
  11926. (format "mouse-2 or RET jump to org file %s"
  11927. (abbreviate-file-name
  11928. (or (buffer-file-name (buffer-base-buffer))
  11929. (buffer-name (buffer-base-buffer)))))))
  11930. (case-fold-search nil)
  11931. (org-map-continue-from nil)
  11932. lspos tags tags-list
  11933. (tags-alist (list (cons 0 org-file-tags)))
  11934. (llast 0) rtn rtn1 level category i txt
  11935. todo marker entry priority)
  11936. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  11937. (setq action (list 'lambda nil action)))
  11938. (save-excursion
  11939. (goto-char (point-min))
  11940. (when (eq action 'sparse-tree)
  11941. (org-overview)
  11942. (org-remove-occur-highlights))
  11943. (while (re-search-forward re nil t)
  11944. (setq org-map-continue-from nil)
  11945. (catch :skip
  11946. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  11947. tags (if (match-end 4) (org-match-string-no-properties 4)))
  11948. (goto-char (setq lspos (match-beginning 0)))
  11949. (setq level (org-reduced-level (org-outline-level))
  11950. category (org-get-category))
  11951. (setq i llast llast level)
  11952. ;; remove tag lists from same and sublevels
  11953. (while (>= i level)
  11954. (when (setq entry (assoc i tags-alist))
  11955. (setq tags-alist (delete entry tags-alist)))
  11956. (setq i (1- i)))
  11957. ;; add the next tags
  11958. (when tags
  11959. (setq tags (org-split-string tags ":")
  11960. tags-alist
  11961. (cons (cons level tags) tags-alist)))
  11962. ;; compile tags for current headline
  11963. (setq tags-list
  11964. (if org-use-tag-inheritance
  11965. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  11966. tags)
  11967. org-scanner-tags tags-list)
  11968. (when org-use-tag-inheritance
  11969. (setcdr (car tags-alist)
  11970. (mapcar (lambda (x)
  11971. (setq x (copy-sequence x))
  11972. (org-add-prop-inherited x))
  11973. (cdar tags-alist))))
  11974. (when (and tags org-use-tag-inheritance
  11975. (or (not (eq t org-use-tag-inheritance))
  11976. org-tags-exclude-from-inheritance))
  11977. ;; selective inheritance, remove uninherited ones
  11978. (setcdr (car tags-alist)
  11979. (org-remove-uninherited-tags (cdar tags-alist))))
  11980. (when (and
  11981. ;; eval matcher only when the todo condition is OK
  11982. (and (or (not todo-only) (member todo org-not-done-keywords))
  11983. (let ((case-fold-search t) (org-trust-scanner-tags t))
  11984. (eval matcher)))
  11985. ;; Call the skipper, but return t if it does not skip,
  11986. ;; so that the `and' form continues evaluating
  11987. (progn
  11988. (unless (eq action 'sparse-tree) (org-agenda-skip))
  11989. t)
  11990. ;; Check if timestamps are deselecting this entry
  11991. (or (not todo-only)
  11992. (and (member todo org-not-done-keywords)
  11993. (or (not org-agenda-tags-todo-honor-ignore-options)
  11994. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))))
  11995. ;; Extra check for the archive tag
  11996. ;; FIXME: Does the skipper already do this????
  11997. (or
  11998. (not (member org-archive-tag tags-list))
  11999. ;; we have an archive tag, should we use this anyway?
  12000. (or (not org-agenda-skip-archived-trees)
  12001. (and (eq action 'agenda) org-agenda-archives-mode))))
  12002. ;; select this headline
  12003. (cond
  12004. ((eq action 'sparse-tree)
  12005. (and org-highlight-sparse-tree-matches
  12006. (org-get-heading) (match-end 0)
  12007. (org-highlight-new-match
  12008. (match-beginning 1) (match-end 1)))
  12009. (org-show-context 'tags-tree))
  12010. ((eq action 'agenda)
  12011. (setq txt (org-agenda-format-item
  12012. ""
  12013. (concat
  12014. (if (eq org-tags-match-list-sublevels 'indented)
  12015. (make-string (1- level) ?.) "")
  12016. (org-get-heading))
  12017. level category
  12018. tags-list)
  12019. priority (org-get-priority txt))
  12020. (goto-char lspos)
  12021. (setq marker (org-agenda-new-marker))
  12022. (org-add-props txt props
  12023. 'org-marker marker 'org-hd-marker marker 'org-category category
  12024. 'todo-state todo
  12025. 'priority priority 'type "tagsmatch")
  12026. (push txt rtn))
  12027. ((functionp action)
  12028. (setq org-map-continue-from nil)
  12029. (save-excursion
  12030. (setq rtn1 (funcall action))
  12031. (push rtn1 rtn)))
  12032. (t (error "Invalid action")))
  12033. ;; if we are to skip sublevels, jump to end of subtree
  12034. (unless org-tags-match-list-sublevels
  12035. (org-end-of-subtree t)
  12036. (backward-char 1))))
  12037. ;; Get the correct position from where to continue
  12038. (if org-map-continue-from
  12039. (goto-char org-map-continue-from)
  12040. (and (= (point) lspos) (end-of-line 1)))))
  12041. (when (and (eq action 'sparse-tree)
  12042. (not org-sparse-tree-open-archived-trees))
  12043. (org-hide-archived-subtrees (point-min) (point-max)))
  12044. (nreverse rtn)))
  12045. (defun org-remove-uninherited-tags (tags)
  12046. "Remove all tags that are not inherited from the list TAGS."
  12047. (cond
  12048. ((eq org-use-tag-inheritance t)
  12049. (if org-tags-exclude-from-inheritance
  12050. (org-delete-all org-tags-exclude-from-inheritance tags)
  12051. tags))
  12052. ((not org-use-tag-inheritance) nil)
  12053. ((stringp org-use-tag-inheritance)
  12054. (delq nil (mapcar
  12055. (lambda (x)
  12056. (if (and (string-match org-use-tag-inheritance x)
  12057. (not (member x org-tags-exclude-from-inheritance)))
  12058. x nil))
  12059. tags)))
  12060. ((listp org-use-tag-inheritance)
  12061. (delq nil (mapcar
  12062. (lambda (x)
  12063. (if (member x org-use-tag-inheritance) x nil))
  12064. tags)))))
  12065. (defun org-match-sparse-tree (&optional todo-only match)
  12066. "Create a sparse tree according to tags string MATCH.
  12067. MATCH can contain positive and negative selection of tags, like
  12068. \"+WORK+URGENT-WITHBOSS\".
  12069. If optional argument TODO-ONLY is non-nil, only select lines that are
  12070. also TODO lines."
  12071. (interactive "P")
  12072. (org-agenda-prepare-buffers (list (current-buffer)))
  12073. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  12074. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  12075. (defvar org-cached-props nil)
  12076. (defun org-cached-entry-get (pom property)
  12077. (if (or (eq t org-use-property-inheritance)
  12078. (and (stringp org-use-property-inheritance)
  12079. (string-match org-use-property-inheritance property))
  12080. (and (listp org-use-property-inheritance)
  12081. (member property org-use-property-inheritance)))
  12082. ;; Caching is not possible, check it directly
  12083. (org-entry-get pom property 'inherit)
  12084. ;; Get all properties, so that we can do complicated checks easily
  12085. (cdr (assoc property (or org-cached-props
  12086. (setq org-cached-props
  12087. (org-entry-properties pom)))))))
  12088. (defun org-global-tags-completion-table (&optional files)
  12089. "Return the list of all tags in all agenda buffer/files.
  12090. Optional FILES argument is a list of files which can be used
  12091. instead of the agenda files."
  12092. (save-excursion
  12093. (org-uniquify
  12094. (delq nil
  12095. (apply 'append
  12096. (mapcar
  12097. (lambda (file)
  12098. (set-buffer (find-file-noselect file))
  12099. (append (org-get-buffer-tags)
  12100. (mapcar (lambda (x) (if (stringp (car-safe x))
  12101. (list (car-safe x)) nil))
  12102. org-tag-alist)))
  12103. (if (and files (car files))
  12104. files
  12105. (org-agenda-files))))))))
  12106. (defun org-make-tags-matcher (match)
  12107. "Create the TAGS/TODO matcher form for the selection string MATCH.
  12108. The variable `todo-only' is scoped dynamically into this function.
  12109. It will be set to t if the matcher restricts matching to TODO entries,
  12110. otherwise will not be touched.
  12111. Returns a cons of the selection string MATCH and the constructed
  12112. lisp form implementing the matcher. The matcher is to be evaluated
  12113. at an Org entry, with point on the headline, and returns t if the
  12114. entry matches the selection string MATCH. The returned lisp form
  12115. references two variables with information about the entry, which
  12116. must be bound around the form's evaluation: todo, the TODO keyword
  12117. at the entry (or nil of none); and tags-list, the list of all tags
  12118. at the entry including inherited ones. Additionally, the category
  12119. of the entry (if any) must be specified as the text property
  12120. 'org-category on the headline.
  12121. See also `org-scan-tags'.
  12122. "
  12123. (declare (special todo-only))
  12124. (unless (boundp 'todo-only)
  12125. (error "org-make-tags-matcher expects todo-only to be scoped in"))
  12126. (unless match
  12127. ;; Get a new match request, with completion
  12128. (let ((org-last-tags-completion-table
  12129. (org-global-tags-completion-table)))
  12130. (setq match (org-completing-read-no-i
  12131. "Match: " 'org-tags-completion-function nil nil nil
  12132. 'org-tags-history))))
  12133. ;; Parse the string and create a lisp form
  12134. (let ((match0 match)
  12135. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\(\\(?:[[:alnum:]_]+\\(?:\\\\-\\)*\\)+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@#%]+\\)"))
  12136. minus tag mm
  12137. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  12138. orterms term orlist re-p str-p level-p level-op time-p
  12139. prop-p pn pv po gv rest)
  12140. (if (string-match "/+" match)
  12141. ;; match contains also a todo-matching request
  12142. (progn
  12143. (setq tagsmatch (substring match 0 (match-beginning 0))
  12144. todomatch (substring match (match-end 0)))
  12145. (if (string-match "^!" todomatch)
  12146. (setq todo-only t todomatch (substring todomatch 1)))
  12147. (if (string-match "^\\s-*$" todomatch)
  12148. (setq todomatch nil)))
  12149. ;; only matching tags
  12150. (setq tagsmatch match todomatch nil))
  12151. ;; Make the tags matcher
  12152. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  12153. (setq tagsmatcher t)
  12154. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  12155. (while (setq term (pop orterms))
  12156. (while (and (equal (substring term -1) "\\") orterms)
  12157. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  12158. (while (string-match re term)
  12159. (setq rest (substring term (match-end 0))
  12160. minus (and (match-end 1)
  12161. (equal (match-string 1 term) "-"))
  12162. tag (save-match-data (replace-regexp-in-string
  12163. "\\\\-" "-"
  12164. (match-string 2 term)))
  12165. re-p (equal (string-to-char tag) ?{)
  12166. level-p (match-end 4)
  12167. prop-p (match-end 5)
  12168. mm (cond
  12169. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  12170. (level-p
  12171. (setq level-op (org-op-to-function (match-string 3 term)))
  12172. `(,level-op level ,(string-to-number
  12173. (match-string 4 term))))
  12174. (prop-p
  12175. (setq pn (match-string 5 term)
  12176. po (match-string 6 term)
  12177. pv (match-string 7 term)
  12178. re-p (equal (string-to-char pv) ?{)
  12179. str-p (equal (string-to-char pv) ?\")
  12180. time-p (save-match-data
  12181. (string-match "^\"[[<].*[]>]\"$" pv))
  12182. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  12183. (if time-p (setq pv (org-matcher-time pv)))
  12184. (setq po (org-op-to-function po (if time-p 'time str-p)))
  12185. (cond
  12186. ((equal pn "CATEGORY")
  12187. (setq gv '(get-text-property (point) 'org-category)))
  12188. ((equal pn "TODO")
  12189. (setq gv 'todo))
  12190. (t
  12191. (setq gv `(org-cached-entry-get nil ,pn))))
  12192. (if re-p
  12193. (if (eq po 'org<>)
  12194. `(not (string-match ,pv (or ,gv "")))
  12195. `(string-match ,pv (or ,gv "")))
  12196. (if str-p
  12197. `(,po (or ,gv "") ,pv)
  12198. `(,po (string-to-number (or ,gv ""))
  12199. ,(string-to-number pv) ))))
  12200. (t `(member ,tag tags-list)))
  12201. mm (if minus (list 'not mm) mm)
  12202. term rest)
  12203. (push mm tagsmatcher))
  12204. (push (if (> (length tagsmatcher) 1)
  12205. (cons 'and tagsmatcher)
  12206. (car tagsmatcher))
  12207. orlist)
  12208. (setq tagsmatcher nil))
  12209. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  12210. (setq tagsmatcher
  12211. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  12212. ;; Make the todo matcher
  12213. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  12214. (setq todomatcher t)
  12215. (setq orterms (org-split-string todomatch "|") orlist nil)
  12216. (while (setq term (pop orterms))
  12217. (while (string-match re term)
  12218. (setq minus (and (match-end 1)
  12219. (equal (match-string 1 term) "-"))
  12220. kwd (match-string 2 term)
  12221. re-p (equal (string-to-char kwd) ?{)
  12222. term (substring term (match-end 0))
  12223. mm (if re-p
  12224. `(string-match ,(substring kwd 1 -1) todo)
  12225. (list 'equal 'todo kwd))
  12226. mm (if minus (list 'not mm) mm))
  12227. (push mm todomatcher))
  12228. (push (if (> (length todomatcher) 1)
  12229. (cons 'and todomatcher)
  12230. (car todomatcher))
  12231. orlist)
  12232. (setq todomatcher nil))
  12233. (setq todomatcher (if (> (length orlist) 1)
  12234. (cons 'or orlist) (car orlist))))
  12235. ;; Return the string and lisp forms of the matcher
  12236. (setq matcher (if todomatcher
  12237. (list 'and tagsmatcher todomatcher)
  12238. tagsmatcher))
  12239. (when todo-only
  12240. (setq matcher (list 'and '(member todo org-not-done-keywords)
  12241. matcher)))
  12242. (cons match0 matcher)))
  12243. (defun org-op-to-function (op &optional stringp)
  12244. "Turn an operator into the appropriate function."
  12245. (setq op
  12246. (cond
  12247. ((equal op "<" ) '(< string< org-time<))
  12248. ((equal op ">" ) '(> org-string> org-time>))
  12249. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  12250. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  12251. ((member op '("=" "==")) '(= string= org-time=))
  12252. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  12253. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  12254. (defun org<> (a b) (not (= a b)))
  12255. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  12256. (defun org-string>= (a b) (not (string< a b)))
  12257. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  12258. (defun org-string<> (a b) (not (string= a b)))
  12259. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  12260. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  12261. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  12262. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  12263. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  12264. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  12265. (defun org-2ft (s)
  12266. "Convert S to a floating point time.
  12267. If S is already a number, just return it. If it is a string, parse
  12268. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  12269. (cond
  12270. ((numberp s) s)
  12271. ((stringp s)
  12272. (condition-case nil
  12273. (float-time (apply 'encode-time (org-parse-time-string s)))
  12274. (error 0.)))
  12275. (t 0.)))
  12276. (defun org-time-today ()
  12277. "Time in seconds today at 0:00.
  12278. Returns the float number of seconds since the beginning of the
  12279. epoch to the beginning of today (00:00)."
  12280. (float-time (apply 'encode-time
  12281. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  12282. (defun org-matcher-time (s)
  12283. "Interpret a time comparison value."
  12284. (save-match-data
  12285. (cond
  12286. ((string= s "<now>") (float-time))
  12287. ((string= s "<today>") (org-time-today))
  12288. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  12289. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  12290. ((string-match "^<\\([-+][0-9]+\\)\\([hdwmy]\\)>$" s)
  12291. (+ (org-time-today)
  12292. (* (string-to-number (match-string 1 s))
  12293. (cdr (assoc (match-string 2 s)
  12294. '(("d" . 86400.0) ("w" . 604800.0)
  12295. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  12296. (t (org-2ft s)))))
  12297. (defun org-match-any-p (re list)
  12298. "Does re match any element of list?"
  12299. (setq list (mapcar (lambda (x) (string-match re x)) list))
  12300. (delq nil list))
  12301. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  12302. (defvar org-tags-overlay (make-overlay 1 1))
  12303. (org-detach-overlay org-tags-overlay)
  12304. (defun org-get-local-tags-at (&optional pos)
  12305. "Get a list of tags defined in the current headline."
  12306. (org-get-tags-at pos 'local))
  12307. (defun org-get-local-tags ()
  12308. "Get a list of tags defined in the current headline."
  12309. (org-get-tags-at nil 'local))
  12310. (defun org-get-tags-at (&optional pos local)
  12311. "Get a list of all headline tags applicable at POS.
  12312. POS defaults to point. If tags are inherited, the list contains
  12313. the targets in the same sequence as the headlines appear, i.e.
  12314. the tags of the current headline come last.
  12315. When LOCAL is non-nil, only return tags from the current headline,
  12316. ignore inherited ones."
  12317. (interactive)
  12318. (if (and org-trust-scanner-tags
  12319. (or (not pos) (equal pos (point)))
  12320. (not local))
  12321. org-scanner-tags
  12322. (let (tags ltags lastpos parent)
  12323. (save-excursion
  12324. (save-restriction
  12325. (widen)
  12326. (goto-char (or pos (point)))
  12327. (save-match-data
  12328. (catch 'done
  12329. (condition-case nil
  12330. (progn
  12331. (org-back-to-heading t)
  12332. (while (not (equal lastpos (point)))
  12333. (setq lastpos (point))
  12334. (when (looking-at
  12335. (org-re "[^\r\n]+?:\\([[:alnum:]_@#%:]+\\):[ \t]*$"))
  12336. (setq ltags (org-split-string
  12337. (org-match-string-no-properties 1) ":"))
  12338. (when parent
  12339. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  12340. (setq tags (append
  12341. (if parent
  12342. (org-remove-uninherited-tags ltags)
  12343. ltags)
  12344. tags)))
  12345. (or org-use-tag-inheritance (throw 'done t))
  12346. (if local (throw 'done t))
  12347. (or (org-up-heading-safe) (error nil))
  12348. (setq parent t)))
  12349. (error nil)))))
  12350. (if local
  12351. tags
  12352. (append (org-remove-uninherited-tags org-file-tags) tags))))))
  12353. (defun org-add-prop-inherited (s)
  12354. (add-text-properties 0 (length s) '(inherited t) s)
  12355. s)
  12356. (defun org-toggle-tag (tag &optional onoff)
  12357. "Toggle the tag TAG for the current line.
  12358. If ONOFF is `on' or `off', don't toggle but set to this state."
  12359. (let (res current)
  12360. (save-excursion
  12361. (org-back-to-heading t)
  12362. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t]*$")
  12363. (point-at-eol) t)
  12364. (progn
  12365. (setq current (match-string 1))
  12366. (replace-match ""))
  12367. (setq current ""))
  12368. (setq current (nreverse (org-split-string current ":")))
  12369. (cond
  12370. ((eq onoff 'on)
  12371. (setq res t)
  12372. (or (member tag current) (push tag current)))
  12373. ((eq onoff 'off)
  12374. (or (not (member tag current)) (setq current (delete tag current))))
  12375. (t (if (member tag current)
  12376. (setq current (delete tag current))
  12377. (setq res t)
  12378. (push tag current))))
  12379. (end-of-line 1)
  12380. (if current
  12381. (progn
  12382. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  12383. (org-set-tags nil t))
  12384. (delete-horizontal-space))
  12385. (run-hooks 'org-after-tags-change-hook))
  12386. res))
  12387. (defun org-align-tags-here (to-col)
  12388. ;; Assumes that this is a headline
  12389. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  12390. (beginning-of-line 1)
  12391. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  12392. (< pos (match-beginning 2)))
  12393. (progn
  12394. (setq tags-l (- (match-end 2) (match-beginning 2)))
  12395. (goto-char (match-beginning 1))
  12396. (insert " ")
  12397. (delete-region (point) (1+ (match-beginning 2)))
  12398. (setq ncol (max (current-column)
  12399. (1+ col)
  12400. (if (> to-col 0)
  12401. to-col
  12402. (- (abs to-col) tags-l))))
  12403. (setq p (point))
  12404. (insert (make-string (- ncol (current-column)) ?\ ))
  12405. (setq ncol (current-column))
  12406. (when indent-tabs-mode (tabify p (point-at-eol)))
  12407. (org-move-to-column (min ncol col) t))
  12408. (goto-char pos))))
  12409. (defun org-set-tags-command (&optional arg just-align)
  12410. "Call the set-tags command for the current entry."
  12411. (interactive "P")
  12412. (if (or (org-at-heading-p) (and arg (org-before-first-heading-p)))
  12413. (org-set-tags arg just-align)
  12414. (save-excursion
  12415. (org-back-to-heading t)
  12416. (org-set-tags arg just-align))))
  12417. (defun org-set-tags-to (data)
  12418. "Set the tags of the current entry to DATA, replacing the current tags.
  12419. DATA may be a tags string like :aa:bb:cc:, or a list of tags.
  12420. If DATA is nil or the empty string, any tags will be removed."
  12421. (interactive "sTags: ")
  12422. (setq data
  12423. (cond
  12424. ((eq data nil) "")
  12425. ((equal data "") "")
  12426. ((stringp data)
  12427. (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
  12428. ":"))
  12429. ((listp data)
  12430. (concat ":" (mapconcat 'identity data ":") ":"))))
  12431. (when data
  12432. (save-excursion
  12433. (org-back-to-heading t)
  12434. (when (looking-at org-complex-heading-regexp)
  12435. (if (match-end 5)
  12436. (progn
  12437. (goto-char (match-beginning 5))
  12438. (insert data)
  12439. (delete-region (point) (point-at-eol))
  12440. (org-set-tags nil 'align))
  12441. (goto-char (point-at-eol))
  12442. (insert " " data)
  12443. (org-set-tags nil 'align)))
  12444. (beginning-of-line 1)
  12445. (if (looking-at ".*?\\([ \t]+\\)$")
  12446. (delete-region (match-beginning 1) (match-end 1))))))
  12447. (defun org-align-all-tags ()
  12448. "Align the tags i all headings."
  12449. (interactive)
  12450. (save-excursion
  12451. (or (ignore-errors (org-back-to-heading t))
  12452. (outline-next-heading))
  12453. (if (org-at-heading-p)
  12454. (org-set-tags t)
  12455. (message "No headings"))))
  12456. (defvar org-indent-indentation-per-level)
  12457. (defun org-set-tags (&optional arg just-align)
  12458. "Set the tags for the current headline.
  12459. With prefix ARG, realign all tags in headings in the current buffer."
  12460. (interactive "P")
  12461. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  12462. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  12463. 'region-start-level 'region))
  12464. org-loop-over-headlines-in-active-region)
  12465. (org-map-entries
  12466. ;; We don't use ARG and JUST-ALIGN here these args are not
  12467. ;; useful when looping over headlines
  12468. `(org-set-tags)
  12469. org-loop-over-headlines-in-active-region
  12470. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  12471. (let* ((re org-outline-regexp-bol)
  12472. (current (unless arg (org-get-tags-string)))
  12473. (col (current-column))
  12474. (org-setting-tags t)
  12475. table current-tags inherited-tags ; computed below when needed
  12476. tags p0 c0 c1 rpl di tc level)
  12477. (if arg
  12478. (save-excursion
  12479. (goto-char (point-min))
  12480. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  12481. (while (re-search-forward re nil t)
  12482. (org-set-tags nil t)
  12483. (end-of-line 1)))
  12484. (message "All tags realigned to column %d" org-tags-column))
  12485. (if just-align
  12486. (setq tags current)
  12487. ;; Get a new set of tags from the user
  12488. (save-excursion
  12489. (setq table (append org-tag-persistent-alist
  12490. (or org-tag-alist (org-get-buffer-tags))
  12491. (and
  12492. org-complete-tags-always-offer-all-agenda-tags
  12493. (org-global-tags-completion-table
  12494. (org-agenda-files))))
  12495. org-last-tags-completion-table table
  12496. current-tags (org-split-string current ":")
  12497. inherited-tags (nreverse
  12498. (nthcdr (length current-tags)
  12499. (nreverse (org-get-tags-at))))
  12500. tags
  12501. (if (or (eq t org-use-fast-tag-selection)
  12502. (and org-use-fast-tag-selection
  12503. (delq nil (mapcar 'cdr table))))
  12504. (org-fast-tag-selection
  12505. current-tags inherited-tags table
  12506. (if org-fast-tag-selection-include-todo
  12507. org-todo-key-alist))
  12508. (let ((org-add-colon-after-tag-completion (< 1 (length table))))
  12509. (org-trim
  12510. (org-icompleting-read "Tags: "
  12511. 'org-tags-completion-function
  12512. nil nil current 'org-tags-history))))))
  12513. (while (string-match "[-+&]+" tags)
  12514. ;; No boolean logic, just a list
  12515. (setq tags (replace-match ":" t t tags))))
  12516. (setq tags (replace-regexp-in-string "[,]" ":" tags))
  12517. (if org-tags-sort-function
  12518. (setq tags (mapconcat 'identity
  12519. (sort (org-split-string
  12520. tags (org-re "[^[:alnum:]_@#%]+"))
  12521. org-tags-sort-function) ":")))
  12522. (if (string-match "\\`[\t ]*\\'" tags)
  12523. (setq tags "")
  12524. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  12525. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  12526. ;; Insert new tags at the correct column
  12527. (beginning-of-line 1)
  12528. (setq level (or (and (looking-at org-outline-regexp)
  12529. (- (match-end 0) (point) 1))
  12530. 1))
  12531. (cond
  12532. ((and (equal current "") (equal tags "")))
  12533. ((re-search-forward
  12534. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  12535. (point-at-eol) t)
  12536. (if (equal tags "")
  12537. (setq rpl "")
  12538. (goto-char (match-beginning 0))
  12539. (setq c0 (current-column)
  12540. ;; compute offset for the case of org-indent-mode active
  12541. di (if org-indent-mode
  12542. (* (1- org-indent-indentation-per-level) (1- level))
  12543. 0)
  12544. p0 (if (equal (char-before) ?*) (1+ (point)) (point))
  12545. tc (+ org-tags-column (if (> org-tags-column 0) (- di) di))
  12546. c1 (max (1+ c0) (if (> tc 0) tc (- (- tc) (length tags))))
  12547. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  12548. (replace-match rpl t t)
  12549. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  12550. tags)
  12551. (t (error "Tags alignment failed")))
  12552. (org-move-to-column col)
  12553. (unless just-align
  12554. (run-hooks 'org-after-tags-change-hook))))))
  12555. (defun org-change-tag-in-region (beg end tag off)
  12556. "Add or remove TAG for each entry in the region.
  12557. This works in the agenda, and also in an org-mode buffer."
  12558. (interactive
  12559. (list (region-beginning) (region-end)
  12560. (let ((org-last-tags-completion-table
  12561. (if (derived-mode-p 'org-mode)
  12562. (org-get-buffer-tags)
  12563. (org-global-tags-completion-table))))
  12564. (org-icompleting-read
  12565. "Tag: " 'org-tags-completion-function nil nil nil
  12566. 'org-tags-history))
  12567. (progn
  12568. (message "[s]et or [r]emove? ")
  12569. (equal (read-char-exclusive) ?r))))
  12570. (if (fboundp 'deactivate-mark) (deactivate-mark))
  12571. (let ((agendap (equal major-mode 'org-agenda-mode))
  12572. l1 l2 m buf pos newhead (cnt 0))
  12573. (goto-char end)
  12574. (setq l2 (1- (org-current-line)))
  12575. (goto-char beg)
  12576. (setq l1 (org-current-line))
  12577. (loop for l from l1 to l2 do
  12578. (org-goto-line l)
  12579. (setq m (get-text-property (point) 'org-hd-marker))
  12580. (when (or (and (derived-mode-p 'org-mode) (org-at-heading-p))
  12581. (and agendap m))
  12582. (setq buf (if agendap (marker-buffer m) (current-buffer))
  12583. pos (if agendap m (point)))
  12584. (with-current-buffer buf
  12585. (save-excursion
  12586. (save-restriction
  12587. (goto-char pos)
  12588. (setq cnt (1+ cnt))
  12589. (org-toggle-tag tag (if off 'off 'on))
  12590. (setq newhead (org-get-heading)))))
  12591. (and agendap (org-agenda-change-all-lines newhead m))))
  12592. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  12593. (defun org-tags-completion-function (string predicate &optional flag)
  12594. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  12595. (confirm (lambda (x) (stringp (car x)))))
  12596. (if (string-match "^\\(.*[-+:&,|]\\)\\([^-+:&,|]*\\)$" string)
  12597. (setq s1 (match-string 1 string)
  12598. s2 (match-string 2 string))
  12599. (setq s1 "" s2 string))
  12600. (cond
  12601. ((eq flag nil)
  12602. ;; try completion
  12603. (setq rtn (try-completion s2 ctable confirm))
  12604. (if (stringp rtn)
  12605. (setq rtn
  12606. (concat s1 s2 (substring rtn (length s2))
  12607. (if (and org-add-colon-after-tag-completion
  12608. (assoc rtn ctable))
  12609. ":" ""))))
  12610. rtn)
  12611. ((eq flag t)
  12612. ;; all-completions
  12613. (all-completions s2 ctable confirm)
  12614. )
  12615. ((eq flag 'lambda)
  12616. ;; exact match?
  12617. (assoc s2 ctable)))
  12618. ))
  12619. (defun org-fast-tag-insert (kwd tags face &optional end)
  12620. "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
  12621. (insert (format "%-12s" (concat kwd ":"))
  12622. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  12623. (or end "")))
  12624. (defun org-fast-tag-show-exit (flag)
  12625. (save-excursion
  12626. (org-goto-line 3)
  12627. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  12628. (replace-match ""))
  12629. (when flag
  12630. (end-of-line 1)
  12631. (org-move-to-column (- (window-width) 19) t)
  12632. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  12633. (defun org-set-current-tags-overlay (current prefix)
  12634. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  12635. (if (featurep 'xemacs)
  12636. (org-overlay-display org-tags-overlay (concat prefix s)
  12637. 'secondary-selection)
  12638. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  12639. (org-overlay-display org-tags-overlay (concat prefix s)))))
  12640. (defvar org-last-tag-selection-key nil)
  12641. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  12642. "Fast tag selection with single keys.
  12643. CURRENT is the current list of tags in the headline, INHERITED is the
  12644. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  12645. possibly with grouping information. TODO-TABLE is a similar table with
  12646. TODO keywords, should these have keys assigned to them.
  12647. If the keys are nil, a-z are automatically assigned.
  12648. Returns the new tags string, or nil to not change the current settings."
  12649. (let* ((fulltable (append table todo-table))
  12650. (maxlen (apply 'max (mapcar
  12651. (lambda (x)
  12652. (if (stringp (car x)) (string-width (car x)) 0))
  12653. fulltable)))
  12654. (buf (current-buffer))
  12655. (expert (eq org-fast-tag-selection-single-key 'expert))
  12656. (buffer-tags nil)
  12657. (fwidth (+ maxlen 3 1 3))
  12658. (ncol (/ (- (window-width) 4) fwidth))
  12659. (i-face 'org-done)
  12660. (c-face 'org-todo)
  12661. tg cnt e c char c1 c2 ntable tbl rtn
  12662. ov-start ov-end ov-prefix
  12663. (exit-after-next org-fast-tag-selection-single-key)
  12664. (done-keywords org-done-keywords)
  12665. groups ingroup)
  12666. (save-excursion
  12667. (beginning-of-line 1)
  12668. (if (looking-at
  12669. (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  12670. (setq ov-start (match-beginning 1)
  12671. ov-end (match-end 1)
  12672. ov-prefix "")
  12673. (setq ov-start (1- (point-at-eol))
  12674. ov-end (1+ ov-start))
  12675. (skip-chars-forward "^\n\r")
  12676. (setq ov-prefix
  12677. (concat
  12678. (buffer-substring (1- (point)) (point))
  12679. (if (> (current-column) org-tags-column)
  12680. " "
  12681. (make-string (- org-tags-column (current-column)) ?\ ))))))
  12682. (move-overlay org-tags-overlay ov-start ov-end)
  12683. (save-window-excursion
  12684. (if expert
  12685. (set-buffer (get-buffer-create " *Org tags*"))
  12686. (delete-other-windows)
  12687. (split-window-vertically)
  12688. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  12689. (erase-buffer)
  12690. (org-set-local 'org-done-keywords done-keywords)
  12691. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  12692. (org-fast-tag-insert "Current" current c-face "\n\n")
  12693. (org-fast-tag-show-exit exit-after-next)
  12694. (org-set-current-tags-overlay current ov-prefix)
  12695. (setq tbl fulltable char ?a cnt 0)
  12696. (while (setq e (pop tbl))
  12697. (cond
  12698. ((equal (car e) :startgroup)
  12699. (push '() groups) (setq ingroup t)
  12700. (when (not (= cnt 0))
  12701. (setq cnt 0)
  12702. (insert "\n"))
  12703. (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
  12704. ((equal (car e) :endgroup)
  12705. (setq ingroup nil cnt 0)
  12706. (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
  12707. ((equal e '(:newline))
  12708. (when (not (= cnt 0))
  12709. (setq cnt 0)
  12710. (insert "\n")
  12711. (setq e (car tbl))
  12712. (while (equal (car tbl) '(:newline))
  12713. (insert "\n")
  12714. (setq tbl (cdr tbl)))))
  12715. (t
  12716. (setq tg (copy-sequence (car e)) c2 nil)
  12717. (if (cdr e)
  12718. (setq c (cdr e))
  12719. ;; automatically assign a character.
  12720. (setq c1 (string-to-char
  12721. (downcase (substring
  12722. tg (if (= (string-to-char tg) ?@) 1 0)))))
  12723. (if (or (rassoc c1 ntable) (rassoc c1 table))
  12724. (while (or (rassoc char ntable) (rassoc char table))
  12725. (setq char (1+ char)))
  12726. (setq c2 c1))
  12727. (setq c (or c2 char)))
  12728. (if ingroup (push tg (car groups)))
  12729. (setq tg (org-add-props tg nil 'face
  12730. (cond
  12731. ((not (assoc tg table))
  12732. (org-get-todo-face tg))
  12733. ((member tg current) c-face)
  12734. ((member tg inherited) i-face))))
  12735. (if (and (= cnt 0) (not ingroup)) (insert " "))
  12736. (insert "[" c "] " tg (make-string
  12737. (- fwidth 4 (length tg)) ?\ ))
  12738. (push (cons tg c) ntable)
  12739. (when (= (setq cnt (1+ cnt)) ncol)
  12740. (insert "\n")
  12741. (if ingroup (insert " "))
  12742. (setq cnt 0)))))
  12743. (setq ntable (nreverse ntable))
  12744. (insert "\n")
  12745. (goto-char (point-min))
  12746. (if (not expert) (org-fit-window-to-buffer))
  12747. (setq rtn
  12748. (catch 'exit
  12749. (while t
  12750. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
  12751. (if (not groups) "no " "")
  12752. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  12753. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  12754. (setq org-last-tag-selection-key c)
  12755. (cond
  12756. ((= c ?\r) (throw 'exit t))
  12757. ((= c ?!)
  12758. (setq groups (not groups))
  12759. (goto-char (point-min))
  12760. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  12761. ((= c ?\C-c)
  12762. (if (not expert)
  12763. (org-fast-tag-show-exit
  12764. (setq exit-after-next (not exit-after-next)))
  12765. (setq expert nil)
  12766. (delete-other-windows)
  12767. (set-window-buffer (split-window-vertically) " *Org tags*")
  12768. (org-switch-to-buffer-other-window " *Org tags*")
  12769. (org-fit-window-to-buffer)))
  12770. ((or (= c ?\C-g)
  12771. (and (= c ?q) (not (rassoc c ntable))))
  12772. (org-detach-overlay org-tags-overlay)
  12773. (setq quit-flag t))
  12774. ((= c ?\ )
  12775. (setq current nil)
  12776. (if exit-after-next (setq exit-after-next 'now)))
  12777. ((= c ?\t)
  12778. (condition-case nil
  12779. (setq tg (org-icompleting-read
  12780. "Tag: "
  12781. (or buffer-tags
  12782. (with-current-buffer buf
  12783. (org-get-buffer-tags)))))
  12784. (quit (setq tg "")))
  12785. (when (string-match "\\S-" tg)
  12786. (add-to-list 'buffer-tags (list tg))
  12787. (if (member tg current)
  12788. (setq current (delete tg current))
  12789. (push tg current)))
  12790. (if exit-after-next (setq exit-after-next 'now)))
  12791. ((setq e (rassoc c todo-table) tg (car e))
  12792. (with-current-buffer buf
  12793. (save-excursion (org-todo tg)))
  12794. (if exit-after-next (setq exit-after-next 'now)))
  12795. ((setq e (rassoc c ntable) tg (car e))
  12796. (if (member tg current)
  12797. (setq current (delete tg current))
  12798. (loop for g in groups do
  12799. (if (member tg g)
  12800. (mapc (lambda (x)
  12801. (setq current (delete x current)))
  12802. g)))
  12803. (push tg current))
  12804. (if exit-after-next (setq exit-after-next 'now))))
  12805. ;; Create a sorted list
  12806. (setq current
  12807. (sort current
  12808. (lambda (a b)
  12809. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  12810. (if (eq exit-after-next 'now) (throw 'exit t))
  12811. (goto-char (point-min))
  12812. (beginning-of-line 2)
  12813. (delete-region (point) (point-at-eol))
  12814. (org-fast-tag-insert "Current" current c-face)
  12815. (org-set-current-tags-overlay current ov-prefix)
  12816. (while (re-search-forward
  12817. (org-re "\\[.\\] \\([[:alnum:]_@#%]+\\)") nil t)
  12818. (setq tg (match-string 1))
  12819. (add-text-properties
  12820. (match-beginning 1) (match-end 1)
  12821. (list 'face
  12822. (cond
  12823. ((member tg current) c-face)
  12824. ((member tg inherited) i-face)
  12825. (t (get-text-property (match-beginning 1) 'face))))))
  12826. (goto-char (point-min)))))
  12827. (org-detach-overlay org-tags-overlay)
  12828. (if rtn
  12829. (mapconcat 'identity current ":")
  12830. nil))))
  12831. (defun org-get-tags-string ()
  12832. "Get the TAGS string in the current headline."
  12833. (unless (org-at-heading-p t)
  12834. (error "Not on a heading"))
  12835. (save-excursion
  12836. (beginning-of-line 1)
  12837. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  12838. (org-match-string-no-properties 1)
  12839. "")))
  12840. (defun org-get-tags ()
  12841. "Get the list of tags specified in the current headline."
  12842. (org-split-string (org-get-tags-string) ":"))
  12843. (defun org-get-buffer-tags ()
  12844. "Get a table of all tags used in the buffer, for completion."
  12845. (let (tags)
  12846. (save-excursion
  12847. (goto-char (point-min))
  12848. (while (re-search-forward
  12849. (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t\r\n]") nil t)
  12850. (when (equal (char-after (point-at-bol 0)) ?*)
  12851. (mapc (lambda (x) (add-to-list 'tags x))
  12852. (org-split-string (org-match-string-no-properties 1) ":")))))
  12853. (mapc (lambda (s) (add-to-list 'tags s)) org-file-tags)
  12854. (mapcar 'list tags)))
  12855. ;;;; The mapping API
  12856. (defun org-map-entries (func &optional match scope &rest skip)
  12857. "Call FUNC at each headline selected by MATCH in SCOPE.
  12858. FUNC is a function or a lisp form. The function will be called without
  12859. arguments, with the cursor positioned at the beginning of the headline.
  12860. The return values of all calls to the function will be collected and
  12861. returned as a list.
  12862. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  12863. does not need to preserve point. After evaluation, the cursor will be
  12864. moved to the end of the line (presumably of the headline of the
  12865. processed entry) and search continues from there. Under some
  12866. circumstances, this may not produce the wanted results. For example,
  12867. if you have removed (e.g. archived) the current (sub)tree it could
  12868. mean that the next entry will be skipped entirely. In such cases, you
  12869. can specify the position from where search should continue by making
  12870. FUNC set the variable `org-map-continue-from' to the desired buffer
  12871. position.
  12872. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  12873. Only headlines that are matched by this query will be considered during
  12874. the iteration. When MATCH is nil or t, all headlines will be
  12875. visited by the iteration.
  12876. SCOPE determines the scope of this command. It can be any of:
  12877. nil The current buffer, respecting the restriction if any
  12878. tree The subtree started with the entry at point
  12879. region The entries within the active region, if any
  12880. region-start-level
  12881. The entries within the active region, but only those at
  12882. the same level than the first one.
  12883. file The current buffer, without restriction
  12884. file-with-archives
  12885. The current buffer, and any archives associated with it
  12886. agenda All agenda files
  12887. agenda-with-archives
  12888. All agenda files with any archive files associated with them
  12889. \(file1 file2 ...)
  12890. If this is a list, all files in the list will be scanned
  12891. The remaining args are treated as settings for the skipping facilities of
  12892. the scanner. The following items can be given here:
  12893. archive skip trees with the archive tag.
  12894. comment skip trees with the COMMENT keyword
  12895. function or Emacs Lisp form:
  12896. will be used as value for `org-agenda-skip-function', so whenever
  12897. the function returns t, FUNC will not be called for that
  12898. entry and search will continue from the point where the
  12899. function leaves it.
  12900. If your function needs to retrieve the tags including inherited tags
  12901. at the *current* entry, you can use the value of the variable
  12902. `org-scanner-tags' which will be much faster than getting the value
  12903. with `org-get-tags-at'. If your function gets properties with
  12904. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  12905. to t around the call to `org-entry-properties' to get the same speedup.
  12906. Note that if your function moves around to retrieve tags and properties at
  12907. a *different* entry, you cannot use these techniques."
  12908. (unless (and (or (eq scope 'region) (eq scope 'region-start-level))
  12909. (not (org-region-active-p)))
  12910. (let* ((org-agenda-archives-mode nil) ; just to make sure
  12911. (org-agenda-skip-archived-trees (memq 'archive skip))
  12912. (org-agenda-skip-comment-trees (memq 'comment skip))
  12913. (org-agenda-skip-function
  12914. (car (org-delete-all '(comment archive) skip)))
  12915. (org-tags-match-list-sublevels t)
  12916. (start-level (eq scope 'region-start-level))
  12917. matcher file res
  12918. org-todo-keywords-for-agenda
  12919. org-done-keywords-for-agenda
  12920. org-todo-keyword-alist-for-agenda
  12921. org-drawers-for-agenda
  12922. org-tag-alist-for-agenda
  12923. todo-only)
  12924. (cond
  12925. ((eq match t) (setq matcher t))
  12926. ((eq match nil) (setq matcher t))
  12927. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  12928. (save-excursion
  12929. (save-restriction
  12930. (cond ((eq scope 'tree)
  12931. (org-back-to-heading t)
  12932. (org-narrow-to-subtree)
  12933. (setq scope nil))
  12934. ((and (or (eq scope 'region) (eq scope 'region-start-level))
  12935. (org-region-active-p))
  12936. ;; If needed, set start-level to a string like "2"
  12937. (when start-level
  12938. (save-excursion
  12939. (goto-char (region-beginning))
  12940. (unless (org-at-heading-p) (outline-next-heading))
  12941. (setq start-level (org-current-level))))
  12942. (narrow-to-region (region-beginning)
  12943. (save-excursion
  12944. (goto-char (region-end))
  12945. (unless (and (bolp) (org-at-heading-p))
  12946. (outline-next-heading))
  12947. (point)))
  12948. (setq scope nil)))
  12949. (if (not scope)
  12950. (progn
  12951. (org-agenda-prepare-buffers
  12952. (list (buffer-file-name (current-buffer))))
  12953. (setq res (org-scan-tags func matcher todo-only start-level)))
  12954. ;; Get the right scope
  12955. (cond
  12956. ((and scope (listp scope) (symbolp (car scope)))
  12957. (setq scope (eval scope)))
  12958. ((eq scope 'agenda)
  12959. (setq scope (org-agenda-files t)))
  12960. ((eq scope 'agenda-with-archives)
  12961. (setq scope (org-agenda-files t))
  12962. (setq scope (org-add-archive-files scope)))
  12963. ((eq scope 'file)
  12964. (setq scope (list (buffer-file-name))))
  12965. ((eq scope 'file-with-archives)
  12966. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  12967. (org-agenda-prepare-buffers scope)
  12968. (while (setq file (pop scope))
  12969. (with-current-buffer (org-find-base-buffer-visiting file)
  12970. (save-excursion
  12971. (save-restriction
  12972. (widen)
  12973. (goto-char (point-min))
  12974. (setq res (append res (org-scan-tags func matcher todo-only))))))))))
  12975. res)))
  12976. ;;;; Properties
  12977. ;;; Setting and retrieving properties
  12978. (defconst org-special-properties
  12979. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  12980. "TIMESTAMP" "TIMESTAMP_IA" "BLOCKED" "FILE" "CLOCKSUM" "CLOCKSUM_T")
  12981. "The special properties valid in Org-mode.
  12982. These are properties that are not defined in the property drawer,
  12983. but in some other way.")
  12984. (defconst org-default-properties
  12985. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  12986. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  12987. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  12988. "EXPORT_OPTIONS" "EXPORT_TEXT" "EXPORT_FILE_NAME"
  12989. "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
  12990. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER" "REPEAT_TO_STATE"
  12991. "CLOCK_MODELINE_TOTAL" "STYLE" "HTML_CONTAINER_CLASS")
  12992. "Some properties that are used by Org-mode for various purposes.
  12993. Being in this list makes sure that they are offered for completion.")
  12994. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  12995. "Regular expression matching the first line of a property drawer.")
  12996. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  12997. "Regular expression matching the last line of a property drawer.")
  12998. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  12999. "Regular expression matching the first line of a property drawer.")
  13000. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  13001. "Regular expression matching the first line of a property drawer.")
  13002. (defconst org-property-drawer-re
  13003. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  13004. org-property-end-re "\\)\n?")
  13005. "Matches an entire property drawer.")
  13006. (defconst org-clock-drawer-re
  13007. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  13008. org-property-end-re "\\)\n?")
  13009. "Matches an entire clock drawer.")
  13010. (defsubst org-re-property (property)
  13011. "Return a regexp matching a PROPERTY line.
  13012. Match group 1 will be set to the value."
  13013. (concat "^[ \t]*:" (regexp-quote property) ":[ \t]*\\(\\S-.*\\)"))
  13014. (defsubst org-re-property-keyword (property)
  13015. "Return a regexp matching a PROPERTY line, possibly with no
  13016. value for the property."
  13017. (concat "^[ \t]*:" (regexp-quote property) ":[ \t]*\\(\\S-.*\\)?"))
  13018. (defun org-property-action ()
  13019. "Do an action on properties."
  13020. (interactive)
  13021. (let (c)
  13022. (org-at-property-p)
  13023. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  13024. (setq c (read-char-exclusive))
  13025. (cond
  13026. ((equal c ?s)
  13027. (call-interactively 'org-set-property))
  13028. ((equal c ?d)
  13029. (call-interactively 'org-delete-property))
  13030. ((equal c ?D)
  13031. (call-interactively 'org-delete-property-globally))
  13032. ((equal c ?c)
  13033. (call-interactively 'org-compute-property-at-point))
  13034. (t (error "No such property action %c" c)))))
  13035. (defun org-inc-effort ()
  13036. "Increment the value of the effort property in the current entry."
  13037. (interactive)
  13038. (org-set-effort nil t))
  13039. (defun org-set-effort (&optional value increment)
  13040. "Set the effort property of the current entry.
  13041. With numerical prefix arg, use the nth allowed value, 0 stands for the
  13042. 10th allowed value.
  13043. When INCREMENT is non-nil, set the property to the next allowed value."
  13044. (interactive "P")
  13045. (if (equal value 0) (setq value 10))
  13046. (let* ((completion-ignore-case t)
  13047. (prop org-effort-property)
  13048. (cur (org-entry-get nil prop))
  13049. (allowed (org-property-get-allowed-values nil prop 'table))
  13050. (existing (mapcar 'list (org-property-values prop)))
  13051. rpl
  13052. (val (cond
  13053. ((stringp value) value)
  13054. ((and allowed (integerp value))
  13055. (or (car (nth (1- value) allowed))
  13056. (car (org-last allowed))))
  13057. ((and allowed increment)
  13058. (or (caadr (member (list cur) allowed))
  13059. (error "Allowed effort values are not set")))
  13060. (allowed
  13061. (message "Select 1-9,0, [RET%s]: %s"
  13062. (if cur (concat "=" cur) "")
  13063. (mapconcat 'car allowed " "))
  13064. (setq rpl (read-char-exclusive))
  13065. (if (equal rpl ?\r)
  13066. cur
  13067. (setq rpl (- rpl ?0))
  13068. (if (equal rpl 0) (setq rpl 10))
  13069. (if (and (> rpl 0) (<= rpl (length allowed)))
  13070. (car (nth (1- rpl) allowed))
  13071. (org-completing-read "Effort: " allowed nil))))
  13072. (t
  13073. (let (org-completion-use-ido org-completion-use-iswitchb)
  13074. (org-completing-read
  13075. (concat "Effort " (if (and cur (string-match "\\S-" cur))
  13076. (concat "[" cur "]") "")
  13077. ": ")
  13078. existing nil nil "" nil cur))))))
  13079. (unless (equal (org-entry-get nil prop) val)
  13080. (org-entry-put nil prop val))
  13081. (message "%s is now %s" prop val)))
  13082. (defun org-at-property-p ()
  13083. "Is cursor inside a property drawer?"
  13084. (save-excursion
  13085. (beginning-of-line 1)
  13086. (when (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))
  13087. (save-match-data ;; Used by calling procedures
  13088. (let ((p (point))
  13089. (range (unless (org-before-first-heading-p)
  13090. (org-get-property-block))))
  13091. (and range (<= (car range) p) (< p (cdr range))))))))
  13092. (defun org-get-property-block (&optional beg end force)
  13093. "Return the (beg . end) range of the body of the property drawer.
  13094. BEG and END are the beginning and end of the current subtree, or of
  13095. the part before the first headline. If they are not given, they will
  13096. be found. If the drawer does not exist and FORCE is non-nil, create
  13097. the drawer."
  13098. (catch 'exit
  13099. (save-excursion
  13100. (let* ((beg (or beg (and (org-before-first-heading-p) (point-min))
  13101. (progn (org-back-to-heading t) (point))))
  13102. (end (or end (and (not (outline-next-heading)) (point-max))
  13103. (point))))
  13104. (goto-char beg)
  13105. (if (re-search-forward org-property-start-re end t)
  13106. (setq beg (1+ (match-end 0)))
  13107. (if force
  13108. (save-excursion
  13109. (org-insert-property-drawer)
  13110. (setq end (progn (outline-next-heading) (point))))
  13111. (throw 'exit nil))
  13112. (goto-char beg)
  13113. (if (re-search-forward org-property-start-re end t)
  13114. (setq beg (1+ (match-end 0)))))
  13115. (if (re-search-forward org-property-end-re end t)
  13116. (setq end (match-beginning 0))
  13117. (or force (throw 'exit nil))
  13118. (goto-char beg)
  13119. (setq end beg)
  13120. (org-indent-line)
  13121. (insert ":END:\n"))
  13122. (cons beg end)))))
  13123. (defun org-entry-properties (&optional pom which specific)
  13124. "Get all properties of the entry at point-or-marker POM.
  13125. This includes the TODO keyword, the tags, time strings for deadline,
  13126. scheduled, and clocking, and any additional properties defined in the
  13127. entry. The return value is an alist, keys may occur multiple times
  13128. if the property key was used several times.
  13129. POM may also be nil, in which case the current entry is used.
  13130. If WHICH is nil or `all', get all properties. If WHICH is
  13131. `special' or `standard', only get that subclass. If WHICH
  13132. is a string only get exactly this property. SPECIFIC can be a string, the
  13133. specific property we are interested in. Specifying it can speed
  13134. things up because then unnecessary parsing is avoided."
  13135. (setq which (or which 'all))
  13136. (org-with-point-at pom
  13137. (let ((clockstr (substring org-clock-string 0 -1))
  13138. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY" "BLOCKED"))
  13139. (case-fold-search nil)
  13140. beg end range props sum-props key key1 value string clocksum clocksumt)
  13141. (save-excursion
  13142. (when (condition-case nil
  13143. (and (derived-mode-p 'org-mode) (org-back-to-heading t))
  13144. (error nil))
  13145. (setq beg (point))
  13146. (setq sum-props (get-text-property (point) 'org-summaries))
  13147. (setq clocksum (get-text-property (point) :org-clock-minutes)
  13148. clocksumt (get-text-property (point) :org-clock-minutes-today))
  13149. (outline-next-heading)
  13150. (setq end (point))
  13151. (when (memq which '(all special))
  13152. ;; Get the special properties, like TODO and tags
  13153. (goto-char beg)
  13154. (when (and (or (not specific) (string= specific "TODO"))
  13155. (looking-at org-todo-line-regexp) (match-end 2))
  13156. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  13157. (when (and (or (not specific) (string= specific "PRIORITY"))
  13158. (looking-at org-priority-regexp))
  13159. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  13160. (when (or (not specific) (string= specific "FILE"))
  13161. (push (cons "FILE" buffer-file-name) props))
  13162. (when (and (or (not specific) (string= specific "TAGS"))
  13163. (setq value (org-get-tags-string))
  13164. (string-match "\\S-" value))
  13165. (push (cons "TAGS" value) props))
  13166. (when (and (or (not specific) (string= specific "ALLTAGS"))
  13167. (setq value (org-get-tags-at)))
  13168. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":")
  13169. ":"))
  13170. props))
  13171. (when (or (not specific) (string= specific "BLOCKED"))
  13172. (push (cons "BLOCKED" (if (org-entry-blocked-p) "t" "")) props))
  13173. (when (or (not specific)
  13174. (member specific
  13175. '("SCHEDULED" "DEADLINE" "CLOCK" "CLOSED"
  13176. "TIMESTAMP" "TIMESTAMP_IA")))
  13177. (catch 'match
  13178. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  13179. (setq key (if (match-end 1)
  13180. (substring (org-match-string-no-properties 1)
  13181. 0 -1))
  13182. string (if (equal key clockstr)
  13183. (org-trim
  13184. (buffer-substring-no-properties
  13185. (match-beginning 3) (goto-char
  13186. (point-at-eol))))
  13187. (substring (org-match-string-no-properties 3)
  13188. 1 -1)))
  13189. ;; Get the correct property name from the key. This is
  13190. ;; necessary if the user has configured time keywords.
  13191. (setq key1 (concat key ":"))
  13192. (cond
  13193. ((not key)
  13194. (setq key
  13195. (if (= (char-after (match-beginning 3)) ?\[)
  13196. "TIMESTAMP_IA" "TIMESTAMP")))
  13197. ((equal key1 org-scheduled-string) (setq key "SCHEDULED"))
  13198. ((equal key1 org-deadline-string) (setq key "DEADLINE"))
  13199. ((equal key1 org-closed-string) (setq key "CLOSED"))
  13200. ((equal key1 org-clock-string) (setq key "CLOCK")))
  13201. (if (and specific (equal key specific) (not (equal key "CLOCK")))
  13202. (progn
  13203. (push (cons key string) props)
  13204. ;; no need to search further if match is found
  13205. (throw 'match t))
  13206. (when (or (equal key "CLOCK") (not (assoc key props)))
  13207. (push (cons key string) props)))))))
  13208. (when (memq which '(all standard))
  13209. ;; Get the standard properties, like :PROP: ...
  13210. (setq range (org-get-property-block beg end))
  13211. (when range
  13212. (goto-char (car range))
  13213. (while (re-search-forward
  13214. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  13215. (cdr range) t)
  13216. (setq key (org-match-string-no-properties 1)
  13217. value (org-trim (or (org-match-string-no-properties 2) "")))
  13218. (unless (member key excluded)
  13219. (push (cons key (or value "")) props)))))
  13220. (if clocksum
  13221. (push (cons "CLOCKSUM"
  13222. (org-columns-number-to-string (/ (float clocksum) 60.)
  13223. 'add_times))
  13224. props))
  13225. (if clocksumt
  13226. (push (cons "CLOCKSUM_T"
  13227. (org-columns-number-to-string (/ (float clocksumt) 60.)
  13228. 'add_times))
  13229. props))
  13230. (unless (assoc "CATEGORY" props)
  13231. (push (cons "CATEGORY" (org-get-category)) props))
  13232. (append sum-props (nreverse props)))))))
  13233. (defun org-entry-get (pom property &optional inherit literal-nil)
  13234. "Get value of PROPERTY for entry or content at point-or-marker POM.
  13235. If INHERIT is non-nil and the entry does not have the property,
  13236. then also check higher levels of the hierarchy.
  13237. If INHERIT is the symbol `selective', use inheritance only if the setting
  13238. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  13239. If the property is present but empty, the return value is the empty string.
  13240. If the property is not present at all, nil is returned.
  13241. If LITERAL-NIL is set, return the string value \"nil\" as a string,
  13242. do not interpret it as the list atom nil. This is used for inheritance
  13243. when a \"nil\" value can supersede a non-nil value higher up the hierarchy."
  13244. (org-with-point-at pom
  13245. (if (and inherit (if (eq inherit 'selective)
  13246. (org-property-inherit-p property)
  13247. t))
  13248. (org-entry-get-with-inheritance property literal-nil)
  13249. (if (member property org-special-properties)
  13250. ;; We need a special property. Use `org-entry-properties' to
  13251. ;; retrieve it, but specify the wanted property
  13252. (cdr (assoc property (org-entry-properties nil 'special property)))
  13253. (let* ((range (org-get-property-block))
  13254. (props (list (or (assoc property org-file-properties)
  13255. (assoc property org-global-properties)
  13256. (assoc property org-global-properties-fixed))))
  13257. (ap (lambda (key)
  13258. (when (re-search-forward
  13259. (org-re-property key) (cdr range) t)
  13260. (setq props
  13261. (org-update-property-plist
  13262. key
  13263. (if (match-end 1)
  13264. (org-match-string-no-properties 1) "")
  13265. props)))))
  13266. val)
  13267. (when (and range (goto-char (car range)))
  13268. (funcall ap property)
  13269. (goto-char (car range))
  13270. (while (funcall ap (concat property "+")))
  13271. (setq val (cdr (assoc property props)))
  13272. (when val (if literal-nil val (org-not-nil val)))))))))
  13273. (defun org-property-or-variable-value (var &optional inherit)
  13274. "Check if there is a property fixing the value of VAR.
  13275. If yes, return this value. If not, return the current value of the variable."
  13276. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  13277. (if (and prop (stringp prop) (string-match "\\S-" prop))
  13278. (read prop)
  13279. (symbol-value var))))
  13280. (defun org-entry-delete (pom property)
  13281. "Delete the property PROPERTY from entry at point-or-marker POM."
  13282. (org-with-point-at pom
  13283. (if (member property org-special-properties)
  13284. nil ; cannot delete these properties.
  13285. (let ((range (org-get-property-block)))
  13286. (if (and range
  13287. (goto-char (car range))
  13288. (re-search-forward
  13289. (org-re-property property)
  13290. (cdr range) t))
  13291. (progn
  13292. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  13293. t)
  13294. nil)))))
  13295. ;; Multi-values properties are properties that contain multiple values
  13296. ;; These values are assumed to be single words, separated by whitespace.
  13297. (defun org-entry-add-to-multivalued-property (pom property value)
  13298. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  13299. (let* ((old (org-entry-get pom property))
  13300. (values (and old (org-split-string old "[ \t]"))))
  13301. (setq value (org-entry-protect-space value))
  13302. (unless (member value values)
  13303. (setq values (cons value values))
  13304. (org-entry-put pom property
  13305. (mapconcat 'identity values " ")))))
  13306. (defun org-entry-remove-from-multivalued-property (pom property value)
  13307. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  13308. (let* ((old (org-entry-get pom property))
  13309. (values (and old (org-split-string old "[ \t]"))))
  13310. (setq value (org-entry-protect-space value))
  13311. (when (member value values)
  13312. (setq values (delete value values))
  13313. (org-entry-put pom property
  13314. (mapconcat 'identity values " ")))))
  13315. (defun org-entry-member-in-multivalued-property (pom property value)
  13316. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  13317. (let* ((old (org-entry-get pom property))
  13318. (values (and old (org-split-string old "[ \t]"))))
  13319. (setq value (org-entry-protect-space value))
  13320. (member value values)))
  13321. (defun org-entry-get-multivalued-property (pom property)
  13322. "Return a list of values in a multivalued property."
  13323. (let* ((value (org-entry-get pom property))
  13324. (values (and value (org-split-string value "[ \t]"))))
  13325. (mapcar 'org-entry-restore-space values)))
  13326. (defun org-entry-put-multivalued-property (pom property &rest values)
  13327. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  13328. VALUES should be a list of strings. Spaces will be protected."
  13329. (org-entry-put pom property
  13330. (mapconcat 'org-entry-protect-space values " "))
  13331. (let* ((value (org-entry-get pom property))
  13332. (values (and value (org-split-string value "[ \t]"))))
  13333. (mapcar 'org-entry-restore-space values)))
  13334. (defun org-entry-protect-space (s)
  13335. "Protect spaces and newline in string S."
  13336. (while (string-match " " s)
  13337. (setq s (replace-match "%20" t t s)))
  13338. (while (string-match "\n" s)
  13339. (setq s (replace-match "%0A" t t s)))
  13340. s)
  13341. (defun org-entry-restore-space (s)
  13342. "Restore spaces and newline in string S."
  13343. (while (string-match "%20" s)
  13344. (setq s (replace-match " " t t s)))
  13345. (while (string-match "%0A" s)
  13346. (setq s (replace-match "\n" t t s)))
  13347. s)
  13348. (defvar org-entry-property-inherited-from (make-marker)
  13349. "Marker pointing to the entry from where a property was inherited.
  13350. Each call to `org-entry-get-with-inheritance' will set this marker to the
  13351. location of the entry where the inheritance search matched. If there was
  13352. no match, the marker will point nowhere.
  13353. Note that also `org-entry-get' calls this function, if the INHERIT flag
  13354. is set.")
  13355. (defun org-entry-get-with-inheritance (property &optional literal-nil)
  13356. "Get PROPERTY of entry or content at point, search higher levels if needed.
  13357. The search will stop at the first ancestor which has the property defined.
  13358. If the value found is \"nil\", return nil to show that the property
  13359. should be considered as undefined (this is the meaning of nil here).
  13360. However, if LITERAL-NIL is set, return the string value \"nil\" instead."
  13361. (move-marker org-entry-property-inherited-from nil)
  13362. (let (tmp)
  13363. (save-excursion
  13364. (save-restriction
  13365. (widen)
  13366. (catch 'ex
  13367. (while t
  13368. (when (setq tmp (org-entry-get nil property nil 'literal-nil))
  13369. (or (ignore-errors (org-back-to-heading t))
  13370. (goto-char (point-min)))
  13371. (move-marker org-entry-property-inherited-from (point))
  13372. (throw 'ex tmp))
  13373. (or (ignore-errors (org-up-heading-safe))
  13374. (throw 'ex nil))))))
  13375. (setq tmp (or tmp
  13376. (cdr (assoc property org-file-properties))
  13377. (cdr (assoc property org-global-properties))
  13378. (cdr (assoc property org-global-properties-fixed))))
  13379. (if literal-nil tmp (org-not-nil tmp))))
  13380. (defvar org-property-changed-functions nil
  13381. "Hook called when the value of a property has changed.
  13382. Each hook function should accept two arguments, the name of the property
  13383. and the new value.")
  13384. (defun org-entry-put (pom property value)
  13385. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  13386. (org-with-point-at pom
  13387. (org-back-to-heading t)
  13388. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  13389. range)
  13390. (cond
  13391. ((equal property "TODO")
  13392. (when (and (stringp value) (string-match "\\S-" value)
  13393. (not (member value org-todo-keywords-1)))
  13394. (error "\"%s\" is not a valid TODO state" value))
  13395. (if (or (not value)
  13396. (not (string-match "\\S-" value)))
  13397. (setq value 'none))
  13398. (org-todo value)
  13399. (org-set-tags nil 'align))
  13400. ((equal property "PRIORITY")
  13401. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  13402. (string-to-char value) ?\ ))
  13403. (org-set-tags nil 'align))
  13404. ((equal property "CLOCKSUM")
  13405. (if (not (re-search-forward
  13406. (concat org-clock-string ".*\\]--\\(\\[[^]]+\\]\\)") nil t))
  13407. (error "Cannot find a clock log")
  13408. (goto-char (- (match-end 1) 2))
  13409. (cond
  13410. ((eq value 'earlier) (org-timestamp-down))
  13411. ((eq value 'later) (org-timestamp-up)))
  13412. (org-clock-sum-current-item)))
  13413. ((equal property "SCHEDULED")
  13414. (if (re-search-forward org-scheduled-time-regexp end t)
  13415. (cond
  13416. ((eq value 'earlier) (org-timestamp-change -1 'day))
  13417. ((eq value 'later) (org-timestamp-change 1 'day))
  13418. (t (call-interactively 'org-schedule)))
  13419. (call-interactively 'org-schedule)))
  13420. ((equal property "DEADLINE")
  13421. (if (re-search-forward org-deadline-time-regexp end t)
  13422. (cond
  13423. ((eq value 'earlier) (org-timestamp-change -1 'day))
  13424. ((eq value 'later) (org-timestamp-change 1 'day))
  13425. (t (call-interactively 'org-deadline)))
  13426. (call-interactively 'org-deadline)))
  13427. ((member property org-special-properties)
  13428. (error "The %s property can not yet be set with `org-entry-put'"
  13429. property))
  13430. (t ; a non-special property
  13431. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  13432. (setq range (org-get-property-block beg end 'force))
  13433. (goto-char (car range))
  13434. (if (re-search-forward
  13435. (org-re-property-keyword property) (cdr range) t)
  13436. (progn
  13437. (delete-region (match-beginning 0) (match-end 0))
  13438. (goto-char (match-beginning 0)))
  13439. (goto-char (cdr range))
  13440. (insert "\n")
  13441. (backward-char 1)
  13442. (org-indent-line))
  13443. (insert ":" property ":")
  13444. (and value (insert " " value))
  13445. (org-indent-line)))))
  13446. (run-hook-with-args 'org-property-changed-functions property value)))
  13447. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  13448. "Get all property keys in the current buffer.
  13449. With INCLUDE-SPECIALS, also list the special properties that reflect things
  13450. like tags and TODO state.
  13451. With INCLUDE-DEFAULTS, also include properties that has special meaning
  13452. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING
  13453. and others.
  13454. With INCLUDE-COLUMNS, also include property names given in COLUMN
  13455. formats in the current buffer."
  13456. (let (rtn range cfmt s p)
  13457. (save-excursion
  13458. (save-restriction
  13459. (widen)
  13460. (goto-char (point-min))
  13461. (while (re-search-forward org-property-start-re nil t)
  13462. (setq range (org-get-property-block))
  13463. (goto-char (car range))
  13464. (while (re-search-forward
  13465. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  13466. (cdr range) t)
  13467. (add-to-list 'rtn (org-match-string-no-properties 1)))
  13468. (outline-next-heading))))
  13469. (when include-specials
  13470. (setq rtn (append org-special-properties rtn)))
  13471. (when include-defaults
  13472. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties)
  13473. (add-to-list 'rtn org-effort-property))
  13474. (when include-columns
  13475. (save-excursion
  13476. (save-restriction
  13477. (widen)
  13478. (goto-char (point-min))
  13479. (while (re-search-forward
  13480. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  13481. nil t)
  13482. (setq cfmt (match-string 2) s 0)
  13483. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  13484. cfmt s)
  13485. (setq s (match-end 0)
  13486. p (match-string 1 cfmt))
  13487. (unless (or (equal p "ITEM")
  13488. (member p org-special-properties))
  13489. (add-to-list 'rtn (match-string 1 cfmt))))))))
  13490. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  13491. (defun org-property-values (key)
  13492. "Return a list of all values of property KEY in the current buffer."
  13493. (save-excursion
  13494. (save-restriction
  13495. (widen)
  13496. (goto-char (point-min))
  13497. (let ((re (org-re-property key))
  13498. values)
  13499. (while (re-search-forward re nil t)
  13500. (add-to-list 'values (org-trim (match-string 1))))
  13501. (delete "" values)))))
  13502. (defun org-insert-property-drawer ()
  13503. "Insert a property drawer into the current entry."
  13504. (org-back-to-heading t)
  13505. (looking-at org-outline-regexp)
  13506. (let ((indent (if org-adapt-indentation
  13507. (- (match-end 0) (match-beginning 0))
  13508. 0))
  13509. (beg (point))
  13510. (re (concat "^[ \t]*" org-keyword-time-regexp))
  13511. end hiddenp)
  13512. (outline-next-heading)
  13513. (setq end (point))
  13514. (goto-char beg)
  13515. (while (re-search-forward re end t))
  13516. (setq hiddenp (outline-invisible-p))
  13517. (end-of-line 1)
  13518. (and (equal (char-after) ?\n) (forward-char 1))
  13519. (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
  13520. (if (member (match-string 1) '("CLOCK:" ":END:"))
  13521. ;; just skip this line
  13522. (beginning-of-line 2)
  13523. ;; Drawer start, find the end
  13524. (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
  13525. (beginning-of-line 1)))
  13526. (org-skip-over-state-notes)
  13527. (skip-chars-backward " \t\n\r")
  13528. (if (eq (char-before) ?*) (forward-char 1))
  13529. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  13530. (beginning-of-line 0)
  13531. (org-indent-to-column indent)
  13532. (beginning-of-line 2)
  13533. (org-indent-to-column indent)
  13534. (beginning-of-line 0)
  13535. (if hiddenp
  13536. (save-excursion
  13537. (org-back-to-heading t)
  13538. (hide-entry))
  13539. (org-flag-drawer t))))
  13540. (defun org-insert-drawer (&optional arg drawer)
  13541. "Insert a drawer at point.
  13542. Optional argument DRAWER, when non-nil, is a string representing
  13543. drawer's name. Otherwise, the user is prompted for a name.
  13544. If a region is active, insert the drawer around that region
  13545. instead.
  13546. Point is left between drawer's boundaries."
  13547. (interactive "P")
  13548. (let* ((logbook (if (stringp org-log-into-drawer) org-log-into-drawer
  13549. "LOGBOOK"))
  13550. ;; SYSTEM-DRAWERS is a list of drawer names that are used
  13551. ;; internally by Org. They are meant to be inserted
  13552. ;; automatically.
  13553. (system-drawers `("CLOCK" ,logbook "PROPERTIES"))
  13554. ;; Remove system drawers from list. Note: For some reason,
  13555. ;; `org-completing-read' ignores the predicate while
  13556. ;; `completing-read' handles it fine.
  13557. (drawer (if arg "PROPERTIES"
  13558. (or drawer
  13559. (completing-read
  13560. "Drawer: " org-drawers
  13561. (lambda (d) (not (member d system-drawers))))))))
  13562. (cond
  13563. ;; With C-u, fall back on `org-insert-property-drawer'
  13564. (arg (org-insert-property-drawer))
  13565. ;; With an active region, insert a drawer at point.
  13566. ((not (org-region-active-p))
  13567. (progn
  13568. (unless (bolp) (insert "\n"))
  13569. (insert (format ":%s:\n\n:END:\n" drawer))
  13570. (forward-line -2)))
  13571. ;; Otherwise, insert the drawer at point
  13572. (t
  13573. (let ((rbeg (region-beginning))
  13574. (rend (copy-marker (region-end))))
  13575. (unwind-protect
  13576. (progn
  13577. (goto-char rbeg)
  13578. (beginning-of-line)
  13579. (when (save-excursion
  13580. (re-search-forward org-outline-regexp-bol rend t))
  13581. (error "Drawers cannot contain headlines"))
  13582. ;; Position point at the beginning of the first
  13583. ;; non-blank line in region. Insert drawer's opening
  13584. ;; there, then indent it.
  13585. (org-skip-whitespace)
  13586. (beginning-of-line)
  13587. (insert ":" drawer ":\n")
  13588. (forward-line -1)
  13589. (indent-for-tab-command)
  13590. ;; Move point to the beginning of the first blank line
  13591. ;; after the last non-blank line in region. Insert
  13592. ;; drawer's closing, then indent it.
  13593. (goto-char rend)
  13594. (skip-chars-backward " \r\t\n")
  13595. (insert "\n:END:")
  13596. (deactivate-mark t)
  13597. (indent-for-tab-command)
  13598. (unless (eolp) (insert "\n")))
  13599. ;; Clear marker, whatever the outcome of insertion is.
  13600. (set-marker rend nil)))))))
  13601. (defvar org-property-set-functions-alist nil
  13602. "Property set function alist.
  13603. Each entry should have the following format:
  13604. (PROPERTY . READ-FUNCTION)
  13605. The read function will be called with the same argument as
  13606. `org-completing-read'.")
  13607. (defun org-set-property-function (property)
  13608. "Get the function that should be used to set PROPERTY.
  13609. This is computed according to `org-property-set-functions-alist'."
  13610. (or (cdr (assoc property org-property-set-functions-alist))
  13611. 'org-completing-read))
  13612. (defun org-read-property-value (property)
  13613. "Read PROPERTY value from user."
  13614. (let* ((completion-ignore-case t)
  13615. (allowed (org-property-get-allowed-values nil property 'table))
  13616. (cur (org-entry-get nil property))
  13617. (prompt (concat property " value"
  13618. (if (and cur (string-match "\\S-" cur))
  13619. (concat " [" cur "]") "") ": "))
  13620. (set-function (org-set-property-function property))
  13621. (val (if allowed
  13622. (funcall set-function prompt allowed nil
  13623. (not (get-text-property 0 'org-unrestricted
  13624. (caar allowed))))
  13625. (let (org-completion-use-ido org-completion-use-iswitchb)
  13626. (funcall set-function prompt
  13627. (mapcar 'list (org-property-values property))
  13628. nil nil "" nil cur)))))
  13629. (if (equal val "")
  13630. cur
  13631. val)))
  13632. (defvar org-last-set-property nil)
  13633. (defun org-read-property-name ()
  13634. "Read a property name."
  13635. (let* ((completion-ignore-case t)
  13636. (keys (org-buffer-property-keys nil t t))
  13637. (default-prop (or (save-excursion
  13638. (save-match-data
  13639. (beginning-of-line)
  13640. (and (looking-at "^\\s-*:\\([^:\n]+\\):")
  13641. (null (string= (match-string 1) "END"))
  13642. (match-string 1))))
  13643. org-last-set-property))
  13644. (property (org-icompleting-read
  13645. (concat "Property"
  13646. (if default-prop (concat " [" default-prop "]") "")
  13647. ": ")
  13648. (mapcar 'list keys)
  13649. nil nil nil nil
  13650. default-prop
  13651. )))
  13652. (if (member property keys)
  13653. property
  13654. (or (cdr (assoc (downcase property)
  13655. (mapcar (lambda (x) (cons (downcase x) x))
  13656. keys)))
  13657. property))))
  13658. (defun org-set-property (property value)
  13659. "In the current entry, set PROPERTY to VALUE.
  13660. When called interactively, this will prompt for a property name, offering
  13661. completion on existing and default properties. And then it will prompt
  13662. for a value, offering completion either on allowed values (via an inherited
  13663. xxx_ALL property) or on existing values in other instances of this property
  13664. in the current file."
  13665. (interactive (list nil nil))
  13666. (let* ((property (or property (org-read-property-name)))
  13667. (value (or value (org-read-property-value property)))
  13668. (fn (cdr (assoc property org-properties-postprocess-alist))))
  13669. (setq org-last-set-property property)
  13670. ;; Possibly postprocess the inserted value:
  13671. (when fn (setq value (funcall fn value)))
  13672. (unless (equal (org-entry-get nil property) value)
  13673. (org-entry-put nil property value))))
  13674. (defun org-delete-property (property)
  13675. "In the current entry, delete PROPERTY."
  13676. (interactive
  13677. (let* ((completion-ignore-case t)
  13678. (prop (org-icompleting-read "Property: "
  13679. (org-entry-properties nil 'standard))))
  13680. (list prop)))
  13681. (message "Property %s %s" property
  13682. (if (org-entry-delete nil property)
  13683. "deleted"
  13684. "was not present in the entry")))
  13685. (defun org-delete-property-globally (property)
  13686. "Remove PROPERTY globally, from all entries."
  13687. (interactive
  13688. (let* ((completion-ignore-case t)
  13689. (prop (org-icompleting-read
  13690. "Globally remove property: "
  13691. (mapcar 'list (org-buffer-property-keys)))))
  13692. (list prop)))
  13693. (save-excursion
  13694. (save-restriction
  13695. (widen)
  13696. (goto-char (point-min))
  13697. (let ((cnt 0))
  13698. (while (re-search-forward
  13699. (org-re-property property)
  13700. nil t)
  13701. (setq cnt (1+ cnt))
  13702. (delete-region (match-beginning 0) (1+ (point-at-eol))))
  13703. (message "Property \"%s\" removed from %d entries" property cnt)))))
  13704. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  13705. (defun org-compute-property-at-point ()
  13706. "Compute the property at point.
  13707. This looks for an enclosing column format, extracts the operator and
  13708. then applies it to the property in the column format's scope."
  13709. (interactive)
  13710. (unless (org-at-property-p)
  13711. (error "Not at a property"))
  13712. (let ((prop (org-match-string-no-properties 2)))
  13713. (org-columns-get-format-and-top-level)
  13714. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  13715. (error "No operator defined for property %s" prop))
  13716. (org-columns-compute prop)))
  13717. (defvar org-property-allowed-value-functions nil
  13718. "Hook for functions supplying allowed values for a specific property.
  13719. The functions must take a single argument, the name of the property, and
  13720. return a flat list of allowed values. If \":ETC\" is one of
  13721. the values, this means that these values are intended as defaults for
  13722. completion, but that other values should be allowed too.
  13723. The functions must return nil if they are not responsible for this
  13724. property.")
  13725. (defun org-property-get-allowed-values (pom property &optional table)
  13726. "Get allowed values for the property PROPERTY.
  13727. When TABLE is non-nil, return an alist that can directly be used for
  13728. completion."
  13729. (let (vals)
  13730. (cond
  13731. ((equal property "TODO")
  13732. (setq vals (org-with-point-at pom
  13733. (append org-todo-keywords-1 '("")))))
  13734. ((equal property "PRIORITY")
  13735. (let ((n org-lowest-priority))
  13736. (while (>= n org-highest-priority)
  13737. (push (char-to-string n) vals)
  13738. (setq n (1- n)))))
  13739. ((member property org-special-properties))
  13740. ((setq vals (run-hook-with-args-until-success
  13741. 'org-property-allowed-value-functions property)))
  13742. (t
  13743. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  13744. (when (and vals (string-match "\\S-" vals))
  13745. (setq vals (car (read-from-string (concat "(" vals ")"))))
  13746. (setq vals (mapcar (lambda (x)
  13747. (cond ((stringp x) x)
  13748. ((numberp x) (number-to-string x))
  13749. ((symbolp x) (symbol-name x))
  13750. (t "???")))
  13751. vals)))))
  13752. (when (member ":ETC" vals)
  13753. (setq vals (remove ":ETC" vals))
  13754. (org-add-props (car vals) '(org-unrestricted t)))
  13755. (if table (mapcar 'list vals) vals)))
  13756. (defun org-property-previous-allowed-value (&optional previous)
  13757. "Switch to the next allowed value for this property."
  13758. (interactive)
  13759. (org-property-next-allowed-value t))
  13760. (defun org-property-next-allowed-value (&optional previous)
  13761. "Switch to the next allowed value for this property."
  13762. (interactive)
  13763. (unless (org-at-property-p)
  13764. (error "Not at a property"))
  13765. (let* ((key (match-string 2))
  13766. (value (match-string 3))
  13767. (allowed (or (org-property-get-allowed-values (point) key)
  13768. (and (member value '("[ ]" "[-]" "[X]"))
  13769. '("[ ]" "[X]"))))
  13770. nval)
  13771. (unless allowed
  13772. (error "Allowed values for this property have not been defined"))
  13773. (if previous (setq allowed (reverse allowed)))
  13774. (if (member value allowed)
  13775. (setq nval (car (cdr (member value allowed)))))
  13776. (setq nval (or nval (car allowed)))
  13777. (if (equal nval value)
  13778. (error "Only one allowed value for this property"))
  13779. (org-at-property-p)
  13780. (replace-match (concat " :" key ": " nval) t t)
  13781. (org-indent-line)
  13782. (beginning-of-line 1)
  13783. (skip-chars-forward " \t")
  13784. (run-hook-with-args 'org-property-changed-functions key nval)))
  13785. (defun org-find-olp (path &optional this-buffer)
  13786. "Return a marker pointing to the entry at outline path OLP.
  13787. If anything goes wrong, throw an error.
  13788. You can wrap this call to catch the error like this:
  13789. (condition-case msg
  13790. (org-mobile-locate-entry (match-string 4))
  13791. (error (nth 1 msg)))
  13792. The return value will then be either a string with the error message,
  13793. or a marker if everything is OK.
  13794. If THIS-BUFFER is set, the outline path does not contain a file,
  13795. only headings."
  13796. (let* ((file (if this-buffer buffer-file-name (pop path)))
  13797. (buffer (if this-buffer (current-buffer) (find-file-noselect file)))
  13798. (level 1)
  13799. (lmin 1)
  13800. (lmax 1)
  13801. limit re end found pos heading cnt flevel)
  13802. (unless buffer (error "File not found :%s" file))
  13803. (with-current-buffer buffer
  13804. (save-excursion
  13805. (save-restriction
  13806. (widen)
  13807. (setq limit (point-max))
  13808. (goto-char (point-min))
  13809. (while (setq heading (pop path))
  13810. (setq re (format org-complex-heading-regexp-format
  13811. (regexp-quote heading)))
  13812. (setq cnt 0 pos (point))
  13813. (while (re-search-forward re end t)
  13814. (setq level (- (match-end 1) (match-beginning 1)))
  13815. (if (and (>= level lmin) (<= level lmax))
  13816. (setq found (match-beginning 0) flevel level cnt (1+ cnt))))
  13817. (when (= cnt 0) (error "Heading not found on level %d: %s"
  13818. lmax heading))
  13819. (when (> cnt 1) (error "Heading not unique on level %d: %s"
  13820. lmax heading))
  13821. (goto-char found)
  13822. (setq lmin (1+ flevel) lmax (+ lmin (if org-odd-levels-only 1 0)))
  13823. (setq end (save-excursion (org-end-of-subtree t t))))
  13824. (when (org-at-heading-p)
  13825. (move-marker (make-marker) (point))))))))
  13826. (defun org-find-exact-headline-in-buffer (heading &optional buffer pos-only)
  13827. "Find node HEADING in BUFFER.
  13828. Return a marker to the heading if it was found, or nil if not.
  13829. If POS-ONLY is set, return just the position instead of a marker.
  13830. The heading text must match exact, but it may have a TODO keyword,
  13831. a priority cookie and tags in the standard locations."
  13832. (with-current-buffer (or buffer (current-buffer))
  13833. (save-excursion
  13834. (save-restriction
  13835. (widen)
  13836. (goto-char (point-min))
  13837. (let (case-fold-search)
  13838. (if (re-search-forward
  13839. (format org-complex-heading-regexp-format
  13840. (regexp-quote heading)) nil t)
  13841. (if pos-only
  13842. (match-beginning 0)
  13843. (move-marker (make-marker) (match-beginning 0)))))))))
  13844. (defun org-find-exact-heading-in-directory (heading &optional dir)
  13845. "Find Org node headline HEADING in all .org files in directory DIR.
  13846. When the target headline is found, return a marker to this location."
  13847. (let ((files (directory-files (or dir default-directory)
  13848. nil "\\`[^.#].*\\.org\\'"))
  13849. file visiting m buffer)
  13850. (catch 'found
  13851. (while (setq file (pop files))
  13852. (message "trying %s" file)
  13853. (setq visiting (org-find-base-buffer-visiting file))
  13854. (setq buffer (or visiting (find-file-noselect file)))
  13855. (setq m (org-find-exact-headline-in-buffer
  13856. heading buffer))
  13857. (when (and (not m) (not visiting)) (kill-buffer buffer))
  13858. (and m (throw 'found m))))))
  13859. (defun org-find-entry-with-id (ident)
  13860. "Locate the entry that contains the ID property with exact value IDENT.
  13861. IDENT can be a string, a symbol or a number, this function will search for
  13862. the string representation of it.
  13863. Return the position where this entry starts, or nil if there is no such entry."
  13864. (interactive "sID: ")
  13865. (let ((id (cond
  13866. ((stringp ident) ident)
  13867. ((symbol-name ident) (symbol-name ident))
  13868. ((numberp ident) (number-to-string ident))
  13869. (t (error "IDENT %s must be a string, symbol or number" ident))))
  13870. (case-fold-search nil))
  13871. (save-excursion
  13872. (save-restriction
  13873. (widen)
  13874. (goto-char (point-min))
  13875. (when (re-search-forward
  13876. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  13877. nil t)
  13878. (org-back-to-heading t)
  13879. (point))))))
  13880. ;;;; Timestamps
  13881. (defvar org-last-changed-timestamp nil)
  13882. (defvar org-last-inserted-timestamp nil
  13883. "The last time stamp inserted with `org-insert-time-stamp'.")
  13884. (defvar org-time-was-given) ; dynamically scoped parameter
  13885. (defvar org-end-time-was-given) ; dynamically scoped parameter
  13886. (defvar org-ts-what) ; dynamically scoped parameter
  13887. (defun org-time-stamp (arg &optional inactive)
  13888. "Prompt for a date/time and insert a time stamp.
  13889. If the user specifies a time like HH:MM or if this command is
  13890. called with at least one prefix argument, the time stamp contains
  13891. the date and the time. Otherwise, only the date is be included.
  13892. All parts of a date not specified by the user is filled in from
  13893. the current date/time. So if you just press return without
  13894. typing anything, the time stamp will represent the current
  13895. date/time.
  13896. If there is already a timestamp at the cursor, it will be
  13897. modified.
  13898. With two universal prefix arguments, insert an active timestamp
  13899. with the current time without prompting the user."
  13900. (interactive "P")
  13901. (let* ((ts nil)
  13902. (default-time
  13903. ;; Default time is either today, or, when entering a range,
  13904. ;; the range start.
  13905. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  13906. (save-excursion
  13907. (re-search-backward
  13908. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  13909. (- (point) 20) t)))
  13910. (apply 'encode-time (org-parse-time-string (match-string 1)))
  13911. (current-time)))
  13912. (default-input (and ts (org-get-compact-tod ts)))
  13913. (repeater (save-excursion
  13914. (save-match-data
  13915. (beginning-of-line)
  13916. (when (re-search-forward
  13917. "\\([.+-]+[0-9]+[hdwmy] ?\\)+" ;;\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  13918. (save-excursion (progn (end-of-line) (point))) t)
  13919. (match-string 0)))))
  13920. org-time-was-given org-end-time-was-given time)
  13921. (cond
  13922. ((and (org-at-timestamp-p t)
  13923. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  13924. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  13925. (insert "--")
  13926. (setq time (let ((this-command this-command))
  13927. (org-read-date arg 'totime nil nil
  13928. default-time default-input inactive)))
  13929. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  13930. ((org-at-timestamp-p t)
  13931. (setq time (let ((this-command this-command))
  13932. (org-read-date arg 'totime nil nil default-time default-input inactive)))
  13933. (when (org-at-timestamp-p t) ; just to get the match data
  13934. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  13935. (replace-match "")
  13936. (setq org-last-changed-timestamp
  13937. (org-insert-time-stamp
  13938. time (or org-time-was-given arg)
  13939. inactive nil nil (list org-end-time-was-given)))
  13940. (when repeater (goto-char (1- (point))) (insert " " repeater)
  13941. (setq org-last-changed-timestamp
  13942. (concat (substring org-last-inserted-timestamp 0 -1)
  13943. " " repeater ">"))))
  13944. (message "Timestamp updated"))
  13945. ((equal arg '(16))
  13946. (org-insert-time-stamp (current-time) t))
  13947. (t
  13948. (setq time (let ((this-command this-command))
  13949. (org-read-date arg 'totime nil nil default-time default-input inactive)))
  13950. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  13951. nil nil (list org-end-time-was-given))))))
  13952. ;; FIXME: can we use this for something else, like computing time differences?
  13953. (defun org-get-compact-tod (s)
  13954. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  13955. (let* ((t1 (match-string 1 s))
  13956. (h1 (string-to-number (match-string 2 s)))
  13957. (m1 (string-to-number (match-string 3 s)))
  13958. (t2 (and (match-end 4) (match-string 5 s)))
  13959. (h2 (and t2 (string-to-number (match-string 6 s))))
  13960. (m2 (and t2 (string-to-number (match-string 7 s))))
  13961. dh dm)
  13962. (if (not t2)
  13963. t1
  13964. (setq dh (- h2 h1) dm (- m2 m1))
  13965. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  13966. (concat t1 "+" (number-to-string dh)
  13967. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  13968. (defun org-time-stamp-inactive (&optional arg)
  13969. "Insert an inactive time stamp.
  13970. An inactive time stamp is enclosed in square brackets instead of angle
  13971. brackets. It is inactive in the sense that it does not trigger agenda entries,
  13972. does not link to the calendar and cannot be changed with the S-cursor keys.
  13973. So these are more for recording a certain time/date."
  13974. (interactive "P")
  13975. (org-time-stamp arg 'inactive))
  13976. (defvar org-date-ovl (make-overlay 1 1))
  13977. (overlay-put org-date-ovl 'face 'org-date-selected)
  13978. (org-detach-overlay org-date-ovl)
  13979. (defvar org-ans1) ; dynamically scoped parameter
  13980. (defvar org-ans2) ; dynamically scoped parameter
  13981. (defvar org-plain-time-of-day-regexp) ; defined below
  13982. (defvar org-overriding-default-time nil) ; dynamically scoped
  13983. (defvar org-read-date-overlay nil)
  13984. (defvar org-dcst nil) ; dynamically scoped
  13985. (defvar org-read-date-history nil)
  13986. (defvar org-read-date-final-answer nil)
  13987. (defvar org-read-date-analyze-futurep nil)
  13988. (defvar org-read-date-analyze-forced-year nil)
  13989. (defvar org-read-date-inactive)
  13990. (defun org-read-date (&optional org-with-time to-time from-string prompt
  13991. default-time default-input inactive)
  13992. "Read a date, possibly a time, and make things smooth for the user.
  13993. The prompt will suggest to enter an ISO date, but you can also enter anything
  13994. which will at least partially be understood by `parse-time-string'.
  13995. Unrecognized parts of the date will default to the current day, month, year,
  13996. hour and minute. If this command is called to replace a timestamp at point,
  13997. or to enter the second timestamp of a range, the default time is taken
  13998. from the existing stamp. Furthermore, the command prefers the future,
  13999. so if you are giving a date where the year is not given, and the day-month
  14000. combination is already past in the current year, it will assume you
  14001. mean next year. For details, see the manual. A few examples:
  14002. 3-2-5 --> 2003-02-05
  14003. feb 15 --> currentyear-02-15
  14004. 2/15 --> currentyear-02-15
  14005. sep 12 9 --> 2009-09-12
  14006. 12:45 --> today 12:45
  14007. 22 sept 0:34 --> currentyear-09-22 0:34
  14008. 12 --> currentyear-currentmonth-12
  14009. Fri --> nearest Friday (today or later)
  14010. etc.
  14011. Furthermore you can specify a relative date by giving, as the *first* thing
  14012. in the input: a plus/minus sign, a number and a letter [hdwmy] to indicate
  14013. change in days weeks, months, years.
  14014. With a single plus or minus, the date is relative to today. With a double
  14015. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  14016. +4d --> four days from today
  14017. +4 --> same as above
  14018. +2w --> two weeks from today
  14019. ++5 --> five days from default date
  14020. The function understands only English month and weekday abbreviations.
  14021. While prompting, a calendar is popped up - you can also select the
  14022. date with the mouse (button 1). The calendar shows a period of three
  14023. months. To scroll it to other months, use the keys `>' and `<'.
  14024. If you don't like the calendar, turn it off with
  14025. \(setq org-read-date-popup-calendar nil)
  14026. With optional argument TO-TIME, the date will immediately be converted
  14027. to an internal time.
  14028. With an optional argument ORG-WITH-TIME, the prompt will suggest to
  14029. also insert a time. Note that when ORG-WITH-TIME is not set, you can
  14030. still enter a time, and this function will inform the calling routine
  14031. about this change. The calling routine may then choose to change the
  14032. format used to insert the time stamp into the buffer to include the time.
  14033. With optional argument FROM-STRING, read from this string instead from
  14034. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  14035. the time/date that is used for everything that is not specified by the
  14036. user."
  14037. (require 'parse-time)
  14038. (let* ((org-time-stamp-rounding-minutes
  14039. (if (equal org-with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  14040. (org-dcst org-display-custom-times)
  14041. (ct (org-current-time))
  14042. (org-def (or org-overriding-default-time default-time ct))
  14043. (org-defdecode (decode-time org-def))
  14044. (dummy (progn
  14045. (when (< (nth 2 org-defdecode) org-extend-today-until)
  14046. (setcar (nthcdr 2 org-defdecode) -1)
  14047. (setcar (nthcdr 1 org-defdecode) 59)
  14048. (setq org-def (apply 'encode-time org-defdecode)
  14049. org-defdecode (decode-time org-def)))))
  14050. (calendar-frame-setup nil)
  14051. (calendar-setup nil)
  14052. (calendar-move-hook nil)
  14053. (calendar-view-diary-initially-flag nil)
  14054. (calendar-view-holidays-initially-flag nil)
  14055. (timestr (format-time-string
  14056. (if org-with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") org-def))
  14057. (prompt (concat (if prompt (concat prompt " ") "")
  14058. (format "Date+time [%s]: " timestr)))
  14059. ans (org-ans0 "") org-ans1 org-ans2 final)
  14060. (cond
  14061. (from-string (setq ans from-string))
  14062. (org-read-date-popup-calendar
  14063. (save-excursion
  14064. (save-window-excursion
  14065. (calendar)
  14066. (org-eval-in-calendar '(setq cursor-type nil) t)
  14067. (unwind-protect
  14068. (progn
  14069. (calendar-forward-day (- (time-to-days org-def)
  14070. (calendar-absolute-from-gregorian
  14071. (calendar-current-date))))
  14072. (org-eval-in-calendar nil t)
  14073. (let* ((old-map (current-local-map))
  14074. (map (copy-keymap calendar-mode-map))
  14075. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  14076. (org-defkey map (kbd "RET") 'org-calendar-select)
  14077. (org-defkey map [mouse-1] 'org-calendar-select-mouse)
  14078. (org-defkey map [mouse-2] 'org-calendar-select-mouse)
  14079. (org-defkey minibuffer-local-map [(meta shift left)]
  14080. (lambda () (interactive)
  14081. (org-eval-in-calendar '(calendar-backward-month 1))))
  14082. (org-defkey minibuffer-local-map [(meta shift right)]
  14083. (lambda () (interactive)
  14084. (org-eval-in-calendar '(calendar-forward-month 1))))
  14085. (org-defkey minibuffer-local-map [(meta shift up)]
  14086. (lambda () (interactive)
  14087. (org-eval-in-calendar '(calendar-backward-year 1))))
  14088. (org-defkey minibuffer-local-map [(meta shift down)]
  14089. (lambda () (interactive)
  14090. (org-eval-in-calendar '(calendar-forward-year 1))))
  14091. (org-defkey minibuffer-local-map [?\e (shift left)]
  14092. (lambda () (interactive)
  14093. (org-eval-in-calendar '(calendar-backward-month 1))))
  14094. (org-defkey minibuffer-local-map [?\e (shift right)]
  14095. (lambda () (interactive)
  14096. (org-eval-in-calendar '(calendar-forward-month 1))))
  14097. (org-defkey minibuffer-local-map [?\e (shift up)]
  14098. (lambda () (interactive)
  14099. (org-eval-in-calendar '(calendar-backward-year 1))))
  14100. (org-defkey minibuffer-local-map [?\e (shift down)]
  14101. (lambda () (interactive)
  14102. (org-eval-in-calendar '(calendar-forward-year 1))))
  14103. (org-defkey minibuffer-local-map [(shift up)]
  14104. (lambda () (interactive)
  14105. (org-eval-in-calendar '(calendar-backward-week 1))))
  14106. (org-defkey minibuffer-local-map [(shift down)]
  14107. (lambda () (interactive)
  14108. (org-eval-in-calendar '(calendar-forward-week 1))))
  14109. (org-defkey minibuffer-local-map [(shift left)]
  14110. (lambda () (interactive)
  14111. (org-eval-in-calendar '(calendar-backward-day 1))))
  14112. (org-defkey minibuffer-local-map [(shift right)]
  14113. (lambda () (interactive)
  14114. (org-eval-in-calendar '(calendar-forward-day 1))))
  14115. (org-defkey minibuffer-local-map ">"
  14116. (lambda () (interactive)
  14117. (org-eval-in-calendar '(scroll-calendar-left 1))))
  14118. (org-defkey minibuffer-local-map "<"
  14119. (lambda () (interactive)
  14120. (org-eval-in-calendar '(scroll-calendar-right 1))))
  14121. (org-defkey minibuffer-local-map "\C-v"
  14122. (lambda () (interactive)
  14123. (org-eval-in-calendar
  14124. '(calendar-scroll-left-three-months 1))))
  14125. (org-defkey minibuffer-local-map "\M-v"
  14126. (lambda () (interactive)
  14127. (org-eval-in-calendar
  14128. '(calendar-scroll-right-three-months 1))))
  14129. (run-hooks 'org-read-date-minibuffer-setup-hook)
  14130. (unwind-protect
  14131. (progn
  14132. (use-local-map map)
  14133. (setq org-read-date-inactive inactive)
  14134. (add-hook 'post-command-hook 'org-read-date-display)
  14135. (setq org-ans0 (read-string prompt default-input
  14136. 'org-read-date-history nil))
  14137. ;; org-ans0: from prompt
  14138. ;; org-ans1: from mouse click
  14139. ;; org-ans2: from calendar motion
  14140. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  14141. (remove-hook 'post-command-hook 'org-read-date-display)
  14142. (use-local-map old-map)
  14143. (when org-read-date-overlay
  14144. (delete-overlay org-read-date-overlay)
  14145. (setq org-read-date-overlay nil)))))
  14146. (bury-buffer "*Calendar*")))))
  14147. (t ; Naked prompt only
  14148. (unwind-protect
  14149. (setq ans (read-string prompt default-input
  14150. 'org-read-date-history timestr))
  14151. (when org-read-date-overlay
  14152. (delete-overlay org-read-date-overlay)
  14153. (setq org-read-date-overlay nil)))))
  14154. (setq final (org-read-date-analyze ans org-def org-defdecode))
  14155. (when org-read-date-analyze-forced-year
  14156. (message "Year was forced into %s"
  14157. (if org-read-date-force-compatible-dates
  14158. "compatible range (1970-2037)"
  14159. "range representable on this machine"))
  14160. (ding))
  14161. ;; One round trip to get rid of 34th of August and stuff like that....
  14162. (setq final (decode-time (apply 'encode-time final)))
  14163. (setq org-read-date-final-answer ans)
  14164. (if to-time
  14165. (apply 'encode-time final)
  14166. (if (and (boundp 'org-time-was-given) org-time-was-given)
  14167. (format "%04d-%02d-%02d %02d:%02d"
  14168. (nth 5 final) (nth 4 final) (nth 3 final)
  14169. (nth 2 final) (nth 1 final))
  14170. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  14171. (defvar org-def)
  14172. (defvar org-defdecode)
  14173. (defvar org-with-time)
  14174. (defun org-read-date-display ()
  14175. "Display the current date prompt interpretation in the minibuffer."
  14176. (when org-read-date-display-live
  14177. (when org-read-date-overlay
  14178. (delete-overlay org-read-date-overlay))
  14179. (when (minibufferp (current-buffer))
  14180. (save-excursion
  14181. (end-of-line 1)
  14182. (while (not (equal (buffer-substring
  14183. (max (point-min) (- (point) 4)) (point))
  14184. " "))
  14185. (insert " ")))
  14186. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  14187. " " (or org-ans1 org-ans2)))
  14188. (org-end-time-was-given nil)
  14189. (f (org-read-date-analyze ans org-def org-defdecode))
  14190. (fmts (if org-dcst
  14191. org-time-stamp-custom-formats
  14192. org-time-stamp-formats))
  14193. (fmt (if (or org-with-time
  14194. (and (boundp 'org-time-was-given) org-time-was-given))
  14195. (cdr fmts)
  14196. (car fmts)))
  14197. (txt (format-time-string fmt (apply 'encode-time f)))
  14198. (txt (if org-read-date-inactive (concat "[" (substring txt 1 -1) "]") txt))
  14199. (txt (concat "=> " txt)))
  14200. (when (and org-end-time-was-given
  14201. (string-match org-plain-time-of-day-regexp txt))
  14202. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  14203. org-end-time-was-given
  14204. (substring txt (match-end 0)))))
  14205. (when org-read-date-analyze-futurep
  14206. (setq txt (concat txt " (=>F)")))
  14207. (setq org-read-date-overlay
  14208. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  14209. (org-overlay-display org-read-date-overlay txt 'secondary-selection)))))
  14210. (defun org-read-date-analyze (ans org-def org-defdecode)
  14211. "Analyze the combined answer of the date prompt."
  14212. ;; FIXME: cleanup and comment
  14213. (let ((nowdecode (decode-time (current-time)))
  14214. delta deltan deltaw deltadef year month day
  14215. hour minute second wday pm h2 m2 tl wday1
  14216. iso-year iso-weekday iso-week iso-year iso-date futurep kill-year)
  14217. (setq org-read-date-analyze-futurep nil
  14218. org-read-date-analyze-forced-year nil)
  14219. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  14220. (setq ans "+0"))
  14221. (when (setq delta (org-read-date-get-relative ans (current-time) org-def))
  14222. (setq ans (replace-match "" t t ans)
  14223. deltan (car delta)
  14224. deltaw (nth 1 delta)
  14225. deltadef (nth 2 delta)))
  14226. ;; Check if there is an iso week date in there. If yes, store the
  14227. ;; info and postpone interpreting it until the rest of the parsing
  14228. ;; is done.
  14229. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  14230. (setq iso-year (if (match-end 1)
  14231. (org-small-year-to-year
  14232. (string-to-number (match-string 1 ans))))
  14233. iso-weekday (if (match-end 3)
  14234. (string-to-number (match-string 3 ans)))
  14235. iso-week (string-to-number (match-string 2 ans)))
  14236. (setq ans (replace-match "" t t ans)))
  14237. ;; Help matching ISO dates with single digit month or day, like 2006-8-11.
  14238. (when (string-match
  14239. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  14240. (setq year (if (match-end 2)
  14241. (string-to-number (match-string 2 ans))
  14242. (progn (setq kill-year t)
  14243. (string-to-number (format-time-string "%Y"))))
  14244. month (string-to-number (match-string 3 ans))
  14245. day (string-to-number (match-string 4 ans)))
  14246. (if (< year 100) (setq year (+ 2000 year)))
  14247. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  14248. t nil ans)))
  14249. ;; Help matching dotted european dates
  14250. (when (string-match
  14251. "^ *\\(3[01]\\|0?[1-9]\\|[12][0-9]\\)\\. ?\\(0?[1-9]\\|1[012]\\)\\. ?\\([1-9][0-9][0-9][0-9]\\)?" ans)
  14252. (setq year (if (match-end 3)
  14253. (string-to-number (match-string 3 ans))
  14254. (progn (setq kill-year t)
  14255. (string-to-number (format-time-string "%Y"))))
  14256. day (string-to-number (match-string 1 ans))
  14257. month (string-to-number (match-string 2 ans))
  14258. ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  14259. t nil ans)))
  14260. ;; Help matching american dates, like 5/30 or 5/30/7
  14261. (when (string-match
  14262. "^ *\\(0?[1-9]\\|1[012]\\)/\\(0?[1-9]\\|[12][0-9]\\|3[01]\\)\\(/\\([0-9]+\\)\\)?\\([^/0-9]\\|$\\)" ans)
  14263. (setq year (if (match-end 4)
  14264. (string-to-number (match-string 4 ans))
  14265. (progn (setq kill-year t)
  14266. (string-to-number (format-time-string "%Y"))))
  14267. month (string-to-number (match-string 1 ans))
  14268. day (string-to-number (match-string 2 ans)))
  14269. (if (< year 100) (setq year (+ 2000 year)))
  14270. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  14271. t nil ans)))
  14272. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  14273. ;; If there is a time with am/pm, and *no* time without it, we convert
  14274. ;; so that matching will be successful.
  14275. (loop for i from 1 to 2 do ; twice, for end time as well
  14276. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  14277. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  14278. (setq hour (string-to-number (match-string 1 ans))
  14279. minute (if (match-end 3)
  14280. (string-to-number (match-string 3 ans))
  14281. 0)
  14282. pm (equal ?p
  14283. (string-to-char (downcase (match-string 4 ans)))))
  14284. (if (and (= hour 12) (not pm))
  14285. (setq hour 0)
  14286. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  14287. (setq ans (replace-match (format "%02d:%02d" hour minute)
  14288. t t ans))))
  14289. ;; Check if a time range is given as a duration
  14290. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  14291. (setq hour (string-to-number (match-string 1 ans))
  14292. h2 (+ hour (string-to-number (match-string 3 ans)))
  14293. minute (string-to-number (match-string 2 ans))
  14294. m2 (+ minute (if (match-end 5) (string-to-number
  14295. (match-string 5 ans))0)))
  14296. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  14297. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  14298. t t ans)))
  14299. ;; Check if there is a time range
  14300. (when (boundp 'org-end-time-was-given)
  14301. (setq org-time-was-given nil)
  14302. (when (and (string-match org-plain-time-of-day-regexp ans)
  14303. (match-end 8))
  14304. (setq org-end-time-was-given (match-string 8 ans))
  14305. (setq ans (concat (substring ans 0 (match-beginning 7))
  14306. (substring ans (match-end 7))))))
  14307. (setq tl (parse-time-string ans)
  14308. day (or (nth 3 tl) (nth 3 org-defdecode))
  14309. month (or (nth 4 tl)
  14310. (if (and org-read-date-prefer-future
  14311. (nth 3 tl) (< (nth 3 tl) (nth 3 nowdecode)))
  14312. (prog1 (1+ (nth 4 nowdecode)) (setq futurep t))
  14313. (nth 4 org-defdecode)))
  14314. year (or (and (not kill-year) (nth 5 tl))
  14315. (if (and org-read-date-prefer-future
  14316. (nth 4 tl) (< (nth 4 tl) (nth 4 nowdecode)))
  14317. (prog1 (1+ (nth 5 nowdecode)) (setq futurep t))
  14318. (nth 5 org-defdecode)))
  14319. hour (or (nth 2 tl) (nth 2 org-defdecode))
  14320. minute (or (nth 1 tl) (nth 1 org-defdecode))
  14321. second (or (nth 0 tl) 0)
  14322. wday (nth 6 tl))
  14323. (when (and (eq org-read-date-prefer-future 'time)
  14324. (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
  14325. (equal day (nth 3 nowdecode))
  14326. (equal month (nth 4 nowdecode))
  14327. (equal year (nth 5 nowdecode))
  14328. (nth 2 tl)
  14329. (or (< (nth 2 tl) (nth 2 nowdecode))
  14330. (and (= (nth 2 tl) (nth 2 nowdecode))
  14331. (nth 1 tl)
  14332. (< (nth 1 tl) (nth 1 nowdecode)))))
  14333. (setq day (1+ day)
  14334. futurep t))
  14335. ;; Special date definitions below
  14336. (cond
  14337. (iso-week
  14338. ;; There was an iso week
  14339. (require 'cal-iso)
  14340. (setq futurep nil)
  14341. (setq year (or iso-year year)
  14342. day (or iso-weekday wday 1)
  14343. wday nil ; to make sure that the trigger below does not match
  14344. iso-date (calendar-gregorian-from-absolute
  14345. (calendar-absolute-from-iso
  14346. (list iso-week day year))))
  14347. ; FIXME: Should we also push ISO weeks into the future?
  14348. ; (when (and org-read-date-prefer-future
  14349. ; (not iso-year)
  14350. ; (< (calendar-absolute-from-gregorian iso-date)
  14351. ; (time-to-days (current-time))))
  14352. ; (setq year (1+ year)
  14353. ; iso-date (calendar-gregorian-from-absolute
  14354. ; (calendar-absolute-from-iso
  14355. ; (list iso-week day year)))))
  14356. (setq month (car iso-date)
  14357. year (nth 2 iso-date)
  14358. day (nth 1 iso-date)))
  14359. (deltan
  14360. (setq futurep nil)
  14361. (unless deltadef
  14362. (let ((now (decode-time (current-time))))
  14363. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  14364. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  14365. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  14366. ((equal deltaw "m") (setq month (+ month deltan)))
  14367. ((equal deltaw "y") (setq year (+ year deltan)))))
  14368. ((and wday (not (nth 3 tl)))
  14369. ;; Weekday was given, but no day, so pick that day in the week
  14370. ;; on or after the derived date.
  14371. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  14372. (unless (equal wday wday1)
  14373. (setq day (+ day (% (- wday wday1 -7) 7))))))
  14374. (if (and (boundp 'org-time-was-given)
  14375. (nth 2 tl))
  14376. (setq org-time-was-given t))
  14377. (if (< year 100) (setq year (+ 2000 year)))
  14378. ;; Check of the date is representable
  14379. (if org-read-date-force-compatible-dates
  14380. (progn
  14381. (if (< year 1970)
  14382. (setq year 1970 org-read-date-analyze-forced-year t))
  14383. (if (> year 2037)
  14384. (setq year 2037 org-read-date-analyze-forced-year t)))
  14385. (condition-case nil
  14386. (ignore (encode-time second minute hour day month year))
  14387. (error
  14388. (setq year (nth 5 org-defdecode))
  14389. (setq org-read-date-analyze-forced-year t))))
  14390. (setq org-read-date-analyze-futurep futurep)
  14391. (list second minute hour day month year)))
  14392. (defvar parse-time-weekdays)
  14393. (defun org-read-date-get-relative (s today default)
  14394. "Check string S for special relative date string.
  14395. TODAY and DEFAULT are internal times, for today and for a default.
  14396. Return shift list (N what def-flag)
  14397. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  14398. N is the number of WHATs to shift.
  14399. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  14400. the DEFAULT date rather than TODAY."
  14401. (require 'parse-time)
  14402. (when (and
  14403. (string-match
  14404. (concat
  14405. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  14406. "\\([0-9]+\\)?"
  14407. "\\([hdwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  14408. "\\([ \t]\\|$\\)") s)
  14409. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  14410. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  14411. (string-to-char (substring (match-string 1 s) -1))
  14412. ?+))
  14413. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  14414. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  14415. (what (if (match-end 3) (match-string 3 s) "d"))
  14416. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  14417. (date (if rel default today))
  14418. (wday (nth 6 (decode-time date)))
  14419. delta)
  14420. (if wday1
  14421. (progn
  14422. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  14423. (if (= dir ?-) (setq delta (- delta 7)))
  14424. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  14425. (list delta "d" rel))
  14426. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  14427. (defun org-order-calendar-date-args (arg1 arg2 arg3)
  14428. "Turn a user-specified date into the internal representation.
  14429. The internal representation needed by the calendar is (month day year).
  14430. This is a wrapper to handle the brain-dead convention in calendar that
  14431. user function argument order change dependent on argument order."
  14432. (if (boundp 'calendar-date-style)
  14433. (cond
  14434. ((eq calendar-date-style 'american)
  14435. (list arg1 arg2 arg3))
  14436. ((eq calendar-date-style 'european)
  14437. (list arg2 arg1 arg3))
  14438. ((eq calendar-date-style 'iso)
  14439. (list arg2 arg3 arg1)))
  14440. (org-no-warnings ;; european-calendar-style is obsolete as of version 23.1
  14441. (if (org-bound-and-true-p european-calendar-style)
  14442. (list arg2 arg1 arg3)
  14443. (list arg1 arg2 arg3)))))
  14444. (defun org-eval-in-calendar (form &optional keepdate)
  14445. "Eval FORM in the calendar window and return to current window.
  14446. When KEEPDATE is non-nil, update `org-ans2' from the cursor date,
  14447. otherwise stick to the current value of `org-ans2'."
  14448. (let ((sf (selected-frame))
  14449. (sw (selected-window)))
  14450. (select-window (get-buffer-window "*Calendar*" t))
  14451. (eval form)
  14452. (when (and (not keepdate) (calendar-cursor-to-date))
  14453. (let* ((date (calendar-cursor-to-date))
  14454. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14455. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  14456. (move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  14457. (select-window sw)
  14458. (org-select-frame-set-input-focus sf)))
  14459. (defun org-calendar-select ()
  14460. "Return to `org-read-date' with the date currently selected.
  14461. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  14462. (interactive)
  14463. (when (calendar-cursor-to-date)
  14464. (let* ((date (calendar-cursor-to-date))
  14465. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14466. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  14467. (if (active-minibuffer-window) (exit-minibuffer))))
  14468. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  14469. "Insert a date stamp for the date given by the internal TIME.
  14470. WITH-HM means use the stamp format that includes the time of the day.
  14471. INACTIVE means use square brackets instead of angular ones, so that the
  14472. stamp will not contribute to the agenda.
  14473. PRE and POST are optional strings to be inserted before and after the
  14474. stamp.
  14475. The command returns the inserted time stamp."
  14476. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  14477. stamp)
  14478. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  14479. (insert-before-markers (or pre ""))
  14480. (when (listp extra)
  14481. (setq extra (car extra))
  14482. (if (and (stringp extra)
  14483. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  14484. (setq extra (format "-%02d:%02d"
  14485. (string-to-number (match-string 1 extra))
  14486. (string-to-number (match-string 2 extra))))
  14487. (setq extra nil)))
  14488. (when extra
  14489. (setq fmt (concat (substring fmt 0 -1) extra (substring fmt -1))))
  14490. (insert-before-markers (setq stamp (format-time-string fmt time)))
  14491. (insert-before-markers (or post ""))
  14492. (setq org-last-inserted-timestamp stamp)))
  14493. (defun org-toggle-time-stamp-overlays ()
  14494. "Toggle the use of custom time stamp formats."
  14495. (interactive)
  14496. (setq org-display-custom-times (not org-display-custom-times))
  14497. (unless org-display-custom-times
  14498. (let ((p (point-min)) (bmp (buffer-modified-p)))
  14499. (while (setq p (next-single-property-change p 'display))
  14500. (if (and (get-text-property p 'display)
  14501. (eq (get-text-property p 'face) 'org-date))
  14502. (remove-text-properties
  14503. p (setq p (next-single-property-change p 'display))
  14504. '(display t))))
  14505. (set-buffer-modified-p bmp)))
  14506. (if (featurep 'xemacs)
  14507. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  14508. (org-restart-font-lock)
  14509. (setq org-table-may-need-update t)
  14510. (if org-display-custom-times
  14511. (message "Time stamps are overlaid with custom format")
  14512. (message "Time stamp overlays removed")))
  14513. (defun org-display-custom-time (beg end)
  14514. "Overlay modified time stamp format over timestamp between BEG and END."
  14515. (let* ((ts (buffer-substring beg end))
  14516. t1 w1 with-hm tf time str w2 (off 0))
  14517. (save-match-data
  14518. (setq t1 (org-parse-time-string ts t))
  14519. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)?\\'" ts)
  14520. (setq off (- (match-end 0) (match-beginning 0)))))
  14521. (setq end (- end off))
  14522. (setq w1 (- end beg)
  14523. with-hm (and (nth 1 t1) (nth 2 t1))
  14524. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  14525. time (org-fix-decoded-time t1)
  14526. str (org-add-props
  14527. (format-time-string
  14528. (substring tf 1 -1) (apply 'encode-time time))
  14529. nil 'mouse-face 'highlight)
  14530. w2 (length str))
  14531. (if (not (= w2 w1))
  14532. (add-text-properties (1+ beg) (+ 2 beg)
  14533. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  14534. (if (featurep 'xemacs)
  14535. (progn
  14536. (put-text-property beg end 'invisible t)
  14537. (put-text-property beg end 'end-glyph (make-glyph str)))
  14538. (put-text-property beg end 'display str))))
  14539. (defun org-translate-time (string)
  14540. "Translate all timestamps in STRING to custom format.
  14541. But do this only if the variable `org-display-custom-times' is set."
  14542. (when org-display-custom-times
  14543. (save-match-data
  14544. (let* ((start 0)
  14545. (re org-ts-regexp-both)
  14546. t1 with-hm inactive tf time str beg end)
  14547. (while (setq start (string-match re string start))
  14548. (setq beg (match-beginning 0)
  14549. end (match-end 0)
  14550. t1 (save-match-data
  14551. (org-parse-time-string (substring string beg end) t))
  14552. with-hm (and (nth 1 t1) (nth 2 t1))
  14553. inactive (equal (substring string beg (1+ beg)) "[")
  14554. tf (funcall (if with-hm 'cdr 'car)
  14555. org-time-stamp-custom-formats)
  14556. time (org-fix-decoded-time t1)
  14557. str (format-time-string
  14558. (concat
  14559. (if inactive "[" "<") (substring tf 1 -1)
  14560. (if inactive "]" ">"))
  14561. (apply 'encode-time time))
  14562. string (replace-match str t t string)
  14563. start (+ start (length str)))))))
  14564. string)
  14565. (defun org-fix-decoded-time (time)
  14566. "Set 0 instead of nil for the first 6 elements of time.
  14567. Don't touch the rest."
  14568. (let ((n 0))
  14569. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  14570. (define-obsolete-function-alias 'org-days-to-time 'org-time-stamp-to-now "24.3")
  14571. (defun org-time-stamp-to-now (timestamp-string &optional seconds)
  14572. "Difference between TIMESTAMP-STRING and now in days.
  14573. If SECONDS is non-nil, return the difference in seconds."
  14574. (let ((fdiff (if seconds 'org-float-time 'time-to-days)))
  14575. (- (funcall fdiff (org-time-string-to-time timestamp-string))
  14576. (funcall fdiff (current-time)))))
  14577. (defun org-deadline-close (timestamp-string &optional ndays)
  14578. "Is the time in TIMESTAMP-STRING close to the current date?"
  14579. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  14580. (and (< (org-time-stamp-to-now timestamp-string) ndays)
  14581. (not (org-entry-is-done-p))))
  14582. (defun org-get-wdays (ts)
  14583. "Get the deadline lead time appropriate for timestring TS."
  14584. (cond
  14585. ((<= org-deadline-warning-days 0)
  14586. ;; 0 or negative, enforce this value no matter what
  14587. (- org-deadline-warning-days))
  14588. ((string-match "-\\([0-9]+\\)\\([hdwmy]\\)\\(\\'\\|>\\| \\)" ts)
  14589. ;; lead time is specified.
  14590. (floor (* (string-to-number (match-string 1 ts))
  14591. (cdr (assoc (match-string 2 ts)
  14592. '(("d" . 1) ("w" . 7)
  14593. ("m" . 30.4) ("y" . 365.25)))))))
  14594. ;; go for the default.
  14595. (t org-deadline-warning-days)))
  14596. (defun org-calendar-select-mouse (ev)
  14597. "Return to `org-read-date' with the date currently selected.
  14598. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  14599. (interactive "e")
  14600. (mouse-set-point ev)
  14601. (when (calendar-cursor-to-date)
  14602. (let* ((date (calendar-cursor-to-date))
  14603. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14604. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  14605. (if (active-minibuffer-window) (exit-minibuffer))))
  14606. (defun org-check-deadlines (ndays)
  14607. "Check if there are any deadlines due or past due.
  14608. A deadline is considered due if it happens within `org-deadline-warning-days'
  14609. days from today's date. If the deadline appears in an entry marked DONE,
  14610. it is not shown. The prefix arg NDAYS can be used to test that many
  14611. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  14612. (interactive "P")
  14613. (let* ((org-warn-days
  14614. (cond
  14615. ((equal ndays '(4)) 100000)
  14616. (ndays (prefix-numeric-value ndays))
  14617. (t (abs org-deadline-warning-days))))
  14618. (case-fold-search nil)
  14619. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  14620. (callback
  14621. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  14622. (message "%d deadlines past-due or due within %d days"
  14623. (org-occur regexp nil callback)
  14624. org-warn-days)))
  14625. (defsubst org-re-timestamp (type)
  14626. "Return a regexp for timestamp TYPE.
  14627. Allowed values for TYPE are:
  14628. all: all timestamps
  14629. active: only active timestamps (<...>)
  14630. inactive: only inactive timestamps ([...])
  14631. scheduled: only scheduled timestamps
  14632. deadline: only deadline timestamps
  14633. When TYPE is nil, fall back on returning a regexp that matches
  14634. both scheduled and deadline timestamps."
  14635. (cond ((eq type 'all) "\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}\\(?: +[^]+0-9> \n -]+\\)?\\(?: +[0-9]\\{1,2\\}:[0-9]\\{2\\}\\)?\\)")
  14636. ((eq type 'active) org-ts-regexp)
  14637. ((eq type 'inactive) "\\[\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^ \n>]*?\\)\\]")
  14638. ((eq type 'scheduled) (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>"))
  14639. ((eq type 'deadline) (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  14640. ((eq type 'scheduled-or-deadline)
  14641. (concat "\\<\\(?:" org-deadline-string "\\|" org-scheduled-string "\\) *<\\([^>]+\\)>"))))
  14642. (defun org-check-before-date (date)
  14643. "Check if there are deadlines or scheduled entries before DATE."
  14644. (interactive (list (org-read-date)))
  14645. (let ((case-fold-search nil)
  14646. (regexp (org-re-timestamp org-ts-type))
  14647. (callback
  14648. (lambda () (time-less-p
  14649. (org-time-string-to-time (match-string 1))
  14650. (org-time-string-to-time date)))))
  14651. (message "%d entries before %s"
  14652. (org-occur regexp nil callback) date)))
  14653. (defun org-check-after-date (date)
  14654. "Check if there are deadlines or scheduled entries after DATE."
  14655. (interactive (list (org-read-date)))
  14656. (let ((case-fold-search nil)
  14657. (regexp (org-re-timestamp org-ts-type))
  14658. (callback
  14659. (lambda () (not
  14660. (time-less-p
  14661. (org-time-string-to-time (match-string 1))
  14662. (org-time-string-to-time date))))))
  14663. (message "%d entries after %s"
  14664. (org-occur regexp nil callback) date)))
  14665. (defun org-check-dates-range (start-date end-date)
  14666. "Check for deadlines/scheduled entries between START-DATE and END-DATE."
  14667. (interactive (list (org-read-date nil nil nil "Range starts")
  14668. (org-read-date nil nil nil "Range end")))
  14669. (let ((case-fold-search nil)
  14670. (regexp (org-re-timestamp org-ts-type))
  14671. (callback
  14672. (lambda ()
  14673. (let ((match (match-string 1)))
  14674. (and
  14675. (not (time-less-p
  14676. (org-time-string-to-time match)
  14677. (org-time-string-to-time start-date)))
  14678. (time-less-p
  14679. (org-time-string-to-time match)
  14680. (org-time-string-to-time end-date)))))))
  14681. (message "%d entries between %s and %s"
  14682. (org-occur regexp nil callback) start-date end-date)))
  14683. (defun org-evaluate-time-range (&optional to-buffer)
  14684. "Evaluate a time range by computing the difference between start and end.
  14685. Normally the result is just printed in the echo area, but with prefix arg
  14686. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  14687. If the time range is actually in a table, the result is inserted into the
  14688. next column.
  14689. For time difference computation, a year is assumed to be exactly 365
  14690. days in order to avoid rounding problems."
  14691. (interactive "P")
  14692. (or
  14693. (org-clock-update-time-maybe)
  14694. (save-excursion
  14695. (unless (org-at-date-range-p t)
  14696. (goto-char (point-at-bol))
  14697. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  14698. (if (not (org-at-date-range-p t))
  14699. (error "Not at a time-stamp range, and none found in current line")))
  14700. (let* ((ts1 (match-string 1))
  14701. (ts2 (match-string 2))
  14702. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  14703. (match-end (match-end 0))
  14704. (time1 (org-time-string-to-time ts1))
  14705. (time2 (org-time-string-to-time ts2))
  14706. (t1 (org-float-time time1))
  14707. (t2 (org-float-time time2))
  14708. (diff (abs (- t2 t1)))
  14709. (negative (< (- t2 t1) 0))
  14710. ;; (ys (floor (* 365 24 60 60)))
  14711. (ds (* 24 60 60))
  14712. (hs (* 60 60))
  14713. (fy "%dy %dd %02d:%02d")
  14714. (fy1 "%dy %dd")
  14715. (fd "%dd %02d:%02d")
  14716. (fd1 "%dd")
  14717. (fh "%02d:%02d")
  14718. y d h m align)
  14719. (if havetime
  14720. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  14721. y 0
  14722. d (floor (/ diff ds)) diff (mod diff ds)
  14723. h (floor (/ diff hs)) diff (mod diff hs)
  14724. m (floor (/ diff 60)))
  14725. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  14726. y 0
  14727. d (floor (+ (/ diff ds) 0.5))
  14728. h 0 m 0))
  14729. (if (not to-buffer)
  14730. (message "%s" (org-make-tdiff-string y d h m))
  14731. (if (org-at-table-p)
  14732. (progn
  14733. (goto-char match-end)
  14734. (setq align t)
  14735. (and (looking-at " *|") (goto-char (match-end 0))))
  14736. (goto-char match-end))
  14737. (if (looking-at
  14738. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  14739. (replace-match ""))
  14740. (if negative (insert " -"))
  14741. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  14742. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  14743. (insert " " (format fh h m))))
  14744. (if align (org-table-align))
  14745. (message "Time difference inserted")))))
  14746. (defun org-make-tdiff-string (y d h m)
  14747. (let ((fmt "")
  14748. (l nil))
  14749. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  14750. l (push y l)))
  14751. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  14752. l (push d l)))
  14753. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  14754. l (push h l)))
  14755. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  14756. l (push m l)))
  14757. (apply 'format fmt (nreverse l))))
  14758. (defun org-time-string-to-time (s &optional buffer pos)
  14759. "Convert a timestamp string into internal time."
  14760. (condition-case errdata
  14761. (apply 'encode-time (org-parse-time-string s))
  14762. (error (error "Bad timestamp `%s'%s\nError was: %s"
  14763. s (if (not (and buffer pos))
  14764. ""
  14765. (format " at %d in buffer `%s'" pos buffer))
  14766. (cdr errdata)))))
  14767. (defun org-time-string-to-seconds (s)
  14768. "Convert a timestamp string to a number of seconds."
  14769. (org-float-time (org-time-string-to-time s)))
  14770. (defun org-time-string-to-absolute (s &optional daynr prefer show-all buffer pos)
  14771. "Convert a time stamp to an absolute day number.
  14772. If there is a specifier for a cyclic time stamp, get the closest date to
  14773. DAYNR.
  14774. PREFER and SHOW-ALL are passed through to `org-closest-date'.
  14775. The variable date is bound by the calendar when this is called."
  14776. (cond
  14777. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  14778. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  14779. daynr
  14780. (+ daynr 1000)))
  14781. ((and daynr (string-match "\\+[0-9]+[hdwmy]" s))
  14782. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  14783. (time-to-days (current-time))) (match-string 0 s)
  14784. prefer show-all))
  14785. (t (time-to-days
  14786. (condition-case errdata
  14787. (apply 'encode-time (org-parse-time-string s))
  14788. (error (error "Bad timestamp `%s'%s\nError was: %s"
  14789. s (if (not (and buffer pos))
  14790. ""
  14791. (format " at %d in buffer `%s'" pos buffer))
  14792. (cdr errdata))))))))
  14793. (defun org-days-to-iso-week (days)
  14794. "Return the iso week number."
  14795. (require 'cal-iso)
  14796. (car (calendar-iso-from-absolute days)))
  14797. (defun org-small-year-to-year (year)
  14798. "Convert 2-digit years into 4-digit years.
  14799. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  14800. The year 2000 cannot be abbreviated. Any year larger than 99
  14801. is returned unchanged."
  14802. (if (< year 38)
  14803. (setq year (+ 2000 year))
  14804. (if (< year 100)
  14805. (setq year (+ 1900 year))))
  14806. year)
  14807. (defun org-time-from-absolute (d)
  14808. "Return the time corresponding to date D.
  14809. D may be an absolute day number, or a calendar-type list (month day year)."
  14810. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  14811. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  14812. (defun org-calendar-holiday ()
  14813. "List of holidays, for Diary display in Org-mode."
  14814. (require 'holidays)
  14815. (let ((hl (funcall
  14816. (if (fboundp 'calendar-check-holidays)
  14817. 'calendar-check-holidays 'check-calendar-holidays) date)))
  14818. (if hl (mapconcat 'identity hl "; "))))
  14819. (defun org-diary-sexp-entry (sexp entry date)
  14820. "Process a SEXP diary ENTRY for DATE."
  14821. (require 'diary-lib)
  14822. (let ((result (if calendar-debug-sexp
  14823. (let ((stack-trace-on-error t))
  14824. (eval (car (read-from-string sexp))))
  14825. (condition-case nil
  14826. (eval (car (read-from-string sexp)))
  14827. (error
  14828. (beep)
  14829. (message "Bad sexp at line %d in %s: %s"
  14830. (org-current-line)
  14831. (buffer-file-name) sexp)
  14832. (sleep-for 2))))))
  14833. (cond ((stringp result) (split-string result "; "))
  14834. ((and (consp result)
  14835. (not (consp (cdr result)))
  14836. (stringp (cdr result))) (cdr result))
  14837. ((and (consp result)
  14838. (stringp (car result))) result)
  14839. (result entry))))
  14840. (defun org-diary-to-ical-string (frombuf)
  14841. "Get iCalendar entries from diary entries in buffer FROMBUF.
  14842. This uses the icalendar.el library."
  14843. (let* ((tmpdir (if (featurep 'xemacs)
  14844. (temp-directory)
  14845. temporary-file-directory))
  14846. (tmpfile (make-temp-name
  14847. (expand-file-name "orgics" tmpdir)))
  14848. buf rtn b e)
  14849. (with-current-buffer frombuf
  14850. (icalendar-export-region (point-min) (point-max) tmpfile)
  14851. (setq buf (find-buffer-visiting tmpfile))
  14852. (set-buffer buf)
  14853. (goto-char (point-min))
  14854. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  14855. (setq b (match-beginning 0)))
  14856. (goto-char (point-max))
  14857. (if (re-search-backward "^END:VEVENT" nil t)
  14858. (setq e (match-end 0)))
  14859. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  14860. (kill-buffer buf)
  14861. (delete-file tmpfile)
  14862. rtn))
  14863. (defun org-closest-date (start current change prefer show-all)
  14864. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  14865. When PREFER is `past', return a date that is either CURRENT or past.
  14866. When PREFER is `future', return a date that is either CURRENT or future.
  14867. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  14868. ;; Make the proper lists from the dates
  14869. (catch 'exit
  14870. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  14871. dn dw sday cday n1 n2 n0
  14872. d m y y1 y2 date1 date2 nmonths nm ny m2)
  14873. (setq start (org-date-to-gregorian start)
  14874. current (org-date-to-gregorian
  14875. (if show-all
  14876. current
  14877. (time-to-days (current-time))))
  14878. sday (calendar-absolute-from-gregorian start)
  14879. cday (calendar-absolute-from-gregorian current))
  14880. (if (<= cday sday) (throw 'exit sday))
  14881. (if (string-match "\\(\\+[0-9]+\\)\\([hdwmy]\\)" change)
  14882. (setq dn (string-to-number (match-string 1 change))
  14883. dw (cdr (assoc (match-string 2 change) a1)))
  14884. (error "Invalid change specifier: %s" change))
  14885. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  14886. (cond
  14887. ((eq dw 'day)
  14888. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  14889. n2 (+ n1 dn)))
  14890. ((eq dw 'year)
  14891. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  14892. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  14893. (setq date1 (list m d y1)
  14894. n1 (calendar-absolute-from-gregorian date1)
  14895. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  14896. n2 (calendar-absolute-from-gregorian date2)))
  14897. ((eq dw 'month)
  14898. ;; approx number of month between the two dates
  14899. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  14900. ;; How often does dn fit in there?
  14901. (setq d (nth 1 start) m (car start) y (nth 2 start)
  14902. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  14903. m (+ m nm)
  14904. ny (floor (/ m 12))
  14905. y (+ y ny)
  14906. m (- m (* ny 12)))
  14907. (while (> m 12) (setq m (- m 12) y (1+ y)))
  14908. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  14909. (setq m2 (+ m dn) y2 y)
  14910. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  14911. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  14912. (while (<= n2 cday)
  14913. (setq n1 n2 m m2 y y2)
  14914. (setq m2 (+ m dn) y2 y)
  14915. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  14916. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  14917. ;; Make sure n1 is the earlier date
  14918. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  14919. (if show-all
  14920. (cond
  14921. ((eq prefer 'past) (if (= cday n2) n2 n1))
  14922. ((eq prefer 'future) (if (= cday n1) n1 n2))
  14923. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  14924. (cond
  14925. ((eq prefer 'past) (if (= cday n2) n2 n1))
  14926. ((eq prefer 'future) (if (= cday n1) n1 n2))
  14927. (t (if (= cday n1) n1 n2)))))))
  14928. (defun org-date-to-gregorian (date)
  14929. "Turn any specification of DATE into a Gregorian date for the calendar."
  14930. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  14931. ((and (listp date) (= (length date) 3)) date)
  14932. ((stringp date)
  14933. (setq date (org-parse-time-string date))
  14934. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  14935. ((listp date)
  14936. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  14937. (defun org-parse-time-string (s &optional nodefault)
  14938. "Parse the standard Org-mode time string.
  14939. This should be a lot faster than the normal `parse-time-string'.
  14940. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  14941. hour and minute fields will be nil if not given."
  14942. (if (string-match org-ts-regexp0 s)
  14943. (list 0
  14944. (if (or (match-beginning 8) (not nodefault))
  14945. (string-to-number (or (match-string 8 s) "0")))
  14946. (if (or (match-beginning 7) (not nodefault))
  14947. (string-to-number (or (match-string 7 s) "0")))
  14948. (string-to-number (match-string 4 s))
  14949. (string-to-number (match-string 3 s))
  14950. (string-to-number (match-string 2 s))
  14951. nil nil nil)
  14952. (error "Not a standard Org-mode time string: %s" s)))
  14953. (defun org-timestamp-up (&optional arg)
  14954. "Increase the date item at the cursor by one.
  14955. If the cursor is on the year, change the year. If it is on the month,
  14956. the day or the time, change that.
  14957. With prefix ARG, change by that many units."
  14958. (interactive "p")
  14959. (org-timestamp-change (prefix-numeric-value arg) nil 'updown))
  14960. (defun org-timestamp-down (&optional arg)
  14961. "Decrease the date item at the cursor by one.
  14962. If the cursor is on the year, change the year. If it is on the month,
  14963. the day or the time, change that.
  14964. With prefix ARG, change by that many units."
  14965. (interactive "p")
  14966. (org-timestamp-change (- (prefix-numeric-value arg)) nil 'updown))
  14967. (defun org-timestamp-up-day (&optional arg)
  14968. "Increase the date in the time stamp by one day.
  14969. With prefix ARG, change that many days."
  14970. (interactive "p")
  14971. (if (and (not (org-at-timestamp-p t))
  14972. (org-at-heading-p))
  14973. (org-todo 'up)
  14974. (org-timestamp-change (prefix-numeric-value arg) 'day 'updown)))
  14975. (defun org-timestamp-down-day (&optional arg)
  14976. "Decrease the date in the time stamp by one day.
  14977. With prefix ARG, change that many days."
  14978. (interactive "p")
  14979. (if (and (not (org-at-timestamp-p t))
  14980. (org-at-heading-p))
  14981. (org-todo 'down)
  14982. (org-timestamp-change (- (prefix-numeric-value arg)) 'day) 'updown))
  14983. (defun org-at-timestamp-p (&optional inactive-ok)
  14984. "Determine if the cursor is in or at a timestamp."
  14985. (interactive)
  14986. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  14987. (pos (point))
  14988. (ans (or (looking-at tsr)
  14989. (save-excursion
  14990. (skip-chars-backward "^[<\n\r\t")
  14991. (if (> (point) (point-min)) (backward-char 1))
  14992. (and (looking-at tsr)
  14993. (> (- (match-end 0) pos) -1))))))
  14994. (and ans
  14995. (boundp 'org-ts-what)
  14996. (setq org-ts-what
  14997. (cond
  14998. ((= pos (match-beginning 0)) 'bracket)
  14999. ;; Point is considered to be "on the bracket" whether
  15000. ;; it's really on it or right after it.
  15001. ((= pos (1- (match-end 0))) 'bracket)
  15002. ((= pos (match-end 0)) 'after)
  15003. ((org-pos-in-match-range pos 2) 'year)
  15004. ((org-pos-in-match-range pos 3) 'month)
  15005. ((org-pos-in-match-range pos 7) 'hour)
  15006. ((org-pos-in-match-range pos 8) 'minute)
  15007. ((or (org-pos-in-match-range pos 4)
  15008. (org-pos-in-match-range pos 5)) 'day)
  15009. ((and (> pos (or (match-end 8) (match-end 5)))
  15010. (< pos (match-end 0)))
  15011. (- pos (or (match-end 8) (match-end 5))))
  15012. (t 'day))))
  15013. ans))
  15014. (defun org-toggle-timestamp-type ()
  15015. "Toggle the type (<active> or [inactive]) of a time stamp."
  15016. (interactive)
  15017. (when (org-at-timestamp-p t)
  15018. (let ((beg (match-beginning 0)) (end (match-end 0))
  15019. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  15020. (save-excursion
  15021. (goto-char beg)
  15022. (while (re-search-forward "[][<>]" end t)
  15023. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  15024. t t)))
  15025. (message "Timestamp is now %sactive"
  15026. (if (equal (char-after beg) ?<) "" "in")))))
  15027. (defvar org-clock-history) ; defined in org-clock.el
  15028. (defvar org-clock-adjust-closest nil) ; defined in org-clock.el
  15029. (defun org-timestamp-change (n &optional what updown)
  15030. "Change the date in the time stamp at point.
  15031. The date will be changed by N times WHAT. WHAT can be `day', `month',
  15032. `year', `minute', `second'. If WHAT is not given, the cursor position
  15033. in the timestamp determines what will be changed."
  15034. (let ((origin (point)) origin-cat
  15035. with-hm inactive
  15036. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  15037. org-ts-what
  15038. extra rem
  15039. ts time time0 fixnext clrgx)
  15040. (if (not (org-at-timestamp-p t))
  15041. (error "Not at a timestamp"))
  15042. (if (and (not what) (eq org-ts-what 'bracket))
  15043. (org-toggle-timestamp-type)
  15044. ;; Point isn't on brackets. Remember the part of the time-stamp
  15045. ;; the point was in. Indeed, size of time-stamps may change,
  15046. ;; but point must be kept in the same category nonetheless.
  15047. (setq origin-cat org-ts-what)
  15048. (if (and (not what) (not (eq org-ts-what 'day))
  15049. org-display-custom-times
  15050. (get-text-property (point) 'display)
  15051. (not (get-text-property (1- (point)) 'display)))
  15052. (setq org-ts-what 'day))
  15053. (setq org-ts-what (or what org-ts-what)
  15054. inactive (= (char-after (match-beginning 0)) ?\[)
  15055. ts (match-string 0))
  15056. (replace-match "")
  15057. (if (string-match
  15058. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)*\\)[]>]"
  15059. ts)
  15060. (setq extra (match-string 1 ts)))
  15061. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  15062. (setq with-hm t))
  15063. (setq time0 (org-parse-time-string ts))
  15064. (when (and updown
  15065. (eq org-ts-what 'minute)
  15066. (not current-prefix-arg))
  15067. ;; This looks like s-up and s-down. Change by one rounding step.
  15068. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  15069. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  15070. (setcar (cdr time0) (+ (nth 1 time0)
  15071. (if (> n 0) (- rem) (- dm rem))))))
  15072. (setq time
  15073. (encode-time (or (car time0) 0)
  15074. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  15075. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  15076. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  15077. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  15078. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  15079. (nthcdr 6 time0)))
  15080. (when (and (member org-ts-what '(hour minute))
  15081. extra
  15082. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  15083. (setq extra (org-modify-ts-extra
  15084. extra
  15085. (if (eq org-ts-what 'hour) 2 5)
  15086. n dm)))
  15087. (when (integerp org-ts-what)
  15088. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  15089. (if (eq what 'calendar)
  15090. (let ((cal-date (org-get-date-from-calendar)))
  15091. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  15092. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  15093. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  15094. (setcar time0 (or (car time0) 0))
  15095. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  15096. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  15097. (setq time (apply 'encode-time time0))))
  15098. ;; Insert the new time-stamp, and ensure point stays in the same
  15099. ;; category as before (i.e. not after the last position in that
  15100. ;; category).
  15101. (let ((pos (point)))
  15102. ;; Stay before inserted string. `save-excursion' is of no use.
  15103. (setq org-last-changed-timestamp
  15104. (org-insert-time-stamp time with-hm inactive nil nil extra))
  15105. (goto-char pos))
  15106. (save-match-data
  15107. (looking-at org-ts-regexp3)
  15108. (goto-char (cond
  15109. ;; `day' category ends before `hour' if any, or at
  15110. ;; the end of the day name.
  15111. ((eq origin-cat 'day)
  15112. (min (or (match-beginning 7) (1- (match-end 5))) origin))
  15113. ((eq origin-cat 'hour) (min (match-end 7) origin))
  15114. ((eq origin-cat 'minute) (min (1- (match-end 8)) origin))
  15115. ((integerp origin-cat) (min (1- (match-end 0)) origin))
  15116. ;; `year' and `month' have both fixed size: point
  15117. ;; couldn't have moved into another part.
  15118. (t origin))))
  15119. ;; Update clock if on a CLOCK line.
  15120. (org-clock-update-time-maybe)
  15121. ;; Maybe adjust the closest clock in `org-clock-history'
  15122. (when org-clock-adjust-closest
  15123. (if (not (and (org-at-clock-log-p)
  15124. (< 1 (length (delq nil (mapcar (lambda(m) (marker-position m))
  15125. org-clock-history))))))
  15126. (message "No clock to adjust")
  15127. (cond ((save-excursion ; fix previous clock?
  15128. (re-search-backward org-ts-regexp0 nil t)
  15129. (org-looking-back (concat org-clock-string " \\[")))
  15130. (setq fixnext 1 clrgx (concat org-ts-regexp0 "\\] =>.*$")))
  15131. ((save-excursion ; fix next clock?
  15132. (re-search-backward org-ts-regexp0 nil t)
  15133. (looking-at (concat org-ts-regexp0 "\\] =>")))
  15134. (setq fixnext -1 clrgx (concat org-clock-string " \\[" org-ts-regexp0))))
  15135. (save-window-excursion
  15136. ;; Find closest clock to point, adjust the previous/next one in history
  15137. (let* ((p (save-excursion (org-back-to-heading t)))
  15138. (cl (mapcar (lambda(c) (abs (- (marker-position c) p))) org-clock-history))
  15139. (clfixnth
  15140. (+ fixnext (- (length cl) (or (length (member (apply #'min cl) cl)) 100))))
  15141. (clfixpos (if (> 0 clfixnth) nil (nth clfixnth org-clock-history))))
  15142. (if (not clfixpos)
  15143. (message "No clock to adjust")
  15144. (save-excursion
  15145. (org-goto-marker-or-bmk clfixpos)
  15146. (org-show-subtree)
  15147. (when (re-search-forward clrgx nil t)
  15148. (goto-char (match-beginning 1))
  15149. (let (org-clock-adjust-closest)
  15150. (org-timestamp-change n org-ts-what updown))
  15151. (message "Clock adjusted in %s for heading: %s"
  15152. (file-name-nondirectory (buffer-file-name))
  15153. (org-get-heading t t)))))))))
  15154. ;; Try to recenter the calendar window, if any.
  15155. (if (and org-calendar-follow-timestamp-change
  15156. (get-buffer-window "*Calendar*" t)
  15157. (memq org-ts-what '(day month year)))
  15158. (org-recenter-calendar (time-to-days time))))))
  15159. (defun org-modify-ts-extra (s pos n dm)
  15160. "Change the different parts of the lead-time and repeat fields in timestamp."
  15161. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  15162. ng h m new rem)
  15163. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  15164. (cond
  15165. ((or (org-pos-in-match-range pos 2)
  15166. (org-pos-in-match-range pos 3))
  15167. (setq m (string-to-number (match-string 3 s))
  15168. h (string-to-number (match-string 2 s)))
  15169. (if (org-pos-in-match-range pos 2)
  15170. (setq h (+ h n))
  15171. (setq n (* dm (org-no-warnings (signum n))))
  15172. (when (not (= 0 (setq rem (% m dm))))
  15173. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  15174. (setq m (+ m n)))
  15175. (if (< m 0) (setq m (+ m 60) h (1- h)))
  15176. (if (> m 59) (setq m (- m 60) h (1+ h)))
  15177. (setq h (min 24 (max 0 h)))
  15178. (setq ng 1 new (format "-%02d:%02d" h m)))
  15179. ((org-pos-in-match-range pos 6)
  15180. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  15181. ((org-pos-in-match-range pos 5)
  15182. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  15183. ((org-pos-in-match-range pos 9)
  15184. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  15185. ((org-pos-in-match-range pos 8)
  15186. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  15187. (when ng
  15188. (setq s (concat
  15189. (substring s 0 (match-beginning ng))
  15190. new
  15191. (substring s (match-end ng))))))
  15192. s))
  15193. (defun org-recenter-calendar (date)
  15194. "If the calendar is visible, recenter it to DATE."
  15195. (let ((cwin (get-buffer-window "*Calendar*" t)))
  15196. (when cwin
  15197. (let ((calendar-move-hook nil))
  15198. (with-selected-window cwin
  15199. (calendar-goto-date (if (listp date) date
  15200. (calendar-gregorian-from-absolute date))))))))
  15201. (defun org-goto-calendar (&optional arg)
  15202. "Go to the Emacs calendar at the current date.
  15203. If there is a time stamp in the current line, go to that date.
  15204. A prefix ARG can be used to force the current date."
  15205. (interactive "P")
  15206. (let ((tsr org-ts-regexp) diff
  15207. (calendar-move-hook nil)
  15208. (calendar-view-holidays-initially-flag nil)
  15209. (calendar-view-diary-initially-flag nil))
  15210. (if (or (org-at-timestamp-p)
  15211. (save-excursion
  15212. (beginning-of-line 1)
  15213. (looking-at (concat ".*" tsr))))
  15214. (let ((d1 (time-to-days (current-time)))
  15215. (d2 (time-to-days
  15216. (org-time-string-to-time (match-string 1)))))
  15217. (setq diff (- d2 d1))))
  15218. (calendar)
  15219. (calendar-goto-today)
  15220. (if (and diff (not arg)) (calendar-forward-day diff))))
  15221. (defun org-get-date-from-calendar ()
  15222. "Return a list (month day year) of date at point in calendar."
  15223. (with-current-buffer "*Calendar*"
  15224. (save-match-data
  15225. (calendar-cursor-to-date))))
  15226. (defun org-date-from-calendar ()
  15227. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  15228. If there is already a time stamp at the cursor position, update it."
  15229. (interactive)
  15230. (if (org-at-timestamp-p t)
  15231. (org-timestamp-change 0 'calendar)
  15232. (let ((cal-date (org-get-date-from-calendar)))
  15233. (org-insert-time-stamp
  15234. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  15235. (defun org-minutes-to-hh:mm-string (m)
  15236. "Compute H:MM from a number of minutes."
  15237. (let ((h (/ m 60)))
  15238. (setq m (- m (* 60 h)))
  15239. (format org-time-clocksum-format h m)))
  15240. (defun org-hh:mm-string-to-minutes (s)
  15241. "Convert a string H:MM to a number of minutes.
  15242. If the string is just a number, interpret it as minutes.
  15243. In fact, the first hh:mm or number in the string will be taken,
  15244. there can be extra stuff in the string.
  15245. If no number is found, the return value is 0."
  15246. (cond
  15247. ((integerp s) s)
  15248. ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  15249. (+ (* (string-to-number (match-string 1 s)) 60)
  15250. (string-to-number (match-string 2 s))))
  15251. ((string-match "\\([0-9]+\\)" s)
  15252. (string-to-number (match-string 1 s)))
  15253. (t 0)))
  15254. (defcustom org-effort-durations
  15255. `(("h" . 60)
  15256. ("d" . ,(* 60 8))
  15257. ("w" . ,(* 60 8 5))
  15258. ("m" . ,(* 60 8 5 4))
  15259. ("y" . ,(* 60 8 5 40)))
  15260. "Conversion factor to minutes for an effort modifier.
  15261. Each entry has the form (MODIFIER . MINUTES).
  15262. In an effort string, a number followed by MODIFIER is multiplied
  15263. by the specified number of MINUTES to obtain an effort in
  15264. minutes.
  15265. For example, if the value of this variable is ((\"hours\" . 60)), then an
  15266. effort string \"2hours\" is equivalent to 120 minutes."
  15267. :group 'org-agenda
  15268. :version "24.1"
  15269. :type '(alist :key-type (string :tag "Modifier")
  15270. :value-type (number :tag "Minutes")))
  15271. (defcustom org-image-actual-width t
  15272. "Should we use the actual width of images when inlining them?
  15273. When set to `t', always use the image width.
  15274. When set to a number, use imagemagick (when available) to set
  15275. the image's width to this value.
  15276. When set to a number in a list, try to get the width from the
  15277. #+ATTR.* keyword if it matches a width specification like
  15278. width=\"[0-9]+\", and fall back on that number if none is found.
  15279. When set to nil, try to get the width from an #+ATTR.* keyword
  15280. and fall back on the original width if none is found.
  15281. This requires Emacs >= 24.1, build with imagemagick support."
  15282. :group 'org-appearance
  15283. :version "24.3"
  15284. :type '(choice
  15285. (const :tag "Use the image width" t)
  15286. (integer :tag "Use a number of pixels")
  15287. (list :tag "Use #+ATTR* or a number of pixels" (integer))
  15288. (const :tag "Use #+ATTR* or don't resize" nil)))
  15289. (defun org-duration-string-to-minutes (s &optional output-to-string)
  15290. "Convert a duration string S to minutes.
  15291. A bare number is interpreted as minutes, modifiers can be set by
  15292. customizing `org-effort-durations' (which see).
  15293. Entries containing a colon are interpreted as H:MM by
  15294. `org-hh:mm-string-to-minutes'."
  15295. (let ((result 0)
  15296. (re (concat "\\([0-9.]+\\) *\\("
  15297. (regexp-opt (mapcar 'car org-effort-durations))
  15298. "\\)")))
  15299. (while (string-match re s)
  15300. (incf result (* (cdr (assoc (match-string 2 s) org-effort-durations))
  15301. (string-to-number (match-string 1 s))))
  15302. (setq s (replace-match "" nil t s)))
  15303. (setq result (floor result))
  15304. (incf result (org-hh:mm-string-to-minutes s))
  15305. (if output-to-string (number-to-string result) result)))
  15306. ;;;; Files
  15307. (defun org-save-all-org-buffers ()
  15308. "Save all Org-mode buffers without user confirmation."
  15309. (interactive)
  15310. (message "Saving all Org-mode buffers...")
  15311. (save-some-buffers t (lambda () (derived-mode-p 'org-mode)))
  15312. (when (featurep 'org-id) (org-id-locations-save))
  15313. (message "Saving all Org-mode buffers... done"))
  15314. (defun org-revert-all-org-buffers ()
  15315. "Revert all Org-mode buffers.
  15316. Prompt for confirmation when there are unsaved changes.
  15317. Be sure you know what you are doing before letting this function
  15318. overwrite your changes.
  15319. This function is useful in a setup where one tracks org files
  15320. with a version control system, to revert on one machine after pulling
  15321. changes from another. I believe the procedure must be like this:
  15322. 1. M-x org-save-all-org-buffers
  15323. 2. Pull changes from the other machine, resolve conflicts
  15324. 3. M-x org-revert-all-org-buffers"
  15325. (interactive)
  15326. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  15327. (error "Abort"))
  15328. (save-excursion
  15329. (save-window-excursion
  15330. (mapc
  15331. (lambda (b)
  15332. (when (and (with-current-buffer b (derived-mode-p 'org-mode))
  15333. (with-current-buffer b buffer-file-name))
  15334. (org-pop-to-buffer-same-window b)
  15335. (revert-buffer t 'no-confirm)))
  15336. (buffer-list))
  15337. (when (and (featurep 'org-id) org-id-track-globally)
  15338. (org-id-locations-load)))))
  15339. ;;;; Agenda files
  15340. ;;;###autoload
  15341. (defun org-switchb (&optional arg)
  15342. "Switch between Org buffers.
  15343. With one prefix argument, restrict available buffers to files.
  15344. With two prefix arguments, restrict available buffers to agenda files.
  15345. Defaults to `iswitchb' for buffer name completion.
  15346. Set `org-completion-use-ido' to make it use ido instead."
  15347. (interactive "P")
  15348. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  15349. ((equal arg '(16)) (org-buffer-list 'agenda))
  15350. (t (org-buffer-list))))
  15351. (org-completion-use-iswitchb org-completion-use-iswitchb)
  15352. (org-completion-use-ido org-completion-use-ido))
  15353. (unless (or org-completion-use-ido org-completion-use-iswitchb)
  15354. (setq org-completion-use-iswitchb t))
  15355. (org-pop-to-buffer-same-window
  15356. (org-icompleting-read "Org buffer: "
  15357. (mapcar 'list (mapcar 'buffer-name blist))
  15358. nil t))))
  15359. ;;; Define some older names previously used for this functionality
  15360. ;;;###autoload
  15361. (defalias 'org-ido-switchb 'org-switchb)
  15362. ;;;###autoload
  15363. (defalias 'org-iswitchb 'org-switchb)
  15364. (defun org-buffer-list (&optional predicate exclude-tmp)
  15365. "Return a list of Org buffers.
  15366. PREDICATE can be `export', `files' or `agenda'.
  15367. export restrict the list to Export buffers.
  15368. files restrict the list to buffers visiting Org files.
  15369. agenda restrict the list to buffers visiting agenda files.
  15370. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  15371. (let* ((bfn nil)
  15372. (agenda-files (and (eq predicate 'agenda)
  15373. (mapcar 'file-truename (org-agenda-files t))))
  15374. (filter
  15375. (cond
  15376. ((eq predicate 'files)
  15377. (lambda (b) (with-current-buffer b (derived-mode-p 'org-mode))))
  15378. ((eq predicate 'export)
  15379. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  15380. ((eq predicate 'agenda)
  15381. (lambda (b)
  15382. (with-current-buffer b
  15383. (and (derived-mode-p 'org-mode)
  15384. (setq bfn (buffer-file-name b))
  15385. (member (file-truename bfn) agenda-files)))))
  15386. (t (lambda (b) (with-current-buffer b
  15387. (or (derived-mode-p 'org-mode)
  15388. (string-match "\*Org .*Export"
  15389. (buffer-name b)))))))))
  15390. (delq nil
  15391. (mapcar
  15392. (lambda(b)
  15393. (if (and (funcall filter b)
  15394. (or (not exclude-tmp)
  15395. (not (string-match "tmp" (buffer-name b)))))
  15396. b
  15397. nil))
  15398. (buffer-list)))))
  15399. (defun org-agenda-files (&optional unrestricted archives)
  15400. "Get the list of agenda files.
  15401. Optional UNRESTRICTED means return the full list even if a restriction
  15402. is currently in place.
  15403. When ARCHIVES is t, include all archive files that are really being
  15404. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  15405. `org-agenda-archives-mode' is t."
  15406. (let ((files
  15407. (cond
  15408. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  15409. ((stringp org-agenda-files) (org-read-agenda-file-list))
  15410. ((listp org-agenda-files) org-agenda-files)
  15411. (t (error "Invalid value of `org-agenda-files'")))))
  15412. (setq files (apply 'append
  15413. (mapcar (lambda (f)
  15414. (if (file-directory-p f)
  15415. (directory-files
  15416. f t org-agenda-file-regexp)
  15417. (list f)))
  15418. files)))
  15419. (when org-agenda-skip-unavailable-files
  15420. (setq files (delq nil
  15421. (mapcar (function
  15422. (lambda (file)
  15423. (and (file-readable-p file) file)))
  15424. files))))
  15425. (when (or (eq archives t)
  15426. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  15427. (setq files (org-add-archive-files files)))
  15428. files))
  15429. (defun org-agenda-file-p (&optional file)
  15430. "Return non-nil, if FILE is an agenda file.
  15431. If FILE is omitted, use the file associated with the current
  15432. buffer."
  15433. (member (or file (buffer-file-name))
  15434. (org-agenda-files t)))
  15435. (defun org-edit-agenda-file-list ()
  15436. "Edit the list of agenda files.
  15437. Depending on setup, this either uses customize to edit the variable
  15438. `org-agenda-files', or it visits the file that is holding the list. In the
  15439. latter case, the buffer is set up in a way that saving it automatically kills
  15440. the buffer and restores the previous window configuration."
  15441. (interactive)
  15442. (if (stringp org-agenda-files)
  15443. (let ((cw (current-window-configuration)))
  15444. (find-file org-agenda-files)
  15445. (org-set-local 'org-window-configuration cw)
  15446. (org-add-hook 'after-save-hook
  15447. (lambda ()
  15448. (set-window-configuration
  15449. (prog1 org-window-configuration
  15450. (kill-buffer (current-buffer))))
  15451. (org-install-agenda-files-menu)
  15452. (message "New agenda file list installed"))
  15453. nil 'local)
  15454. (message "%s" (substitute-command-keys
  15455. "Edit list and finish with \\[save-buffer]")))
  15456. (customize-variable 'org-agenda-files)))
  15457. (defun org-store-new-agenda-file-list (list)
  15458. "Set new value for the agenda file list and save it correctly."
  15459. (if (stringp org-agenda-files)
  15460. (let ((fe (org-read-agenda-file-list t)) b u)
  15461. (while (setq b (find-buffer-visiting org-agenda-files))
  15462. (kill-buffer b))
  15463. (with-temp-file org-agenda-files
  15464. (insert
  15465. (mapconcat
  15466. (lambda (f) ;; Keep un-expanded entries.
  15467. (if (setq u (assoc f fe))
  15468. (cdr u)
  15469. f))
  15470. list "\n")
  15471. "\n")))
  15472. (let ((org-mode-hook nil) (org-inhibit-startup t)
  15473. (org-insert-mode-line-in-empty-file nil))
  15474. (setq org-agenda-files list)
  15475. (customize-save-variable 'org-agenda-files org-agenda-files))))
  15476. (defun org-read-agenda-file-list (&optional pair-with-expansion)
  15477. "Read the list of agenda files from a file.
  15478. If PAIR-WITH-EXPANSION is t return pairs with un-expanded
  15479. filenames, used by `org-store-new-agenda-file-list' to write back
  15480. un-expanded file names."
  15481. (when (file-directory-p org-agenda-files)
  15482. (error "`org-agenda-files' cannot be a single directory"))
  15483. (when (stringp org-agenda-files)
  15484. (with-temp-buffer
  15485. (insert-file-contents org-agenda-files)
  15486. (mapcar
  15487. (lambda (f)
  15488. (let ((e (expand-file-name (substitute-in-file-name f)
  15489. org-directory)))
  15490. (if pair-with-expansion
  15491. (cons e f)
  15492. e)))
  15493. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*")))))
  15494. ;;;###autoload
  15495. (defun org-cycle-agenda-files ()
  15496. "Cycle through the files in `org-agenda-files'.
  15497. If the current buffer visits an agenda file, find the next one in the list.
  15498. If the current buffer does not, find the first agenda file."
  15499. (interactive)
  15500. (let* ((fs (org-agenda-files t))
  15501. (files (append fs (list (car fs))))
  15502. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  15503. file)
  15504. (unless files (error "No agenda files"))
  15505. (catch 'exit
  15506. (while (setq file (pop files))
  15507. (if (equal (file-truename file) tcf)
  15508. (when (car files)
  15509. (find-file (car files))
  15510. (throw 'exit t))))
  15511. (find-file (car fs)))
  15512. (if (buffer-base-buffer) (org-pop-to-buffer-same-window (buffer-base-buffer)))))
  15513. (defun org-agenda-file-to-front (&optional to-end)
  15514. "Move/add the current file to the top of the agenda file list.
  15515. If the file is not present in the list, it is added to the front. If it is
  15516. present, it is moved there. With optional argument TO-END, add/move to the
  15517. end of the list."
  15518. (interactive "P")
  15519. (let ((org-agenda-skip-unavailable-files nil)
  15520. (file-alist (mapcar (lambda (x)
  15521. (cons (file-truename x) x))
  15522. (org-agenda-files t)))
  15523. (ctf (file-truename buffer-file-name))
  15524. x had)
  15525. (setq x (assoc ctf file-alist) had x)
  15526. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  15527. (if to-end
  15528. (setq file-alist (append (delq x file-alist) (list x)))
  15529. (setq file-alist (cons x (delq x file-alist))))
  15530. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  15531. (org-install-agenda-files-menu)
  15532. (message "File %s to %s of agenda file list"
  15533. (if had "moved" "added") (if to-end "end" "front"))))
  15534. (defun org-remove-file (&optional file)
  15535. "Remove current file from the list of files in variable `org-agenda-files'.
  15536. These are the files which are being checked for agenda entries.
  15537. Optional argument FILE means use this file instead of the current."
  15538. (interactive)
  15539. (let* ((org-agenda-skip-unavailable-files nil)
  15540. (file (or file buffer-file-name))
  15541. (true-file (file-truename file))
  15542. (afile (abbreviate-file-name file))
  15543. (files (delq nil (mapcar
  15544. (lambda (x)
  15545. (if (equal true-file
  15546. (file-truename x))
  15547. nil x))
  15548. (org-agenda-files t)))))
  15549. (if (not (= (length files) (length (org-agenda-files t))))
  15550. (progn
  15551. (org-store-new-agenda-file-list files)
  15552. (org-install-agenda-files-menu)
  15553. (message "Removed file: %s" afile))
  15554. (message "File was not in list: %s (not removed)" afile))))
  15555. (defun org-file-menu-entry (file)
  15556. (vector file (list 'find-file file) t))
  15557. (defun org-check-agenda-file (file)
  15558. "Make sure FILE exists. If not, ask user what to do."
  15559. (when (not (file-exists-p file))
  15560. (message "non-existent agenda file %s. [R]emove from list or [A]bort?"
  15561. (abbreviate-file-name file))
  15562. (let ((r (downcase (read-char-exclusive))))
  15563. (cond
  15564. ((equal r ?r)
  15565. (org-remove-file file)
  15566. (throw 'nextfile t))
  15567. (t (error "Abort"))))))
  15568. (defun org-get-agenda-file-buffer (file)
  15569. "Get a buffer visiting FILE. If the buffer needs to be created, add
  15570. it to the list of buffers which might be released later."
  15571. (let ((buf (org-find-base-buffer-visiting file)))
  15572. (if buf
  15573. buf ; just return it
  15574. ;; Make a new buffer and remember it
  15575. (setq buf (find-file-noselect file))
  15576. (if buf (push buf org-agenda-new-buffers))
  15577. buf)))
  15578. (defun org-release-buffers (blist)
  15579. "Release all buffers in list, asking the user for confirmation when needed.
  15580. When a buffer is unmodified, it is just killed. When modified, it is saved
  15581. \(if the user agrees) and then killed."
  15582. (let (buf file)
  15583. (while (setq buf (pop blist))
  15584. (setq file (buffer-file-name buf))
  15585. (when (and (buffer-modified-p buf)
  15586. file
  15587. (y-or-n-p (format "Save file %s? " file)))
  15588. (with-current-buffer buf (save-buffer)))
  15589. (kill-buffer buf))))
  15590. (defun org-agenda-prepare-buffers (files)
  15591. "Create buffers for all agenda files, protect archived trees and comments."
  15592. (interactive)
  15593. (let ((pa '(:org-archived t))
  15594. (pc '(:org-comment t))
  15595. (pall '(:org-archived t :org-comment t))
  15596. (inhibit-read-only t)
  15597. (rea (concat ":" org-archive-tag ":"))
  15598. bmp file re)
  15599. (save-excursion
  15600. (save-restriction
  15601. (while (setq file (pop files))
  15602. (catch 'nextfile
  15603. (if (bufferp file)
  15604. (set-buffer file)
  15605. (org-check-agenda-file file)
  15606. (set-buffer (org-get-agenda-file-buffer file)))
  15607. (widen)
  15608. (setq bmp (buffer-modified-p))
  15609. (org-refresh-category-properties)
  15610. (setq org-todo-keywords-for-agenda
  15611. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  15612. (setq org-done-keywords-for-agenda
  15613. (append org-done-keywords-for-agenda org-done-keywords))
  15614. (setq org-todo-keyword-alist-for-agenda
  15615. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  15616. (setq org-drawers-for-agenda
  15617. (append org-drawers-for-agenda org-drawers))
  15618. (setq org-tag-alist-for-agenda
  15619. (append org-tag-alist-for-agenda org-tag-alist))
  15620. (save-excursion
  15621. (remove-text-properties (point-min) (point-max) pall)
  15622. (when org-agenda-skip-archived-trees
  15623. (goto-char (point-min))
  15624. (while (re-search-forward rea nil t)
  15625. (if (org-at-heading-p t)
  15626. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  15627. (goto-char (point-min))
  15628. (setq re (format org-heading-keyword-regexp-format
  15629. org-comment-string))
  15630. (while (re-search-forward re nil t)
  15631. (add-text-properties
  15632. (match-beginning 0) (org-end-of-subtree t) pc)))
  15633. (set-buffer-modified-p bmp)))))
  15634. (setq org-todo-keywords-for-agenda
  15635. (org-uniquify org-todo-keywords-for-agenda))
  15636. (setq org-todo-keyword-alist-for-agenda
  15637. (org-uniquify org-todo-keyword-alist-for-agenda)
  15638. org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
  15639. ;;;; Embedded LaTeX
  15640. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  15641. "Keymap for the minor `org-cdlatex-mode'.")
  15642. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  15643. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  15644. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  15645. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  15646. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  15647. (defvar org-cdlatex-texmathp-advice-is-done nil
  15648. "Flag remembering if we have applied the advice to texmathp already.")
  15649. (define-minor-mode org-cdlatex-mode
  15650. "Toggle the minor `org-cdlatex-mode'.
  15651. This mode supports entering LaTeX environment and math in LaTeX fragments
  15652. in Org-mode.
  15653. \\{org-cdlatex-mode-map}"
  15654. nil " OCDL" nil
  15655. (when org-cdlatex-mode
  15656. (require 'cdlatex)
  15657. (run-hooks 'cdlatex-mode-hook)
  15658. (cdlatex-compute-tables))
  15659. (unless org-cdlatex-texmathp-advice-is-done
  15660. (setq org-cdlatex-texmathp-advice-is-done t)
  15661. (defadvice texmathp (around org-math-always-on activate)
  15662. "Always return t in org-mode buffers.
  15663. This is because we want to insert math symbols without dollars even outside
  15664. the LaTeX math segments. If Orgmode thinks that point is actually inside
  15665. an embedded LaTeX fragment, let texmathp do its job.
  15666. \\[org-cdlatex-mode-map]"
  15667. (interactive)
  15668. (let (p)
  15669. (cond
  15670. ((not (derived-mode-p 'org-mode)) ad-do-it)
  15671. ((eq this-command 'cdlatex-math-symbol)
  15672. (setq ad-return-value t
  15673. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  15674. (t
  15675. (let ((p (org-inside-LaTeX-fragment-p)))
  15676. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  15677. (setq ad-return-value t
  15678. texmathp-why '("Org-mode embedded math" . 0))
  15679. (if p ad-do-it)))))))))
  15680. (defun turn-on-org-cdlatex ()
  15681. "Unconditionally turn on `org-cdlatex-mode'."
  15682. (org-cdlatex-mode 1))
  15683. (defun org-inside-LaTeX-fragment-p ()
  15684. "Test if point is inside a LaTeX fragment.
  15685. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  15686. sequence appearing also before point.
  15687. Even though the matchers for math are configurable, this function assumes
  15688. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  15689. delimiters are skipped when they have been removed by customization.
  15690. The return value is nil, or a cons cell with the delimiter and the
  15691. position of this delimiter.
  15692. This function does a reasonably good job, but can locally be fooled by
  15693. for example currency specifications. For example it will assume being in
  15694. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  15695. fragments that are properly closed, but during editing, we have to live
  15696. with the uncertainty caused by missing closing delimiters. This function
  15697. looks only before point, not after."
  15698. (catch 'exit
  15699. (let ((pos (point))
  15700. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  15701. (lim (progn
  15702. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  15703. (point)))
  15704. dd-on str (start 0) m re)
  15705. (goto-char pos)
  15706. (when dodollar
  15707. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  15708. re (nth 1 (assoc "$" org-latex-regexps)))
  15709. (while (string-match re str start)
  15710. (cond
  15711. ((= (match-end 0) (length str))
  15712. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  15713. ((= (match-end 0) (- (length str) 5))
  15714. (throw 'exit nil))
  15715. (t (setq start (match-end 0))))))
  15716. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  15717. (goto-char pos)
  15718. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  15719. (and (match-beginning 2) (throw 'exit nil))
  15720. ;; count $$
  15721. (while (re-search-backward "\\$\\$" lim t)
  15722. (setq dd-on (not dd-on)))
  15723. (goto-char pos)
  15724. (if dd-on (cons "$$" m))))))
  15725. (defun org-inside-latex-macro-p ()
  15726. "Is point inside a LaTeX macro or its arguments?"
  15727. (save-match-data
  15728. (org-in-regexp
  15729. "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")))
  15730. (defun org-try-cdlatex-tab ()
  15731. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  15732. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  15733. - inside a LaTeX fragment, or
  15734. - after the first word in a line, where an abbreviation expansion could
  15735. insert a LaTeX environment."
  15736. (when org-cdlatex-mode
  15737. (cond
  15738. ;; Before any word on the line: No expansion possible.
  15739. ((save-excursion (skip-chars-backward " \t") (bolp)) nil)
  15740. ;; Just after first word on the line: Expand it. Make sure it
  15741. ;; cannot happen on headlines, though.
  15742. ((save-excursion
  15743. (skip-chars-backward "a-zA-Z0-9*")
  15744. (skip-chars-backward " \t")
  15745. (and (bolp) (not (org-at-heading-p))))
  15746. (cdlatex-tab) t)
  15747. ((org-inside-LaTeX-fragment-p) (cdlatex-tab) t))))
  15748. (defun org-cdlatex-underscore-caret (&optional arg)
  15749. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  15750. Revert to the normal definition outside of these fragments."
  15751. (interactive "P")
  15752. (if (org-inside-LaTeX-fragment-p)
  15753. (call-interactively 'cdlatex-sub-superscript)
  15754. (let (org-cdlatex-mode)
  15755. (call-interactively (key-binding (vector last-input-event))))))
  15756. (defun org-cdlatex-math-modify (&optional arg)
  15757. "Execute `cdlatex-math-modify' in LaTeX fragments.
  15758. Revert to the normal definition outside of these fragments."
  15759. (interactive "P")
  15760. (if (org-inside-LaTeX-fragment-p)
  15761. (call-interactively 'cdlatex-math-modify)
  15762. (let (org-cdlatex-mode)
  15763. (call-interactively (key-binding (vector last-input-event))))))
  15764. (defvar org-latex-fragment-image-overlays nil
  15765. "List of overlays carrying the images of latex fragments.")
  15766. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  15767. (defun org-remove-latex-fragment-image-overlays ()
  15768. "Remove all overlays with LaTeX fragment images in current buffer."
  15769. (mapc 'delete-overlay org-latex-fragment-image-overlays)
  15770. (setq org-latex-fragment-image-overlays nil))
  15771. (defun org-preview-latex-fragment (&optional subtree)
  15772. "Preview the LaTeX fragment at point, or all locally or globally.
  15773. If the cursor is in a LaTeX fragment, create the image and overlay
  15774. it over the source code. If there is no fragment at point, display
  15775. all fragments in the current text, from one headline to the next. With
  15776. prefix SUBTREE, display all fragments in the current subtree. With a
  15777. double prefix arg \\[universal-argument] \\[universal-argument], or when \
  15778. the cursor is before the first headline,
  15779. display all fragments in the buffer.
  15780. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  15781. (interactive "P")
  15782. (unless buffer-file-name
  15783. (error "Can't preview LaTeX fragment in a non-file buffer"))
  15784. (org-remove-latex-fragment-image-overlays)
  15785. (save-excursion
  15786. (save-restriction
  15787. (let (beg end at msg)
  15788. (cond
  15789. ((or (equal subtree '(16))
  15790. (not (save-excursion
  15791. (re-search-backward org-outline-regexp-bol nil t))))
  15792. (setq beg (point-min) end (point-max)
  15793. msg "Creating images for buffer...%s"))
  15794. ((equal subtree '(4))
  15795. (org-back-to-heading)
  15796. (setq beg (point) end (org-end-of-subtree t)
  15797. msg "Creating images for subtree...%s"))
  15798. (t
  15799. (if (setq at (org-inside-LaTeX-fragment-p))
  15800. (goto-char (max (point-min) (- (cdr at) 2)))
  15801. (org-back-to-heading))
  15802. (setq beg (point) end (progn (outline-next-heading) (point))
  15803. msg (if at "Creating image...%s"
  15804. "Creating images for entry...%s"))))
  15805. (message msg "")
  15806. (narrow-to-region beg end)
  15807. (goto-char beg)
  15808. (org-format-latex
  15809. (concat org-latex-preview-ltxpng-directory (file-name-sans-extension
  15810. (file-name-nondirectory
  15811. buffer-file-name)))
  15812. default-directory 'overlays msg at 'forbuffer
  15813. org-latex-create-formula-image-program)
  15814. (message msg "done. Use `C-c C-c' to remove images.")))))
  15815. (defvar org-latex-regexps
  15816. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  15817. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  15818. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  15819. ("$1" "\\([^$]\\|^\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  15820. ("$" "\\([^$]\\|^\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  15821. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  15822. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
  15823. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
  15824. "Regular expressions for matching embedded LaTeX.")
  15825. (defvar org-export-have-math nil) ;; dynamic scoping
  15826. (defun org-format-latex (prefix &optional dir overlays msg at
  15827. forbuffer processing-type)
  15828. "Replace LaTeX fragments with links to an image, and produce images.
  15829. Some of the options can be changed using the variable
  15830. `org-format-latex-options'."
  15831. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  15832. (let* ((prefixnodir (file-name-nondirectory prefix))
  15833. (absprefix (expand-file-name prefix dir))
  15834. (todir (file-name-directory absprefix))
  15835. (opt org-format-latex-options)
  15836. (optnew org-format-latex-options)
  15837. (matchers (plist-get opt :matchers))
  15838. (re-list org-latex-regexps)
  15839. (org-format-latex-header-extra
  15840. (plist-get (org-infile-export-plist) :latex-header-extra))
  15841. (cnt 0) txt hash link beg end re e checkdir
  15842. string
  15843. m n block-type block linkfile movefile ov)
  15844. ;; Check the different regular expressions
  15845. (while (setq e (pop re-list))
  15846. (setq m (car e) re (nth 1 e) n (nth 2 e) block-type (nth 3 e)
  15847. block (if block-type "\n\n" ""))
  15848. (when (member m matchers)
  15849. (goto-char (point-min))
  15850. (while (re-search-forward re nil t)
  15851. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  15852. (not (get-text-property (match-beginning n)
  15853. 'org-protected))
  15854. (or (not overlays)
  15855. (not (eq (get-char-property (match-beginning n)
  15856. 'org-overlay-type)
  15857. 'org-latex-overlay))))
  15858. (setq org-export-have-math t)
  15859. (cond
  15860. ((eq processing-type 'verbatim)
  15861. ;; Leave the text verbatim, just protect it
  15862. (add-text-properties (match-beginning n) (match-end n)
  15863. '(org-protected t)))
  15864. ((eq processing-type 'mathjax)
  15865. ;; Prepare for MathJax processing
  15866. (setq string (match-string n))
  15867. (if (member m '("$" "$1"))
  15868. (save-excursion
  15869. (delete-region (match-beginning n) (match-end n))
  15870. (goto-char (match-beginning n))
  15871. (insert (org-add-props (concat "\\(" (substring string 1 -1)
  15872. "\\)")
  15873. '(org-protected t))))
  15874. (add-text-properties (match-beginning n) (match-end n)
  15875. '(org-protected t))))
  15876. ((or (eq processing-type 'dvipng)
  15877. (eq processing-type 'imagemagick))
  15878. ;; Process to an image
  15879. (setq txt (match-string n)
  15880. beg (match-beginning n) end (match-end n)
  15881. cnt (1+ cnt))
  15882. (let ((face (face-at-point))
  15883. (fg (plist-get opt :foreground))
  15884. (bg (plist-get opt :background))
  15885. print-length print-level) ; make sure full list is printed
  15886. (when forbuffer
  15887. ; Get the colors from the face at point
  15888. (goto-char beg)
  15889. (when (eq fg 'auto)
  15890. (setq fg (face-attribute face :foreground nil 'default)))
  15891. (when (eq bg 'auto)
  15892. (setq bg (face-attribute face :background nil 'default)))
  15893. (setq optnew (copy-sequence opt))
  15894. (plist-put optnew :foreground fg)
  15895. (plist-put optnew :background bg))
  15896. (setq hash (sha1 (prin1-to-string
  15897. (list org-format-latex-header
  15898. org-format-latex-header-extra
  15899. org-export-latex-default-packages-alist
  15900. org-export-latex-packages-alist
  15901. org-format-latex-options
  15902. forbuffer txt fg bg)))
  15903. linkfile (format "%s_%s.png" prefix hash)
  15904. movefile (format "%s_%s.png" absprefix hash)))
  15905. (setq link (concat block "[[file:" linkfile "]]" block))
  15906. (if msg (message msg cnt))
  15907. (goto-char beg)
  15908. (unless checkdir ; make sure the directory exists
  15909. (setq checkdir t)
  15910. (or (file-directory-p todir) (make-directory todir t)))
  15911. (org-create-formula-image
  15912. txt movefile optnew forbuffer processing-type)
  15913. (if overlays
  15914. (progn
  15915. (mapc (lambda (o)
  15916. (if (eq (overlay-get o 'org-overlay-type)
  15917. 'org-latex-overlay)
  15918. (delete-overlay o)))
  15919. (overlays-in beg end))
  15920. (setq ov (make-overlay beg end))
  15921. (overlay-put ov 'org-overlay-type 'org-latex-overlay)
  15922. (if (featurep 'xemacs)
  15923. (progn
  15924. (overlay-put ov 'invisible t)
  15925. (overlay-put
  15926. ov 'end-glyph
  15927. (make-glyph (vector 'png :file movefile))))
  15928. (overlay-put
  15929. ov 'display
  15930. (list 'image :type 'png :file movefile :ascent 'center)))
  15931. (push ov org-latex-fragment-image-overlays)
  15932. (goto-char end))
  15933. (delete-region beg end)
  15934. (insert (org-add-props link
  15935. (list 'org-latex-src
  15936. (replace-regexp-in-string
  15937. "\"" "" txt)
  15938. 'org-latex-src-embed-type
  15939. (if block-type 'paragraph 'character))))))
  15940. ((eq processing-type 'mathml)
  15941. ;; Process to MathML
  15942. (unless (save-match-data (org-format-latex-mathml-available-p))
  15943. (error "LaTeX to MathML converter not configured"))
  15944. (setq txt (match-string n)
  15945. beg (match-beginning n) end (match-end n)
  15946. cnt (1+ cnt))
  15947. (if msg (message msg cnt))
  15948. (goto-char beg)
  15949. (delete-region beg end)
  15950. (insert (org-format-latex-as-mathml
  15951. txt block-type prefix dir)))
  15952. (t
  15953. (error "Unknown conversion type %s for latex fragments"
  15954. processing-type)))))))))
  15955. (defun org-create-math-formula (latex-frag &optional mathml-file)
  15956. "Convert LATEX-FRAG to MathML and store it in MATHML-FILE.
  15957. Use `org-latex-to-mathml-convert-command'. If the conversion is
  15958. sucessful, return the portion between \"<math...> </math>\"
  15959. elements otherwise return nil. When MATHML-FILE is specified,
  15960. write the results in to that file. When invoked as an
  15961. interactive command, prompt for LATEX-FRAG, with initial value
  15962. set to the current active region and echo the results for user
  15963. inspection."
  15964. (interactive (list (let ((frag (when (org-region-active-p)
  15965. (buffer-substring-no-properties
  15966. (region-beginning) (region-end)))))
  15967. (read-string "LaTeX Fragment: " frag nil frag))))
  15968. (unless latex-frag (error "Invalid latex-frag"))
  15969. (let* ((tmp-in-file (file-relative-name
  15970. (make-temp-name (expand-file-name "ltxmathml-in"))))
  15971. (ignore (write-region latex-frag nil tmp-in-file))
  15972. (tmp-out-file (file-relative-name
  15973. (make-temp-name (expand-file-name "ltxmathml-out"))))
  15974. (cmd (format-spec
  15975. org-latex-to-mathml-convert-command
  15976. `((?j . ,(shell-quote-argument
  15977. (expand-file-name org-latex-to-mathml-jar-file)))
  15978. (?I . ,(shell-quote-argument tmp-in-file))
  15979. (?o . ,(shell-quote-argument tmp-out-file)))))
  15980. mathml shell-command-output)
  15981. (when (org-called-interactively-p 'any)
  15982. (unless (org-format-latex-mathml-available-p)
  15983. (error "LaTeX to MathML converter not configured")))
  15984. (message "Running %s" cmd)
  15985. (setq shell-command-output (shell-command-to-string cmd))
  15986. (setq mathml
  15987. (when (file-readable-p tmp-out-file)
  15988. (with-current-buffer (find-file-noselect tmp-out-file t)
  15989. (goto-char (point-min))
  15990. (when (re-search-forward
  15991. (concat
  15992. (regexp-quote
  15993. "<math xmlns=\"http://www.w3.org/1998/Math/MathML\">")
  15994. "\\(.\\|\n\\)*"
  15995. (regexp-quote "</math>")) nil t)
  15996. (prog1 (match-string 0) (kill-buffer))))))
  15997. (cond
  15998. (mathml
  15999. (setq mathml
  16000. (concat "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" mathml))
  16001. (when mathml-file
  16002. (write-region mathml nil mathml-file))
  16003. (when (org-called-interactively-p 'any)
  16004. (message mathml)))
  16005. ((message "LaTeX to MathML conversion failed")
  16006. (message shell-command-output)))
  16007. (delete-file tmp-in-file)
  16008. (when (file-exists-p tmp-out-file)
  16009. (delete-file tmp-out-file))
  16010. mathml))
  16011. (defun org-format-latex-as-mathml (latex-frag latex-frag-type
  16012. prefix &optional dir)
  16013. "Use `org-create-math-formula' but check local cache first."
  16014. (let* ((absprefix (expand-file-name prefix dir))
  16015. (print-length nil) (print-level nil)
  16016. (formula-id (concat
  16017. "formula-"
  16018. (sha1
  16019. (prin1-to-string
  16020. (list latex-frag
  16021. org-latex-to-mathml-convert-command)))))
  16022. (formula-cache (format "%s-%s.mathml" absprefix formula-id))
  16023. (formula-cache-dir (file-name-directory formula-cache)))
  16024. (unless (file-directory-p formula-cache-dir)
  16025. (make-directory formula-cache-dir t))
  16026. (unless (file-exists-p formula-cache)
  16027. (org-create-math-formula latex-frag formula-cache))
  16028. (if (file-exists-p formula-cache)
  16029. ;; Successful conversion. Return the link to MathML file.
  16030. (org-add-props
  16031. (format "[[file:%s]]" (file-relative-name formula-cache dir))
  16032. (list 'org-latex-src (replace-regexp-in-string "\"" "" latex-frag)
  16033. 'org-latex-src-embed-type (if latex-frag-type
  16034. 'paragraph 'character)))
  16035. ;; Failed conversion. Return the LaTeX fragment verbatim
  16036. (add-text-properties
  16037. 0 (1- (length latex-frag)) '(org-protected t) latex-frag)
  16038. latex-frag)))
  16039. (defun org-create-formula-image (string tofile options buffer &optional type)
  16040. "Create an image from LaTeX source using dvipng or convert.
  16041. This function calls either `org-create-formula-image-with-dvipng'
  16042. or `org-create-formula-image-with-imagemagick' depending on the
  16043. value of `org-latex-create-formula-image-program' or on the value
  16044. of the optional TYPE variable.
  16045. Note: ultimately these two function should be combined as they
  16046. share a good deal of logic."
  16047. (org-check-external-command
  16048. "latex" "needed to convert LaTeX fragments to images")
  16049. (funcall
  16050. (case (or type org-latex-create-formula-image-program)
  16051. ('dvipng
  16052. (org-check-external-command
  16053. "dvipng" "needed to convert LaTeX fragments to images")
  16054. #'org-create-formula-image-with-dvipng)
  16055. ('imagemagick
  16056. (org-check-external-command
  16057. "convert" "you need to install imagemagick")
  16058. #'org-create-formula-image-with-imagemagick)
  16059. (t (error
  16060. "invalid value of `org-latex-create-formula-image-program'")))
  16061. string tofile options buffer))
  16062. ;; This function borrows from Ganesh Swami's latex2png.el
  16063. (defun org-create-formula-image-with-dvipng (string tofile options buffer)
  16064. "This calls dvipng."
  16065. (require 'org-latex)
  16066. (let* ((tmpdir (if (featurep 'xemacs)
  16067. (temp-directory)
  16068. temporary-file-directory))
  16069. (texfilebase (make-temp-name
  16070. (expand-file-name "orgtex" tmpdir)))
  16071. (texfile (concat texfilebase ".tex"))
  16072. (dvifile (concat texfilebase ".dvi"))
  16073. (pngfile (concat texfilebase ".png"))
  16074. (fnh (if (featurep 'xemacs)
  16075. (font-height (face-font 'default))
  16076. (face-attribute 'default :height nil)))
  16077. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  16078. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  16079. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  16080. "Black"))
  16081. (bg (or (plist-get options (if buffer :background :html-background))
  16082. "Transparent")))
  16083. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground))
  16084. (unless (string= fg "Transparent") (setq fg (org-dvipng-color-format fg))))
  16085. (if (eq bg 'default) (setq bg (org-dvipng-color :background))
  16086. (unless (string= bg "Transparent") (setq bg (org-dvipng-color-format bg))))
  16087. (with-temp-file texfile
  16088. (insert (org-splice-latex-header
  16089. org-format-latex-header
  16090. org-export-latex-default-packages-alist
  16091. org-export-latex-packages-alist t
  16092. org-format-latex-header-extra))
  16093. (insert "\n\\begin{document}\n" string "\n\\end{document}\n")
  16094. (require 'org-latex)
  16095. (org-export-latex-fix-inputenc))
  16096. (let ((dir default-directory))
  16097. (condition-case nil
  16098. (progn
  16099. (cd tmpdir)
  16100. (call-process "latex" nil nil nil texfile))
  16101. (error nil))
  16102. (cd dir))
  16103. (if (not (file-exists-p dvifile))
  16104. (progn (message "Failed to create dvi file from %s" texfile) nil)
  16105. (condition-case nil
  16106. (if (featurep 'xemacs)
  16107. (call-process "dvipng" nil nil nil
  16108. "-fg" fg "-bg" bg
  16109. "-T" "tight"
  16110. "-o" pngfile
  16111. dvifile)
  16112. (call-process "dvipng" nil nil nil
  16113. "-fg" fg "-bg" bg
  16114. "-D" dpi
  16115. ;;"-x" scale "-y" scale
  16116. "-T" "tight"
  16117. "-o" pngfile
  16118. dvifile))
  16119. (error nil))
  16120. (if (not (file-exists-p pngfile))
  16121. (if org-format-latex-signal-error
  16122. (error "Failed to create png file from %s" texfile)
  16123. (message "Failed to create png file from %s" texfile)
  16124. nil)
  16125. ;; Use the requested file name and clean up
  16126. (copy-file pngfile tofile 'replace)
  16127. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png" ".out") do
  16128. (if (file-exists-p (concat texfilebase e))
  16129. (delete-file (concat texfilebase e))))
  16130. pngfile))))
  16131. (defvar org-latex-to-pdf-process) ;; Defined in org-latex.el
  16132. (defun org-create-formula-image-with-imagemagick (string tofile options buffer)
  16133. "This calls convert, which is included into imagemagick."
  16134. (require 'org-latex)
  16135. (let* ((tmpdir (if (featurep 'xemacs)
  16136. (temp-directory)
  16137. temporary-file-directory))
  16138. (texfilebase (make-temp-name
  16139. (expand-file-name "orgtex" tmpdir)))
  16140. (texfile (concat texfilebase ".tex"))
  16141. (pdffile (concat texfilebase ".pdf"))
  16142. (pngfile (concat texfilebase ".png"))
  16143. (fnh (if (featurep 'xemacs)
  16144. (font-height (face-font 'default))
  16145. (face-attribute 'default :height nil)))
  16146. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  16147. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  16148. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  16149. "black"))
  16150. (bg (or (plist-get options (if buffer :background :html-background))
  16151. "white")))
  16152. (if (eq fg 'default) (setq fg (org-latex-color :foreground))
  16153. (setq fg (org-latex-color-format fg)))
  16154. (if (eq bg 'default) (setq bg (org-latex-color :background))
  16155. (setq bg (org-latex-color-format
  16156. (if (string= bg "Transparent") "white" bg))))
  16157. (with-temp-file texfile
  16158. (insert (org-splice-latex-header
  16159. org-format-latex-header
  16160. org-export-latex-default-packages-alist
  16161. org-export-latex-packages-alist t
  16162. org-format-latex-header-extra))
  16163. (insert "\n\\begin{document}\n"
  16164. "\\definecolor{fg}{rgb}{" fg "}\n"
  16165. "\\definecolor{bg}{rgb}{" bg "}\n"
  16166. "\n\\pagecolor{bg}\n"
  16167. "\n{\\color{fg}\n"
  16168. string
  16169. "\n}\n"
  16170. "\n\\end{document}\n" )
  16171. (require 'org-latex)
  16172. (org-export-latex-fix-inputenc))
  16173. (let ((dir default-directory) cmd cmds latex-frags-cmds)
  16174. (condition-case nil
  16175. (progn
  16176. (cd tmpdir)
  16177. (setq cmds org-latex-to-pdf-process)
  16178. (while cmds
  16179. (setq latex-frags-cmds (pop cmds))
  16180. (if (listp latex-frags-cmds)
  16181. (setq cmds nil)
  16182. (setq latex-frags-cmds (list (car org-latex-to-pdf-process)))))
  16183. (while latex-frags-cmds
  16184. (setq cmd (pop latex-frags-cmds))
  16185. (while (string-match "%b" cmd)
  16186. (setq cmd (replace-match
  16187. (save-match-data
  16188. (shell-quote-argument texfile))
  16189. t t cmd)))
  16190. (while (string-match "%f" cmd)
  16191. (setq cmd (replace-match
  16192. (save-match-data
  16193. (shell-quote-argument (file-name-nondirectory texfile)))
  16194. t t cmd)))
  16195. (while (string-match "%o" cmd)
  16196. (setq cmd (replace-match
  16197. (save-match-data
  16198. (shell-quote-argument (file-name-directory texfile)))
  16199. t t cmd)))
  16200. (setq cmd (split-string cmd))
  16201. (eval (append (list 'call-process (pop cmd) nil nil nil) cmd))))
  16202. (error nil))
  16203. (cd dir))
  16204. (if (not (file-exists-p pdffile))
  16205. (progn (message "Failed to create pdf file from %s" texfile) nil)
  16206. (condition-case nil
  16207. (if (featurep 'xemacs)
  16208. (call-process "convert" nil nil nil
  16209. "-density" "96"
  16210. "-trim"
  16211. "-antialias"
  16212. pdffile
  16213. "-quality" "100"
  16214. ;; "-sharpen" "0x1.0"
  16215. pngfile)
  16216. (call-process "convert" nil nil nil
  16217. "-density" dpi
  16218. "-trim"
  16219. "-antialias"
  16220. pdffile
  16221. "-quality" "100"
  16222. ; "-sharpen" "0x1.0"
  16223. pngfile))
  16224. (error nil))
  16225. (if (not (file-exists-p pngfile))
  16226. (if org-format-latex-signal-error
  16227. (error "Failed to create png file from %s" texfile)
  16228. (message "Failed to create png file from %s" texfile)
  16229. nil)
  16230. ;; Use the requested file name and clean up
  16231. (copy-file pngfile tofile 'replace)
  16232. (loop for e in '(".pdf" ".tex" ".aux" ".log" ".png") do
  16233. (if (file-exists-p (concat texfilebase e))
  16234. (delete-file (concat texfilebase e))))
  16235. pngfile))))
  16236. (defun org-splice-latex-header (tpl def-pkg pkg snippets-p &optional extra)
  16237. "Fill a LaTeX header template TPL.
  16238. In the template, the following place holders will be recognized:
  16239. [DEFAULT-PACKAGES] \\usepackage statements for DEF-PKG
  16240. [NO-DEFAULT-PACKAGES] do not include DEF-PKG
  16241. [PACKAGES] \\usepackage statements for PKG
  16242. [NO-PACKAGES] do not include PKG
  16243. [EXTRA] the string EXTRA
  16244. [NO-EXTRA] do not include EXTRA
  16245. For backward compatibility, if both the positive and the negative place
  16246. holder is missing, the positive one (without the \"NO-\") will be
  16247. assumed to be present at the end of the template.
  16248. DEF-PKG and PKG are assumed to be alists of options/packagename lists.
  16249. EXTRA is a string.
  16250. SNIPPETS-P indicates if this is run to create snippet images for HTML."
  16251. (let (rpl (end ""))
  16252. (if (string-match "^[ \t]*\\[\\(NO-\\)?DEFAULT-PACKAGES\\][ \t]*\n?" tpl)
  16253. (setq rpl (if (or (match-end 1) (not def-pkg))
  16254. "" (org-latex-packages-to-string def-pkg snippets-p t))
  16255. tpl (replace-match rpl t t tpl))
  16256. (if def-pkg (setq end (org-latex-packages-to-string def-pkg snippets-p))))
  16257. (if (string-match "\\[\\(NO-\\)?PACKAGES\\][ \t]*\n?" tpl)
  16258. (setq rpl (if (or (match-end 1) (not pkg))
  16259. "" (org-latex-packages-to-string pkg snippets-p t))
  16260. tpl (replace-match rpl t t tpl))
  16261. (if pkg (setq end
  16262. (concat end "\n"
  16263. (org-latex-packages-to-string pkg snippets-p)))))
  16264. (if (string-match "\\[\\(NO-\\)?EXTRA\\][ \t]*\n?" tpl)
  16265. (setq rpl (if (or (match-end 1) (not extra))
  16266. "" (concat extra "\n"))
  16267. tpl (replace-match rpl t t tpl))
  16268. (if (and extra (string-match "\\S-" extra))
  16269. (setq end (concat end "\n" extra))))
  16270. (if (string-match "\\S-" end)
  16271. (concat tpl "\n" end)
  16272. tpl)))
  16273. (defun org-latex-packages-to-string (pkg &optional snippets-p newline)
  16274. "Turn an alist of packages into a string with the \\usepackage macros."
  16275. (setq pkg (mapconcat (lambda(p)
  16276. (cond
  16277. ((stringp p) p)
  16278. ((and snippets-p (>= (length p) 3) (not (nth 2 p)))
  16279. (format "%% Package %s omitted" (cadr p)))
  16280. ((equal "" (car p))
  16281. (format "\\usepackage{%s}" (cadr p)))
  16282. (t
  16283. (format "\\usepackage[%s]{%s}"
  16284. (car p) (cadr p)))))
  16285. pkg
  16286. "\n"))
  16287. (if newline (concat pkg "\n") pkg))
  16288. (defun org-dvipng-color (attr)
  16289. "Return a RGB color specification for dvipng."
  16290. (apply 'format "rgb %s %s %s"
  16291. (mapcar 'org-normalize-color
  16292. (if (featurep 'xemacs)
  16293. (color-rgb-components
  16294. (face-property 'default
  16295. (cond ((eq attr :foreground) 'foreground)
  16296. ((eq attr :background) 'background))))
  16297. (color-values (face-attribute 'default attr nil))))))
  16298. (defun org-dvipng-color-format (color-name)
  16299. "Convert COLOR-NAME to a RGB color value for dvipng."
  16300. (apply 'format "rgb %s %s %s"
  16301. (mapcar 'org-normalize-color
  16302. (color-values color-name))))
  16303. (defun org-latex-color (attr)
  16304. "Return a RGB color for the LaTeX color package."
  16305. (apply 'format "%s,%s,%s"
  16306. (mapcar 'org-normalize-color
  16307. (if (featurep 'xemacs)
  16308. (color-rgb-components
  16309. (face-property 'default
  16310. (cond ((eq attr :foreground) 'foreground)
  16311. ((eq attr :background) 'background))))
  16312. (color-values (face-attribute 'default attr nil))))))
  16313. (defun org-latex-color-format (color-name)
  16314. "Convert COLOR-NAME to a RGB color value."
  16315. (apply 'format "%s,%s,%s"
  16316. (mapcar 'org-normalize-color
  16317. (color-values color-name))))
  16318. (defun org-normalize-color (value)
  16319. "Return string to be used as color value for an RGB component."
  16320. (format "%g" (/ value 65535.0)))
  16321. ;; Image display
  16322. (defvar org-inline-image-overlays nil)
  16323. (make-variable-buffer-local 'org-inline-image-overlays)
  16324. (defun org-toggle-inline-images (&optional include-linked)
  16325. "Toggle the display of inline images.
  16326. INCLUDE-LINKED is passed to `org-display-inline-images'."
  16327. (interactive "P")
  16328. (if org-inline-image-overlays
  16329. (progn
  16330. (org-remove-inline-images)
  16331. (message "Inline image display turned off"))
  16332. (org-display-inline-images include-linked)
  16333. (if (and (org-called-interactively-p)
  16334. org-inline-image-overlays)
  16335. (message "%d images displayed inline"
  16336. (length org-inline-image-overlays))
  16337. (message "No images to display inline"))))
  16338. (defun org-redisplay-inline-images ()
  16339. "Refresh the display of inline images."
  16340. (interactive)
  16341. (if (not org-inline-image-overlays)
  16342. (org-toggle-inline-images)
  16343. (org-toggle-inline-images)
  16344. (org-toggle-inline-images)))
  16345. (defun org-display-inline-images (&optional include-linked refresh beg end)
  16346. "Display inline images.
  16347. Normally only links without a description part are inlined, because this
  16348. is how it will work for export. When INCLUDE-LINKED is set, also links
  16349. with a description part will be inlined. This can be nice for a quick
  16350. look at those images, but it does not reflect what exported files will look
  16351. like.
  16352. When REFRESH is set, refresh existing images between BEG and END.
  16353. This will create new image displays only if necessary.
  16354. BEG and END default to the buffer boundaries."
  16355. (interactive "P")
  16356. (unless refresh
  16357. (org-remove-inline-images)
  16358. (if (fboundp 'clear-image-cache) (clear-image-cache)))
  16359. (save-excursion
  16360. (save-restriction
  16361. (widen)
  16362. (setq beg (or beg (point-min)) end (or end (point-max)))
  16363. (goto-char beg)
  16364. (let ((re (concat "\\[\\[\\(\\(file:\\)\\|\\([./~]\\)\\)\\([^]\n]+?"
  16365. (substring (org-image-file-name-regexp) 0 -2)
  16366. "\\)\\]" (if include-linked "" "\\]")))
  16367. old file ov img type attrwidth width)
  16368. (while (re-search-forward re end t)
  16369. (setq old (get-char-property-and-overlay (match-beginning 1)
  16370. 'org-image-overlay)
  16371. file (expand-file-name
  16372. (concat (or (match-string 3) "") (match-string 4))))
  16373. (when (image-type-available-p 'imagemagick)
  16374. (setq attrwidth (if (or (listp org-image-actual-width)
  16375. (null org-image-actual-width))
  16376. (save-excursion
  16377. (save-match-data
  16378. (when (re-search-backward
  16379. "#\\+ATTR.*width=\"\\([^\"]+\\)\""
  16380. (save-excursion
  16381. (re-search-backward "^[ \t]*$\\|\\`" nil t)) t)
  16382. (string-to-number (match-string 1))))))
  16383. width (cond ((eq org-image-actual-width t) nil)
  16384. ((null org-image-actual-width) attrwidth)
  16385. ((numberp org-image-actual-width)
  16386. org-image-actual-width)
  16387. ((listp org-image-actual-width)
  16388. (or attrwidth (car org-image-actual-width))))
  16389. type (if width 'imagemagick)))
  16390. (when (file-exists-p file)
  16391. (if (and (car-safe old) refresh)
  16392. (image-refresh (overlay-get (cdr old) 'display))
  16393. (setq img (save-match-data (create-image file type nil :width width)))
  16394. (when img
  16395. (setq ov (make-overlay (match-beginning 0) (match-end 0)))
  16396. (overlay-put ov 'display img)
  16397. (overlay-put ov 'face 'default)
  16398. (overlay-put ov 'org-image-overlay t)
  16399. (overlay-put ov 'modification-hooks
  16400. (list 'org-display-inline-remove-overlay))
  16401. (push ov org-inline-image-overlays)))))))))
  16402. (define-obsolete-function-alias
  16403. 'org-display-inline-modification-hook 'org-display-inline-remove-overlay "24.3")
  16404. (defun org-display-inline-remove-overlay (ov after beg end &optional len)
  16405. "Remove inline-display overlay if a corresponding region is modified."
  16406. (let ((inhibit-modification-hooks t))
  16407. (when (and ov after)
  16408. (delete ov org-inline-image-overlays)
  16409. (delete-overlay ov))))
  16410. (defun org-remove-inline-images ()
  16411. "Remove inline display of images."
  16412. (interactive)
  16413. (mapc 'delete-overlay org-inline-image-overlays)
  16414. (setq org-inline-image-overlays nil))
  16415. ;;;; Key bindings
  16416. ;; Outline functions from `outline-mode-prefix-map'
  16417. ;; that can be remapped in Org:
  16418. (define-key org-mode-map [remap outline-mark-subtree] 'org-mark-subtree)
  16419. (define-key org-mode-map [remap show-subtree] 'org-show-subtree)
  16420. (define-key org-mode-map [remap outline-forward-same-level]
  16421. 'org-forward-heading-same-level)
  16422. (define-key org-mode-map [remap outline-backward-same-level]
  16423. 'org-backward-heading-same-level)
  16424. (define-key org-mode-map [remap show-branches]
  16425. 'org-kill-note-or-show-branches)
  16426. (define-key org-mode-map [remap outline-promote] 'org-promote-subtree)
  16427. (define-key org-mode-map [remap outline-demote] 'org-demote-subtree)
  16428. (define-key org-mode-map [remap outline-insert-heading] 'org-ctrl-c-ret)
  16429. ;; Outline functions from `outline-mode-prefix-map' that can not
  16430. ;; be remapped in Org:
  16431. ;;
  16432. ;; - the column "key binding" shows whether the Outline function is still
  16433. ;; available in Org mode on the same key that it has been bound to in
  16434. ;; Outline mode:
  16435. ;; - "overridden": key used for a different functionality in Org mode
  16436. ;; - else: key still bound to the same Outline function in Org mode
  16437. ;;
  16438. ;; | Outline function | key binding | Org replacement |
  16439. ;; |------------------------------------+-------------+-----------------------|
  16440. ;; | `outline-next-visible-heading' | `C-c C-n' | still same function |
  16441. ;; | `outline-previous-visible-heading' | `C-c C-p' | still same function |
  16442. ;; | `outline-up-heading' | `C-c C-u' | still same function |
  16443. ;; | `outline-move-subtree-up' | overridden | better: org-shiftup |
  16444. ;; | `outline-move-subtree-down' | overridden | better: org-shiftdown |
  16445. ;; | `show-entry' | overridden | no replacement |
  16446. ;; | `show-children' | `C-c C-i' | visibility cycling |
  16447. ;; | `show-branches' | `C-c C-k' | still same function |
  16448. ;; | `show-subtree' | overridden | visibility cycling |
  16449. ;; | `show-all' | overridden | no replacement |
  16450. ;; | `hide-subtree' | overridden | visibility cycling |
  16451. ;; | `hide-body' | overridden | no replacement |
  16452. ;; | `hide-entry' | overridden | visibility cycling |
  16453. ;; | `hide-leaves' | overridden | no replacement |
  16454. ;; | `hide-sublevels' | overridden | no replacement |
  16455. ;; | `hide-other' | overridden | no replacement |
  16456. ;; Make `C-c C-x' a prefix key
  16457. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  16458. ;; TAB key with modifiers
  16459. (org-defkey org-mode-map "\C-i" 'org-cycle)
  16460. (org-defkey org-mode-map [(tab)] 'org-cycle)
  16461. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  16462. (org-defkey org-mode-map "\M-\t" 'pcomplete)
  16463. ;; The following line is necessary under Suse GNU/Linux
  16464. (unless (featurep 'xemacs)
  16465. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  16466. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  16467. (define-key org-mode-map [backtab] 'org-shifttab)
  16468. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  16469. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  16470. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  16471. ;; Cursor keys with modifiers
  16472. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  16473. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  16474. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  16475. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  16476. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  16477. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  16478. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  16479. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  16480. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  16481. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  16482. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  16483. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  16484. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  16485. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  16486. (org-defkey org-mode-map [(control shift up)] 'org-shiftcontrolup)
  16487. (org-defkey org-mode-map [(control shift down)] 'org-shiftcontroldown)
  16488. ;; Babel keys
  16489. (define-key org-mode-map org-babel-key-prefix org-babel-map)
  16490. (mapc (lambda (pair)
  16491. (define-key org-babel-map (car pair) (cdr pair)))
  16492. org-babel-key-bindings)
  16493. ;;; Extra keys for tty access.
  16494. ;; We only set them when really needed because otherwise the
  16495. ;; menus don't show the simple keys
  16496. (when (or org-use-extra-keys
  16497. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  16498. (not window-system))
  16499. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  16500. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  16501. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  16502. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  16503. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  16504. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  16505. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  16506. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  16507. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  16508. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  16509. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  16510. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  16511. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  16512. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  16513. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  16514. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  16515. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  16516. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  16517. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  16518. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  16519. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  16520. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
  16521. (org-defkey org-mode-map [?\e (tab)] 'pcomplete)
  16522. (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
  16523. (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
  16524. (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
  16525. (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
  16526. (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
  16527. ;; All the other keys
  16528. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  16529. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  16530. (if (boundp 'narrow-map)
  16531. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  16532. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  16533. (if (boundp 'narrow-map)
  16534. (org-defkey narrow-map "b" 'org-narrow-to-block)
  16535. (org-defkey org-mode-map "\C-xnb" 'org-narrow-to-block))
  16536. (if (boundp 'narrow-map)
  16537. (org-defkey narrow-map "e" 'org-narrow-to-element)
  16538. (org-defkey org-mode-map "\C-xne" 'org-narrow-to-element))
  16539. (org-defkey org-mode-map "\C-\M-t" 'org-transpose-element)
  16540. (org-defkey org-mode-map "\M-}" 'org-forward-element)
  16541. (org-defkey org-mode-map "\M-{" 'org-backward-element)
  16542. (org-defkey org-mode-map "\C-c\C-^" 'org-up-element)
  16543. (org-defkey org-mode-map "\C-c\C-_" 'org-down-element)
  16544. (org-defkey org-mode-map "\C-c\C-f" 'org-forward-heading-same-level)
  16545. (org-defkey org-mode-map "\C-c\C-b" 'org-backward-heading-same-level)
  16546. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  16547. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  16548. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
  16549. (org-defkey org-mode-map "\C-c\C-xd" 'org-insert-drawer)
  16550. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  16551. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  16552. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  16553. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  16554. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  16555. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  16556. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  16557. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  16558. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  16559. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  16560. (org-defkey org-mode-map "\C-c\M-w" 'org-copy)
  16561. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  16562. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  16563. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  16564. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  16565. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  16566. (org-defkey org-mode-map "\C-c\C-xv" 'org-copy-visible)
  16567. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  16568. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  16569. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  16570. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  16571. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  16572. (org-defkey org-mode-map "\C-c\C-\M-l" 'org-insert-all-links)
  16573. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  16574. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  16575. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  16576. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  16577. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  16578. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  16579. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  16580. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  16581. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  16582. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  16583. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  16584. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  16585. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  16586. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  16587. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  16588. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  16589. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  16590. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  16591. (org-defkey org-mode-map "\C-c^" 'org-sort)
  16592. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  16593. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  16594. (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
  16595. (org-defkey org-mode-map "\C-m" 'org-return)
  16596. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  16597. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  16598. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  16599. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  16600. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  16601. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  16602. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  16603. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  16604. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  16605. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  16606. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  16607. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  16608. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  16609. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  16610. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  16611. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  16612. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  16613. (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
  16614. (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
  16615. (org-defkey org-mode-map "\C-c@" 'org-mark-subtree)
  16616. (org-defkey org-mode-map "\M-h" 'org-mark-element)
  16617. (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
  16618. ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
  16619. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  16620. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  16621. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  16622. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  16623. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  16624. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-in-last)
  16625. (org-defkey org-mode-map "\C-c\C-x\C-z" 'org-resolve-clocks)
  16626. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  16627. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  16628. (org-defkey org-mode-map "\C-c\C-x\C-q" 'org-clock-cancel)
  16629. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  16630. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  16631. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  16632. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  16633. (org-defkey org-mode-map "\C-c\C-x\C-v" 'org-toggle-inline-images)
  16634. (org-defkey org-mode-map "\C-c\C-x\C-\M-v" 'org-redisplay-inline-images)
  16635. (org-defkey org-mode-map "\C-c\C-x\\" 'org-toggle-pretty-entities)
  16636. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  16637. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  16638. (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
  16639. (org-defkey org-mode-map "\C-c\C-xE" 'org-inc-effort)
  16640. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  16641. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  16642. (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
  16643. (org-defkey org-mode-map [(control ?c) (control ?x) ?\:] 'org-timer-cancel-timer)
  16644. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  16645. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  16646. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  16647. (org-defkey org-mode-map "\C-c\C-x_" 'org-timer-stop)
  16648. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  16649. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  16650. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  16651. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  16652. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  16653. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  16654. (when (featurep 'xemacs)
  16655. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  16656. (defconst org-speed-commands-default
  16657. '(
  16658. ("Outline Navigation")
  16659. ("n" . (org-speed-move-safe 'outline-next-visible-heading))
  16660. ("p" . (org-speed-move-safe 'outline-previous-visible-heading))
  16661. ("f" . (org-speed-move-safe 'org-forward-heading-same-level))
  16662. ("b" . (org-speed-move-safe 'org-backward-heading-same-level))
  16663. ("u" . (org-speed-move-safe 'outline-up-heading))
  16664. ("j" . org-goto)
  16665. ("g" . (org-refile t))
  16666. ("Outline Visibility")
  16667. ("c" . org-cycle)
  16668. ("C" . org-shifttab)
  16669. (" " . org-display-outline-path)
  16670. (":" . org-columns)
  16671. ("Outline Structure Editing")
  16672. ("U" . org-shiftmetaup)
  16673. ("D" . org-shiftmetadown)
  16674. ("r" . org-metaright)
  16675. ("l" . org-metaleft)
  16676. ("R" . org-shiftmetaright)
  16677. ("L" . org-shiftmetaleft)
  16678. ("i" . (progn (forward-char 1) (call-interactively
  16679. 'org-insert-heading-respect-content)))
  16680. ("^" . org-sort)
  16681. ("w" . org-refile)
  16682. ("a" . org-archive-subtree-default-with-confirmation)
  16683. ("." . org-mark-subtree)
  16684. ("#" . org-toggle-comment)
  16685. ("Clock Commands")
  16686. ("I" . org-clock-in)
  16687. ("O" . org-clock-out)
  16688. ("Meta Data Editing")
  16689. ("t" . org-todo)
  16690. ("," . (org-priority))
  16691. ("0" . (org-priority ?\ ))
  16692. ("1" . (org-priority ?A))
  16693. ("2" . (org-priority ?B))
  16694. ("3" . (org-priority ?C))
  16695. (";" . org-set-tags-command)
  16696. ("e" . org-set-effort)
  16697. ("E" . org-inc-effort)
  16698. ("W" . (lambda(m) (interactive "sMinutes before warning: ")
  16699. (org-entry-put (point) "APPT_WARNTIME" m)))
  16700. ("Agenda Views etc")
  16701. ("v" . org-agenda)
  16702. ("/" . org-sparse-tree)
  16703. ("Misc")
  16704. ("o" . org-open-at-point)
  16705. ("?" . org-speed-command-help)
  16706. ("<" . (org-agenda-set-restriction-lock 'subtree))
  16707. (">" . (org-agenda-remove-restriction-lock))
  16708. )
  16709. "The default speed commands.")
  16710. (defun org-print-speed-command (e)
  16711. (if (> (length (car e)) 1)
  16712. (progn
  16713. (princ "\n")
  16714. (princ (car e))
  16715. (princ "\n")
  16716. (princ (make-string (length (car e)) ?-))
  16717. (princ "\n"))
  16718. (princ (car e))
  16719. (princ " ")
  16720. (if (symbolp (cdr e))
  16721. (princ (symbol-name (cdr e)))
  16722. (prin1 (cdr e)))
  16723. (princ "\n")))
  16724. (defun org-speed-command-help ()
  16725. "Show the available speed commands."
  16726. (interactive)
  16727. (if (not org-use-speed-commands)
  16728. (error "Speed commands are not activated, customize `org-use-speed-commands'")
  16729. (with-output-to-temp-buffer "*Help*"
  16730. (princ "User-defined Speed commands\n===========================\n")
  16731. (mapc 'org-print-speed-command org-speed-commands-user)
  16732. (princ "\n")
  16733. (princ "Built-in Speed commands\n=======================\n")
  16734. (mapc 'org-print-speed-command org-speed-commands-default))
  16735. (with-current-buffer "*Help*"
  16736. (setq truncate-lines t))))
  16737. (defun org-speed-move-safe (cmd)
  16738. "Execute CMD, but make sure that the cursor always ends up in a headline.
  16739. If not, return to the original position and throw an error."
  16740. (interactive)
  16741. (let ((pos (point)))
  16742. (call-interactively cmd)
  16743. (unless (and (bolp) (org-at-heading-p))
  16744. (goto-char pos)
  16745. (error "Boundary reached while executing %s" cmd))))
  16746. (defvar org-self-insert-command-undo-counter 0)
  16747. (defvar org-table-auto-blank-field) ; defined in org-table.el
  16748. (defvar org-speed-command nil)
  16749. (define-obsolete-function-alias
  16750. 'org-speed-command-default-hook 'org-speed-command-activate "24.3")
  16751. (defun org-speed-command-activate (keys)
  16752. "Hook for activating single-letter speed commands.
  16753. `org-speed-commands-default' specifies a minimal command set.
  16754. Use `org-speed-commands-user' for further customization."
  16755. (when (or (and (bolp) (looking-at org-outline-regexp))
  16756. (and (functionp org-use-speed-commands)
  16757. (funcall org-use-speed-commands)))
  16758. (cdr (assoc keys (append org-speed-commands-user
  16759. org-speed-commands-default)))))
  16760. (define-obsolete-function-alias
  16761. 'org-babel-speed-command-hook 'org-babel-speed-command-activate "24.3")
  16762. (defun org-babel-speed-command-activate (keys)
  16763. "Hook for activating single-letter code block commands."
  16764. (when (and (bolp) (looking-at org-babel-src-block-regexp))
  16765. (cdr (assoc keys org-babel-key-bindings))))
  16766. (defcustom org-speed-command-hook
  16767. '(org-speed-command-default-hook org-babel-speed-command-hook)
  16768. "Hook for activating speed commands at strategic locations.
  16769. Hook functions are called in sequence until a valid handler is
  16770. found.
  16771. Each hook takes a single argument, a user-pressed command key
  16772. which is also a `self-insert-command' from the global map.
  16773. Within the hook, examine the cursor position and the command key
  16774. and return nil or a valid handler as appropriate. Handler could
  16775. be one of an interactive command, a function, or a form.
  16776. Set `org-use-speed-commands' to non-nil value to enable this
  16777. hook. The default setting is `org-speed-command-activate'."
  16778. :group 'org-structure
  16779. :version "24.1"
  16780. :type 'hook)
  16781. (defun org-self-insert-command (N)
  16782. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  16783. If the cursor is in a table looking at whitespace, the whitespace is
  16784. overwritten, and the table is not marked as requiring realignment."
  16785. (interactive "p")
  16786. (org-check-before-invisible-edit 'insert)
  16787. (cond
  16788. ((and org-use-speed-commands
  16789. (setq org-speed-command
  16790. (run-hook-with-args-until-success
  16791. 'org-speed-command-hook (this-command-keys))))
  16792. (cond
  16793. ((commandp org-speed-command)
  16794. (setq this-command org-speed-command)
  16795. (call-interactively org-speed-command))
  16796. ((functionp org-speed-command)
  16797. (funcall org-speed-command))
  16798. ((and org-speed-command (listp org-speed-command))
  16799. (eval org-speed-command))
  16800. (t (let (org-use-speed-commands)
  16801. (call-interactively 'org-self-insert-command)))))
  16802. ((and
  16803. (org-table-p)
  16804. (progn
  16805. ;; check if we blank the field, and if that triggers align
  16806. (and (featurep 'org-table) org-table-auto-blank-field
  16807. (member last-command
  16808. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c yas/expand))
  16809. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  16810. ;; got extra space, this field does not determine column width
  16811. (let (org-table-may-need-update) (org-table-blank-field))
  16812. ;; no extra space, this field may determine column width
  16813. (org-table-blank-field)))
  16814. t)
  16815. (eq N 1)
  16816. (looking-at "[^|\n]* |"))
  16817. (let (org-table-may-need-update)
  16818. (goto-char (1- (match-end 0)))
  16819. (backward-delete-char 1)
  16820. (goto-char (match-beginning 0))
  16821. (self-insert-command N)))
  16822. (t
  16823. (setq org-table-may-need-update t)
  16824. (self-insert-command N)
  16825. (org-fix-tags-on-the-fly)
  16826. (if org-self-insert-cluster-for-undo
  16827. (if (not (eq last-command 'org-self-insert-command))
  16828. (setq org-self-insert-command-undo-counter 1)
  16829. (if (>= org-self-insert-command-undo-counter 20)
  16830. (setq org-self-insert-command-undo-counter 1)
  16831. (and (> org-self-insert-command-undo-counter 0)
  16832. buffer-undo-list (listp buffer-undo-list)
  16833. (not (cadr buffer-undo-list)) ; remove nil entry
  16834. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  16835. (setq org-self-insert-command-undo-counter
  16836. (1+ org-self-insert-command-undo-counter))))))))
  16837. (defun org-check-before-invisible-edit (kind)
  16838. "Check is editing if kind KIND would be dangerous with invisible text around.
  16839. The detailed reaction depends on the user option `org-catch-invisible-edits'."
  16840. ;; First, try to get out of here as quickly as possible, to reduce overhead
  16841. (if (and org-catch-invisible-edits
  16842. (or (not (boundp 'visible-mode)) (not visible-mode))
  16843. (or (get-char-property (point) 'invisible)
  16844. (get-char-property (max (point-min) (1- (point))) 'invisible)))
  16845. ;; OK, we need to take a closer look
  16846. (let* ((invisible-at-point (get-char-property (point) 'invisible))
  16847. (invisible-before-point (if (bobp) nil (get-char-property
  16848. (1- (point)) 'invisible)))
  16849. (border-and-ok-direction
  16850. (or
  16851. ;; Check if we are acting predictably before invisible text
  16852. (and invisible-at-point (not invisible-before-point)
  16853. (memq kind '(insert delete-backward)))
  16854. ;; Check if we are acting predictably after invisible text
  16855. ;; This works not well, and I have turned it off. It seems
  16856. ;; better to always show and stop after invisible text.
  16857. ;; (and (not invisible-at-point) invisible-before-point
  16858. ;; (memq kind '(insert delete)))
  16859. )))
  16860. (when (or (memq invisible-at-point '(outline org-hide-block t))
  16861. (memq invisible-before-point '(outline org-hide-block t)))
  16862. (if (eq org-catch-invisible-edits 'error)
  16863. (error "Editing in invisible areas is prohibited - make visible first"))
  16864. (if (and org-custom-properties-overlays
  16865. (y-or-n-p "Display invisible properties in this buffer? "))
  16866. (org-toggle-custom-properties-visibility)
  16867. ;; Make the area visible
  16868. (save-excursion
  16869. (if invisible-before-point
  16870. (goto-char (previous-single-char-property-change
  16871. (point) 'invisible)))
  16872. (org-cycle))
  16873. (cond
  16874. ((eq org-catch-invisible-edits 'show)
  16875. ;; That's it, we do the edit after showing
  16876. (message
  16877. "Unfolding invisible region around point before editing")
  16878. (sit-for 1))
  16879. ((and (eq org-catch-invisible-edits 'smart)
  16880. border-and-ok-direction)
  16881. (message "Unfolding invisible region around point before editing"))
  16882. (t
  16883. ;; Don't do the edit, make the user repeat it in full visibility
  16884. (error "Edit in invisible region aborted, repeat to confirm with text visible"))))))))
  16885. (defun org-fix-tags-on-the-fly ()
  16886. (when (and (equal (char-after (point-at-bol)) ?*)
  16887. (org-at-heading-p))
  16888. (org-align-tags-here org-tags-column)))
  16889. (defun org-delete-backward-char (N)
  16890. "Like `delete-backward-char', insert whitespace at field end in tables.
  16891. When deleting backwards, in tables this function will insert whitespace in
  16892. front of the next \"|\" separator, to keep the table aligned. The table will
  16893. still be marked for re-alignment if the field did fill the entire column,
  16894. because, in this case the deletion might narrow the column."
  16895. (interactive "p")
  16896. (org-check-before-invisible-edit 'delete-backward)
  16897. (if (and (org-table-p)
  16898. (eq N 1)
  16899. (string-match "|" (buffer-substring (point-at-bol) (point)))
  16900. (looking-at ".*?|"))
  16901. (let ((pos (point))
  16902. (noalign (looking-at "[^|\n\r]* |"))
  16903. (c org-table-may-need-update))
  16904. (backward-delete-char N)
  16905. (if (not overwrite-mode)
  16906. (progn
  16907. (skip-chars-forward "^|")
  16908. (insert " ")
  16909. (goto-char (1- pos))))
  16910. ;; noalign: if there were two spaces at the end, this field
  16911. ;; does not determine the width of the column.
  16912. (if noalign (setq org-table-may-need-update c)))
  16913. (backward-delete-char N)
  16914. (org-fix-tags-on-the-fly)))
  16915. (defun org-delete-char (N)
  16916. "Like `delete-char', but insert whitespace at field end in tables.
  16917. When deleting characters, in tables this function will insert whitespace in
  16918. front of the next \"|\" separator, to keep the table aligned. The table will
  16919. still be marked for re-alignment if the field did fill the entire column,
  16920. because, in this case the deletion might narrow the column."
  16921. (interactive "p")
  16922. (org-check-before-invisible-edit 'delete)
  16923. (if (and (org-table-p)
  16924. (not (bolp))
  16925. (not (= (char-after) ?|))
  16926. (eq N 1))
  16927. (if (looking-at ".*?|")
  16928. (let ((pos (point))
  16929. (noalign (looking-at "[^|\n\r]* |"))
  16930. (c org-table-may-need-update))
  16931. (replace-match (concat
  16932. (substring (match-string 0) 1 -1)
  16933. " |"))
  16934. (goto-char pos)
  16935. ;; noalign: if there were two spaces at the end, this field
  16936. ;; does not determine the width of the column.
  16937. (if noalign (setq org-table-may-need-update c)))
  16938. (delete-char N))
  16939. (delete-char N)
  16940. (org-fix-tags-on-the-fly)))
  16941. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  16942. (put 'org-self-insert-command 'delete-selection t)
  16943. (put 'orgtbl-self-insert-command 'delete-selection t)
  16944. (put 'org-delete-char 'delete-selection 'supersede)
  16945. (put 'org-delete-backward-char 'delete-selection 'supersede)
  16946. (put 'org-yank 'delete-selection 'yank)
  16947. ;; Make `flyspell-mode' delay after some commands
  16948. (put 'org-self-insert-command 'flyspell-delayed t)
  16949. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  16950. (put 'org-delete-char 'flyspell-delayed t)
  16951. (put 'org-delete-backward-char 'flyspell-delayed t)
  16952. ;; Make pabbrev-mode expand after org-mode commands
  16953. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  16954. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  16955. ;; How to do this: Measure non-white length of current string
  16956. ;; If equal to column width, we should realign.
  16957. (defun org-remap (map &rest commands)
  16958. "In MAP, remap the functions given in COMMANDS.
  16959. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  16960. (let (new old)
  16961. (while commands
  16962. (setq old (pop commands) new (pop commands))
  16963. (if (fboundp 'command-remapping)
  16964. (org-defkey map (vector 'remap old) new)
  16965. (substitute-key-definition old new map global-map)))))
  16966. (when (eq org-enable-table-editor 'optimized)
  16967. ;; If the user wants maximum table support, we need to hijack
  16968. ;; some standard editing functions
  16969. (org-remap org-mode-map
  16970. 'self-insert-command 'org-self-insert-command
  16971. 'delete-char 'org-delete-char
  16972. 'delete-backward-char 'org-delete-backward-char)
  16973. (org-defkey org-mode-map "|" 'org-force-self-insert))
  16974. (defvar org-ctrl-c-ctrl-c-hook nil
  16975. "Hook for functions attaching themselves to `C-c C-c'.
  16976. This can be used to add additional functionality to the C-c C-c
  16977. key which executes context-dependent commands. This hook is run
  16978. before any other test, while `org-ctrl-c-ctrl-c-final-hook' is
  16979. run after the last test.
  16980. Each function will be called with no arguments. The function
  16981. must check if the context is appropriate for it to act. If yes,
  16982. it should do its thing and then return a non-nil value. If the
  16983. context is wrong, just do nothing and return nil.")
  16984. (defvar org-ctrl-c-ctrl-c-final-hook nil
  16985. "Hook for functions attaching themselves to `C-c C-c'.
  16986. This can be used to add additional functionality to the C-c C-c
  16987. key which executes context-dependent commands. This hook is run
  16988. after any other test, while `org-ctrl-c-ctrl-c-hook' is run
  16989. before the first test.
  16990. Each function will be called with no arguments. The function
  16991. must check if the context is appropriate for it to act. If yes,
  16992. it should do its thing and then return a non-nil value. If the
  16993. context is wrong, just do nothing and return nil.")
  16994. (defvar org-tab-first-hook nil
  16995. "Hook for functions to attach themselves to TAB.
  16996. See `org-ctrl-c-ctrl-c-hook' for more information.
  16997. This hook runs as the first action when TAB is pressed, even before
  16998. `org-cycle' messes around with the `outline-regexp' to cater for
  16999. inline tasks and plain list item folding.
  17000. If any function in this hook returns t, any other actions that
  17001. would have been caused by TAB (such as table field motion or visibility
  17002. cycling) will not occur.")
  17003. (defvar org-tab-after-check-for-table-hook nil
  17004. "Hook for functions to attach themselves to TAB.
  17005. See `org-ctrl-c-ctrl-c-hook' for more information.
  17006. This hook runs after it has been established that the cursor is not in a
  17007. table, but before checking if the cursor is in a headline or if global cycling
  17008. should be done.
  17009. If any function in this hook returns t, not other actions like visibility
  17010. cycling will be done.")
  17011. (defvar org-tab-after-check-for-cycling-hook nil
  17012. "Hook for functions to attach themselves to TAB.
  17013. See `org-ctrl-c-ctrl-c-hook' for more information.
  17014. This hook runs after it has been established that not table field motion and
  17015. not visibility should be done because of current context. This is probably
  17016. the place where a package like yasnippets can hook in.")
  17017. (defvar org-tab-before-tab-emulation-hook nil
  17018. "Hook for functions to attach themselves to TAB.
  17019. See `org-ctrl-c-ctrl-c-hook' for more information.
  17020. This hook runs after every other options for TAB have been exhausted, but
  17021. before indentation and \t insertion takes place.")
  17022. (defvar org-metaleft-hook nil
  17023. "Hook for functions attaching themselves to `M-left'.
  17024. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17025. (defvar org-metaright-hook nil
  17026. "Hook for functions attaching themselves to `M-right'.
  17027. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17028. (defvar org-metaup-hook nil
  17029. "Hook for functions attaching themselves to `M-up'.
  17030. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17031. (defvar org-metadown-hook nil
  17032. "Hook for functions attaching themselves to `M-down'.
  17033. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17034. (defvar org-shiftmetaleft-hook nil
  17035. "Hook for functions attaching themselves to `M-S-left'.
  17036. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17037. (defvar org-shiftmetaright-hook nil
  17038. "Hook for functions attaching themselves to `M-S-right'.
  17039. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17040. (defvar org-shiftmetaup-hook nil
  17041. "Hook for functions attaching themselves to `M-S-up'.
  17042. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17043. (defvar org-shiftmetadown-hook nil
  17044. "Hook for functions attaching themselves to `M-S-down'.
  17045. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17046. (defvar org-metareturn-hook nil
  17047. "Hook for functions attaching themselves to `M-RET'.
  17048. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17049. (defvar org-shiftup-hook nil
  17050. "Hook for functions attaching themselves to `S-up'.
  17051. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17052. (defvar org-shiftup-final-hook nil
  17053. "Hook for functions attaching themselves to `S-up'.
  17054. This one runs after all other options except shift-select have been excluded.
  17055. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17056. (defvar org-shiftdown-hook nil
  17057. "Hook for functions attaching themselves to `S-down'.
  17058. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17059. (defvar org-shiftdown-final-hook nil
  17060. "Hook for functions attaching themselves to `S-down'.
  17061. This one runs after all other options except shift-select have been excluded.
  17062. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17063. (defvar org-shiftleft-hook nil
  17064. "Hook for functions attaching themselves to `S-left'.
  17065. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17066. (defvar org-shiftleft-final-hook nil
  17067. "Hook for functions attaching themselves to `S-left'.
  17068. This one runs after all other options except shift-select have been excluded.
  17069. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17070. (defvar org-shiftright-hook nil
  17071. "Hook for functions attaching themselves to `S-right'.
  17072. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17073. (defvar org-shiftright-final-hook nil
  17074. "Hook for functions attaching themselves to `S-right'.
  17075. This one runs after all other options except shift-select have been excluded.
  17076. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17077. (defun org-modifier-cursor-error ()
  17078. "Throw an error, a modified cursor command was applied in wrong context."
  17079. (error "This command is active in special context like tables, headlines or items"))
  17080. (defun org-shiftselect-error ()
  17081. "Throw an error because Shift-Cursor command was applied in wrong context."
  17082. (if (and (boundp 'shift-select-mode) shift-select-mode)
  17083. (error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
  17084. (error "This command works only in special context like headlines or timestamps")))
  17085. (defun org-call-for-shift-select (cmd)
  17086. (let ((this-command-keys-shift-translated t))
  17087. (call-interactively cmd)))
  17088. (defun org-shifttab (&optional arg)
  17089. "Global visibility cycling or move to previous table field.
  17090. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  17091. on context.
  17092. See the individual commands for more information."
  17093. (interactive "P")
  17094. (cond
  17095. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  17096. ((integerp arg)
  17097. (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
  17098. (message "Content view to level: %d" arg)
  17099. (org-content (prefix-numeric-value arg2))
  17100. (setq org-cycle-global-status 'overview)))
  17101. (t (call-interactively 'org-global-cycle))))
  17102. (defun org-shiftmetaleft ()
  17103. "Promote subtree or delete table column.
  17104. Calls `org-promote-subtree', `org-outdent-item-tree', or
  17105. `org-table-delete-column', depending on context. See the
  17106. individual commands for more information."
  17107. (interactive)
  17108. (cond
  17109. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  17110. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  17111. ((org-at-heading-p) (call-interactively 'org-promote-subtree))
  17112. ((if (not (org-region-active-p)) (org-at-item-p)
  17113. (save-excursion (goto-char (region-beginning))
  17114. (org-at-item-p)))
  17115. (call-interactively 'org-outdent-item-tree))
  17116. (t (org-modifier-cursor-error))))
  17117. (defun org-shiftmetaright ()
  17118. "Demote subtree or insert table column.
  17119. Calls `org-demote-subtree', `org-indent-item-tree', or
  17120. `org-table-insert-column', depending on context. See the
  17121. individual commands for more information."
  17122. (interactive)
  17123. (cond
  17124. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  17125. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  17126. ((org-at-heading-p) (call-interactively 'org-demote-subtree))
  17127. ((if (not (org-region-active-p)) (org-at-item-p)
  17128. (save-excursion (goto-char (region-beginning))
  17129. (org-at-item-p)))
  17130. (call-interactively 'org-indent-item-tree))
  17131. (t (org-modifier-cursor-error))))
  17132. (defun org-shiftmetaup (&optional arg)
  17133. "Move subtree up or kill table row.
  17134. Calls `org-move-subtree-up' or `org-table-kill-row' or
  17135. `org-move-item-up' or `org-timestamp-up', depending on context.
  17136. See the individual commands for more information."
  17137. (interactive "P")
  17138. (cond
  17139. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  17140. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  17141. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  17142. ((org-at-item-p) (call-interactively 'org-move-item-up))
  17143. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  17144. (call-interactively 'org-timestamp-up)))
  17145. (t (org-modifier-cursor-error))))
  17146. (defun org-shiftmetadown (&optional arg)
  17147. "Move subtree down or insert table row.
  17148. Calls `org-move-subtree-down' or `org-table-insert-row' or
  17149. `org-move-item-down' or `org-timestamp-up', depending on context.
  17150. See the individual commands for more information."
  17151. (interactive "P")
  17152. (cond
  17153. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  17154. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  17155. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  17156. ((org-at-item-p) (call-interactively 'org-move-item-down))
  17157. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  17158. (call-interactively 'org-timestamp-down)))
  17159. (t (org-modifier-cursor-error))))
  17160. (defsubst org-hidden-tree-error ()
  17161. (error
  17162. "Hidden subtree, open with TAB or use subtree command M-S-<left>/<right>"))
  17163. (defun org-metaleft (&optional arg)
  17164. "Promote heading or move table column to left.
  17165. Calls `org-do-promote' or `org-table-move-column', depending on context.
  17166. With no specific context, calls the Emacs default `backward-word'.
  17167. See the individual commands for more information."
  17168. (interactive "P")
  17169. (cond
  17170. ((run-hook-with-args-until-success 'org-metaleft-hook))
  17171. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  17172. ((org-with-limited-levels
  17173. (or (org-at-heading-p)
  17174. (and (org-region-active-p)
  17175. (save-excursion
  17176. (goto-char (region-beginning))
  17177. (org-at-heading-p)))))
  17178. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  17179. (call-interactively 'org-do-promote))
  17180. ;; At an inline task.
  17181. ((org-at-heading-p)
  17182. (call-interactively 'org-inlinetask-promote))
  17183. ((or (org-at-item-p)
  17184. (and (org-region-active-p)
  17185. (save-excursion
  17186. (goto-char (region-beginning))
  17187. (org-at-item-p))))
  17188. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  17189. (call-interactively 'org-outdent-item))
  17190. (t (call-interactively 'backward-word))))
  17191. (defun org-metaright (&optional arg)
  17192. "Demote a subtree, a list item or move table column to right.
  17193. In front of a drawer or a block keyword, indent it correctly.
  17194. With no specific context, calls the Emacs default `forward-word'.
  17195. See the individual commands for more information."
  17196. (interactive "P")
  17197. (cond
  17198. ((run-hook-with-args-until-success 'org-metaright-hook))
  17199. ((org-at-table-p) (call-interactively 'org-table-move-column))
  17200. ((org-at-drawer-p) (call-interactively 'org-indent-drawer))
  17201. ((org-at-block-p) (call-interactively 'org-indent-block))
  17202. ((org-with-limited-levels
  17203. (or (org-at-heading-p)
  17204. (and (org-region-active-p)
  17205. (save-excursion
  17206. (goto-char (region-beginning))
  17207. (org-at-heading-p)))))
  17208. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  17209. (call-interactively 'org-do-demote))
  17210. ;; At an inline task.
  17211. ((org-at-heading-p)
  17212. (call-interactively 'org-inlinetask-demote))
  17213. ((or (org-at-item-p)
  17214. (and (org-region-active-p)
  17215. (save-excursion
  17216. (goto-char (region-beginning))
  17217. (org-at-item-p))))
  17218. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  17219. (call-interactively 'org-indent-item))
  17220. (t (call-interactively 'forward-word))))
  17221. (defun org-check-for-hidden (what)
  17222. "Check if there are hidden headlines/items in the current visual line.
  17223. WHAT can be either `headlines' or `items'. If the current line is
  17224. an outline or item heading and it has a folded subtree below it,
  17225. this function returns t, nil otherwise."
  17226. (let ((re (cond
  17227. ((eq what 'headlines) org-outline-regexp-bol)
  17228. ((eq what 'items) (org-item-beginning-re))
  17229. (t (error "This should not happen"))))
  17230. beg end)
  17231. (save-excursion
  17232. (catch 'exit
  17233. (unless (org-region-active-p)
  17234. (setq beg (point-at-bol))
  17235. (beginning-of-line 2)
  17236. (while (and (not (eobp)) ;; this is like `next-line'
  17237. (get-char-property (1- (point)) 'invisible))
  17238. (beginning-of-line 2))
  17239. (setq end (point))
  17240. (goto-char beg)
  17241. (goto-char (point-at-eol))
  17242. (setq end (max end (point)))
  17243. (while (re-search-forward re end t)
  17244. (if (get-char-property (match-beginning 0) 'invisible)
  17245. (throw 'exit t))))
  17246. nil))))
  17247. (autoload 'org-element-at-point "org-element")
  17248. (declare-function org-element-at-point "org-element" (&optional keep-trail))
  17249. (declare-function org-element-type "org-element" (element))
  17250. (declare-function org-element-context "org-element" ())
  17251. (declare-function org-element-contents "org-element" (element))
  17252. (declare-function org-element-property "org-element" (property element))
  17253. (declare-function org-element-paragraph-parser "org-element" (limit))
  17254. (declare-function org-element-map "org-element" (data types fun &optional info first-match no-recursion))
  17255. (declare-function org-element-nested-p "org-element" (elem-a elem-b))
  17256. (declare-function org-element-swap-A-B "org-element" (elem-a elem-b))
  17257. (declare-function org-element--parse-objects "org-element" (beg end acc restriction))
  17258. (declare-function org-element-parse-buffer "org-element" (&optional granularity visible-only))
  17259. (defun org-metaup (&optional arg)
  17260. "Move subtree up or move table row up.
  17261. Calls `org-move-subtree-up' or `org-table-move-row' or
  17262. `org-move-item-up', depending on context. See the individual commands
  17263. for more information."
  17264. (interactive "P")
  17265. (cond
  17266. ((run-hook-with-args-until-success 'org-metaup-hook))
  17267. ((org-region-active-p)
  17268. (let* ((a (min (region-beginning) (region-end)))
  17269. (b (1- (max (region-beginning) (region-end))))
  17270. (c (save-excursion (goto-char a)
  17271. (move-beginning-of-line 0)))
  17272. (d (save-excursion (goto-char a)
  17273. (move-end-of-line 0) (point))))
  17274. (transpose-regions a b c d)
  17275. (goto-char c)))
  17276. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  17277. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  17278. ((org-at-item-p) (call-interactively 'org-move-item-up))
  17279. (t (org-drag-element-backward))))
  17280. (defun org-metadown (&optional arg)
  17281. "Move subtree down or move table row down.
  17282. Calls `org-move-subtree-down' or `org-table-move-row' or
  17283. `org-move-item-down', depending on context. See the individual
  17284. commands for more information."
  17285. (interactive "P")
  17286. (cond
  17287. ((run-hook-with-args-until-success 'org-metadown-hook))
  17288. ((org-region-active-p)
  17289. (let* ((a (min (region-beginning) (region-end)))
  17290. (b (max (region-beginning) (region-end)))
  17291. (c (save-excursion (goto-char b)
  17292. (move-beginning-of-line 1)))
  17293. (d (save-excursion (goto-char b)
  17294. (move-end-of-line 1) (1+ (point)))))
  17295. (transpose-regions a b c d)
  17296. (goto-char d)))
  17297. ((org-at-table-p) (call-interactively 'org-table-move-row))
  17298. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  17299. ((org-at-item-p) (call-interactively 'org-move-item-down))
  17300. (t (org-drag-element-forward))))
  17301. (defun org-shiftup (&optional arg)
  17302. "Increase item in timestamp or increase priority of current headline.
  17303. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  17304. depending on context. See the individual commands for more information."
  17305. (interactive "P")
  17306. (cond
  17307. ((run-hook-with-args-until-success 'org-shiftup-hook))
  17308. ((and org-support-shift-select (org-region-active-p))
  17309. (org-call-for-shift-select 'previous-line))
  17310. ((org-at-timestamp-p t)
  17311. (call-interactively (if org-edit-timestamp-down-means-later
  17312. 'org-timestamp-down 'org-timestamp-up)))
  17313. ((and (not (eq org-support-shift-select 'always))
  17314. org-enable-priority-commands
  17315. (org-at-heading-p))
  17316. (call-interactively 'org-priority-up))
  17317. ((and (not org-support-shift-select) (org-at-item-p))
  17318. (call-interactively 'org-previous-item))
  17319. ((org-clocktable-try-shift 'up arg))
  17320. ((run-hook-with-args-until-success 'org-shiftup-final-hook))
  17321. (org-support-shift-select
  17322. (org-call-for-shift-select 'previous-line))
  17323. (t (org-shiftselect-error))))
  17324. (defun org-shiftdown (&optional arg)
  17325. "Decrease item in timestamp or decrease priority of current headline.
  17326. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  17327. depending on context. See the individual commands for more information."
  17328. (interactive "P")
  17329. (cond
  17330. ((run-hook-with-args-until-success 'org-shiftdown-hook))
  17331. ((and org-support-shift-select (org-region-active-p))
  17332. (org-call-for-shift-select 'next-line))
  17333. ((org-at-timestamp-p t)
  17334. (call-interactively (if org-edit-timestamp-down-means-later
  17335. 'org-timestamp-up 'org-timestamp-down)))
  17336. ((and (not (eq org-support-shift-select 'always))
  17337. org-enable-priority-commands
  17338. (org-at-heading-p))
  17339. (call-interactively 'org-priority-down))
  17340. ((and (not org-support-shift-select) (org-at-item-p))
  17341. (call-interactively 'org-next-item))
  17342. ((org-clocktable-try-shift 'down arg))
  17343. ((run-hook-with-args-until-success 'org-shiftdown-final-hook))
  17344. (org-support-shift-select
  17345. (org-call-for-shift-select 'next-line))
  17346. (t (org-shiftselect-error))))
  17347. (defun org-shiftright (&optional arg)
  17348. "Cycle the thing at point or in the current line, depending on context.
  17349. Depending on context, this does one of the following:
  17350. - switch a timestamp at point one day into the future
  17351. - on a headline, switch to the next TODO keyword.
  17352. - on an item, switch entire list to the next bullet type
  17353. - on a property line, switch to the next allowed value
  17354. - on a clocktable definition line, move time block into the future"
  17355. (interactive "P")
  17356. (cond
  17357. ((run-hook-with-args-until-success 'org-shiftright-hook))
  17358. ((and org-support-shift-select (org-region-active-p))
  17359. (org-call-for-shift-select 'forward-char))
  17360. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  17361. ((and (not (eq org-support-shift-select 'always))
  17362. (org-at-heading-p))
  17363. (let ((org-inhibit-logging
  17364. (not org-treat-S-cursor-todo-selection-as-state-change))
  17365. (org-inhibit-blocking
  17366. (not org-treat-S-cursor-todo-selection-as-state-change)))
  17367. (org-call-with-arg 'org-todo 'right)))
  17368. ((or (and org-support-shift-select
  17369. (not (eq org-support-shift-select 'always))
  17370. (org-at-item-bullet-p))
  17371. (and (not org-support-shift-select) (org-at-item-p)))
  17372. (org-call-with-arg 'org-cycle-list-bullet nil))
  17373. ((and (not (eq org-support-shift-select 'always))
  17374. (org-at-property-p))
  17375. (call-interactively 'org-property-next-allowed-value))
  17376. ((org-clocktable-try-shift 'right arg))
  17377. ((run-hook-with-args-until-success 'org-shiftright-final-hook))
  17378. (org-support-shift-select
  17379. (org-call-for-shift-select 'forward-char))
  17380. (t (org-shiftselect-error))))
  17381. (defun org-shiftleft (&optional arg)
  17382. "Cycle the thing at point or in the current line, depending on context.
  17383. Depending on context, this does one of the following:
  17384. - switch a timestamp at point one day into the past
  17385. - on a headline, switch to the previous TODO keyword.
  17386. - on an item, switch entire list to the previous bullet type
  17387. - on a property line, switch to the previous allowed value
  17388. - on a clocktable definition line, move time block into the past"
  17389. (interactive "P")
  17390. (cond
  17391. ((run-hook-with-args-until-success 'org-shiftleft-hook))
  17392. ((and org-support-shift-select (org-region-active-p))
  17393. (org-call-for-shift-select 'backward-char))
  17394. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  17395. ((and (not (eq org-support-shift-select 'always))
  17396. (org-at-heading-p))
  17397. (let ((org-inhibit-logging
  17398. (not org-treat-S-cursor-todo-selection-as-state-change))
  17399. (org-inhibit-blocking
  17400. (not org-treat-S-cursor-todo-selection-as-state-change)))
  17401. (org-call-with-arg 'org-todo 'left)))
  17402. ((or (and org-support-shift-select
  17403. (not (eq org-support-shift-select 'always))
  17404. (org-at-item-bullet-p))
  17405. (and (not org-support-shift-select) (org-at-item-p)))
  17406. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  17407. ((and (not (eq org-support-shift-select 'always))
  17408. (org-at-property-p))
  17409. (call-interactively 'org-property-previous-allowed-value))
  17410. ((org-clocktable-try-shift 'left arg))
  17411. ((run-hook-with-args-until-success 'org-shiftleft-final-hook))
  17412. (org-support-shift-select
  17413. (org-call-for-shift-select 'backward-char))
  17414. (t (org-shiftselect-error))))
  17415. (defun org-shiftcontrolright ()
  17416. "Switch to next TODO set."
  17417. (interactive)
  17418. (cond
  17419. ((and org-support-shift-select (org-region-active-p))
  17420. (org-call-for-shift-select 'forward-word))
  17421. ((and (not (eq org-support-shift-select 'always))
  17422. (org-at-heading-p))
  17423. (org-call-with-arg 'org-todo 'nextset))
  17424. (org-support-shift-select
  17425. (org-call-for-shift-select 'forward-word))
  17426. (t (org-shiftselect-error))))
  17427. (defun org-shiftcontrolleft ()
  17428. "Switch to previous TODO set."
  17429. (interactive)
  17430. (cond
  17431. ((and org-support-shift-select (org-region-active-p))
  17432. (org-call-for-shift-select 'backward-word))
  17433. ((and (not (eq org-support-shift-select 'always))
  17434. (org-at-heading-p))
  17435. (org-call-with-arg 'org-todo 'previousset))
  17436. (org-support-shift-select
  17437. (org-call-for-shift-select 'backward-word))
  17438. (t (org-shiftselect-error))))
  17439. (defun org-shiftcontrolup ()
  17440. "Change timestamps synchronously up in CLOCK log lines."
  17441. (interactive)
  17442. (cond ((and (not org-support-shift-select)
  17443. (org-at-clock-log-p)
  17444. (org-at-timestamp-p t))
  17445. (org-clock-timestamps-up))
  17446. (t (org-shiftselect-error))))
  17447. (defun org-shiftcontroldown ()
  17448. "Change timestamps synchronously down in CLOCK log lines."
  17449. (interactive)
  17450. (cond ((and (not org-support-shift-select)
  17451. (org-at-clock-log-p)
  17452. (org-at-timestamp-p t))
  17453. (org-clock-timestamps-down))
  17454. (t (org-shiftselect-error))))
  17455. (defun org-ctrl-c-ret ()
  17456. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  17457. (interactive)
  17458. (cond
  17459. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  17460. (t (call-interactively 'org-insert-heading))))
  17461. (defun org-find-visible ()
  17462. (let ((s (point)))
  17463. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  17464. (get-char-property s 'invisible)))
  17465. s))
  17466. (defun org-find-invisible ()
  17467. (let ((s (point)))
  17468. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  17469. (not (get-char-property s 'invisible))))
  17470. s))
  17471. (defun org-copy-visible (beg end)
  17472. "Copy the visible parts of the region."
  17473. (interactive "r")
  17474. (let (snippets s)
  17475. (save-excursion
  17476. (save-restriction
  17477. (narrow-to-region beg end)
  17478. (setq s (goto-char (point-min)))
  17479. (while (not (= (point) (point-max)))
  17480. (goto-char (org-find-invisible))
  17481. (push (buffer-substring s (point)) snippets)
  17482. (setq s (goto-char (org-find-visible))))))
  17483. (kill-new (apply 'concat (nreverse snippets)))))
  17484. (defun org-copy-special ()
  17485. "Copy region in table or copy current subtree.
  17486. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  17487. See the individual commands for more information."
  17488. (interactive)
  17489. (call-interactively
  17490. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  17491. (defun org-cut-special ()
  17492. "Cut region in table or cut current subtree.
  17493. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  17494. See the individual commands for more information."
  17495. (interactive)
  17496. (call-interactively
  17497. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  17498. (defun org-paste-special (arg)
  17499. "Paste rectangular region into table, or past subtree relative to level.
  17500. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  17501. See the individual commands for more information."
  17502. (interactive "P")
  17503. (if (org-at-table-p)
  17504. (org-table-paste-rectangle)
  17505. (org-paste-subtree arg)))
  17506. (defun org-edit-special (&optional arg)
  17507. "Call a special editor for the stuff at point.
  17508. When at a table, call the formula editor with `org-table-edit-formulas'.
  17509. When at the first line of an src example, call `org-edit-src-code'.
  17510. When in an #+include line, visit the include file. Otherwise call
  17511. `ffap' to visit the file at point."
  17512. (interactive)
  17513. ;; possibly prep session before editing source
  17514. (when arg
  17515. (let* ((info (org-babel-get-src-block-info))
  17516. (lang (nth 0 info))
  17517. (params (nth 2 info))
  17518. (session (cdr (assoc :session params))))
  17519. (when (and info session) ;; we are in a source-code block with a session
  17520. (funcall
  17521. (intern (concat "org-babel-prep-session:" lang)) session params))))
  17522. (cond ;; proceed with `org-edit-special'
  17523. ((save-excursion
  17524. (beginning-of-line 1)
  17525. (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
  17526. (find-file (org-trim (match-string 1))))
  17527. ((org-edit-src-code))
  17528. ((org-edit-fixed-width-region))
  17529. ((org-at-table.el-p)
  17530. (org-edit-src-code))
  17531. ((or (org-at-table-p)
  17532. (save-excursion
  17533. (beginning-of-line 1)
  17534. (let ((case-fold-search )) (looking-at "[ \t]*#\\+tblfm:"))))
  17535. (call-interactively 'org-table-edit-formulas))
  17536. (t (call-interactively 'ffap))))
  17537. (defvar org-table-coordinate-overlays) ; defined in org-table.el
  17538. (defun org-ctrl-c-ctrl-c (&optional arg)
  17539. "Set tags in headline, or update according to changed information at point.
  17540. This command does many different things, depending on context:
  17541. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  17542. this is what we do.
  17543. - If the cursor is on a statistics cookie, update it.
  17544. - If the cursor is in a headline, prompt for tags and insert them
  17545. into the current line, aligned to `org-tags-column'. When called
  17546. with prefix arg, realign all tags in the current buffer.
  17547. - If the cursor is in one of the special #+KEYWORD lines, this
  17548. triggers scanning the buffer for these lines and updating the
  17549. information.
  17550. - If the cursor is inside a table, realign the table. This command
  17551. works even if the automatic table editor has been turned off.
  17552. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  17553. the entire table.
  17554. - If the cursor is at a footnote reference or definition, jump to
  17555. the corresponding definition or references, respectively.
  17556. - If the cursor is a the beginning of a dynamic block, update it.
  17557. - If the current buffer is a capture buffer, close note and file it.
  17558. - If the cursor is on a <<<target>>>, update radio targets and
  17559. corresponding links in this buffer.
  17560. - If the cursor is on a numbered item in a plain list, renumber the
  17561. ordered list.
  17562. - If the cursor is on a checkbox, toggle it.
  17563. - If the cursor is on a code block, evaluate it. The variable
  17564. `org-confirm-babel-evaluate' can be used to control prompting
  17565. before code block evaluation, by default every code block
  17566. evaluation requires confirmation. Code block evaluation can be
  17567. inhibited by setting `org-babel-no-eval-on-ctrl-c-ctrl-c'."
  17568. (interactive "P")
  17569. (let ((org-enable-table-editor t))
  17570. (cond
  17571. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  17572. org-occur-highlights
  17573. org-latex-fragment-image-overlays)
  17574. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  17575. (org-remove-occur-highlights)
  17576. (org-remove-latex-fragment-image-overlays)
  17577. (message "Temporary highlights/overlays removed from current buffer"))
  17578. ((and (local-variable-p 'org-finish-function (current-buffer))
  17579. (fboundp org-finish-function))
  17580. (funcall org-finish-function))
  17581. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  17582. ((org-in-regexp org-ts-regexp-both)
  17583. (org-timestamp-change 0 'day))
  17584. ((or (looking-at org-property-start-re)
  17585. (org-at-property-p))
  17586. (call-interactively 'org-property-action))
  17587. ((org-at-target-p) (call-interactively 'org-update-radio-target-regexp))
  17588. ((and (org-in-regexp "\\[\\([0-9]*%\\|[0-9]*/[0-9]*\\)\\]")
  17589. (or (org-at-heading-p) (org-at-item-p)))
  17590. (call-interactively 'org-update-statistics-cookies))
  17591. ((org-at-heading-p) (call-interactively 'org-set-tags))
  17592. ((org-at-table.el-p)
  17593. (message "Use C-c ' to edit table.el tables"))
  17594. ((org-at-table-p)
  17595. (org-table-maybe-eval-formula)
  17596. (if arg
  17597. (call-interactively 'org-table-recalculate)
  17598. (org-table-maybe-recalculate-line))
  17599. (call-interactively 'org-table-align)
  17600. (orgtbl-send-table 'maybe))
  17601. ((or (org-footnote-at-reference-p)
  17602. (org-footnote-at-definition-p))
  17603. (call-interactively 'org-footnote-action))
  17604. ((org-at-item-checkbox-p)
  17605. ;; Cursor at a checkbox: repair list and update checkboxes. Send
  17606. ;; list only if at top item.
  17607. (let* ((cbox (match-string 1))
  17608. (struct (org-list-struct))
  17609. (old-struct (copy-tree struct))
  17610. (parents (org-list-parents-alist struct))
  17611. (orderedp (org-entry-get nil "ORDERED"))
  17612. (firstp (= (org-list-get-top-point struct) (point-at-bol)))
  17613. block-item)
  17614. ;; Use a light version of `org-toggle-checkbox' to avoid
  17615. ;; computing list structure twice.
  17616. (let ((new-box (cond
  17617. ((equal arg '(16)) "[-]")
  17618. ((equal arg '(4)) nil)
  17619. ((equal "[X]" cbox) "[ ]")
  17620. (t "[X]"))))
  17621. (if (and firstp arg)
  17622. ;; If at first item of sub-list, remove check-box from
  17623. ;; every item at the same level.
  17624. (mapc
  17625. (lambda (pos) (org-list-set-checkbox pos struct new-box))
  17626. (org-list-get-all-items
  17627. (point-at-bol) struct (org-list-prevs-alist struct)))
  17628. (org-list-set-checkbox (point-at-bol) struct new-box)))
  17629. ;; Replicate `org-list-write-struct', while grabbing a return
  17630. ;; value from `org-list-struct-fix-box'.
  17631. (org-list-struct-fix-ind struct parents 2)
  17632. (org-list-struct-fix-item-end struct)
  17633. (let ((prevs (org-list-prevs-alist struct)))
  17634. (org-list-struct-fix-bul struct prevs)
  17635. (org-list-struct-fix-ind struct parents)
  17636. (setq block-item
  17637. (org-list-struct-fix-box struct parents prevs orderedp)))
  17638. (org-list-struct-apply-struct struct old-struct)
  17639. (org-update-checkbox-count-maybe)
  17640. (when block-item
  17641. (message
  17642. "Checkboxes were removed due to unchecked box at line %d"
  17643. (org-current-line block-item)))
  17644. (when firstp (org-list-send-list 'maybe))))
  17645. ((org-at-item-p)
  17646. ;; Cursor at an item: repair list. Do checkbox related actions
  17647. ;; only if function was called with an argument. Send list only
  17648. ;; if at top item.
  17649. (let* ((struct (org-list-struct))
  17650. (firstp (= (org-list-get-top-point struct) (point-at-bol)))
  17651. old-struct)
  17652. (when arg
  17653. (setq old-struct (copy-tree struct))
  17654. (if firstp
  17655. ;; If at first item of sub-list, add check-box to every
  17656. ;; item at the same level.
  17657. (mapc
  17658. (lambda (pos)
  17659. (unless (org-list-get-checkbox pos struct)
  17660. (org-list-set-checkbox pos struct "[ ]")))
  17661. (org-list-get-all-items
  17662. (point-at-bol) struct (org-list-prevs-alist struct)))
  17663. (org-list-set-checkbox (point-at-bol) struct "[ ]")))
  17664. (org-list-write-struct
  17665. struct (org-list-parents-alist struct) old-struct)
  17666. (when arg (org-update-checkbox-count-maybe))
  17667. (when firstp (org-list-send-list 'maybe))))
  17668. ((save-excursion (beginning-of-line 1) (looking-at org-dblock-start-re))
  17669. ;; Dynamic block
  17670. (beginning-of-line 1)
  17671. (save-excursion (org-update-dblock)))
  17672. ((save-excursion
  17673. (let ((case-fold-search t))
  17674. (beginning-of-line 1)
  17675. (looking-at "[ \t]*#\\+\\([a-z]+\\)")))
  17676. (cond
  17677. ((or (equal (match-string 1) "TBLFM")
  17678. (equal (match-string 1) "tblfm"))
  17679. ;; Recalculate the table before this line
  17680. (save-excursion
  17681. (beginning-of-line 1)
  17682. (skip-chars-backward " \r\n\t")
  17683. (if (org-at-table-p)
  17684. (org-call-with-arg 'org-table-recalculate (or arg t)))))
  17685. (t
  17686. (let ((org-inhibit-startup-visibility-stuff t)
  17687. (org-startup-align-all-tables nil))
  17688. (when (boundp 'org-table-coordinate-overlays)
  17689. (mapc 'delete-overlay org-table-coordinate-overlays)
  17690. (setq org-table-coordinate-overlays nil))
  17691. (org-save-outline-visibility 'use-markers (org-mode-restart)))
  17692. (message "Local setup has been refreshed"))))
  17693. ((org-clock-update-time-maybe))
  17694. (t
  17695. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  17696. (error "C-c C-c can do nothing useful at this location"))))))
  17697. (defun org-mode-restart ()
  17698. "Restart Org-mode, to scan again for special lines.
  17699. Also updates the keyword regular expressions."
  17700. (interactive)
  17701. (org-mode)
  17702. (message "Org-mode restarted"))
  17703. (defun org-kill-note-or-show-branches ()
  17704. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  17705. (interactive)
  17706. (if (not org-finish-function)
  17707. (progn
  17708. (hide-subtree)
  17709. (call-interactively 'show-branches))
  17710. (let ((org-note-abort t))
  17711. (funcall org-finish-function))))
  17712. (defun org-return (&optional indent)
  17713. "Goto next table row or insert a newline.
  17714. Calls `org-table-next-row' or `newline', depending on context.
  17715. See the individual commands for more information."
  17716. (interactive)
  17717. (let (org-ts-what)
  17718. (cond
  17719. ((or (bobp) (org-in-src-block-p))
  17720. (if indent (newline-and-indent) (newline)))
  17721. ((org-at-table-p)
  17722. (org-table-justify-field-maybe)
  17723. (call-interactively 'org-table-next-row))
  17724. ;; when `newline-and-indent' is called within a list, make sure
  17725. ;; text moved stays inside the item.
  17726. ((and (org-in-item-p) indent)
  17727. (if (and (org-at-item-p) (>= (point) (match-end 0)))
  17728. (progn
  17729. (save-match-data (newline))
  17730. (org-indent-line-to (length (match-string 0))))
  17731. (let ((ind (org-get-indentation)))
  17732. (newline)
  17733. (if (org-looking-back org-list-end-re)
  17734. (org-indent-line)
  17735. (org-indent-line-to ind)))))
  17736. ((and org-return-follows-link
  17737. (org-at-timestamp-p t)
  17738. (not (eq org-ts-what 'after)))
  17739. (org-follow-timestamp-link))
  17740. ((and org-return-follows-link
  17741. (let ((tprop (get-text-property (point) 'face)))
  17742. (or (eq tprop 'org-link)
  17743. (and (listp tprop) (memq 'org-link tprop)))))
  17744. (call-interactively 'org-open-at-point))
  17745. ((and (org-at-heading-p)
  17746. (looking-at
  17747. (org-re "\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$")))
  17748. (org-show-entry)
  17749. (end-of-line 1)
  17750. (newline))
  17751. (t (if indent (newline-and-indent) (newline))))))
  17752. (defun org-return-indent ()
  17753. "Goto next table row or insert a newline and indent.
  17754. Calls `org-table-next-row' or `newline-and-indent', depending on
  17755. context. See the individual commands for more information."
  17756. (interactive)
  17757. (org-return t))
  17758. (defun org-ctrl-c-star ()
  17759. "Compute table, or change heading status of lines.
  17760. Calls `org-table-recalculate' or `org-toggle-heading',
  17761. depending on context."
  17762. (interactive)
  17763. (cond
  17764. ((org-at-table-p)
  17765. (call-interactively 'org-table-recalculate))
  17766. (t
  17767. ;; Convert all lines in region to list items
  17768. (call-interactively 'org-toggle-heading))))
  17769. (defun org-ctrl-c-minus ()
  17770. "Insert separator line in table or modify bullet status of line.
  17771. Also turns a plain line or a region of lines into list items.
  17772. Calls `org-table-insert-hline', `org-toggle-item', or
  17773. `org-cycle-list-bullet', depending on context."
  17774. (interactive)
  17775. (cond
  17776. ((org-at-table-p)
  17777. (call-interactively 'org-table-insert-hline))
  17778. ((org-region-active-p)
  17779. (call-interactively 'org-toggle-item))
  17780. ((org-in-item-p)
  17781. (call-interactively 'org-cycle-list-bullet))
  17782. (t
  17783. (call-interactively 'org-toggle-item))))
  17784. (defun org-toggle-item (arg)
  17785. "Convert headings or normal lines to items, items to normal lines.
  17786. If there is no active region, only the current line is considered.
  17787. If the first non blank line in the region is an headline, convert
  17788. all headlines to items, shifting text accordingly.
  17789. If it is an item, convert all items to normal lines.
  17790. If it is normal text, change region into an item. With a prefix
  17791. argument ARG, change each line in region into an item."
  17792. (interactive "P")
  17793. (let ((shift-text
  17794. (function
  17795. ;; Shift text in current section to IND, from point to END.
  17796. ;; The function leaves point to END line.
  17797. (lambda (ind end)
  17798. (let ((min-i 1000) (end (copy-marker end)))
  17799. ;; First determine the minimum indentation (MIN-I) of
  17800. ;; the text.
  17801. (save-excursion
  17802. (catch 'exit
  17803. (while (< (point) end)
  17804. (let ((i (org-get-indentation)))
  17805. (cond
  17806. ;; Skip blank lines and inline tasks.
  17807. ((looking-at "^[ \t]*$"))
  17808. ((looking-at org-outline-regexp-bol))
  17809. ;; We can't find less than 0 indentation.
  17810. ((zerop i) (throw 'exit (setq min-i 0)))
  17811. ((< i min-i) (setq min-i i))))
  17812. (forward-line))))
  17813. ;; Then indent each line so that a line indented to
  17814. ;; MIN-I becomes indented to IND. Ignore blank lines
  17815. ;; and inline tasks in the process.
  17816. (let ((delta (- ind min-i)))
  17817. (while (< (point) end)
  17818. (unless (or (looking-at "^[ \t]*$")
  17819. (looking-at org-outline-regexp-bol))
  17820. (org-indent-line-to (+ (org-get-indentation) delta)))
  17821. (forward-line)))))))
  17822. (skip-blanks
  17823. (function
  17824. ;; Return beginning of first non-blank line, starting from
  17825. ;; line at POS.
  17826. (lambda (pos)
  17827. (save-excursion
  17828. (goto-char pos)
  17829. (skip-chars-forward " \r\t\n")
  17830. (point-at-bol)))))
  17831. beg end)
  17832. ;; Determine boundaries of changes.
  17833. (if (org-region-active-p)
  17834. (setq beg (funcall skip-blanks (region-beginning))
  17835. end (copy-marker (region-end)))
  17836. (setq beg (funcall skip-blanks (point-at-bol))
  17837. end (copy-marker (point-at-eol))))
  17838. ;; Depending on the starting line, choose an action on the text
  17839. ;; between BEG and END.
  17840. (org-with-limited-levels
  17841. (save-excursion
  17842. (goto-char beg)
  17843. (cond
  17844. ;; Case 1. Start at an item: de-itemize. Note that it only
  17845. ;; happens when a region is active: `org-ctrl-c-minus'
  17846. ;; would call `org-cycle-list-bullet' otherwise.
  17847. ((org-at-item-p)
  17848. (while (< (point) end)
  17849. (when (org-at-item-p)
  17850. (skip-chars-forward " \t")
  17851. (delete-region (point) (match-end 0)))
  17852. (forward-line)))
  17853. ;; Case 2. Start at an heading: convert to items.
  17854. ((org-at-heading-p)
  17855. (let* ((bul (org-list-bullet-string "-"))
  17856. (bul-len (length bul))
  17857. ;; Indentation of the first heading. It should be
  17858. ;; relative to the indentation of its parent, if any.
  17859. (start-ind (save-excursion
  17860. (cond
  17861. ((not org-adapt-indentation) 0)
  17862. ((not (outline-previous-heading)) 0)
  17863. (t (length (match-string 0))))))
  17864. ;; Level of first heading. Further headings will be
  17865. ;; compared to it to determine hierarchy in the list.
  17866. (ref-level (org-reduced-level (org-outline-level))))
  17867. (while (< (point) end)
  17868. (let* ((level (org-reduced-level (org-outline-level)))
  17869. (delta (max 0 (- level ref-level))))
  17870. ;; If current headline is less indented than the first
  17871. ;; one, set it as reference, in order to preserve
  17872. ;; subtrees.
  17873. (when (< level ref-level) (setq ref-level level))
  17874. (replace-match bul t t)
  17875. (org-indent-line-to (+ start-ind (* delta bul-len)))
  17876. ;; Ensure all text down to END (or SECTION-END) belongs
  17877. ;; to the newly created item.
  17878. (let ((section-end (save-excursion
  17879. (or (outline-next-heading) (point)))))
  17880. (forward-line)
  17881. (funcall shift-text
  17882. (+ start-ind (* (1+ delta) bul-len))
  17883. (min end section-end)))))))
  17884. ;; Case 3. Normal line with ARG: turn each non-item line into
  17885. ;; an item.
  17886. (arg
  17887. (while (< (point) end)
  17888. (unless (or (org-at-heading-p) (org-at-item-p))
  17889. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  17890. (replace-match
  17891. (concat "\\1" (org-list-bullet-string "-") "\\2"))))
  17892. (forward-line)))
  17893. ;; Case 4. Normal line without ARG: make the first line of
  17894. ;; region an item, and shift indentation of others
  17895. ;; lines to set them as item's body.
  17896. (t (let* ((bul (org-list-bullet-string "-"))
  17897. (bul-len (length bul))
  17898. (ref-ind (org-get-indentation)))
  17899. (skip-chars-forward " \t")
  17900. (insert bul)
  17901. (forward-line)
  17902. (while (< (point) end)
  17903. ;; Ensure that lines less indented than first one
  17904. ;; still get included in item body.
  17905. (funcall shift-text
  17906. (+ ref-ind bul-len)
  17907. (min end (save-excursion (or (outline-next-heading)
  17908. (point)))))
  17909. (forward-line)))))))))
  17910. (defun org-toggle-heading (&optional nstars)
  17911. "Convert headings to normal text, or items or text to headings.
  17912. If there is no active region, only the current line is considered.
  17913. With a \\[universal-argument] prefix, convert the whole list at
  17914. point into heading.
  17915. In a region:
  17916. - If the first non blank line is an headline, remove the stars
  17917. from all headlines in the region.
  17918. - If it is a normal line turn each and every normal line (i.e. not an
  17919. heading or an item) in the region into a heading.
  17920. - If it is a plain list item, turn all plain list items into headings.
  17921. When converting a line into a heading, the number of stars is chosen
  17922. such that the lines become children of the current entry. However,
  17923. when a prefix argument is given, its value determines the number of
  17924. stars to add."
  17925. (interactive "P")
  17926. (let ((skip-blanks
  17927. (function
  17928. ;; Return beginning of first non-blank line, starting from
  17929. ;; line at POS.
  17930. (lambda (pos)
  17931. (save-excursion
  17932. (goto-char pos)
  17933. (while (org-at-comment-p) (forward-line))
  17934. (skip-chars-forward " \r\t\n")
  17935. (point-at-bol)))))
  17936. beg end toggled)
  17937. ;; Determine boundaries of changes. If a universal prefix has
  17938. ;; been given, put the list in a region. If region ends at a bol,
  17939. ;; do not consider the last line to be in the region.
  17940. (when (and current-prefix-arg (org-at-item-p))
  17941. (if (equal current-prefix-arg '(4)) (setq current-prefix-arg 1))
  17942. (org-mark-element))
  17943. (if (org-region-active-p)
  17944. (setq beg (funcall skip-blanks (region-beginning))
  17945. end (copy-marker (save-excursion
  17946. (goto-char (region-end))
  17947. (if (bolp) (point) (point-at-eol)))))
  17948. (setq beg (funcall skip-blanks (point-at-bol))
  17949. end (copy-marker (point-at-eol))))
  17950. ;; Ensure inline tasks don't count as headings.
  17951. (org-with-limited-levels
  17952. (save-excursion
  17953. (goto-char beg)
  17954. (cond
  17955. ;; Case 1. Started at an heading: de-star headings.
  17956. ((org-at-heading-p)
  17957. (while (< (point) end)
  17958. (when (org-at-heading-p t)
  17959. (looking-at org-outline-regexp) (replace-match "")
  17960. (setq toggled t))
  17961. (forward-line)))
  17962. ;; Case 2. Started at an item: change items into headlines.
  17963. ;; One star will be added by `org-list-to-subtree'.
  17964. ((org-at-item-p)
  17965. (let* ((stars (make-string
  17966. (if nstars
  17967. ;; subtract the star that will be added again by
  17968. ;; `org-list-to-subtree'
  17969. (1- (prefix-numeric-value current-prefix-arg))
  17970. (or (org-current-level) 0))
  17971. ?*))
  17972. (add-stars
  17973. (cond (nstars "") ; stars from prefix only
  17974. ((equal stars "") "") ; before first heading
  17975. (org-odd-levels-only "*") ; inside heading, odd
  17976. (t "")))) ; inside heading, oddeven
  17977. (while (< (point) end)
  17978. (when (org-at-item-p)
  17979. ;; Pay attention to cases when region ends before list.
  17980. (let* ((struct (org-list-struct))
  17981. (list-end (min (org-list-get-bottom-point struct) (1+ end))))
  17982. (save-restriction
  17983. (narrow-to-region (point) list-end)
  17984. (insert
  17985. (org-list-to-subtree
  17986. (org-list-parse-list t)
  17987. '(:istart (concat stars add-stars (funcall get-stars depth))
  17988. :icount (concat stars add-stars (funcall get-stars depth)))))))
  17989. (setq toggled t))
  17990. (forward-line))))
  17991. ;; Case 3. Started at normal text: make every line an heading,
  17992. ;; skipping headlines and items.
  17993. (t (let* ((stars (make-string
  17994. (if nstars
  17995. (prefix-numeric-value current-prefix-arg)
  17996. (or (org-current-level) 0))
  17997. ?*))
  17998. (add-stars
  17999. (cond (nstars "") ; stars from prefix only
  18000. ((equal stars "") "*") ; before first heading
  18001. (org-odd-levels-only "**") ; inside heading, odd
  18002. (t "*"))) ; inside heading, oddeven
  18003. (rpl (concat stars add-stars " ")))
  18004. (while (< (point) end)
  18005. (when (and (not (or (org-at-heading-p) (org-at-item-p) (org-at-comment-p)))
  18006. (looking-at "\\([ \t]*\\)\\(\\S-\\)"))
  18007. (replace-match (concat rpl (match-string 2))) (setq toggled t))
  18008. (forward-line)))))))
  18009. (unless toggled (message "Cannot toggle heading from here"))))
  18010. (defun org-meta-return (&optional arg)
  18011. "Insert a new heading or wrap a region in a table.
  18012. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  18013. See the individual commands for more information."
  18014. (interactive "P")
  18015. (cond
  18016. ((run-hook-with-args-until-success 'org-metareturn-hook))
  18017. ((or (org-at-drawer-p) (org-at-property-p))
  18018. (newline-and-indent))
  18019. ((org-at-table-p)
  18020. (call-interactively 'org-table-wrap-region))
  18021. (t (call-interactively 'org-insert-heading))))
  18022. ;;; Menu entries
  18023. (defsubst org-in-subtree-not-table-p ()
  18024. "Are we in a subtree and not in a table?"
  18025. (and (not (org-before-first-heading-p))
  18026. (not (org-at-table-p))))
  18027. ;; Define the Org-mode menus
  18028. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  18029. '("Tbl"
  18030. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  18031. ["Next Field" org-cycle (org-at-table-p)]
  18032. ["Previous Field" org-shifttab (org-at-table-p)]
  18033. ["Next Row" org-return (org-at-table-p)]
  18034. "--"
  18035. ["Blank Field" org-table-blank-field (org-at-table-p)]
  18036. ["Edit Field" org-table-edit-field (org-at-table-p)]
  18037. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  18038. "--"
  18039. ("Column"
  18040. ["Move Column Left" org-metaleft (org-at-table-p)]
  18041. ["Move Column Right" org-metaright (org-at-table-p)]
  18042. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  18043. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  18044. ("Row"
  18045. ["Move Row Up" org-metaup (org-at-table-p)]
  18046. ["Move Row Down" org-metadown (org-at-table-p)]
  18047. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  18048. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  18049. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  18050. "--"
  18051. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  18052. ("Rectangle"
  18053. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  18054. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  18055. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  18056. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  18057. "--"
  18058. ("Calculate"
  18059. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  18060. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  18061. ["Edit Formulas" org-edit-special (org-at-table-p)]
  18062. "--"
  18063. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  18064. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  18065. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  18066. "--"
  18067. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  18068. "--"
  18069. ["Sum Column/Rectangle" org-table-sum
  18070. (or (org-at-table-p) (org-region-active-p))]
  18071. ["Which Column?" org-table-current-column (org-at-table-p)])
  18072. ["Debug Formulas"
  18073. org-table-toggle-formula-debugger
  18074. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  18075. ["Show Col/Row Numbers"
  18076. org-table-toggle-coordinate-overlays
  18077. :style toggle
  18078. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  18079. "--"
  18080. ["Create" org-table-create (and (not (org-at-table-p))
  18081. org-enable-table-editor)]
  18082. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  18083. ["Import from File" org-table-import (not (org-at-table-p))]
  18084. ["Export to File" org-table-export (org-at-table-p)]
  18085. "--"
  18086. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  18087. (easy-menu-define org-org-menu org-mode-map "Org menu"
  18088. '("Org"
  18089. ("Show/Hide"
  18090. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  18091. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  18092. ["Sparse Tree..." org-sparse-tree t]
  18093. ["Reveal Context" org-reveal t]
  18094. ["Show All" show-all t]
  18095. "--"
  18096. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  18097. "--"
  18098. ["New Heading" org-insert-heading t]
  18099. ("Navigate Headings"
  18100. ["Up" outline-up-heading t]
  18101. ["Next" outline-next-visible-heading t]
  18102. ["Previous" outline-previous-visible-heading t]
  18103. ["Next Same Level" outline-forward-same-level t]
  18104. ["Previous Same Level" outline-backward-same-level t]
  18105. "--"
  18106. ["Jump" org-goto t])
  18107. ("Edit Structure"
  18108. ["Refile Subtree" org-refile (org-in-subtree-not-table-p)]
  18109. "--"
  18110. ["Move Subtree Up" org-shiftmetaup (org-in-subtree-not-table-p)]
  18111. ["Move Subtree Down" org-shiftmetadown (org-in-subtree-not-table-p)]
  18112. "--"
  18113. ["Copy Subtree" org-copy-special (org-in-subtree-not-table-p)]
  18114. ["Cut Subtree" org-cut-special (org-in-subtree-not-table-p)]
  18115. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  18116. "--"
  18117. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  18118. "--"
  18119. ["Copy visible text" org-copy-visible t]
  18120. "--"
  18121. ["Promote Heading" org-metaleft (org-in-subtree-not-table-p)]
  18122. ["Promote Subtree" org-shiftmetaleft (org-in-subtree-not-table-p)]
  18123. ["Demote Heading" org-metaright (org-in-subtree-not-table-p)]
  18124. ["Demote Subtree" org-shiftmetaright (org-in-subtree-not-table-p)]
  18125. "--"
  18126. ["Sort Region/Children" org-sort t]
  18127. "--"
  18128. ["Convert to odd levels" org-convert-to-odd-levels t]
  18129. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  18130. ("Editing"
  18131. ["Emphasis..." org-emphasize t]
  18132. ["Edit Source Example" org-edit-special t]
  18133. "--"
  18134. ["Footnote new/jump" org-footnote-action t]
  18135. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  18136. ("Archive"
  18137. ["Archive (default method)" org-archive-subtree-default (org-in-subtree-not-table-p)]
  18138. "--"
  18139. ["Move Subtree to Archive file" org-advertized-archive-subtree (org-in-subtree-not-table-p)]
  18140. ["Toggle ARCHIVE tag" org-toggle-archive-tag (org-in-subtree-not-table-p)]
  18141. ["Move subtree to Archive sibling" org-archive-to-archive-sibling (org-in-subtree-not-table-p)]
  18142. )
  18143. "--"
  18144. ("Hyperlinks"
  18145. ["Store Link (Global)" org-store-link t]
  18146. ["Find existing link to here" org-occur-link-in-agenda-files t]
  18147. ["Insert Link" org-insert-link t]
  18148. ["Follow Link" org-open-at-point t]
  18149. "--"
  18150. ["Next link" org-next-link t]
  18151. ["Previous link" org-previous-link t]
  18152. "--"
  18153. ["Descriptive Links"
  18154. org-toggle-link-display
  18155. :style radio
  18156. :selected org-descriptive-links
  18157. ]
  18158. ["Literal Links"
  18159. org-toggle-link-display
  18160. :style radio
  18161. :selected (not org-descriptive-links)])
  18162. "--"
  18163. ("TODO Lists"
  18164. ["TODO/DONE/-" org-todo t]
  18165. ("Select keyword"
  18166. ["Next keyword" org-shiftright (org-at-heading-p)]
  18167. ["Previous keyword" org-shiftleft (org-at-heading-p)]
  18168. ["Complete Keyword" pcomplete (assq :todo-keyword (org-context))]
  18169. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))]
  18170. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))])
  18171. ["Show TODO Tree" org-show-todo-tree :active t :keys "C-c / t"]
  18172. ["Global TODO list" org-todo-list :active t :keys "C-c a t"]
  18173. "--"
  18174. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  18175. :selected org-enforce-todo-dependencies :style toggle :active t]
  18176. "Settings for tree at point"
  18177. ["Do Children sequentially" org-toggle-ordered-property :style radio
  18178. :selected (org-entry-get nil "ORDERED")
  18179. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  18180. ["Do Children parallel" org-toggle-ordered-property :style radio
  18181. :selected (not (org-entry-get nil "ORDERED"))
  18182. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  18183. "--"
  18184. ["Set Priority" org-priority t]
  18185. ["Priority Up" org-shiftup t]
  18186. ["Priority Down" org-shiftdown t]
  18187. "--"
  18188. ["Get news from all feeds" org-feed-update-all t]
  18189. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  18190. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  18191. ("TAGS and Properties"
  18192. ["Set Tags" org-set-tags-command (not (org-before-first-heading-p))]
  18193. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  18194. "--"
  18195. ["Set property" org-set-property (not (org-before-first-heading-p))]
  18196. ["Column view of properties" org-columns t]
  18197. ["Insert Column View DBlock" org-insert-columns-dblock t])
  18198. ("Dates and Scheduling"
  18199. ["Timestamp" org-time-stamp (not (org-before-first-heading-p))]
  18200. ["Timestamp (inactive)" org-time-stamp-inactive (not (org-before-first-heading-p))]
  18201. ("Change Date"
  18202. ["1 Day Later" org-shiftright (org-at-timestamp-p)]
  18203. ["1 Day Earlier" org-shiftleft (org-at-timestamp-p)]
  18204. ["1 ... Later" org-shiftup (org-at-timestamp-p)]
  18205. ["1 ... Earlier" org-shiftdown (org-at-timestamp-p)])
  18206. ["Compute Time Range" org-evaluate-time-range t]
  18207. ["Schedule Item" org-schedule (not (org-before-first-heading-p))]
  18208. ["Deadline" org-deadline (not (org-before-first-heading-p))]
  18209. "--"
  18210. ["Custom time format" org-toggle-time-stamp-overlays
  18211. :style radio :selected org-display-custom-times]
  18212. "--"
  18213. ["Goto Calendar" org-goto-calendar t]
  18214. ["Date from Calendar" org-date-from-calendar t]
  18215. "--"
  18216. ["Start/Restart Timer" org-timer-start t]
  18217. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  18218. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  18219. ["Insert Timer String" org-timer t]
  18220. ["Insert Timer Item" org-timer-item t])
  18221. ("Logging work"
  18222. ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
  18223. ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
  18224. ["Clock out" org-clock-out t]
  18225. ["Clock cancel" org-clock-cancel t]
  18226. "--"
  18227. ["Mark as default task" org-clock-mark-default-task t]
  18228. ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
  18229. ["Goto running clock" org-clock-goto t]
  18230. "--"
  18231. ["Display times" org-clock-display t]
  18232. ["Create clock table" org-clock-report t]
  18233. "--"
  18234. ["Record DONE time"
  18235. (progn (setq org-log-done (not org-log-done))
  18236. (message "Switching to %s will %s record a timestamp"
  18237. (car org-done-keywords)
  18238. (if org-log-done "automatically" "not")))
  18239. :style toggle :selected org-log-done])
  18240. "--"
  18241. ["Agenda Command..." org-agenda t]
  18242. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  18243. ("File List for Agenda")
  18244. ("Special views current file"
  18245. ["TODO Tree" org-show-todo-tree t]
  18246. ["Check Deadlines" org-check-deadlines t]
  18247. ["Timeline" org-timeline t]
  18248. ["Tags/Property tree" org-match-sparse-tree t])
  18249. "--"
  18250. ["Export/Publish..." org-export t]
  18251. ("LaTeX"
  18252. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  18253. :selected org-cdlatex-mode]
  18254. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  18255. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  18256. ["Modify math symbol" org-cdlatex-math-modify
  18257. (org-inside-LaTeX-fragment-p)]
  18258. ["Insert citation" org-reftex-citation t]
  18259. "--"
  18260. ["Template for BEAMER" (progn (require 'org-beamer)
  18261. (org-insert-beamer-options-template)) t])
  18262. "--"
  18263. ("MobileOrg"
  18264. ["Push Files and Views" org-mobile-push t]
  18265. ["Get Captured and Flagged" org-mobile-pull t]
  18266. ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
  18267. "--"
  18268. ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
  18269. "--"
  18270. ("Documentation"
  18271. ["Show Version" org-version t]
  18272. ["Info Documentation" org-info t])
  18273. ("Customize"
  18274. ["Browse Org Group" org-customize t]
  18275. "--"
  18276. ["Expand This Menu" org-create-customize-menu
  18277. (fboundp 'customize-menu-create)])
  18278. ["Send bug report" org-submit-bug-report t]
  18279. "--"
  18280. ("Refresh/Reload"
  18281. ["Refresh setup current buffer" org-mode-restart t]
  18282. ["Reload Org (after update)" org-reload t]
  18283. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x r"])
  18284. ))
  18285. (defun org-info (&optional node)
  18286. "Read documentation for Org-mode in the info system.
  18287. With optional NODE, go directly to that node."
  18288. (interactive)
  18289. (info (format "(org)%s" (or node ""))))
  18290. ;;;###autoload
  18291. (defun org-submit-bug-report ()
  18292. "Submit a bug report on Org-mode via mail.
  18293. Don't hesitate to report any problems or inaccurate documentation.
  18294. If you don't have setup sending mail from (X)Emacs, please copy the
  18295. output buffer into your mail program, as it gives us important
  18296. information about your Org-mode version and configuration."
  18297. (interactive)
  18298. (require 'reporter)
  18299. (org-load-modules-maybe)
  18300. (org-require-autoloaded-modules)
  18301. (let ((reporter-prompt-for-summary-p "Bug report subject: "))
  18302. (reporter-submit-bug-report
  18303. "emacs-orgmode@gnu.org"
  18304. (org-version nil 'full)
  18305. (let (list)
  18306. (save-window-excursion
  18307. (org-pop-to-buffer-same-window (get-buffer-create "*Warn about privacy*"))
  18308. (delete-other-windows)
  18309. (erase-buffer)
  18310. (insert "You are about to submit a bug report to the Org-mode mailing list.
  18311. We would like to add your full Org-mode and Outline configuration to the
  18312. bug report. This greatly simplifies the work of the maintainer and
  18313. other experts on the mailing list.
  18314. HOWEVER, some variables you have customized may contain private
  18315. information. The names of customers, colleagues, or friends, might
  18316. appear in the form of file names, tags, todo states, or search strings.
  18317. If you answer yes to the prompt, you might want to check and remove
  18318. such private information before sending the email.")
  18319. (add-text-properties (point-min) (point-max) '(face org-warning))
  18320. (when (yes-or-no-p "Include your Org-mode configuration ")
  18321. (mapatoms
  18322. (lambda (v)
  18323. (and (boundp v)
  18324. (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
  18325. (or (and (symbol-value v)
  18326. (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
  18327. (and
  18328. (get v 'custom-type) (get v 'standard-value)
  18329. (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
  18330. (push v list)))))
  18331. (kill-buffer (get-buffer "*Warn about privacy*"))
  18332. list))
  18333. nil nil
  18334. "Remember to cover the basics, that is, what you expected to happen and
  18335. what in fact did happen. You don't know how to make a good report? See
  18336. http://orgmode.org/manual/Feedback.html#Feedback
  18337. Your bug report will be posted to the Org-mode mailing list.
  18338. ------------------------------------------------------------------------")
  18339. (save-excursion
  18340. (if (re-search-backward "^\\(Subject: \\)Org-mode version \\(.*?\\);[ \t]*\\(.*\\)" nil t)
  18341. (replace-match "\\1Bug: \\3 [\\2]")))))
  18342. (defun org-install-agenda-files-menu ()
  18343. (let ((bl (buffer-list)))
  18344. (save-excursion
  18345. (while bl
  18346. (set-buffer (pop bl))
  18347. (if (derived-mode-p 'org-mode) (setq bl nil)))
  18348. (when (derived-mode-p 'org-mode)
  18349. (easy-menu-change
  18350. '("Org") "File List for Agenda"
  18351. (append
  18352. (list
  18353. ["Edit File List" (org-edit-agenda-file-list) t]
  18354. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  18355. ["Remove Current File from List" org-remove-file t]
  18356. ["Cycle through agenda files" org-cycle-agenda-files t]
  18357. ["Occur in all agenda files" org-occur-in-agenda-files t]
  18358. "--")
  18359. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  18360. ;;;; Documentation
  18361. (defun org-require-autoloaded-modules ()
  18362. (interactive)
  18363. (mapc 'require
  18364. '(org-agenda org-archive org-ascii org-attach org-clock org-colview
  18365. org-docbook org-exp org-html org-icalendar
  18366. org-id org-latex
  18367. org-publish org-remember org-table
  18368. org-timer org-xoxo)))
  18369. ;;;###autoload
  18370. (defun org-reload (&optional uncompiled)
  18371. "Reload all org lisp files.
  18372. With prefix arg UNCOMPILED, load the uncompiled versions."
  18373. (interactive "P")
  18374. (require 'loadhist)
  18375. (let* ((org-dir (org-find-library-dir "org"))
  18376. (babel-dir (or (org-find-library-dir "ob") org-dir))
  18377. (contrib-dir (or (org-find-library-dir "org-contribdir") org-dir))
  18378. (feature-re "^\\(org\\|ob\\)\\(-.*\\)?")
  18379. (remove-re (mapconcat 'identity
  18380. (mapcar (lambda (f) (concat "^" f "$"))
  18381. (list (if (featurep 'xemacs)
  18382. "org-colview"
  18383. "org-colview-xemacs")
  18384. "org" "org-loaddefs" "org-version"))
  18385. "\\|"))
  18386. (feats (delete-dups
  18387. (mapcar 'file-name-sans-extension
  18388. (mapcar 'file-name-nondirectory
  18389. (delq nil
  18390. (mapcar 'feature-file
  18391. features))))))
  18392. (lfeat (append
  18393. (sort
  18394. (setq feats
  18395. (delq nil (mapcar
  18396. (lambda (f)
  18397. (if (and (string-match feature-re f)
  18398. (not (string-match remove-re f)))
  18399. f nil))
  18400. feats)))
  18401. 'string-lessp)
  18402. (list "org-version" "org")))
  18403. (load-suffixes (if uncompiled (reverse load-suffixes) load-suffixes))
  18404. (load-misses ()))
  18405. (setq load-misses
  18406. (delq 't
  18407. (mapcar (lambda (f)
  18408. (or
  18409. (load (concat org-dir f) 'noerror nil nil 'mustsuffix)
  18410. (unless (string= org-dir babel-dir)
  18411. (load (concat babel-dir f) 'noerror nil nil 'mustsuffix))
  18412. (unless (string= org-dir contrib-dir)
  18413. (load (concat contrib-dir f) 'noerror nil nil 'mustsuffix))
  18414. (and (load (concat (org-find-library-dir f) f) 'noerror nil nil 'mustsuffix)
  18415. ;; fallback to load-path, report as a possible error
  18416. (message "Had to fall back onto load-path, something is not quite right...")
  18417. f)))
  18418. lfeat)))
  18419. (if (not load-misses)
  18420. (message "Successfully reloaded Org\n%s" (org-version nil 'full))
  18421. (message "Some error occured while reloading Org features\n%s\nPlease check *Messages*!\n%s"
  18422. load-misses (org-version nil 'full)))))
  18423. ;;;###autoload
  18424. (defun org-customize ()
  18425. "Call the customize function with org as argument."
  18426. (interactive)
  18427. (org-load-modules-maybe)
  18428. (org-require-autoloaded-modules)
  18429. (customize-browse 'org))
  18430. (defun org-create-customize-menu ()
  18431. "Create a full customization menu for Org-mode, insert it into the menu."
  18432. (interactive)
  18433. (org-load-modules-maybe)
  18434. (org-require-autoloaded-modules)
  18435. (if (fboundp 'customize-menu-create)
  18436. (progn
  18437. (easy-menu-change
  18438. '("Org") "Customize"
  18439. `(["Browse Org group" org-customize t]
  18440. "--"
  18441. ,(customize-menu-create 'org)
  18442. ["Set" Custom-set t]
  18443. ["Save" Custom-save t]
  18444. ["Reset to Current" Custom-reset-current t]
  18445. ["Reset to Saved" Custom-reset-saved t]
  18446. ["Reset to Standard Settings" Custom-reset-standard t]))
  18447. (message "\"Org\"-menu now contains full customization menu"))
  18448. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  18449. ;;;; Miscellaneous stuff
  18450. ;;; Generally useful functions
  18451. (defun org-get-at-bol (property)
  18452. "Get text property PROPERTY at beginning of line."
  18453. (get-text-property (point-at-bol) property))
  18454. (defun org-find-text-property-in-string (prop s)
  18455. "Return the first non-nil value of property PROP in string S."
  18456. (or (get-text-property 0 prop s)
  18457. (get-text-property (or (next-single-property-change 0 prop s) 0)
  18458. prop s)))
  18459. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  18460. "Display the given MESSAGE as a warning."
  18461. (if (fboundp 'display-warning)
  18462. (display-warning 'org message
  18463. (if (featurep 'xemacs) 'warning :warning))
  18464. (let ((buf (get-buffer-create "*Org warnings*")))
  18465. (with-current-buffer buf
  18466. (goto-char (point-max))
  18467. (insert "Warning (Org): " message)
  18468. (unless (bolp)
  18469. (newline)))
  18470. (display-buffer buf)
  18471. (sit-for 0))))
  18472. (defun org-eval (form)
  18473. "Eval FORM and return result."
  18474. (condition-case error
  18475. (eval form)
  18476. (error (format "%%![Error: %s]" error))))
  18477. (defun org-in-clocktable-p ()
  18478. "Check if the cursor is in a clocktable."
  18479. (let ((pos (point)) start)
  18480. (save-excursion
  18481. (end-of-line 1)
  18482. (and (re-search-backward "^[ \t]*#\\+BEGIN:[ \t]+clocktable" nil t)
  18483. (setq start (match-beginning 0))
  18484. (re-search-forward "^[ \t]*#\\+END:.*" nil t)
  18485. (>= (match-end 0) pos)
  18486. start))))
  18487. (defun org-in-commented-line ()
  18488. "Is point in a line starting with `#'?"
  18489. (equal (char-after (point-at-bol)) ?#))
  18490. (defun org-in-indented-comment-line ()
  18491. "Is point in a line starting with `#' after some white space?"
  18492. (save-excursion
  18493. (save-match-data
  18494. (goto-char (point-at-bol))
  18495. (looking-at "[ \t]*#"))))
  18496. (defun org-in-verbatim-emphasis ()
  18497. (save-match-data
  18498. (and (org-in-regexp org-emph-re 2) (member (match-string 3) '("=" "~")))))
  18499. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  18500. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  18501. (if (and marker (marker-buffer marker)
  18502. (buffer-live-p (marker-buffer marker)))
  18503. (progn
  18504. (org-pop-to-buffer-same-window (marker-buffer marker))
  18505. (if (or (> marker (point-max)) (< marker (point-min)))
  18506. (widen))
  18507. (goto-char marker)
  18508. (org-show-context 'org-goto))
  18509. (if bookmark
  18510. (bookmark-jump bookmark)
  18511. (error "Cannot find location"))))
  18512. (defun org-quote-csv-field (s)
  18513. "Quote field for inclusion in CSV material."
  18514. (if (string-match "[\",]" s)
  18515. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  18516. s))
  18517. (defun org-force-self-insert (N)
  18518. "Needed to enforce self-insert under remapping."
  18519. (interactive "p")
  18520. (self-insert-command N))
  18521. (defun org-string-width (s)
  18522. "Compute width of string, ignoring invisible characters.
  18523. This ignores character with invisibility property `org-link', and also
  18524. characters with property `org-cwidth', because these will become invisible
  18525. upon the next fontification round."
  18526. (let (b l)
  18527. (when (or (eq t buffer-invisibility-spec)
  18528. (assq 'org-link buffer-invisibility-spec))
  18529. (while (setq b (text-property-any 0 (length s)
  18530. 'invisible 'org-link s))
  18531. (setq s (concat (substring s 0 b)
  18532. (substring s (or (next-single-property-change
  18533. b 'invisible s) (length s)))))))
  18534. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  18535. (setq s (concat (substring s 0 b)
  18536. (substring s (or (next-single-property-change
  18537. b 'org-cwidth s) (length s))))))
  18538. (setq l (string-width s) b -1)
  18539. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  18540. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  18541. l))
  18542. (defun org-shorten-string (s maxlength)
  18543. "Shorten string S so tht it is no longer than MAXLENGTH characters.
  18544. If the string is shorter or has length MAXLENGTH, just return the
  18545. original string. If it is longer, the functions finds a space in the
  18546. string, breaks this string off at that locations and adds three dots
  18547. as ellipsis. Including the ellipsis, the string will not be longer
  18548. than MAXLENGTH. If finding a good breaking point in the string does
  18549. not work, the string is just chopped off in the middle of a word
  18550. if necessary."
  18551. (if (<= (length s) maxlength)
  18552. s
  18553. (let* ((n (max (- maxlength 4) 1))
  18554. (re (concat "\\`\\(.\\{1," (int-to-string n) "\\}[^ ]\\)\\([ ]\\|\\'\\)")))
  18555. (if (string-match re s)
  18556. (concat (match-string 1 s) "...")
  18557. (concat (substring s 0 (max (- maxlength 3) 0)) "...")))))
  18558. (defun org-get-indentation (&optional line)
  18559. "Get the indentation of the current line, interpreting tabs.
  18560. When LINE is given, assume it represents a line and compute its indentation."
  18561. (if line
  18562. (if (string-match "^ *" (org-remove-tabs line))
  18563. (match-end 0))
  18564. (save-excursion
  18565. (beginning-of-line 1)
  18566. (skip-chars-forward " \t")
  18567. (current-column))))
  18568. (defun org-get-string-indentation (s)
  18569. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  18570. (let ((n -1) (i 0) (w tab-width) c)
  18571. (catch 'exit
  18572. (while (< (setq n (1+ n)) (length s))
  18573. (setq c (aref s n))
  18574. (cond ((= c ?\ ) (setq i (1+ i)))
  18575. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  18576. (t (throw 'exit t)))))
  18577. i))
  18578. (defun org-remove-tabs (s &optional width)
  18579. "Replace tabulators in S with spaces.
  18580. Assumes that s is a single line, starting in column 0."
  18581. (setq width (or width tab-width))
  18582. (while (string-match "\t" s)
  18583. (setq s (replace-match
  18584. (make-string
  18585. (- (* width (/ (+ (match-beginning 0) width) width))
  18586. (match-beginning 0)) ?\ )
  18587. t t s)))
  18588. s)
  18589. (defun org-fix-indentation (line ind)
  18590. "Fix indentation in LINE.
  18591. IND is a cons cell with target and minimum indentation.
  18592. If the current indentation in LINE is smaller than the minimum,
  18593. leave it alone. If it is larger than ind, set it to the target."
  18594. (let* ((l (org-remove-tabs line))
  18595. (i (org-get-indentation l))
  18596. (i1 (car ind)) (i2 (cdr ind)))
  18597. (if (>= i i2) (setq l (substring line i2)))
  18598. (if (> i1 0)
  18599. (concat (make-string i1 ?\ ) l)
  18600. l)))
  18601. (defun org-remove-indentation (code &optional n)
  18602. "Remove the maximum common indentation from the lines in CODE.
  18603. N may optionally be the number of spaces to remove."
  18604. (with-temp-buffer
  18605. (insert code)
  18606. (org-do-remove-indentation n)
  18607. (buffer-string)))
  18608. (defun org-do-remove-indentation (&optional n)
  18609. "Remove the maximum common indentation from the buffer."
  18610. (untabify (point-min) (point-max))
  18611. (let ((min 10000) re)
  18612. (if n
  18613. (setq min n)
  18614. (goto-char (point-min))
  18615. (while (re-search-forward "^ *[^ \n]" nil t)
  18616. (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
  18617. (unless (or (= min 0) (= min 10000))
  18618. (setq re (format "^ \\{%d\\}" min))
  18619. (goto-char (point-min))
  18620. (while (re-search-forward re nil t)
  18621. (replace-match "")
  18622. (end-of-line 1))
  18623. min)))
  18624. (defun org-fill-template (template alist)
  18625. "Find each %key of ALIST in TEMPLATE and replace it."
  18626. (let ((case-fold-search nil)
  18627. entry key value)
  18628. (setq alist (sort (copy-sequence alist)
  18629. (lambda (a b) (< (length (car a)) (length (car b))))))
  18630. (while (setq entry (pop alist))
  18631. (setq template
  18632. (replace-regexp-in-string
  18633. (concat "%" (regexp-quote (car entry)))
  18634. (or (cdr entry) "") template t t)))
  18635. template))
  18636. (defun org-base-buffer (buffer)
  18637. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  18638. (if (not buffer)
  18639. buffer
  18640. (or (buffer-base-buffer buffer)
  18641. buffer)))
  18642. (defun org-trim (s)
  18643. "Remove whitespace at beginning and end of string."
  18644. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  18645. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  18646. s)
  18647. (defun org-wrap (string &optional width lines)
  18648. "Wrap string to either a number of lines, or a width in characters.
  18649. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  18650. that costs. If there is a word longer than WIDTH, the text is actually
  18651. wrapped to the length of that word.
  18652. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  18653. many lines, whatever width that takes.
  18654. The return value is a list of lines, without newlines at the end."
  18655. (let* ((words (org-split-string string "[ \t\n]+"))
  18656. (maxword (apply 'max (mapcar 'org-string-width words)))
  18657. w ll)
  18658. (cond (width
  18659. (org-do-wrap words (max maxword width)))
  18660. (lines
  18661. (setq w maxword)
  18662. (setq ll (org-do-wrap words maxword))
  18663. (if (<= (length ll) lines)
  18664. ll
  18665. (setq ll words)
  18666. (while (> (length ll) lines)
  18667. (setq w (1+ w))
  18668. (setq ll (org-do-wrap words w)))
  18669. ll))
  18670. (t (error "Cannot wrap this")))))
  18671. (defun org-do-wrap (words width)
  18672. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  18673. (let (lines line)
  18674. (while words
  18675. (setq line (pop words))
  18676. (while (and words (< (+ (length line) (length (car words))) width))
  18677. (setq line (concat line " " (pop words))))
  18678. (setq lines (push line lines)))
  18679. (nreverse lines)))
  18680. (defun org-split-string (string &optional separators)
  18681. "Splits STRING into substrings at SEPARATORS.
  18682. No empty strings are returned if there are matches at the beginning
  18683. and end of string."
  18684. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  18685. (start 0)
  18686. notfirst
  18687. (list nil))
  18688. (while (and (string-match rexp string
  18689. (if (and notfirst
  18690. (= start (match-beginning 0))
  18691. (< start (length string)))
  18692. (1+ start) start))
  18693. (< (match-beginning 0) (length string)))
  18694. (setq notfirst t)
  18695. (or (eq (match-beginning 0) 0)
  18696. (and (eq (match-beginning 0) (match-end 0))
  18697. (eq (match-beginning 0) start))
  18698. (setq list
  18699. (cons (substring string start (match-beginning 0))
  18700. list)))
  18701. (setq start (match-end 0)))
  18702. (or (eq start (length string))
  18703. (setq list
  18704. (cons (substring string start)
  18705. list)))
  18706. (nreverse list)))
  18707. (defun org-quote-vert (s)
  18708. "Replace \"|\" with \"\\vert\"."
  18709. (while (string-match "|" s)
  18710. (setq s (replace-match "\\vert" t t s)))
  18711. s)
  18712. (defun org-uuidgen-p (s)
  18713. "Is S an ID created by UUIDGEN?"
  18714. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  18715. (defun org-in-src-block-p nil
  18716. "Whether point is in a code source block."
  18717. (let (ov)
  18718. (when (setq ov (overlays-at (point)))
  18719. (memq 'org-block-background
  18720. (overlay-properties
  18721. (car ov))))))
  18722. (defun org-context ()
  18723. "Return a list of contexts of the current cursor position.
  18724. If several contexts apply, all are returned.
  18725. Each context entry is a list with a symbol naming the context, and
  18726. two positions indicating start and end of the context. Possible
  18727. contexts are:
  18728. :headline anywhere in a headline
  18729. :headline-stars on the leading stars in a headline
  18730. :todo-keyword on a TODO keyword (including DONE) in a headline
  18731. :tags on the TAGS in a headline
  18732. :priority on the priority cookie in a headline
  18733. :item on the first line of a plain list item
  18734. :item-bullet on the bullet/number of a plain list item
  18735. :checkbox on the checkbox in a plain list item
  18736. :table in an org-mode table
  18737. :table-special on a special filed in a table
  18738. :table-table in a table.el table
  18739. :clocktable in a clocktable
  18740. :src-block in a source block
  18741. :link on a hyperlink
  18742. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE, COMMENT, QUOTE.
  18743. :target on a <<target>>
  18744. :radio-target on a <<<radio-target>>>
  18745. :latex-fragment on a LaTeX fragment
  18746. :latex-preview on a LaTeX fragment with overlaid preview image
  18747. This function expects the position to be visible because it uses font-lock
  18748. faces as a help to recognize the following contexts: :table-special, :link,
  18749. and :keyword."
  18750. (let* ((f (get-text-property (point) 'face))
  18751. (faces (if (listp f) f (list f)))
  18752. (case-fold-search t)
  18753. (p (point)) clist o)
  18754. ;; First the large context
  18755. (cond
  18756. ((org-at-heading-p t)
  18757. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  18758. (when (progn
  18759. (beginning-of-line 1)
  18760. (looking-at org-todo-line-tags-regexp))
  18761. (push (org-point-in-group p 1 :headline-stars) clist)
  18762. (push (org-point-in-group p 2 :todo-keyword) clist)
  18763. (push (org-point-in-group p 4 :tags) clist))
  18764. (goto-char p)
  18765. (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
  18766. (if (looking-at "\\[#[A-Z0-9]\\]")
  18767. (push (org-point-in-group p 0 :priority) clist)))
  18768. ((org-at-item-p)
  18769. (push (org-point-in-group p 2 :item-bullet) clist)
  18770. (push (list :item (point-at-bol)
  18771. (save-excursion (org-end-of-item) (point)))
  18772. clist)
  18773. (and (org-at-item-checkbox-p)
  18774. (push (org-point-in-group p 0 :checkbox) clist)))
  18775. ((org-at-table-p)
  18776. (push (list :table (org-table-begin) (org-table-end)) clist)
  18777. (if (memq 'org-formula faces)
  18778. (push (list :table-special
  18779. (previous-single-property-change p 'face)
  18780. (next-single-property-change p 'face)) clist)))
  18781. ((org-at-table-p 'any)
  18782. (push (list :table-table) clist)))
  18783. (goto-char p)
  18784. (let ((case-fold-search t))
  18785. ;; New the "medium" contexts: clocktables, source blocks
  18786. (cond ((org-in-clocktable-p)
  18787. (push (list :clocktable
  18788. (and (or (looking-at "#\\+BEGIN: clocktable")
  18789. (search-backward "#+BEGIN: clocktable" nil t))
  18790. (match-beginning 0))
  18791. (and (re-search-forward "#\\+END:?" nil t)
  18792. (match-end 0))) clist))
  18793. ((org-in-src-block-p)
  18794. (push (list :src-block
  18795. (and (or (looking-at "#\\+BEGIN_SRC")
  18796. (search-backward "#+BEGIN_SRC" nil t))
  18797. (match-beginning 0))
  18798. (and (search-forward "#+END_SRC" nil t)
  18799. (match-beginning 0))) clist))))
  18800. (goto-char p)
  18801. ;; Now the small context
  18802. (cond
  18803. ((org-at-timestamp-p)
  18804. (push (org-point-in-group p 0 :timestamp) clist))
  18805. ((memq 'org-link faces)
  18806. (push (list :link
  18807. (previous-single-property-change p 'face)
  18808. (next-single-property-change p 'face)) clist))
  18809. ((memq 'org-special-keyword faces)
  18810. (push (list :keyword
  18811. (previous-single-property-change p 'face)
  18812. (next-single-property-change p 'face)) clist))
  18813. ((org-at-target-p)
  18814. (push (org-point-in-group p 0 :target) clist)
  18815. (goto-char (1- (match-beginning 0)))
  18816. (if (looking-at org-radio-target-regexp)
  18817. (push (org-point-in-group p 0 :radio-target) clist))
  18818. (goto-char p))
  18819. ((setq o (car (delq nil
  18820. (mapcar
  18821. (lambda (x)
  18822. (if (memq x org-latex-fragment-image-overlays) x))
  18823. (overlays-at (point))))))
  18824. (push (list :latex-fragment
  18825. (overlay-start o) (overlay-end o)) clist)
  18826. (push (list :latex-preview
  18827. (overlay-start o) (overlay-end o)) clist))
  18828. ((org-inside-LaTeX-fragment-p)
  18829. ;; FIXME: positions wrong.
  18830. (push (list :latex-fragment (point) (point)) clist)))
  18831. (setq clist (nreverse (delq nil clist)))
  18832. clist))
  18833. ;; FIXME: Compare with at-regexp-p Do we need both?
  18834. (defun org-in-regexp (re &optional nlines visually)
  18835. "Check if point is inside a match of regexp.
  18836. Normally only the current line is checked, but you can include NLINES extra
  18837. lines both before and after point into the search.
  18838. If VISUALLY is set, require that the cursor is not after the match but
  18839. really on, so that the block visually is on the match."
  18840. (catch 'exit
  18841. (let ((pos (point))
  18842. (eol (point-at-eol (+ 1 (or nlines 0))))
  18843. (inc (if visually 1 0)))
  18844. (save-excursion
  18845. (beginning-of-line (- 1 (or nlines 0)))
  18846. (while (re-search-forward re eol t)
  18847. (if (and (<= (match-beginning 0) pos)
  18848. (>= (+ inc (match-end 0)) pos))
  18849. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  18850. (defun org-at-regexp-p (regexp)
  18851. "Is point inside a match of REGEXP in the current line?"
  18852. (catch 'exit
  18853. (save-excursion
  18854. (let ((pos (point)) (end (point-at-eol)))
  18855. (beginning-of-line 1)
  18856. (while (re-search-forward regexp end t)
  18857. (if (and (<= (match-beginning 0) pos)
  18858. (>= (match-end 0) pos))
  18859. (throw 'exit t)))
  18860. nil))))
  18861. (defun org-between-regexps-p (start-re end-re &optional lim-up lim-down)
  18862. "Non-nil when point is between matches of START-RE and END-RE.
  18863. Also return a non-nil value when point is on one of the matches.
  18864. Optional arguments LIM-UP and LIM-DOWN bound the search; they are
  18865. buffer positions. Default values are the positions of headlines
  18866. surrounding the point.
  18867. The functions returns a cons cell whose car (resp. cdr) is the
  18868. position before START-RE (resp. after END-RE)."
  18869. (save-match-data
  18870. (let ((pos (point))
  18871. (limit-up (or lim-up (save-excursion (outline-previous-heading))))
  18872. (limit-down (or lim-down (save-excursion (outline-next-heading))))
  18873. beg end)
  18874. (save-excursion
  18875. ;; Point is on a block when on START-RE or if START-RE can be
  18876. ;; found before it...
  18877. (and (or (org-at-regexp-p start-re)
  18878. (re-search-backward start-re limit-up t))
  18879. (setq beg (match-beginning 0))
  18880. ;; ... and END-RE after it...
  18881. (goto-char (match-end 0))
  18882. (re-search-forward end-re limit-down t)
  18883. (> (setq end (match-end 0)) pos)
  18884. ;; ... without another START-RE in-between.
  18885. (goto-char (match-beginning 0))
  18886. (not (re-search-backward start-re (1+ beg) t))
  18887. ;; Return value.
  18888. (cons beg end))))))
  18889. (defun org-in-block-p (names)
  18890. "Non-nil when point belongs to a block whose name belongs to NAMES.
  18891. NAMES is a list of strings containing names of blocks.
  18892. Return first block name matched, or nil. Beware that in case of
  18893. nested blocks, the returned name may not belong to the closest
  18894. block from point."
  18895. (save-match-data
  18896. (catch 'exit
  18897. (let ((case-fold-search t)
  18898. (lim-up (save-excursion (outline-previous-heading)))
  18899. (lim-down (save-excursion (outline-next-heading))))
  18900. (mapc (lambda (name)
  18901. (let ((n (regexp-quote name)))
  18902. (when (org-between-regexps-p
  18903. (concat "^[ \t]*#\\+begin_" n)
  18904. (concat "^[ \t]*#\\+end_" n)
  18905. lim-up lim-down)
  18906. (throw 'exit n))))
  18907. names))
  18908. nil)))
  18909. (defun org-occur-in-agenda-files (regexp &optional nlines)
  18910. "Call `multi-occur' with buffers for all agenda files."
  18911. (interactive "sOrg-files matching: \np")
  18912. (let* ((files (org-agenda-files))
  18913. (tnames (mapcar 'file-truename files))
  18914. (extra org-agenda-text-search-extra-files)
  18915. f)
  18916. (when (eq (car extra) 'agenda-archives)
  18917. (setq extra (cdr extra))
  18918. (setq files (org-add-archive-files files)))
  18919. (while (setq f (pop extra))
  18920. (unless (member (file-truename f) tnames)
  18921. (add-to-list 'files f 'append)
  18922. (add-to-list 'tnames (file-truename f) 'append)))
  18923. (multi-occur
  18924. (mapcar (lambda (x)
  18925. (with-current-buffer
  18926. (or (get-file-buffer x) (find-file-noselect x))
  18927. (widen)
  18928. (current-buffer)))
  18929. files)
  18930. regexp)))
  18931. (if (boundp 'occur-mode-find-occurrence-hook)
  18932. ;; Emacs 23
  18933. (add-hook 'occur-mode-find-occurrence-hook
  18934. (lambda ()
  18935. (when (derived-mode-p 'org-mode)
  18936. (org-reveal))))
  18937. ;; Emacs 22
  18938. (defadvice occur-mode-goto-occurrence
  18939. (after org-occur-reveal activate)
  18940. (and (derived-mode-p 'org-mode) (org-reveal)))
  18941. (defadvice occur-mode-goto-occurrence-other-window
  18942. (after org-occur-reveal activate)
  18943. (and (derived-mode-p 'org-mode) (org-reveal)))
  18944. (defadvice occur-mode-display-occurrence
  18945. (after org-occur-reveal activate)
  18946. (when (derived-mode-p 'org-mode)
  18947. (let ((pos (occur-mode-find-occurrence)))
  18948. (with-current-buffer (marker-buffer pos)
  18949. (save-excursion
  18950. (goto-char pos)
  18951. (org-reveal)))))))
  18952. (defun org-occur-link-in-agenda-files ()
  18953. "Create a link and search for it in the agendas.
  18954. The link is not stored in `org-stored-links', it is just created
  18955. for the search purpose."
  18956. (interactive)
  18957. (let ((link (condition-case nil
  18958. (org-store-link nil)
  18959. (error "Unable to create a link to here"))))
  18960. (org-occur-in-agenda-files (regexp-quote link))))
  18961. (defun org-uniquify (list)
  18962. "Remove duplicate elements from LIST."
  18963. (let (res)
  18964. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  18965. res))
  18966. (defun org-delete-all (elts list)
  18967. "Remove all elements in ELTS from LIST."
  18968. (while elts
  18969. (setq list (delete (pop elts) list)))
  18970. list)
  18971. (defun org-count (cl-item cl-seq)
  18972. "Count the number of occurrences of ITEM in SEQ.
  18973. Taken from `count' in cl-seq.el with all keyword arguments removed."
  18974. (let ((cl-end (length cl-seq)) (cl-start 0) (cl-count 0) cl-x)
  18975. (when (consp cl-seq) (setq cl-seq (nthcdr cl-start cl-seq)))
  18976. (while (< cl-start cl-end)
  18977. (setq cl-x (if (consp cl-seq) (pop cl-seq) (aref cl-seq cl-start)))
  18978. (if (equal cl-item cl-x) (setq cl-count (1+ cl-count)))
  18979. (setq cl-start (1+ cl-start)))
  18980. cl-count))
  18981. (defun org-remove-if (predicate seq)
  18982. "Remove everything from SEQ that fulfills PREDICATE."
  18983. (let (res e)
  18984. (while seq
  18985. (setq e (pop seq))
  18986. (if (not (funcall predicate e)) (push e res)))
  18987. (nreverse res)))
  18988. (defun org-remove-if-not (predicate seq)
  18989. "Remove everything from SEQ that does not fulfill PREDICATE."
  18990. (let (res e)
  18991. (while seq
  18992. (setq e (pop seq))
  18993. (if (funcall predicate e) (push e res)))
  18994. (nreverse res)))
  18995. (defun org-reduce (cl-func cl-seq &rest cl-keys)
  18996. "Reduce two-argument FUNCTION across SEQ.
  18997. Taken from `reduce' in cl-seq.el with all keyword arguments but
  18998. \":initial-value\" removed."
  18999. (let ((cl-accum (cond ((memq :initial-value cl-keys)
  19000. (cadr (memq :initial-value cl-keys)))
  19001. (cl-seq (pop cl-seq))
  19002. (t (funcall cl-func)))))
  19003. (while cl-seq
  19004. (setq cl-accum (funcall cl-func cl-accum (pop cl-seq))))
  19005. cl-accum))
  19006. (defun org-back-over-empty-lines ()
  19007. "Move backwards over whitespace, to the beginning of the first empty line.
  19008. Returns the number of empty lines passed."
  19009. (let ((pos (point)))
  19010. (if (cdr (assoc 'heading org-blank-before-new-entry))
  19011. (skip-chars-backward " \t\n\r")
  19012. (unless (eobp)
  19013. (forward-line -1)))
  19014. (beginning-of-line 2)
  19015. (goto-char (min (point) pos))
  19016. (count-lines (point) pos)))
  19017. (defun org-skip-whitespace ()
  19018. (skip-chars-forward " \t\n\r"))
  19019. (defun org-point-in-group (point group &optional context)
  19020. "Check if POINT is in match-group GROUP.
  19021. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  19022. match. If the match group does not exist or point is not inside it,
  19023. return nil."
  19024. (and (match-beginning group)
  19025. (>= point (match-beginning group))
  19026. (<= point (match-end group))
  19027. (if context
  19028. (list context (match-beginning group) (match-end group))
  19029. t)))
  19030. (defun org-switch-to-buffer-other-window (&rest args)
  19031. "Switch to buffer in a second window on the current frame.
  19032. In particular, do not allow pop-up frames.
  19033. Returns the newly created buffer."
  19034. (let (pop-up-frames special-display-buffer-names special-display-regexps
  19035. special-display-function)
  19036. (apply 'switch-to-buffer-other-window args)))
  19037. (defun org-combine-plists (&rest plists)
  19038. "Create a single property list from all plists in PLISTS.
  19039. The process starts by copying the first list, and then setting properties
  19040. from the other lists. Settings in the last list are the most significant
  19041. ones and overrule settings in the other lists."
  19042. (let ((rtn (copy-sequence (pop plists)))
  19043. p v ls)
  19044. (while plists
  19045. (setq ls (pop plists))
  19046. (while ls
  19047. (setq p (pop ls) v (pop ls))
  19048. (setq rtn (plist-put rtn p v))))
  19049. rtn))
  19050. (defun org-replace-escapes (string table)
  19051. "Replace %-escapes in STRING with values in TABLE.
  19052. TABLE is an association list with keys like \"%a\" and string values.
  19053. The sequences in STRING may contain normal field width and padding information,
  19054. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  19055. so values can contain further %-escapes if they are define later in TABLE."
  19056. (let ((tbl (copy-alist table))
  19057. (case-fold-search nil)
  19058. (pchg 0)
  19059. e re rpl)
  19060. (while (setq e (pop tbl))
  19061. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  19062. (when (and (cdr e) (string-match re (cdr e)))
  19063. (let ((sref (substring (cdr e) (match-beginning 0) (match-end 0)))
  19064. (safe "SREF"))
  19065. (add-text-properties 0 3 (list 'sref sref) safe)
  19066. (setcdr e (replace-match safe t t (cdr e)))))
  19067. (while (string-match re string)
  19068. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  19069. (cdr e)))
  19070. (setq string (replace-match rpl t t string))))
  19071. (while (setq pchg (next-property-change pchg string))
  19072. (let ((sref (get-text-property pchg 'sref string)))
  19073. (when (and sref (string-match "SREF" string pchg))
  19074. (setq string (replace-match sref t t string)))))
  19075. string))
  19076. (defun org-sublist (list start end)
  19077. "Return a section of LIST, from START to END.
  19078. Counting starts at 1."
  19079. (let (rtn (c start))
  19080. (setq list (nthcdr (1- start) list))
  19081. (while (and list (<= c end))
  19082. (push (pop list) rtn)
  19083. (setq c (1+ c)))
  19084. (nreverse rtn)))
  19085. (defun org-find-base-buffer-visiting (file)
  19086. "Like `find-buffer-visiting' but always return the base buffer and
  19087. not an indirect buffer."
  19088. (let ((buf (or (get-file-buffer file)
  19089. (find-buffer-visiting file))))
  19090. (if buf
  19091. (or (buffer-base-buffer buf) buf)
  19092. nil)))
  19093. (defun org-image-file-name-regexp (&optional extensions)
  19094. "Return regexp matching the file names of images.
  19095. If EXTENSIONS is given, only match these."
  19096. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  19097. (image-file-name-regexp)
  19098. (let ((image-file-name-extensions
  19099. (or extensions
  19100. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  19101. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  19102. (concat "\\."
  19103. (regexp-opt (nconc (mapcar 'upcase
  19104. image-file-name-extensions)
  19105. image-file-name-extensions)
  19106. t)
  19107. "\\'"))))
  19108. (defun org-file-image-p (file &optional extensions)
  19109. "Return non-nil if FILE is an image."
  19110. (save-match-data
  19111. (string-match (org-image-file-name-regexp extensions) file)))
  19112. (defun org-get-cursor-date ()
  19113. "Return the date at cursor in as a time.
  19114. This works in the calendar and in the agenda, anywhere else it just
  19115. returns the current time."
  19116. (let (date day defd)
  19117. (cond
  19118. ((eq major-mode 'calendar-mode)
  19119. (setq date (calendar-cursor-to-date)
  19120. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  19121. ((eq major-mode 'org-agenda-mode)
  19122. (setq day (get-text-property (point) 'day))
  19123. (if day
  19124. (setq date (calendar-gregorian-from-absolute day)
  19125. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
  19126. (nth 2 date))))))
  19127. (or defd (current-time))))
  19128. (defun org-mark-subtree (&optional up)
  19129. "Mark the current subtree.
  19130. This puts point at the start of the current subtree, and mark at
  19131. the end. If a numeric prefix UP is given, move up into the
  19132. hierarchy of headlines by UP levels before marking the subtree."
  19133. (interactive "P")
  19134. (org-with-limited-levels
  19135. (cond ((org-at-heading-p) (beginning-of-line))
  19136. ((org-before-first-heading-p) (error "Not in a subtree"))
  19137. (t (outline-previous-visible-heading 1))))
  19138. (when up (while (and (> up 0) (org-up-heading-safe)) (decf up)))
  19139. (if (org-called-interactively-p 'any)
  19140. (call-interactively 'org-mark-element)
  19141. (org-mark-element)))
  19142. ;;; Macros
  19143. ;; Macros are expanded with `org-macro-replace-all', which relies
  19144. ;; internally on `org-macro-expand'.
  19145. ;; Default templates for expansion are stored in the buffer-local
  19146. ;; variable `org-macro-templates'. This variable is updated by
  19147. ;; `org-macro-initialize-templates'.
  19148. ;; Along with macros defined through #+MACRO: keyword, default
  19149. ;; templates include the following hard-coded macros:
  19150. ;; {{{time(format-string)}}}, {{{property(node-property)}}},
  19151. ;; {{{input-file}}} and {{{modification-time(format-string)}}}.
  19152. ;; During export, {{{author}}}, {{{date}}}, {{{email}}} and
  19153. ;; {{{title}}} will also be provided.
  19154. (defvar org-macro-templates nil
  19155. "Alist containing all macro templates in current buffer.
  19156. Associations are in the shape of (NAME . TEMPLATE) where NAME
  19157. stands for macro's name and template for its replacement value,
  19158. both as strings. This is an internal variable. Do not set it
  19159. directly, use instead:
  19160. #+MACRO: name template")
  19161. (make-variable-buffer-local 'org-macro-templates)
  19162. (defun org-macro-expand (macro templates)
  19163. "Return expanded MACRO, as a string.
  19164. MACRO is an object, obtained, for example, with
  19165. `org-element-context'. TEMPLATES is an alist of templates used
  19166. for expansion. See `org-macro-templates' for a buffer-local
  19167. default value. Return nil if no template was found."
  19168. (let ((template
  19169. ;; Macro names are case-insensitive.
  19170. (cdr (assoc-string (org-element-property :key macro) templates t))))
  19171. (when template
  19172. (let ((value (replace-regexp-in-string
  19173. "\\$[0-9]+"
  19174. (lambda (arg)
  19175. (or (nth (1- (string-to-number (substring arg 1)))
  19176. (org-element-property :args macro))
  19177. ;; No argument provided: remove
  19178. ;; place-holder.
  19179. ""))
  19180. template)))
  19181. ;; VALUE starts with "(eval": it is a s-exp, `eval' it.
  19182. (when (string-match "\\`(eval\\>" value)
  19183. (setq value (eval (read value))))
  19184. ;; Return string.
  19185. (format "%s" (or value ""))))))
  19186. (defun org-macro-replace-all (templates)
  19187. "Replace all macros in current buffer by their expansion.
  19188. TEMPLATES is an alist of templates used for expansion. See
  19189. `org-macro-templates' for a buffer-local default value."
  19190. (save-excursion
  19191. (goto-char (point-min))
  19192. (while (re-search-forward "{{{[-A-Za-z0-9_]" nil t)
  19193. (let ((object (org-element-context)))
  19194. (when (eq (org-element-type object) 'macro)
  19195. (let ((value (org-macro-expand object templates)))
  19196. (when value
  19197. (delete-region
  19198. (org-element-property :begin object)
  19199. ;; Preserve white spaces after the macro.
  19200. (progn (goto-char (org-element-property :end object))
  19201. (skip-chars-backward " \t")
  19202. (point)))
  19203. ;; Leave point before replacement in case of recursive
  19204. ;; expansions.
  19205. (save-excursion (insert value)))))))))
  19206. (defun org-macro-initialize-templates ()
  19207. "Collect macro templates defined in current buffer.
  19208. Templates are stored in buffer-local variable
  19209. `org-macro-templates'. In addition to buffer-defined macros, the
  19210. function installs the following ones: \"property\", \"date\",
  19211. \"time\". and, if appropriate, \"input-file\" and
  19212. \"modification-time\"."
  19213. (let ((case-fold-search t)
  19214. (set-template
  19215. (lambda (cell)
  19216. ;; Add CELL to `org-macro-templates' if there's no
  19217. ;; association matching its name already. Otherwise,
  19218. ;; replace old association with the new one in that
  19219. ;; variable.
  19220. (let ((old-template (assoc (car cell) org-macro-templates)))
  19221. (if old-template (setcdr old-template (cdr cell))
  19222. (push cell org-macro-templates))))))
  19223. ;; Install buffer-local macros.
  19224. (org-with-wide-buffer
  19225. (goto-char (point-min))
  19226. (while (re-search-forward "^[ \t]*#\\+MACRO:" nil t)
  19227. (let ((element (org-element-at-point)))
  19228. (when (eq (org-element-type element) 'keyword)
  19229. (let ((value (org-element-property :value element)))
  19230. (when (string-match "^\\(.*?\\)\\(?:\\s-+\\(.*\\)\\)?\\s-*$" value)
  19231. (funcall set-template
  19232. (cons (match-string 1 value)
  19233. (or (match-string 2 value) "")))))))))
  19234. ;; Install hard-coded macros.
  19235. (mapc (lambda (cell) (funcall set-template cell))
  19236. (list
  19237. (cons "property" "(eval (org-entry-get nil \"$1\" 'selective))")
  19238. (cons "time" "(eval (format-time-string \"$1\"))")))
  19239. (let ((visited-file (buffer-file-name (buffer-base-buffer))))
  19240. (when (and visited-file (file-exists-p visited-file))
  19241. (mapc (lambda (cell) (funcall set-template cell))
  19242. (list
  19243. (cons "input-file" (file-name-nondirectory visited-file))
  19244. (cons "modification-time"
  19245. (format "(eval (format-time-string \"$1\" '%s))"
  19246. (prin1-to-string
  19247. (nth 5 (file-attributes visited-file)))))))))))
  19248. ;;; Indentation
  19249. (defun org-indent-line ()
  19250. "Indent line depending on context."
  19251. (interactive)
  19252. (let* ((pos (point))
  19253. (itemp (org-at-item-p))
  19254. (case-fold-search t)
  19255. (org-drawer-regexp (or org-drawer-regexp "\000"))
  19256. (inline-task-p (and (featurep 'org-inlinetask)
  19257. (org-inlinetask-in-task-p)))
  19258. (inline-re (and inline-task-p
  19259. (org-inlinetask-outline-regexp)))
  19260. column)
  19261. (if (and orgstruct-is-++ (eq pos (point)))
  19262. (let ((indent-line-function (cadadr (assoc 'indent-line-function org-fb-vars))))
  19263. (indent-according-to-mode))
  19264. (beginning-of-line 1)
  19265. (cond
  19266. ;; Headings
  19267. ((looking-at org-outline-regexp) (setq column 0))
  19268. ;; Included files
  19269. ((looking-at "#\\+include:") (setq column 0))
  19270. ;; Footnote definition
  19271. ((looking-at org-footnote-definition-re) (setq column 0))
  19272. ;; Literal examples
  19273. ((looking-at "[ \t]*:\\( \\|$\\)")
  19274. (setq column (org-get-indentation))) ; do nothing
  19275. ;; Lists
  19276. ((ignore-errors (goto-char (org-in-item-p)))
  19277. (setq column (if itemp
  19278. (org-get-indentation)
  19279. (org-list-item-body-column (point))))
  19280. (goto-char pos))
  19281. ;; Drawers
  19282. ((and (looking-at "[ \t]*:END:")
  19283. (save-excursion (re-search-backward org-drawer-regexp nil t)))
  19284. (save-excursion
  19285. (goto-char (1- (match-beginning 1)))
  19286. (setq column (current-column))))
  19287. ;; Special blocks
  19288. ((and (looking-at "[ \t]*#\\+end_\\([a-z]+\\)")
  19289. (save-excursion
  19290. (re-search-backward
  19291. (concat "^[ \t]*#\\+begin_" (downcase (match-string 1))) nil t)))
  19292. (setq column (org-get-indentation (match-string 0))))
  19293. ((and (not (looking-at "[ \t]*#\\+begin_"))
  19294. (org-between-regexps-p "^[ \t]*#\\+begin_" "[ \t]*#\\+end_"))
  19295. (save-excursion
  19296. (re-search-backward "^[ \t]*#\\+begin_\\([a-z]+\\)" nil t))
  19297. (setq column
  19298. (cond ((equal (downcase (match-string 1)) "src")
  19299. ;; src blocks: let `org-edit-src-exit' handle them
  19300. (org-get-indentation))
  19301. ((equal (downcase (match-string 1)) "example")
  19302. (max (org-get-indentation)
  19303. (org-get-indentation (match-string 0))))
  19304. (t
  19305. (org-get-indentation (match-string 0))))))
  19306. ;; This line has nothing special, look at the previous relevant
  19307. ;; line to compute indentation
  19308. (t
  19309. (beginning-of-line 0)
  19310. (while (and (not (bobp))
  19311. (not (looking-at org-drawer-regexp))
  19312. ;; When point started in an inline task, do not move
  19313. ;; above task starting line.
  19314. (not (and inline-task-p (looking-at inline-re)))
  19315. ;; Skip drawers, blocks, empty lines, verbatim,
  19316. ;; comments, tables, footnotes definitions, lists,
  19317. ;; inline tasks.
  19318. (or (and (looking-at "[ \t]*:END:")
  19319. (re-search-backward org-drawer-regexp nil t))
  19320. (and (looking-at "[ \t]*#\\+end_")
  19321. (re-search-backward "[ \t]*#\\+begin_"nil t))
  19322. (looking-at "[ \t]*[\n:#|]")
  19323. (looking-at org-footnote-definition-re)
  19324. (and (ignore-errors (goto-char (org-in-item-p)))
  19325. (goto-char
  19326. (org-list-get-top-point (org-list-struct))))
  19327. (and (not inline-task-p)
  19328. (featurep 'org-inlinetask)
  19329. (org-inlinetask-in-task-p)
  19330. (or (org-inlinetask-goto-beginning) t))))
  19331. (beginning-of-line 0))
  19332. (cond
  19333. ;; There was an heading above.
  19334. ((looking-at "\\*+[ \t]+")
  19335. (if (not org-adapt-indentation)
  19336. (setq column 0)
  19337. (goto-char (match-end 0))
  19338. (setq column (current-column))))
  19339. ;; A drawer had started and is unfinished
  19340. ((looking-at org-drawer-regexp)
  19341. (goto-char (1- (match-beginning 1)))
  19342. (setq column (current-column)))
  19343. ;; Else, nothing noticeable found: get indentation and go on.
  19344. (t (setq column (org-get-indentation))))))
  19345. ;; Now apply indentation and move cursor accordingly
  19346. (goto-char pos)
  19347. (if (<= (current-column) (current-indentation))
  19348. (org-indent-line-to column)
  19349. (save-excursion (org-indent-line-to column)))
  19350. ;; Special polishing for properties, see `org-property-format'
  19351. (setq column (current-column))
  19352. (beginning-of-line 1)
  19353. (if (looking-at
  19354. "\\([ \t]*\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  19355. (replace-match (concat (match-string 1)
  19356. (format org-property-format
  19357. (match-string 2) (match-string 3)))
  19358. t t))
  19359. (org-move-to-column column))))
  19360. (defun org-indent-drawer ()
  19361. "Indent the drawer at point."
  19362. (interactive)
  19363. (let ((p (point))
  19364. (e (and (save-excursion (re-search-forward ":END:" nil t))
  19365. (match-end 0)))
  19366. (folded
  19367. (save-excursion
  19368. (end-of-line)
  19369. (when (overlays-at (point))
  19370. (member 'invisible (overlay-properties
  19371. (car (overlays-at (point)))))))))
  19372. (when folded (org-cycle))
  19373. (indent-for-tab-command)
  19374. (while (and (move-beginning-of-line 2) (< (point) e))
  19375. (indent-for-tab-command))
  19376. (goto-char p)
  19377. (when folded (org-cycle)))
  19378. (message "Drawer at point indented"))
  19379. (defun org-indent-block ()
  19380. "Indent the block at point."
  19381. (interactive)
  19382. (let ((p (point))
  19383. (case-fold-search t)
  19384. (e (and (save-excursion (re-search-forward "#\\+end_?\\(?:[a-z]+\\)?" nil t))
  19385. (match-end 0)))
  19386. (folded
  19387. (save-excursion
  19388. (end-of-line)
  19389. (when (overlays-at (point))
  19390. (member 'invisible (overlay-properties
  19391. (car (overlays-at (point)))))))))
  19392. (when folded (org-cycle))
  19393. (indent-for-tab-command)
  19394. (while (and (move-beginning-of-line 2) (< (point) e))
  19395. (indent-for-tab-command))
  19396. (goto-char p)
  19397. (when folded (org-cycle)))
  19398. (message "Block at point indented"))
  19399. (defun org-indent-region (start end)
  19400. "Indent region."
  19401. (interactive "r")
  19402. (save-excursion
  19403. (let ((line-end (org-current-line end)))
  19404. (goto-char start)
  19405. (while (< (org-current-line) line-end)
  19406. (cond ((org-in-src-block-p) (org-src-native-tab-command-maybe))
  19407. (t (call-interactively 'org-indent-line)))
  19408. (move-beginning-of-line 2)))))
  19409. ;;; Filling
  19410. ;; We use our own fill-paragraph and auto-fill functions.
  19411. ;; `org-fill-paragraph' relies on adaptive filling and context
  19412. ;; checking. Appropriate `fill-prefix' is computed with
  19413. ;; `org-adaptive-fill-function'.
  19414. ;; `org-auto-fill-function' takes care of auto-filling. It calls
  19415. ;; `do-auto-fill' only on valid areas with `fill-prefix' shadowed with
  19416. ;; `org-adaptive-fill-function' value. Internally,
  19417. ;; `org-comment-line-break-function' breaks the line.
  19418. ;; `org-setup-filling' installs filling and auto-filling related
  19419. ;; variables during `org-mode' initialization.
  19420. (defun org-setup-filling ()
  19421. (interactive)
  19422. ;; Prevent auto-fill from inserting unwanted new items.
  19423. (when (boundp 'fill-nobreak-predicate)
  19424. (org-set-local
  19425. 'fill-nobreak-predicate
  19426. (org-uniquify
  19427. (append fill-nobreak-predicate
  19428. '(org-fill-paragraph-separate-nobreak-p
  19429. org-fill-line-break-nobreak-p)))))
  19430. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  19431. (org-set-local 'adaptive-fill-function 'org-adaptive-fill-function)
  19432. (org-set-local 'normal-auto-fill-function 'org-auto-fill-function)
  19433. (org-set-local 'comment-line-break-function 'org-comment-line-break-function))
  19434. (defvar org-element-paragraph-separate) ; org-element.el
  19435. (defun org-fill-paragraph-separate-nobreak-p ()
  19436. "Non-nil when a line break at point would insert a new item."
  19437. (looking-at (substring org-element-paragraph-separate 1)))
  19438. (defun org-fill-line-break-nobreak-p ()
  19439. "Non-nil when a line break at point would create an Org line break."
  19440. (save-excursion
  19441. (skip-chars-backward "[ \t]")
  19442. (skip-chars-backward "\\\\")
  19443. (looking-at "\\\\\\\\\\($\\|[^\\\\]\\)")))
  19444. (declare-function message-in-body-p "message" ())
  19445. (defvar org-element--affiliated-re) ; From org-element.el
  19446. (defun org-adaptive-fill-function ()
  19447. "Compute a fill prefix for the current line.
  19448. Return fill prefix, as a string, or nil if current line isn't
  19449. meant to be filled."
  19450. (org-with-wide-buffer
  19451. (unless (and (derived-mode-p 'message-mode) (not (message-in-body-p)))
  19452. ;; FIXME: This is really the job of orgstruct++-mode
  19453. (let* ((p (line-beginning-position))
  19454. (element (save-excursion (beginning-of-line)
  19455. (org-element-at-point)))
  19456. (type (org-element-type element))
  19457. (post-affiliated
  19458. (save-excursion
  19459. (goto-char (org-element-property :begin element))
  19460. (while (looking-at org-element--affiliated-re) (forward-line))
  19461. (point))))
  19462. (unless (< p post-affiliated)
  19463. (case type
  19464. (comment (looking-at "[ \t]*# ?") (match-string 0))
  19465. (footnote-definition "")
  19466. ((item plain-list)
  19467. (make-string (org-list-item-body-column post-affiliated) ? ))
  19468. (paragraph
  19469. ;; Fill prefix is usually the same as the current line,
  19470. ;; except if the paragraph is at the beginning of an item.
  19471. (let ((parent (org-element-property :parent element)))
  19472. (cond ((eq (org-element-type parent) 'item)
  19473. (make-string (org-list-item-body-column
  19474. (org-element-property :begin parent))
  19475. ? ))
  19476. ((save-excursion (beginning-of-line) (looking-at "[ \t]+"))
  19477. (match-string 0))
  19478. (t ""))))
  19479. (comment-block
  19480. ;; Only fill contents if P is within block boundaries.
  19481. (let* ((cbeg (save-excursion (goto-char post-affiliated)
  19482. (forward-line)
  19483. (point)))
  19484. (cend (save-excursion
  19485. (goto-char (org-element-property :end element))
  19486. (skip-chars-backward " \r\t\n")
  19487. (line-beginning-position))))
  19488. (when (and (>= p cbeg) (< p cend))
  19489. (if (save-excursion (beginning-of-line) (looking-at "[ \t]+"))
  19490. (match-string 0)
  19491. ""))))))))))
  19492. (declare-function message-goto-body "message" ())
  19493. (defvar message-cite-prefix-regexp) ; From message.el
  19494. (defvar org-element-all-objects) ; From org-element.el
  19495. (defun org-fill-paragraph (&optional justify)
  19496. "Fill element at point, when applicable.
  19497. This function only applies to comment blocks, comments, example
  19498. blocks and paragraphs. Also, as a special case, re-align table
  19499. when point is at one.
  19500. If JUSTIFY is non-nil (interactively, with prefix argument),
  19501. justify as well. If `sentence-end-double-space' is non-nil, then
  19502. period followed by one space does not end a sentence, so don't
  19503. break a line there. The variable `fill-column' controls the
  19504. width for filling.
  19505. For convenience, when point is at a plain list, an item or
  19506. a footnote definition, try to fill the first paragraph within."
  19507. ;; Falls back on message-fill-paragraph when necessary
  19508. (interactive)
  19509. (if (and (derived-mode-p 'message-mode)
  19510. (or (not (message-in-body-p))
  19511. (save-excursion (move-beginning-of-line 1)
  19512. (looking-at message-cite-prefix-regexp))))
  19513. (let ((fill-paragraph-function
  19514. (cadadr (assoc 'fill-paragraph-function org-fb-vars)))
  19515. (fill-prefix (cadadr (assoc 'fill-prefix org-fb-vars)))
  19516. (paragraph-start (cadadr (assoc 'paragraph-start org-fb-vars)))
  19517. (paragraph-separate
  19518. (cadadr (assoc 'paragraph-separate org-fb-vars))))
  19519. (fill-paragraph nil))
  19520. (save-excursion
  19521. ;; Move to end of line in order to get the first paragraph
  19522. ;; within a plain list or a footnote definition.
  19523. (end-of-line)
  19524. (let ((element (org-element-at-point)))
  19525. ;; First check if point is in a blank line at the beginning of
  19526. ;; the buffer. In that case, ignore filling.
  19527. (if (< (point) (org-element-property :begin element)) t
  19528. (case (org-element-type element)
  19529. ;; Align Org tables, leave table.el tables as-is.
  19530. (table-row (org-table-align) t)
  19531. (table
  19532. (when (eq (org-element-property :type element) 'org)
  19533. (org-table-align))
  19534. t)
  19535. (paragraph
  19536. ;; Paragraphs may contain `line-break' type objects.
  19537. (let ((beg (max (point-min)
  19538. (org-element-property :contents-begin element)))
  19539. (end (min (point-max)
  19540. (org-element-property :contents-end element))))
  19541. ;; Do nothing if point is at an affiliated keyword.
  19542. (if (< (point) beg) t
  19543. (when (derived-mode-p 'message-mode)
  19544. ;; In `message-mode', do not fill following
  19545. ;; citation in current paragraph nor text before
  19546. ;; message body.
  19547. (let ((body-start (save-excursion (message-goto-body))))
  19548. (when body-start (setq beg (max body-start beg))))
  19549. (when (save-excursion
  19550. (re-search-forward
  19551. (concat "^" message-cite-prefix-regexp) end t))
  19552. (setq end (match-beginning 0))))
  19553. ;; Fill paragraph, taking line breaks into
  19554. ;; consideration. For that, slice the paragraph
  19555. ;; using line breaks as separators, and fill the
  19556. ;; parts in reverse order to avoid messing with
  19557. ;; markers.
  19558. (save-excursion
  19559. (goto-char end)
  19560. (mapc
  19561. (lambda (pos)
  19562. (fill-region-as-paragraph pos (point) justify)
  19563. (goto-char pos))
  19564. ;; Find the list of ending positions for line
  19565. ;; breaks in the current paragraph. Add paragraph
  19566. ;; beginning to include first slice.
  19567. (nreverse
  19568. (cons
  19569. beg
  19570. (org-element-map
  19571. (org-element--parse-objects
  19572. beg end nil org-element-all-objects)
  19573. 'line-break
  19574. (lambda (lb) (org-element-property :end lb)))))))
  19575. t)))
  19576. ;; Contents of `comment-block' type elements should be
  19577. ;; filled as plain text, but only if point is within block
  19578. ;; markers.
  19579. (comment-block
  19580. (let* ((case-fold-search t)
  19581. (beg (save-excursion
  19582. (goto-char (org-element-property :begin element))
  19583. (re-search-forward "^[ \t]*#\\+begin_comment" nil t)
  19584. (forward-line)
  19585. (point)))
  19586. (end (save-excursion
  19587. (goto-char (org-element-property :end element))
  19588. (re-search-backward "^[ \t]*#\\+end_comment" nil t)
  19589. (line-beginning-position))))
  19590. (when (and (>= (point) beg) (< (point) end))
  19591. (fill-region-as-paragraph
  19592. (save-excursion
  19593. (end-of-line)
  19594. (re-search-backward "^[ \t]*$" beg 'move)
  19595. (line-beginning-position))
  19596. (save-excursion
  19597. (beginning-of-line)
  19598. (re-search-forward "^[ \t]*$" end 'move)
  19599. (line-beginning-position))
  19600. justify)))
  19601. t)
  19602. ;; Fill comments.
  19603. (comment (fill-comment-paragraph justify))
  19604. ;; Ignore every other element.
  19605. (otherwise t)))))))
  19606. (defun org-auto-fill-function ()
  19607. "Auto-fill function."
  19608. ;; Check if auto-filling is meaningful.
  19609. (let ((fc (current-fill-column)))
  19610. (when (and fc (> (current-column) fc))
  19611. (let ((fill-prefix (org-adaptive-fill-function)))
  19612. (when fill-prefix (do-auto-fill))))))
  19613. (defun org-comment-line-break-function (&optional soft)
  19614. "Break line at point and indent, continuing comment if within one.
  19615. The inserted newline is marked hard if variable
  19616. `use-hard-newlines' is true, unless optional argument SOFT is
  19617. non-nil."
  19618. (if soft (insert-and-inherit ?\n) (newline 1))
  19619. (save-excursion (forward-char -1) (delete-horizontal-space))
  19620. (delete-horizontal-space)
  19621. (indent-to-left-margin)
  19622. (insert-before-markers-and-inherit fill-prefix))
  19623. ;;; Comments
  19624. ;; Org comments syntax is quite complex. It requires the entire line
  19625. ;; to be just a comment. Also, even with the right syntax at the
  19626. ;; beginning of line, some some elements (i.e. verse-block or
  19627. ;; example-block) don't accept comments. Usual Emacs comment commands
  19628. ;; cannot cope with those requirements. Therefore, Org replaces them.
  19629. ;; Org still relies on `comment-dwim', but cannot trust
  19630. ;; `comment-only-p'. So, `comment-region-function' and
  19631. ;; `uncomment-region-function' both point
  19632. ;; to`org-comment-or-uncomment-region'. Eventually,
  19633. ;; `org-insert-comment' takes care of insertion of comments at the
  19634. ;; beginning of line.
  19635. ;; `org-setup-comments-handling' install comments related variables
  19636. ;; during `org-mode' initialization.
  19637. (defun org-setup-comments-handling ()
  19638. (interactive)
  19639. (org-set-local 'comment-use-syntax nil)
  19640. (org-set-local 'comment-start "# ")
  19641. (org-set-local 'comment-start-skip "^\\s-*#\\(?: \\|$\\)")
  19642. (org-set-local 'comment-insert-comment-function 'org-insert-comment)
  19643. (org-set-local 'comment-region-function 'org-comment-or-uncomment-region)
  19644. (org-set-local 'uncomment-region-function 'org-comment-or-uncomment-region))
  19645. (defun org-insert-comment ()
  19646. "Insert an empty comment above current line.
  19647. If the line is empty, insert comment at its beginning."
  19648. (beginning-of-line)
  19649. (if (looking-at "\\s-*$") (replace-match "") (open-line 1))
  19650. (org-indent-line)
  19651. (insert "# "))
  19652. (defvar comment-empty-lines) ; From newcomment.el.
  19653. (defun org-comment-or-uncomment-region (beg end &rest ignore)
  19654. "Comment or uncomment each non-blank line in the region.
  19655. Uncomment each non-blank line between BEG and END if it only
  19656. contains commented lines. Otherwise, comment them."
  19657. (save-restriction
  19658. ;; Restrict region
  19659. (narrow-to-region (save-excursion (goto-char beg)
  19660. (skip-chars-forward " \r\t\n" end)
  19661. (line-beginning-position))
  19662. (save-excursion (goto-char end)
  19663. (skip-chars-backward " \r\t\n" beg)
  19664. (line-end-position)))
  19665. (let ((uncommentp
  19666. ;; UNCOMMENTP is non-nil when every non blank line between
  19667. ;; BEG and END is a comment.
  19668. (save-excursion
  19669. (goto-char (point-min))
  19670. (while (and (not (eobp))
  19671. (let ((element (org-element-at-point)))
  19672. (and (eq (org-element-type element) 'comment)
  19673. (goto-char (min (point-max)
  19674. (org-element-property
  19675. :end element)))))))
  19676. (eobp))))
  19677. (if uncommentp
  19678. ;; Only blank lines and comments in region: uncomment it.
  19679. (save-excursion
  19680. (goto-char (point-min))
  19681. (while (not (eobp))
  19682. (when (looking-at "[ \t]*\\(#\\(?: \\|$\\)\\)")
  19683. (replace-match "" nil nil nil 1))
  19684. (forward-line)))
  19685. ;; Comment each line in region.
  19686. (let ((min-indent (point-max)))
  19687. ;; First find the minimum indentation across all lines.
  19688. (save-excursion
  19689. (goto-char (point-min))
  19690. (while (and (not (eobp)) (not (zerop min-indent)))
  19691. (unless (looking-at "[ \t]*$")
  19692. (setq min-indent (min min-indent (current-indentation))))
  19693. (forward-line)))
  19694. ;; Then loop over all lines.
  19695. (save-excursion
  19696. (goto-char (point-min))
  19697. (while (not (eobp))
  19698. (unless (and (not comment-empty-lines) (looking-at "[ \t]*$"))
  19699. (org-move-to-column min-indent t)
  19700. (insert comment-start))
  19701. (forward-line))))))))
  19702. ;;; Other stuff.
  19703. (defun org-toggle-fixed-width-section (arg)
  19704. "Toggle the fixed-width export.
  19705. If there is no active region, the QUOTE keyword at the current headline is
  19706. inserted or removed. When present, it causes the text between this headline
  19707. and the next to be exported as fixed-width text, and unmodified.
  19708. If there is an active region, this command adds or removes a colon as the
  19709. first character of this line. If the first character of a line is a colon,
  19710. this line is also exported in fixed-width font."
  19711. (interactive "P")
  19712. (let* ((cc 0)
  19713. (regionp (org-region-active-p))
  19714. (beg (if regionp (region-beginning) (point)))
  19715. (end (if regionp (region-end)))
  19716. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  19717. (case-fold-search nil)
  19718. (re "[ \t]*\\(:\\(?: \\|$\\)\\)")
  19719. off)
  19720. (if regionp
  19721. (save-excursion
  19722. (goto-char beg)
  19723. (setq cc (current-column))
  19724. (beginning-of-line 1)
  19725. (setq off (looking-at re))
  19726. (while (> nlines 0)
  19727. (setq nlines (1- nlines))
  19728. (beginning-of-line 1)
  19729. (cond
  19730. (arg
  19731. (org-move-to-column cc t)
  19732. (insert ": \n")
  19733. (forward-line -1))
  19734. ((and off (looking-at re))
  19735. (replace-match "" t t nil 1))
  19736. ((not off) (org-move-to-column cc t) (insert ": ")))
  19737. (forward-line 1)))
  19738. (save-excursion
  19739. (org-back-to-heading)
  19740. (cond
  19741. ((looking-at (format org-heading-keyword-regexp-format
  19742. org-quote-string))
  19743. (goto-char (match-end 1))
  19744. (looking-at (concat " +" org-quote-string))
  19745. (replace-match "" t t)
  19746. (when (eolp) (insert " ")))
  19747. ((looking-at org-outline-regexp)
  19748. (goto-char (match-end 0))
  19749. (insert org-quote-string " ")))))))
  19750. (defun org-reftex-citation ()
  19751. "Use reftex-citation to insert a citation into the buffer.
  19752. This looks for a line like
  19753. #+BIBLIOGRAPHY: foo plain option:-d
  19754. and derives from it that foo.bib is the bibliography file relevant
  19755. for this document. It then installs the necessary environment for RefTeX
  19756. to work in this buffer and calls `reftex-citation' to insert a citation
  19757. into the buffer.
  19758. Export of such citations to both LaTeX and HTML is handled by the contributed
  19759. package org-exp-bibtex by Taru Karttunen."
  19760. (interactive)
  19761. (let ((reftex-docstruct-symbol 'rds)
  19762. (reftex-cite-format "\\cite{%l}")
  19763. rds bib)
  19764. (save-excursion
  19765. (save-restriction
  19766. (widen)
  19767. (let ((case-fold-search t)
  19768. (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
  19769. (if (not (save-excursion
  19770. (or (re-search-forward re nil t)
  19771. (re-search-backward re nil t))))
  19772. (error "No bibliography defined in file")
  19773. (setq bib (concat (match-string 1) ".bib")
  19774. rds (list (list 'bib bib)))))))
  19775. (call-interactively 'reftex-citation)))
  19776. ;;;; Functions extending outline functionality
  19777. (defun org-beginning-of-line (&optional arg)
  19778. "Go to the beginning of the current line. If that is invisible, continue
  19779. to a visible line beginning. This makes the function of C-a more intuitive.
  19780. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  19781. first attempt, and only move to after the tags when the cursor is already
  19782. beyond the end of the headline."
  19783. (interactive "P")
  19784. (let ((pos (point))
  19785. (special (if (consp org-special-ctrl-a/e)
  19786. (car org-special-ctrl-a/e)
  19787. org-special-ctrl-a/e))
  19788. refpos)
  19789. (if (org-bound-and-true-p line-move-visual)
  19790. (beginning-of-visual-line 1)
  19791. (beginning-of-line 1))
  19792. (if (and arg (fboundp 'move-beginning-of-line))
  19793. (call-interactively 'move-beginning-of-line)
  19794. (if (bobp)
  19795. nil
  19796. (backward-char 1)
  19797. (if (org-truely-invisible-p)
  19798. (while (and (not (bobp)) (org-truely-invisible-p))
  19799. (backward-char 1)
  19800. (beginning-of-line 1))
  19801. (forward-char 1))))
  19802. (when special
  19803. (cond
  19804. ((and (looking-at org-complex-heading-regexp)
  19805. (= (char-after (match-end 1)) ?\ ))
  19806. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  19807. (point-at-eol)))
  19808. (goto-char
  19809. (if (eq special t)
  19810. (cond ((> pos refpos) refpos)
  19811. ((= pos (point)) refpos)
  19812. (t (point)))
  19813. (cond ((> pos (point)) (point))
  19814. ((not (eq last-command this-command)) (point))
  19815. (t refpos)))))
  19816. ((org-at-item-p)
  19817. ;; Being at an item and not looking at an the item means point
  19818. ;; was previously moved to beginning of a visual line, which
  19819. ;; doesn't contain the item. Therefore, do nothing special,
  19820. ;; just stay here.
  19821. (when (looking-at org-list-full-item-re)
  19822. ;; Set special position at first white space character after
  19823. ;; bullet, and check-box, if any.
  19824. (let ((after-bullet
  19825. (let ((box (match-end 3)))
  19826. (if (not box) (match-end 1)
  19827. (let ((after (char-after box)))
  19828. (if (and after (= after ? )) (1+ box) box))))))
  19829. ;; Special case: Move point to special position when
  19830. ;; currently after it or at beginning of line.
  19831. (if (eq special t)
  19832. (when (or (> pos after-bullet) (= (point) pos))
  19833. (goto-char after-bullet))
  19834. ;; Reversed case: Move point to special position when
  19835. ;; point was already at beginning of line and command is
  19836. ;; repeated.
  19837. (when (and (= (point) pos) (eq last-command this-command))
  19838. (goto-char after-bullet))))))))
  19839. (org-no-warnings
  19840. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  19841. (defun org-end-of-line (&optional arg)
  19842. "Go to the end of the line.
  19843. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  19844. first attempt, and only move to after the tags when the cursor is already
  19845. beyond the end of the headline."
  19846. (interactive "P")
  19847. (let ((special (if (consp org-special-ctrl-a/e)
  19848. (cdr org-special-ctrl-a/e)
  19849. org-special-ctrl-a/e)))
  19850. (cond
  19851. ((or (not special) arg
  19852. (not (or (org-at-heading-p) (org-at-item-p) (org-at-drawer-p))))
  19853. (call-interactively
  19854. (cond ((org-bound-and-true-p line-move-visual) 'end-of-visual-line)
  19855. ((fboundp 'move-end-of-line) 'move-end-of-line)
  19856. (t 'end-of-line))))
  19857. ((org-at-heading-p)
  19858. (let ((pos (point)))
  19859. (beginning-of-line 1)
  19860. (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*\\)?$"))
  19861. (if (eq special t)
  19862. (if (or (< pos (match-beginning 1))
  19863. (= pos (match-end 0)))
  19864. (goto-char (match-beginning 1))
  19865. (goto-char (match-end 0)))
  19866. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  19867. (goto-char (match-end 0))
  19868. (goto-char (match-beginning 1))))
  19869. (call-interactively (if (fboundp 'move-end-of-line)
  19870. 'move-end-of-line
  19871. 'end-of-line)))))
  19872. ((org-at-drawer-p)
  19873. (move-end-of-line 1)
  19874. (when (overlays-at (1- (point))) (backward-char 1)))
  19875. ;; At an item: Move before any hidden text.
  19876. (t (call-interactively
  19877. (cond ((org-bound-and-true-p line-move-visual) 'end-of-visual-line)
  19878. ((fboundp 'move-end-of-line) 'move-end-of-line)
  19879. (t 'end-of-line)))))
  19880. (org-no-warnings
  19881. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  19882. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  19883. (define-key org-mode-map "\C-e" 'org-end-of-line)
  19884. (defun org-backward-sentence (&optional arg)
  19885. "Go to beginning of sentence, or beginning of table field.
  19886. This will call `backward-sentence' or `org-table-beginning-of-field',
  19887. depending on context."
  19888. (interactive "P")
  19889. (cond
  19890. ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
  19891. (t (call-interactively 'backward-sentence))))
  19892. (defun org-forward-sentence (&optional arg)
  19893. "Go to end of sentence, or end of table field.
  19894. This will call `forward-sentence' or `org-table-end-of-field',
  19895. depending on context."
  19896. (interactive "P")
  19897. (cond
  19898. ((org-at-table-p) (call-interactively 'org-table-end-of-field))
  19899. (t (call-interactively 'forward-sentence))))
  19900. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  19901. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  19902. (defun org-kill-line (&optional arg)
  19903. "Kill line, to tags or end of line."
  19904. (interactive "P")
  19905. (cond
  19906. ((or (not org-special-ctrl-k)
  19907. (bolp)
  19908. (not (org-at-heading-p)))
  19909. (if (and (get-char-property (min (point-max) (point-at-eol)) 'invisible)
  19910. org-ctrl-k-protect-subtree)
  19911. (if (or (eq org-ctrl-k-protect-subtree 'error)
  19912. (not (y-or-n-p "Kill hidden subtree along with headline? ")))
  19913. (error "C-k aborted - would kill hidden subtree")))
  19914. (call-interactively
  19915. (if (and (boundp 'visual-line-mode) visual-line-mode) 'kill-visual-line 'kill-line)))
  19916. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$"))
  19917. (kill-region (point) (match-beginning 1))
  19918. (org-set-tags nil t))
  19919. (t (kill-region (point) (point-at-eol)))))
  19920. (define-key org-mode-map "\C-k" 'org-kill-line)
  19921. (defun org-yank (&optional arg)
  19922. "Yank. If the kill is a subtree, treat it specially.
  19923. This command will look at the current kill and check if is a single
  19924. subtree, or a series of subtrees[1]. If it passes the test, and if the
  19925. cursor is at the beginning of a line or after the stars of a currently
  19926. empty headline, then the yank is handled specially. How exactly depends
  19927. on the value of the following variables, both set by default.
  19928. org-yank-folded-subtrees
  19929. When set, the subtree(s) will be folded after insertion, but only
  19930. if doing so would now swallow text after the yanked text.
  19931. org-yank-adjusted-subtrees
  19932. When set, the subtree will be promoted or demoted in order to
  19933. fit into the local outline tree structure, which means that the level
  19934. will be adjusted so that it becomes the smaller one of the two
  19935. *visible* surrounding headings.
  19936. Any prefix to this command will cause `yank' to be called directly with
  19937. no special treatment. In particular, a simple \\[universal-argument] prefix \
  19938. will just
  19939. plainly yank the text as it is.
  19940. \[1] The test checks if the first non-white line is a heading
  19941. and if there are no other headings with fewer stars."
  19942. (interactive "P")
  19943. (org-yank-generic 'yank arg))
  19944. (defun org-yank-generic (command arg)
  19945. "Perform some yank-like command.
  19946. This function implements the behavior described in the `org-yank'
  19947. documentation. However, it has been generalized to work for any
  19948. interactive command with similar behavior."
  19949. ;; pretend to be command COMMAND
  19950. (setq this-command command)
  19951. (if arg
  19952. (call-interactively command)
  19953. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  19954. (and (org-kill-is-subtree-p)
  19955. (or (bolp)
  19956. (and (looking-at "[ \t]*$")
  19957. (string-match
  19958. "\\`\\*+\\'"
  19959. (buffer-substring (point-at-bol) (point)))))))
  19960. swallowp)
  19961. (cond
  19962. ((and subtreep org-yank-folded-subtrees)
  19963. (let ((beg (point))
  19964. end)
  19965. (if (and subtreep org-yank-adjusted-subtrees)
  19966. (org-paste-subtree nil nil 'for-yank)
  19967. (call-interactively command))
  19968. (setq end (point))
  19969. (goto-char beg)
  19970. (when (and (bolp) subtreep
  19971. (not (setq swallowp
  19972. (org-yank-folding-would-swallow-text beg end))))
  19973. (org-with-limited-levels
  19974. (or (looking-at org-outline-regexp)
  19975. (re-search-forward org-outline-regexp-bol end t))
  19976. (while (and (< (point) end) (looking-at org-outline-regexp))
  19977. (hide-subtree)
  19978. (org-cycle-show-empty-lines 'folded)
  19979. (condition-case nil
  19980. (outline-forward-same-level 1)
  19981. (error (goto-char end))))))
  19982. (when swallowp
  19983. (message
  19984. "Inserted text not folded because that would swallow text"))
  19985. (goto-char end)
  19986. (skip-chars-forward " \t\n\r")
  19987. (beginning-of-line 1)
  19988. (push-mark beg 'nomsg)))
  19989. ((and subtreep org-yank-adjusted-subtrees)
  19990. (let ((beg (point-at-bol)))
  19991. (org-paste-subtree nil nil 'for-yank)
  19992. (push-mark beg 'nomsg)))
  19993. (t
  19994. (call-interactively command))))))
  19995. (defun org-yank-folding-would-swallow-text (beg end)
  19996. "Would hide-subtree at BEG swallow any text after END?"
  19997. (let (level)
  19998. (org-with-limited-levels
  19999. (save-excursion
  20000. (goto-char beg)
  20001. (when (or (looking-at org-outline-regexp)
  20002. (re-search-forward org-outline-regexp-bol end t))
  20003. (setq level (org-outline-level)))
  20004. (goto-char end)
  20005. (skip-chars-forward " \t\r\n\v\f")
  20006. (if (or (eobp)
  20007. (and (bolp) (looking-at org-outline-regexp)
  20008. (<= (org-outline-level) level)))
  20009. nil ; Nothing would be swallowed
  20010. t))))) ; something would swallow
  20011. (define-key org-mode-map "\C-y" 'org-yank)
  20012. (defun org-truely-invisible-p ()
  20013. "Check if point is at a character currently not visible.
  20014. This version does not only check the character property, but also
  20015. `visible-mode'."
  20016. ;; Early versions of noutline don't have `outline-invisible-p'.
  20017. (if (org-bound-and-true-p visible-mode)
  20018. nil
  20019. (outline-invisible-p)))
  20020. (defun org-invisible-p2 ()
  20021. "Check if point is at a character currently not visible."
  20022. (save-excursion
  20023. (if (and (eolp) (not (bobp))) (backward-char 1))
  20024. ;; Early versions of noutline don't have `outline-invisible-p'.
  20025. (outline-invisible-p)))
  20026. (defun org-back-to-heading (&optional invisible-ok)
  20027. "Call `outline-back-to-heading', but provide a better error message."
  20028. (condition-case nil
  20029. (outline-back-to-heading invisible-ok)
  20030. (error (error "Before first headline at position %d in buffer %s"
  20031. (point) (current-buffer)))))
  20032. (defun org-before-first-heading-p ()
  20033. "Before first heading?"
  20034. (save-excursion
  20035. (end-of-line)
  20036. (null (re-search-backward org-outline-regexp-bol nil t))))
  20037. (defun org-at-heading-p (&optional ignored)
  20038. (outline-on-heading-p t))
  20039. ;; Compatibility alias with Org versions < 7.8.03
  20040. (defalias 'org-on-heading-p 'org-at-heading-p)
  20041. (defun org-at-comment-p nil
  20042. "Is cursor in a line starting with a # character?"
  20043. (save-excursion
  20044. (beginning-of-line)
  20045. (looking-at "^#")))
  20046. (defun org-at-drawer-p nil
  20047. "Is cursor at a drawer keyword?"
  20048. (save-excursion
  20049. (move-beginning-of-line 1)
  20050. (looking-at org-drawer-regexp)))
  20051. (defun org-at-block-p nil
  20052. "Is cursor at a block keyword?"
  20053. (save-excursion
  20054. (move-beginning-of-line 1)
  20055. (looking-at org-block-regexp)))
  20056. (defun org-point-at-end-of-empty-headline ()
  20057. "If point is at the end of an empty headline, return t, else nil.
  20058. If the heading only contains a TODO keyword, it is still still considered
  20059. empty."
  20060. (and (looking-at "[ \t]*$")
  20061. (when org-todo-line-regexp
  20062. (save-excursion
  20063. (beginning-of-line 1)
  20064. (let ((case-fold-search nil))
  20065. (looking-at org-todo-line-regexp)
  20066. (string= (match-string 3) ""))))))
  20067. (defun org-at-heading-or-item-p ()
  20068. (or (org-at-heading-p) (org-at-item-p)))
  20069. (defun org-at-target-p ()
  20070. (or (org-in-regexp org-radio-target-regexp)
  20071. (org-in-regexp org-target-regexp)))
  20072. ;; Compatibility alias with Org versions < 7.8.03
  20073. (defalias 'org-on-target-p 'org-at-target-p)
  20074. (defun org-up-heading-all (arg)
  20075. "Move to the heading line of which the present line is a subheading.
  20076. This function considers both visible and invisible heading lines.
  20077. With argument, move up ARG levels."
  20078. (if (fboundp 'outline-up-heading-all)
  20079. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  20080. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  20081. (defun org-up-heading-safe ()
  20082. "Move to the heading line of which the present line is a subheading.
  20083. This version will not throw an error. It will return the level of the
  20084. headline found, or nil if no higher level is found.
  20085. Also, this function will be a lot faster than `outline-up-heading',
  20086. because it relies on stars being the outline starters. This can really
  20087. make a significant difference in outlines with very many siblings."
  20088. (let (start-level re)
  20089. (org-back-to-heading t)
  20090. (setq start-level (funcall outline-level))
  20091. (if (equal start-level 1)
  20092. nil
  20093. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  20094. (if (re-search-backward re nil t)
  20095. (funcall outline-level)))))
  20096. (defun org-first-sibling-p ()
  20097. "Is this heading the first child of its parents?"
  20098. (interactive)
  20099. (let ((re org-outline-regexp-bol)
  20100. level l)
  20101. (unless (org-at-heading-p t)
  20102. (error "Not at a heading"))
  20103. (setq level (funcall outline-level))
  20104. (save-excursion
  20105. (if (not (re-search-backward re nil t))
  20106. t
  20107. (setq l (funcall outline-level))
  20108. (< l level)))))
  20109. (defun org-goto-sibling (&optional previous)
  20110. "Goto the next sibling, even if it is invisible.
  20111. When PREVIOUS is set, go to the previous sibling instead. Returns t
  20112. when a sibling was found. When none is found, return nil and don't
  20113. move point."
  20114. (let ((fun (if previous 're-search-backward 're-search-forward))
  20115. (pos (point))
  20116. (re org-outline-regexp-bol)
  20117. level l)
  20118. (when (condition-case nil (org-back-to-heading t) (error nil))
  20119. (setq level (funcall outline-level))
  20120. (catch 'exit
  20121. (or previous (forward-char 1))
  20122. (while (funcall fun re nil t)
  20123. (setq l (funcall outline-level))
  20124. (when (< l level) (goto-char pos) (throw 'exit nil))
  20125. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  20126. (goto-char pos)
  20127. nil))))
  20128. (defun org-show-siblings ()
  20129. "Show all siblings of the current headline."
  20130. (save-excursion
  20131. (while (org-goto-sibling) (org-flag-heading nil)))
  20132. (save-excursion
  20133. (while (org-goto-sibling 'previous)
  20134. (org-flag-heading nil))))
  20135. (defun org-goto-first-child ()
  20136. "Goto the first child, even if it is invisible.
  20137. Return t when a child was found. Otherwise don't move point and
  20138. return nil."
  20139. (let (level (pos (point)) (re org-outline-regexp-bol))
  20140. (when (condition-case nil (org-back-to-heading t) (error nil))
  20141. (setq level (outline-level))
  20142. (forward-char 1)
  20143. (if (and (re-search-forward re nil t) (> (outline-level) level))
  20144. (progn (goto-char (match-beginning 0)) t)
  20145. (goto-char pos) nil))))
  20146. (defun org-show-hidden-entry ()
  20147. "Show an entry where even the heading is hidden."
  20148. (save-excursion
  20149. (org-show-entry)))
  20150. (defun org-flag-heading (flag &optional entry)
  20151. "Flag the current heading. FLAG non-nil means make invisible.
  20152. When ENTRY is non-nil, show the entire entry."
  20153. (save-excursion
  20154. (org-back-to-heading t)
  20155. ;; Check if we should show the entire entry
  20156. (if entry
  20157. (progn
  20158. (org-show-entry)
  20159. (save-excursion
  20160. (and (outline-next-heading)
  20161. (org-flag-heading nil))))
  20162. (outline-flag-region (max (point-min) (1- (point)))
  20163. (save-excursion (outline-end-of-heading) (point))
  20164. flag))))
  20165. (defun org-get-next-sibling ()
  20166. "Move to next heading of the same level, and return point.
  20167. If there is no such heading, return nil.
  20168. This is like outline-next-sibling, but invisible headings are ok."
  20169. (let ((level (funcall outline-level)))
  20170. (outline-next-heading)
  20171. (while (and (not (eobp)) (> (funcall outline-level) level))
  20172. (outline-next-heading))
  20173. (if (or (eobp) (< (funcall outline-level) level))
  20174. nil
  20175. (point))))
  20176. (defun org-get-last-sibling ()
  20177. "Move to previous heading of the same level, and return point.
  20178. If there is no such heading, return nil."
  20179. (let ((opoint (point))
  20180. (level (funcall outline-level)))
  20181. (outline-previous-heading)
  20182. (when (and (/= (point) opoint) (outline-on-heading-p t))
  20183. (while (and (> (funcall outline-level) level)
  20184. (not (bobp)))
  20185. (outline-previous-heading))
  20186. (if (< (funcall outline-level) level)
  20187. nil
  20188. (point)))))
  20189. (defun org-end-of-subtree (&optional invisible-ok to-heading)
  20190. "Goto to the end of a subtree."
  20191. ;; This contains an exact copy of the original function, but it uses
  20192. ;; `org-back-to-heading', to make it work also in invisible
  20193. ;; trees. And is uses an invisible-ok argument.
  20194. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  20195. ;; Furthermore, when used inside Org, finding the end of a large subtree
  20196. ;; with many children and grandchildren etc, this can be much faster
  20197. ;; than the outline version.
  20198. (org-back-to-heading invisible-ok)
  20199. (let ((first t)
  20200. (level (funcall outline-level)))
  20201. (if (and (derived-mode-p 'org-mode) (< level 1000))
  20202. ;; A true heading (not a plain list item), in Org-mode
  20203. ;; This means we can easily find the end by looking
  20204. ;; only for the right number of stars. Using a regexp to do
  20205. ;; this is so much faster than using a Lisp loop.
  20206. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  20207. (forward-char 1)
  20208. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  20209. ;; something else, do it the slow way
  20210. (while (and (not (eobp))
  20211. (or first (> (funcall outline-level) level)))
  20212. (setq first nil)
  20213. (outline-next-heading)))
  20214. (unless to-heading
  20215. (if (memq (preceding-char) '(?\n ?\^M))
  20216. (progn
  20217. ;; Go to end of line before heading
  20218. (forward-char -1)
  20219. (if (memq (preceding-char) '(?\n ?\^M))
  20220. ;; leave blank line before heading
  20221. (forward-char -1))))))
  20222. (point))
  20223. (defadvice outline-end-of-subtree (around prefer-org-version activate compile)
  20224. "Use Org version in org-mode, for dramatic speed-up."
  20225. (if (derived-mode-p 'org-mode)
  20226. (progn
  20227. (org-end-of-subtree nil t)
  20228. (unless (eobp) (backward-char 1)))
  20229. ad-do-it))
  20230. (defun org-end-of-meta-data-and-drawers ()
  20231. "Jump to the first text after meta data and drawers in the current entry.
  20232. This will move over empty lines, lines with planning time stamps,
  20233. clocking lines, and drawers."
  20234. (org-back-to-heading t)
  20235. (let ((end (save-excursion (outline-next-heading) (point)))
  20236. (re (concat "\\(" org-drawer-regexp "\\)"
  20237. "\\|" "[ \t]*" org-keyword-time-regexp)))
  20238. (forward-line 1)
  20239. (while (re-search-forward re end t)
  20240. (if (not (match-end 1))
  20241. ;; empty or planning line
  20242. (forward-line 1)
  20243. ;; a drawer, find the end
  20244. (re-search-forward "^[ \t]*:END:" end 'move)
  20245. (forward-line 1)))
  20246. (and (re-search-forward "[^\n]" nil t) (backward-char 1))
  20247. (point)))
  20248. (defun org-forward-heading-same-level (arg &optional invisible-ok)
  20249. "Move forward to the arg'th subheading at same level as this one.
  20250. Stop at the first and last subheadings of a superior heading.
  20251. Normally this only looks at visible headings, but when INVISIBLE-OK is
  20252. non-nil it will also look at invisible ones."
  20253. (interactive "p")
  20254. (org-back-to-heading invisible-ok)
  20255. (org-at-heading-p)
  20256. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  20257. (re (format "^\\*\\{1,%d\\} " level))
  20258. l)
  20259. (forward-char 1)
  20260. (while (> arg 0)
  20261. (while (and (re-search-forward re nil 'move)
  20262. (setq l (- (match-end 0) (match-beginning 0) 1))
  20263. (= l level)
  20264. (not invisible-ok)
  20265. (progn (backward-char 1) (outline-invisible-p)))
  20266. (if (< l level) (setq arg 1)))
  20267. (setq arg (1- arg)))
  20268. (beginning-of-line 1)))
  20269. (defun org-backward-heading-same-level (arg &optional invisible-ok)
  20270. "Move backward to the arg'th subheading at same level as this one.
  20271. Stop at the first and last subheadings of a superior heading."
  20272. (interactive "p")
  20273. (org-back-to-heading)
  20274. (org-at-heading-p)
  20275. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  20276. (re (format "^\\*\\{1,%d\\} " level))
  20277. l)
  20278. (while (> arg 0)
  20279. (while (and (re-search-backward re nil 'move)
  20280. (setq l (- (match-end 0) (match-beginning 0) 1))
  20281. (= l level)
  20282. (not invisible-ok)
  20283. (outline-invisible-p))
  20284. (if (< l level) (setq arg 1)))
  20285. (setq arg (1- arg)))))
  20286. (defun org-forward-element ()
  20287. "Move forward by one element.
  20288. Move to the next element at the same level, when possible."
  20289. (interactive)
  20290. (cond ((eobp) (error "Cannot move further down"))
  20291. ((org-with-limited-levels (org-at-heading-p))
  20292. (let ((origin (point)))
  20293. (org-forward-heading-same-level 1)
  20294. (unless (org-with-limited-levels (org-at-heading-p))
  20295. (goto-char origin)
  20296. (error "Cannot move further down"))))
  20297. (t
  20298. (let* ((elem (org-element-at-point))
  20299. (end (org-element-property :end elem))
  20300. (parent (org-element-property :parent elem)))
  20301. (if (and parent (= (org-element-property :contents-end parent) end))
  20302. (goto-char (org-element-property :end parent))
  20303. (goto-char end))))))
  20304. (defun org-backward-element ()
  20305. "Move backward by one element.
  20306. Move to the previous element at the same level, when possible."
  20307. (interactive)
  20308. (cond ((bobp) (error "Cannot move further up"))
  20309. ((org-with-limited-levels (org-at-heading-p))
  20310. ;; At an headline, move to the previous one, if any, or stay
  20311. ;; here.
  20312. (let ((origin (point)))
  20313. (org-backward-heading-same-level 1)
  20314. (unless (org-with-limited-levels (org-at-heading-p))
  20315. (goto-char origin)
  20316. (error "Cannot move further up"))))
  20317. (t
  20318. (let* ((trail (org-element-at-point 'keep-trail))
  20319. (elem (car trail))
  20320. (prev-elem (nth 1 trail))
  20321. (beg (org-element-property :begin elem)))
  20322. (cond
  20323. ;; Move to beginning of current element if point isn't
  20324. ;; there already.
  20325. ((/= (point) beg) (goto-char beg))
  20326. (prev-elem (goto-char (org-element-property :begin prev-elem)))
  20327. ((org-before-first-heading-p) (goto-char (point-min)))
  20328. (t (org-back-to-heading)))))))
  20329. (defun org-up-element ()
  20330. "Move to upper element."
  20331. (interactive)
  20332. (if (org-with-limited-levels (org-at-heading-p))
  20333. (unless (org-up-heading-safe) (error "No surrounding element"))
  20334. (let* ((elem (org-element-at-point))
  20335. (parent (org-element-property :parent elem)))
  20336. (if parent (goto-char (org-element-property :begin parent))
  20337. (if (org-with-limited-levels (org-before-first-heading-p))
  20338. (error "No surrounding element")
  20339. (org-with-limited-levels (org-back-to-heading)))))))
  20340. (defvar org-element-greater-elements)
  20341. (defun org-down-element ()
  20342. "Move to inner element."
  20343. (interactive)
  20344. (let ((element (org-element-at-point)))
  20345. (cond
  20346. ((memq (org-element-type element) '(plain-list table))
  20347. (goto-char (org-element-property :contents-begin element))
  20348. (forward-char))
  20349. ((memq (org-element-type element) org-element-greater-elements)
  20350. ;; If contents are hidden, first disclose them.
  20351. (when (org-element-property :hiddenp element) (org-cycle))
  20352. (goto-char (or (org-element-property :contents-begin element)
  20353. (error "No content for this element"))))
  20354. (t (error "No inner element")))))
  20355. (defun org-drag-element-backward ()
  20356. "Move backward element at point."
  20357. (interactive)
  20358. (if (org-with-limited-levels (org-at-heading-p)) (org-move-subtree-up)
  20359. (let* ((trail (org-element-at-point 'keep-trail))
  20360. (elem (car trail))
  20361. (prev-elem (nth 1 trail)))
  20362. ;; Error out if no previous element or previous element is
  20363. ;; a parent of the current one.
  20364. (if (or (not prev-elem) (org-element-nested-p elem prev-elem))
  20365. (error "Cannot drag element backward")
  20366. (let ((pos (point)))
  20367. (org-element-swap-A-B prev-elem elem)
  20368. (goto-char (+ (org-element-property :begin prev-elem)
  20369. (- pos (org-element-property :begin elem)))))))))
  20370. (defun org-drag-element-forward ()
  20371. "Move forward element at point."
  20372. (interactive)
  20373. (let* ((pos (point))
  20374. (elem (org-element-at-point)))
  20375. (when (= (point-max) (org-element-property :end elem))
  20376. (error "Cannot drag element forward"))
  20377. (goto-char (org-element-property :end elem))
  20378. (let ((next-elem (org-element-at-point)))
  20379. (when (or (org-element-nested-p elem next-elem)
  20380. (and (eq (org-element-type next-elem) 'headline)
  20381. (not (eq (org-element-type elem) 'headline))))
  20382. (goto-char pos)
  20383. (error "Cannot drag element forward"))
  20384. ;; Compute new position of point: it's shifted by NEXT-ELEM
  20385. ;; body's length (without final blanks) and by the length of
  20386. ;; blanks between ELEM and NEXT-ELEM.
  20387. (let ((size-next (- (save-excursion
  20388. (goto-char (org-element-property :end next-elem))
  20389. (skip-chars-backward " \r\t\n")
  20390. (forward-line)
  20391. ;; Small correction if buffer doesn't end
  20392. ;; with a newline character.
  20393. (if (and (eolp) (not (bolp))) (1+ (point)) (point)))
  20394. (org-element-property :begin next-elem)))
  20395. (size-blank (- (org-element-property :end elem)
  20396. (save-excursion
  20397. (goto-char (org-element-property :end elem))
  20398. (skip-chars-backward " \r\t\n")
  20399. (forward-line)
  20400. (point)))))
  20401. (org-element-swap-A-B elem next-elem)
  20402. (goto-char (+ pos size-next size-blank))))))
  20403. (defun org-mark-element ()
  20404. "Put point at beginning of this element, mark at end.
  20405. Interactively, if this command is repeated or (in Transient Mark
  20406. mode) if the mark is active, it marks the next element after the
  20407. ones already marked."
  20408. (interactive)
  20409. (let (deactivate-mark)
  20410. (if (and (org-called-interactively-p 'any)
  20411. (or (and (eq last-command this-command) (mark t))
  20412. (and transient-mark-mode mark-active)))
  20413. (set-mark
  20414. (save-excursion
  20415. (goto-char (mark))
  20416. (goto-char (org-element-property :end (org-element-at-point)))))
  20417. (let ((element (org-element-at-point)))
  20418. (end-of-line)
  20419. (push-mark (org-element-property :end element) t t)
  20420. (goto-char (org-element-property :begin element))))))
  20421. (defun org-narrow-to-element ()
  20422. "Narrow buffer to current element."
  20423. (interactive)
  20424. (let ((elem (org-element-at-point)))
  20425. (cond
  20426. ((eq (car elem) 'headline)
  20427. (narrow-to-region
  20428. (org-element-property :begin elem)
  20429. (org-element-property :end elem)))
  20430. ((memq (car elem) org-element-greater-elements)
  20431. (narrow-to-region
  20432. (org-element-property :contents-begin elem)
  20433. (org-element-property :contents-end elem)))
  20434. (t
  20435. (narrow-to-region
  20436. (org-element-property :begin elem)
  20437. (org-element-property :end elem))))))
  20438. (defun org-transpose-element ()
  20439. "Transpose current and previous elements, keeping blank lines between.
  20440. Point is moved after both elements."
  20441. (interactive)
  20442. (org-skip-whitespace)
  20443. (let ((end (org-element-property :end (org-element-at-point))))
  20444. (org-drag-element-backward)
  20445. (goto-char end)))
  20446. (defun org-unindent-buffer ()
  20447. "Un-indent the visible part of the buffer.
  20448. Relative indentation (between items, inside blocks, etc.) isn't
  20449. modified."
  20450. (interactive)
  20451. (unless (eq major-mode 'org-mode)
  20452. (error "Cannot un-indent a buffer not in Org mode"))
  20453. (let* ((parse-tree (org-element-parse-buffer 'greater-element))
  20454. unindent-tree ; For byte-compiler.
  20455. (unindent-tree
  20456. (function
  20457. (lambda (contents)
  20458. (mapc
  20459. (lambda (element)
  20460. (if (memq (org-element-type element) '(headline section))
  20461. (funcall unindent-tree (org-element-contents element))
  20462. (save-excursion
  20463. (save-restriction
  20464. (narrow-to-region
  20465. (org-element-property :begin element)
  20466. (org-element-property :end element))
  20467. (org-do-remove-indentation)))))
  20468. (reverse contents))))))
  20469. (funcall unindent-tree (org-element-contents parse-tree))))
  20470. (defun org-show-subtree ()
  20471. "Show everything after this heading at deeper levels."
  20472. (interactive)
  20473. (outline-flag-region
  20474. (point)
  20475. (save-excursion
  20476. (org-end-of-subtree t t))
  20477. nil))
  20478. (defun org-show-entry ()
  20479. "Show the body directly following this heading.
  20480. Show the heading too, if it is currently invisible."
  20481. (interactive)
  20482. (save-excursion
  20483. (condition-case nil
  20484. (progn
  20485. (org-back-to-heading t)
  20486. (outline-flag-region
  20487. (max (point-min) (1- (point)))
  20488. (save-excursion
  20489. (if (re-search-forward
  20490. (concat "[\r\n]\\(" org-outline-regexp "\\)") nil t)
  20491. (match-beginning 1)
  20492. (point-max)))
  20493. nil)
  20494. (org-cycle-hide-drawers 'children))
  20495. (error nil))))
  20496. (defun org-make-options-regexp (kwds &optional extra)
  20497. "Make a regular expression for keyword lines."
  20498. (concat
  20499. "^#\\+\\("
  20500. (mapconcat 'regexp-quote kwds "\\|")
  20501. (if extra (concat "\\|" extra))
  20502. "\\):[ \t]*\\(.*\\)"))
  20503. ;; Make isearch reveal the necessary context
  20504. (defun org-isearch-end ()
  20505. "Reveal context after isearch exits."
  20506. (when isearch-success ; only if search was successful
  20507. (if (featurep 'xemacs)
  20508. ;; Under XEmacs, the hook is run in the correct place,
  20509. ;; we directly show the context.
  20510. (org-show-context 'isearch)
  20511. ;; In Emacs the hook runs *before* restoring the overlays.
  20512. ;; So we have to use a one-time post-command-hook to do this.
  20513. ;; (Emacs 22 has a special variable, see function `org-mode')
  20514. (unless (and (boundp 'isearch-mode-end-hook-quit)
  20515. isearch-mode-end-hook-quit)
  20516. ;; Only when the isearch was not quitted.
  20517. (org-add-hook 'post-command-hook 'org-isearch-post-command
  20518. 'append 'local)))))
  20519. (defun org-isearch-post-command ()
  20520. "Remove self from hook, and show context."
  20521. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  20522. (org-show-context 'isearch))
  20523. ;;;; Integration with and fixes for other packages
  20524. ;;; Imenu support
  20525. (defvar org-imenu-markers nil
  20526. "All markers currently used by Imenu.")
  20527. (make-variable-buffer-local 'org-imenu-markers)
  20528. (defun org-imenu-new-marker (&optional pos)
  20529. "Return a new marker for use by Imenu, and remember the marker."
  20530. (let ((m (make-marker)))
  20531. (move-marker m (or pos (point)))
  20532. (push m org-imenu-markers)
  20533. m))
  20534. (defun org-imenu-get-tree ()
  20535. "Produce the index for Imenu."
  20536. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  20537. (setq org-imenu-markers nil)
  20538. (let* ((n org-imenu-depth)
  20539. (re (concat "^" (org-get-limited-outline-regexp)))
  20540. (subs (make-vector (1+ n) nil))
  20541. (last-level 0)
  20542. m level head)
  20543. (save-excursion
  20544. (save-restriction
  20545. (widen)
  20546. (goto-char (point-max))
  20547. (while (re-search-backward re nil t)
  20548. (setq level (org-reduced-level (funcall outline-level)))
  20549. (when (and (<= level n)
  20550. (looking-at org-complex-heading-regexp))
  20551. (setq head (org-link-display-format
  20552. (org-match-string-no-properties 4))
  20553. m (org-imenu-new-marker))
  20554. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  20555. (if (>= level last-level)
  20556. (push (cons head m) (aref subs level))
  20557. (push (cons head (aref subs (1+ level))) (aref subs level))
  20558. (loop for i from (1+ level) to n do (aset subs i nil)))
  20559. (setq last-level level)))))
  20560. (aref subs 1)))
  20561. (eval-after-load "imenu"
  20562. '(progn
  20563. (add-hook 'imenu-after-jump-hook
  20564. (lambda ()
  20565. (if (derived-mode-p 'org-mode)
  20566. (org-show-context 'org-goto))))))
  20567. (defun org-link-display-format (link)
  20568. "Replace a link with either the description, or the link target
  20569. if no description is present"
  20570. (save-match-data
  20571. (if (string-match org-bracket-link-analytic-regexp link)
  20572. (replace-match (if (match-end 5)
  20573. (match-string 5 link)
  20574. (concat (match-string 1 link)
  20575. (match-string 3 link)))
  20576. nil t link)
  20577. link)))
  20578. (defun org-toggle-link-display ()
  20579. "Toggle the literal or descriptive display of links."
  20580. (interactive)
  20581. (if org-descriptive-links
  20582. (progn (org-remove-from-invisibility-spec '(org-link))
  20583. (org-restart-font-lock)
  20584. (setq org-descriptive-links nil))
  20585. (progn (add-to-invisibility-spec '(org-link))
  20586. (org-restart-font-lock)
  20587. (setq org-descriptive-links t))))
  20588. ;; Speedbar support
  20589. (defvar org-speedbar-restriction-lock-overlay (make-overlay 1 1)
  20590. "Overlay marking the agenda restriction line in speedbar.")
  20591. (overlay-put org-speedbar-restriction-lock-overlay
  20592. 'face 'org-agenda-restriction-lock)
  20593. (overlay-put org-speedbar-restriction-lock-overlay
  20594. 'help-echo "Agendas are currently limited to this item.")
  20595. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  20596. (defun org-speedbar-set-agenda-restriction ()
  20597. "Restrict future agenda commands to the location at point in speedbar.
  20598. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  20599. (interactive)
  20600. (require 'org-agenda)
  20601. (let (p m tp np dir txt)
  20602. (cond
  20603. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  20604. 'org-imenu t))
  20605. (setq m (get-text-property p 'org-imenu-marker))
  20606. (with-current-buffer (marker-buffer m)
  20607. (goto-char m)
  20608. (org-agenda-set-restriction-lock 'subtree)))
  20609. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  20610. 'speedbar-function 'speedbar-find-file))
  20611. (setq tp (previous-single-property-change
  20612. (1+ p) 'speedbar-function)
  20613. np (next-single-property-change
  20614. tp 'speedbar-function)
  20615. dir (speedbar-line-directory)
  20616. txt (buffer-substring-no-properties (or tp (point-min))
  20617. (or np (point-max))))
  20618. (with-current-buffer (find-file-noselect
  20619. (let ((default-directory dir))
  20620. (expand-file-name txt)))
  20621. (unless (derived-mode-p 'org-mode)
  20622. (error "Cannot restrict to non-Org-mode file"))
  20623. (org-agenda-set-restriction-lock 'file)))
  20624. (t (error "Don't know how to restrict Org-mode's agenda")))
  20625. (move-overlay org-speedbar-restriction-lock-overlay
  20626. (point-at-bol) (point-at-eol))
  20627. (setq current-prefix-arg nil)
  20628. (org-agenda-maybe-redo)))
  20629. (eval-after-load "speedbar"
  20630. '(progn
  20631. (speedbar-add-supported-extension ".org")
  20632. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  20633. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  20634. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  20635. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  20636. (add-hook 'speedbar-visiting-tag-hook
  20637. (lambda () (and (derived-mode-p 'org-mode) (org-show-context 'org-goto))))))
  20638. ;;; Fixes and Hacks for problems with other packages
  20639. ;; Make flyspell not check words in links, to not mess up our keymap
  20640. (defun org-mode-flyspell-verify ()
  20641. "Don't let flyspell put overlays at active buttons, or on
  20642. {todo,all-time,additional-option-like}-keywords."
  20643. (let ((pos (max (1- (point)) (point-min)))
  20644. (word (thing-at-point 'word)))
  20645. (and (not (get-text-property pos 'keymap))
  20646. (not (get-text-property pos 'org-no-flyspell))
  20647. (not (member word org-todo-keywords-1))
  20648. (not (member word org-all-time-keywords))
  20649. (not (member word org-options-keywords))
  20650. (not (member word (mapcar 'car org-startup-options)))
  20651. (not (member word org-additional-option-like-keywords-for-flyspell)))))
  20652. (defun org-remove-flyspell-overlays-in (beg end)
  20653. "Remove flyspell overlays in region."
  20654. (and (org-bound-and-true-p flyspell-mode)
  20655. (fboundp 'flyspell-delete-region-overlays)
  20656. (flyspell-delete-region-overlays beg end))
  20657. (add-text-properties beg end '(org-no-flyspell t)))
  20658. ;; Make `bookmark-jump' shows the jump location if it was hidden.
  20659. (eval-after-load "bookmark"
  20660. '(if (boundp 'bookmark-after-jump-hook)
  20661. ;; We can use the hook
  20662. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  20663. ;; Hook not available, use advice
  20664. (defadvice bookmark-jump (after org-make-visible activate)
  20665. "Make the position visible."
  20666. (org-bookmark-jump-unhide))))
  20667. ;; Make sure saveplace shows the location if it was hidden
  20668. (eval-after-load "saveplace"
  20669. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  20670. "Make the position visible."
  20671. (org-bookmark-jump-unhide)))
  20672. ;; Make sure ecb shows the location if it was hidden
  20673. (eval-after-load "ecb"
  20674. '(defadvice ecb-method-clicked (after esf/org-show-context activate)
  20675. "Make hierarchy visible when jumping into location from ECB tree buffer."
  20676. (if (derived-mode-p 'org-mode)
  20677. (org-show-context))))
  20678. (defun org-bookmark-jump-unhide ()
  20679. "Unhide the current position, to show the bookmark location."
  20680. (and (derived-mode-p 'org-mode)
  20681. (or (outline-invisible-p)
  20682. (save-excursion (goto-char (max (point-min) (1- (point))))
  20683. (outline-invisible-p)))
  20684. (org-show-context 'bookmark-jump)))
  20685. ;; Make session.el ignore our circular variable
  20686. (eval-after-load "session"
  20687. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  20688. ;;;; Experimental code
  20689. (defun org-closed-in-range ()
  20690. "Sparse tree of items closed in a certain time range.
  20691. Still experimental, may disappear in the future."
  20692. (interactive)
  20693. ;; Get the time interval from the user.
  20694. (let* ((time1 (org-float-time
  20695. (org-read-date nil 'to-time nil "Starting date: ")))
  20696. (time2 (org-float-time
  20697. (org-read-date nil 'to-time nil "End date:")))
  20698. ;; callback function
  20699. (callback (lambda ()
  20700. (let ((time
  20701. (org-float-time
  20702. (apply 'encode-time
  20703. (org-parse-time-string
  20704. (match-string 1))))))
  20705. ;; check if time in interval
  20706. (and (>= time time1) (<= time time2))))))
  20707. ;; make tree, check each match with the callback
  20708. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  20709. ;;;; Finish up
  20710. (provide 'org)
  20711. (run-hooks 'org-load-hook)
  20712. ;;; org.el ends here