org.el 790 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720177211772217723177241772517726177271772817729177301773117732177331773417735177361773717738177391774017741177421774317744177451774617747177481774917750177511775217753177541775517756177571775817759177601776117762177631776417765177661776717768177691777017771177721777317774177751777617777177781777917780177811778217783177841778517786177871778817789177901779117792177931779417795177961779717798177991780017801178021780317804178051780617807178081780917810178111781217813178141781517816178171781817819178201782117822178231782417825178261782717828178291783017831178321783317834178351783617837178381783917840178411784217843178441784517846178471784817849178501785117852178531785417855178561785717858178591786017861178621786317864178651786617867178681786917870178711787217873178741787517876178771787817879178801788117882178831788417885178861788717888178891789017891178921789317894178951789617897178981789917900179011790217903179041790517906179071790817909179101791117912179131791417915179161791717918179191792017921179221792317924179251792617927179281792917930179311793217933179341793517936179371793817939179401794117942179431794417945179461794717948179491795017951179521795317954179551795617957179581795917960179611796217963179641796517966179671796817969179701797117972179731797417975179761797717978179791798017981179821798317984179851798617987179881798917990179911799217993179941799517996179971799817999180001800118002180031800418005180061800718008180091801018011180121801318014180151801618017180181801918020180211802218023180241802518026180271802818029180301803118032180331803418035180361803718038180391804018041180421804318044180451804618047180481804918050180511805218053180541805518056180571805818059180601806118062180631806418065180661806718068180691807018071180721807318074180751807618077180781807918080180811808218083180841808518086180871808818089180901809118092180931809418095180961809718098180991810018101181021810318104181051810618107181081810918110181111811218113181141811518116181171811818119181201812118122181231812418125181261812718128181291813018131181321813318134181351813618137181381813918140181411814218143181441814518146181471814818149181501815118152181531815418155181561815718158181591816018161181621816318164181651816618167181681816918170181711817218173181741817518176181771817818179181801818118182181831818418185181861818718188181891819018191181921819318194181951819618197181981819918200182011820218203182041820518206182071820818209182101821118212182131821418215182161821718218182191822018221182221822318224182251822618227182281822918230182311823218233182341823518236182371823818239182401824118242182431824418245182461824718248182491825018251182521825318254182551825618257182581825918260182611826218263182641826518266182671826818269182701827118272182731827418275182761827718278182791828018281182821828318284182851828618287182881828918290182911829218293182941829518296182971829818299183001830118302183031830418305183061830718308183091831018311183121831318314183151831618317183181831918320183211832218323183241832518326183271832818329183301833118332183331833418335183361833718338183391834018341183421834318344183451834618347183481834918350183511835218353183541835518356183571835818359183601836118362183631836418365183661836718368183691837018371183721837318374183751837618377183781837918380183811838218383183841838518386183871838818389183901839118392183931839418395183961839718398183991840018401184021840318404184051840618407184081840918410184111841218413184141841518416184171841818419184201842118422184231842418425184261842718428184291843018431184321843318434184351843618437184381843918440184411844218443184441844518446184471844818449184501845118452184531845418455184561845718458184591846018461184621846318464184651846618467184681846918470184711847218473184741847518476184771847818479184801848118482184831848418485184861848718488184891849018491184921849318494184951849618497184981849918500185011850218503185041850518506185071850818509185101851118512185131851418515185161851718518185191852018521185221852318524185251852618527185281852918530185311853218533185341853518536185371853818539185401854118542185431854418545185461854718548185491855018551185521855318554185551855618557185581855918560185611856218563185641856518566185671856818569185701857118572185731857418575185761857718578185791858018581185821858318584185851858618587185881858918590185911859218593185941859518596185971859818599186001860118602186031860418605186061860718608186091861018611186121861318614186151861618617186181861918620186211862218623186241862518626186271862818629186301863118632186331863418635186361863718638186391864018641186421864318644186451864618647186481864918650186511865218653186541865518656186571865818659186601866118662186631866418665186661866718668186691867018671186721867318674186751867618677186781867918680186811868218683186841868518686186871868818689186901869118692186931869418695186961869718698186991870018701187021870318704187051870618707187081870918710187111871218713187141871518716187171871818719187201872118722187231872418725187261872718728187291873018731187321873318734187351873618737187381873918740187411874218743187441874518746187471874818749187501875118752187531875418755187561875718758187591876018761187621876318764187651876618767187681876918770187711877218773187741877518776187771877818779187801878118782187831878418785187861878718788187891879018791187921879318794187951879618797187981879918800188011880218803188041880518806188071880818809188101881118812188131881418815188161881718818188191882018821188221882318824188251882618827188281882918830188311883218833188341883518836188371883818839188401884118842188431884418845188461884718848188491885018851188521885318854188551885618857188581885918860188611886218863188641886518866188671886818869188701887118872188731887418875188761887718878188791888018881188821888318884188851888618887188881888918890188911889218893188941889518896188971889818899189001890118902189031890418905189061890718908189091891018911189121891318914189151891618917189181891918920189211892218923189241892518926189271892818929189301893118932189331893418935189361893718938189391894018941189421894318944189451894618947189481894918950189511895218953189541895518956189571895818959189601896118962189631896418965189661896718968189691897018971189721897318974189751897618977189781897918980189811898218983189841898518986189871898818989189901899118992189931899418995189961899718998189991900019001190021900319004190051900619007190081900919010190111901219013190141901519016190171901819019190201902119022190231902419025190261902719028190291903019031190321903319034190351903619037190381903919040190411904219043190441904519046190471904819049190501905119052190531905419055190561905719058190591906019061190621906319064190651906619067190681906919070190711907219073190741907519076190771907819079190801908119082190831908419085190861908719088190891909019091190921909319094190951909619097190981909919100191011910219103191041910519106191071910819109191101911119112191131911419115191161911719118191191912019121191221912319124191251912619127191281912919130191311913219133191341913519136191371913819139191401914119142191431914419145191461914719148191491915019151191521915319154191551915619157191581915919160191611916219163191641916519166191671916819169191701917119172191731917419175191761917719178191791918019181191821918319184191851918619187191881918919190191911919219193191941919519196191971919819199192001920119202192031920419205192061920719208192091921019211192121921319214192151921619217192181921919220192211922219223192241922519226192271922819229192301923119232192331923419235192361923719238192391924019241192421924319244192451924619247192481924919250192511925219253192541925519256192571925819259192601926119262192631926419265192661926719268192691927019271192721927319274192751927619277192781927919280192811928219283192841928519286192871928819289192901929119292192931929419295192961929719298192991930019301193021930319304193051930619307193081930919310193111931219313193141931519316193171931819319193201932119322193231932419325193261932719328193291933019331193321933319334193351933619337193381933919340193411934219343193441934519346193471934819349193501935119352193531935419355193561935719358193591936019361193621936319364193651936619367193681936919370193711937219373193741937519376193771937819379193801938119382193831938419385193861938719388193891939019391193921939319394193951939619397193981939919400194011940219403194041940519406194071940819409194101941119412194131941419415194161941719418194191942019421194221942319424194251942619427194281942919430194311943219433194341943519436194371943819439194401944119442194431944419445194461944719448194491945019451194521945319454194551945619457194581945919460194611946219463194641946519466194671946819469194701947119472194731947419475194761947719478194791948019481194821948319484194851948619487194881948919490194911949219493194941949519496194971949819499195001950119502195031950419505195061950719508195091951019511195121951319514195151951619517195181951919520195211952219523195241952519526195271952819529195301953119532195331953419535195361953719538195391954019541195421954319544195451954619547195481954919550195511955219553195541955519556195571955819559195601956119562195631956419565195661956719568195691957019571195721957319574195751957619577195781957919580195811958219583195841958519586195871958819589195901959119592195931959419595195961959719598195991960019601196021960319604196051960619607196081960919610196111961219613196141961519616196171961819619196201962119622196231962419625196261962719628196291963019631196321963319634196351963619637196381963919640196411964219643196441964519646196471964819649196501965119652196531965419655196561965719658196591966019661196621966319664196651966619667196681966919670196711967219673196741967519676196771967819679196801968119682196831968419685196861968719688196891969019691196921969319694196951969619697196981969919700197011970219703197041970519706197071970819709197101971119712197131971419715197161971719718197191972019721197221972319724197251972619727197281972919730197311973219733197341973519736197371973819739197401974119742197431974419745197461974719748197491975019751197521975319754197551975619757197581975919760197611976219763197641976519766197671976819769197701977119772197731977419775197761977719778197791978019781197821978319784197851978619787197881978919790197911979219793197941979519796197971979819799198001980119802198031980419805198061980719808198091981019811198121981319814198151981619817198181981919820198211982219823198241982519826198271982819829198301983119832198331983419835198361983719838198391984019841198421984319844198451984619847198481984919850198511985219853198541985519856198571985819859198601986119862198631986419865198661986719868198691987019871198721987319874198751987619877198781987919880198811988219883198841988519886198871988819889198901989119892198931989419895198961989719898198991990019901199021990319904199051990619907199081990919910199111991219913199141991519916199171991819919199201992119922199231992419925199261992719928199291993019931199321993319934199351993619937199381993919940199411994219943199441994519946199471994819949199501995119952199531995419955199561995719958199591996019961199621996319964199651996619967199681996919970199711997219973199741997519976199771997819979199801998119982199831998419985199861998719988199891999019991199921999319994199951999619997199981999920000200012000220003200042000520006200072000820009200102001120012200132001420015200162001720018200192002020021200222002320024200252002620027200282002920030200312003220033200342003520036200372003820039200402004120042200432004420045200462004720048200492005020051200522005320054200552005620057200582005920060200612006220063200642006520066200672006820069200702007120072200732007420075200762007720078200792008020081200822008320084200852008620087200882008920090200912009220093200942009520096200972009820099201002010120102201032010420105201062010720108201092011020111201122011320114201152011620117201182011920120201212012220123201242012520126201272012820129201302013120132201332013420135201362013720138201392014020141201422014320144201452014620147201482014920150201512015220153201542015520156201572015820159201602016120162201632016420165201662016720168201692017020171201722017320174201752017620177201782017920180201812018220183201842018520186201872018820189201902019120192201932019420195201962019720198201992020020201202022020320204202052020620207202082020920210202112021220213202142021520216202172021820219202202022120222202232022420225202262022720228202292023020231202322023320234202352023620237202382023920240202412024220243202442024520246202472024820249202502025120252202532025420255202562025720258202592026020261202622026320264202652026620267202682026920270202712027220273202742027520276202772027820279202802028120282202832028420285202862028720288202892029020291202922029320294202952029620297202982029920300203012030220303203042030520306203072030820309203102031120312203132031420315203162031720318203192032020321203222032320324203252032620327203282032920330203312033220333203342033520336203372033820339203402034120342203432034420345203462034720348203492035020351203522035320354203552035620357203582035920360203612036220363203642036520366203672036820369203702037120372203732037420375203762037720378203792038020381203822038320384203852038620387203882038920390203912039220393203942039520396203972039820399204002040120402204032040420405204062040720408204092041020411204122041320414204152041620417204182041920420204212042220423204242042520426204272042820429204302043120432204332043420435204362043720438204392044020441204422044320444204452044620447204482044920450204512045220453204542045520456204572045820459204602046120462204632046420465204662046720468204692047020471204722047320474204752047620477204782047920480204812048220483204842048520486204872048820489204902049120492204932049420495204962049720498204992050020501205022050320504205052050620507205082050920510205112051220513205142051520516205172051820519205202052120522205232052420525205262052720528205292053020531205322053320534205352053620537205382053920540205412054220543205442054520546205472054820549205502055120552205532055420555205562055720558205592056020561205622056320564205652056620567205682056920570205712057220573205742057520576205772057820579205802058120582205832058420585205862058720588205892059020591205922059320594205952059620597205982059920600206012060220603206042060520606206072060820609206102061120612206132061420615206162061720618206192062020621206222062320624206252062620627206282062920630206312063220633206342063520636206372063820639206402064120642206432064420645206462064720648206492065020651206522065320654206552065620657206582065920660206612066220663206642066520666206672066820669206702067120672206732067420675206762067720678206792068020681206822068320684206852068620687206882068920690206912069220693206942069520696206972069820699207002070120702207032070420705207062070720708207092071020711207122071320714207152071620717207182071920720207212072220723207242072520726207272072820729207302073120732207332073420735207362073720738207392074020741207422074320744207452074620747207482074920750207512075220753207542075520756207572075820759207602076120762207632076420765207662076720768207692077020771207722077320774207752077620777207782077920780207812078220783207842078520786207872078820789207902079120792207932079420795207962079720798207992080020801208022080320804208052080620807208082080920810208112081220813208142081520816208172081820819208202082120822208232082420825208262082720828208292083020831208322083320834208352083620837208382083920840208412084220843208442084520846208472084820849208502085120852208532085420855208562085720858208592086020861208622086320864208652086620867208682086920870208712087220873208742087520876208772087820879208802088120882208832088420885208862088720888208892089020891208922089320894208952089620897208982089920900209012090220903209042090520906209072090820909209102091120912209132091420915209162091720918209192092020921209222092320924209252092620927209282092920930209312093220933209342093520936209372093820939209402094120942209432094420945209462094720948209492095020951209522095320954209552095620957209582095920960209612096220963209642096520966209672096820969209702097120972209732097420975209762097720978209792098020981209822098320984209852098620987209882098920990209912099220993209942099520996209972099820999210002100121002210032100421005210062100721008210092101021011210122101321014210152101621017210182101921020210212102221023210242102521026
  1. ;;; org.el --- Outline-based notes management and organizer
  2. ;; Carstens outline-mode for keeping track of everything.
  3. ;; Copyright (C) 2004-2011 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. ;; Version: 7.7
  10. ;;
  11. ;; This file is part of GNU Emacs.
  12. ;;
  13. ;; GNU Emacs is free software: you can redistribute it and/or modify
  14. ;; it under the terms of the GNU General Public License as published by
  15. ;; the Free Software Foundation, either version 3 of the License, or
  16. ;; (at your option) any later version.
  17. ;; GNU Emacs is distributed in the hope that it will be useful,
  18. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. ;; GNU General Public License for more details.
  21. ;; You should have received a copy of the GNU General Public License
  22. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  23. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  24. ;;
  25. ;;; Commentary:
  26. ;;
  27. ;; Org-mode is a mode for keeping notes, maintaining ToDo lists, and doing
  28. ;; project planning with a fast and effective plain-text system.
  29. ;;
  30. ;; Org-mode develops organizational tasks around NOTES files that contain
  31. ;; information about projects as plain text. Org-mode is implemented on
  32. ;; top of outline-mode, which makes it possible to keep the content of
  33. ;; large files well structured. Visibility cycling and structure editing
  34. ;; help to work with the tree. Tables are easily created with a built-in
  35. ;; table editor. Org-mode supports ToDo items, deadlines, time stamps,
  36. ;; and scheduling. It dynamically compiles entries into an agenda that
  37. ;; utilizes and smoothly integrates much of the Emacs calendar and diary.
  38. ;; Plain text URL-like links connect to websites, emails, Usenet
  39. ;; messages, BBDB entries, and any files related to the projects. For
  40. ;; printing and sharing of notes, an Org-mode file can be exported as a
  41. ;; structured ASCII file, as HTML, or (todo and agenda items only) as an
  42. ;; iCalendar file. It can also serve as a publishing tool for a set of
  43. ;; linked webpages.
  44. ;;
  45. ;; Installation and Activation
  46. ;; ---------------------------
  47. ;; See the corresponding sections in the manual at
  48. ;;
  49. ;; http://orgmode.org/org.html#Installation
  50. ;;
  51. ;; Documentation
  52. ;; -------------
  53. ;; The documentation of Org-mode can be found in the TeXInfo file. The
  54. ;; distribution also contains a PDF version of it. At the homepage of
  55. ;; Org-mode, you can read the same text online as HTML. There is also an
  56. ;; excellent reference card made by Philip Rooke. This card can be found
  57. ;; in the etc/ directory of Emacs 22.
  58. ;;
  59. ;; A list of recent changes can be found at
  60. ;; http://orgmode.org/Changes.html
  61. ;;
  62. ;;; Code:
  63. (defvar org-inhibit-highlight-removal nil) ; dynamically scoped param
  64. (defvar org-table-formula-constants-local nil
  65. "Local version of `org-table-formula-constants'.")
  66. (make-variable-buffer-local 'org-table-formula-constants-local)
  67. ;;;; Require other packages
  68. (eval-when-compile
  69. (require 'cl)
  70. (require 'gnus-sum))
  71. (require 'calendar)
  72. ;; Emacs 22 calendar compatibility: Make sure the new variables are available
  73. (when (fboundp 'defvaralias)
  74. (unless (boundp 'calendar-view-holidays-initially-flag)
  75. (defvaralias 'calendar-view-holidays-initially-flag
  76. 'view-calendar-holidays-initially))
  77. (unless (boundp 'calendar-view-diary-initially-flag)
  78. (defvaralias 'calendar-view-diary-initially-flag
  79. 'view-diary-entries-initially))
  80. (unless (boundp 'diary-fancy-buffer)
  81. (defvaralias 'diary-fancy-buffer 'fancy-diary-buffer)))
  82. (require 'outline) (require 'noutline)
  83. ;; Other stuff we need.
  84. (require 'time-date)
  85. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  86. (require 'easymenu)
  87. (require 'overlay)
  88. (require 'org-macs)
  89. (require 'org-entities)
  90. (require 'org-compat)
  91. (require 'org-faces)
  92. (require 'org-list)
  93. (require 'org-pcomplete)
  94. (require 'org-src)
  95. (require 'org-footnote)
  96. (declare-function org-inlinetask-at-task-p "org-inlinetask" ())
  97. (declare-function org-inlinetask-outline-regexp "org-inlinetask" ())
  98. (declare-function org-inlinetask-toggle-visibility "org-inlinetask" ())
  99. (declare-function org-pop-to-buffer-same-window "org-compat" (&optional buffer-or-name norecord label))
  100. (declare-function org-at-clock-log-p "org-clock" ())
  101. (declare-function org-clock-timestamps-up "org-clock" ())
  102. (declare-function org-clock-timestamps-down "org-clock" ())
  103. ;; babel
  104. (require 'ob)
  105. (require 'ob-table)
  106. (require 'ob-lob)
  107. (require 'ob-ref)
  108. (require 'ob-tangle)
  109. (require 'ob-comint)
  110. (require 'ob-keys)
  111. ;; load languages based on value of `org-babel-load-languages'
  112. (defvar org-babel-load-languages)
  113. ;;;###autoload
  114. (defun org-babel-do-load-languages (sym value)
  115. "Load the languages defined in `org-babel-load-languages'."
  116. (set-default sym value)
  117. (mapc (lambda (pair)
  118. (let ((active (cdr pair)) (lang (symbol-name (car pair))))
  119. (if active
  120. (progn
  121. (require (intern (concat "ob-" lang))))
  122. (progn
  123. (funcall 'fmakunbound
  124. (intern (concat "org-babel-execute:" lang)))
  125. (funcall 'fmakunbound
  126. (intern (concat "org-babel-expand-body:" lang)))))))
  127. org-babel-load-languages))
  128. (defcustom org-babel-load-languages '((emacs-lisp . t))
  129. "Languages which can be evaluated in Org-mode buffers.
  130. This list can be used to load support for any of the languages
  131. below, note that each language will depend on a different set of
  132. system executables and/or Emacs modes. When a language is
  133. \"loaded\", then code blocks in that language can be evaluated
  134. with `org-babel-execute-src-block' bound by default to C-c
  135. C-c (note the `org-babel-no-eval-on-ctrl-c-ctrl-c' variable can
  136. be set to remove code block evaluation from the C-c C-c
  137. keybinding. By default only Emacs Lisp (which has no
  138. requirements) is loaded."
  139. :group 'org-babel
  140. :set 'org-babel-do-load-languages
  141. :type '(alist :tag "Babel Languages"
  142. :key-type
  143. (choice
  144. (const :tag "Awk" awk)
  145. (const :tag "C" C)
  146. (const :tag "R" R)
  147. (const :tag "Asymptote" asymptote)
  148. (const :tag "Calc" calc)
  149. (const :tag "Clojure" clojure)
  150. (const :tag "CSS" css)
  151. (const :tag "Ditaa" ditaa)
  152. (const :tag "Dot" dot)
  153. (const :tag "Emacs Lisp" emacs-lisp)
  154. (const :tag "Fortran" fortran)
  155. (const :tag "Gnuplot" gnuplot)
  156. (const :tag "Haskell" haskell)
  157. (const :tag "Java" java)
  158. (const :tag "Javascript" js)
  159. (const :tag "Latex" latex)
  160. (const :tag "Ledger" ledger)
  161. (const :tag "Lilypond" lilypond)
  162. (const :tag "Maxima" maxima)
  163. (const :tag "Matlab" matlab)
  164. (const :tag "Mscgen" mscgen)
  165. (const :tag "Ocaml" ocaml)
  166. (const :tag "Octave" octave)
  167. (const :tag "Org" org)
  168. (const :tag "Perl" perl)
  169. (const :tag "PlantUML" plantuml)
  170. (const :tag "Python" python)
  171. (const :tag "Ruby" ruby)
  172. (const :tag "Sass" sass)
  173. (const :tag "Scheme" scheme)
  174. (const :tag "Screen" screen)
  175. (const :tag "Shell Script" sh)
  176. (const :tag "Shen" shen)
  177. (const :tag "Sql" sql)
  178. (const :tag "Sqlite" sqlite))
  179. :value-type (boolean :tag "Activate" :value t)))
  180. ;;;; Customization variables
  181. (defcustom org-clone-delete-id nil
  182. "Remove ID property of clones of a subtree.
  183. When non-nil, clones of a subtree don't inherit the ID property.
  184. Otherwise they inherit the ID property with a new unique
  185. identifier."
  186. :type 'boolean
  187. :group 'org-id)
  188. ;;; Version
  189. (defconst org-version "7.7"
  190. "The version number of the file org.el.")
  191. (defun org-version (&optional here)
  192. "Show the org-mode version in the echo area.
  193. With prefix arg HERE, insert it at point."
  194. (interactive "P")
  195. (let* ((origin default-directory)
  196. (version org-version)
  197. (git-version)
  198. (dir (concat (file-name-directory (locate-library "org")) "../" )))
  199. (when (and (file-exists-p (expand-file-name ".git" dir))
  200. (executable-find "git"))
  201. (unwind-protect
  202. (progn
  203. (cd dir)
  204. (when (eql 0 (shell-command "git describe --abbrev=4 HEAD"))
  205. (with-current-buffer "*Shell Command Output*"
  206. (goto-char (point-min))
  207. (setq git-version (buffer-substring (point) (point-at-eol))))
  208. (subst-char-in-string ?- ?. git-version t)
  209. (when (string-match "\\S-"
  210. (shell-command-to-string
  211. "git diff-index --name-only HEAD --"))
  212. (setq git-version (concat git-version ".dirty")))
  213. (setq version (concat version " (" git-version ")"))))
  214. (cd origin)))
  215. (setq version (format "Org-mode version %s" version))
  216. (if here (insert version))
  217. (message version)))
  218. ;;; Compatibility constants
  219. ;;; The custom variables
  220. (defgroup org nil
  221. "Outline-based notes management and organizer."
  222. :tag "Org"
  223. :group 'outlines
  224. :group 'calendar)
  225. (defcustom org-mode-hook nil
  226. "Mode hook for Org-mode, run after the mode was turned on."
  227. :group 'org
  228. :type 'hook)
  229. (defcustom org-load-hook nil
  230. "Hook that is run after org.el has been loaded."
  231. :group 'org
  232. :type 'hook)
  233. (defcustom org-log-buffer-setup-hook nil
  234. "Hook that is run after an Org log buffer is created."
  235. :group 'org
  236. :type 'hook)
  237. (defvar org-modules) ; defined below
  238. (defvar org-modules-loaded nil
  239. "Have the modules been loaded already?")
  240. (defun org-load-modules-maybe (&optional force)
  241. "Load all extensions listed in `org-modules'."
  242. (when (or force (not org-modules-loaded))
  243. (mapc (lambda (ext)
  244. (condition-case nil (require ext)
  245. (error (message "Problems while trying to load feature `%s'" ext))))
  246. org-modules)
  247. (setq org-modules-loaded t)))
  248. (defun org-set-modules (var value)
  249. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  250. (set var value)
  251. (when (featurep 'org)
  252. (org-load-modules-maybe 'force)))
  253. (when (org-bound-and-true-p org-modules)
  254. (let ((a (member 'org-infojs org-modules)))
  255. (and a (setcar a 'org-jsinfo))))
  256. (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)
  257. "Modules that should always be loaded together with org.el.
  258. If a description starts with <C>, the file is not part of Emacs
  259. and loading it will require that you have downloaded and properly installed
  260. the org-mode distribution.
  261. You can also use this system to load external packages (i.e. neither Org
  262. core modules, nor modules from the CONTRIB directory). Just add symbols
  263. to the end of the list. If the package is called org-xyz.el, then you need
  264. to add the symbol `xyz', and the package must have a call to
  265. (provide 'org-xyz)"
  266. :group 'org
  267. :set 'org-set-modules
  268. :type
  269. '(set :greedy t
  270. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  271. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  272. (const :tag " crypt: Encryption of subtrees" org-crypt)
  273. (const :tag " ctags: Access to Emacs tags with links" org-ctags)
  274. (const :tag " docview: Links to doc-view buffers" org-docview)
  275. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  276. (const :tag " id: Global IDs for identifying entries" org-id)
  277. (const :tag " info: Links to Info nodes" org-info)
  278. (const :tag " jsinfo: Set up Sebastian Rose's JavaScript org-info.js" org-jsinfo)
  279. (const :tag " habit: Track your consistency with habits" org-habit)
  280. (const :tag " inlinetask: Tasks independent of outline hierarchy" org-inlinetask)
  281. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  282. (const :tag " mac-message: Links to messages in Apple Mail" org-mac-message)
  283. (const :tag " mew Links to Mew folders/messages" org-mew)
  284. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  285. (const :tag " protocol: Intercept calls from emacsclient" org-protocol)
  286. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  287. (const :tag " special-blocks: Turn blocks into LaTeX envs and HTML divs" org-special-blocks)
  288. (const :tag " vm: Links to VM folders/messages" org-vm)
  289. (const :tag " wl: Links to Wanderlust folders/messages" org-wl)
  290. (const :tag " w3m: Special cut/paste from w3m to Org-mode." org-w3m)
  291. (const :tag " mouse: Additional mouse support" org-mouse)
  292. (const :tag " TaskJuggler: Export tasks to a TaskJuggler project" org-taskjuggler)
  293. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  294. (const :tag "C bookmark: Org-mode links to bookmarks" org-bookmark)
  295. (const :tag "C checklist: Extra functions for checklists in repeated tasks" org-checklist)
  296. (const :tag "C choose: Use TODO keywords to mark decisions states" org-choose)
  297. (const :tag "C collector: Collect properties into tables" org-collector)
  298. (const :tag "C depend: TODO dependencies for Org-mode\n\t\t\t(PARTIALLY OBSOLETE, see built-in dependency support))" org-depend)
  299. (const :tag "C drill: Flashcards and spaced repetition for Org-mode" org-drill)
  300. (const :tag "C elisp-symbol: Org-mode links to emacs-lisp symbols" org-elisp-symbol)
  301. (const :tag "C eshell Support for links to working directories in eshell" org-eshell)
  302. (const :tag "C eval: Include command output as text" org-eval)
  303. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  304. (const :tag "C expiry: Expiry mechanism for Org-mode entries" org-expiry)
  305. (const :tag "C exp-bibtex: Export citations using BibTeX" org-exp-bibtex)
  306. (const :tag "C git-link: Provide org links to specific file version" org-git-link)
  307. (const :tag "C interactive-query: Interactive modification of tags query\n\t\t\t(PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query)
  308. (const :tag "C invoice: Help manage client invoices in Org-mode" org-invoice)
  309. (const :tag "C jira: Add a jira:ticket protocol to Org-mode" org-jira)
  310. (const :tag "C learn: SuperMemo's incremental learning algorithm" org-learn)
  311. (const :tag "C mairix: Hook mairix search into Org-mode for different MUAs" org-mairix)
  312. (const :tag "C notmuch: Provide org links to notmuch searches or messages" org-notmuch)
  313. (const :tag "C mac-iCal Imports events from iCal.app to the Emacs diary" org-mac-iCal)
  314. (const :tag "C mac-link-grabber Grab links and URLs from various Mac applications" org-mac-link-grabber)
  315. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  316. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  317. (const :tag "C odt: OpenDocumentText exporter for Org-mode" org-odt)
  318. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  319. (const :tag "C registry: A registry for Org-mode links" org-registry)
  320. (const :tag "C org2rem: Convert org appointments into reminders" org2rem)
  321. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  322. (const :tag "C secretary: Team management with org-mode" org-secretary)
  323. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  324. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  325. (const :tag "C track: Keep up with Org-mode development" org-track)
  326. (const :tag "C velocity Something like Notational Velocity for Org" org-velocity)
  327. (const :tag "C wikinodes: CamelCase wiki-like links" org-wikinodes)
  328. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  329. (defcustom org-support-shift-select nil
  330. "Non-nil means make shift-cursor commands select text when possible.
  331. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys
  332. start selecting a region, or enlarge regions started in this way.
  333. In Org-mode, in special contexts, these same keys are used for
  334. other purposes, important enough to compete with shift selection.
  335. Org tries to balance these needs by supporting `shift-select-mode'
  336. outside these special contexts, under control of this variable.
  337. The default of this variable is nil, to avoid confusing behavior. Shifted
  338. cursor keys will then execute Org commands in the following contexts:
  339. - on a headline, changing TODO state (left/right) and priority (up/down)
  340. - on a time stamp, changing the time
  341. - in a plain list item, changing the bullet type
  342. - in a property definition line, switching between allowed values
  343. - in the BEGIN line of a clock table (changing the time block).
  344. Outside these contexts, the commands will throw an error.
  345. When this variable is t and the cursor is not in a special
  346. context, Org-mode will support shift-selection for making and
  347. enlarging regions. To make this more effective, the bullet
  348. cycling will no longer happen anywhere in an item line, but only
  349. if the cursor is exactly on the bullet.
  350. If you set this variable to the symbol `always', then the keys
  351. will not be special in headlines, property lines, and item lines,
  352. to make shift selection work there as well. If this is what you
  353. want, you can use the following alternative commands: `C-c C-t'
  354. and `C-c ,' to change TODO state and priority, `C-u C-u C-c C-t'
  355. can be used to switch TODO sets, `C-c -' to cycle item bullet
  356. types, and properties can be edited by hand or in column view.
  357. However, when the cursor is on a timestamp, shift-cursor commands
  358. will still edit the time stamp - this is just too good to give up.
  359. XEmacs user should have this variable set to nil, because
  360. `shift-select-mode' is in Emacs 23 or later only."
  361. :group 'org
  362. :type '(choice
  363. (const :tag "Never" nil)
  364. (const :tag "When outside special context" t)
  365. (const :tag "Everywhere except timestamps" always)))
  366. (defcustom org-loop-over-headlines-in-active-region nil
  367. "Shall some commands act upon headlines in the active region?
  368. When set to `t', some commands will be performed in all headlines
  369. within the active region.
  370. When set to a string, those commands will be performed on the
  371. matching headlines within the active region. Such string must be
  372. a tags/property/todo match as it is used in the agenda tags view.
  373. The list of commands is:
  374. - `org-schedule'
  375. - `org-deadline'"
  376. :type '(choice (const :tag "Don't loop" nil)
  377. (const :tag "All headlines in active region" t)
  378. (string :tag "Tags/Property/Todo matcher"))
  379. :group 'org-todo
  380. :group 'org-archive)
  381. (defgroup org-startup nil
  382. "Options concerning startup of Org-mode."
  383. :tag "Org Startup"
  384. :group 'org)
  385. (defcustom org-startup-folded t
  386. "Non-nil means entering Org-mode will switch to OVERVIEW.
  387. This can also be configured on a per-file basis by adding one of
  388. the following lines anywhere in the buffer:
  389. #+STARTUP: fold (or `overview', this is equivalent)
  390. #+STARTUP: nofold (or `showall', this is equivalent)
  391. #+STARTUP: content
  392. #+STARTUP: showeverything"
  393. :group 'org-startup
  394. :type '(choice
  395. (const :tag "nofold: show all" nil)
  396. (const :tag "fold: overview" t)
  397. (const :tag "content: all headlines" content)
  398. (const :tag "show everything, even drawers" showeverything)))
  399. (defcustom org-startup-truncated t
  400. "Non-nil means entering Org-mode will set `truncate-lines'.
  401. This is useful since some lines containing links can be very long and
  402. uninteresting. Also tables look terrible when wrapped."
  403. :group 'org-startup
  404. :type 'boolean)
  405. (defcustom org-startup-indented nil
  406. "Non-nil means turn on `org-indent-mode' on startup.
  407. This can also be configured on a per-file basis by adding one of
  408. the following lines anywhere in the buffer:
  409. #+STARTUP: indent
  410. #+STARTUP: noindent"
  411. :group 'org-structure
  412. :type '(choice
  413. (const :tag "Not" nil)
  414. (const :tag "Globally (slow on startup in large files)" t)))
  415. (defcustom org-use-sub-superscripts t
  416. "Non-nil means interpret \"_\" and \"^\" for export.
  417. When this option is turned on, you can use TeX-like syntax for sub- and
  418. superscripts. Several characters after \"_\" or \"^\" will be
  419. considered as a single item - so grouping with {} is normally not
  420. needed. For example, the following things will be parsed as single
  421. sub- or superscripts.
  422. 10^24 or 10^tau several digits will be considered 1 item.
  423. 10^-12 or 10^-tau a leading sign with digits or a word
  424. x^2-y^3 will be read as x^2 - y^3, because items are
  425. terminated by almost any nonword/nondigit char.
  426. x_{i^2} or x^(2-i) braces or parenthesis do grouping.
  427. Still, ambiguity is possible - so when in doubt use {} to enclose the
  428. sub/superscript. If you set this variable to the symbol `{}',
  429. the braces are *required* in order to trigger interpretations as
  430. sub/superscript. This can be helpful in documents that need \"_\"
  431. frequently in plain text.
  432. Not all export backends support this, but HTML does.
  433. This option can also be set with the +OPTIONS line, e.g. \"^:nil\"."
  434. :group 'org-startup
  435. :group 'org-export-translation
  436. :type '(choice
  437. (const :tag "Always interpret" t)
  438. (const :tag "Only with braces" {})
  439. (const :tag "Never interpret" nil)))
  440. (if (fboundp 'defvaralias)
  441. (defvaralias 'org-export-with-sub-superscripts 'org-use-sub-superscripts))
  442. (defcustom org-startup-with-beamer-mode nil
  443. "Non-nil means turn on `org-beamer-mode' on startup.
  444. This can also be configured on a per-file basis by adding one of
  445. the following lines anywhere in the buffer:
  446. #+STARTUP: beamer"
  447. :group 'org-startup
  448. :type 'boolean)
  449. (defcustom org-startup-align-all-tables nil
  450. "Non-nil means align all tables when visiting a file.
  451. This is useful when the column width in tables is forced with <N> cookies
  452. in table fields. Such tables will look correct only after the first re-align.
  453. This can also be configured on a per-file basis by adding one of
  454. the following lines anywhere in the buffer:
  455. #+STARTUP: align
  456. #+STARTUP: noalign"
  457. :group 'org-startup
  458. :type 'boolean)
  459. (defcustom org-startup-with-inline-images nil
  460. "Non-nil means show inline images when loading a new Org file.
  461. This can also be configured on a per-file basis by adding one of
  462. the following lines anywhere in the buffer:
  463. #+STARTUP: inlineimages
  464. #+STARTUP: noinlineimages"
  465. :group 'org-startup
  466. :type 'boolean)
  467. (defcustom org-insert-mode-line-in-empty-file nil
  468. "Non-nil means insert the first line setting Org-mode in empty files.
  469. When the function `org-mode' is called interactively in an empty file, this
  470. normally means that the file name does not automatically trigger Org-mode.
  471. To ensure that the file will always be in Org-mode in the future, a
  472. line enforcing Org-mode will be inserted into the buffer, if this option
  473. has been set."
  474. :group 'org-startup
  475. :type 'boolean)
  476. (defcustom org-replace-disputed-keys nil
  477. "Non-nil means use alternative key bindings for some keys.
  478. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  479. These keys are also used by other packages like shift-selection-mode'
  480. \(built into Emacs 23), `CUA-mode' or `windmove.el'.
  481. If you want to use Org-mode together with one of these other modes,
  482. or more generally if you would like to move some Org-mode commands to
  483. other keys, set this variable and configure the keys with the variable
  484. `org-disputed-keys'.
  485. This option is only relevant at load-time of Org-mode, and must be set
  486. *before* org.el is loaded. Changing it requires a restart of Emacs to
  487. become effective."
  488. :group 'org-startup
  489. :type 'boolean)
  490. (defcustom org-use-extra-keys nil
  491. "Non-nil means use extra key sequence definitions for certain commands.
  492. This happens automatically if you run XEmacs or if `window-system'
  493. is nil. This variable lets you do the same manually. You must
  494. set it before loading org.
  495. Example: on Carbon Emacs 22 running graphically, with an external
  496. keyboard on a Powerbook, the default way of setting M-left might
  497. not work for either Alt or ESC. Setting this variable will make
  498. it work for ESC."
  499. :group 'org-startup
  500. :type 'boolean)
  501. (if (fboundp 'defvaralias)
  502. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  503. (defcustom org-disputed-keys
  504. '(([(shift up)] . [(meta p)])
  505. ([(shift down)] . [(meta n)])
  506. ([(shift left)] . [(meta -)])
  507. ([(shift right)] . [(meta +)])
  508. ([(control shift right)] . [(meta shift +)])
  509. ([(control shift left)] . [(meta shift -)]))
  510. "Keys for which Org-mode and other modes compete.
  511. This is an alist, cars are the default keys, second element specifies
  512. the alternative to use when `org-replace-disputed-keys' is t.
  513. Keys can be specified in any syntax supported by `define-key'.
  514. The value of this option takes effect only at Org-mode's startup,
  515. therefore you'll have to restart Emacs to apply it after changing."
  516. :group 'org-startup
  517. :type 'alist)
  518. (defun org-key (key)
  519. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  520. Or return the original if not disputed.
  521. Also apply the translations defined in `org-xemacs-key-equivalents'."
  522. (when org-replace-disputed-keys
  523. (let* ((nkey (key-description key))
  524. (x (org-find-if (lambda (x)
  525. (equal (key-description (car x)) nkey))
  526. org-disputed-keys)))
  527. (setq key (if x (cdr x) key))))
  528. (when (featurep 'xemacs)
  529. (setq key (or (cdr (assoc key org-xemacs-key-equivalents)) key)))
  530. key)
  531. (defun org-find-if (predicate seq)
  532. (catch 'exit
  533. (while seq
  534. (if (funcall predicate (car seq))
  535. (throw 'exit (car seq))
  536. (pop seq)))))
  537. (defun org-defkey (keymap key def)
  538. "Define a key, possibly translated, as returned by `org-key'."
  539. (define-key keymap (org-key key) def))
  540. (defcustom org-ellipsis nil
  541. "The ellipsis to use in the Org-mode outline.
  542. When nil, just use the standard three dots. When a string, use that instead,
  543. When a face, use the standard 3 dots, but with the specified face.
  544. The change affects only Org-mode (which will then use its own display table).
  545. Changing this requires executing `M-x org-mode' in a buffer to become
  546. effective."
  547. :group 'org-startup
  548. :type '(choice (const :tag "Default" nil)
  549. (face :tag "Face" :value org-warning)
  550. (string :tag "String" :value "...#")))
  551. (defvar org-display-table nil
  552. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  553. (defgroup org-keywords nil
  554. "Keywords in Org-mode."
  555. :tag "Org Keywords"
  556. :group 'org)
  557. (defcustom org-deadline-string "DEADLINE:"
  558. "String to mark deadline entries.
  559. A deadline is this string, followed by a time stamp. Should be a word,
  560. terminated by a colon. You can insert a schedule keyword and
  561. a timestamp with \\[org-deadline].
  562. Changes become only effective after restarting Emacs."
  563. :group 'org-keywords
  564. :type 'string)
  565. (defcustom org-scheduled-string "SCHEDULED:"
  566. "String to mark scheduled TODO entries.
  567. A schedule is this string, followed by a time stamp. Should be a word,
  568. terminated by a colon. You can insert a schedule keyword and
  569. a timestamp with \\[org-schedule].
  570. Changes become only effective after restarting Emacs."
  571. :group 'org-keywords
  572. :type 'string)
  573. (defcustom org-closed-string "CLOSED:"
  574. "String used as the prefix for timestamps logging closing a TODO entry."
  575. :group 'org-keywords
  576. :type 'string)
  577. (defcustom org-clock-string "CLOCK:"
  578. "String used as prefix for timestamps clocking work hours on an item."
  579. :group 'org-keywords
  580. :type 'string)
  581. (defcustom org-comment-string "COMMENT"
  582. "Entries starting with this keyword will never be exported.
  583. An entry can be toggled between COMMENT and normal with
  584. \\[org-toggle-comment].
  585. Changes become only effective after restarting Emacs."
  586. :group 'org-keywords
  587. :type 'string)
  588. (defcustom org-quote-string "QUOTE"
  589. "Entries starting with this keyword will be exported in fixed-width font.
  590. Quoting applies only to the text in the entry following the headline, and does
  591. not extend beyond the next headline, even if that is lower level.
  592. An entry can be toggled between QUOTE and normal with
  593. \\[org-toggle-fixed-width-section]."
  594. :group 'org-keywords
  595. :type 'string)
  596. (defconst org-repeat-re
  597. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*?\\([.+]?\\+[0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)"
  598. "Regular expression for specifying repeated events.
  599. After a match, group 1 contains the repeat expression.")
  600. (defgroup org-structure nil
  601. "Options concerning the general structure of Org-mode files."
  602. :tag "Org Structure"
  603. :group 'org)
  604. (defgroup org-reveal-location nil
  605. "Options about how to make context of a location visible."
  606. :tag "Org Reveal Location"
  607. :group 'org-structure)
  608. (defconst org-context-choice
  609. '(choice
  610. (const :tag "Always" t)
  611. (const :tag "Never" nil)
  612. (repeat :greedy t :tag "Individual contexts"
  613. (cons
  614. (choice :tag "Context"
  615. (const agenda)
  616. (const org-goto)
  617. (const occur-tree)
  618. (const tags-tree)
  619. (const link-search)
  620. (const mark-goto)
  621. (const bookmark-jump)
  622. (const isearch)
  623. (const default))
  624. (boolean))))
  625. "Contexts for the reveal options.")
  626. (defcustom org-show-hierarchy-above '((default . t))
  627. "Non-nil means show full hierarchy when revealing a location.
  628. Org-mode often shows locations in an org-mode file which might have
  629. been invisible before. When this is set, the hierarchy of headings
  630. above the exposed location is shown.
  631. Turning this off for example for sparse trees makes them very compact.
  632. Instead of t, this can also be an alist specifying this option for different
  633. contexts. Valid contexts are
  634. agenda when exposing an entry from the agenda
  635. org-goto when using the command `org-goto' on key C-c C-j
  636. occur-tree when using the command `org-occur' on key C-c /
  637. tags-tree when constructing a sparse tree based on tags matches
  638. link-search when exposing search matches associated with a link
  639. mark-goto when exposing the jump goal of a mark
  640. bookmark-jump when exposing a bookmark location
  641. isearch when exiting from an incremental search
  642. default default for all contexts not set explicitly"
  643. :group 'org-reveal-location
  644. :type org-context-choice)
  645. (defcustom org-show-following-heading '((default . nil))
  646. "Non-nil means show following heading when revealing a location.
  647. Org-mode often shows locations in an org-mode file which might have
  648. been invisible before. When this is set, the heading following the
  649. match is shown.
  650. Turning this off for example for sparse trees makes them very compact,
  651. but makes it harder to edit the location of the match. In such a case,
  652. use the command \\[org-reveal] to show more context.
  653. Instead of t, this can also be an alist specifying this option for different
  654. contexts. See `org-show-hierarchy-above' for valid contexts."
  655. :group 'org-reveal-location
  656. :type org-context-choice)
  657. (defcustom org-show-siblings '((default . nil) (isearch t))
  658. "Non-nil means show all sibling heading when revealing a location.
  659. Org-mode often shows locations in an org-mode file which might have
  660. been invisible before. When this is set, the sibling of the current entry
  661. heading are all made visible. If `org-show-hierarchy-above' is t,
  662. the same happens on each level of the hierarchy above the current entry.
  663. By default this is on for the isearch context, off for all other contexts.
  664. Turning this off for example for sparse trees makes them very compact,
  665. but makes it harder to edit the location of the match. In such a case,
  666. use the command \\[org-reveal] to show more context.
  667. Instead of t, this can also be an alist specifying this option for different
  668. contexts. See `org-show-hierarchy-above' for valid contexts."
  669. :group 'org-reveal-location
  670. :type org-context-choice)
  671. (defcustom org-show-entry-below '((default . nil))
  672. "Non-nil means show the entry below a headline when revealing a location.
  673. Org-mode often shows locations in an org-mode file which might have
  674. been invisible before. When this is set, the text below the headline that is
  675. exposed is also shown.
  676. By default this is off for all contexts.
  677. Instead of t, this can also be an alist specifying this option for different
  678. contexts. See `org-show-hierarchy-above' for valid contexts."
  679. :group 'org-reveal-location
  680. :type org-context-choice)
  681. (defcustom org-indirect-buffer-display 'other-window
  682. "How should indirect tree buffers be displayed?
  683. This applies to indirect buffers created with the commands
  684. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  685. Valid values are:
  686. current-window Display in the current window
  687. other-window Just display in another window.
  688. dedicated-frame Create one new frame, and re-use it each time.
  689. new-frame Make a new frame each time. Note that in this case
  690. previously-made indirect buffers are kept, and you need to
  691. kill these buffers yourself."
  692. :group 'org-structure
  693. :group 'org-agenda-windows
  694. :type '(choice
  695. (const :tag "In current window" current-window)
  696. (const :tag "In current frame, other window" other-window)
  697. (const :tag "Each time a new frame" new-frame)
  698. (const :tag "One dedicated frame" dedicated-frame)))
  699. (defcustom org-use-speed-commands nil
  700. "Non-nil means activate single letter commands at beginning of a headline.
  701. This may also be a function to test for appropriate locations where speed
  702. commands should be active."
  703. :group 'org-structure
  704. :type '(choice
  705. (const :tag "Never" nil)
  706. (const :tag "At beginning of headline stars" t)
  707. (function)))
  708. (defcustom org-speed-commands-user nil
  709. "Alist of additional speed commands.
  710. This list will be checked before `org-speed-commands-default'
  711. when the variable `org-use-speed-commands' is non-nil
  712. and when the cursor is at the beginning of a headline.
  713. The car if each entry is a string with a single letter, which must
  714. be assigned to `self-insert-command' in the global map.
  715. The cdr is either a command to be called interactively, a function
  716. to be called, or a form to be evaluated.
  717. An entry that is just a list with a single string will be interpreted
  718. as a descriptive headline that will be added when listing the speed
  719. commands in the Help buffer using the `?' speed command."
  720. :group 'org-structure
  721. :type '(repeat :value ("k" . ignore)
  722. (choice :value ("k" . ignore)
  723. (list :tag "Descriptive Headline" (string :tag "Headline"))
  724. (cons :tag "Letter and Command"
  725. (string :tag "Command letter")
  726. (choice
  727. (function)
  728. (sexp))))))
  729. (defgroup org-cycle nil
  730. "Options concerning visibility cycling in Org-mode."
  731. :tag "Org Cycle"
  732. :group 'org-structure)
  733. (defcustom org-cycle-skip-children-state-if-no-children t
  734. "Non-nil means skip CHILDREN state in entries that don't have any."
  735. :group 'org-cycle
  736. :type 'boolean)
  737. (defcustom org-cycle-max-level nil
  738. "Maximum level which should still be subject to visibility cycling.
  739. Levels higher than this will, for cycling, be treated as text, not a headline.
  740. When `org-odd-levels-only' is set, a value of N in this variable actually
  741. means 2N-1 stars as the limiting headline.
  742. When nil, cycle all levels.
  743. Note that the limiting level of cycling is also influenced by
  744. `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
  745. `org-inlinetask-min-level' is, cycling will be limited to levels one less
  746. than its value."
  747. :group 'org-cycle
  748. :type '(choice
  749. (const :tag "No limit" nil)
  750. (integer :tag "Maximum level")))
  751. (defcustom org-drawers '("PROPERTIES" "CLOCK" "LOGBOOK")
  752. "Names of drawers. Drawers are not opened by cycling on the headline above.
  753. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  754. this:
  755. :DRAWERNAME:
  756. .....
  757. :END:
  758. The drawer \"PROPERTIES\" is special for capturing properties through
  759. the property API.
  760. Drawers can be defined on the per-file basis with a line like:
  761. #+DRAWERS: HIDDEN STATE PROPERTIES"
  762. :group 'org-structure
  763. :group 'org-cycle
  764. :type '(repeat (string :tag "Drawer Name")))
  765. (defcustom org-hide-block-startup nil
  766. "Non-nil means entering Org-mode will fold all blocks.
  767. This can also be set in on a per-file basis with
  768. #+STARTUP: hideblocks
  769. #+STARTUP: showblocks"
  770. :group 'org-startup
  771. :group 'org-cycle
  772. :type 'boolean)
  773. (defcustom org-cycle-global-at-bob nil
  774. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  775. This makes it possible to do global cycling without having to use S-TAB or
  776. \\[universal-argument] TAB. For this special case to work, the first line \
  777. of the buffer
  778. must not be a headline - it may be empty or some other text. When used in
  779. this way, `org-cycle-hook' is disables temporarily, to make sure the
  780. cursor stays at the beginning of the buffer.
  781. When this option is nil, don't do anything special at the beginning
  782. of the buffer."
  783. :group 'org-cycle
  784. :type 'boolean)
  785. (defcustom org-cycle-level-after-item/entry-creation t
  786. "Non-nil means cycle entry level or item indentation in new empty entries.
  787. When the cursor is at the end of an empty headline, i.e with only stars
  788. and maybe a TODO keyword, TAB will then switch the entry to become a child,
  789. and then all possible ancestor states, before returning to the original state.
  790. This makes data entry extremely fast: M-RET to create a new headline,
  791. on TAB to make it a child, two or more tabs to make it a (grand-)uncle.
  792. When the cursor is at the end of an empty plain list item, one TAB will
  793. make it a subitem, two or more tabs will back up to make this an item
  794. higher up in the item hierarchy."
  795. :group 'org-cycle
  796. :type 'boolean)
  797. (defcustom org-cycle-emulate-tab t
  798. "Where should `org-cycle' emulate TAB.
  799. nil Never
  800. white Only in completely white lines
  801. whitestart Only at the beginning of lines, before the first non-white char
  802. t Everywhere except in headlines
  803. exc-hl-bol Everywhere except at the start of a headline
  804. If TAB is used in a place where it does not emulate TAB, the current subtree
  805. visibility is cycled."
  806. :group 'org-cycle
  807. :type '(choice (const :tag "Never" nil)
  808. (const :tag "Only in completely white lines" white)
  809. (const :tag "Before first char in a line" whitestart)
  810. (const :tag "Everywhere except in headlines" t)
  811. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  812. ))
  813. (defcustom org-cycle-separator-lines 2
  814. "Number of empty lines needed to keep an empty line between collapsed trees.
  815. If you leave an empty line between the end of a subtree and the following
  816. headline, this empty line is hidden when the subtree is folded.
  817. Org-mode will leave (exactly) one empty line visible if the number of
  818. empty lines is equal or larger to the number given in this variable.
  819. So the default 2 means at least 2 empty lines after the end of a subtree
  820. are needed to produce free space between a collapsed subtree and the
  821. following headline.
  822. If the number is negative, and the number of empty lines is at least -N,
  823. all empty lines are shown.
  824. Special case: when 0, never leave empty lines in collapsed view."
  825. :group 'org-cycle
  826. :type 'integer)
  827. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  828. (defcustom org-pre-cycle-hook nil
  829. "Hook that is run before visibility cycling is happening.
  830. The function(s) in this hook must accept a single argument which indicates
  831. the new state that will be set right after running this hook. The
  832. argument is a symbol. Before a global state change, it can have the values
  833. `overview', `content', or `all'. Before a local state change, it can have
  834. the values `folded', `children', or `subtree'."
  835. :group 'org-cycle
  836. :type 'hook)
  837. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  838. org-cycle-hide-drawers
  839. org-cycle-show-empty-lines
  840. org-optimize-window-after-visibility-change)
  841. "Hook that is run after `org-cycle' has changed the buffer visibility.
  842. The function(s) in this hook must accept a single argument which indicates
  843. the new state that was set by the most recent `org-cycle' command. The
  844. argument is a symbol. After a global state change, it can have the values
  845. `overview', `content', or `all'. After a local state change, it can have
  846. the values `folded', `children', or `subtree'."
  847. :group 'org-cycle
  848. :type 'hook)
  849. (defgroup org-edit-structure nil
  850. "Options concerning structure editing in Org-mode."
  851. :tag "Org Edit Structure"
  852. :group 'org-structure)
  853. (defcustom org-odd-levels-only nil
  854. "Non-nil means skip even levels and only use odd levels for the outline.
  855. This has the effect that two stars are being added/taken away in
  856. promotion/demotion commands. It also influences how levels are
  857. handled by the exporters.
  858. Changing it requires restart of `font-lock-mode' to become effective
  859. for fontification also in regions already fontified.
  860. You may also set this on a per-file basis by adding one of the following
  861. lines to the buffer:
  862. #+STARTUP: odd
  863. #+STARTUP: oddeven"
  864. :group 'org-edit-structure
  865. :group 'org-appearance
  866. :type 'boolean)
  867. (defcustom org-adapt-indentation t
  868. "Non-nil means adapt indentation to outline node level.
  869. When this variable is set, Org assumes that you write outlines by
  870. indenting text in each node to align with the headline (after the stars).
  871. The following issues are influenced by this variable:
  872. - When this is set and the *entire* text in an entry is indented, the
  873. indentation is increased by one space in a demotion command, and
  874. decreased by one in a promotion command. If any line in the entry
  875. body starts with text at column 0, indentation is not changed at all.
  876. - Property drawers and planning information is inserted indented when
  877. this variable s set. When nil, they will not be indented.
  878. - TAB indents a line relative to context. The lines below a headline
  879. will be indented when this variable is set.
  880. Note that this is all about true indentation, by adding and removing
  881. space characters. See also `org-indent.el' which does level-dependent
  882. indentation in a virtual way, i.e. at display time in Emacs."
  883. :group 'org-edit-structure
  884. :type 'boolean)
  885. (defcustom org-special-ctrl-a/e nil
  886. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  887. When t, `C-a' will bring back the cursor to the beginning of the
  888. headline text, i.e. after the stars and after a possible TODO keyword.
  889. In an item, this will be the position after the bullet.
  890. When the cursor is already at that position, another `C-a' will bring
  891. it to the beginning of the line.
  892. `C-e' will jump to the end of the headline, ignoring the presence of tags
  893. in the headline. A second `C-e' will then jump to the true end of the
  894. line, after any tags. This also means that, when this variable is
  895. non-nil, `C-e' also will never jump beyond the end of the heading of a
  896. folded section, i.e. not after the ellipses.
  897. When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
  898. going to the true line boundary first. Only a directly following, identical
  899. keypress will bring the cursor to the special positions.
  900. This may also be a cons cell where the behavior for `C-a' and `C-e' is
  901. set separately."
  902. :group 'org-edit-structure
  903. :type '(choice
  904. (const :tag "off" nil)
  905. (const :tag "on: after stars/bullet and before tags first" t)
  906. (const :tag "reversed: true line boundary first" reversed)
  907. (cons :tag "Set C-a and C-e separately"
  908. (choice :tag "Special C-a"
  909. (const :tag "off" nil)
  910. (const :tag "on: after stars/bullet first" t)
  911. (const :tag "reversed: before stars/bullet first" reversed))
  912. (choice :tag "Special C-e"
  913. (const :tag "off" nil)
  914. (const :tag "on: before tags first" t)
  915. (const :tag "reversed: after tags first" reversed)))))
  916. (if (fboundp 'defvaralias)
  917. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  918. (defcustom org-special-ctrl-k nil
  919. "Non-nil means `C-k' will behave specially in headlines.
  920. When nil, `C-k' will call the default `kill-line' command.
  921. When t, the following will happen while the cursor is in the headline:
  922. - When the cursor is at the beginning of a headline, kill the entire
  923. line and possible the folded subtree below the line.
  924. - When in the middle of the headline text, kill the headline up to the tags.
  925. - When after the headline text, kill the tags."
  926. :group 'org-edit-structure
  927. :type 'boolean)
  928. (defcustom org-ctrl-k-protect-subtree nil
  929. "Non-nil means, do not delete a hidden subtree with C-k.
  930. When set to the symbol `error', simply throw an error when C-k is
  931. used to kill (part-of) a headline that has hidden text behind it.
  932. Any other non-nil value will result in a query to the user, if it is
  933. OK to kill that hidden subtree. When nil, kill without remorse."
  934. :group 'org-edit-structure
  935. :type '(choice
  936. (const :tag "Do not protect hidden subtrees" nil)
  937. (const :tag "Protect hidden subtrees with a security query" t)
  938. (const :tag "Never kill a hidden subtree with C-k" error)))
  939. (defcustom org-catch-invisible-edits nil
  940. "Check if in invisible region before inserting or deleting a character.
  941. Valid values are:
  942. nil Do not check, so just do invisible edits.
  943. error Throw an error and do nothing.
  944. show Make point visible, and do the requested edit.
  945. show-and-error Make point visible, then throw an error and abort the edit.
  946. smart Make point visible, and do insertion/deletion if it is
  947. adjacent to visible text and the change feels predictable.
  948. Never delete a previously invisible character or add in the
  949. middle or right after an invisible region. Basically, this
  950. allows insertion and backward-delete right before ellipses.
  951. FIXME: maybe in this case we should not even show?"
  952. :group 'org-edit-structure
  953. :type '(choice
  954. (const :tag "Do not check" nil)
  955. (const :tag "Throw error when trying to edit" error)
  956. (const :tag "Unhide, but do not do the edit" show-and-error)
  957. (const :tag "Show invisible part and do the edit" show)
  958. (const :tag "Be smart and do the right thing" smart)))
  959. (defcustom org-yank-folded-subtrees t
  960. "Non-nil means when yanking subtrees, fold them.
  961. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  962. it starts with a heading and all other headings in it are either children
  963. or siblings, then fold all the subtrees. However, do this only if no
  964. text after the yank would be swallowed into a folded tree by this action."
  965. :group 'org-edit-structure
  966. :type 'boolean)
  967. (defcustom org-yank-adjusted-subtrees nil
  968. "Non-nil means when yanking subtrees, adjust the level.
  969. With this setting, `org-paste-subtree' is used to insert the subtree, see
  970. this function for details."
  971. :group 'org-edit-structure
  972. :type 'boolean)
  973. (defcustom org-M-RET-may-split-line '((default . t))
  974. "Non-nil means M-RET will split the line at the cursor position.
  975. When nil, it will go to the end of the line before making a
  976. new line.
  977. You may also set this option in a different way for different
  978. contexts. Valid contexts are:
  979. headline when creating a new headline
  980. item when creating a new item
  981. table in a table field
  982. default the value to be used for all contexts not explicitly
  983. customized"
  984. :group 'org-structure
  985. :group 'org-table
  986. :type '(choice
  987. (const :tag "Always" t)
  988. (const :tag "Never" nil)
  989. (repeat :greedy t :tag "Individual contexts"
  990. (cons
  991. (choice :tag "Context"
  992. (const headline)
  993. (const item)
  994. (const table)
  995. (const default))
  996. (boolean)))))
  997. (defcustom org-insert-heading-respect-content nil
  998. "Non-nil means insert new headings after the current subtree.
  999. When nil, the new heading is created directly after the current line.
  1000. The commands \\[org-insert-heading-respect-content] and
  1001. \\[org-insert-todo-heading-respect-content] turn this variable on
  1002. for the duration of the command."
  1003. :group 'org-structure
  1004. :type 'boolean)
  1005. (defcustom org-blank-before-new-entry '((heading . auto)
  1006. (plain-list-item . auto))
  1007. "Should `org-insert-heading' leave a blank line before new heading/item?
  1008. The value is an alist, with `heading' and `plain-list-item' as CAR,
  1009. and a boolean flag as CDR. The cdr may also be the symbol `auto', in
  1010. which case Org will look at the surrounding headings/items and try to
  1011. make an intelligent decision whether to insert a blank line or not.
  1012. For plain lists, if the variable `org-empty-line-terminates-plain-lists' is
  1013. set, the setting here is ignored and no empty line is inserted, to avoid
  1014. breaking the list structure."
  1015. :group 'org-edit-structure
  1016. :type '(list
  1017. (cons (const heading)
  1018. (choice (const :tag "Never" nil)
  1019. (const :tag "Always" t)
  1020. (const :tag "Auto" auto)))
  1021. (cons (const plain-list-item)
  1022. (choice (const :tag "Never" nil)
  1023. (const :tag "Always" t)
  1024. (const :tag "Auto" auto)))))
  1025. (defcustom org-insert-heading-hook nil
  1026. "Hook being run after inserting a new heading."
  1027. :group 'org-edit-structure
  1028. :type 'hook)
  1029. (defcustom org-enable-fixed-width-editor t
  1030. "Non-nil means lines starting with \":\" are treated as fixed-width.
  1031. This currently only means they are never auto-wrapped.
  1032. When nil, such lines will be treated like ordinary lines.
  1033. See also the QUOTE keyword."
  1034. :group 'org-edit-structure
  1035. :type 'boolean)
  1036. (defcustom org-goto-auto-isearch t
  1037. "Non-nil means typing characters in `org-goto' starts incremental search."
  1038. :group 'org-edit-structure
  1039. :type 'boolean)
  1040. (defgroup org-sparse-trees nil
  1041. "Options concerning sparse trees in Org-mode."
  1042. :tag "Org Sparse Trees"
  1043. :group 'org-structure)
  1044. (defcustom org-highlight-sparse-tree-matches t
  1045. "Non-nil means highlight all matches that define a sparse tree.
  1046. The highlights will automatically disappear the next time the buffer is
  1047. changed by an edit command."
  1048. :group 'org-sparse-trees
  1049. :type 'boolean)
  1050. (defcustom org-remove-highlights-with-change t
  1051. "Non-nil means any change to the buffer will remove temporary highlights.
  1052. Such highlights are created by `org-occur' and `org-clock-display'.
  1053. When nil, `C-c C-c needs to be used to get rid of the highlights.
  1054. The highlights created by `org-preview-latex-fragment' always need
  1055. `C-c C-c' to be removed."
  1056. :group 'org-sparse-trees
  1057. :group 'org-time
  1058. :type 'boolean)
  1059. (defcustom org-occur-hook '(org-first-headline-recenter)
  1060. "Hook that is run after `org-occur' has constructed a sparse tree.
  1061. This can be used to recenter the window to show as much of the structure
  1062. as possible."
  1063. :group 'org-sparse-trees
  1064. :type 'hook)
  1065. (defgroup org-imenu-and-speedbar nil
  1066. "Options concerning imenu and speedbar in Org-mode."
  1067. :tag "Org Imenu and Speedbar"
  1068. :group 'org-structure)
  1069. (defcustom org-imenu-depth 2
  1070. "The maximum level for Imenu access to Org-mode headlines.
  1071. This also applied for speedbar access."
  1072. :group 'org-imenu-and-speedbar
  1073. :type 'integer)
  1074. (defgroup org-table nil
  1075. "Options concerning tables in Org-mode."
  1076. :tag "Org Table"
  1077. :group 'org)
  1078. (defcustom org-enable-table-editor 'optimized
  1079. "Non-nil means lines starting with \"|\" are handled by the table editor.
  1080. When nil, such lines will be treated like ordinary lines.
  1081. When equal to the symbol `optimized', the table editor will be optimized to
  1082. do the following:
  1083. - Automatic overwrite mode in front of whitespace in table fields.
  1084. This makes the structure of the table stay in tact as long as the edited
  1085. field does not exceed the column width.
  1086. - Minimize the number of realigns. Normally, the table is aligned each time
  1087. TAB or RET are pressed to move to another field. With optimization this
  1088. happens only if changes to a field might have changed the column width.
  1089. Optimization requires replacing the functions `self-insert-command',
  1090. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  1091. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  1092. very good at guessing when a re-align will be necessary, but you can always
  1093. force one with \\[org-ctrl-c-ctrl-c].
  1094. If you would like to use the optimized version in Org-mode, but the
  1095. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  1096. This variable can be used to turn on and off the table editor during a session,
  1097. but in order to toggle optimization, a restart is required.
  1098. See also the variable `org-table-auto-blank-field'."
  1099. :group 'org-table
  1100. :type '(choice
  1101. (const :tag "off" nil)
  1102. (const :tag "on" t)
  1103. (const :tag "on, optimized" optimized)))
  1104. (defcustom org-self-insert-cluster-for-undo t
  1105. "Non-nil means cluster self-insert commands for undo when possible.
  1106. If this is set, then, like in the Emacs command loop, 20 consecutive
  1107. characters will be undone together.
  1108. This is configurable, because there is some impact on typing performance."
  1109. :group 'org-table
  1110. :type 'boolean)
  1111. (defcustom org-table-tab-recognizes-table.el t
  1112. "Non-nil means TAB will automatically notice a table.el table.
  1113. When it sees such a table, it moves point into it and - if necessary -
  1114. calls `table-recognize-table'."
  1115. :group 'org-table-editing
  1116. :type 'boolean)
  1117. (defgroup org-link nil
  1118. "Options concerning links in Org-mode."
  1119. :tag "Org Link"
  1120. :group 'org)
  1121. (defvar org-link-abbrev-alist-local nil
  1122. "Buffer-local version of `org-link-abbrev-alist', which see.
  1123. The value of this is taken from the #+LINK lines.")
  1124. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  1125. (defcustom org-link-abbrev-alist nil
  1126. "Alist of link abbreviations.
  1127. The car of each element is a string, to be replaced at the start of a link.
  1128. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  1129. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  1130. [[linkkey:tag][description]]
  1131. The 'linkkey' must be a word word, starting with a letter, followed
  1132. by letters, numbers, '-' or '_'.
  1133. If REPLACE is a string, the tag will simply be appended to create the link.
  1134. If the string contains \"%s\", the tag will be inserted there. Alternatively,
  1135. the placeholder \"%h\" will cause a url-encoded version of the tag to
  1136. be inserted at that point (see the function `url-hexify-string').
  1137. REPLACE may also be a function that will be called with the tag as the
  1138. only argument to create the link, which should be returned as a string.
  1139. See the manual for examples."
  1140. :group 'org-link
  1141. :type '(repeat
  1142. (cons
  1143. (string :tag "Protocol")
  1144. (choice
  1145. (string :tag "Format")
  1146. (function)))))
  1147. (defcustom org-descriptive-links t
  1148. "Non-nil means Org will display descriptive links.
  1149. E.g. [[http://orgmode.org][Org website]] will be displayed as
  1150. \"Org Website\", hiding the link itself and just displaying its
  1151. description. When set to `nil', Org will display the full links
  1152. literally.
  1153. You can interactively set the value of this variable by calling
  1154. `org-toggle-link-display' or from the menu Org>Hyperlinks menu."
  1155. :group 'org-link
  1156. :type 'boolean)
  1157. (defcustom org-link-file-path-type 'adaptive
  1158. "How the path name in file links should be stored.
  1159. Valid values are:
  1160. relative Relative to the current directory, i.e. the directory of the file
  1161. into which the link is being inserted.
  1162. absolute Absolute path, if possible with ~ for home directory.
  1163. noabbrev Absolute path, no abbreviation of home directory.
  1164. adaptive Use relative path for files in the current directory and sub-
  1165. directories of it. For other files, use an absolute path."
  1166. :group 'org-link
  1167. :type '(choice
  1168. (const relative)
  1169. (const absolute)
  1170. (const noabbrev)
  1171. (const adaptive)))
  1172. (defcustom org-activate-links '(bracket angle plain radio tag date footnote)
  1173. "Types of links that should be activated in Org-mode files.
  1174. This is a list of symbols, each leading to the activation of a certain link
  1175. type. In principle, it does not hurt to turn on most link types - there may
  1176. be a small gain when turning off unused link types. The types are:
  1177. bracket The recommended [[link][description]] or [[link]] links with hiding.
  1178. angle Links in angular brackets that may contain whitespace like
  1179. <bbdb:Carsten Dominik>.
  1180. plain Plain links in normal text, no whitespace, like http://google.com.
  1181. radio Text that is matched by a radio target, see manual for details.
  1182. tag Tag settings in a headline (link to tag search).
  1183. date Time stamps (link to calendar).
  1184. footnote Footnote labels.
  1185. Changing this variable requires a restart of Emacs to become effective."
  1186. :group 'org-link
  1187. :type '(set :greedy t
  1188. (const :tag "Double bracket links" bracket)
  1189. (const :tag "Angular bracket links" angle)
  1190. (const :tag "Plain text links" plain)
  1191. (const :tag "Radio target matches" radio)
  1192. (const :tag "Tags" tag)
  1193. (const :tag "Timestamps" date)
  1194. (const :tag "Footnotes" footnote)))
  1195. (defcustom org-make-link-description-function nil
  1196. "Function to use to generate link descriptions from links.
  1197. If nil the link location will be used. This function must take
  1198. two parameters; the first is the link and the second the
  1199. description `org-insert-link' has generated, and should return the
  1200. description to use."
  1201. :group 'org-link
  1202. :type 'function)
  1203. (defgroup org-link-store nil
  1204. "Options concerning storing links in Org-mode."
  1205. :tag "Org Store Link"
  1206. :group 'org-link)
  1207. (defcustom org-email-link-description-format "Email %c: %.30s"
  1208. "Format of the description part of a link to an email or usenet message.
  1209. The following %-escapes will be replaced by corresponding information:
  1210. %F full \"From\" field
  1211. %f name, taken from \"From\" field, address if no name
  1212. %T full \"To\" field
  1213. %t first name in \"To\" field, address if no name
  1214. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  1215. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  1216. %s subject
  1217. %d date
  1218. %m message-id.
  1219. You may use normal field width specification between the % and the letter.
  1220. This is for example useful to limit the length of the subject.
  1221. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  1222. :group 'org-link-store
  1223. :type 'string)
  1224. (defcustom org-from-is-user-regexp
  1225. (let (r1 r2)
  1226. (when (and user-mail-address (not (string= user-mail-address "")))
  1227. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  1228. (when (and user-full-name (not (string= user-full-name "")))
  1229. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  1230. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  1231. "Regexp matched against the \"From:\" header of an email or usenet message.
  1232. It should match if the message is from the user him/herself."
  1233. :group 'org-link-store
  1234. :type 'regexp)
  1235. (defcustom org-link-to-org-use-id 'create-if-interactive-and-no-custom-id
  1236. "Non-nil means storing a link to an Org file will use entry IDs.
  1237. Note that before this variable is even considered, org-id must be loaded,
  1238. so please customize `org-modules' and turn it on.
  1239. The variable can have the following values:
  1240. t Create an ID if needed to make a link to the current entry.
  1241. create-if-interactive
  1242. If `org-store-link' is called directly (interactively, as a user
  1243. command), do create an ID to support the link. But when doing the
  1244. job for remember, only use the ID if it already exists. The
  1245. purpose of this setting is to avoid proliferation of unwanted
  1246. IDs, just because you happen to be in an Org file when you
  1247. call `org-remember' that automatically and preemptively
  1248. creates a link. If you do want to get an ID link in a remember
  1249. template to an entry not having an ID, create it first by
  1250. explicitly creating a link to it, using `C-c C-l' first.
  1251. create-if-interactive-and-no-custom-id
  1252. Like create-if-interactive, but do not create an ID if there is
  1253. a CUSTOM_ID property defined in the entry. This is the default.
  1254. use-existing
  1255. Use existing ID, do not create one.
  1256. nil Never use an ID to make a link, instead link using a text search for
  1257. the headline text."
  1258. :group 'org-link-store
  1259. :type '(choice
  1260. (const :tag "Create ID to make link" t)
  1261. (const :tag "Create if storing link interactively"
  1262. create-if-interactive)
  1263. (const :tag "Create if storing link interactively and no CUSTOM_ID is present"
  1264. create-if-interactive-and-no-custom-id)
  1265. (const :tag "Only use existing" use-existing)
  1266. (const :tag "Do not use ID to create link" nil)))
  1267. (defcustom org-context-in-file-links t
  1268. "Non-nil means file links from `org-store-link' contain context.
  1269. A search string will be added to the file name with :: as separator and
  1270. used to find the context when the link is activated by the command
  1271. `org-open-at-point'. When this option is t, the entire active region
  1272. will be placed in the search string of the file link. If set to a
  1273. positive integer, only the first n lines of context will be stored.
  1274. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1275. negates this setting for the duration of the command."
  1276. :group 'org-link-store
  1277. :type '(choice boolean integer))
  1278. (defcustom org-keep-stored-link-after-insertion nil
  1279. "Non-nil means keep link in list for entire session.
  1280. The command `org-store-link' adds a link pointing to the current
  1281. location to an internal list. These links accumulate during a session.
  1282. The command `org-insert-link' can be used to insert links into any
  1283. Org-mode file (offering completion for all stored links). When this
  1284. option is nil, every link which has been inserted once using \\[org-insert-link]
  1285. will be removed from the list, to make completing the unused links
  1286. more efficient."
  1287. :group 'org-link-store
  1288. :type 'boolean)
  1289. (defgroup org-link-follow nil
  1290. "Options concerning following links in Org-mode."
  1291. :tag "Org Follow Link"
  1292. :group 'org-link)
  1293. (defcustom org-link-translation-function nil
  1294. "Function to translate links with different syntax to Org syntax.
  1295. This can be used to translate links created for example by the Planner
  1296. or emacs-wiki packages to Org syntax.
  1297. The function must accept two parameters, a TYPE containing the link
  1298. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  1299. which is everything after the link protocol. It should return a cons
  1300. with possibly modified values of type and path.
  1301. Org contains a function for this, so if you set this variable to
  1302. `org-translate-link-from-planner', you should be able follow many
  1303. links created by planner."
  1304. :group 'org-link-follow
  1305. :type 'function)
  1306. (defcustom org-follow-link-hook nil
  1307. "Hook that is run after a link has been followed."
  1308. :group 'org-link-follow
  1309. :type 'hook)
  1310. (defcustom org-tab-follows-link nil
  1311. "Non-nil means on links TAB will follow the link.
  1312. Needs to be set before org.el is loaded.
  1313. This really should not be used, it does not make sense, and the
  1314. implementation is bad."
  1315. :group 'org-link-follow
  1316. :type 'boolean)
  1317. (defcustom org-return-follows-link nil
  1318. "Non-nil means on links RET will follow the link."
  1319. :group 'org-link-follow
  1320. :type 'boolean)
  1321. (defcustom org-mouse-1-follows-link
  1322. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1323. "Non-nil means mouse-1 on a link will follow the link.
  1324. A longer mouse click will still set point. Does not work on XEmacs.
  1325. Needs to be set before org.el is loaded."
  1326. :group 'org-link-follow
  1327. :type 'boolean)
  1328. (defcustom org-mark-ring-length 4
  1329. "Number of different positions to be recorded in the ring.
  1330. Changing this requires a restart of Emacs to work correctly."
  1331. :group 'org-link-follow
  1332. :type 'integer)
  1333. (defcustom org-link-search-must-match-exact-headline 'query-to-create
  1334. "Non-nil means internal links in Org files must exactly match a headline.
  1335. When nil, the link search tries to match a phrase with all words
  1336. in the search text."
  1337. :group 'org-link-follow
  1338. :type '(choice
  1339. (const :tag "Use fuzzy text search" nil)
  1340. (const :tag "Match only exact headline" t)
  1341. (const :tag "Match exact headline or query to create it"
  1342. query-to-create)))
  1343. (defcustom org-link-frame-setup
  1344. '((vm . vm-visit-folder-other-frame)
  1345. (gnus . org-gnus-no-new-news)
  1346. (file . find-file-other-window)
  1347. (wl . wl-other-frame))
  1348. "Setup the frame configuration for following links.
  1349. When following a link with Emacs, it may often be useful to display
  1350. this link in another window or frame. This variable can be used to
  1351. set this up for the different types of links.
  1352. For VM, use any of
  1353. `vm-visit-folder'
  1354. `vm-visit-folder-other-window'
  1355. `vm-visit-folder-other-frame'
  1356. For Gnus, use any of
  1357. `gnus'
  1358. `gnus-other-frame'
  1359. `org-gnus-no-new-news'
  1360. For FILE, use any of
  1361. `find-file'
  1362. `find-file-other-window'
  1363. `find-file-other-frame'
  1364. For Wanderlust use any of
  1365. `wl'
  1366. `wl-other-frame'
  1367. For the calendar, use the variable `calendar-setup'.
  1368. For BBDB, it is currently only possible to display the matches in
  1369. another window."
  1370. :group 'org-link-follow
  1371. :type '(list
  1372. (cons (const vm)
  1373. (choice
  1374. (const vm-visit-folder)
  1375. (const vm-visit-folder-other-window)
  1376. (const vm-visit-folder-other-frame)))
  1377. (cons (const gnus)
  1378. (choice
  1379. (const gnus)
  1380. (const gnus-other-frame)
  1381. (const org-gnus-no-new-news)))
  1382. (cons (const file)
  1383. (choice
  1384. (const find-file)
  1385. (const find-file-other-window)
  1386. (const find-file-other-frame)))
  1387. (cons (const wl)
  1388. (choice
  1389. (const wl)
  1390. (const wl-other-frame)))))
  1391. (defcustom org-display-internal-link-with-indirect-buffer nil
  1392. "Non-nil means use indirect buffer to display infile links.
  1393. Activating internal links (from one location in a file to another location
  1394. in the same file) normally just jumps to the location. When the link is
  1395. activated with a \\[universal-argument] prefix (or with mouse-3), the link \
  1396. is displayed in
  1397. another window. When this option is set, the other window actually displays
  1398. an indirect buffer clone of the current buffer, to avoid any visibility
  1399. changes to the current buffer."
  1400. :group 'org-link-follow
  1401. :type 'boolean)
  1402. (defcustom org-open-non-existing-files nil
  1403. "Non-nil means `org-open-file' will open non-existing files.
  1404. When nil, an error will be generated.
  1405. This variable applies only to external applications because they
  1406. might choke on non-existing files. If the link is to a file that
  1407. will be opened in Emacs, the variable is ignored."
  1408. :group 'org-link-follow
  1409. :type 'boolean)
  1410. (defcustom org-open-directory-means-index-dot-org nil
  1411. "Non-nil means a link to a directory really means to index.org.
  1412. When nil, following a directory link will run dired or open a finder/explorer
  1413. window on that directory."
  1414. :group 'org-link-follow
  1415. :type 'boolean)
  1416. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1417. "Function and arguments to call for following mailto links.
  1418. This is a list with the first element being a Lisp function, and the
  1419. remaining elements being arguments to the function. In string arguments,
  1420. %a will be replaced by the address, and %s will be replaced by the subject
  1421. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1422. :group 'org-link-follow
  1423. :type '(choice
  1424. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1425. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1426. (const :tag "message-mail" (message-mail "%a" "%s"))
  1427. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1428. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1429. "Non-nil means ask for confirmation before executing shell links.
  1430. Shell links can be dangerous: just think about a link
  1431. [[shell:rm -rf ~/*][Google Search]]
  1432. This link would show up in your Org-mode document as \"Google Search\",
  1433. but really it would remove your entire home directory.
  1434. Therefore we advise against setting this variable to nil.
  1435. Just change it to `y-or-n-p' if you want to confirm with a
  1436. single keystroke rather than having to type \"yes\"."
  1437. :group 'org-link-follow
  1438. :type '(choice
  1439. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1440. (const :tag "with y-or-n (faster)" y-or-n-p)
  1441. (const :tag "no confirmation (dangerous)" nil)))
  1442. (put 'org-confirm-shell-link-function
  1443. 'safe-local-variable
  1444. #'(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1445. (defcustom org-confirm-shell-link-not-regexp ""
  1446. "A regexp to skip confirmation for shell links."
  1447. :group 'org-link-follow
  1448. :type 'regexp)
  1449. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1450. "Non-nil means ask for confirmation before executing Emacs Lisp links.
  1451. Elisp links can be dangerous: just think about a link
  1452. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1453. This link would show up in your Org-mode document as \"Google Search\",
  1454. but really it would remove your entire home directory.
  1455. Therefore we advise against setting this variable to nil.
  1456. Just change it to `y-or-n-p' if you want to confirm with a
  1457. single keystroke rather than having to type \"yes\"."
  1458. :group 'org-link-follow
  1459. :type '(choice
  1460. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1461. (const :tag "with y-or-n (faster)" y-or-n-p)
  1462. (const :tag "no confirmation (dangerous)" nil)))
  1463. (put 'org-confirm-shell-link-function
  1464. 'safe-local-variable
  1465. #'(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1466. (defcustom org-confirm-elisp-link-not-regexp ""
  1467. "A regexp to skip confirmation for Elisp links."
  1468. :group 'org-link-follow
  1469. :type 'regexp)
  1470. (defconst org-file-apps-defaults-gnu
  1471. '((remote . emacs)
  1472. (system . mailcap)
  1473. (t . mailcap))
  1474. "Default file applications on a UNIX or GNU/Linux system.
  1475. See `org-file-apps'.")
  1476. (defconst org-file-apps-defaults-macosx
  1477. '((remote . emacs)
  1478. (t . "open %s")
  1479. (system . "open %s")
  1480. ("ps.gz" . "gv %s")
  1481. ("eps.gz" . "gv %s")
  1482. ("dvi" . "xdvi %s")
  1483. ("fig" . "xfig %s"))
  1484. "Default file applications on a MacOS X system.
  1485. The system \"open\" is known as a default, but we use X11 applications
  1486. for some files for which the OS does not have a good default.
  1487. See `org-file-apps'.")
  1488. (defconst org-file-apps-defaults-windowsnt
  1489. (list
  1490. '(remote . emacs)
  1491. (cons t
  1492. (list (if (featurep 'xemacs)
  1493. 'mswindows-shell-execute
  1494. 'w32-shell-execute)
  1495. "open" 'file))
  1496. (cons 'system
  1497. (list (if (featurep 'xemacs)
  1498. 'mswindows-shell-execute
  1499. 'w32-shell-execute)
  1500. "open" 'file)))
  1501. "Default file applications on a Windows NT system.
  1502. The system \"open\" is used for most files.
  1503. See `org-file-apps'.")
  1504. (defcustom org-file-apps
  1505. '(
  1506. (auto-mode . emacs)
  1507. ("\\.mm\\'" . default)
  1508. ("\\.x?html?\\'" . default)
  1509. ("\\.pdf\\'" . default)
  1510. )
  1511. "External applications for opening `file:path' items in a document.
  1512. Org-mode uses system defaults for different file types, but
  1513. you can use this variable to set the application for a given file
  1514. extension. The entries in this list are cons cells where the car identifies
  1515. files and the cdr the corresponding command. Possible values for the
  1516. file identifier are
  1517. \"string\" A string as a file identifier can be interpreted in different
  1518. ways, depending on its contents:
  1519. - Alphanumeric characters only:
  1520. Match links with this file extension.
  1521. Example: (\"pdf\" . \"evince %s\")
  1522. to open PDFs with evince.
  1523. - Regular expression: Match links where the
  1524. filename matches the regexp. If you want to
  1525. use groups here, use shy groups.
  1526. Example: (\"\\.x?html\\'\" . \"firefox %s\")
  1527. (\"\\(?:xhtml\\|html\\)\" . \"firefox %s\")
  1528. to open *.html and *.xhtml with firefox.
  1529. - Regular expression which contains (non-shy) groups:
  1530. Match links where the whole link, including \"::\", and
  1531. anything after that, matches the regexp.
  1532. In a custom command string, %1, %2, etc. are replaced with
  1533. the parts of the link that were matched by the groups.
  1534. For backwards compatibility, if a command string is given
  1535. that does not use any of the group matches, this case is
  1536. handled identically to the second one (i.e. match against
  1537. file name only).
  1538. In a custom lisp form, you can access the group matches with
  1539. (match-string n link).
  1540. Example: (\"\\.pdf::\\(\\d+\\)\\'\" . \"evince -p %1 %s\")
  1541. to open [[file:document.pdf::5]] with evince at page 5.
  1542. `directory' Matches a directory
  1543. `remote' Matches a remote file, accessible through tramp or efs.
  1544. Remote files most likely should be visited through Emacs
  1545. because external applications cannot handle such paths.
  1546. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1547. so all files Emacs knows how to handle. Using this with
  1548. command `emacs' will open most files in Emacs. Beware that this
  1549. will also open html files inside Emacs, unless you add
  1550. (\"html\" . default) to the list as well.
  1551. t Default for files not matched by any of the other options.
  1552. `system' The system command to open files, like `open' on Windows
  1553. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1554. that will be selected if you call `C-c C-o' with a double
  1555. \\[universal-argument] \\[universal-argument] prefix.
  1556. Possible values for the command are:
  1557. `emacs' The file will be visited by the current Emacs process.
  1558. `default' Use the default application for this file type, which is the
  1559. association for t in the list, most likely in the system-specific
  1560. part.
  1561. This can be used to overrule an unwanted setting in the
  1562. system-specific variable.
  1563. `system' Use the system command for opening files, like \"open\".
  1564. This command is specified by the entry whose car is `system'.
  1565. Most likely, the system-specific version of this variable
  1566. does define this command, but you can overrule/replace it
  1567. here.
  1568. string A command to be executed by a shell; %s will be replaced
  1569. by the path to the file.
  1570. sexp A Lisp form which will be evaluated. The file path will
  1571. be available in the Lisp variable `file'.
  1572. For more examples, see the system specific constants
  1573. `org-file-apps-defaults-macosx'
  1574. `org-file-apps-defaults-windowsnt'
  1575. `org-file-apps-defaults-gnu'."
  1576. :group 'org-link-follow
  1577. :type '(repeat
  1578. (cons (choice :value ""
  1579. (string :tag "Extension")
  1580. (const :tag "System command to open files" system)
  1581. (const :tag "Default for unrecognized files" t)
  1582. (const :tag "Remote file" remote)
  1583. (const :tag "Links to a directory" directory)
  1584. (const :tag "Any files that have Emacs modes"
  1585. auto-mode))
  1586. (choice :value ""
  1587. (const :tag "Visit with Emacs" emacs)
  1588. (const :tag "Use default" default)
  1589. (const :tag "Use the system command" system)
  1590. (string :tag "Command")
  1591. (sexp :tag "Lisp form")))))
  1592. (defgroup org-refile nil
  1593. "Options concerning refiling entries in Org-mode."
  1594. :tag "Org Refile"
  1595. :group 'org)
  1596. (defcustom org-directory "~/org"
  1597. "Directory with org files.
  1598. This is just a default location to look for Org files. There is no need
  1599. at all to put your files into this directory. It is only used in the
  1600. following situations:
  1601. 1. When a remember template specifies a target file that is not an
  1602. absolute path. The path will then be interpreted relative to
  1603. `org-directory'
  1604. 2. When a remember note is filed away in an interactive way (when exiting the
  1605. note buffer with `C-1 C-c C-c'. The user is prompted for an org file,
  1606. with `org-directory' as the default path."
  1607. :group 'org-refile
  1608. :group 'org-remember
  1609. :type 'directory)
  1610. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1611. "Default target for storing notes.
  1612. Used as a fall back file for org-remember.el and org-capture.el, for
  1613. templates that do not specify a target file."
  1614. :group 'org-refile
  1615. :group 'org-remember
  1616. :type '(choice
  1617. (const :tag "Default from remember-data-file" nil)
  1618. file))
  1619. (defcustom org-goto-interface 'outline
  1620. "The default interface to be used for `org-goto'.
  1621. Allowed values are:
  1622. outline The interface shows an outline of the relevant file
  1623. and the correct heading is found by moving through
  1624. the outline or by searching with incremental search.
  1625. outline-path-completion Headlines in the current buffer are offered via
  1626. completion. This is the interface also used by
  1627. the refile command."
  1628. :group 'org-refile
  1629. :type '(choice
  1630. (const :tag "Outline" outline)
  1631. (const :tag "Outline-path-completion" outline-path-completion)))
  1632. (defcustom org-goto-max-level 5
  1633. "Maximum target level when running `org-goto' with refile interface."
  1634. :group 'org-refile
  1635. :type 'integer)
  1636. (defcustom org-reverse-note-order nil
  1637. "Non-nil means store new notes at the beginning of a file or entry.
  1638. When nil, new notes will be filed to the end of a file or entry.
  1639. This can also be a list with cons cells of regular expressions that
  1640. are matched against file names, and values."
  1641. :group 'org-remember
  1642. :group 'org-refile
  1643. :type '(choice
  1644. (const :tag "Reverse always" t)
  1645. (const :tag "Reverse never" nil)
  1646. (repeat :tag "By file name regexp"
  1647. (cons regexp boolean))))
  1648. (defcustom org-log-refile nil
  1649. "Information to record when a task is refiled.
  1650. Possible values are:
  1651. nil Don't add anything
  1652. time Add a time stamp to the task
  1653. note Prompt for a note and add it with template `org-log-note-headings'
  1654. This option can also be set with on a per-file-basis with
  1655. #+STARTUP: nologrefile
  1656. #+STARTUP: logrefile
  1657. #+STARTUP: lognoterefile
  1658. You can have local logging settings for a subtree by setting the LOGGING
  1659. property to one or more of these keywords.
  1660. When bulk-refiling from the agenda, the value `note' is forbidden and
  1661. will temporarily be changed to `time'."
  1662. :group 'org-refile
  1663. :group 'org-progress
  1664. :type '(choice
  1665. (const :tag "No logging" nil)
  1666. (const :tag "Record timestamp" time)
  1667. (const :tag "Record timestamp with note." note)))
  1668. (defcustom org-refile-targets nil
  1669. "Targets for refiling entries with \\[org-refile].
  1670. This is a list of cons cells. Each cell contains:
  1671. - a specification of the files to be considered, either a list of files,
  1672. or a symbol whose function or variable value will be used to retrieve
  1673. a file name or a list of file names. If you use `org-agenda-files' for
  1674. that, all agenda files will be scanned for targets. Nil means consider
  1675. headings in the current buffer.
  1676. - A specification of how to find candidate refile targets. This may be
  1677. any of:
  1678. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1679. This tag has to be present in all target headlines, inheritance will
  1680. not be considered.
  1681. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1682. todo keyword.
  1683. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1684. headlines that are refiling targets.
  1685. - a cons cell (:level . N). Any headline of level N is considered a target.
  1686. Note that, when `org-odd-levels-only' is set, level corresponds to
  1687. order in hierarchy, not to the number of stars.
  1688. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1689. Note that, when `org-odd-levels-only' is set, level corresponds to
  1690. order in hierarchy, not to the number of stars.
  1691. Each element of this list generates a set of possible targets.
  1692. The union of these sets is presented (with completion) to
  1693. the user by `org-refile'.
  1694. You can set the variable `org-refile-target-verify-function' to a function
  1695. to verify each headline found by the simple criteria above.
  1696. When this variable is nil, all top-level headlines in the current buffer
  1697. are used, equivalent to the value `((nil . (:level . 1))'."
  1698. :group 'org-refile
  1699. :type '(repeat
  1700. (cons
  1701. (choice :value org-agenda-files
  1702. (const :tag "All agenda files" org-agenda-files)
  1703. (const :tag "Current buffer" nil)
  1704. (function) (variable) (file))
  1705. (choice :tag "Identify target headline by"
  1706. (cons :tag "Specific tag" (const :value :tag) (string))
  1707. (cons :tag "TODO keyword" (const :value :todo) (string))
  1708. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  1709. (cons :tag "Level number" (const :value :level) (integer))
  1710. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  1711. (defcustom org-refile-target-verify-function nil
  1712. "Function to verify if the headline at point should be a refile target.
  1713. The function will be called without arguments, with point at the
  1714. beginning of the headline. It should return t and leave point
  1715. where it is if the headline is a valid target for refiling.
  1716. If the target should not be selected, the function must return nil.
  1717. In addition to this, it may move point to a place from where the search
  1718. should be continued. For example, the function may decide that the entire
  1719. subtree of the current entry should be excluded and move point to the end
  1720. of the subtree."
  1721. :group 'org-refile
  1722. :type 'function)
  1723. (defcustom org-refile-use-cache nil
  1724. "Non-nil means cache refile targets to speed up the process.
  1725. The cache for a particular file will be updated automatically when
  1726. the buffer has been killed, or when any of the marker used for flagging
  1727. refile targets no longer points at a live buffer.
  1728. If you have added new entries to a buffer that might themselves be targets,
  1729. you need to clear the cache manually by pressing `C-0 C-c C-w' or, if you
  1730. find that easier, `C-u C-u C-u C-c C-w'."
  1731. :group 'org-refile
  1732. :type 'boolean)
  1733. (defcustom org-refile-use-outline-path nil
  1734. "Non-nil means provide refile targets as paths.
  1735. So a level 3 headline will be available as level1/level2/level3.
  1736. When the value is `file', also include the file name (without directory)
  1737. into the path. In this case, you can also stop the completion after
  1738. the file name, to get entries inserted as top level in the file.
  1739. When `full-file-path', include the full file path."
  1740. :group 'org-refile
  1741. :type '(choice
  1742. (const :tag "Not" nil)
  1743. (const :tag "Yes" t)
  1744. (const :tag "Start with file name" file)
  1745. (const :tag "Start with full file path" full-file-path)))
  1746. (defcustom org-outline-path-complete-in-steps t
  1747. "Non-nil means complete the outline path in hierarchical steps.
  1748. When Org-mode uses the refile interface to select an outline path
  1749. \(see variable `org-refile-use-outline-path'), the completion of
  1750. the path can be done is a single go, or if can be done in steps down
  1751. the headline hierarchy. Going in steps is probably the best if you
  1752. do not use a special completion package like `ido' or `icicles'.
  1753. However, when using these packages, going in one step can be very
  1754. fast, while still showing the whole path to the entry."
  1755. :group 'org-refile
  1756. :type 'boolean)
  1757. (defcustom org-refile-allow-creating-parent-nodes nil
  1758. "Non-nil means allow to create new nodes as refile targets.
  1759. New nodes are then created by adding \"/new node name\" to the completion
  1760. of an existing node. When the value of this variable is `confirm',
  1761. new node creation must be confirmed by the user (recommended)
  1762. When nil, the completion must match an existing entry.
  1763. Note that, if the new heading is not seen by the criteria
  1764. listed in `org-refile-targets', multiple instances of the same
  1765. heading would be created by trying again to file under the new
  1766. heading."
  1767. :group 'org-refile
  1768. :type '(choice
  1769. (const :tag "Never" nil)
  1770. (const :tag "Always" t)
  1771. (const :tag "Prompt for confirmation" confirm)))
  1772. (defcustom org-refile-active-region-within-subtree nil
  1773. "Non-nil means also refile active region within a subtree.
  1774. By default `org-refile' doesn't allow refiling regions if they
  1775. don't contain a set of subtrees, but it might be convenient to
  1776. do so sometimes: in that case, the first line of the region is
  1777. converted to a headline before refiling."
  1778. :group 'org-refile
  1779. :type 'boolean)
  1780. (defgroup org-todo nil
  1781. "Options concerning TODO items in Org-mode."
  1782. :tag "Org TODO"
  1783. :group 'org)
  1784. (defgroup org-progress nil
  1785. "Options concerning Progress logging in Org-mode."
  1786. :tag "Org Progress"
  1787. :group 'org-time)
  1788. (defvar org-todo-interpretation-widgets
  1789. '((:tag "Sequence (cycling hits every state)" sequence)
  1790. (:tag "Type (cycling directly to DONE)" type))
  1791. "The available interpretation symbols for customizing `org-todo-keywords'.
  1792. Interested libraries should add to this list.")
  1793. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1794. "List of TODO entry keyword sequences and their interpretation.
  1795. \\<org-mode-map>This is a list of sequences.
  1796. Each sequence starts with a symbol, either `sequence' or `type',
  1797. indicating if the keywords should be interpreted as a sequence of
  1798. action steps, or as different types of TODO items. The first
  1799. keywords are states requiring action - these states will select a headline
  1800. for inclusion into the global TODO list Org-mode produces. If one of
  1801. the \"keywords\" is the vertical bar, \"|\", the remaining keywords
  1802. signify that no further action is necessary. If \"|\" is not found,
  1803. the last keyword is treated as the only DONE state of the sequence.
  1804. The command \\[org-todo] cycles an entry through these states, and one
  1805. additional state where no keyword is present. For details about this
  1806. cycling, see the manual.
  1807. TODO keywords and interpretation can also be set on a per-file basis with
  1808. the special #+SEQ_TODO and #+TYP_TODO lines.
  1809. Each keyword can optionally specify a character for fast state selection
  1810. \(in combination with the variable `org-use-fast-todo-selection')
  1811. and specifiers for state change logging, using the same syntax
  1812. that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
  1813. that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1814. indicates to record a time stamp each time this state is selected.
  1815. Each keyword may also specify if a timestamp or a note should be
  1816. recorded when entering or leaving the state, by adding additional
  1817. characters in the parenthesis after the keyword. This looks like this:
  1818. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1819. record only the time of the state change. With X and Y being either
  1820. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1821. Y when leaving the state if and only if the *target* state does not
  1822. define X. You may omit any of the fast-selection key or X or /Y,
  1823. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1824. For backward compatibility, this variable may also be just a list
  1825. of keywords - in this case the interpretation (sequence or type) will be
  1826. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1827. :group 'org-todo
  1828. :group 'org-keywords
  1829. :type '(choice
  1830. (repeat :tag "Old syntax, just keywords"
  1831. (string :tag "Keyword"))
  1832. (repeat :tag "New syntax"
  1833. (cons
  1834. (choice
  1835. :tag "Interpretation"
  1836. ;;Quick and dirty way to see
  1837. ;;`org-todo-interpretations'. This takes the
  1838. ;;place of item arguments
  1839. :convert-widget
  1840. (lambda (widget)
  1841. (widget-put widget
  1842. :args (mapcar
  1843. #'(lambda (x)
  1844. (widget-convert
  1845. (cons 'const x)))
  1846. org-todo-interpretation-widgets))
  1847. widget))
  1848. (repeat
  1849. (string :tag "Keyword"))))))
  1850. (defvar org-todo-keywords-1 nil
  1851. "All TODO and DONE keywords active in a buffer.")
  1852. (make-variable-buffer-local 'org-todo-keywords-1)
  1853. (defvar org-todo-keywords-for-agenda nil)
  1854. (defvar org-done-keywords-for-agenda nil)
  1855. (defvar org-drawers-for-agenda nil)
  1856. (defvar org-todo-keyword-alist-for-agenda nil)
  1857. (defvar org-tag-alist-for-agenda nil)
  1858. (defvar org-agenda-contributing-files nil)
  1859. (defvar org-not-done-keywords nil)
  1860. (make-variable-buffer-local 'org-not-done-keywords)
  1861. (defvar org-done-keywords nil)
  1862. (make-variable-buffer-local 'org-done-keywords)
  1863. (defvar org-todo-heads nil)
  1864. (make-variable-buffer-local 'org-todo-heads)
  1865. (defvar org-todo-sets nil)
  1866. (make-variable-buffer-local 'org-todo-sets)
  1867. (defvar org-todo-log-states nil)
  1868. (make-variable-buffer-local 'org-todo-log-states)
  1869. (defvar org-todo-kwd-alist nil)
  1870. (make-variable-buffer-local 'org-todo-kwd-alist)
  1871. (defvar org-todo-key-alist nil)
  1872. (make-variable-buffer-local 'org-todo-key-alist)
  1873. (defvar org-todo-key-trigger nil)
  1874. (make-variable-buffer-local 'org-todo-key-trigger)
  1875. (defcustom org-todo-interpretation 'sequence
  1876. "Controls how TODO keywords are interpreted.
  1877. This variable is in principle obsolete and is only used for
  1878. backward compatibility, if the interpretation of todo keywords is
  1879. not given already in `org-todo-keywords'. See that variable for
  1880. more information."
  1881. :group 'org-todo
  1882. :group 'org-keywords
  1883. :type '(choice (const sequence)
  1884. (const type)))
  1885. (defcustom org-use-fast-todo-selection t
  1886. "Non-nil means use the fast todo selection scheme with C-c C-t.
  1887. This variable describes if and under what circumstances the cycling
  1888. mechanism for TODO keywords will be replaced by a single-key, direct
  1889. selection scheme.
  1890. When nil, fast selection is never used.
  1891. When the symbol `prefix', it will be used when `org-todo' is called with
  1892. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1893. in an agenda buffer.
  1894. When t, fast selection is used by default. In this case, the prefix
  1895. argument forces cycling instead.
  1896. In all cases, the special interface is only used if access keys have actually
  1897. been assigned by the user, i.e. if keywords in the configuration are followed
  1898. by a letter in parenthesis, like TODO(t)."
  1899. :group 'org-todo
  1900. :type '(choice
  1901. (const :tag "Never" nil)
  1902. (const :tag "By default" t)
  1903. (const :tag "Only with C-u C-c C-t" prefix)))
  1904. (defcustom org-provide-todo-statistics t
  1905. "Non-nil means update todo statistics after insert and toggle.
  1906. ALL-HEADLINES means update todo statistics by including headlines
  1907. with no TODO keyword as well, counting them as not done.
  1908. A list of TODO keywords means the same, but skip keywords that are
  1909. not in this list.
  1910. When this is set, todo statistics is updated in the parent of the
  1911. current entry each time a todo state is changed."
  1912. :group 'org-todo
  1913. :type '(choice
  1914. (const :tag "Yes, only for TODO entries" t)
  1915. (const :tag "Yes, including all entries" 'all-headlines)
  1916. (repeat :tag "Yes, for TODOs in this list"
  1917. (string :tag "TODO keyword"))
  1918. (other :tag "No TODO statistics" nil)))
  1919. (defcustom org-hierarchical-todo-statistics t
  1920. "Non-nil means TODO statistics covers just direct children.
  1921. When nil, all entries in the subtree are considered.
  1922. This has only an effect if `org-provide-todo-statistics' is set.
  1923. To set this to nil for only a single subtree, use a COOKIE_DATA
  1924. property and include the word \"recursive\" into the value."
  1925. :group 'org-todo
  1926. :type 'boolean)
  1927. (defcustom org-after-todo-state-change-hook nil
  1928. "Hook which is run after the state of a TODO item was changed.
  1929. The new state (a string with a TODO keyword, or nil) is available in the
  1930. Lisp variable `state'."
  1931. :group 'org-todo
  1932. :type 'hook)
  1933. (defvar org-blocker-hook nil
  1934. "Hook for functions that are allowed to block a state change.
  1935. Each function gets as its single argument a property list, see
  1936. `org-trigger-hook' for more information about this list.
  1937. If any of the functions in this hook returns nil, the state change
  1938. is blocked.")
  1939. (defvar org-trigger-hook nil
  1940. "Hook for functions that are triggered by a state change.
  1941. Each function gets as its single argument a property list with at least
  1942. the following elements:
  1943. (:type type-of-change :position pos-at-entry-start
  1944. :from old-state :to new-state)
  1945. Depending on the type, more properties may be present.
  1946. This mechanism is currently implemented for:
  1947. TODO state changes
  1948. ------------------
  1949. :type todo-state-change
  1950. :from previous state (keyword as a string), or nil, or a symbol
  1951. 'todo' or 'done', to indicate the general type of state.
  1952. :to new state, like in :from")
  1953. (defcustom org-enforce-todo-dependencies nil
  1954. "Non-nil means undone TODO entries will block switching the parent to DONE.
  1955. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  1956. be blocked if any prior sibling is not yet done.
  1957. Finally, if the parent is blocked because of ordered siblings of its own,
  1958. the child will also be blocked.
  1959. This variable needs to be set before org.el is loaded, and you need to
  1960. restart Emacs after a change to make the change effective. The only way
  1961. to change is while Emacs is running is through the customize interface."
  1962. :set (lambda (var val)
  1963. (set var val)
  1964. (if val
  1965. (add-hook 'org-blocker-hook
  1966. 'org-block-todo-from-children-or-siblings-or-parent)
  1967. (remove-hook 'org-blocker-hook
  1968. 'org-block-todo-from-children-or-siblings-or-parent)))
  1969. :group 'org-todo
  1970. :type 'boolean)
  1971. (defcustom org-enforce-todo-checkbox-dependencies nil
  1972. "Non-nil means unchecked boxes will block switching the parent to DONE.
  1973. When this is nil, checkboxes have no influence on switching TODO states.
  1974. When non-nil, you first need to check off all check boxes before the TODO
  1975. entry can be switched to DONE.
  1976. This variable needs to be set before org.el is loaded, and you need to
  1977. restart Emacs after a change to make the change effective. The only way
  1978. to change is while Emacs is running is through the customize interface."
  1979. :set (lambda (var val)
  1980. (set var val)
  1981. (if val
  1982. (add-hook 'org-blocker-hook
  1983. 'org-block-todo-from-checkboxes)
  1984. (remove-hook 'org-blocker-hook
  1985. 'org-block-todo-from-checkboxes)))
  1986. :group 'org-todo
  1987. :type 'boolean)
  1988. (defcustom org-treat-insert-todo-heading-as-state-change nil
  1989. "Non-nil means inserting a TODO heading is treated as state change.
  1990. So when the command \\[org-insert-todo-heading] is used, state change
  1991. logging will apply if appropriate. When nil, the new TODO item will
  1992. be inserted directly, and no logging will take place."
  1993. :group 'org-todo
  1994. :type 'boolean)
  1995. (defcustom org-treat-S-cursor-todo-selection-as-state-change t
  1996. "Non-nil means switching TODO states with S-cursor counts as state change.
  1997. This is the default behavior. However, setting this to nil allows a
  1998. convenient way to select a TODO state and bypass any logging associated
  1999. with that."
  2000. :group 'org-todo
  2001. :type 'boolean)
  2002. (defcustom org-todo-state-tags-triggers nil
  2003. "Tag changes that should be triggered by TODO state changes.
  2004. This is a list. Each entry is
  2005. (state-change (tag . flag) .......)
  2006. State-change can be a string with a state, and empty string to indicate the
  2007. state that has no TODO keyword, or it can be one of the symbols `todo'
  2008. or `done', meaning any not-done or done state, respectively."
  2009. :group 'org-todo
  2010. :group 'org-tags
  2011. :type '(repeat
  2012. (cons (choice :tag "When changing to"
  2013. (const :tag "Not-done state" todo)
  2014. (const :tag "Done state" done)
  2015. (string :tag "State"))
  2016. (repeat
  2017. (cons :tag "Tag action"
  2018. (string :tag "Tag")
  2019. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  2020. (defcustom org-log-done nil
  2021. "Information to record when a task moves to the DONE state.
  2022. Possible values are:
  2023. nil Don't add anything, just change the keyword
  2024. time Add a time stamp to the task
  2025. note Prompt for a note and add it with template `org-log-note-headings'
  2026. This option can also be set with on a per-file-basis with
  2027. #+STARTUP: nologdone
  2028. #+STARTUP: logdone
  2029. #+STARTUP: lognotedone
  2030. You can have local logging settings for a subtree by setting the LOGGING
  2031. property to one or more of these keywords."
  2032. :group 'org-todo
  2033. :group 'org-progress
  2034. :type '(choice
  2035. (const :tag "No logging" nil)
  2036. (const :tag "Record CLOSED timestamp" time)
  2037. (const :tag "Record CLOSED timestamp with note." note)))
  2038. ;; Normalize old uses of org-log-done.
  2039. (cond
  2040. ((eq org-log-done t) (setq org-log-done 'time))
  2041. ((and (listp org-log-done) (memq 'done org-log-done))
  2042. (setq org-log-done 'note)))
  2043. (defcustom org-log-reschedule nil
  2044. "Information to record when the scheduling date of a tasks is modified.
  2045. Possible values are:
  2046. nil Don't add anything, just change the date
  2047. time Add a time stamp to the task
  2048. note Prompt for a note and add it with template `org-log-note-headings'
  2049. This option can also be set with on a per-file-basis with
  2050. #+STARTUP: nologreschedule
  2051. #+STARTUP: logreschedule
  2052. #+STARTUP: lognotereschedule"
  2053. :group 'org-todo
  2054. :group 'org-progress
  2055. :type '(choice
  2056. (const :tag "No logging" nil)
  2057. (const :tag "Record timestamp" time)
  2058. (const :tag "Record timestamp with note." note)))
  2059. (defcustom org-log-redeadline nil
  2060. "Information to record when the deadline date of a tasks is modified.
  2061. Possible values are:
  2062. nil Don't add anything, just change the date
  2063. time Add a time stamp to the task
  2064. note Prompt for a note and add it with template `org-log-note-headings'
  2065. This option can also be set with on a per-file-basis with
  2066. #+STARTUP: nologredeadline
  2067. #+STARTUP: logredeadline
  2068. #+STARTUP: lognoteredeadline
  2069. You can have local logging settings for a subtree by setting the LOGGING
  2070. property to one or more of these keywords."
  2071. :group 'org-todo
  2072. :group 'org-progress
  2073. :type '(choice
  2074. (const :tag "No logging" nil)
  2075. (const :tag "Record timestamp" time)
  2076. (const :tag "Record timestamp with note." note)))
  2077. (defcustom org-log-note-clock-out nil
  2078. "Non-nil means record a note when clocking out of an item.
  2079. This can also be configured on a per-file basis by adding one of
  2080. the following lines anywhere in the buffer:
  2081. #+STARTUP: lognoteclock-out
  2082. #+STARTUP: nolognoteclock-out"
  2083. :group 'org-todo
  2084. :group 'org-progress
  2085. :type 'boolean)
  2086. (defcustom org-log-done-with-time t
  2087. "Non-nil means the CLOSED time stamp will contain date and time.
  2088. When nil, only the date will be recorded."
  2089. :group 'org-progress
  2090. :type 'boolean)
  2091. (defcustom org-log-note-headings
  2092. '((done . "CLOSING NOTE %t")
  2093. (state . "State %-12s from %-12S %t")
  2094. (note . "Note taken on %t")
  2095. (reschedule . "Rescheduled from %S on %t")
  2096. (delschedule . "Not scheduled, was %S on %t")
  2097. (redeadline . "New deadline from %S on %t")
  2098. (deldeadline . "Removed deadline, was %S on %t")
  2099. (refile . "Refiled on %t")
  2100. (clock-out . ""))
  2101. "Headings for notes added to entries.
  2102. The value is an alist, with the car being a symbol indicating the note
  2103. context, and the cdr is the heading to be used. The heading may also be the
  2104. empty string.
  2105. %t in the heading will be replaced by a time stamp.
  2106. %T will be an active time stamp instead the default inactive one
  2107. %d will be replaced by a short-format time stamp.
  2108. %D will be replaced by an active short-format time stamp.
  2109. %s will be replaced by the new TODO state, in double quotes.
  2110. %S will be replaced by the old TODO state, in double quotes.
  2111. %u will be replaced by the user name.
  2112. %U will be replaced by the full user name.
  2113. In fact, it is not a good idea to change the `state' entry, because
  2114. agenda log mode depends on the format of these entries."
  2115. :group 'org-todo
  2116. :group 'org-progress
  2117. :type '(list :greedy t
  2118. (cons (const :tag "Heading when closing an item" done) string)
  2119. (cons (const :tag
  2120. "Heading when changing todo state (todo sequence only)"
  2121. state) string)
  2122. (cons (const :tag "Heading when just taking a note" note) string)
  2123. (cons (const :tag "Heading when clocking out" clock-out) string)
  2124. (cons (const :tag "Heading when an item is no longer scheduled" delschedule) string)
  2125. (cons (const :tag "Heading when rescheduling" reschedule) string)
  2126. (cons (const :tag "Heading when changing deadline" redeadline) string)
  2127. (cons (const :tag "Heading when deleting a deadline" deldeadline) string)
  2128. (cons (const :tag "Heading when refiling" refile) string)))
  2129. (unless (assq 'note org-log-note-headings)
  2130. (push '(note . "%t") org-log-note-headings))
  2131. (defcustom org-log-into-drawer nil
  2132. "Non-nil means insert state change notes and time stamps into a drawer.
  2133. When nil, state changes notes will be inserted after the headline and
  2134. any scheduling and clock lines, but not inside a drawer.
  2135. The value of this variable should be the name of the drawer to use.
  2136. LOGBOOK is proposed as the default drawer for this purpose, you can
  2137. also set this to a string to define the drawer of your choice.
  2138. A value of t is also allowed, representing \"LOGBOOK\".
  2139. If this variable is set, `org-log-state-notes-insert-after-drawers'
  2140. will be ignored.
  2141. You can set the property LOG_INTO_DRAWER to overrule this setting for
  2142. a subtree."
  2143. :group 'org-todo
  2144. :group 'org-progress
  2145. :type '(choice
  2146. (const :tag "Not into a drawer" nil)
  2147. (const :tag "LOGBOOK" t)
  2148. (string :tag "Other")))
  2149. (if (fboundp 'defvaralias)
  2150. (defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer))
  2151. (defun org-log-into-drawer ()
  2152. "Return the value of `org-log-into-drawer', but let properties overrule.
  2153. If the current entry has or inherits a LOG_INTO_DRAWER property, it will be
  2154. used instead of the default value."
  2155. (let ((p (org-entry-get nil "LOG_INTO_DRAWER" 'inherit)))
  2156. (cond
  2157. ((or (not p) (equal p "nil")) org-log-into-drawer)
  2158. ((equal p "t") "LOGBOOK")
  2159. (t p))))
  2160. (defcustom org-log-state-notes-insert-after-drawers nil
  2161. "Non-nil means insert state change notes after any drawers in entry.
  2162. Only the drawers that *immediately* follow the headline and the
  2163. deadline/scheduled line are skipped.
  2164. When nil, insert notes right after the heading and perhaps the line
  2165. with deadline/scheduling if present.
  2166. This variable will have no effect if `org-log-into-drawer' is
  2167. set."
  2168. :group 'org-todo
  2169. :group 'org-progress
  2170. :type 'boolean)
  2171. (defcustom org-log-states-order-reversed t
  2172. "Non-nil means the latest state note will be directly after heading.
  2173. When nil, the state change notes will be ordered according to time."
  2174. :group 'org-todo
  2175. :group 'org-progress
  2176. :type 'boolean)
  2177. (defcustom org-todo-repeat-to-state nil
  2178. "The TODO state to which a repeater should return the repeating task.
  2179. By default this is the first task in a TODO sequence, or the previous state
  2180. in a TODO_TYP set. But you can specify another task here.
  2181. alternatively, set the :REPEAT_TO_STATE: property of the entry."
  2182. :group 'org-todo
  2183. :type '(choice (const :tag "Head of sequence" nil)
  2184. (string :tag "Specific state")))
  2185. (defcustom org-log-repeat 'time
  2186. "Non-nil means record moving through the DONE state when triggering repeat.
  2187. An auto-repeating task is immediately switched back to TODO when
  2188. marked DONE. If you are not logging state changes (by adding \"@\"
  2189. or \"!\" to the TODO keyword definition), or set `org-log-done' to
  2190. record a closing note, there will be no record of the task moving
  2191. through DONE. This variable forces taking a note anyway.
  2192. nil Don't force a record
  2193. time Record a time stamp
  2194. note Record a note
  2195. This option can also be set with on a per-file-basis with
  2196. #+STARTUP: logrepeat
  2197. #+STARTUP: lognoterepeat
  2198. #+STARTUP: nologrepeat
  2199. You can have local logging settings for a subtree by setting the LOGGING
  2200. property to one or more of these keywords."
  2201. :group 'org-todo
  2202. :group 'org-progress
  2203. :type '(choice
  2204. (const :tag "Don't force a record" nil)
  2205. (const :tag "Force recording the DONE state" time)
  2206. (const :tag "Force recording a note with the DONE state" note)))
  2207. (defgroup org-priorities nil
  2208. "Priorities in Org-mode."
  2209. :tag "Org Priorities"
  2210. :group 'org-todo)
  2211. (defcustom org-enable-priority-commands t
  2212. "Non-nil means priority commands are active.
  2213. When nil, these commands will be disabled, so that you never accidentally
  2214. set a priority."
  2215. :group 'org-priorities
  2216. :type 'boolean)
  2217. (defcustom org-highest-priority ?A
  2218. "The highest priority of TODO items. A character like ?A, ?B etc.
  2219. Must have a smaller ASCII number than `org-lowest-priority'."
  2220. :group 'org-priorities
  2221. :type 'character)
  2222. (defcustom org-lowest-priority ?C
  2223. "The lowest priority of TODO items. A character like ?A, ?B etc.
  2224. Must have a larger ASCII number than `org-highest-priority'."
  2225. :group 'org-priorities
  2226. :type 'character)
  2227. (defcustom org-default-priority ?B
  2228. "The default priority of TODO items.
  2229. This is the priority an item gets if no explicit priority is given.
  2230. When starting to cycle on an empty priority the first step in the cycle
  2231. depends on `org-priority-start-cycle-with-default'. The resulting first
  2232. step priority must not exceed the range from `org-highest-priority' to
  2233. `org-lowest-priority' which means that `org-default-priority' has to be
  2234. in this range exclusive or inclusive the range boundaries. Else the
  2235. first step refuses to set the default and the second will fall back
  2236. to (depending on the command used) the highest or lowest priority."
  2237. :group 'org-priorities
  2238. :type 'character)
  2239. (defcustom org-priority-start-cycle-with-default t
  2240. "Non-nil means start with default priority when starting to cycle.
  2241. When this is nil, the first step in the cycle will be (depending on the
  2242. command used) one higher or lower than the default priority.
  2243. See also `org-default-priority'."
  2244. :group 'org-priorities
  2245. :type 'boolean)
  2246. (defcustom org-get-priority-function nil
  2247. "Function to extract the priority from a string.
  2248. The string is normally the headline. If this is nil Org computes the
  2249. priority from the priority cookie like [#A] in the headline. It returns
  2250. an integer, increasing by 1000 for each priority level.
  2251. The user can set a different function here, which should take a string
  2252. as an argument and return the numeric priority."
  2253. :group 'org-priorities
  2254. :type 'function)
  2255. (defgroup org-time nil
  2256. "Options concerning time stamps and deadlines in Org-mode."
  2257. :tag "Org Time"
  2258. :group 'org)
  2259. (defcustom org-insert-labeled-timestamps-at-point nil
  2260. "Non-nil means SCHEDULED and DEADLINE timestamps are inserted at point.
  2261. When nil, these labeled time stamps are forces into the second line of an
  2262. entry, just after the headline. When scheduling from the global TODO list,
  2263. the time stamp will always be forced into the second line."
  2264. :group 'org-time
  2265. :type 'boolean)
  2266. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  2267. "Formats for `format-time-string' which are used for time stamps.
  2268. It is not recommended to change this constant.")
  2269. (defcustom org-time-stamp-rounding-minutes '(0 5)
  2270. "Number of minutes to round time stamps to.
  2271. These are two values, the first applies when first creating a time stamp.
  2272. The second applies when changing it with the commands `S-up' and `S-down'.
  2273. When changing the time stamp, this means that it will change in steps
  2274. of N minutes, as given by the second value.
  2275. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  2276. numbers should be factors of 60, so for example 5, 10, 15.
  2277. When this is larger than 1, you can still force an exact time stamp by using
  2278. a double prefix argument to a time stamp command like `C-c .' or `C-c !',
  2279. and by using a prefix arg to `S-up/down' to specify the exact number
  2280. of minutes to shift."
  2281. :group 'org-time
  2282. :get #'(lambda (var) ; Make sure both elements are there
  2283. (if (integerp (default-value var))
  2284. (list (default-value var) 5)
  2285. (default-value var)))
  2286. :type '(list
  2287. (integer :tag "when inserting times")
  2288. (integer :tag "when modifying times")))
  2289. ;; Normalize old customizations of this variable.
  2290. (when (integerp org-time-stamp-rounding-minutes)
  2291. (setq org-time-stamp-rounding-minutes
  2292. (list org-time-stamp-rounding-minutes
  2293. org-time-stamp-rounding-minutes)))
  2294. (defcustom org-display-custom-times nil
  2295. "Non-nil means overlay custom formats over all time stamps.
  2296. The formats are defined through the variable `org-time-stamp-custom-formats'.
  2297. To turn this on on a per-file basis, insert anywhere in the file:
  2298. #+STARTUP: customtime"
  2299. :group 'org-time
  2300. :set 'set-default
  2301. :type 'sexp)
  2302. (make-variable-buffer-local 'org-display-custom-times)
  2303. (defcustom org-time-stamp-custom-formats
  2304. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  2305. "Custom formats for time stamps. See `format-time-string' for the syntax.
  2306. These are overlayed over the default ISO format if the variable
  2307. `org-display-custom-times' is set. Time like %H:%M should be at the
  2308. end of the second format. The custom formats are also honored by export
  2309. commands, if custom time display is turned on at the time of export."
  2310. :group 'org-time
  2311. :type 'sexp)
  2312. (defun org-time-stamp-format (&optional long inactive)
  2313. "Get the right format for a time string."
  2314. (let ((f (if long (cdr org-time-stamp-formats)
  2315. (car org-time-stamp-formats))))
  2316. (if inactive
  2317. (concat "[" (substring f 1 -1) "]")
  2318. f)))
  2319. (defcustom org-time-clocksum-format "%d:%02d"
  2320. "The format string used when creating CLOCKSUM lines.
  2321. This is also used when org-mode generates a time duration."
  2322. :group 'org-time
  2323. :type 'string)
  2324. (defcustom org-time-clocksum-use-fractional nil
  2325. "If non-nil, \\[org-clock-display] uses fractional times.
  2326. org-mode generates a time duration."
  2327. :group 'org-time
  2328. :type 'boolean)
  2329. (defcustom org-time-clocksum-fractional-format "%.2f"
  2330. "The format string used when creating CLOCKSUM lines, or when
  2331. org-mode generates a time duration."
  2332. :group 'org-time
  2333. :type 'string)
  2334. (defcustom org-deadline-warning-days 14
  2335. "No. of days before expiration during which a deadline becomes active.
  2336. This variable governs the display in sparse trees and in the agenda.
  2337. When 0 or negative, it means use this number (the absolute value of it)
  2338. even if a deadline has a different individual lead time specified.
  2339. Custom commands can set this variable in the options section."
  2340. :group 'org-time
  2341. :group 'org-agenda-daily/weekly
  2342. :type 'integer)
  2343. (defcustom org-read-date-prefer-future t
  2344. "Non-nil means assume future for incomplete date input from user.
  2345. This affects the following situations:
  2346. 1. The user gives a month but not a year.
  2347. For example, if it is April and you enter \"feb 2\", this will be read
  2348. as Feb 2, *next* year. \"May 5\", however, will be this year.
  2349. 2. The user gives a day, but no month.
  2350. For example, if today is the 15th, and you enter \"3\", Org-mode will
  2351. read this as the third of *next* month. However, if you enter \"17\",
  2352. it will be considered as *this* month.
  2353. If you set this variable to the symbol `time', then also the following
  2354. will work:
  2355. 3. If the user gives a time, but no day. If the time is before now,
  2356. to will be interpreted as tomorrow.
  2357. Currently none of this works for ISO week specifications.
  2358. When this option is nil, the current day, month and year will always be
  2359. used as defaults.
  2360. See also `org-agenda-jump-prefer-future'."
  2361. :group 'org-time
  2362. :type '(choice
  2363. (const :tag "Never" nil)
  2364. (const :tag "Check month and day" t)
  2365. (const :tag "Check month, day, and time" time)))
  2366. (defcustom org-agenda-jump-prefer-future 'org-read-date-prefer-future
  2367. "Should the agenda jump command prefer the future for incomplete dates?
  2368. The default is to do the same as configured in `org-read-date-prefer-future'.
  2369. But you can also set a deviating value here.
  2370. This may t or nil, or the symbol `org-read-date-prefer-future'."
  2371. :group 'org-agenda
  2372. :group 'org-time
  2373. :type '(choice
  2374. (const :tag "Use org-read-date-prefer-future"
  2375. org-read-date-prefer-future)
  2376. (const :tag "Never" nil)
  2377. (const :tag "Always" t)))
  2378. (defcustom org-read-date-force-compatible-dates t
  2379. "Should date/time prompt force dates that are guaranteed to work in Emacs?
  2380. Depending on the system Emacs is running on, certain dates cannot
  2381. be represented with the type used internally to represent time.
  2382. Dates between 1970-1-1 and 2038-1-1 can always be represented
  2383. correctly. Some systems allow for earlier dates, some for later,
  2384. some for both. One way to find out it to insert any date into an
  2385. Org buffer, putting the cursor on the year and hitting S-up and
  2386. S-down to test the range.
  2387. When this variable is set to t, the date/time prompt will not let
  2388. you specify dates outside the 1970-2037 range, so it is certain that
  2389. these dates will work in whatever version of Emacs you are
  2390. running, and also that you can move a file from one Emacs implementation
  2391. to another. WHenever Org is forcing the year for you, it will display
  2392. a message and beep.
  2393. When this variable is nil, Org will check if the date is
  2394. representable in the specific Emacs implementation you are using.
  2395. If not, it will force a year, usually the current year, and beep
  2396. to remind you. Currently this setting is not recommended because
  2397. the likelihood that you will open your Org files in an Emacs that
  2398. has limited date range is not negligible.
  2399. A workaround for this problem is to use diary sexp dates for time
  2400. stamps outside of this range."
  2401. :group 'org-time
  2402. :type 'boolean)
  2403. (defcustom org-read-date-display-live t
  2404. "Non-nil means display current interpretation of date prompt live.
  2405. This display will be in an overlay, in the minibuffer."
  2406. :group 'org-time
  2407. :type 'boolean)
  2408. (defcustom org-read-date-popup-calendar t
  2409. "Non-nil means pop up a calendar when prompting for a date.
  2410. In the calendar, the date can be selected with mouse-1. However, the
  2411. minibuffer will also be active, and you can simply enter the date as well.
  2412. When nil, only the minibuffer will be available."
  2413. :group 'org-time
  2414. :type 'boolean)
  2415. (if (fboundp 'defvaralias)
  2416. (defvaralias 'org-popup-calendar-for-date-prompt
  2417. 'org-read-date-popup-calendar))
  2418. (defcustom org-read-date-minibuffer-setup-hook nil
  2419. "Hook to be used to set up keys for the date/time interface.
  2420. Add key definitions to `minibuffer-local-map', which will be a temporary
  2421. copy."
  2422. :group 'org-time
  2423. :type 'hook)
  2424. (defcustom org-extend-today-until 0
  2425. "The hour when your day really ends. Must be an integer.
  2426. This has influence for the following applications:
  2427. - When switching the agenda to \"today\". It it is still earlier than
  2428. the time given here, the day recognized as TODAY is actually yesterday.
  2429. - When a date is read from the user and it is still before the time given
  2430. here, the current date and time will be assumed to be yesterday, 23:59.
  2431. Also, timestamps inserted in remember templates follow this rule.
  2432. IMPORTANT: This is a feature whose implementation is and likely will
  2433. remain incomplete. Really, it is only here because past midnight seems to
  2434. be the favorite working time of John Wiegley :-)"
  2435. :group 'org-time
  2436. :type 'integer)
  2437. (defcustom org-use-effective-time nil
  2438. "If non-nil, consider `org-extend-today-until' when creating timestamps.
  2439. For example, if `org-extend-today-until' is 8, and it's 4am, then the
  2440. \"effective time\" of any timestamps between midnight and 8am will be
  2441. 23:59 of the previous day."
  2442. :group 'boolean
  2443. :type 'integer)
  2444. (defcustom org-edit-timestamp-down-means-later nil
  2445. "Non-nil means S-down will increase the time in a time stamp.
  2446. When nil, S-up will increase."
  2447. :group 'org-time
  2448. :type 'boolean)
  2449. (defcustom org-calendar-follow-timestamp-change t
  2450. "Non-nil means make the calendar window follow timestamp changes.
  2451. When a timestamp is modified and the calendar window is visible, it will be
  2452. moved to the new date."
  2453. :group 'org-time
  2454. :type 'boolean)
  2455. (defgroup org-tags nil
  2456. "Options concerning tags in Org-mode."
  2457. :tag "Org Tags"
  2458. :group 'org)
  2459. (defcustom org-tag-alist nil
  2460. "List of tags allowed in Org-mode files.
  2461. When this list is nil, Org-mode will base TAG input on what is already in the
  2462. buffer.
  2463. The value of this variable is an alist, the car of each entry must be a
  2464. keyword as a string, the cdr may be a character that is used to select
  2465. that tag through the fast-tag-selection interface.
  2466. See the manual for details."
  2467. :group 'org-tags
  2468. :type '(repeat
  2469. (choice
  2470. (cons (string :tag "Tag name")
  2471. (character :tag "Access char"))
  2472. (list :tag "Start radio group"
  2473. (const :startgroup)
  2474. (option (string :tag "Group description")))
  2475. (list :tag "End radio group"
  2476. (const :endgroup)
  2477. (option (string :tag "Group description")))
  2478. (const :tag "New line" (:newline)))))
  2479. (defcustom org-tag-persistent-alist nil
  2480. "List of tags that will always appear in all Org-mode files.
  2481. This is in addition to any in buffer settings or customizations
  2482. of `org-tag-alist'.
  2483. When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
  2484. The value of this variable is an alist, the car of each entry must be a
  2485. keyword as a string, the cdr may be a character that is used to select
  2486. that tag through the fast-tag-selection interface.
  2487. See the manual for details.
  2488. To disable these tags on a per-file basis, insert anywhere in the file:
  2489. #+STARTUP: noptag"
  2490. :group 'org-tags
  2491. :type '(repeat
  2492. (choice
  2493. (cons (string :tag "Tag name")
  2494. (character :tag "Access char"))
  2495. (const :tag "Start radio group" (:startgroup))
  2496. (const :tag "End radio group" (:endgroup))
  2497. (const :tag "New line" (:newline)))))
  2498. (defcustom org-complete-tags-always-offer-all-agenda-tags nil
  2499. "If non-nil, always offer completion for all tags of all agenda files.
  2500. Instead of customizing this variable directly, you might want to
  2501. set it locally for capture buffers, because there no list of
  2502. tags in that file can be created dynamically (there are none).
  2503. (add-hook 'org-capture-mode-hook
  2504. (lambda ()
  2505. (set (make-local-variable
  2506. 'org-complete-tags-always-offer-all-agenda-tags)
  2507. t)))"
  2508. :group 'org-tags
  2509. :type 'boolean)
  2510. (defvar org-file-tags nil
  2511. "List of tags that can be inherited by all entries in the file.
  2512. The tags will be inherited if the variable `org-use-tag-inheritance'
  2513. says they should be.
  2514. This variable is populated from #+FILETAGS lines.")
  2515. (defcustom org-use-fast-tag-selection 'auto
  2516. "Non-nil means use fast tag selection scheme.
  2517. This is a special interface to select and deselect tags with single keys.
  2518. When nil, fast selection is never used.
  2519. When the symbol `auto', fast selection is used if and only if selection
  2520. characters for tags have been configured, either through the variable
  2521. `org-tag-alist' or through a #+TAGS line in the buffer.
  2522. When t, fast selection is always used and selection keys are assigned
  2523. automatically if necessary."
  2524. :group 'org-tags
  2525. :type '(choice
  2526. (const :tag "Always" t)
  2527. (const :tag "Never" nil)
  2528. (const :tag "When selection characters are configured" 'auto)))
  2529. (defcustom org-fast-tag-selection-single-key nil
  2530. "Non-nil means fast tag selection exits after first change.
  2531. When nil, you have to press RET to exit it.
  2532. During fast tag selection, you can toggle this flag with `C-c'.
  2533. This variable can also have the value `expert'. In this case, the window
  2534. displaying the tags menu is not even shown, until you press C-c again."
  2535. :group 'org-tags
  2536. :type '(choice
  2537. (const :tag "No" nil)
  2538. (const :tag "Yes" t)
  2539. (const :tag "Expert" expert)))
  2540. (defvar org-fast-tag-selection-include-todo nil
  2541. "Non-nil means fast tags selection interface will also offer TODO states.
  2542. This is an undocumented feature, you should not rely on it.")
  2543. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  2544. "The column to which tags should be indented in a headline.
  2545. If this number is positive, it specifies the column. If it is negative,
  2546. it means that the tags should be flushright to that column. For example,
  2547. -80 works well for a normal 80 character screen.
  2548. When 0, place tags directly after headline text, with only one space in
  2549. between."
  2550. :group 'org-tags
  2551. :type 'integer)
  2552. (defcustom org-auto-align-tags t
  2553. "Non-nil keeps tags aligned when modifying headlines.
  2554. Some operations (i.e. demoting) change the length of a headline and
  2555. therefore shift the tags around. With this option turned on, after
  2556. each such operation the tags are again aligned to `org-tags-column'."
  2557. :group 'org-tags
  2558. :type 'boolean)
  2559. (defcustom org-use-tag-inheritance t
  2560. "Non-nil means tags in levels apply also for sublevels.
  2561. When nil, only the tags directly given in a specific line apply there.
  2562. This may also be a list of tags that should be inherited, or a regexp that
  2563. matches tags that should be inherited. Additional control is possible
  2564. with the variable `org-tags-exclude-from-inheritance' which gives an
  2565. explicit list of tags to be excluded from inheritance., even if the value of
  2566. `org-use-tag-inheritance' would select it for inheritance.
  2567. If this option is t, a match early-on in a tree can lead to a large
  2568. number of matches in the subtree when constructing the agenda or creating
  2569. a sparse tree. If you only want to see the first match in a tree during
  2570. a search, check out the variable `org-tags-match-list-sublevels'."
  2571. :group 'org-tags
  2572. :type '(choice
  2573. (const :tag "Not" nil)
  2574. (const :tag "Always" t)
  2575. (repeat :tag "Specific tags" (string :tag "Tag"))
  2576. (regexp :tag "Tags matched by regexp")))
  2577. (defcustom org-tags-exclude-from-inheritance nil
  2578. "List of tags that should never be inherited.
  2579. This is a way to exclude a few tags from inheritance. For way to do
  2580. the opposite, to actively allow inheritance for selected tags,
  2581. see the variable `org-use-tag-inheritance'."
  2582. :group 'org-tags
  2583. :type '(repeat (string :tag "Tag")))
  2584. (defun org-tag-inherit-p (tag)
  2585. "Check if TAG is one that should be inherited."
  2586. (cond
  2587. ((member tag org-tags-exclude-from-inheritance) nil)
  2588. ((eq org-use-tag-inheritance t) t)
  2589. ((not org-use-tag-inheritance) nil)
  2590. ((stringp org-use-tag-inheritance)
  2591. (string-match org-use-tag-inheritance tag))
  2592. ((listp org-use-tag-inheritance)
  2593. (member tag org-use-tag-inheritance))
  2594. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  2595. (defcustom org-tags-match-list-sublevels t
  2596. "Non-nil means list also sublevels of headlines matching a search.
  2597. This variable applies to tags/property searches, and also to stuck
  2598. projects because this search is based on a tags match as well.
  2599. When set to the symbol `indented', sublevels are indented with
  2600. leading dots.
  2601. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  2602. the sublevels of a headline matching a tag search often also match
  2603. the same search. Listing all of them can create very long lists.
  2604. Setting this variable to nil causes subtrees of a match to be skipped.
  2605. This variable is semi-obsolete and probably should always be true. It
  2606. is better to limit inheritance to certain tags using the variables
  2607. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  2608. :group 'org-tags
  2609. :type '(choice
  2610. (const :tag "No, don't list them" nil)
  2611. (const :tag "Yes, do list them" t)
  2612. (const :tag "List them, indented with leading dots" indented)))
  2613. (defcustom org-tags-sort-function nil
  2614. "When set, tags are sorted using this function as a comparator."
  2615. :group 'org-tags
  2616. :type '(choice
  2617. (const :tag "No sorting" nil)
  2618. (const :tag "Alphabetical" string<)
  2619. (const :tag "Reverse alphabetical" string>)
  2620. (function :tag "Custom function" nil)))
  2621. (defvar org-tags-history nil
  2622. "History of minibuffer reads for tags.")
  2623. (defvar org-last-tags-completion-table nil
  2624. "The last used completion table for tags.")
  2625. (defvar org-after-tags-change-hook nil
  2626. "Hook that is run after the tags in a line have changed.")
  2627. (defgroup org-properties nil
  2628. "Options concerning properties in Org-mode."
  2629. :tag "Org Properties"
  2630. :group 'org)
  2631. (defcustom org-property-format "%-10s %s"
  2632. "How property key/value pairs should be formatted by `indent-line'.
  2633. When `indent-line' hits a property definition, it will format the line
  2634. according to this format, mainly to make sure that the values are
  2635. lined-up with respect to each other."
  2636. :group 'org-properties
  2637. :type 'string)
  2638. (defcustom org-properties-postprocess-alist nil
  2639. "Alist of properties and functions to adjust inserted values.
  2640. Elements of this alist must be of the form
  2641. ([string] [function])
  2642. where [string] must be a property name and [function] must be a
  2643. lambda expression: this lambda expression must take one argument,
  2644. the value to adjust, and return the new value as a string.
  2645. For example, this element will allow the property \"Remaining\"
  2646. to be updated wrt the relation between the \"Effort\" property
  2647. and the clock summary:
  2648. ((\"Remaining\" (lambda(value)
  2649. (let ((clocksum (org-clock-sum-current-item))
  2650. (effort (org-duration-string-to-minutes
  2651. (org-entry-get (point) \"Effort\"))))
  2652. (org-minutes-to-hh:mm-string (- effort clocksum))))))"
  2653. :group 'org-properties
  2654. :type 'alist)
  2655. (defcustom org-use-property-inheritance nil
  2656. "Non-nil means properties apply also for sublevels.
  2657. This setting is chiefly used during property searches. Turning it on can
  2658. cause significant overhead when doing a search, which is why it is not
  2659. on by default.
  2660. When nil, only the properties directly given in the current entry count.
  2661. When t, every property is inherited. The value may also be a list of
  2662. properties that should have inheritance, or a regular expression matching
  2663. properties that should be inherited.
  2664. However, note that some special properties use inheritance under special
  2665. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  2666. and the properties ending in \"_ALL\" when they are used as descriptor
  2667. for valid values of a property.
  2668. Note for programmers:
  2669. When querying an entry with `org-entry-get', you can control if inheritance
  2670. should be used. By default, `org-entry-get' looks only at the local
  2671. properties. You can request inheritance by setting the inherit argument
  2672. to t (to force inheritance) or to `selective' (to respect the setting
  2673. in this variable)."
  2674. :group 'org-properties
  2675. :type '(choice
  2676. (const :tag "Not" nil)
  2677. (const :tag "Always" t)
  2678. (repeat :tag "Specific properties" (string :tag "Property"))
  2679. (regexp :tag "Properties matched by regexp")))
  2680. (defun org-property-inherit-p (property)
  2681. "Check if PROPERTY is one that should be inherited."
  2682. (cond
  2683. ((eq org-use-property-inheritance t) t)
  2684. ((not org-use-property-inheritance) nil)
  2685. ((stringp org-use-property-inheritance)
  2686. (string-match org-use-property-inheritance property))
  2687. ((listp org-use-property-inheritance)
  2688. (member property org-use-property-inheritance))
  2689. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  2690. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  2691. "The default column format, if no other format has been defined.
  2692. This variable can be set on the per-file basis by inserting a line
  2693. #+COLUMNS: %25ITEM ....."
  2694. :group 'org-properties
  2695. :type 'string)
  2696. (defcustom org-columns-ellipses ".."
  2697. "The ellipses to be used when a field in column view is truncated.
  2698. When this is the empty string, as many characters as possible are shown,
  2699. but then there will be no visual indication that the field has been truncated.
  2700. When this is a string of length N, the last N characters of a truncated
  2701. field are replaced by this string. If the column is narrower than the
  2702. ellipses string, only part of the ellipses string will be shown."
  2703. :group 'org-properties
  2704. :type 'string)
  2705. (defcustom org-columns-modify-value-for-display-function nil
  2706. "Function that modifies values for display in column view.
  2707. For example, it can be used to cut out a certain part from a time stamp.
  2708. The function must take 2 arguments:
  2709. column-title The title of the column (*not* the property name)
  2710. value The value that should be modified.
  2711. The function should return the value that should be displayed,
  2712. or nil if the normal value should be used."
  2713. :group 'org-properties
  2714. :type 'function)
  2715. (defcustom org-effort-property "Effort"
  2716. "The property that is being used to keep track of effort estimates.
  2717. Effort estimates given in this property need to have the format H:MM."
  2718. :group 'org-properties
  2719. :group 'org-progress
  2720. :type '(string :tag "Property"))
  2721. (defconst org-global-properties-fixed
  2722. '(("VISIBILITY_ALL" . "folded children content all")
  2723. ("CLOCK_MODELINE_TOTAL_ALL" . "current today repeat all auto"))
  2724. "List of property/value pairs that can be inherited by any entry.
  2725. These are fixed values, for the preset properties. The user variable
  2726. that can be used to add to this list is `org-global-properties'.
  2727. The entries in this list are cons cells where the car is a property
  2728. name and cdr is a string with the value. If the value represents
  2729. multiple items like an \"_ALL\" property, separate the items by
  2730. spaces.")
  2731. (defcustom org-global-properties nil
  2732. "List of property/value pairs that can be inherited by any entry.
  2733. This list will be combined with the constant `org-global-properties-fixed'.
  2734. The entries in this list are cons cells where the car is a property
  2735. name and cdr is a string with the value.
  2736. You can set buffer-local values for the same purpose in the variable
  2737. `org-file-properties' this by adding lines like
  2738. #+PROPERTY: NAME VALUE"
  2739. :group 'org-properties
  2740. :type '(repeat
  2741. (cons (string :tag "Property")
  2742. (string :tag "Value"))))
  2743. (defvar org-file-properties nil
  2744. "List of property/value pairs that can be inherited by any entry.
  2745. Valid for the current buffer.
  2746. This variable is populated from #+PROPERTY lines.")
  2747. (make-variable-buffer-local 'org-file-properties)
  2748. (defgroup org-agenda nil
  2749. "Options concerning agenda views in Org-mode."
  2750. :tag "Org Agenda"
  2751. :group 'org)
  2752. (defvar org-category nil
  2753. "Variable used by org files to set a category for agenda display.
  2754. Such files should use a file variable to set it, for example
  2755. # -*- mode: org; org-category: \"ELisp\"
  2756. or contain a special line
  2757. #+CATEGORY: ELisp
  2758. If the file does not specify a category, then file's base name
  2759. is used instead.")
  2760. (make-variable-buffer-local 'org-category)
  2761. (put 'org-category 'safe-local-variable #'(lambda (x) (or (symbolp x) (stringp x))))
  2762. (defcustom org-agenda-files nil
  2763. "The files to be used for agenda display.
  2764. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  2765. \\[org-remove-file]. You can also use customize to edit the list.
  2766. If an entry is a directory, all files in that directory that are matched by
  2767. `org-agenda-file-regexp' will be part of the file list.
  2768. If the value of the variable is not a list but a single file name, then
  2769. the list of agenda files is actually stored and maintained in that file, one
  2770. agenda file per line. In this file paths can be given relative to
  2771. `org-directory'. Tilde expansion and environment variable substitution
  2772. are also made."
  2773. :group 'org-agenda
  2774. :type '(choice
  2775. (repeat :tag "List of files and directories" file)
  2776. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  2777. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  2778. "Regular expression to match files for `org-agenda-files'.
  2779. If any element in the list in that variable contains a directory instead
  2780. of a normal file, all files in that directory that are matched by this
  2781. regular expression will be included."
  2782. :group 'org-agenda
  2783. :type 'regexp)
  2784. (defcustom org-agenda-text-search-extra-files nil
  2785. "List of extra files to be searched by text search commands.
  2786. These files will be search in addition to the agenda files by the
  2787. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  2788. Note that these files will only be searched for text search commands,
  2789. not for the other agenda views like todo lists, tag searches or the weekly
  2790. agenda. This variable is intended to list notes and possibly archive files
  2791. that should also be searched by these two commands.
  2792. In fact, if the first element in the list is the symbol `agenda-archives',
  2793. than all archive files of all agenda files will be added to the search
  2794. scope."
  2795. :group 'org-agenda
  2796. :type '(set :greedy t
  2797. (const :tag "Agenda Archives" agenda-archives)
  2798. (repeat :inline t (file))))
  2799. (if (fboundp 'defvaralias)
  2800. (defvaralias 'org-agenda-multi-occur-extra-files
  2801. 'org-agenda-text-search-extra-files))
  2802. (defcustom org-agenda-skip-unavailable-files nil
  2803. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  2804. A nil value means to remove them, after a query, from the list."
  2805. :group 'org-agenda
  2806. :type 'boolean)
  2807. (defcustom org-calendar-to-agenda-key [?c]
  2808. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  2809. The command `org-calendar-goto-agenda' will be bound to this key. The
  2810. default is the character `c' because then `c' can be used to switch back and
  2811. forth between agenda and calendar."
  2812. :group 'org-agenda
  2813. :type 'sexp)
  2814. (defcustom org-calendar-agenda-action-key [?k]
  2815. "The key to be installed in `calendar-mode-map' for agenda-action.
  2816. The command `org-agenda-action' will be bound to this key. The
  2817. default is the character `k' because we use the same key in the agenda."
  2818. :group 'org-agenda
  2819. :type 'sexp)
  2820. (defcustom org-calendar-insert-diary-entry-key [?i]
  2821. "The key to be installed in `calendar-mode-map' for adding diary entries.
  2822. This option is irrelevant until `org-agenda-diary-file' has been configured
  2823. to point to an Org-mode file. When that is the case, the command
  2824. `org-agenda-diary-entry' will be bound to the key given here, by default
  2825. `i'. In the calendar, `i' normally adds entries to `diary-file'. So
  2826. if you want to continue doing this, you need to change this to a different
  2827. key."
  2828. :group 'org-agenda
  2829. :type 'sexp)
  2830. (defcustom org-agenda-diary-file 'diary-file
  2831. "File to which to add new entries with the `i' key in agenda and calendar.
  2832. When this is the symbol `diary-file', the functionality in the Emacs
  2833. calendar will be used to add entries to the `diary-file'. But when this
  2834. points to a file, `org-agenda-diary-entry' will be used instead."
  2835. :group 'org-agenda
  2836. :type '(choice
  2837. (const :tag "The standard Emacs diary file" diary-file)
  2838. (file :tag "Special Org file diary entries")))
  2839. (eval-after-load "calendar"
  2840. '(progn
  2841. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  2842. 'org-calendar-goto-agenda)
  2843. (org-defkey calendar-mode-map org-calendar-agenda-action-key
  2844. 'org-agenda-action)
  2845. (add-hook 'calendar-mode-hook
  2846. (lambda ()
  2847. (unless (eq org-agenda-diary-file 'diary-file)
  2848. (define-key calendar-mode-map
  2849. org-calendar-insert-diary-entry-key
  2850. 'org-agenda-diary-entry))))))
  2851. (defgroup org-latex nil
  2852. "Options for embedding LaTeX code into Org-mode."
  2853. :tag "Org LaTeX"
  2854. :group 'org)
  2855. (defcustom org-format-latex-options
  2856. '(:foreground default :background default :scale 1.0
  2857. :html-foreground "Black" :html-background "Transparent"
  2858. :html-scale 1.0 :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  2859. "Options for creating images from LaTeX fragments.
  2860. This is a property list with the following properties:
  2861. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  2862. `default' means use the foreground of the default face.
  2863. :background the background color, or \"Transparent\".
  2864. `default' means use the background of the default face.
  2865. :scale a scaling factor for the size of the images, to get more pixels
  2866. :html-foreground, :html-background, :html-scale
  2867. the same numbers for HTML export.
  2868. :matchers a list indicating which matchers should be used to
  2869. find LaTeX fragments. Valid members of this list are:
  2870. \"begin\" find environments
  2871. \"$1\" find single characters surrounded by $.$
  2872. \"$\" find math expressions surrounded by $...$
  2873. \"$$\" find math expressions surrounded by $$....$$
  2874. \"\\(\" find math expressions surrounded by \\(...\\)
  2875. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  2876. :group 'org-latex
  2877. :type 'plist)
  2878. (defcustom org-format-latex-signal-error t
  2879. "Non-nil means signal an error when image creation of LaTeX snippets fails.
  2880. When nil, just push out a message."
  2881. :group 'org-latex
  2882. :type 'boolean)
  2883. (defcustom org-latex-to-mathml-jar-file nil
  2884. "Value of\"%j\" in `org-latex-to-mathml-convert-command'.
  2885. Use this to specify additional executable file say a jar file.
  2886. When using MathToWeb as the converter, specify the full-path to
  2887. your mathtoweb.jar file."
  2888. :group 'org-latex
  2889. :type '(choice
  2890. (const :tag "None" nil)
  2891. (file :tag "JAR file" :must-match t)))
  2892. (defcustom org-latex-to-mathml-convert-command nil
  2893. "Command to convert LaTeX fragments to MathML.
  2894. Replace format-specifiers in the command as noted below and use
  2895. `shell-command' to convert LaTeX to MathML.
  2896. %j: Executable file in fully expanded form as specified by
  2897. `org-latex-to-mathml-jar-file'.
  2898. %I: Input LaTeX file in fully expanded form
  2899. %o: Output MathML file
  2900. This command is used by `org-create-math-formula'.
  2901. When using MathToWeb as the converter, set this to
  2902. \"java -jar %j -unicode -force -df %o %I\"."
  2903. :group 'org-latex
  2904. :type '(choice
  2905. (const :tag "None" nil)
  2906. (string :tag "\nShell command")))
  2907. (defun org-format-latex-mathml-available-p ()
  2908. "Return t if `org-latex-to-mathml-convert-command' is usable."
  2909. (save-match-data
  2910. (when (and (boundp 'org-latex-to-mathml-convert-command)
  2911. org-latex-to-mathml-convert-command)
  2912. (let ((executable (car (split-string
  2913. org-latex-to-mathml-convert-command))))
  2914. (when (executable-find executable)
  2915. (if (string-match
  2916. "%j" org-latex-to-mathml-convert-command)
  2917. (file-readable-p org-latex-to-mathml-jar-file)
  2918. t))))))
  2919. (defcustom org-format-latex-header "\\documentclass{article}
  2920. \\usepackage[usenames]{color}
  2921. \\usepackage{amsmath}
  2922. \\usepackage[mathscr]{eucal}
  2923. \\pagestyle{empty} % do not remove
  2924. \[PACKAGES]
  2925. \[DEFAULT-PACKAGES]
  2926. % The settings below are copied from fullpage.sty
  2927. \\setlength{\\textwidth}{\\paperwidth}
  2928. \\addtolength{\\textwidth}{-3cm}
  2929. \\setlength{\\oddsidemargin}{1.5cm}
  2930. \\addtolength{\\oddsidemargin}{-2.54cm}
  2931. \\setlength{\\evensidemargin}{\\oddsidemargin}
  2932. \\setlength{\\textheight}{\\paperheight}
  2933. \\addtolength{\\textheight}{-\\headheight}
  2934. \\addtolength{\\textheight}{-\\headsep}
  2935. \\addtolength{\\textheight}{-\\footskip}
  2936. \\addtolength{\\textheight}{-3cm}
  2937. \\setlength{\\topmargin}{1.5cm}
  2938. \\addtolength{\\topmargin}{-2.54cm}"
  2939. "The document header used for processing LaTeX fragments.
  2940. It is imperative that this header make sure that no page number
  2941. appears on the page. The package defined in the variables
  2942. `org-export-latex-default-packages-alist' and `org-export-latex-packages-alist'
  2943. will either replace the placeholder \"[PACKAGES]\" in this header, or they
  2944. will be appended."
  2945. :group 'org-latex
  2946. :type 'string)
  2947. (defvar org-format-latex-header-extra nil)
  2948. (defun org-set-packages-alist (var val)
  2949. "Set the packages alist and make sure it has 3 elements per entry."
  2950. (set var (mapcar (lambda (x)
  2951. (if (and (consp x) (= (length x) 2))
  2952. (list (car x) (nth 1 x) t)
  2953. x))
  2954. val)))
  2955. (defun org-get-packages-alist (var)
  2956. "Get the packages alist and make sure it has 3 elements per entry."
  2957. (mapcar (lambda (x)
  2958. (if (and (consp x) (= (length x) 2))
  2959. (list (car x) (nth 1 x) t)
  2960. x))
  2961. (default-value var)))
  2962. ;; The following variables are defined here because is it also used
  2963. ;; when formatting latex fragments. Originally it was part of the
  2964. ;; LaTeX exporter, which is why the name includes "export".
  2965. (defcustom org-export-latex-default-packages-alist
  2966. '(("AUTO" "inputenc" t)
  2967. ("T1" "fontenc" t)
  2968. ("" "fixltx2e" nil)
  2969. ("" "graphicx" t)
  2970. ("" "longtable" nil)
  2971. ("" "float" nil)
  2972. ("" "wrapfig" nil)
  2973. ("" "soul" t)
  2974. ("" "textcomp" t)
  2975. ("" "marvosym" t)
  2976. ("" "wasysym" t)
  2977. ("" "latexsym" t)
  2978. ("" "amssymb" t)
  2979. ("" "hyperref" nil)
  2980. "\\tolerance=1000"
  2981. )
  2982. "Alist of default packages to be inserted in the header.
  2983. Change this only if one of the packages here causes an incompatibility
  2984. with another package you are using.
  2985. The packages in this list are needed by one part or another of Org-mode
  2986. to function properly.
  2987. - inputenc, fontenc: for basic font and character selection
  2988. - textcomp, marvosymb, wasysym, latexsym, amssym: for various symbols used
  2989. for interpreting the entities in `org-entities'. You can skip some of these
  2990. packages if you don't use any of the symbols in it.
  2991. - graphicx: for including images
  2992. - float, wrapfig: for figure placement
  2993. - longtable: for long tables
  2994. - hyperref: for cross references
  2995. Therefore you should not modify this variable unless you know what you
  2996. are doing. The one reason to change it anyway is that you might be loading
  2997. some other package that conflicts with one of the default packages.
  2998. Each cell is of the format \( \"options\" \"package\" snippet-flag\).
  2999. If SNIPPET-FLAG is t, the package also needs to be included when
  3000. compiling LaTeX snippets into images for inclusion into HTML."
  3001. :group 'org-export-latex
  3002. :set 'org-set-packages-alist
  3003. :get 'org-get-packages-alist
  3004. :type '(repeat
  3005. (choice
  3006. (list :tag "options/package pair"
  3007. (string :tag "options")
  3008. (string :tag "package")
  3009. (boolean :tag "Snippet"))
  3010. (string :tag "A line of LaTeX"))))
  3011. (defcustom org-export-latex-packages-alist nil
  3012. "Alist of packages to be inserted in every LaTeX header.
  3013. These will be inserted after `org-export-latex-default-packages-alist'.
  3014. Each cell is of the format \( \"options\" \"package\" snippet-flag \).
  3015. SNIPPET-FLAG, when t, indicates that this package is also needed when
  3016. turning LaTeX snippets into images for inclusion into HTML.
  3017. Make sure that you only list packages here which:
  3018. - you want in every file
  3019. - do not conflict with the default packages in
  3020. `org-export-latex-default-packages-alist'
  3021. - do not conflict with the setup in `org-format-latex-header'."
  3022. :group 'org-export-latex
  3023. :set 'org-set-packages-alist
  3024. :get 'org-get-packages-alist
  3025. :type '(repeat
  3026. (choice
  3027. (list :tag "options/package pair"
  3028. (string :tag "options")
  3029. (string :tag "package")
  3030. (boolean :tag "Snippet"))
  3031. (string :tag "A line of LaTeX"))))
  3032. (defgroup org-appearance nil
  3033. "Settings for Org-mode appearance."
  3034. :tag "Org Appearance"
  3035. :group 'org)
  3036. (defcustom org-level-color-stars-only nil
  3037. "Non-nil means fontify only the stars in each headline.
  3038. When nil, the entire headline is fontified.
  3039. Changing it requires restart of `font-lock-mode' to become effective
  3040. also in regions already fontified."
  3041. :group 'org-appearance
  3042. :type 'boolean)
  3043. (defcustom org-hide-leading-stars nil
  3044. "Non-nil means hide the first N-1 stars in a headline.
  3045. This works by using the face `org-hide' for these stars. This
  3046. face is white for a light background, and black for a dark
  3047. background. You may have to customize the face `org-hide' to
  3048. make this work.
  3049. Changing it requires restart of `font-lock-mode' to become effective
  3050. also in regions already fontified.
  3051. You may also set this on a per-file basis by adding one of the following
  3052. lines to the buffer:
  3053. #+STARTUP: hidestars
  3054. #+STARTUP: showstars"
  3055. :group 'org-appearance
  3056. :type 'boolean)
  3057. (defcustom org-hidden-keywords nil
  3058. "List of symbols corresponding to keywords to be hidden the org buffer.
  3059. For example, a value '(title) for this list will make the document's title
  3060. appear in the buffer without the initial #+TITLE: keyword."
  3061. :group 'org-appearance
  3062. :type '(set (const :tag "#+AUTHOR" author)
  3063. (const :tag "#+DATE" date)
  3064. (const :tag "#+EMAIL" email)
  3065. (const :tag "#+TITLE" title)))
  3066. (defcustom org-fontify-done-headline nil
  3067. "Non-nil means change the face of a headline if it is marked DONE.
  3068. Normally, only the TODO/DONE keyword indicates the state of a headline.
  3069. When this is non-nil, the headline after the keyword is set to the
  3070. `org-headline-done' as an additional indication."
  3071. :group 'org-appearance
  3072. :type 'boolean)
  3073. (defcustom org-fontify-emphasized-text t
  3074. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  3075. Changing this variable requires a restart of Emacs to take effect."
  3076. :group 'org-appearance
  3077. :type 'boolean)
  3078. (defcustom org-fontify-whole-heading-line nil
  3079. "Non-nil means fontify the whole line for headings.
  3080. This is useful when setting a background color for the
  3081. org-level-* faces."
  3082. :group 'org-appearance
  3083. :type 'boolean)
  3084. (defcustom org-highlight-latex-fragments-and-specials nil
  3085. "Non-nil means fontify what is treated specially by the exporters."
  3086. :group 'org-appearance
  3087. :type 'boolean)
  3088. (defcustom org-hide-emphasis-markers nil
  3089. "Non-nil mean font-lock should hide the emphasis marker characters."
  3090. :group 'org-appearance
  3091. :type 'boolean)
  3092. (defcustom org-pretty-entities nil
  3093. "Non-nil means show entities as UTF8 characters.
  3094. When nil, the \\name form remains in the buffer."
  3095. :group 'org-appearance
  3096. :type 'boolean)
  3097. (defcustom org-pretty-entities-include-sub-superscripts t
  3098. "Non-nil means, pretty entity display includes formatting sub/superscripts."
  3099. :group 'org-appearance
  3100. :type 'boolean)
  3101. (defvar org-emph-re nil
  3102. "Regular expression for matching emphasis.
  3103. After a match, the match groups contain these elements:
  3104. 0 The match of the full regular expression, including the characters
  3105. before and after the proper match
  3106. 1 The character before the proper match, or empty at beginning of line
  3107. 2 The proper match, including the leading and trailing markers
  3108. 3 The leading marker like * or /, indicating the type of highlighting
  3109. 4 The text between the emphasis markers, not including the markers
  3110. 5 The character after the match, empty at the end of a line")
  3111. (defvar org-verbatim-re nil
  3112. "Regular expression for matching verbatim text.")
  3113. (defvar org-emphasis-regexp-components) ; defined just below
  3114. (defvar org-emphasis-alist) ; defined just below
  3115. (defun org-set-emph-re (var val)
  3116. "Set variable and compute the emphasis regular expression."
  3117. (set var val)
  3118. (when (and (boundp 'org-emphasis-alist)
  3119. (boundp 'org-emphasis-regexp-components)
  3120. org-emphasis-alist org-emphasis-regexp-components)
  3121. (let* ((e org-emphasis-regexp-components)
  3122. (pre (car e))
  3123. (post (nth 1 e))
  3124. (border (nth 2 e))
  3125. (body (nth 3 e))
  3126. (nl (nth 4 e))
  3127. (body1 (concat body "*?"))
  3128. (markers (mapconcat 'car org-emphasis-alist ""))
  3129. (vmarkers (mapconcat
  3130. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  3131. org-emphasis-alist "")))
  3132. ;; make sure special characters appear at the right position in the class
  3133. (if (string-match "\\^" markers)
  3134. (setq markers (concat (replace-match "" t t markers) "^")))
  3135. (if (string-match "-" markers)
  3136. (setq markers (concat (replace-match "" t t markers) "-")))
  3137. (if (string-match "\\^" vmarkers)
  3138. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  3139. (if (string-match "-" vmarkers)
  3140. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  3141. (if (> nl 0)
  3142. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  3143. (int-to-string nl) "\\}")))
  3144. ;; Make the regexp
  3145. (setq org-emph-re
  3146. (concat "\\([" pre "]\\|^\\)"
  3147. "\\("
  3148. "\\([" markers "]\\)"
  3149. "\\("
  3150. "[^" border "]\\|"
  3151. "[^" border "]"
  3152. body1
  3153. "[^" border "]"
  3154. "\\)"
  3155. "\\3\\)"
  3156. "\\([" post "]\\|$\\)"))
  3157. (setq org-verbatim-re
  3158. (concat "\\([" pre "]\\|^\\)"
  3159. "\\("
  3160. "\\([" vmarkers "]\\)"
  3161. "\\("
  3162. "[^" border "]\\|"
  3163. "[^" border "]"
  3164. body1
  3165. "[^" border "]"
  3166. "\\)"
  3167. "\\3\\)"
  3168. "\\([" post "]\\|$\\)")))))
  3169. (defcustom org-emphasis-regexp-components
  3170. '(" \t('\"{" "- \t.,:!?;'\")}\\" " \t\r\n,\"'" "." 1)
  3171. "Components used to build the regular expression for emphasis.
  3172. This is a list with five entries. Terminology: In an emphasis string
  3173. like \" *strong word* \", we call the initial space PREMATCH, the final
  3174. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  3175. and \"trong wor\" is the body. The different components in this variable
  3176. specify what is allowed/forbidden in each part:
  3177. pre Chars allowed as prematch. Beginning of line will be allowed too.
  3178. post Chars allowed as postmatch. End of line will be allowed too.
  3179. border The chars *forbidden* as border characters.
  3180. body-regexp A regexp like \".\" to match a body character. Don't use
  3181. non-shy groups here, and don't allow newline here.
  3182. newline The maximum number of newlines allowed in an emphasis exp.
  3183. Use customize to modify this, or restart Emacs after changing it."
  3184. :group 'org-appearance
  3185. :set 'org-set-emph-re
  3186. :type '(list
  3187. (sexp :tag "Allowed chars in pre ")
  3188. (sexp :tag "Allowed chars in post ")
  3189. (sexp :tag "Forbidden chars in border ")
  3190. (sexp :tag "Regexp for body ")
  3191. (integer :tag "number of newlines allowed")
  3192. (option (boolean :tag "Please ignore this button"))))
  3193. (defcustom org-emphasis-alist
  3194. `(("*" bold "<b>" "</b>")
  3195. ("/" italic "<i>" "</i>")
  3196. ("_" underline "<span style=\"text-decoration:underline;\">" "</span>")
  3197. ("=" org-code "<code>" "</code>" verbatim)
  3198. ("~" org-verbatim "<code>" "</code>" verbatim)
  3199. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  3200. "<del>" "</del>")
  3201. )
  3202. "Special syntax for emphasized text.
  3203. Text starting and ending with a special character will be emphasized, for
  3204. example *bold*, _underlined_ and /italic/. This variable sets the marker
  3205. characters, the face to be used by font-lock for highlighting in Org-mode
  3206. Emacs buffers, and the HTML tags to be used for this.
  3207. For LaTeX export, see the variable `org-export-latex-emphasis-alist'.
  3208. For DocBook export, see the variable `org-export-docbook-emphasis-alist'.
  3209. Use customize to modify this, or restart Emacs after changing it."
  3210. :group 'org-appearance
  3211. :set 'org-set-emph-re
  3212. :type '(repeat
  3213. (list
  3214. (string :tag "Marker character")
  3215. (choice
  3216. (face :tag "Font-lock-face")
  3217. (plist :tag "Face property list"))
  3218. (string :tag "HTML start tag")
  3219. (string :tag "HTML end tag")
  3220. (option (const verbatim)))))
  3221. (defvar org-protecting-blocks
  3222. '("src" "example" "latex" "ascii" "html" "docbook" "ditaa" "dot" "r" "R")
  3223. "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
  3224. This is needed for font-lock setup.")
  3225. ;;; Miscellaneous options
  3226. (defgroup org-completion nil
  3227. "Completion in Org-mode."
  3228. :tag "Org Completion"
  3229. :group 'org)
  3230. (defcustom org-completion-use-ido nil
  3231. "Non-nil means use ido completion wherever possible.
  3232. Note that `ido-mode' must be active for this variable to be relevant.
  3233. If you decide to turn this variable on, you might well want to turn off
  3234. `org-outline-path-complete-in-steps'.
  3235. See also `org-completion-use-iswitchb'."
  3236. :group 'org-completion
  3237. :type 'boolean)
  3238. (defcustom org-completion-use-iswitchb nil
  3239. "Non-nil means use iswitchb completion wherever possible.
  3240. Note that `iswitchb-mode' must be active for this variable to be relevant.
  3241. If you decide to turn this variable on, you might well want to turn off
  3242. `org-outline-path-complete-in-steps'.
  3243. Note that this variable has only an effect if `org-completion-use-ido' is nil."
  3244. :group 'org-completion
  3245. :type 'boolean)
  3246. (defcustom org-completion-fallback-command 'hippie-expand
  3247. "The expansion command called by \\[pcomplete] in normal context.
  3248. Normal means, no org-mode-specific context."
  3249. :group 'org-completion
  3250. :type 'function)
  3251. ;;; Functions and variables from their packages
  3252. ;; Declared here to avoid compiler warnings
  3253. ;; XEmacs only
  3254. (defvar outline-mode-menu-heading)
  3255. (defvar outline-mode-menu-show)
  3256. (defvar outline-mode-menu-hide)
  3257. (defvar zmacs-regions) ; XEmacs regions
  3258. ;; Emacs only
  3259. (defvar mark-active)
  3260. ;; Various packages
  3261. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  3262. (declare-function calendar-forward-day "cal-move" (arg))
  3263. (declare-function calendar-goto-date "cal-move" (date))
  3264. (declare-function calendar-goto-today "cal-move" ())
  3265. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  3266. (defvar calc-embedded-close-formula)
  3267. (defvar calc-embedded-open-formula)
  3268. (declare-function cdlatex-tab "ext:cdlatex" ())
  3269. (declare-function cdlatex-compute-tables "ext:cdlatex" ())
  3270. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  3271. (defvar font-lock-unfontify-region-function)
  3272. (declare-function iswitchb-read-buffer "iswitchb"
  3273. (prompt &optional default require-match start matches-set))
  3274. (defvar iswitchb-temp-buflist)
  3275. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  3276. (defvar org-agenda-tags-todo-honor-ignore-options)
  3277. (declare-function org-agenda-skip "org-agenda" ())
  3278. (declare-function
  3279. org-agenda-format-item "org-agenda"
  3280. (extra txt &optional category tags dotime noprefix remove-re habitp))
  3281. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  3282. (declare-function org-agenda-change-all-lines "org-agenda"
  3283. (newhead hdmarker &optional fixface just-this))
  3284. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  3285. (declare-function org-agenda-maybe-redo "org-agenda" ())
  3286. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  3287. (beg end))
  3288. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  3289. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  3290. "org-agenda" (&optional end))
  3291. (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
  3292. (declare-function org-inlinetask-in-task-p "org-inlinetask" ())
  3293. (declare-function org-inlinetask-goto-beginning "org-inlinetask" ())
  3294. (declare-function org-inlinetask-goto-end "org-inlinetask" ())
  3295. (declare-function org-indent-mode "org-indent" (&optional arg))
  3296. (declare-function parse-time-string "parse-time" (string))
  3297. (declare-function org-attach-reveal "org-attach" (&optional if-exists))
  3298. (declare-function org-export-latex-fix-inputenc "org-latex" ())
  3299. (declare-function orgtbl-send-table "org-table" (&optional maybe))
  3300. (defvar remember-data-file)
  3301. (defvar texmathp-why)
  3302. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  3303. (declare-function table--at-cell-p "table" (position &optional object at-column))
  3304. (defvar w3m-current-url)
  3305. (defvar w3m-current-title)
  3306. (defvar org-latex-regexps)
  3307. ;;; Autoload and prepare some org modules
  3308. ;; Some table stuff that needs to be defined here, because it is used
  3309. ;; by the functions setting up org-mode or checking for table context.
  3310. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  3311. "Detect an org-type or table-type table.")
  3312. (defconst org-table-line-regexp "^[ \t]*|"
  3313. "Detect an org-type table line.")
  3314. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  3315. "Detect an org-type table line.")
  3316. (defconst org-table-hline-regexp "^[ \t]*|-"
  3317. "Detect an org-type table hline.")
  3318. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  3319. "Detect a table-type table hline.")
  3320. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  3321. "Detect the first line outside a table when searching from within it.
  3322. This works for both table types.")
  3323. ;; Autoload the functions in org-table.el that are needed by functions here.
  3324. (eval-and-compile
  3325. (org-autoload "org-table"
  3326. '(org-table-align org-table-begin org-table-blank-field
  3327. org-table-convert org-table-convert-region org-table-copy-down
  3328. org-table-copy-region org-table-create
  3329. org-table-create-or-convert-from-region
  3330. org-table-create-with-table.el org-table-current-dline
  3331. org-table-cut-region org-table-delete-column org-table-edit-field
  3332. org-table-edit-formulas org-table-end org-table-eval-formula
  3333. org-table-export org-table-field-info
  3334. org-table-get-stored-formulas org-table-goto-column
  3335. org-table-hline-and-move org-table-import org-table-insert-column
  3336. org-table-insert-hline org-table-insert-row org-table-iterate
  3337. org-table-justify-field-maybe org-table-kill-row
  3338. org-table-maybe-eval-formula org-table-maybe-recalculate-line
  3339. org-table-move-column org-table-move-column-left
  3340. org-table-move-column-right org-table-move-row
  3341. org-table-move-row-down org-table-move-row-up
  3342. org-table-next-field org-table-next-row org-table-paste-rectangle
  3343. org-table-previous-field org-table-recalculate
  3344. org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
  3345. org-table-toggle-coordinate-overlays
  3346. org-table-toggle-formula-debugger org-table-wrap-region
  3347. orgtbl-mode turn-on-orgtbl org-table-to-lisp
  3348. orgtbl-to-generic orgtbl-to-tsv orgtbl-to-csv orgtbl-to-latex
  3349. orgtbl-to-orgtbl orgtbl-to-html orgtbl-to-texinfo)))
  3350. (defun org-at-table-p (&optional table-type)
  3351. "Return t if the cursor is inside an org-type table.
  3352. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  3353. (if org-enable-table-editor
  3354. (save-excursion
  3355. (beginning-of-line 1)
  3356. (looking-at (if table-type org-table-any-line-regexp
  3357. org-table-line-regexp)))
  3358. nil))
  3359. (defsubst org-table-p () (org-at-table-p))
  3360. (defun org-at-table.el-p ()
  3361. "Return t if and only if we are at a table.el table."
  3362. (and (org-at-table-p 'any)
  3363. (save-excursion
  3364. (goto-char (org-table-begin 'any))
  3365. (looking-at org-table1-hline-regexp))))
  3366. (defun org-table-recognize-table.el ()
  3367. "If there is a table.el table nearby, recognize it and move into it."
  3368. (if org-table-tab-recognizes-table.el
  3369. (if (org-at-table.el-p)
  3370. (progn
  3371. (beginning-of-line 1)
  3372. (if (looking-at org-table-dataline-regexp)
  3373. nil
  3374. (if (looking-at org-table1-hline-regexp)
  3375. (progn
  3376. (beginning-of-line 2)
  3377. (if (looking-at org-table-any-border-regexp)
  3378. (beginning-of-line -1)))))
  3379. (if (re-search-forward "|" (org-table-end t) t)
  3380. (progn
  3381. (require 'table)
  3382. (if (table--at-cell-p (point))
  3383. t
  3384. (message "recognizing table.el table...")
  3385. (table-recognize-table)
  3386. (message "recognizing table.el table...done")))
  3387. (error "This should not happen"))
  3388. t)
  3389. nil)
  3390. nil))
  3391. (defun org-at-table-hline-p ()
  3392. "Return t if the cursor is inside a hline in a table."
  3393. (if org-enable-table-editor
  3394. (save-excursion
  3395. (beginning-of-line 1)
  3396. (looking-at org-table-hline-regexp))
  3397. nil))
  3398. (defvar org-table-clean-did-remove-column nil)
  3399. (defun org-table-map-tables (function &optional quietly)
  3400. "Apply FUNCTION to the start of all tables in the buffer."
  3401. (save-excursion
  3402. (save-restriction
  3403. (widen)
  3404. (goto-char (point-min))
  3405. (while (re-search-forward org-table-any-line-regexp nil t)
  3406. (unless quietly
  3407. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size))))
  3408. (beginning-of-line 1)
  3409. (when (looking-at org-table-line-regexp)
  3410. (save-excursion (funcall function))
  3411. (or (looking-at org-table-line-regexp)
  3412. (forward-char 1)))
  3413. (re-search-forward org-table-any-border-regexp nil 1))))
  3414. (unless quietly (message "Mapping tables: done")))
  3415. ;; Declare and autoload functions from org-exp.el & Co
  3416. (declare-function org-default-export-plist "org-exp")
  3417. (declare-function org-infile-export-plist "org-exp")
  3418. (declare-function org-get-current-options "org-exp")
  3419. (eval-and-compile
  3420. (org-autoload "org-exp"
  3421. '(org-export org-export-visible
  3422. org-insert-export-options-template
  3423. org-table-clean-before-export))
  3424. (org-autoload "org-ascii"
  3425. '(org-export-as-ascii org-export-ascii-preprocess
  3426. org-export-as-ascii-to-buffer org-replace-region-by-ascii
  3427. org-export-region-as-ascii))
  3428. (org-autoload "org-latex"
  3429. '(org-export-as-latex-batch org-export-as-latex-to-buffer
  3430. org-replace-region-by-latex org-export-region-as-latex
  3431. org-export-as-latex org-export-as-pdf
  3432. org-export-as-pdf-and-open))
  3433. (org-autoload "org-html"
  3434. '(org-export-as-html-and-open
  3435. org-export-as-html-batch org-export-as-html-to-buffer
  3436. org-replace-region-by-html org-export-region-as-html
  3437. org-export-as-html))
  3438. (org-autoload "org-docbook"
  3439. '(org-export-as-docbook-batch org-export-as-docbook-to-buffer
  3440. org-replace-region-by-docbook org-export-region-as-docbook
  3441. org-export-as-docbook-pdf org-export-as-docbook-pdf-and-open
  3442. org-export-as-docbook))
  3443. (org-autoload "org-icalendar"
  3444. '(org-export-icalendar-this-file
  3445. org-export-icalendar-all-agenda-files
  3446. org-export-icalendar-combine-agenda-files))
  3447. (org-autoload "org-xoxo" '(org-export-as-xoxo))
  3448. (org-autoload "org-beamer" '(org-beamer-mode org-beamer-sectioning)))
  3449. ;; Declare and autoload functions from org-agenda.el
  3450. (eval-and-compile
  3451. (org-autoload "org-agenda"
  3452. '(org-agenda org-agenda-list org-search-view
  3453. org-todo-list org-tags-view org-agenda-list-stuck-projects
  3454. org-diary org-agenda-to-appt
  3455. org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
  3456. ;; Autoload org-remember
  3457. (eval-and-compile
  3458. (org-autoload "org-remember"
  3459. '(org-remember-insinuate org-remember-annotation
  3460. org-remember-apply-template org-remember org-remember-handler)))
  3461. (eval-and-compile
  3462. (org-autoload "org-capture"
  3463. '(org-capture org-capture-insert-template-here
  3464. org-capture-import-remember-templates)))
  3465. ;; Autoload org-clock.el
  3466. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock"
  3467. (beg end))
  3468. (declare-function org-clock-update-mode-line "org-clock" ())
  3469. (declare-function org-resolve-clocks "org-clock"
  3470. (&optional also-non-dangling-p prompt last-valid))
  3471. (defvar org-clock-start-time)
  3472. (defvar org-clock-marker (make-marker)
  3473. "Marker recording the last clock-in.")
  3474. (defvar org-clock-hd-marker (make-marker)
  3475. "Marker recording the last clock-in, but the headline position.")
  3476. (defvar org-clock-heading ""
  3477. "The heading of the current clock entry.")
  3478. (defun org-clock-is-active ()
  3479. "Return non-nil if clock is currently running.
  3480. The return value is actually the clock marker."
  3481. (marker-buffer org-clock-marker))
  3482. (eval-and-compile
  3483. (org-autoload
  3484. "org-clock"
  3485. '(org-clock-in org-clock-out org-clock-cancel
  3486. org-clock-goto org-clock-sum org-clock-display
  3487. org-clock-remove-overlays org-clock-report
  3488. org-clocktable-shift org-dblock-write:clocktable
  3489. org-get-clocktable org-resolve-clocks)))
  3490. (defun org-clock-update-time-maybe ()
  3491. "If this is a CLOCK line, update it and return t.
  3492. Otherwise, return nil."
  3493. (interactive)
  3494. (save-excursion
  3495. (beginning-of-line 1)
  3496. (skip-chars-forward " \t")
  3497. (when (looking-at org-clock-string)
  3498. (let ((re (concat "[ \t]*" org-clock-string
  3499. " *[[<]\\([^]>]+\\)[]>]\\(-+[[<]\\([^]>]+\\)[]>]"
  3500. "\\([ \t]*=>.*\\)?\\)?"))
  3501. ts te h m s neg)
  3502. (cond
  3503. ((not (looking-at re))
  3504. nil)
  3505. ((not (match-end 2))
  3506. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3507. (> org-clock-marker (point))
  3508. (<= org-clock-marker (point-at-eol)))
  3509. ;; The clock is running here
  3510. (setq org-clock-start-time
  3511. (apply 'encode-time
  3512. (org-parse-time-string (match-string 1))))
  3513. (org-clock-update-mode-line)))
  3514. (t
  3515. (and (match-end 4) (delete-region (match-beginning 4) (match-end 4)))
  3516. (end-of-line 1)
  3517. (setq ts (match-string 1)
  3518. te (match-string 3))
  3519. (setq s (- (org-float-time
  3520. (apply 'encode-time (org-parse-time-string te)))
  3521. (org-float-time
  3522. (apply 'encode-time (org-parse-time-string ts))))
  3523. neg (< s 0)
  3524. s (abs s)
  3525. h (floor (/ s 3600))
  3526. s (- s (* 3600 h))
  3527. m (floor (/ s 60))
  3528. s (- s (* 60 s)))
  3529. (insert " => " (format (if neg "-%d:%02d" "%2d:%02d") h m))
  3530. t))))))
  3531. (defun org-check-running-clock ()
  3532. "Check if the current buffer contains the running clock.
  3533. If yes, offer to stop it and to save the buffer with the changes."
  3534. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3535. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  3536. (buffer-name))))
  3537. (org-clock-out)
  3538. (when (y-or-n-p "Save changed buffer?")
  3539. (save-buffer))))
  3540. (defun org-clocktable-try-shift (dir n)
  3541. "Check if this line starts a clock table, if yes, shift the time block."
  3542. (when (org-match-line "^[ \t]*#\\+BEGIN:[ \t]+clocktable\\>")
  3543. (org-clocktable-shift dir n)))
  3544. ;; Autoload org-timer.el
  3545. (eval-and-compile
  3546. (org-autoload
  3547. "org-timer"
  3548. '(org-timer-start org-timer org-timer-item
  3549. org-timer-change-times-in-region
  3550. org-timer-set-timer
  3551. org-timer-reset-timers
  3552. org-timer-show-remaining-time)))
  3553. ;; Autoload org-feed.el
  3554. (eval-and-compile
  3555. (org-autoload
  3556. "org-feed"
  3557. '(org-feed-update org-feed-update-all org-feed-goto-inbox)))
  3558. ;; Autoload org-indent.el
  3559. ;; Define the variable already here, to make sure we have it.
  3560. (defvar org-indent-mode nil
  3561. "Non-nil if Org-Indent mode is enabled.
  3562. Use the command `org-indent-mode' to change this variable.")
  3563. (eval-and-compile
  3564. (org-autoload
  3565. "org-indent"
  3566. '(org-indent-mode)))
  3567. ;; Autoload org-mobile.el
  3568. (eval-and-compile
  3569. (org-autoload
  3570. "org-mobile"
  3571. '(org-mobile-push org-mobile-pull org-mobile-create-sumo-agenda)))
  3572. ;; Autoload archiving code
  3573. ;; The stuff that is needed for cycling and tags has to be defined here.
  3574. (defgroup org-archive nil
  3575. "Options concerning archiving in Org-mode."
  3576. :tag "Org Archive"
  3577. :group 'org-structure)
  3578. (defcustom org-archive-location "%s_archive::"
  3579. "The location where subtrees should be archived.
  3580. The value of this variable is a string, consisting of two parts,
  3581. separated by a double-colon. The first part is a filename and
  3582. the second part is a headline.
  3583. When the filename is omitted, archiving happens in the same file.
  3584. %s in the filename will be replaced by the current file
  3585. name (without the directory part). Archiving to a different file
  3586. is useful to keep archived entries from contributing to the
  3587. Org-mode Agenda.
  3588. The archived entries will be filed as subtrees of the specified
  3589. headline. When the headline is omitted, the subtrees are simply
  3590. filed away at the end of the file, as top-level entries. Also in
  3591. the heading you can use %s to represent the file name, this can be
  3592. useful when using the same archive for a number of different files.
  3593. Here are a few examples:
  3594. \"%s_archive::\"
  3595. If the current file is Projects.org, archive in file
  3596. Projects.org_archive, as top-level trees. This is the default.
  3597. \"::* Archived Tasks\"
  3598. Archive in the current file, under the top-level headline
  3599. \"* Archived Tasks\".
  3600. \"~/org/archive.org::\"
  3601. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  3602. \"~/org/archive.org::* From %s\"
  3603. Archive in file ~/org/archive.org (absolute path), under headlines
  3604. \"From FILENAME\" where file name is the current file name.
  3605. \"basement::** Finished Tasks\"
  3606. Archive in file ./basement (relative path), as level 3 trees
  3607. below the level 2 heading \"** Finished Tasks\".
  3608. You may set this option on a per-file basis by adding to the buffer a
  3609. line like
  3610. #+ARCHIVE: basement::** Finished Tasks
  3611. You may also define it locally for a subtree by setting an ARCHIVE property
  3612. in the entry. If such a property is found in an entry, or anywhere up
  3613. the hierarchy, it will be used."
  3614. :group 'org-archive
  3615. :type 'string)
  3616. (defcustom org-archive-tag "ARCHIVE"
  3617. "The tag that marks a subtree as archived.
  3618. An archived subtree does not open during visibility cycling, and does
  3619. not contribute to the agenda listings.
  3620. After changing this, font-lock must be restarted in the relevant buffers to
  3621. get the proper fontification."
  3622. :group 'org-archive
  3623. :group 'org-keywords
  3624. :type 'string)
  3625. (defcustom org-agenda-skip-archived-trees t
  3626. "Non-nil means the agenda will skip any items located in archived trees.
  3627. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  3628. variable is no longer recommended, you should leave it at the value t.
  3629. Instead, use the key `v' to cycle the archives-mode in the agenda."
  3630. :group 'org-archive
  3631. :group 'org-agenda-skip
  3632. :type 'boolean)
  3633. (defcustom org-columns-skip-archived-trees t
  3634. "Non-nil means ignore archived trees when creating column view."
  3635. :group 'org-archive
  3636. :group 'org-properties
  3637. :type 'boolean)
  3638. (defcustom org-cycle-open-archived-trees nil
  3639. "Non-nil means `org-cycle' will open archived trees.
  3640. An archived tree is a tree marked with the tag ARCHIVE.
  3641. When nil, archived trees will stay folded. You can still open them with
  3642. normal outline commands like `show-all', but not with the cycling commands."
  3643. :group 'org-archive
  3644. :group 'org-cycle
  3645. :type 'boolean)
  3646. (defcustom org-sparse-tree-open-archived-trees nil
  3647. "Non-nil means sparse tree construction shows matches in archived trees.
  3648. When nil, matches in these trees are highlighted, but the trees are kept in
  3649. collapsed state."
  3650. :group 'org-archive
  3651. :group 'org-sparse-trees
  3652. :type 'boolean)
  3653. (defun org-cycle-hide-archived-subtrees (state)
  3654. "Re-hide all archived subtrees after a visibility state change."
  3655. (when (and (not org-cycle-open-archived-trees)
  3656. (not (memq state '(overview folded))))
  3657. (save-excursion
  3658. (let* ((globalp (memq state '(contents all)))
  3659. (beg (if globalp (point-min) (point)))
  3660. (end (if globalp (point-max) (org-end-of-subtree t))))
  3661. (org-hide-archived-subtrees beg end)
  3662. (goto-char beg)
  3663. (if (looking-at (concat ".*:" org-archive-tag ":"))
  3664. (message "%s" (substitute-command-keys
  3665. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  3666. (defun org-force-cycle-archived ()
  3667. "Cycle subtree even if it is archived."
  3668. (interactive)
  3669. (setq this-command 'org-cycle)
  3670. (let ((org-cycle-open-archived-trees t))
  3671. (call-interactively 'org-cycle)))
  3672. (defun org-hide-archived-subtrees (beg end)
  3673. "Re-hide all archived subtrees after a visibility state change."
  3674. (save-excursion
  3675. (let* ((re (concat ":" org-archive-tag ":")))
  3676. (goto-char beg)
  3677. (while (re-search-forward re end t)
  3678. (when (org-on-heading-p)
  3679. (org-flag-subtree t)
  3680. (org-end-of-subtree t))))))
  3681. (defun org-flag-subtree (flag)
  3682. (save-excursion
  3683. (org-back-to-heading t)
  3684. (outline-end-of-heading)
  3685. (outline-flag-region (point)
  3686. (progn (org-end-of-subtree t) (point))
  3687. flag)))
  3688. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  3689. (eval-and-compile
  3690. (org-autoload "org-archive"
  3691. '(org-add-archive-files org-archive-subtree
  3692. org-archive-to-archive-sibling org-toggle-archive-tag
  3693. org-archive-subtree-default
  3694. org-archive-subtree-default-with-confirmation)))
  3695. ;; Autoload Column View Code
  3696. (declare-function org-columns-number-to-string "org-colview")
  3697. (declare-function org-columns-get-format-and-top-level "org-colview")
  3698. (declare-function org-columns-compute "org-colview")
  3699. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  3700. '(org-columns-number-to-string org-columns-get-format-and-top-level
  3701. org-columns-compute org-agenda-columns org-columns-remove-overlays
  3702. org-columns org-insert-columns-dblock org-dblock-write:columnview))
  3703. ;; Autoload ID code
  3704. (declare-function org-id-store-link "org-id")
  3705. (declare-function org-id-locations-load "org-id")
  3706. (declare-function org-id-locations-save "org-id")
  3707. (defvar org-id-track-globally)
  3708. (org-autoload "org-id"
  3709. '(org-id-get-create org-id-new org-id-copy org-id-get
  3710. org-id-get-with-outline-path-completion
  3711. org-id-get-with-outline-drilling org-id-store-link
  3712. org-id-goto org-id-find org-id-store-link))
  3713. ;; Autoload Plotting Code
  3714. (org-autoload "org-plot"
  3715. '(org-plot/gnuplot))
  3716. ;;; Variables for pre-computed regular expressions, all buffer local
  3717. (defvar org-drawer-regexp nil
  3718. "Matches first line of a hidden block.")
  3719. (make-variable-buffer-local 'org-drawer-regexp)
  3720. (defvar org-todo-regexp nil
  3721. "Matches any of the TODO state keywords.")
  3722. (make-variable-buffer-local 'org-todo-regexp)
  3723. (defvar org-not-done-regexp nil
  3724. "Matches any of the TODO state keywords except the last one.")
  3725. (make-variable-buffer-local 'org-not-done-regexp)
  3726. (defvar org-not-done-heading-regexp nil
  3727. "Matches a TODO headline that is not done.")
  3728. (make-variable-buffer-local 'org-not-done-regexp)
  3729. (defvar org-todo-line-regexp nil
  3730. "Matches a headline and puts TODO state into group 2 if present.")
  3731. (make-variable-buffer-local 'org-todo-line-regexp)
  3732. (defvar org-complex-heading-regexp nil
  3733. "Matches a headline and puts everything into groups:
  3734. group 1: the stars
  3735. group 2: The todo keyword, maybe
  3736. group 3: Priority cookie
  3737. group 4: True headline
  3738. group 5: Tags")
  3739. (make-variable-buffer-local 'org-complex-heading-regexp)
  3740. (defvar org-complex-heading-regexp-format nil
  3741. "Printf format to make regexp to match an exact headline.
  3742. This regexp will match the headline of any node which has the
  3743. exact headline text that is put into the format, but may have any
  3744. TODO state, priority and tags.")
  3745. (make-variable-buffer-local 'org-complex-heading-regexp-format)
  3746. (defvar org-todo-line-tags-regexp nil
  3747. "Matches a headline and puts TODO state into group 2 if present.
  3748. Also put tags into group 4 if tags are present.")
  3749. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  3750. (defvar org-ds-keyword-length 12
  3751. "Maximum length of the Deadline and SCHEDULED keywords.")
  3752. (make-variable-buffer-local 'org-ds-keyword-length)
  3753. (defvar org-deadline-regexp nil
  3754. "Matches the DEADLINE keyword.")
  3755. (make-variable-buffer-local 'org-deadline-regexp)
  3756. (defvar org-deadline-time-regexp nil
  3757. "Matches the DEADLINE keyword together with a time stamp.")
  3758. (make-variable-buffer-local 'org-deadline-time-regexp)
  3759. (defvar org-deadline-line-regexp nil
  3760. "Matches the DEADLINE keyword and the rest of the line.")
  3761. (make-variable-buffer-local 'org-deadline-line-regexp)
  3762. (defvar org-scheduled-regexp nil
  3763. "Matches the SCHEDULED keyword.")
  3764. (make-variable-buffer-local 'org-scheduled-regexp)
  3765. (defvar org-scheduled-time-regexp nil
  3766. "Matches the SCHEDULED keyword together with a time stamp.")
  3767. (make-variable-buffer-local 'org-scheduled-time-regexp)
  3768. (defvar org-closed-time-regexp nil
  3769. "Matches the CLOSED keyword together with a time stamp.")
  3770. (make-variable-buffer-local 'org-closed-time-regexp)
  3771. (defvar org-keyword-time-regexp nil
  3772. "Matches any of the 4 keywords, together with the time stamp.")
  3773. (make-variable-buffer-local 'org-keyword-time-regexp)
  3774. (defvar org-keyword-time-not-clock-regexp nil
  3775. "Matches any of the 3 keywords, together with the time stamp.")
  3776. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  3777. (defvar org-maybe-keyword-time-regexp nil
  3778. "Matches a timestamp, possibly preceded by a keyword.")
  3779. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  3780. (defvar org-planning-or-clock-line-re nil
  3781. "Matches a line with planning or clock info.")
  3782. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  3783. (defvar org-all-time-keywords nil
  3784. "List of time keywords.")
  3785. (make-variable-buffer-local 'org-all-time-keywords)
  3786. (defconst org-plain-time-of-day-regexp
  3787. (concat
  3788. "\\(\\<[012]?[0-9]"
  3789. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3790. "\\(--?"
  3791. "\\(\\<[012]?[0-9]"
  3792. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3793. "\\)?")
  3794. "Regular expression to match a plain time or time range.
  3795. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  3796. groups carry important information:
  3797. 0 the full match
  3798. 1 the first time, range or not
  3799. 8 the second time, if it is a range.")
  3800. (defconst org-plain-time-extension-regexp
  3801. (concat
  3802. "\\(\\<[012]?[0-9]"
  3803. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3804. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  3805. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  3806. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  3807. groups carry important information:
  3808. 0 the full match
  3809. 7 hours of duration
  3810. 9 minutes of duration")
  3811. (defconst org-stamp-time-of-day-regexp
  3812. (concat
  3813. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  3814. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  3815. "\\(--?"
  3816. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  3817. "Regular expression to match a timestamp time or time range.
  3818. After a match, the following groups carry important information:
  3819. 0 the full match
  3820. 1 date plus weekday, for back referencing to make sure both times are on the same day
  3821. 2 the first time, range or not
  3822. 4 the second time, if it is a range.")
  3823. (defconst org-startup-options
  3824. '(("fold" org-startup-folded t)
  3825. ("overview" org-startup-folded t)
  3826. ("nofold" org-startup-folded nil)
  3827. ("showall" org-startup-folded nil)
  3828. ("showeverything" org-startup-folded showeverything)
  3829. ("content" org-startup-folded content)
  3830. ("indent" org-startup-indented t)
  3831. ("noindent" org-startup-indented nil)
  3832. ("hidestars" org-hide-leading-stars t)
  3833. ("showstars" org-hide-leading-stars nil)
  3834. ("odd" org-odd-levels-only t)
  3835. ("oddeven" org-odd-levels-only nil)
  3836. ("align" org-startup-align-all-tables t)
  3837. ("noalign" org-startup-align-all-tables nil)
  3838. ("inlineimages" org-startup-with-inline-images t)
  3839. ("noinlineimages" org-startup-with-inline-images nil)
  3840. ("customtime" org-display-custom-times t)
  3841. ("logdone" org-log-done time)
  3842. ("lognotedone" org-log-done note)
  3843. ("nologdone" org-log-done nil)
  3844. ("lognoteclock-out" org-log-note-clock-out t)
  3845. ("nolognoteclock-out" org-log-note-clock-out nil)
  3846. ("logrepeat" org-log-repeat state)
  3847. ("lognoterepeat" org-log-repeat note)
  3848. ("nologrepeat" org-log-repeat nil)
  3849. ("logreschedule" org-log-reschedule time)
  3850. ("lognotereschedule" org-log-reschedule note)
  3851. ("nologreschedule" org-log-reschedule nil)
  3852. ("logredeadline" org-log-redeadline time)
  3853. ("lognoteredeadline" org-log-redeadline note)
  3854. ("nologredeadline" org-log-redeadline nil)
  3855. ("logrefile" org-log-refile time)
  3856. ("lognoterefile" org-log-refile note)
  3857. ("nologrefile" org-log-refile nil)
  3858. ("fninline" org-footnote-define-inline t)
  3859. ("nofninline" org-footnote-define-inline nil)
  3860. ("fnlocal" org-footnote-section nil)
  3861. ("fnauto" org-footnote-auto-label t)
  3862. ("fnprompt" org-footnote-auto-label nil)
  3863. ("fnconfirm" org-footnote-auto-label confirm)
  3864. ("fnplain" org-footnote-auto-label plain)
  3865. ("fnadjust" org-footnote-auto-adjust t)
  3866. ("nofnadjust" org-footnote-auto-adjust nil)
  3867. ("constcgs" constants-unit-system cgs)
  3868. ("constSI" constants-unit-system SI)
  3869. ("noptag" org-tag-persistent-alist nil)
  3870. ("hideblocks" org-hide-block-startup t)
  3871. ("nohideblocks" org-hide-block-startup nil)
  3872. ("beamer" org-startup-with-beamer-mode t)
  3873. ("entitiespretty" org-pretty-entities t)
  3874. ("entitiesplain" org-pretty-entities nil))
  3875. "Variable associated with STARTUP options for org-mode.
  3876. Each element is a list of three items: The startup options as written
  3877. in the #+STARTUP line, the corresponding variable, and the value to
  3878. set this variable to if the option is found. An optional forth element PUSH
  3879. means to push this value onto the list in the variable.")
  3880. (defun org-update-property-plist (key val props)
  3881. "Update PROPS with KEY and VAL."
  3882. (if (string= "+" (substring key (- (length key) 1)))
  3883. (let* ((key (substring key 0 (- (length key) 1)))
  3884. (previous (cdr (assoc key props))))
  3885. (cons (cons key (concat previous " " val))
  3886. (org-remove-if (lambda (p) (string= (car p) key)) props)))
  3887. (cons (cons key val) props)))
  3888. (defun org-set-regexps-and-options ()
  3889. "Precompute regular expressions for current buffer."
  3890. (when (eq major-mode 'org-mode)
  3891. (org-set-local 'org-todo-kwd-alist nil)
  3892. (org-set-local 'org-todo-key-alist nil)
  3893. (org-set-local 'org-todo-key-trigger nil)
  3894. (org-set-local 'org-todo-keywords-1 nil)
  3895. (org-set-local 'org-done-keywords nil)
  3896. (org-set-local 'org-todo-heads nil)
  3897. (org-set-local 'org-todo-sets nil)
  3898. (org-set-local 'org-todo-log-states nil)
  3899. (org-set-local 'org-file-properties nil)
  3900. (org-set-local 'org-file-tags nil)
  3901. (let ((re (org-make-options-regexp
  3902. '("CATEGORY" "TODO" "COLUMNS"
  3903. "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
  3904. "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE" "LATEX_CLASS"
  3905. "OPTIONS")
  3906. "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
  3907. (splitre "[ \t]+")
  3908. (scripts org-use-sub-superscripts)
  3909. kwds kws0 kwsa key log value cat arch tags const links hw dws
  3910. tail sep kws1 prio props ftags drawers beamer-p
  3911. ext-setup-or-nil setup-contents (start 0))
  3912. (save-excursion
  3913. (save-restriction
  3914. (widen)
  3915. (goto-char (point-min))
  3916. (while (or (and ext-setup-or-nil
  3917. (string-match re ext-setup-or-nil start)
  3918. (setq start (match-end 0)))
  3919. (and (setq ext-setup-or-nil nil start 0)
  3920. (re-search-forward re nil t)))
  3921. (setq key (upcase (match-string 1 ext-setup-or-nil))
  3922. value (org-match-string-no-properties 2 ext-setup-or-nil))
  3923. (if (stringp value) (setq value (org-trim value)))
  3924. (cond
  3925. ((equal key "CATEGORY")
  3926. (setq cat value))
  3927. ((member key '("SEQ_TODO" "TODO"))
  3928. (push (cons 'sequence (org-split-string value splitre)) kwds))
  3929. ((equal key "TYP_TODO")
  3930. (push (cons 'type (org-split-string value splitre)) kwds))
  3931. ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key)
  3932. ;; general TODO-like setup
  3933. (push (cons (intern (downcase (match-string 1 key)))
  3934. (org-split-string value splitre)) kwds))
  3935. ((equal key "TAGS")
  3936. (setq tags (append tags (if tags '("\\n") nil)
  3937. (org-split-string value splitre))))
  3938. ((equal key "COLUMNS")
  3939. (org-set-local 'org-columns-default-format value))
  3940. ((equal key "LINK")
  3941. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  3942. (push (cons (match-string 1 value)
  3943. (org-trim (match-string 2 value)))
  3944. links)))
  3945. ((equal key "PRIORITIES")
  3946. (setq prio (org-split-string value " +")))
  3947. ((equal key "PROPERTY")
  3948. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  3949. (setq props (org-update-property-plist (match-string 1 value)
  3950. (match-string 2 value)
  3951. props))))
  3952. ((equal key "FILETAGS")
  3953. (when (string-match "\\S-" value)
  3954. (setq ftags
  3955. (append
  3956. ftags
  3957. (apply 'append
  3958. (mapcar (lambda (x) (org-split-string x ":"))
  3959. (org-split-string value)))))))
  3960. ((equal key "DRAWERS")
  3961. (setq drawers (org-split-string value splitre)))
  3962. ((equal key "CONSTANTS")
  3963. (setq const (append const (org-split-string value splitre))))
  3964. ((equal key "STARTUP")
  3965. (let ((opts (org-split-string value splitre))
  3966. l var val)
  3967. (while (setq l (pop opts))
  3968. (when (setq l (assoc l org-startup-options))
  3969. (setq var (nth 1 l) val (nth 2 l))
  3970. (if (not (nth 3 l))
  3971. (set (make-local-variable var) val)
  3972. (if (not (listp (symbol-value var)))
  3973. (set (make-local-variable var) nil))
  3974. (set (make-local-variable var) (symbol-value var))
  3975. (add-to-list var val))))))
  3976. ((equal key "ARCHIVE")
  3977. (setq arch value)
  3978. (remove-text-properties 0 (length arch)
  3979. '(face t fontified t) arch))
  3980. ((equal key "LATEX_CLASS")
  3981. (setq beamer-p (equal value "beamer")))
  3982. ((equal key "OPTIONS")
  3983. (if (string-match "\\([ \t]\\|\\`\\)\\^:\\(t\\|nil\\|{}\\)" value)
  3984. (setq scripts (read (match-string 2 value)))))
  3985. ((equal key "SETUPFILE")
  3986. (setq setup-contents (org-file-contents
  3987. (expand-file-name
  3988. (org-remove-double-quotes value))
  3989. 'noerror))
  3990. (if (not ext-setup-or-nil)
  3991. (setq ext-setup-or-nil setup-contents start 0)
  3992. (setq ext-setup-or-nil
  3993. (concat (substring ext-setup-or-nil 0 start)
  3994. "\n" setup-contents "\n"
  3995. (substring ext-setup-or-nil start)))))))
  3996. ;; search for property blocks
  3997. (goto-char (point-min))
  3998. (while (re-search-forward org-block-regexp nil t)
  3999. (when (equal "PROPERTY" (upcase (match-string 1)))
  4000. (setq value (replace-regexp-in-string
  4001. "[\n\r]" " " (match-string 4)))
  4002. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  4003. (setq props (org-update-property-plist (match-string 1 value)
  4004. (match-string 2 value)
  4005. props)))))))
  4006. (org-set-local 'org-use-sub-superscripts scripts)
  4007. (when cat
  4008. (org-set-local 'org-category (intern cat))
  4009. (push (cons "CATEGORY" cat) props))
  4010. (when prio
  4011. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  4012. (setq prio (mapcar 'string-to-char prio))
  4013. (org-set-local 'org-highest-priority (nth 0 prio))
  4014. (org-set-local 'org-lowest-priority (nth 1 prio))
  4015. (org-set-local 'org-default-priority (nth 2 prio)))
  4016. (and props (org-set-local 'org-file-properties (nreverse props)))
  4017. (and ftags (org-set-local 'org-file-tags
  4018. (mapcar 'org-add-prop-inherited ftags)))
  4019. (and drawers (org-set-local 'org-drawers drawers))
  4020. (and arch (org-set-local 'org-archive-location arch))
  4021. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  4022. ;; Process the TODO keywords
  4023. (unless kwds
  4024. ;; Use the global values as if they had been given locally.
  4025. (setq kwds (default-value 'org-todo-keywords))
  4026. (if (stringp (car kwds))
  4027. (setq kwds (list (cons org-todo-interpretation
  4028. (default-value 'org-todo-keywords)))))
  4029. (setq kwds (reverse kwds)))
  4030. (setq kwds (nreverse kwds))
  4031. (let (inter kws kw)
  4032. (while (setq kws (pop kwds))
  4033. (let ((kws (or
  4034. (run-hook-with-args-until-success
  4035. 'org-todo-setup-filter-hook kws)
  4036. kws)))
  4037. (setq inter (pop kws) sep (member "|" kws)
  4038. kws0 (delete "|" (copy-sequence kws))
  4039. kwsa nil
  4040. kws1 (mapcar
  4041. (lambda (x)
  4042. ;; 1 2
  4043. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  4044. (progn
  4045. (setq kw (match-string 1 x)
  4046. key (and (match-end 2) (match-string 2 x))
  4047. log (org-extract-log-state-settings x))
  4048. (push (cons kw (and key (string-to-char key))) kwsa)
  4049. (and log (push log org-todo-log-states))
  4050. kw)
  4051. (error "Invalid TODO keyword %s" x)))
  4052. kws0)
  4053. kwsa (if kwsa (append '((:startgroup))
  4054. (nreverse kwsa)
  4055. '((:endgroup))))
  4056. hw (car kws1)
  4057. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  4058. tail (list inter hw (car dws) (org-last dws))))
  4059. (add-to-list 'org-todo-heads hw 'append)
  4060. (push kws1 org-todo-sets)
  4061. (setq org-done-keywords (append org-done-keywords dws nil))
  4062. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  4063. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  4064. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  4065. (setq org-todo-sets (nreverse org-todo-sets)
  4066. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  4067. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  4068. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  4069. ;; Process the constants
  4070. (when const
  4071. (let (e cst)
  4072. (while (setq e (pop const))
  4073. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  4074. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  4075. (setq org-table-formula-constants-local cst)))
  4076. ;; Process the tags.
  4077. (when tags
  4078. (let (e tgs)
  4079. (while (setq e (pop tags))
  4080. (cond
  4081. ((equal e "{") (push '(:startgroup) tgs))
  4082. ((equal e "}") (push '(:endgroup) tgs))
  4083. ((equal e "\\n") (push '(:newline) tgs))
  4084. ((string-match (org-re "^\\([[:alnum:]_@#%]+\\)(\\(.\\))$") e)
  4085. (push (cons (match-string 1 e)
  4086. (string-to-char (match-string 2 e)))
  4087. tgs))
  4088. (t (push (list e) tgs))))
  4089. (org-set-local 'org-tag-alist nil)
  4090. (while (setq e (pop tgs))
  4091. (or (and (stringp (car e))
  4092. (assoc (car e) org-tag-alist))
  4093. (push e org-tag-alist)))))
  4094. ;; Compute the regular expressions and other local variables.
  4095. ;; Using `org-outline-regexp-bol' would complicate them much,
  4096. ;; because of the fixed white space at the end of that string.
  4097. (if (not org-done-keywords)
  4098. (setq org-done-keywords (and org-todo-keywords-1
  4099. (list (org-last org-todo-keywords-1)))))
  4100. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  4101. (length org-scheduled-string)
  4102. (length org-clock-string)
  4103. (length org-closed-string)))
  4104. org-drawer-regexp
  4105. (concat "^[ \t]*:\\("
  4106. (mapconcat 'regexp-quote org-drawers "\\|")
  4107. "\\):[ \t]*$")
  4108. org-not-done-keywords
  4109. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  4110. org-todo-regexp
  4111. (concat "\\("
  4112. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  4113. "\\)")
  4114. org-not-done-regexp
  4115. (concat "\\("
  4116. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  4117. "\\)")
  4118. org-not-done-heading-regexp
  4119. (format org-heading-keyword-regexp-format org-not-done-regexp)
  4120. org-todo-line-regexp
  4121. (format org-heading-keyword-maybe-regexp-format org-todo-regexp)
  4122. org-complex-heading-regexp
  4123. (concat "^\\(\\*+\\)"
  4124. "\\(?: +" org-todo-regexp "\\)?"
  4125. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4126. "\\(?: +\\(.*?\\)\\)?"
  4127. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?")
  4128. "[ \t]*$")
  4129. org-complex-heading-regexp-format
  4130. (concat "^\\(\\*+\\)"
  4131. "\\(?: +" org-todo-regexp "\\)?"
  4132. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4133. "\\(?: +"
  4134. ;; Stats cookies can be sticked to body.
  4135. "\\(?:\\[[0-9%%/]+\\] *\\)?"
  4136. "\\(%s\\)"
  4137. "\\(?: *\\[[0-9%%/]+\\]\\)?"
  4138. "\\)"
  4139. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%%:]+:\\)\\)?")
  4140. "[ \t]*$")
  4141. org-todo-line-tags-regexp
  4142. (concat "^\\(\\*+\\)"
  4143. "\\(?: +" org-todo-regexp "\\)?"
  4144. "\\(?: +\\(.*?\\)\\)?"
  4145. (org-re "\\(?:[ \t]+\\(:[[:alnum:]:_@#%]+:\\)\\)?")
  4146. "[ \t]*$")
  4147. org-deadline-regexp (concat "\\<" org-deadline-string)
  4148. org-deadline-time-regexp
  4149. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  4150. org-deadline-line-regexp
  4151. (concat "\\<\\(" org-deadline-string "\\).*")
  4152. org-scheduled-regexp
  4153. (concat "\\<" org-scheduled-string)
  4154. org-scheduled-time-regexp
  4155. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  4156. org-closed-time-regexp
  4157. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  4158. org-keyword-time-regexp
  4159. (concat "\\<\\(" org-scheduled-string
  4160. "\\|" org-deadline-string
  4161. "\\|" org-closed-string
  4162. "\\|" org-clock-string "\\)"
  4163. " *[[<]\\([^]>]+\\)[]>]")
  4164. org-keyword-time-not-clock-regexp
  4165. (concat "\\<\\(" org-scheduled-string
  4166. "\\|" org-deadline-string
  4167. "\\|" org-closed-string
  4168. "\\)"
  4169. " *[[<]\\([^]>]+\\)[]>]")
  4170. org-maybe-keyword-time-regexp
  4171. (concat "\\(\\<\\(" org-scheduled-string
  4172. "\\|" org-deadline-string
  4173. "\\|" org-closed-string
  4174. "\\|" org-clock-string "\\)\\)?"
  4175. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  4176. org-planning-or-clock-line-re
  4177. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  4178. "\\|" org-deadline-string
  4179. "\\|" org-closed-string "\\|" org-clock-string
  4180. "\\)\\>\\)")
  4181. org-all-time-keywords
  4182. (mapcar (lambda (w) (substring w 0 -1))
  4183. (list org-scheduled-string org-deadline-string
  4184. org-clock-string org-closed-string))
  4185. )
  4186. (org-compute-latex-and-specials-regexp)
  4187. (org-set-font-lock-defaults))))
  4188. (defun org-file-contents (file &optional noerror)
  4189. "Return the contents of FILE, as a string."
  4190. (if (or (not file)
  4191. (not (file-readable-p file)))
  4192. (if noerror
  4193. (progn
  4194. (message "Cannot read file \"%s\"" file)
  4195. (ding) (sit-for 2)
  4196. "")
  4197. (error "Cannot read file \"%s\"" file))
  4198. (with-temp-buffer
  4199. (insert-file-contents file)
  4200. (buffer-string))))
  4201. (defun org-extract-log-state-settings (x)
  4202. "Extract the log state setting from a TODO keyword string.
  4203. This will extract info from a string like \"WAIT(w@/!)\"."
  4204. (let (kw key log1 log2)
  4205. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  4206. (setq kw (match-string 1 x)
  4207. key (and (match-end 2) (match-string 2 x))
  4208. log1 (and (match-end 3) (match-string 3 x))
  4209. log2 (and (match-end 4) (match-string 4 x)))
  4210. (and (or log1 log2)
  4211. (list kw
  4212. (and log1 (if (equal log1 "!") 'time 'note))
  4213. (and log2 (if (equal log2 "!") 'time 'note)))))))
  4214. (defun org-remove-keyword-keys (list)
  4215. "Remove a pair of parenthesis at the end of each string in LIST."
  4216. (mapcar (lambda (x)
  4217. (if (string-match "(.*)$" x)
  4218. (substring x 0 (match-beginning 0))
  4219. x))
  4220. list))
  4221. (defun org-assign-fast-keys (alist)
  4222. "Assign fast keys to a keyword-key alist.
  4223. Respect keys that are already there."
  4224. (let (new e (alt ?0))
  4225. (while (setq e (pop alist))
  4226. (if (or (memq (car e) '(:newline :endgroup :startgroup))
  4227. (cdr e)) ;; Key already assigned.
  4228. (push e new)
  4229. (let ((clist (string-to-list (downcase (car e))))
  4230. (used (append new alist)))
  4231. (when (= (car clist) ?@)
  4232. (pop clist))
  4233. (while (and clist (rassoc (car clist) used))
  4234. (pop clist))
  4235. (unless clist
  4236. (while (rassoc alt used)
  4237. (incf alt)))
  4238. (push (cons (car e) (or (car clist) alt)) new))))
  4239. (nreverse new)))
  4240. ;;; Some variables used in various places
  4241. (defvar org-window-configuration nil
  4242. "Used in various places to store a window configuration.")
  4243. (defvar org-selected-window nil
  4244. "Used in various places to store a window configuration.")
  4245. (defvar org-finish-function nil
  4246. "Function to be called when `C-c C-c' is used.
  4247. This is for getting out of special buffers like remember.")
  4248. ;; FIXME: Occasionally check by commenting these, to make sure
  4249. ;; no other functions uses these, forgetting to let-bind them.
  4250. (defvar entry)
  4251. (defvar last-state)
  4252. (defvar date)
  4253. ;; Defined somewhere in this file, but used before definition.
  4254. (defvar org-entities) ;; defined in org-entities.el
  4255. (defvar org-struct-menu)
  4256. (defvar org-org-menu)
  4257. (defvar org-tbl-menu)
  4258. ;;;; Define the Org-mode
  4259. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  4260. (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"))
  4261. ;; We use a before-change function to check if a table might need
  4262. ;; an update.
  4263. (defvar org-table-may-need-update t
  4264. "Indicates that a table might need an update.
  4265. This variable is set by `org-before-change-function'.
  4266. `org-table-align' sets it back to nil.")
  4267. (defun org-before-change-function (beg end)
  4268. "Every change indicates that a table might need an update."
  4269. (setq org-table-may-need-update t))
  4270. (defvar org-mode-map)
  4271. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  4272. (defvar org-inhibit-startup-visibility-stuff nil) ; Dynamically-scoped param.
  4273. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  4274. (defvar org-inhibit-logging nil) ; Dynamically-scoped param.
  4275. (defvar org-inhibit-blocking nil) ; Dynamically-scoped param.
  4276. (defvar org-table-buffer-is-an nil)
  4277. ;; `org-outline-regexp' ought to be a defconst but is let-binding in
  4278. ;; some places -- e.g. see the macro org-with-limited-levels.
  4279. ;;
  4280. ;; In Org buffers, the value of `outline-regexp' is that of
  4281. ;; `org-outline-regexp'. The only function still directly relying on
  4282. ;; `outline-regexp' is `org-overview' so that `org-cycle' can do its
  4283. ;; job when `orgstruct-mode' is active.
  4284. (defvar org-outline-regexp "\\*+ "
  4285. "Regexp to match Org headlines.")
  4286. (defconst org-outline-regexp-bol "^\\*+ "
  4287. "Regexp to match Org headlines.
  4288. This is similar to `org-outline-regexp' but additionally makes
  4289. sure that we are at the beginning of the line.")
  4290. (defconst org-heading-regexp "^\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  4291. "Matches an headline, putting stars and text into groups.
  4292. Stars are put in group 1 and the trimmed body in group 2.")
  4293. (defconst org-heading-keyword-regexp-format
  4294. "^\\(\\*+\\)\\(?: +%s\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  4295. "Printf format for a regexp matching an headline with some keyword.
  4296. This regexp will match the headline of any node which has the
  4297. exact keyword that is put into the format. The keyword isn't in
  4298. any group by default, but the stars and the body are.")
  4299. (defconst org-heading-keyword-maybe-regexp-format
  4300. "^\\(\\*+\\)\\(?: +%s\\)?\\(?: +\\(.*?\\)\\)?[ \t]*$"
  4301. "Printf format for a regexp matching an headline, possibly with some keyword.
  4302. This regexp can match any headline with the specified keyword, or
  4303. without a keyword. The keyword isn't in any group by default,
  4304. but the stars and the body are.")
  4305. ;;;###autoload
  4306. (define-derived-mode org-mode outline-mode "Org"
  4307. "Outline-based notes management and organizer, alias
  4308. \"Carsten's outline-mode for keeping track of everything.\"
  4309. Org-mode develops organizational tasks around a NOTES file which
  4310. contains information about projects as plain text. Org-mode is
  4311. implemented on top of outline-mode, which is ideal to keep the content
  4312. of large files well structured. It supports ToDo items, deadlines and
  4313. time stamps, which magically appear in the diary listing of the Emacs
  4314. calendar. Tables are easily created with a built-in table editor.
  4315. Plain text URL-like links connect to websites, emails (VM), Usenet
  4316. messages (Gnus), BBDB entries, and any files related to the project.
  4317. For printing and sharing of notes, an Org-mode file (or a part of it)
  4318. can be exported as a structured ASCII or HTML file.
  4319. The following commands are available:
  4320. \\{org-mode-map}"
  4321. ;; Get rid of Outline menus, they are not needed
  4322. ;; Need to do this here because define-derived-mode sets up
  4323. ;; the keymap so late. Still, it is a waste to call this each time
  4324. ;; we switch another buffer into org-mode.
  4325. (if (featurep 'xemacs)
  4326. (when (boundp 'outline-mode-menu-heading)
  4327. ;; Assume this is Greg's port, it uses easymenu
  4328. (easy-menu-remove outline-mode-menu-heading)
  4329. (easy-menu-remove outline-mode-menu-show)
  4330. (easy-menu-remove outline-mode-menu-hide))
  4331. (define-key org-mode-map [menu-bar headings] 'undefined)
  4332. (define-key org-mode-map [menu-bar hide] 'undefined)
  4333. (define-key org-mode-map [menu-bar show] 'undefined))
  4334. (org-load-modules-maybe)
  4335. (easy-menu-add org-org-menu)
  4336. (easy-menu-add org-tbl-menu)
  4337. (org-install-agenda-files-menu)
  4338. (if org-descriptive-links (add-to-invisibility-spec '(org-link)))
  4339. (add-to-invisibility-spec '(org-cwidth))
  4340. (add-to-invisibility-spec '(org-hide-block . t))
  4341. (when (featurep 'xemacs)
  4342. (org-set-local 'line-move-ignore-invisible t))
  4343. (org-set-local 'outline-regexp org-outline-regexp)
  4344. (org-set-local 'outline-level 'org-outline-level)
  4345. (setq bidi-paragraph-direction 'left-to-right)
  4346. (when (and org-ellipsis
  4347. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  4348. (fboundp 'make-glyph-code))
  4349. (unless org-display-table
  4350. (setq org-display-table (make-display-table)))
  4351. (set-display-table-slot
  4352. org-display-table 4
  4353. (vconcat (mapcar
  4354. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  4355. org-ellipsis)))
  4356. (if (stringp org-ellipsis) org-ellipsis "..."))))
  4357. (setq buffer-display-table org-display-table))
  4358. (org-set-regexps-and-options)
  4359. (when (and org-tag-faces (not org-tags-special-faces-re))
  4360. ;; tag faces set outside customize.... force initialization.
  4361. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  4362. ;; Calc embedded
  4363. (org-set-local 'calc-embedded-open-mode "# ")
  4364. (modify-syntax-entry ?@ "w")
  4365. (if org-startup-truncated (setq truncate-lines t))
  4366. (org-set-local 'font-lock-unfontify-region-function
  4367. 'org-unfontify-region)
  4368. ;; Activate before-change-function
  4369. (org-set-local 'org-table-may-need-update t)
  4370. (org-add-hook 'before-change-functions 'org-before-change-function nil
  4371. 'local)
  4372. ;; Check for running clock before killing a buffer
  4373. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  4374. ;; Paragraphs and auto-filling
  4375. (org-set-autofill-regexps)
  4376. (setq indent-line-function 'org-indent-line-function)
  4377. (org-update-radio-target-regexp)
  4378. ;; Beginning/end of defun
  4379. (org-set-local 'beginning-of-defun-function 'org-beginning-of-defun)
  4380. (org-set-local 'end-of-defun-function 'org-end-of-defun)
  4381. ;; Next error for sparse trees
  4382. (org-set-local 'next-error-function 'org-occur-next-match)
  4383. ;; Make sure dependence stuff works reliably, even for users who set it
  4384. ;; too late :-(
  4385. (if org-enforce-todo-dependencies
  4386. (add-hook 'org-blocker-hook
  4387. 'org-block-todo-from-children-or-siblings-or-parent)
  4388. (remove-hook 'org-blocker-hook
  4389. 'org-block-todo-from-children-or-siblings-or-parent))
  4390. (if org-enforce-todo-checkbox-dependencies
  4391. (add-hook 'org-blocker-hook
  4392. 'org-block-todo-from-checkboxes)
  4393. (remove-hook 'org-blocker-hook
  4394. 'org-block-todo-from-checkboxes))
  4395. ;; Comment characters
  4396. (org-set-local 'comment-start "#")
  4397. (org-set-local 'comment-padding " ")
  4398. ;; Align options lines
  4399. (org-set-local
  4400. 'align-mode-rules-list
  4401. '((org-in-buffer-settings
  4402. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  4403. (modes . '(org-mode)))))
  4404. ;; Imenu
  4405. (org-set-local 'imenu-create-index-function
  4406. 'org-imenu-get-tree)
  4407. ;; Make isearch reveal context
  4408. (if (or (featurep 'xemacs)
  4409. (not (boundp 'outline-isearch-open-invisible-function)))
  4410. ;; Emacs 21 and XEmacs make use of the hook
  4411. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  4412. ;; Emacs 22 deals with this through a special variable
  4413. (org-set-local 'outline-isearch-open-invisible-function
  4414. (lambda (&rest ignore) (org-show-context 'isearch))))
  4415. ;; Turn on org-beamer-mode?
  4416. (and org-startup-with-beamer-mode (org-beamer-mode 1))
  4417. ;; Setup the pcomplete hooks
  4418. (set (make-local-variable 'pcomplete-command-completion-function)
  4419. 'org-pcomplete-initial)
  4420. (set (make-local-variable 'pcomplete-command-name-function)
  4421. 'org-command-at-point)
  4422. (set (make-local-variable 'pcomplete-default-completion-function)
  4423. 'ignore)
  4424. (set (make-local-variable 'pcomplete-parse-arguments-function)
  4425. 'org-parse-arguments)
  4426. (set (make-local-variable 'pcomplete-termination-string) "")
  4427. (set (make-local-variable 'face-remapping-alist)
  4428. '((default org-default)))
  4429. ;; If empty file that did not turn on org-mode automatically, make it to.
  4430. (if (and org-insert-mode-line-in-empty-file
  4431. (org-called-interactively-p 'any)
  4432. (= (point-min) (point-max)))
  4433. (insert "# -*- mode: org -*-\n\n"))
  4434. (unless org-inhibit-startup
  4435. (when org-startup-align-all-tables
  4436. (let ((bmp (buffer-modified-p)))
  4437. (org-table-map-tables 'org-table-align 'quietly)
  4438. (set-buffer-modified-p bmp)))
  4439. (when org-startup-with-inline-images
  4440. (org-display-inline-images))
  4441. (when org-startup-indented
  4442. (require 'org-indent)
  4443. (org-indent-mode 1))
  4444. (unless org-inhibit-startup-visibility-stuff
  4445. (org-set-startup-visibility))))
  4446. (when (fboundp 'abbrev-table-put)
  4447. (abbrev-table-put org-mode-abbrev-table
  4448. :parents (list text-mode-abbrev-table)))
  4449. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  4450. (defun org-current-time ()
  4451. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  4452. (if (> (car org-time-stamp-rounding-minutes) 1)
  4453. (let ((r (car org-time-stamp-rounding-minutes))
  4454. (time (decode-time)))
  4455. (apply 'encode-time
  4456. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  4457. (nthcdr 2 time))))
  4458. (current-time)))
  4459. (defun org-today ()
  4460. "Return today date, considering `org-extend-today-until'."
  4461. (time-to-days
  4462. (time-subtract (current-time)
  4463. (list 0 (* 3600 org-extend-today-until) 0))))
  4464. ;;;; Font-Lock stuff, including the activators
  4465. (defvar org-mouse-map (make-sparse-keymap))
  4466. (org-defkey org-mouse-map [mouse-2] 'org-open-at-mouse)
  4467. (org-defkey org-mouse-map [mouse-3] 'org-find-file-at-mouse)
  4468. (when org-mouse-1-follows-link
  4469. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  4470. (when org-tab-follows-link
  4471. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  4472. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  4473. (require 'font-lock)
  4474. (defconst org-non-link-chars "]\t\n\r<>")
  4475. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  4476. "shell" "elisp" "doi" "message"))
  4477. (defvar org-link-types-re nil
  4478. "Matches a link that has a url-like prefix like \"http:\"")
  4479. (defvar org-link-re-with-space nil
  4480. "Matches a link with spaces, optional angular brackets around it.")
  4481. (defvar org-link-re-with-space2 nil
  4482. "Matches a link with spaces, optional angular brackets around it.")
  4483. (defvar org-link-re-with-space3 nil
  4484. "Matches a link with spaces, only for internal part in bracket links.")
  4485. (defvar org-angle-link-re nil
  4486. "Matches link with angular brackets, spaces are allowed.")
  4487. (defvar org-plain-link-re nil
  4488. "Matches plain link, without spaces.")
  4489. (defvar org-bracket-link-regexp nil
  4490. "Matches a link in double brackets.")
  4491. (defvar org-bracket-link-analytic-regexp nil
  4492. "Regular expression used to analyze links.
  4493. Here is what the match groups contain after a match:
  4494. 1: http:
  4495. 2: http
  4496. 3: path
  4497. 4: [desc]
  4498. 5: desc")
  4499. (defvar org-bracket-link-analytic-regexp++ nil
  4500. "Like `org-bracket-link-analytic-regexp', but include coderef internal type.")
  4501. (defvar org-any-link-re nil
  4502. "Regular expression matching any link.")
  4503. (defcustom org-match-sexp-depth 3
  4504. "Number of stacked braces for sub/superscript matching.
  4505. This has to be set before loading org.el to be effective."
  4506. :group 'org-export-translation ; ??????????????????????????/
  4507. :type 'integer)
  4508. (defun org-create-multibrace-regexp (left right n)
  4509. "Create a regular expression which will match a balanced sexp.
  4510. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  4511. as single character strings.
  4512. The regexp returned will match the entire expression including the
  4513. delimiters. It will also define a single group which contains the
  4514. match except for the outermost delimiters. The maximum depth of
  4515. stacked delimiters is N. Escaping delimiters is not possible."
  4516. (let* ((nothing (concat "[^" left right "]*?"))
  4517. (or "\\|")
  4518. (re nothing)
  4519. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  4520. (while (> n 1)
  4521. (setq n (1- n)
  4522. re (concat re or next)
  4523. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  4524. (concat left "\\(" re "\\)" right)))
  4525. (defvar org-match-substring-regexp
  4526. (concat
  4527. "\\([^\\]\\|^\\)\\([_^]\\)\\("
  4528. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4529. "\\|"
  4530. "\\(" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  4531. "\\|"
  4532. "\\(\\(?:\\*\\|[-+]?[^-+*!@#$%^_ \t\r\n,:\"?<>~;./{}=()]+\\)\\)\\)")
  4533. "The regular expression matching a sub- or superscript.")
  4534. (defvar org-match-substring-with-braces-regexp
  4535. (concat
  4536. "\\([^\\]\\|^\\)\\([_^]\\)\\("
  4537. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4538. "\\)")
  4539. "The regular expression matching a sub- or superscript, forcing braces.")
  4540. (defun org-make-link-regexps ()
  4541. "Update the link regular expressions.
  4542. This should be called after the variable `org-link-types' has changed."
  4543. (setq org-link-types-re
  4544. (concat
  4545. "\\`\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):")
  4546. org-link-re-with-space
  4547. (concat
  4548. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4549. "\\([^" org-non-link-chars " ]"
  4550. "[^" org-non-link-chars "]*"
  4551. "[^" org-non-link-chars " ]\\)>?")
  4552. org-link-re-with-space2
  4553. (concat
  4554. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4555. "\\([^" org-non-link-chars " ]"
  4556. "[^\t\n\r]*"
  4557. "[^" org-non-link-chars " ]\\)>?")
  4558. org-link-re-with-space3
  4559. (concat
  4560. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4561. "\\([^" org-non-link-chars " ]"
  4562. "[^\t\n\r]*\\)")
  4563. org-angle-link-re
  4564. (concat
  4565. "<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4566. "\\([^" org-non-link-chars " ]"
  4567. "[^" org-non-link-chars "]*"
  4568. "\\)>")
  4569. org-plain-link-re
  4570. (concat
  4571. "\\<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4572. (org-re "\\([^ \t\n()<>]+\\(?:([[:word:]0-9_]+)\\|\\([^[:punct:] \t\n]\\|/\\)\\)\\)"))
  4573. ;; "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  4574. org-bracket-link-regexp
  4575. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  4576. org-bracket-link-analytic-regexp
  4577. (concat
  4578. "\\[\\["
  4579. "\\(\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):\\)?"
  4580. "\\([^]]+\\)"
  4581. "\\]"
  4582. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4583. "\\]")
  4584. org-bracket-link-analytic-regexp++
  4585. (concat
  4586. "\\[\\["
  4587. "\\(\\(" (mapconcat 'regexp-quote (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  4588. "\\([^]]+\\)"
  4589. "\\]"
  4590. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4591. "\\]")
  4592. org-any-link-re
  4593. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  4594. org-angle-link-re "\\)\\|\\("
  4595. org-plain-link-re "\\)")))
  4596. (org-make-link-regexps)
  4597. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^\r\n>]*?\\)>"
  4598. "Regular expression for fast time stamp matching.")
  4599. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?\\)[]>]"
  4600. "Regular expression for fast time stamp matching.")
  4601. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]+0-9>\r\n -]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4602. "Regular expression matching time strings for analysis.
  4603. This one does not require the space after the date, so it can be used
  4604. on a string that terminates immediately after the date.")
  4605. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]+0-9>\r\n -]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4606. "Regular expression matching time strings for analysis.")
  4607. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  4608. "Regular expression matching time stamps, with groups.")
  4609. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  4610. "Regular expression matching time stamps (also [..]), with groups.")
  4611. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  4612. "Regular expression matching a time stamp range.")
  4613. (defconst org-tr-regexp-both
  4614. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  4615. "Regular expression matching a time stamp range.")
  4616. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  4617. org-ts-regexp "\\)?")
  4618. "Regular expression matching a time stamp or time stamp range.")
  4619. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  4620. org-ts-regexp-both "\\)?")
  4621. "Regular expression matching a time stamp or time stamp range.
  4622. The time stamps may be either active or inactive.")
  4623. (defvar org-emph-face nil)
  4624. (defun org-do-emphasis-faces (limit)
  4625. "Run through the buffer and add overlays to emphasised strings."
  4626. (let (rtn a)
  4627. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  4628. (if (not (= (char-after (match-beginning 3))
  4629. (char-after (match-beginning 4))))
  4630. (progn
  4631. (setq rtn t)
  4632. (setq a (assoc (match-string 3) org-emphasis-alist))
  4633. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  4634. 'face
  4635. (nth 1 a))
  4636. (and (nth 4 a)
  4637. (org-remove-flyspell-overlays-in
  4638. (match-beginning 0) (match-end 0)))
  4639. (add-text-properties (match-beginning 2) (match-end 2)
  4640. '(font-lock-multiline t org-emphasis t))
  4641. (when org-hide-emphasis-markers
  4642. (add-text-properties (match-end 4) (match-beginning 5)
  4643. '(invisible org-link))
  4644. (add-text-properties (match-beginning 3) (match-end 3)
  4645. '(invisible org-link)))))
  4646. (backward-char 1))
  4647. rtn))
  4648. (defun org-emphasize (&optional char)
  4649. "Insert or change an emphasis, i.e. a font like bold or italic.
  4650. If there is an active region, change that region to a new emphasis.
  4651. If there is no region, just insert the marker characters and position
  4652. the cursor between them.
  4653. CHAR should be either the marker character, or the first character of the
  4654. HTML tag associated with that emphasis. If CHAR is a space, the means
  4655. to remove the emphasis of the selected region.
  4656. If char is not given (for example in an interactive call) it
  4657. will be prompted for."
  4658. (interactive)
  4659. (let ((eal org-emphasis-alist) e det
  4660. (erc org-emphasis-regexp-components)
  4661. (prompt "")
  4662. (string "") beg end move tag c s)
  4663. (if (org-region-active-p)
  4664. (setq beg (region-beginning) end (region-end)
  4665. string (buffer-substring beg end))
  4666. (setq move t))
  4667. (while (setq e (pop eal))
  4668. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  4669. c (aref tag 0))
  4670. (push (cons c (string-to-char (car e))) det)
  4671. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  4672. (substring tag 1)))))
  4673. (setq det (nreverse det))
  4674. (unless char
  4675. (message "%s" (concat "Emphasis marker or tag:" prompt))
  4676. (setq char (read-char-exclusive)))
  4677. (setq char (or (cdr (assoc char det)) char))
  4678. (if (equal char ?\ )
  4679. (setq s "" move nil)
  4680. (unless (assoc (char-to-string char) org-emphasis-alist)
  4681. (error "No such emphasis marker: \"%c\"" char))
  4682. (setq s (char-to-string char)))
  4683. (while (and (> (length string) 1)
  4684. (equal (substring string 0 1) (substring string -1))
  4685. (assoc (substring string 0 1) org-emphasis-alist))
  4686. (setq string (substring string 1 -1)))
  4687. (setq string (concat s string s))
  4688. (if beg (delete-region beg end))
  4689. (unless (or (bolp)
  4690. (string-match (concat "[" (nth 0 erc) "\n]")
  4691. (char-to-string (char-before (point)))))
  4692. (insert " "))
  4693. (unless (or (eobp)
  4694. (string-match (concat "[" (nth 1 erc) "\n]")
  4695. (char-to-string (char-after (point)))))
  4696. (insert " ") (backward-char 1))
  4697. (insert string)
  4698. (and move (backward-char 1))))
  4699. (defconst org-nonsticky-props
  4700. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  4701. (defsubst org-rear-nonsticky-at (pos)
  4702. (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
  4703. (defun org-activate-plain-links (limit)
  4704. "Run through the buffer and add overlays to links."
  4705. (catch 'exit
  4706. (let (f)
  4707. (if (re-search-forward org-plain-link-re limit t)
  4708. (progn
  4709. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4710. (setq f (get-text-property (match-beginning 0) 'face))
  4711. (if (or (eq f 'org-tag)
  4712. (and (listp f) (memq 'org-tag f)))
  4713. nil
  4714. (add-text-properties (match-beginning 0) (match-end 0)
  4715. (list 'mouse-face 'highlight
  4716. 'face 'org-link
  4717. 'keymap org-mouse-map))
  4718. (org-rear-nonsticky-at (match-end 0)))
  4719. t)))))
  4720. (defun org-activate-code (limit)
  4721. (if (re-search-forward "^[ \t]*\\(:\\(?: .*\\|$\\)\n?\\)" limit t)
  4722. (progn
  4723. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4724. (remove-text-properties (match-beginning 0) (match-end 0)
  4725. '(display t invisible t intangible t))
  4726. t)))
  4727. (defcustom org-src-fontify-natively nil
  4728. "When non-nil, fontify code in code blocks."
  4729. :type 'boolean
  4730. :group 'org-appearance
  4731. :group 'org-babel)
  4732. (defun org-fontify-meta-lines-and-blocks (limit)
  4733. (condition-case nil
  4734. (org-fontify-meta-lines-and-blocks-1 limit)
  4735. (error (message "org-mode fontification error"))))
  4736. (defun org-fontify-meta-lines-and-blocks-1 (limit)
  4737. "Fontify #+ lines and blocks, in the correct ways."
  4738. (let ((case-fold-search t))
  4739. (if (re-search-forward
  4740. "^\\([ \t]*#\\+\\(\\([a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)[ \t]*\\(\\([^ \t\n]*\\)[ \t]*\\(.*\\)\\)\\)"
  4741. limit t)
  4742. (let ((beg (match-beginning 0))
  4743. (block-start (match-end 0))
  4744. (block-end nil)
  4745. (lang (match-string 7))
  4746. (beg1 (line-beginning-position 2))
  4747. (dc1 (downcase (match-string 2)))
  4748. (dc3 (downcase (match-string 3)))
  4749. end end1 quoting block-type ovl)
  4750. (cond
  4751. ((member dc1 '("html:" "ascii:" "latex:" "docbook:"))
  4752. ;; a single line of backend-specific content
  4753. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4754. (remove-text-properties (match-beginning 0) (match-end 0)
  4755. '(display t invisible t intangible t))
  4756. (add-text-properties (match-beginning 1) (match-end 3)
  4757. '(font-lock-fontified t face org-meta-line))
  4758. (add-text-properties (match-beginning 6) (+ (match-end 6) 1)
  4759. '(font-lock-fontified t face org-block))
  4760. ; for backend-specific code
  4761. t)
  4762. ((and (match-end 4) (equal dc3 "begin"))
  4763. ;; Truly a block
  4764. (setq block-type (downcase (match-string 5))
  4765. quoting (member block-type org-protecting-blocks))
  4766. (when (re-search-forward
  4767. (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
  4768. nil t) ;; on purpose, we look further than LIMIT
  4769. (setq end (match-end 0) end1 (1- (match-beginning 0)))
  4770. (setq block-end (match-beginning 0))
  4771. (when quoting
  4772. (remove-text-properties beg end
  4773. '(display t invisible t intangible t)))
  4774. (add-text-properties
  4775. beg end
  4776. '(font-lock-fontified t font-lock-multiline t))
  4777. (add-text-properties beg beg1 '(face org-meta-line))
  4778. (add-text-properties end1 (min (point-max) (1+ end))
  4779. '(face org-meta-line)) ; for end_src
  4780. (cond
  4781. ((and lang (not (string= lang "")) org-src-fontify-natively)
  4782. (org-src-font-lock-fontify-block lang block-start block-end)
  4783. ;; remove old background overlays
  4784. (mapc (lambda (ov)
  4785. (if (eq (overlay-get ov 'face) 'org-block-background)
  4786. (delete-overlay ov)))
  4787. (overlays-at (/ (+ beg1 block-end) 2)))
  4788. ;; add a background overlay
  4789. (setq ovl (make-overlay beg1 block-end))
  4790. (overlay-put ovl 'face 'org-block-background)
  4791. (overlay-put ovl 'evaporate t)) ;; make it go away when empty
  4792. (quoting
  4793. (add-text-properties beg1 (min (point-max) (1+ end1))
  4794. '(face org-block))) ; end of source block
  4795. ((not org-fontify-quote-and-verse-blocks))
  4796. ((string= block-type "quote")
  4797. (add-text-properties beg1 (1+ end1) '(face org-quote)))
  4798. ((string= block-type "verse")
  4799. (add-text-properties beg1 (1+ end1) '(face org-verse))))
  4800. (add-text-properties beg beg1 '(face org-block-begin-line))
  4801. (add-text-properties (1+ end) (1+ end1) '(face org-block-end-line))
  4802. t))
  4803. ((member dc1 '("title:" "author:" "email:" "date:"))
  4804. (add-text-properties
  4805. beg (match-end 3)
  4806. (if (member (intern (substring dc1 0 -1)) org-hidden-keywords)
  4807. '(font-lock-fontified t invisible t)
  4808. '(font-lock-fontified t face org-document-info-keyword)))
  4809. (add-text-properties
  4810. (match-beginning 6) (match-end 6)
  4811. (if (string-equal dc1 "title:")
  4812. '(font-lock-fontified t face org-document-title)
  4813. '(font-lock-fontified t face org-document-info))))
  4814. ((not (member (char-after beg) '(?\ ?\t)))
  4815. ;; just any other in-buffer setting, but not indented
  4816. (add-text-properties
  4817. beg (1+ (match-end 0))
  4818. '(font-lock-fontified t face org-meta-line))
  4819. t)
  4820. ((or (member dc1 '("begin:" "end:" "caption:" "label:"
  4821. "orgtbl:" "tblfm:" "tblname:" "results:"
  4822. "call:" "header:" "headers:"))
  4823. (and (match-end 4) (equal dc3 "attr")))
  4824. (add-text-properties
  4825. beg (match-end 0)
  4826. '(font-lock-fontified t face org-meta-line))
  4827. t)
  4828. ((member dc3 '(" " ""))
  4829. (add-text-properties
  4830. beg (match-end 0)
  4831. '(font-lock-fontified t face font-lock-comment-face)))
  4832. (t nil))))))
  4833. (defun org-activate-angle-links (limit)
  4834. "Run through the buffer and add overlays to links."
  4835. (if (re-search-forward org-angle-link-re limit t)
  4836. (progn
  4837. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4838. (add-text-properties (match-beginning 0) (match-end 0)
  4839. (list 'mouse-face 'highlight
  4840. 'keymap org-mouse-map))
  4841. (org-rear-nonsticky-at (match-end 0))
  4842. t)))
  4843. (defun org-activate-footnote-links (limit)
  4844. "Run through the buffer and add overlays to footnotes."
  4845. (let ((fn (org-footnote-next-reference-or-definition limit)))
  4846. (when fn
  4847. (let ((beg (nth 1 fn)) (end (nth 2 fn)))
  4848. (org-remove-flyspell-overlays-in beg end)
  4849. (add-text-properties beg end
  4850. (list 'mouse-face 'highlight
  4851. 'keymap org-mouse-map
  4852. 'help-echo
  4853. (if (= (point-at-bol) beg)
  4854. "Footnote definition"
  4855. "Footnote reference")
  4856. 'font-lock-fontified t
  4857. 'font-lock-multiline t
  4858. 'face 'org-footnote))))))
  4859. (defun org-activate-bracket-links (limit)
  4860. "Run through the buffer and add overlays to bracketed links."
  4861. (if (re-search-forward org-bracket-link-regexp limit t)
  4862. (let* ((help (concat "LINK: "
  4863. (org-match-string-no-properties 1)))
  4864. ;; FIXME: above we should remove the escapes.
  4865. ;; but that requires another match, protecting match data,
  4866. ;; a lot of overhead for font-lock.
  4867. (ip (org-maybe-intangible
  4868. (list 'invisible 'org-link
  4869. 'keymap org-mouse-map 'mouse-face 'highlight
  4870. 'font-lock-multiline t 'help-echo help)))
  4871. (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
  4872. 'font-lock-multiline t 'help-echo help)))
  4873. ;; We need to remove the invisible property here. Table narrowing
  4874. ;; may have made some of this invisible.
  4875. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4876. (remove-text-properties (match-beginning 0) (match-end 0)
  4877. '(invisible nil))
  4878. (if (match-end 3)
  4879. (progn
  4880. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  4881. (org-rear-nonsticky-at (match-beginning 3))
  4882. (add-text-properties (match-beginning 3) (match-end 3) vp)
  4883. (org-rear-nonsticky-at (match-end 3))
  4884. (add-text-properties (match-end 3) (match-end 0) ip)
  4885. (org-rear-nonsticky-at (match-end 0)))
  4886. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  4887. (org-rear-nonsticky-at (match-beginning 1))
  4888. (add-text-properties (match-beginning 1) (match-end 1) vp)
  4889. (org-rear-nonsticky-at (match-end 1))
  4890. (add-text-properties (match-end 1) (match-end 0) ip)
  4891. (org-rear-nonsticky-at (match-end 0)))
  4892. t)))
  4893. (defun org-activate-dates (limit)
  4894. "Run through the buffer and add overlays to dates."
  4895. (if (re-search-forward org-tsr-regexp-both limit t)
  4896. (progn
  4897. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4898. (add-text-properties (match-beginning 0) (match-end 0)
  4899. (list 'mouse-face 'highlight
  4900. 'keymap org-mouse-map))
  4901. (org-rear-nonsticky-at (match-end 0))
  4902. (when org-display-custom-times
  4903. (if (match-end 3)
  4904. (org-display-custom-time (match-beginning 3) (match-end 3)))
  4905. (org-display-custom-time (match-beginning 1) (match-end 1)))
  4906. t)))
  4907. (defvar org-target-link-regexp nil
  4908. "Regular expression matching radio targets in plain text.")
  4909. (make-variable-buffer-local 'org-target-link-regexp)
  4910. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  4911. "Regular expression matching a link target.")
  4912. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  4913. "Regular expression matching a radio target.")
  4914. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  4915. "Regular expression matching any target.")
  4916. (defun org-activate-target-links (limit)
  4917. "Run through the buffer and add overlays to target matches."
  4918. (when org-target-link-regexp
  4919. (let ((case-fold-search t))
  4920. (if (re-search-forward org-target-link-regexp limit t)
  4921. (progn
  4922. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4923. (add-text-properties (match-beginning 0) (match-end 0)
  4924. (list 'mouse-face 'highlight
  4925. 'keymap org-mouse-map
  4926. 'help-echo "Radio target link"
  4927. 'org-linked-text t))
  4928. (org-rear-nonsticky-at (match-end 0))
  4929. t)))))
  4930. (defun org-update-radio-target-regexp ()
  4931. "Find all radio targets in this file and update the regular expression."
  4932. (interactive)
  4933. (when (memq 'radio org-activate-links)
  4934. (setq org-target-link-regexp
  4935. (org-make-target-link-regexp (org-all-targets 'radio)))
  4936. (org-restart-font-lock)))
  4937. (defun org-hide-wide-columns (limit)
  4938. (let (s e)
  4939. (setq s (text-property-any (point) (or limit (point-max))
  4940. 'org-cwidth t))
  4941. (when s
  4942. (setq e (next-single-property-change s 'org-cwidth))
  4943. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  4944. (goto-char e)
  4945. t)))
  4946. (defvar org-latex-and-specials-regexp nil
  4947. "Regular expression for highlighting export special stuff.")
  4948. (defvar org-match-substring-regexp)
  4949. (defvar org-match-substring-with-braces-regexp)
  4950. ;; This should be with the exporter code, but we also use if for font-locking
  4951. (defconst org-export-html-special-string-regexps
  4952. '(("\\\\-" . "&shy;")
  4953. ("---\\([^-]\\)" . "&mdash;\\1")
  4954. ("--\\([^-]\\)" . "&ndash;\\1")
  4955. ("\\.\\.\\." . "&hellip;"))
  4956. "Regular expressions for special string conversion.")
  4957. (defun org-compute-latex-and-specials-regexp ()
  4958. "Compute regular expression for stuff treated specially by exporters."
  4959. (if (not org-highlight-latex-fragments-and-specials)
  4960. (org-set-local 'org-latex-and-specials-regexp nil)
  4961. (require 'org-exp)
  4962. (let*
  4963. ((matchers (plist-get org-format-latex-options :matchers))
  4964. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  4965. org-latex-regexps)))
  4966. (org-export-allow-BIND nil)
  4967. (options (org-combine-plists (org-default-export-plist)
  4968. (org-infile-export-plist)))
  4969. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  4970. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  4971. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  4972. (org-export-html-expand (plist-get options :expand-quoted-html))
  4973. (org-export-with-special-strings (plist-get options :special-strings))
  4974. (re-sub
  4975. (cond
  4976. ((equal org-export-with-sub-superscripts '{})
  4977. (list org-match-substring-with-braces-regexp))
  4978. (org-export-with-sub-superscripts
  4979. (list org-match-substring-regexp))
  4980. (t nil)))
  4981. (re-latex
  4982. (if org-export-with-LaTeX-fragments
  4983. (mapcar (lambda (x) (nth 1 x)) latexs)))
  4984. (re-macros
  4985. (if org-export-with-TeX-macros
  4986. (list (concat "\\\\"
  4987. (regexp-opt
  4988. (append
  4989. (delq nil
  4990. (mapcar 'car-safe
  4991. (append org-entities-user
  4992. org-entities)))
  4993. (if (boundp 'org-latex-entities)
  4994. (mapcar (lambda (x)
  4995. (or (car-safe x) x))
  4996. org-latex-entities)
  4997. nil))
  4998. 'words))) ; FIXME
  4999. ))
  5000. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  5001. (re-special (if org-export-with-special-strings
  5002. (mapcar (lambda (x) (car x))
  5003. org-export-html-special-string-regexps)))
  5004. (re-rest
  5005. (delq nil
  5006. (list
  5007. (if org-export-html-expand "@<[^>\n]+>")
  5008. ))))
  5009. (org-set-local
  5010. 'org-latex-and-specials-regexp
  5011. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  5012. re-rest) "\\|")))))
  5013. (defun org-do-latex-and-special-faces (limit)
  5014. "Run through the buffer and add overlays to links."
  5015. (when org-latex-and-specials-regexp
  5016. (let (rtn d)
  5017. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  5018. limit t))
  5019. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  5020. 'face))
  5021. '(org-code org-verbatim underline)))
  5022. (progn
  5023. (setq rtn t
  5024. d (cond ((member (char-after (1+ (match-beginning 0)))
  5025. '(?_ ?^)) 1)
  5026. (t 0)))
  5027. (font-lock-prepend-text-property
  5028. (+ d (match-beginning 0)) (match-end 0)
  5029. 'face 'org-latex-and-export-specials)
  5030. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  5031. '(font-lock-multiline t)))))
  5032. rtn)))
  5033. (defun org-restart-font-lock ()
  5034. "Restart `font-lock-mode', to force refontification."
  5035. (when (and (boundp 'font-lock-mode) font-lock-mode)
  5036. (font-lock-mode -1)
  5037. (font-lock-mode 1)))
  5038. (defun org-all-targets (&optional radio)
  5039. "Return a list of all targets in this file.
  5040. With optional argument RADIO, only find radio targets."
  5041. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  5042. rtn)
  5043. (save-excursion
  5044. (goto-char (point-min))
  5045. (while (re-search-forward re nil t)
  5046. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  5047. rtn)))
  5048. (defun org-make-target-link-regexp (targets)
  5049. "Make regular expression matching all strings in TARGETS.
  5050. The regular expression finds the targets also if there is a line break
  5051. between words."
  5052. (and targets
  5053. (concat
  5054. "\\<\\("
  5055. (mapconcat
  5056. (lambda (x)
  5057. (setq x (regexp-quote x))
  5058. (while (string-match " +" x)
  5059. (setq x (replace-match "\\s-+" t t x)))
  5060. x)
  5061. targets
  5062. "\\|")
  5063. "\\)\\>")))
  5064. (defun org-activate-tags (limit)
  5065. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \r\n]") limit t)
  5066. (progn
  5067. (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
  5068. (add-text-properties (match-beginning 1) (match-end 1)
  5069. (list 'mouse-face 'highlight
  5070. 'keymap org-mouse-map))
  5071. (org-rear-nonsticky-at (match-end 1))
  5072. t)))
  5073. (defun org-outline-level ()
  5074. "Compute the outline level of the heading at point.
  5075. This function assumes that the cursor is at the beginning of a line matched
  5076. by `outline-regexp'. Otherwise it returns garbage.
  5077. If this is called at a normal headline, the level is the number of stars.
  5078. Use `org-reduced-level' to remove the effect of `org-odd-levels'."
  5079. (save-excursion
  5080. (looking-at org-outline-regexp)
  5081. (1- (- (match-end 0) (match-beginning 0)))))
  5082. (defvar org-font-lock-keywords nil)
  5083. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  5084. "Regular expression matching a property line.")
  5085. (defvar org-font-lock-hook nil
  5086. "Functions to be called for special font lock stuff.")
  5087. (defvar org-font-lock-set-keywords-hook nil
  5088. "Functions that can manipulate `org-font-lock-extra-keywords'.
  5089. This is calles after `org-font-lock-extra-keywords' is defined, but before
  5090. it is installed to be used by font lock. This can be useful if something
  5091. needs to be inserted at a specific position in the font-lock sequence.")
  5092. (defun org-font-lock-hook (limit)
  5093. (run-hook-with-args 'org-font-lock-hook limit))
  5094. (defun org-set-font-lock-defaults ()
  5095. (let* ((em org-fontify-emphasized-text)
  5096. (lk org-activate-links)
  5097. (org-font-lock-extra-keywords
  5098. (list
  5099. ;; Call the hook
  5100. '(org-font-lock-hook)
  5101. ;; Headlines
  5102. `(,(if org-fontify-whole-heading-line
  5103. "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
  5104. "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
  5105. (1 (org-get-level-face 1))
  5106. (2 (org-get-level-face 2))
  5107. (3 (org-get-level-face 3)))
  5108. ;; Table lines
  5109. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  5110. (1 'org-table t))
  5111. ;; Table internals
  5112. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  5113. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  5114. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  5115. '("| *\\(<[lrc]?[0-9]*>\\)" (1 'org-formula t))
  5116. ;; Drawers
  5117. (list org-drawer-regexp '(0 'org-special-keyword t))
  5118. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  5119. ;; Properties
  5120. (list org-property-re
  5121. '(1 'org-special-keyword t)
  5122. '(3 'org-property-value t))
  5123. ;; Links
  5124. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  5125. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  5126. (if (memq 'plain lk) '(org-activate-plain-links))
  5127. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  5128. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  5129. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  5130. (if (memq 'footnote lk) '(org-activate-footnote-links))
  5131. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  5132. '(org-hide-wide-columns (0 nil append))
  5133. ;; TODO keyword
  5134. (list (format org-heading-keyword-regexp-format
  5135. org-todo-regexp)
  5136. '(2 (org-get-todo-face 2) t))
  5137. ;; DONE
  5138. (if org-fontify-done-headline
  5139. (list (format org-heading-keyword-regexp-format
  5140. (concat
  5141. "\\("
  5142. (mapconcat 'regexp-quote org-done-keywords "\\|")
  5143. "\\)"))
  5144. '(2 'org-headline-done t))
  5145. nil)
  5146. ;; Priorities
  5147. '(org-font-lock-add-priority-faces)
  5148. ;; Tags
  5149. '(org-font-lock-add-tag-faces)
  5150. ;; Special keywords
  5151. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  5152. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  5153. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  5154. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  5155. ;; Emphasis
  5156. (if em
  5157. (if (featurep 'xemacs)
  5158. '(org-do-emphasis-faces (0 nil append))
  5159. '(org-do-emphasis-faces)))
  5160. ;; Checkboxes
  5161. '("^[ \t]*\\(?:[-+*]\\|[0-9]+[.)]\\)[ \t]+\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?\\(\\[[- X]\\]\\)"
  5162. 1 'org-checkbox prepend)
  5163. (if (cdr (assq 'checkbox org-list-automatic-rules))
  5164. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  5165. (0 (org-get-checkbox-statistics-face) t)))
  5166. ;; Description list items
  5167. '("^[ \t]*[-+*][ \t]+\\(.*?[ \t]+::\\)\\([ \t]+\\|$\\)"
  5168. 1 'bold prepend)
  5169. ;; ARCHIVEd headings
  5170. (list (concat
  5171. org-outline-regexp-bol
  5172. "\\(.*:" org-archive-tag ":.*\\)")
  5173. '(1 'org-archived prepend))
  5174. ;; Specials
  5175. '(org-do-latex-and-special-faces)
  5176. '(org-fontify-entities)
  5177. '(org-raise-scripts)
  5178. ;; Code
  5179. '(org-activate-code (1 'org-code t))
  5180. ;; COMMENT
  5181. (list (format org-heading-keyword-regexp-format
  5182. (concat "\\("
  5183. org-comment-string "\\|" org-quote-string
  5184. "\\)"))
  5185. '(2 'org-special-keyword t))
  5186. '("^#.*" (0 'font-lock-comment-face t))
  5187. ;; Blocks and meta lines
  5188. '(org-fontify-meta-lines-and-blocks)
  5189. )))
  5190. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  5191. (run-hooks 'org-font-lock-set-keywords-hook)
  5192. ;; Now set the full font-lock-keywords
  5193. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  5194. (org-set-local 'font-lock-defaults
  5195. '(org-font-lock-keywords t nil nil backward-paragraph))
  5196. (kill-local-variable 'font-lock-keywords) nil))
  5197. (defun org-toggle-pretty-entities ()
  5198. "Toggle the composition display of entities as UTF8 characters."
  5199. (interactive)
  5200. (org-set-local 'org-pretty-entities (not org-pretty-entities))
  5201. (org-restart-font-lock)
  5202. (if org-pretty-entities
  5203. (message "Entities are displayed as UTF8 characers")
  5204. (save-restriction
  5205. (widen)
  5206. (org-decompose-region (point-min) (point-max))
  5207. (message "Entities are displayed plain"))))
  5208. (defun org-fontify-entities (limit)
  5209. "Find an entity to fontify."
  5210. (let (ee)
  5211. (when org-pretty-entities
  5212. (catch 'match
  5213. (while (re-search-forward
  5214. "\\\\\\(frac[13][24]\\|[a-zA-Z]+\\)\\($\\|[^[:alpha:]\n]\\)"
  5215. limit t)
  5216. (if (and (not (org-in-indented-comment-line))
  5217. (setq ee (org-entity-get (match-string 1)))
  5218. (= (length (nth 6 ee)) 1))
  5219. (progn
  5220. (add-text-properties
  5221. (match-beginning 0) (match-end 1)
  5222. (list 'font-lock-fontified t))
  5223. (compose-region (match-beginning 0) (match-end 1)
  5224. (nth 6 ee) nil)
  5225. (backward-char 1)
  5226. (throw 'match t))))
  5227. nil))))
  5228. (defun org-fontify-like-in-org-mode (s &optional odd-levels)
  5229. "Fontify string S like in Org-mode."
  5230. (with-temp-buffer
  5231. (insert s)
  5232. (let ((org-odd-levels-only odd-levels))
  5233. (org-mode)
  5234. (font-lock-fontify-buffer)
  5235. (buffer-string))))
  5236. (defvar org-m nil)
  5237. (defvar org-l nil)
  5238. (defvar org-f nil)
  5239. (defun org-get-level-face (n)
  5240. "Get the right face for match N in font-lock matching of headlines."
  5241. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  5242. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  5243. (if org-cycle-level-faces
  5244. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  5245. (setq org-f (nth (1- (min org-l org-n-level-faces)) org-level-faces)))
  5246. (cond
  5247. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  5248. ((eq n 2) org-f)
  5249. (t (if org-level-color-stars-only nil org-f))))
  5250. (defun org-get-todo-face (kwd)
  5251. "Get the right face for a TODO keyword KWD.
  5252. If KWD is a number, get the corresponding match group."
  5253. (if (numberp kwd) (setq kwd (match-string kwd)))
  5254. (or (org-face-from-face-or-color
  5255. 'todo 'org-todo (cdr (assoc kwd org-todo-keyword-faces)))
  5256. (and (member kwd org-done-keywords) 'org-done)
  5257. 'org-todo))
  5258. (defun org-face-from-face-or-color (context inherit face-or-color)
  5259. "Create a face list that inherits INHERIT, but sets the foreground color.
  5260. When FACE-OR-COLOR is not a string, just return it."
  5261. (if (stringp face-or-color)
  5262. (list :inherit inherit
  5263. (cdr (assoc context org-faces-easy-properties))
  5264. face-or-color)
  5265. face-or-color))
  5266. (defun org-font-lock-add-tag-faces (limit)
  5267. "Add the special tag faces."
  5268. (when (and org-tag-faces org-tags-special-faces-re)
  5269. (while (re-search-forward org-tags-special-faces-re limit t)
  5270. (add-text-properties (match-beginning 1) (match-end 1)
  5271. (list 'face (org-get-tag-face 1)
  5272. 'font-lock-fontified t))
  5273. (backward-char 1))))
  5274. (defun org-font-lock-add-priority-faces (limit)
  5275. "Add the special priority faces."
  5276. (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
  5277. (add-text-properties
  5278. (match-beginning 0) (match-end 0)
  5279. (list 'face (or (org-face-from-face-or-color
  5280. 'priority 'org-special-keyword
  5281. (cdr (assoc (char-after (match-beginning 1))
  5282. org-priority-faces)))
  5283. 'org-special-keyword)
  5284. 'font-lock-fontified t))))
  5285. (defun org-get-tag-face (kwd)
  5286. "Get the right face for a TODO keyword KWD.
  5287. If KWD is a number, get the corresponding match group."
  5288. (if (numberp kwd) (setq kwd (match-string kwd)))
  5289. (or (org-face-from-face-or-color
  5290. 'tag 'org-tag (cdr (assoc kwd org-tag-faces)))
  5291. 'org-tag))
  5292. (defun org-unfontify-region (beg end &optional maybe_loudly)
  5293. "Remove fontification and activation overlays from links."
  5294. (font-lock-default-unfontify-region beg end)
  5295. (let* ((buffer-undo-list t)
  5296. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  5297. (inhibit-modification-hooks t)
  5298. deactivate-mark buffer-file-name buffer-file-truename)
  5299. (org-decompose-region beg end)
  5300. (remove-text-properties beg end
  5301. '(mouse-face t keymap t org-linked-text t
  5302. invisible t intangible t
  5303. org-no-flyspell t org-emphasis t))
  5304. (org-remove-font-lock-display-properties beg end)))
  5305. (defconst org-script-display '(((raise -0.3) (height 0.7))
  5306. ((raise 0.3) (height 0.7))
  5307. ((raise -0.5))
  5308. ((raise 0.5)))
  5309. "Display properties for showing superscripts and subscripts.")
  5310. (defun org-remove-font-lock-display-properties (beg end)
  5311. "Remove specific display properties that have been added by font lock.
  5312. The will remove the raise properties that are used to show superscripts
  5313. and subscripts."
  5314. (let (next prop)
  5315. (while (< beg end)
  5316. (setq next (next-single-property-change beg 'display nil end)
  5317. prop (get-text-property beg 'display))
  5318. (if (member prop org-script-display)
  5319. (put-text-property beg next 'display nil))
  5320. (setq beg next))))
  5321. (defun org-raise-scripts (limit)
  5322. "Add raise properties to sub/superscripts."
  5323. (when (and org-pretty-entities org-pretty-entities-include-sub-superscripts)
  5324. (if (re-search-forward
  5325. (if (eq org-use-sub-superscripts t)
  5326. org-match-substring-regexp
  5327. org-match-substring-with-braces-regexp)
  5328. limit t)
  5329. (let* ((pos (point)) table-p comment-p
  5330. (mpos (match-beginning 3))
  5331. (emph-p (get-text-property mpos 'org-emphasis))
  5332. (link-p (get-text-property mpos 'mouse-face))
  5333. (keyw-p (eq 'org-special-keyword (get-text-property mpos 'face))))
  5334. (goto-char (point-at-bol))
  5335. (setq table-p (org-looking-at-p org-table-dataline-regexp)
  5336. comment-p (org-looking-at-p "[ \t]*#"))
  5337. (goto-char pos)
  5338. ;; FIXME: Should we go back one character here, for a_b^c
  5339. ;; (goto-char (1- pos)) ;????????????????????
  5340. (if (or comment-p emph-p link-p keyw-p)
  5341. t
  5342. (put-text-property (match-beginning 3) (match-end 0)
  5343. 'display
  5344. (if (equal (char-after (match-beginning 2)) ?^)
  5345. (nth (if table-p 3 1) org-script-display)
  5346. (nth (if table-p 2 0) org-script-display)))
  5347. (add-text-properties (match-beginning 2) (match-end 2)
  5348. (list 'invisible t
  5349. 'org-dwidth t 'org-dwidth-n 1))
  5350. (if (and (eq (char-after (match-beginning 3)) ?{)
  5351. (eq (char-before (match-end 3)) ?}))
  5352. (progn
  5353. (add-text-properties
  5354. (match-beginning 3) (1+ (match-beginning 3))
  5355. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))
  5356. (add-text-properties
  5357. (1- (match-end 3)) (match-end 3)
  5358. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))))
  5359. t)))))
  5360. ;;;; Visibility cycling, including org-goto and indirect buffer
  5361. ;;; Cycling
  5362. (defvar org-cycle-global-status nil)
  5363. (make-variable-buffer-local 'org-cycle-global-status)
  5364. (defvar org-cycle-subtree-status nil)
  5365. (make-variable-buffer-local 'org-cycle-subtree-status)
  5366. ;;;###autoload
  5367. (defvar org-inlinetask-min-level)
  5368. (defun org-cycle (&optional arg)
  5369. "TAB-action and visibility cycling for Org-mode.
  5370. This is the command invoked in Org-mode by the TAB key. Its main purpose
  5371. is outline visibility cycling, but it also invokes other actions
  5372. in special contexts.
  5373. - When this function is called with a prefix argument, rotate the entire
  5374. buffer through 3 states (global cycling)
  5375. 1. OVERVIEW: Show only top-level headlines.
  5376. 2. CONTENTS: Show all headlines of all levels, but no body text.
  5377. 3. SHOW ALL: Show everything.
  5378. When called with two `C-u C-u' prefixes, switch to the startup visibility,
  5379. determined by the variable `org-startup-folded', and by any VISIBILITY
  5380. properties in the buffer.
  5381. When called with three `C-u C-u C-u' prefixed, show the entire buffer,
  5382. including any drawers.
  5383. - When inside a table, re-align the table and move to the next field.
  5384. - When point is at the beginning of a headline, rotate the subtree started
  5385. by this line through 3 different states (local cycling)
  5386. 1. FOLDED: Only the main headline is shown.
  5387. 2. CHILDREN: The main headline and the direct children are shown.
  5388. From this state, you can move to one of the children
  5389. and zoom in further.
  5390. 3. SUBTREE: Show the entire subtree, including body text.
  5391. If there is no subtree, switch directly from CHILDREN to FOLDED.
  5392. - When point is at the beginning of an empty headline and the variable
  5393. `org-cycle-level-after-item/entry-creation' is set, cycle the level
  5394. of the headline by demoting and promoting it to likely levels. This
  5395. speeds up creation document structure by pressing TAB once or several
  5396. times right after creating a new headline.
  5397. - When there is a numeric prefix, go up to a heading with level ARG, do
  5398. a `show-subtree' and return to the previous cursor position. If ARG
  5399. is negative, go up that many levels.
  5400. - When point is not at the beginning of a headline, execute the global
  5401. binding for TAB, which is re-indenting the line. See the option
  5402. `org-cycle-emulate-tab' for details.
  5403. - Special case: if point is at the beginning of the buffer and there is
  5404. no headline in line 1, this function will act as if called with prefix arg
  5405. (C-u TAB, same as S-TAB) also when called without prefix arg.
  5406. But only if also the variable `org-cycle-global-at-bob' is t."
  5407. (interactive "P")
  5408. (org-load-modules-maybe)
  5409. (unless (or (run-hook-with-args-until-success 'org-tab-first-hook)
  5410. (and org-cycle-level-after-item/entry-creation
  5411. (or (org-cycle-level)
  5412. (org-cycle-item-indentation))))
  5413. (let* ((limit-level
  5414. (or org-cycle-max-level
  5415. (and (boundp 'org-inlinetask-min-level)
  5416. org-inlinetask-min-level
  5417. (1- org-inlinetask-min-level))))
  5418. (nstars (and limit-level
  5419. (if org-odd-levels-only
  5420. (and limit-level (1- (* limit-level 2)))
  5421. limit-level)))
  5422. (org-outline-regexp
  5423. (if (not (eq major-mode 'org-mode))
  5424. outline-regexp
  5425. (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ "))))
  5426. (bob-special (and org-cycle-global-at-bob (not arg) (bobp)
  5427. (not (looking-at org-outline-regexp))))
  5428. (org-cycle-hook
  5429. (if bob-special
  5430. (delq 'org-optimize-window-after-visibility-change
  5431. (copy-sequence org-cycle-hook))
  5432. org-cycle-hook))
  5433. (pos (point)))
  5434. (if (or bob-special (equal arg '(4)))
  5435. ;; special case: use global cycling
  5436. (setq arg t))
  5437. (cond
  5438. ((equal arg '(16))
  5439. (setq last-command 'dummy)
  5440. (org-set-startup-visibility)
  5441. (message "Startup visibility, plus VISIBILITY properties"))
  5442. ((equal arg '(64))
  5443. (show-all)
  5444. (message "Entire buffer visible, including drawers"))
  5445. ;; Table: enter it or move to the next field.
  5446. ((org-at-table-p 'any)
  5447. (if (org-at-table.el-p)
  5448. (message "Use C-c ' to edit table.el tables")
  5449. (if arg (org-table-edit-field t)
  5450. (org-table-justify-field-maybe)
  5451. (call-interactively 'org-table-next-field))))
  5452. ((run-hook-with-args-until-success
  5453. 'org-tab-after-check-for-table-hook))
  5454. ;; Global cycling: delegate to `org-cycle-internal-global'.
  5455. ((eq arg t) (org-cycle-internal-global))
  5456. ;; Drawers: delegate to `org-flag-drawer'.
  5457. ((and org-drawers org-drawer-regexp
  5458. (save-excursion
  5459. (beginning-of-line 1)
  5460. (looking-at org-drawer-regexp)))
  5461. (org-flag-drawer ; toggle block visibility
  5462. (not (get-char-property (match-end 0) 'invisible))))
  5463. ;; Show-subtree, ARG levels up from here.
  5464. ((integerp arg)
  5465. (save-excursion
  5466. (org-back-to-heading)
  5467. (outline-up-heading (if (< arg 0) (- arg)
  5468. (- (funcall outline-level) arg)))
  5469. (org-show-subtree)))
  5470. ;; Inline task: delegate to `org-inlinetask-toggle-visibility'.
  5471. ((and (featurep 'org-inlinetask)
  5472. (org-inlinetask-at-task-p)
  5473. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5474. (org-inlinetask-toggle-visibility))
  5475. ((org-try-cdlatex-tab))
  5476. ;; At an item/headline: delegate to `org-cycle-internal-local'.
  5477. ((and (or (and org-cycle-include-plain-lists (org-at-item-p))
  5478. (save-excursion (beginning-of-line 1)
  5479. (looking-at org-outline-regexp)))
  5480. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5481. (org-cycle-internal-local))
  5482. ;; From there: TAB emulation and template completion.
  5483. (buffer-read-only (org-back-to-heading))
  5484. ((run-hook-with-args-until-success
  5485. 'org-tab-after-check-for-cycling-hook))
  5486. ((org-try-structure-completion))
  5487. ((run-hook-with-args-until-success
  5488. 'org-tab-before-tab-emulation-hook))
  5489. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  5490. (or (not (bolp))
  5491. (not (looking-at org-outline-regexp))))
  5492. (call-interactively (global-key-binding "\t")))
  5493. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  5494. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  5495. (or (and (eq org-cycle-emulate-tab 'white)
  5496. (= (match-end 0) (point-at-eol)))
  5497. (and (eq org-cycle-emulate-tab 'whitestart)
  5498. (>= (match-end 0) pos))))
  5499. t
  5500. (eq org-cycle-emulate-tab t))
  5501. (call-interactively (global-key-binding "\t")))
  5502. (t (save-excursion
  5503. (org-back-to-heading)
  5504. (org-cycle)))))))
  5505. (defun org-cycle-internal-global ()
  5506. "Do the global cycling action."
  5507. (cond
  5508. ((and (eq last-command this-command)
  5509. (eq org-cycle-global-status 'overview))
  5510. ;; We just created the overview - now do table of contents
  5511. ;; This can be slow in very large buffers, so indicate action
  5512. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  5513. (message "CONTENTS...")
  5514. (org-content)
  5515. (message "CONTENTS...done")
  5516. (setq org-cycle-global-status 'contents)
  5517. (run-hook-with-args 'org-cycle-hook 'contents))
  5518. ((and (eq last-command this-command)
  5519. (eq org-cycle-global-status 'contents))
  5520. ;; We just showed the table of contents - now show everything
  5521. (run-hook-with-args 'org-pre-cycle-hook 'all)
  5522. (show-all)
  5523. (message "SHOW ALL")
  5524. (setq org-cycle-global-status 'all)
  5525. (run-hook-with-args 'org-cycle-hook 'all))
  5526. (t
  5527. ;; Default action: go to overview
  5528. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  5529. (org-overview)
  5530. (message "OVERVIEW")
  5531. (setq org-cycle-global-status 'overview)
  5532. (run-hook-with-args 'org-cycle-hook 'overview))))
  5533. (defun org-cycle-internal-local ()
  5534. "Do the local cycling action."
  5535. (let ((goal-column 0) eoh eol eos has-children children-skipped struct)
  5536. ;; First, determine end of headline (EOH), end of subtree or item
  5537. ;; (EOS), and if item or heading has children (HAS-CHILDREN).
  5538. (save-excursion
  5539. (if (org-at-item-p)
  5540. (progn
  5541. (beginning-of-line)
  5542. (setq struct (org-list-struct))
  5543. (setq eoh (point-at-eol))
  5544. (setq eos (org-list-get-item-end-before-blank (point) struct))
  5545. (setq has-children (org-list-has-child-p (point) struct)))
  5546. (org-back-to-heading)
  5547. (setq eoh (save-excursion (outline-end-of-heading) (point)))
  5548. (setq eos (save-excursion
  5549. (org-end-of-subtree t)
  5550. (unless (eobp)
  5551. (skip-chars-forward " \t\n"))
  5552. (if (eobp) (point) (1- (point)))))
  5553. (setq has-children
  5554. (or (save-excursion
  5555. (let ((level (funcall outline-level)))
  5556. (outline-next-heading)
  5557. (and (org-at-heading-p t)
  5558. (> (funcall outline-level) level))))
  5559. (save-excursion
  5560. (org-list-search-forward (org-item-beginning-re) eos t)))))
  5561. ;; Determine end invisible part of buffer (EOL)
  5562. (beginning-of-line 2)
  5563. ;; XEmacs doesn't have `next-single-char-property-change'
  5564. (if (featurep 'xemacs)
  5565. (while (and (not (eobp)) ;; this is like `next-line'
  5566. (get-char-property (1- (point)) 'invisible))
  5567. (beginning-of-line 2))
  5568. (while (and (not (eobp)) ;; this is like `next-line'
  5569. (get-char-property (1- (point)) 'invisible))
  5570. (goto-char (next-single-char-property-change (point) 'invisible))
  5571. (and (eolp) (beginning-of-line 2))))
  5572. (setq eol (point)))
  5573. ;; Find out what to do next and set `this-command'
  5574. (cond
  5575. ((= eos eoh)
  5576. ;; Nothing is hidden behind this heading
  5577. (run-hook-with-args 'org-pre-cycle-hook 'empty)
  5578. (message "EMPTY ENTRY")
  5579. (setq org-cycle-subtree-status nil)
  5580. (save-excursion
  5581. (goto-char eos)
  5582. (outline-next-heading)
  5583. (if (outline-invisible-p) (org-flag-heading nil))))
  5584. ((and (or (>= eol eos)
  5585. (not (string-match "\\S-" (buffer-substring eol eos))))
  5586. (or has-children
  5587. (not (setq children-skipped
  5588. org-cycle-skip-children-state-if-no-children))))
  5589. ;; Entire subtree is hidden in one line: children view
  5590. (run-hook-with-args 'org-pre-cycle-hook 'children)
  5591. (if (org-at-item-p)
  5592. (org-list-set-item-visibility (point-at-bol) struct 'children)
  5593. (org-show-entry)
  5594. (show-children)
  5595. ;; Fold every list in subtree to top-level items.
  5596. (when (eq org-cycle-include-plain-lists 'integrate)
  5597. (save-excursion
  5598. (org-back-to-heading)
  5599. (while (org-list-search-forward (org-item-beginning-re) eos t)
  5600. (beginning-of-line 1)
  5601. (let* ((struct (org-list-struct))
  5602. (prevs (org-list-prevs-alist struct))
  5603. (end (org-list-get-bottom-point struct)))
  5604. (mapc (lambda (e) (org-list-set-item-visibility e struct 'folded))
  5605. (org-list-get-all-items (point) struct prevs))
  5606. (goto-char end))))))
  5607. (message "CHILDREN")
  5608. (save-excursion
  5609. (goto-char eos)
  5610. (outline-next-heading)
  5611. (if (outline-invisible-p) (org-flag-heading nil)))
  5612. (setq org-cycle-subtree-status 'children)
  5613. (run-hook-with-args 'org-cycle-hook 'children))
  5614. ((or children-skipped
  5615. (and (eq last-command this-command)
  5616. (eq org-cycle-subtree-status 'children)))
  5617. ;; We just showed the children, or no children are there,
  5618. ;; now show everything.
  5619. (run-hook-with-args 'org-pre-cycle-hook 'subtree)
  5620. (outline-flag-region eoh eos nil)
  5621. (message (if children-skipped "SUBTREE (NO CHILDREN)" "SUBTREE"))
  5622. (setq org-cycle-subtree-status 'subtree)
  5623. (run-hook-with-args 'org-cycle-hook 'subtree))
  5624. (t
  5625. ;; Default action: hide the subtree.
  5626. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  5627. (outline-flag-region eoh eos t)
  5628. (message "FOLDED")
  5629. (setq org-cycle-subtree-status 'folded)
  5630. (run-hook-with-args 'org-cycle-hook 'folded)))))
  5631. ;;;###autoload
  5632. (defun org-global-cycle (&optional arg)
  5633. "Cycle the global visibility. For details see `org-cycle'.
  5634. With \\[universal-argument] prefix arg, switch to startup visibility.
  5635. With a numeric prefix, show all headlines up to that level."
  5636. (interactive "P")
  5637. (let ((org-cycle-include-plain-lists
  5638. (if (eq major-mode 'org-mode) org-cycle-include-plain-lists nil)))
  5639. (cond
  5640. ((integerp arg)
  5641. (show-all)
  5642. (hide-sublevels arg)
  5643. (setq org-cycle-global-status 'contents))
  5644. ((equal arg '(4))
  5645. (org-set-startup-visibility)
  5646. (message "Startup visibility, plus VISIBILITY properties."))
  5647. (t
  5648. (org-cycle '(4))))))
  5649. (defun org-set-startup-visibility ()
  5650. "Set the visibility required by startup options and properties."
  5651. (cond
  5652. ((eq org-startup-folded t)
  5653. (org-cycle '(4)))
  5654. ((eq org-startup-folded 'content)
  5655. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  5656. (org-cycle '(4)) (org-cycle '(4)))))
  5657. (unless (eq org-startup-folded 'showeverything)
  5658. (if org-hide-block-startup (org-hide-block-all))
  5659. (org-set-visibility-according-to-property 'no-cleanup)
  5660. (org-cycle-hide-archived-subtrees 'all)
  5661. (org-cycle-hide-drawers 'all)
  5662. (org-cycle-show-empty-lines t)))
  5663. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  5664. "Switch subtree visibilities according to :VISIBILITY: property."
  5665. (interactive)
  5666. (let (org-show-entry-below state)
  5667. (save-excursion
  5668. (goto-char (point-min))
  5669. (while (re-search-forward
  5670. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  5671. nil t)
  5672. (setq state (match-string 1))
  5673. (save-excursion
  5674. (org-back-to-heading t)
  5675. (hide-subtree)
  5676. (org-reveal)
  5677. (cond
  5678. ((equal state '("fold" "folded"))
  5679. (hide-subtree))
  5680. ((equal state "children")
  5681. (org-show-hidden-entry)
  5682. (show-children))
  5683. ((equal state "content")
  5684. (save-excursion
  5685. (save-restriction
  5686. (org-narrow-to-subtree)
  5687. (org-content))))
  5688. ((member state '("all" "showall"))
  5689. (show-subtree)))))
  5690. (unless no-cleanup
  5691. (org-cycle-hide-archived-subtrees 'all)
  5692. (org-cycle-hide-drawers 'all)
  5693. (org-cycle-show-empty-lines 'all)))))
  5694. ;; This function uses outline-regexp instead of the more fundamental
  5695. ;; org-outline-regexp so that org-cycle-global works outside of Org
  5696. ;; buffers, where outline-regexp is needed.
  5697. (defun org-overview ()
  5698. "Switch to overview mode, showing only top-level headlines.
  5699. Really, this shows all headlines with level equal or greater than the level
  5700. of the first headline in the buffer. This is important, because if the
  5701. first headline is not level one, then (hide-sublevels 1) gives confusing
  5702. results."
  5703. (interactive)
  5704. (let ((level (save-excursion
  5705. (goto-char (point-min))
  5706. (if (re-search-forward (concat "^" outline-regexp) nil t)
  5707. (progn
  5708. (goto-char (match-beginning 0))
  5709. (funcall outline-level))))))
  5710. (and level (hide-sublevels level))))
  5711. (defun org-content (&optional arg)
  5712. "Show all headlines in the buffer, like a table of contents.
  5713. With numerical argument N, show content up to level N."
  5714. (interactive "P")
  5715. (save-excursion
  5716. ;; Visit all headings and show their offspring
  5717. (and (integerp arg) (org-overview))
  5718. (goto-char (point-max))
  5719. (catch 'exit
  5720. (while (and (progn (condition-case nil
  5721. (outline-previous-visible-heading 1)
  5722. (error (goto-char (point-min))))
  5723. t)
  5724. (looking-at org-outline-regexp))
  5725. (if (integerp arg)
  5726. (show-children (1- arg))
  5727. (show-branches))
  5728. (if (bobp) (throw 'exit nil))))))
  5729. (defun org-optimize-window-after-visibility-change (state)
  5730. "Adjust the window after a change in outline visibility.
  5731. This function is the default value of the hook `org-cycle-hook'."
  5732. (when (get-buffer-window (current-buffer))
  5733. (cond
  5734. ((eq state 'content) nil)
  5735. ((eq state 'all) nil)
  5736. ((eq state 'folded) nil)
  5737. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  5738. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  5739. (defun org-remove-empty-overlays-at (pos)
  5740. "Remove outline overlays that do not contain non-white stuff."
  5741. (mapc
  5742. (lambda (o)
  5743. (and (eq 'outline (overlay-get o 'invisible))
  5744. (not (string-match "\\S-" (buffer-substring (overlay-start o)
  5745. (overlay-end o))))
  5746. (delete-overlay o)))
  5747. (overlays-at pos)))
  5748. (defun org-clean-visibility-after-subtree-move ()
  5749. "Fix visibility issues after moving a subtree."
  5750. ;; First, find a reasonable region to look at:
  5751. ;; Start two siblings above, end three below
  5752. (let* ((beg (save-excursion
  5753. (and (org-get-last-sibling)
  5754. (org-get-last-sibling))
  5755. (point)))
  5756. (end (save-excursion
  5757. (and (org-get-next-sibling)
  5758. (org-get-next-sibling)
  5759. (org-get-next-sibling))
  5760. (if (org-at-heading-p)
  5761. (point-at-eol)
  5762. (point))))
  5763. (level (looking-at "\\*+"))
  5764. (re (if level (concat "^" (regexp-quote (match-string 0)) " "))))
  5765. (save-excursion
  5766. (save-restriction
  5767. (narrow-to-region beg end)
  5768. (when re
  5769. ;; Properly fold already folded siblings
  5770. (goto-char (point-min))
  5771. (while (re-search-forward re nil t)
  5772. (if (and (not (outline-invisible-p))
  5773. (save-excursion
  5774. (goto-char (point-at-eol)) (outline-invisible-p)))
  5775. (hide-entry))))
  5776. (org-cycle-show-empty-lines 'overview)
  5777. (org-cycle-hide-drawers 'overview)))))
  5778. (defun org-cycle-show-empty-lines (state)
  5779. "Show empty lines above all visible headlines.
  5780. The region to be covered depends on STATE when called through
  5781. `org-cycle-hook'. Lisp program can use t for STATE to get the
  5782. entire buffer covered. Note that an empty line is only shown if there
  5783. are at least `org-cycle-separator-lines' empty lines before the headline."
  5784. (when (not (= org-cycle-separator-lines 0))
  5785. (save-excursion
  5786. (let* ((n (abs org-cycle-separator-lines))
  5787. (re (cond
  5788. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  5789. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  5790. (t (let ((ns (number-to-string (- n 2))))
  5791. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  5792. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  5793. beg end b e)
  5794. (cond
  5795. ((memq state '(overview contents t))
  5796. (setq beg (point-min) end (point-max)))
  5797. ((memq state '(children folded))
  5798. (setq beg (point) end (progn (org-end-of-subtree t t)
  5799. (beginning-of-line 2)
  5800. (point)))))
  5801. (when beg
  5802. (goto-char beg)
  5803. (while (re-search-forward re end t)
  5804. (unless (get-char-property (match-end 1) 'invisible)
  5805. (setq e (match-end 1))
  5806. (if (< org-cycle-separator-lines 0)
  5807. (setq b (save-excursion
  5808. (goto-char (match-beginning 0))
  5809. (org-back-over-empty-lines)
  5810. (if (save-excursion
  5811. (goto-char (max (point-min) (1- (point))))
  5812. (org-on-heading-p))
  5813. (1- (point))
  5814. (point))))
  5815. (setq b (match-beginning 1)))
  5816. (outline-flag-region b e nil)))))))
  5817. ;; Never hide empty lines at the end of the file.
  5818. (save-excursion
  5819. (goto-char (point-max))
  5820. (outline-previous-heading)
  5821. (outline-end-of-heading)
  5822. (if (and (looking-at "[ \t\n]+")
  5823. (= (match-end 0) (point-max)))
  5824. (outline-flag-region (point) (match-end 0) nil))))
  5825. (defun org-show-empty-lines-in-parent ()
  5826. "Move to the parent and re-show empty lines before visible headlines."
  5827. (save-excursion
  5828. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  5829. (org-cycle-show-empty-lines context))))
  5830. (defun org-files-list ()
  5831. "Return `org-agenda-files' list, plus all open org-mode files.
  5832. This is useful for operations that need to scan all of a user's
  5833. open and agenda-wise Org files."
  5834. (let ((files (mapcar 'expand-file-name (org-agenda-files))))
  5835. (dolist (buf (buffer-list))
  5836. (with-current-buffer buf
  5837. (if (and (eq major-mode 'org-mode) (buffer-file-name))
  5838. (let ((file (expand-file-name (buffer-file-name))))
  5839. (unless (member file files)
  5840. (push file files))))))
  5841. files))
  5842. (defsubst org-entry-beginning-position ()
  5843. "Return the beginning position of the current entry."
  5844. (save-excursion (outline-back-to-heading t) (point)))
  5845. (defsubst org-entry-end-position ()
  5846. "Return the end position of the current entry."
  5847. (save-excursion (outline-next-heading) (point)))
  5848. (defun org-cycle-hide-drawers (state)
  5849. "Re-hide all drawers after a visibility state change."
  5850. (when (and (eq major-mode 'org-mode)
  5851. (not (memq state '(overview folded contents))))
  5852. (save-excursion
  5853. (let* ((globalp (memq state '(contents all)))
  5854. (beg (if globalp (point-min) (point)))
  5855. (end (if globalp (point-max)
  5856. (if (eq state 'children)
  5857. (save-excursion (outline-next-heading) (point))
  5858. (org-end-of-subtree t)))))
  5859. (goto-char beg)
  5860. (while (re-search-forward org-drawer-regexp end t)
  5861. (org-flag-drawer t))))))
  5862. (defun org-flag-drawer (flag)
  5863. (save-excursion
  5864. (beginning-of-line 1)
  5865. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  5866. (let ((b (match-end 0)))
  5867. (if (re-search-forward
  5868. "^[ \t]*:END:"
  5869. (save-excursion (outline-next-heading) (point)) t)
  5870. (outline-flag-region b (point-at-eol) flag)
  5871. (error ":END: line missing at position %s" b))))))
  5872. (defun org-subtree-end-visible-p ()
  5873. "Is the end of the current subtree visible?"
  5874. (pos-visible-in-window-p
  5875. (save-excursion (org-end-of-subtree t) (point))))
  5876. (defun org-first-headline-recenter (&optional N)
  5877. "Move cursor to the first headline and recenter the headline.
  5878. Optional argument N means put the headline into the Nth line of the window."
  5879. (goto-char (point-min))
  5880. (when (re-search-forward (concat "^\\(" org-outline-regexp "\\)") nil t)
  5881. (beginning-of-line)
  5882. (recenter (prefix-numeric-value N))))
  5883. ;;; Saving and restoring visibility
  5884. (defun org-outline-overlay-data (&optional use-markers)
  5885. "Return a list of the locations of all outline overlays.
  5886. These are overlays with the `invisible' property value `outline'.
  5887. The return value is a list of cons cells, with start and stop
  5888. positions for each overlay.
  5889. If USE-MARKERS is set, return the positions as markers."
  5890. (let (beg end)
  5891. (save-excursion
  5892. (save-restriction
  5893. (widen)
  5894. (delq nil
  5895. (mapcar (lambda (o)
  5896. (when (eq (overlay-get o 'invisible) 'outline)
  5897. (setq beg (overlay-start o)
  5898. end (overlay-end o))
  5899. (and beg end (> end beg)
  5900. (if use-markers
  5901. (cons (move-marker (make-marker) beg)
  5902. (move-marker (make-marker) end))
  5903. (cons beg end)))))
  5904. (overlays-in (point-min) (point-max))))))))
  5905. (defun org-set-outline-overlay-data (data)
  5906. "Create visibility overlays for all positions in DATA.
  5907. DATA should have been made by `org-outline-overlay-data'."
  5908. (let (o)
  5909. (save-excursion
  5910. (save-restriction
  5911. (widen)
  5912. (show-all)
  5913. (mapc (lambda (c)
  5914. (setq o (make-overlay (car c) (cdr c)))
  5915. (overlay-put o 'invisible 'outline))
  5916. data)))))
  5917. ;;; Folding of blocks
  5918. (defconst org-block-regexp
  5919. "^[ \t]*#\\+begin_?\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_?\\1[ \t]*$"
  5920. "Regular expression for hiding blocks.")
  5921. (defvar org-hide-block-overlays nil
  5922. "Overlays hiding blocks.")
  5923. (make-variable-buffer-local 'org-hide-block-overlays)
  5924. (defun org-block-map (function &optional start end)
  5925. "Call FUNCTION at the head of all source blocks in the current buffer.
  5926. Optional arguments START and END can be used to limit the range."
  5927. (let ((start (or start (point-min)))
  5928. (end (or end (point-max))))
  5929. (save-excursion
  5930. (goto-char start)
  5931. (while (and (< (point) end) (re-search-forward org-block-regexp end t))
  5932. (save-excursion
  5933. (save-match-data
  5934. (goto-char (match-beginning 0))
  5935. (funcall function)))))))
  5936. (defun org-hide-block-toggle-all ()
  5937. "Toggle the visibility of all blocks in the current buffer."
  5938. (org-block-map #'org-hide-block-toggle))
  5939. (defun org-hide-block-all ()
  5940. "Fold all blocks in the current buffer."
  5941. (interactive)
  5942. (org-show-block-all)
  5943. (org-block-map #'org-hide-block-toggle-maybe))
  5944. (defun org-show-block-all ()
  5945. "Unfold all blocks in the current buffer."
  5946. (interactive)
  5947. (mapc 'delete-overlay org-hide-block-overlays)
  5948. (setq org-hide-block-overlays nil))
  5949. (defun org-hide-block-toggle-maybe ()
  5950. "Toggle visibility of block at point."
  5951. (interactive)
  5952. (let ((case-fold-search t))
  5953. (if (save-excursion
  5954. (beginning-of-line 1)
  5955. (looking-at org-block-regexp))
  5956. (progn (org-hide-block-toggle)
  5957. t) ;; to signal that we took action
  5958. nil))) ;; to signal that we did not
  5959. (defun org-hide-block-toggle (&optional force)
  5960. "Toggle the visibility of the current block."
  5961. (interactive)
  5962. (save-excursion
  5963. (beginning-of-line)
  5964. (if (re-search-forward org-block-regexp nil t)
  5965. (let ((start (- (match-beginning 4) 1)) ;; beginning of body
  5966. (end (match-end 0)) ;; end of entire body
  5967. ov)
  5968. (if (memq t (mapcar (lambda (overlay)
  5969. (eq (overlay-get overlay 'invisible)
  5970. 'org-hide-block))
  5971. (overlays-at start)))
  5972. (if (or (not force) (eq force 'off))
  5973. (mapc (lambda (ov)
  5974. (when (member ov org-hide-block-overlays)
  5975. (setq org-hide-block-overlays
  5976. (delq ov org-hide-block-overlays)))
  5977. (when (eq (overlay-get ov 'invisible)
  5978. 'org-hide-block)
  5979. (delete-overlay ov)))
  5980. (overlays-at start)))
  5981. (setq ov (make-overlay start end))
  5982. (overlay-put ov 'invisible 'org-hide-block)
  5983. ;; make the block accessible to isearch
  5984. (overlay-put
  5985. ov 'isearch-open-invisible
  5986. (lambda (ov)
  5987. (when (member ov org-hide-block-overlays)
  5988. (setq org-hide-block-overlays
  5989. (delq ov org-hide-block-overlays)))
  5990. (when (eq (overlay-get ov 'invisible)
  5991. 'org-hide-block)
  5992. (delete-overlay ov))))
  5993. (push ov org-hide-block-overlays)))
  5994. (error "Not looking at a source block"))))
  5995. ;; org-tab-after-check-for-cycling-hook
  5996. (add-hook 'org-tab-first-hook 'org-hide-block-toggle-maybe)
  5997. ;; Remove overlays when changing major mode
  5998. (add-hook 'org-mode-hook
  5999. (lambda () (org-add-hook 'change-major-mode-hook
  6000. 'org-show-block-all 'append 'local)))
  6001. ;;; Org-goto
  6002. (defvar org-goto-window-configuration nil)
  6003. (defvar org-goto-marker nil)
  6004. (defvar org-goto-map
  6005. (let ((map (make-sparse-keymap)))
  6006. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  6007. (while (setq cmd (pop cmds))
  6008. (substitute-key-definition cmd cmd map global-map)))
  6009. (suppress-keymap map)
  6010. (org-defkey map "\C-m" 'org-goto-ret)
  6011. (org-defkey map [(return)] 'org-goto-ret)
  6012. (org-defkey map [(left)] 'org-goto-left)
  6013. (org-defkey map [(right)] 'org-goto-right)
  6014. (org-defkey map [(control ?g)] 'org-goto-quit)
  6015. (org-defkey map "\C-i" 'org-cycle)
  6016. (org-defkey map [(tab)] 'org-cycle)
  6017. (org-defkey map [(down)] 'outline-next-visible-heading)
  6018. (org-defkey map [(up)] 'outline-previous-visible-heading)
  6019. (if org-goto-auto-isearch
  6020. (if (fboundp 'define-key-after)
  6021. (define-key-after map [t] 'org-goto-local-auto-isearch)
  6022. nil)
  6023. (org-defkey map "q" 'org-goto-quit)
  6024. (org-defkey map "n" 'outline-next-visible-heading)
  6025. (org-defkey map "p" 'outline-previous-visible-heading)
  6026. (org-defkey map "f" 'outline-forward-same-level)
  6027. (org-defkey map "b" 'outline-backward-same-level)
  6028. (org-defkey map "u" 'outline-up-heading))
  6029. (org-defkey map "/" 'org-occur)
  6030. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  6031. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  6032. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  6033. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  6034. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  6035. map))
  6036. (defconst org-goto-help
  6037. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  6038. RET=jump to location [Q]uit and return to previous location
  6039. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  6040. (defvar org-goto-start-pos) ; dynamically scoped parameter
  6041. ;; FIXME: Docstring does not mention both interfaces
  6042. (defun org-goto (&optional alternative-interface)
  6043. "Look up a different location in the current file, keeping current visibility.
  6044. When you want look-up or go to a different location in a document, the
  6045. fastest way is often to fold the entire buffer and then dive into the tree.
  6046. This method has the disadvantage, that the previous location will be folded,
  6047. which may not be what you want.
  6048. This command works around this by showing a copy of the current buffer
  6049. in an indirect buffer, in overview mode. You can dive into the tree in
  6050. that copy, use org-occur and incremental search to find a location.
  6051. When pressing RET or `Q', the command returns to the original buffer in
  6052. which the visibility is still unchanged. After RET is will also jump to
  6053. the location selected in the indirect buffer and expose the headline
  6054. hierarchy above."
  6055. (interactive "P")
  6056. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  6057. (org-refile-use-outline-path t)
  6058. (org-refile-target-verify-function nil)
  6059. (interface
  6060. (if (not alternative-interface)
  6061. org-goto-interface
  6062. (if (eq org-goto-interface 'outline)
  6063. 'outline-path-completion
  6064. 'outline)))
  6065. (org-goto-start-pos (point))
  6066. (selected-point
  6067. (if (eq interface 'outline)
  6068. (car (org-get-location (current-buffer) org-goto-help))
  6069. (let ((pa (org-refile-get-location "Goto")))
  6070. (org-refile-check-position pa)
  6071. (nth 3 pa)))))
  6072. (if selected-point
  6073. (progn
  6074. (org-mark-ring-push org-goto-start-pos)
  6075. (goto-char selected-point)
  6076. (if (or (outline-invisible-p) (org-invisible-p2))
  6077. (org-show-context 'org-goto)))
  6078. (message "Quit"))))
  6079. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  6080. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  6081. (defvar org-goto-local-auto-isearch-map) ; defined below
  6082. (defun org-get-location (buf help)
  6083. "Let the user select a location in the Org-mode buffer BUF.
  6084. This function uses a recursive edit. It returns the selected position
  6085. or nil."
  6086. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  6087. (isearch-hide-immediately nil)
  6088. (isearch-search-fun-function
  6089. (lambda () 'org-goto-local-search-headings))
  6090. (org-goto-selected-point org-goto-exit-command)
  6091. (pop-up-frames nil)
  6092. (special-display-buffer-names nil)
  6093. (special-display-regexps nil)
  6094. (special-display-function nil))
  6095. (save-excursion
  6096. (save-window-excursion
  6097. (delete-other-windows)
  6098. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  6099. (org-pop-to-buffer-same-window
  6100. (condition-case nil
  6101. (make-indirect-buffer (current-buffer) "*org-goto*")
  6102. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  6103. (with-output-to-temp-buffer "*Help*"
  6104. (princ help))
  6105. (org-fit-window-to-buffer (get-buffer-window "*Help*"))
  6106. (setq buffer-read-only nil)
  6107. (let ((org-startup-truncated t)
  6108. (org-startup-folded nil)
  6109. (org-startup-align-all-tables nil))
  6110. (org-mode)
  6111. (org-overview))
  6112. (setq buffer-read-only t)
  6113. (if (and (boundp 'org-goto-start-pos)
  6114. (integer-or-marker-p org-goto-start-pos))
  6115. (let ((org-show-hierarchy-above t)
  6116. (org-show-siblings t)
  6117. (org-show-following-heading t))
  6118. (goto-char org-goto-start-pos)
  6119. (and (outline-invisible-p) (org-show-context)))
  6120. (goto-char (point-min)))
  6121. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  6122. (message "Select location and press RET")
  6123. (use-local-map org-goto-map)
  6124. (recursive-edit)
  6125. ))
  6126. (kill-buffer "*org-goto*")
  6127. (cons org-goto-selected-point org-goto-exit-command)))
  6128. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  6129. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  6130. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  6131. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  6132. (defun org-goto-local-search-headings (string bound noerror)
  6133. "Search and make sure that any matches are in headlines."
  6134. (catch 'return
  6135. (while (if isearch-forward
  6136. (search-forward string bound noerror)
  6137. (search-backward string bound noerror))
  6138. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  6139. (and (member :headline context)
  6140. (not (member :tags context))))
  6141. (throw 'return (point))))))
  6142. (defun org-goto-local-auto-isearch ()
  6143. "Start isearch."
  6144. (interactive)
  6145. (goto-char (point-min))
  6146. (let ((keys (this-command-keys)))
  6147. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  6148. (isearch-mode t)
  6149. (isearch-process-search-char (string-to-char keys)))))
  6150. (defun org-goto-ret (&optional arg)
  6151. "Finish `org-goto' by going to the new location."
  6152. (interactive "P")
  6153. (setq org-goto-selected-point (point)
  6154. org-goto-exit-command 'return)
  6155. (throw 'exit nil))
  6156. (defun org-goto-left ()
  6157. "Finish `org-goto' by going to the new location."
  6158. (interactive)
  6159. (if (org-on-heading-p)
  6160. (progn
  6161. (beginning-of-line 1)
  6162. (setq org-goto-selected-point (point)
  6163. org-goto-exit-command 'left)
  6164. (throw 'exit nil))
  6165. (error "Not on a heading")))
  6166. (defun org-goto-right ()
  6167. "Finish `org-goto' by going to the new location."
  6168. (interactive)
  6169. (if (org-on-heading-p)
  6170. (progn
  6171. (setq org-goto-selected-point (point)
  6172. org-goto-exit-command 'right)
  6173. (throw 'exit nil))
  6174. (error "Not on a heading")))
  6175. (defun org-goto-quit ()
  6176. "Finish `org-goto' without cursor motion."
  6177. (interactive)
  6178. (setq org-goto-selected-point nil)
  6179. (setq org-goto-exit-command 'quit)
  6180. (throw 'exit nil))
  6181. ;;; Indirect buffer display of subtrees
  6182. (defvar org-indirect-dedicated-frame nil
  6183. "This is the frame being used for indirect tree display.")
  6184. (defvar org-last-indirect-buffer nil)
  6185. (defun org-tree-to-indirect-buffer (&optional arg)
  6186. "Create indirect buffer and narrow it to current subtree.
  6187. With numerical prefix ARG, go up to this level and then take that tree.
  6188. If ARG is negative, go up that many levels.
  6189. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  6190. indirect buffer previously made with this command, to avoid proliferation of
  6191. indirect buffers. However, when you call the command with a \
  6192. \\[universal-argument] prefix, or
  6193. when `org-indirect-buffer-display' is `new-frame', the last buffer
  6194. is kept so that you can work with several indirect buffers at the same time.
  6195. If `org-indirect-buffer-display' is `dedicated-frame', the \
  6196. \\[universal-argument] prefix also
  6197. requests that a new frame be made for the new buffer, so that the dedicated
  6198. frame is not changed."
  6199. (interactive "P")
  6200. (let ((cbuf (current-buffer))
  6201. (cwin (selected-window))
  6202. (pos (point))
  6203. beg end level heading ibuf)
  6204. (save-excursion
  6205. (org-back-to-heading t)
  6206. (when (numberp arg)
  6207. (setq level (org-outline-level))
  6208. (if (< arg 0) (setq arg (+ level arg)))
  6209. (while (> (setq level (org-outline-level)) arg)
  6210. (outline-up-heading 1 t)))
  6211. (setq beg (point)
  6212. heading (org-get-heading))
  6213. (org-end-of-subtree t t)
  6214. (if (org-on-heading-p) (backward-char 1))
  6215. (setq end (point)))
  6216. (if (and (buffer-live-p org-last-indirect-buffer)
  6217. (not (eq org-indirect-buffer-display 'new-frame))
  6218. (not arg))
  6219. (kill-buffer org-last-indirect-buffer))
  6220. (setq ibuf (org-get-indirect-buffer cbuf)
  6221. org-last-indirect-buffer ibuf)
  6222. (cond
  6223. ((or (eq org-indirect-buffer-display 'new-frame)
  6224. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  6225. (select-frame (make-frame))
  6226. (delete-other-windows)
  6227. (org-pop-to-buffer-same-window ibuf)
  6228. (org-set-frame-title heading))
  6229. ((eq org-indirect-buffer-display 'dedicated-frame)
  6230. (raise-frame
  6231. (select-frame (or (and org-indirect-dedicated-frame
  6232. (frame-live-p org-indirect-dedicated-frame)
  6233. org-indirect-dedicated-frame)
  6234. (setq org-indirect-dedicated-frame (make-frame)))))
  6235. (delete-other-windows)
  6236. (org-pop-to-buffer-same-window ibuf)
  6237. (org-set-frame-title (concat "Indirect: " heading)))
  6238. ((eq org-indirect-buffer-display 'current-window)
  6239. (org-pop-to-buffer-same-window ibuf))
  6240. ((eq org-indirect-buffer-display 'other-window)
  6241. (pop-to-buffer ibuf))
  6242. (t (error "Invalid value")))
  6243. (if (featurep 'xemacs)
  6244. (save-excursion (org-mode) (turn-on-font-lock)))
  6245. (narrow-to-region beg end)
  6246. (show-all)
  6247. (goto-char pos)
  6248. (run-hook-with-args 'org-cycle-hook 'all)
  6249. (and (window-live-p cwin) (select-window cwin))))
  6250. (defun org-get-indirect-buffer (&optional buffer)
  6251. (setq buffer (or buffer (current-buffer)))
  6252. (let ((n 1) (base (buffer-name buffer)) bname)
  6253. (while (buffer-live-p
  6254. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  6255. (setq n (1+ n)))
  6256. (condition-case nil
  6257. (make-indirect-buffer buffer bname 'clone)
  6258. (error (make-indirect-buffer buffer bname)))))
  6259. (defun org-set-frame-title (title)
  6260. "Set the title of the current frame to the string TITLE."
  6261. ;; FIXME: how to name a single frame in XEmacs???
  6262. (unless (featurep 'xemacs)
  6263. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  6264. ;;;; Structure editing
  6265. ;;; Inserting headlines
  6266. (defun org-previous-line-empty-p ()
  6267. (save-excursion
  6268. (and (not (bobp))
  6269. (or (beginning-of-line 0) t)
  6270. (save-match-data
  6271. (looking-at "[ \t]*$")))))
  6272. (defun org-insert-heading (&optional force-heading invisible-ok)
  6273. "Insert a new heading or item with same depth at point.
  6274. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  6275. If point is at the beginning of a headline, insert a sibling before the
  6276. current headline. If point is not at the beginning, split the line,
  6277. create the new headline with the text in the current line after point
  6278. \(but see also the variable `org-M-RET-may-split-line').
  6279. When INVISIBLE-OK is set, stop at invisible headlines when going back.
  6280. This is important for non-interactive uses of the command."
  6281. (interactive "P")
  6282. (if (or (= (buffer-size) 0)
  6283. (and (not (save-excursion
  6284. (and (ignore-errors (org-back-to-heading invisible-ok))
  6285. (org-on-heading-p))))
  6286. (or force-heading (not (org-in-item-p)))))
  6287. (progn
  6288. (insert "\n* ")
  6289. (run-hooks 'org-insert-heading-hook))
  6290. (when (or force-heading (not (org-insert-item)))
  6291. (let* ((empty-line-p nil)
  6292. (level nil)
  6293. (on-heading (org-on-heading-p))
  6294. (head (save-excursion
  6295. (condition-case nil
  6296. (progn
  6297. (org-back-to-heading invisible-ok)
  6298. (when (and (not on-heading)
  6299. (featurep 'org-inlinetask)
  6300. (integerp org-inlinetask-min-level)
  6301. (>= (length (match-string 0))
  6302. org-inlinetask-min-level))
  6303. ;; Find a heading level before the inline task
  6304. (while (and (setq level (org-up-heading-safe))
  6305. (>= level org-inlinetask-min-level)))
  6306. (if (org-on-heading-p)
  6307. (org-back-to-heading invisible-ok)
  6308. (error "This should not happen")))
  6309. (setq empty-line-p (org-previous-line-empty-p))
  6310. (match-string 0))
  6311. (error "*"))))
  6312. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  6313. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  6314. pos hide-previous previous-pos)
  6315. (cond
  6316. ((and (org-on-heading-p) (bolp)
  6317. (or (bobp)
  6318. (save-excursion (backward-char 1) (not (outline-invisible-p)))))
  6319. ;; insert before the current line
  6320. (open-line (if blank 2 1)))
  6321. ((and (bolp)
  6322. (not org-insert-heading-respect-content)
  6323. (or (bobp)
  6324. (save-excursion
  6325. (backward-char 1) (not (outline-invisible-p)))))
  6326. ;; insert right here
  6327. nil)
  6328. (t
  6329. ;; somewhere in the line
  6330. (save-excursion
  6331. (setq previous-pos (point-at-bol))
  6332. (end-of-line)
  6333. (setq hide-previous (outline-invisible-p)))
  6334. (and org-insert-heading-respect-content (org-show-subtree))
  6335. (let ((split
  6336. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  6337. (save-excursion
  6338. (let ((p (point)))
  6339. (goto-char (point-at-bol))
  6340. (and (looking-at org-complex-heading-regexp)
  6341. (match-beginning 4)
  6342. (> p (match-beginning 4)))))))
  6343. tags pos)
  6344. (cond
  6345. (org-insert-heading-respect-content
  6346. (org-end-of-subtree nil t)
  6347. (when (featurep 'org-inlinetask)
  6348. (while (and (not (eobp))
  6349. (looking-at "\\(\\*+\\)[ \t]+")
  6350. (>= (length (match-string 1))
  6351. org-inlinetask-min-level))
  6352. (org-end-of-subtree nil t)))
  6353. (or (bolp) (newline))
  6354. (or (org-previous-line-empty-p)
  6355. (and blank (newline)))
  6356. (open-line 1))
  6357. ((org-on-heading-p)
  6358. (when hide-previous
  6359. (show-children)
  6360. (org-show-entry))
  6361. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?[ \t]*$")
  6362. (setq tags (and (match-end 2) (match-string 2)))
  6363. (and (match-end 1)
  6364. (delete-region (match-beginning 1) (match-end 1)))
  6365. (setq pos (point-at-bol))
  6366. (or split (end-of-line 1))
  6367. (delete-horizontal-space)
  6368. (if (string-match "\\`\\*+\\'"
  6369. (buffer-substring (point-at-bol) (point)))
  6370. (insert " "))
  6371. (newline (if blank 2 1))
  6372. (when tags
  6373. (save-excursion
  6374. (goto-char pos)
  6375. (end-of-line 1)
  6376. (insert " " tags)
  6377. (org-set-tags nil 'align))))
  6378. (t
  6379. (or split (end-of-line 1))
  6380. (newline (if blank 2 1)))))))
  6381. (insert head) (just-one-space)
  6382. (setq pos (point))
  6383. (end-of-line 1)
  6384. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  6385. (when (and org-insert-heading-respect-content hide-previous)
  6386. (save-excursion
  6387. (goto-char previous-pos)
  6388. (hide-subtree)))
  6389. (run-hooks 'org-insert-heading-hook)))))
  6390. (defun org-get-heading (&optional no-tags no-todo)
  6391. "Return the heading of the current entry, without the stars.
  6392. When NO-TAGS is non-nil, don't include tags.
  6393. When NO-TODO is non-nil, don't include TODO keywords."
  6394. (save-excursion
  6395. (org-back-to-heading t)
  6396. (cond
  6397. ((and no-tags no-todo)
  6398. (looking-at org-complex-heading-regexp)
  6399. (match-string 4))
  6400. (no-tags
  6401. (looking-at (concat org-outline-regexp
  6402. "\\(.*?\\)"
  6403. "\\(?:[ \t]+:[[:alnum:]:_@#%]+:\\)?[ \t]*$"))
  6404. (match-string 1))
  6405. (no-todo
  6406. (looking-at org-todo-line-regexp)
  6407. (match-string 3))
  6408. (t (looking-at org-heading-regexp)
  6409. (match-string 2)))))
  6410. (defun org-heading-components ()
  6411. "Return the components of the current heading.
  6412. This is a list with the following elements:
  6413. - the level as an integer
  6414. - the reduced level, different if `org-odd-levels-only' is set.
  6415. - the TODO keyword, or nil
  6416. - the priority character, like ?A, or nil if no priority is given
  6417. - the headline text itself, or the tags string if no headline text
  6418. - the tags string, or nil."
  6419. (save-excursion
  6420. (org-back-to-heading t)
  6421. (if (let (case-fold-search) (looking-at org-complex-heading-regexp))
  6422. (list (length (match-string 1))
  6423. (org-reduced-level (length (match-string 1)))
  6424. (org-match-string-no-properties 2)
  6425. (and (match-end 3) (aref (match-string 3) 2))
  6426. (org-match-string-no-properties 4)
  6427. (org-match-string-no-properties 5)))))
  6428. (defun org-get-entry ()
  6429. "Get the entry text, after heading, entire subtree."
  6430. (save-excursion
  6431. (org-back-to-heading t)
  6432. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  6433. (defun org-insert-heading-after-current ()
  6434. "Insert a new heading with same level as current, after current subtree."
  6435. (interactive)
  6436. (org-back-to-heading)
  6437. (org-insert-heading)
  6438. (org-move-subtree-down)
  6439. (end-of-line 1))
  6440. (defun org-insert-heading-respect-content ()
  6441. (interactive)
  6442. (let ((org-insert-heading-respect-content t))
  6443. (org-insert-heading t)))
  6444. (defun org-insert-todo-heading-respect-content (&optional force-state)
  6445. (interactive "P")
  6446. (let ((org-insert-heading-respect-content t))
  6447. (org-insert-todo-heading force-state t)))
  6448. (defun org-insert-todo-heading (arg &optional force-heading)
  6449. "Insert a new heading with the same level and TODO state as current heading.
  6450. If the heading has no TODO state, or if the state is DONE, use the first
  6451. state (TODO by default). Also with prefix arg, force first state."
  6452. (interactive "P")
  6453. (when (or force-heading (not (org-insert-item 'checkbox)))
  6454. (org-insert-heading force-heading)
  6455. (save-excursion
  6456. (org-back-to-heading)
  6457. (outline-previous-heading)
  6458. (looking-at org-todo-line-regexp))
  6459. (let*
  6460. ((new-mark-x
  6461. (if (or arg
  6462. (not (match-beginning 2))
  6463. (member (match-string 2) org-done-keywords))
  6464. (car org-todo-keywords-1)
  6465. (match-string 2)))
  6466. (new-mark
  6467. (or
  6468. (run-hook-with-args-until-success
  6469. 'org-todo-get-default-hook new-mark-x nil)
  6470. new-mark-x)))
  6471. (beginning-of-line 1)
  6472. (and (looking-at org-outline-regexp) (goto-char (match-end 0))
  6473. (if org-treat-insert-todo-heading-as-state-change
  6474. (org-todo new-mark)
  6475. (insert new-mark " "))))
  6476. (when org-provide-todo-statistics
  6477. (org-update-parent-todo-statistics))))
  6478. (defun org-insert-subheading (arg)
  6479. "Insert a new subheading and demote it.
  6480. Works for outline headings and for plain lists alike."
  6481. (interactive "P")
  6482. (org-insert-heading arg)
  6483. (cond
  6484. ((org-on-heading-p) (org-do-demote))
  6485. ((org-at-item-p) (org-indent-item))))
  6486. (defun org-insert-todo-subheading (arg)
  6487. "Insert a new subheading with TODO keyword or checkbox and demote it.
  6488. Works for outline headings and for plain lists alike."
  6489. (interactive "P")
  6490. (org-insert-todo-heading arg)
  6491. (cond
  6492. ((org-on-heading-p) (org-do-demote))
  6493. ((org-at-item-p) (org-indent-item))))
  6494. ;;; Promotion and Demotion
  6495. (defvar org-after-demote-entry-hook nil
  6496. "Hook run after an entry has been demoted.
  6497. The cursor will be at the beginning of the entry.
  6498. When a subtree is being demoted, the hook will be called for each node.")
  6499. (defvar org-after-promote-entry-hook nil
  6500. "Hook run after an entry has been promoted.
  6501. The cursor will be at the beginning of the entry.
  6502. When a subtree is being promoted, the hook will be called for each node.")
  6503. (defun org-promote-subtree ()
  6504. "Promote the entire subtree.
  6505. See also `org-promote'."
  6506. (interactive)
  6507. (save-excursion
  6508. (org-with-limited-levels (org-map-tree 'org-promote)))
  6509. (org-fix-position-after-promote))
  6510. (defun org-demote-subtree ()
  6511. "Demote the entire subtree. See `org-demote'.
  6512. See also `org-promote'."
  6513. (interactive)
  6514. (save-excursion
  6515. (org-with-limited-levels (org-map-tree 'org-demote)))
  6516. (org-fix-position-after-promote))
  6517. (defun org-do-promote ()
  6518. "Promote the current heading higher up the tree.
  6519. If the region is active in `transient-mark-mode', promote all headings
  6520. in the region."
  6521. (interactive)
  6522. (save-excursion
  6523. (if (org-region-active-p)
  6524. (org-map-region 'org-promote (region-beginning) (region-end))
  6525. (org-promote)))
  6526. (org-fix-position-after-promote))
  6527. (defun org-do-demote ()
  6528. "Demote the current heading lower down the tree.
  6529. If the region is active in `transient-mark-mode', demote all headings
  6530. in the region."
  6531. (interactive)
  6532. (save-excursion
  6533. (if (org-region-active-p)
  6534. (org-map-region 'org-demote (region-beginning) (region-end))
  6535. (org-demote)))
  6536. (org-fix-position-after-promote))
  6537. (defun org-fix-position-after-promote ()
  6538. "Make sure that after pro/demotion cursor position is right."
  6539. (let ((pos (point)))
  6540. (when (save-excursion
  6541. (beginning-of-line 1)
  6542. (looking-at org-todo-line-regexp)
  6543. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  6544. (cond ((eobp) (insert " "))
  6545. ((eolp) (insert " "))
  6546. ((equal (char-after) ?\ ) (forward-char 1))))))
  6547. (defun org-current-level ()
  6548. "Return the level of the current entry, or nil if before the first headline.
  6549. The level is the number of stars at the beginning of the headline."
  6550. (save-excursion
  6551. (org-with-limited-levels
  6552. (if (ignore-errors (org-back-to-heading t))
  6553. (funcall outline-level)))))
  6554. (defun org-get-previous-line-level ()
  6555. "Return the outline depth of the last headline before the current line.
  6556. Returns 0 for the first headline in the buffer, and nil if before the
  6557. first headline."
  6558. (let ((current-level (org-current-level))
  6559. (prev-level (when (> (line-number-at-pos) 1)
  6560. (save-excursion
  6561. (beginning-of-line 0)
  6562. (org-current-level)))))
  6563. (cond ((null current-level) nil) ; Before first headline
  6564. ((null prev-level) 0) ; At first headline
  6565. (prev-level))))
  6566. (defun org-reduced-level (l)
  6567. "Compute the effective level of a heading.
  6568. This takes into account the setting of `org-odd-levels-only'."
  6569. (cond
  6570. ((zerop l) 0)
  6571. (org-odd-levels-only (1+ (floor (/ l 2))))
  6572. (t l)))
  6573. (defun org-level-increment ()
  6574. "Return the number of stars that will be added or removed at a
  6575. time to headlines when structure editing, based on the value of
  6576. `org-odd-levels-only'."
  6577. (if org-odd-levels-only 2 1))
  6578. (defun org-get-valid-level (level &optional change)
  6579. "Rectify a level change under the influence of `org-odd-levels-only'
  6580. LEVEL is a current level, CHANGE is by how much the level should be
  6581. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  6582. even level numbers will become the next higher odd number."
  6583. (if org-odd-levels-only
  6584. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  6585. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  6586. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  6587. (max 1 (+ level (or change 0)))))
  6588. (if (boundp 'define-obsolete-function-alias)
  6589. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  6590. (define-obsolete-function-alias 'org-get-legal-level
  6591. 'org-get-valid-level)
  6592. (define-obsolete-function-alias 'org-get-legal-level
  6593. 'org-get-valid-level "23.1")))
  6594. (defun org-promote ()
  6595. "Promote the current heading higher up the tree.
  6596. If the region is active in `transient-mark-mode', promote all headings
  6597. in the region."
  6598. (org-back-to-heading t)
  6599. (let* ((level (save-match-data (funcall outline-level)))
  6600. (after-change-functions (remove 'flyspell-after-change-function
  6601. after-change-functions))
  6602. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  6603. (diff (abs (- level (length up-head) -1))))
  6604. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  6605. (replace-match up-head nil t)
  6606. ;; Fixup tag positioning
  6607. (and org-auto-align-tags (org-set-tags nil t))
  6608. (if org-adapt-indentation (org-fixup-indentation (- diff)))
  6609. (run-hooks 'org-after-promote-entry-hook)))
  6610. (defun org-demote ()
  6611. "Demote the current heading lower down the tree.
  6612. If the region is active in `transient-mark-mode', demote all headings
  6613. in the region."
  6614. (org-back-to-heading t)
  6615. (let* ((level (save-match-data (funcall outline-level)))
  6616. (after-change-functions (remove 'flyspell-after-change-function
  6617. after-change-functions))
  6618. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  6619. (diff (abs (- level (length down-head) -1))))
  6620. (replace-match down-head nil t)
  6621. ;; Fixup tag positioning
  6622. (and org-auto-align-tags (org-set-tags nil t))
  6623. (if org-adapt-indentation (org-fixup-indentation diff))
  6624. (run-hooks 'org-after-demote-entry-hook)))
  6625. (defun org-cycle-level ()
  6626. "Cycle the level of an empty headline through possible states.
  6627. This goes first to child, then to parent, level, then up the hierarchy.
  6628. After top level, it switches back to sibling level."
  6629. (interactive)
  6630. (let ((org-adapt-indentation nil))
  6631. (when (org-point-at-end-of-empty-headline)
  6632. (setq this-command 'org-cycle-level) ; Only needed for caching
  6633. (let ((cur-level (org-current-level))
  6634. (prev-level (org-get-previous-line-level)))
  6635. (cond
  6636. ;; If first headline in file, promote to top-level.
  6637. ((= prev-level 0)
  6638. (loop repeat (/ (- cur-level 1) (org-level-increment))
  6639. do (org-do-promote)))
  6640. ;; If same level as prev, demote one.
  6641. ((= prev-level cur-level)
  6642. (org-do-demote))
  6643. ;; If parent is top-level, promote to top level if not already.
  6644. ((= prev-level 1)
  6645. (loop repeat (/ (- cur-level 1) (org-level-increment))
  6646. do (org-do-promote)))
  6647. ;; If top-level, return to prev-level.
  6648. ((= cur-level 1)
  6649. (loop repeat (/ (- prev-level 1) (org-level-increment))
  6650. do (org-do-demote)))
  6651. ;; If less than prev-level, promote one.
  6652. ((< cur-level prev-level)
  6653. (org-do-promote))
  6654. ;; If deeper than prev-level, promote until higher than
  6655. ;; prev-level.
  6656. ((> cur-level prev-level)
  6657. (loop repeat (+ 1 (/ (- cur-level prev-level) (org-level-increment)))
  6658. do (org-do-promote))))
  6659. t))))
  6660. (defun org-map-tree (fun)
  6661. "Call FUN for every heading underneath the current one."
  6662. (org-back-to-heading)
  6663. (let ((level (funcall outline-level)))
  6664. (save-excursion
  6665. (funcall fun)
  6666. (while (and (progn
  6667. (outline-next-heading)
  6668. (> (funcall outline-level) level))
  6669. (not (eobp)))
  6670. (funcall fun)))))
  6671. (defun org-map-region (fun beg end)
  6672. "Call FUN for every heading between BEG and END."
  6673. (let ((org-ignore-region t))
  6674. (save-excursion
  6675. (setq end (copy-marker end))
  6676. (goto-char beg)
  6677. (if (and (re-search-forward org-outline-regexp-bol nil t)
  6678. (< (point) end))
  6679. (funcall fun))
  6680. (while (and (progn
  6681. (outline-next-heading)
  6682. (< (point) end))
  6683. (not (eobp)))
  6684. (funcall fun)))))
  6685. (defvar org-property-end-re) ; silence byte-compiler
  6686. (defun org-fixup-indentation (diff)
  6687. "Change the indentation in the current entry by DIFF.
  6688. However, if any line in the current entry has no indentation, or if it
  6689. would end up with no indentation after the change, nothing at all is done."
  6690. (save-excursion
  6691. (let ((end (save-excursion (outline-next-heading)
  6692. (point-marker)))
  6693. (prohibit (if (> diff 0)
  6694. "^\\S-"
  6695. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  6696. col)
  6697. (unless (save-excursion (end-of-line 1)
  6698. (re-search-forward prohibit end t))
  6699. (while (and (< (point) end)
  6700. (re-search-forward "^[ \t]+" end t))
  6701. (goto-char (match-end 0))
  6702. (setq col (current-column))
  6703. (if (< diff 0) (replace-match ""))
  6704. (org-indent-to-column (+ diff col))))
  6705. (move-marker end nil))))
  6706. (defun org-convert-to-odd-levels ()
  6707. "Convert an org-mode file with all levels allowed to one with odd levels.
  6708. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  6709. level 5 etc."
  6710. (interactive)
  6711. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  6712. (let ((outline-level 'org-outline-level)
  6713. (org-odd-levels-only nil) n)
  6714. (save-excursion
  6715. (goto-char (point-min))
  6716. (while (re-search-forward "^\\*\\*+ " nil t)
  6717. (setq n (- (length (match-string 0)) 2))
  6718. (while (>= (setq n (1- n)) 0)
  6719. (org-demote))
  6720. (end-of-line 1))))))
  6721. (defun org-convert-to-oddeven-levels ()
  6722. "Convert an org-mode file with only odd levels to one with odd/even levels.
  6723. This promotes level 3 to level 2, level 5 to level 3 etc. If the
  6724. file contains a section with an even level, conversion would
  6725. destroy the structure of the file. An error is signaled in this
  6726. case."
  6727. (interactive)
  6728. (goto-char (point-min))
  6729. ;; First check if there are no even levels
  6730. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  6731. (org-show-context t)
  6732. (error "Not all levels are odd in this file. Conversion not possible"))
  6733. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  6734. (let ((outline-regexp org-outline-regexp)
  6735. (outline-level 'org-outline-level)
  6736. (org-odd-levels-only nil) n)
  6737. (save-excursion
  6738. (goto-char (point-min))
  6739. (while (re-search-forward "^\\*\\*+ " nil t)
  6740. (setq n (/ (1- (length (match-string 0))) 2))
  6741. (while (>= (setq n (1- n)) 0)
  6742. (org-promote))
  6743. (end-of-line 1))))))
  6744. (defun org-tr-level (n)
  6745. "Make N odd if required."
  6746. (if org-odd-levels-only (1+ (/ n 2)) n))
  6747. ;;; Vertical tree motion, cutting and pasting of subtrees
  6748. (defun org-move-subtree-up (&optional arg)
  6749. "Move the current subtree up past ARG headlines of the same level."
  6750. (interactive "p")
  6751. (org-move-subtree-down (- (prefix-numeric-value arg))))
  6752. (defun org-move-subtree-down (&optional arg)
  6753. "Move the current subtree down past ARG headlines of the same level."
  6754. (interactive "p")
  6755. (setq arg (prefix-numeric-value arg))
  6756. (let ((movfunc (if (> arg 0) 'org-get-next-sibling
  6757. 'org-get-last-sibling))
  6758. (ins-point (make-marker))
  6759. (cnt (abs arg))
  6760. (col (current-column))
  6761. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  6762. ;; Select the tree
  6763. (org-back-to-heading)
  6764. (setq beg0 (point))
  6765. (save-excursion
  6766. (setq ne-beg (org-back-over-empty-lines))
  6767. (setq beg (point)))
  6768. (save-match-data
  6769. (save-excursion (outline-end-of-heading)
  6770. (setq folded (outline-invisible-p)))
  6771. (outline-end-of-subtree))
  6772. (outline-next-heading)
  6773. (setq ne-end (org-back-over-empty-lines))
  6774. (setq end (point))
  6775. (goto-char beg0)
  6776. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  6777. ;; include less whitespace
  6778. (save-excursion
  6779. (goto-char beg)
  6780. (forward-line (- ne-beg ne-end))
  6781. (setq beg (point))))
  6782. ;; Find insertion point, with error handling
  6783. (while (> cnt 0)
  6784. (or (and (funcall movfunc) (looking-at org-outline-regexp))
  6785. (progn (goto-char beg0)
  6786. (error "Cannot move past superior level or buffer limit")))
  6787. (setq cnt (1- cnt)))
  6788. (if (> arg 0)
  6789. ;; Moving forward - still need to move over subtree
  6790. (progn (org-end-of-subtree t t)
  6791. (save-excursion
  6792. (org-back-over-empty-lines)
  6793. (or (bolp) (newline)))))
  6794. (setq ne-ins (org-back-over-empty-lines))
  6795. (move-marker ins-point (point))
  6796. (setq txt (buffer-substring beg end))
  6797. (org-save-markers-in-region beg end)
  6798. (delete-region beg end)
  6799. (org-remove-empty-overlays-at beg)
  6800. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  6801. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  6802. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  6803. (let ((bbb (point)))
  6804. (insert-before-markers txt)
  6805. (org-reinstall-markers-in-region bbb)
  6806. (move-marker ins-point bbb))
  6807. (or (bolp) (insert "\n"))
  6808. (setq ins-end (point))
  6809. (goto-char ins-point)
  6810. (org-skip-whitespace)
  6811. (when (and (< arg 0)
  6812. (org-first-sibling-p)
  6813. (> ne-ins ne-beg))
  6814. ;; Move whitespace back to beginning
  6815. (save-excursion
  6816. (goto-char ins-end)
  6817. (let ((kill-whole-line t))
  6818. (kill-line (- ne-ins ne-beg)) (point)))
  6819. (insert (make-string (- ne-ins ne-beg) ?\n)))
  6820. (move-marker ins-point nil)
  6821. (if folded
  6822. (hide-subtree)
  6823. (org-show-entry)
  6824. (show-children)
  6825. (org-cycle-hide-drawers 'children))
  6826. (org-clean-visibility-after-subtree-move)
  6827. ;; move back to the initial column we were at
  6828. (move-to-column col)))
  6829. (defvar org-subtree-clip ""
  6830. "Clipboard for cut and paste of subtrees.
  6831. This is actually only a copy of the kill, because we use the normal kill
  6832. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  6833. (defvar org-subtree-clip-folded nil
  6834. "Was the last copied subtree folded?
  6835. This is used to fold the tree back after pasting.")
  6836. (defun org-cut-subtree (&optional n)
  6837. "Cut the current subtree into the clipboard.
  6838. With prefix arg N, cut this many sequential subtrees.
  6839. This is a short-hand for marking the subtree and then cutting it."
  6840. (interactive "p")
  6841. (org-copy-subtree n 'cut))
  6842. (defun org-copy-subtree (&optional n cut force-store-markers)
  6843. "Cut the current subtree into the clipboard.
  6844. With prefix arg N, cut this many sequential subtrees.
  6845. This is a short-hand for marking the subtree and then copying it.
  6846. If CUT is non-nil, actually cut the subtree.
  6847. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  6848. of some markers in the region, even if CUT is non-nil. This is
  6849. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  6850. (interactive "p")
  6851. (let (beg end folded (beg0 (point)))
  6852. (if (org-called-interactively-p 'any)
  6853. (org-back-to-heading nil) ; take what looks like a subtree
  6854. (org-back-to-heading t)) ; take what is really there
  6855. (org-back-over-empty-lines)
  6856. (setq beg (point))
  6857. (skip-chars-forward " \t\r\n")
  6858. (save-match-data
  6859. (save-excursion (outline-end-of-heading)
  6860. (setq folded (outline-invisible-p)))
  6861. (condition-case nil
  6862. (org-forward-same-level (1- n) t)
  6863. (error nil))
  6864. (org-end-of-subtree t t))
  6865. (org-back-over-empty-lines)
  6866. (setq end (point))
  6867. (goto-char beg0)
  6868. (when (> end beg)
  6869. (setq org-subtree-clip-folded folded)
  6870. (when (or cut force-store-markers)
  6871. (org-save-markers-in-region beg end))
  6872. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  6873. (setq org-subtree-clip (current-kill 0))
  6874. (message "%s: Subtree(s) with %d characters"
  6875. (if cut "Cut" "Copied")
  6876. (length org-subtree-clip)))))
  6877. (defun org-paste-subtree (&optional level tree for-yank)
  6878. "Paste the clipboard as a subtree, with modification of headline level.
  6879. The entire subtree is promoted or demoted in order to match a new headline
  6880. level.
  6881. If the cursor is at the beginning of a headline, the same level as
  6882. that headline is used to paste the tree
  6883. If not, the new level is derived from the *visible* headings
  6884. before and after the insertion point, and taken to be the inferior headline
  6885. level of the two. So if the previous visible heading is level 3 and the
  6886. next is level 4 (or vice versa), level 4 will be used for insertion.
  6887. This makes sure that the subtree remains an independent subtree and does
  6888. not swallow low level entries.
  6889. You can also force a different level, either by using a numeric prefix
  6890. argument, or by inserting the heading marker by hand. For example, if the
  6891. cursor is after \"*****\", then the tree will be shifted to level 5.
  6892. If optional TREE is given, use this text instead of the kill ring.
  6893. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  6894. move back over whitespace before inserting, and move point to the end of
  6895. the inserted text when done."
  6896. (interactive "P")
  6897. (setq tree (or tree (and kill-ring (current-kill 0))))
  6898. (unless (org-kill-is-subtree-p tree)
  6899. (error "%s"
  6900. (substitute-command-keys
  6901. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  6902. (org-with-limited-levels
  6903. (let* ((visp (not (outline-invisible-p)))
  6904. (txt tree)
  6905. (^re_ "\\(\\*+\\)[ \t]*")
  6906. (old-level (if (string-match org-outline-regexp-bol txt)
  6907. (- (match-end 0) (match-beginning 0) 1)
  6908. -1))
  6909. (force-level (cond (level (prefix-numeric-value level))
  6910. ((and (looking-at "[ \t]*$")
  6911. (string-match
  6912. "^\\*+$" (buffer-substring
  6913. (point-at-bol) (point))))
  6914. (- (match-end 1) (match-beginning 1)))
  6915. ((and (bolp)
  6916. (looking-at org-outline-regexp))
  6917. (- (match-end 0) (point) 1))
  6918. (t nil)))
  6919. (previous-level (save-excursion
  6920. (condition-case nil
  6921. (progn
  6922. (outline-previous-visible-heading 1)
  6923. (if (looking-at ^re_)
  6924. (- (match-end 0) (match-beginning 0) 1)
  6925. 1))
  6926. (error 1))))
  6927. (next-level (save-excursion
  6928. (condition-case nil
  6929. (progn
  6930. (or (looking-at org-outline-regexp)
  6931. (outline-next-visible-heading 1))
  6932. (if (looking-at ^re_)
  6933. (- (match-end 0) (match-beginning 0) 1)
  6934. 1))
  6935. (error 1))))
  6936. (new-level (or force-level (max previous-level next-level)))
  6937. (shift (if (or (= old-level -1)
  6938. (= new-level -1)
  6939. (= old-level new-level))
  6940. 0
  6941. (- new-level old-level)))
  6942. (delta (if (> shift 0) -1 1))
  6943. (func (if (> shift 0) 'org-demote 'org-promote))
  6944. (org-odd-levels-only nil)
  6945. beg end newend)
  6946. ;; Remove the forced level indicator
  6947. (if force-level
  6948. (delete-region (point-at-bol) (point)))
  6949. ;; Paste
  6950. (beginning-of-line (if (bolp) 1 2))
  6951. (unless for-yank (org-back-over-empty-lines))
  6952. (setq beg (point))
  6953. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  6954. (insert-before-markers txt)
  6955. (unless (string-match "\n\\'" txt) (insert "\n"))
  6956. (setq newend (point))
  6957. (org-reinstall-markers-in-region beg)
  6958. (setq end (point))
  6959. (goto-char beg)
  6960. (skip-chars-forward " \t\n\r")
  6961. (setq beg (point))
  6962. (if (and (outline-invisible-p) visp)
  6963. (save-excursion (outline-show-heading)))
  6964. ;; Shift if necessary
  6965. (unless (= shift 0)
  6966. (save-restriction
  6967. (narrow-to-region beg end)
  6968. (while (not (= shift 0))
  6969. (org-map-region func (point-min) (point-max))
  6970. (setq shift (+ delta shift)))
  6971. (goto-char (point-min))
  6972. (setq newend (point-max))))
  6973. (when (or (org-called-interactively-p 'interactive) for-yank)
  6974. (message "Clipboard pasted as level %d subtree" new-level))
  6975. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  6976. kill-ring
  6977. (eq org-subtree-clip (current-kill 0))
  6978. org-subtree-clip-folded)
  6979. ;; The tree was folded before it was killed/copied
  6980. (hide-subtree))
  6981. (and for-yank (goto-char newend)))))
  6982. (defun org-kill-is-subtree-p (&optional txt)
  6983. "Check if the current kill is an outline subtree, or a set of trees.
  6984. Returns nil if kill does not start with a headline, or if the first
  6985. headline level is not the largest headline level in the tree.
  6986. So this will actually accept several entries of equal levels as well,
  6987. which is OK for `org-paste-subtree'.
  6988. If optional TXT is given, check this string instead of the current kill."
  6989. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  6990. (re (org-get-limited-outline-regexp))
  6991. (^re (concat "^" re))
  6992. (start-level (and kill
  6993. (string-match
  6994. (concat "\\`\\([ \t\n\r]*?\n\\)?\\(" re "\\)")
  6995. kill)
  6996. (- (match-end 2) (match-beginning 2) 1)))
  6997. (start (1+ (or (match-beginning 2) -1))))
  6998. (if (not start-level)
  6999. (progn
  7000. nil) ;; does not even start with a heading
  7001. (catch 'exit
  7002. (while (setq start (string-match ^re kill (1+ start)))
  7003. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  7004. (throw 'exit nil)))
  7005. t))))
  7006. (defvar org-markers-to-move nil
  7007. "Markers that should be moved with a cut-and-paste operation.
  7008. Those markers are stored together with their positions relative to
  7009. the start of the region.")
  7010. (defun org-save-markers-in-region (beg end)
  7011. "Check markers in region.
  7012. If these markers are between BEG and END, record their position relative
  7013. to BEG, so that after moving the block of text, we can put the markers back
  7014. into place.
  7015. This function gets called just before an entry or tree gets cut from the
  7016. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  7017. called immediately, to move the markers with the entries."
  7018. (setq org-markers-to-move nil)
  7019. (when (featurep 'org-clock)
  7020. (org-clock-save-markers-for-cut-and-paste beg end))
  7021. (when (featurep 'org-agenda)
  7022. (org-agenda-save-markers-for-cut-and-paste beg end)))
  7023. (defun org-check-and-save-marker (marker beg end)
  7024. "Check if MARKER is between BEG and END.
  7025. If yes, remember the marker and the distance to BEG."
  7026. (when (and (marker-buffer marker)
  7027. (equal (marker-buffer marker) (current-buffer)))
  7028. (if (and (>= marker beg) (< marker end))
  7029. (push (cons marker (- marker beg)) org-markers-to-move))))
  7030. (defun org-reinstall-markers-in-region (beg)
  7031. "Move all remembered markers to their position relative to BEG."
  7032. (mapc (lambda (x)
  7033. (move-marker (car x) (+ beg (cdr x))))
  7034. org-markers-to-move)
  7035. (setq org-markers-to-move nil))
  7036. (defun org-narrow-to-subtree ()
  7037. "Narrow buffer to the current subtree."
  7038. (interactive)
  7039. (save-excursion
  7040. (save-match-data
  7041. (org-with-limited-levels
  7042. (narrow-to-region
  7043. (progn (org-back-to-heading t) (point))
  7044. (progn (org-end-of-subtree t t)
  7045. (if (and (org-on-heading-p) (not (eobp))) (backward-char 1))
  7046. (point)))))))
  7047. (defun org-narrow-to-block ()
  7048. "Narrow buffer to the current block."
  7049. (interactive)
  7050. (let* ((case-fold-search t)
  7051. (blockp (org-between-regexps-p "^[ \t]*#\\+begin_.*"
  7052. "^[ \t]*#\\+end_.*")))
  7053. (if blockp
  7054. (narrow-to-region (car blockp) (cdr blockp))
  7055. (error "Not in a block"))))
  7056. (eval-when-compile
  7057. (defvar org-property-drawer-re))
  7058. (defvar org-property-start-re) ;; defined below
  7059. (defun org-clone-subtree-with-time-shift (n &optional shift)
  7060. "Clone the task (subtree) at point N times.
  7061. The clones will be inserted as siblings.
  7062. In interactive use, the user will be prompted for the number of
  7063. clones to be produced, and for a time SHIFT, which may be a
  7064. repeater as used in time stamps, for example `+3d'.
  7065. When a valid repeater is given and the entry contains any time
  7066. stamps, the clones will become a sequence in time, with time
  7067. stamps in the subtree shifted for each clone produced. If SHIFT
  7068. is nil or the empty string, time stamps will be left alone. The
  7069. ID property of the original subtree is removed.
  7070. If the original subtree did contain time stamps with a repeater,
  7071. the following will happen:
  7072. - the repeater will be removed in each clone
  7073. - an additional clone will be produced, with the current, unshifted
  7074. date(s) in the entry.
  7075. - the original entry will be placed *after* all the clones, with
  7076. repeater intact.
  7077. - the start days in the repeater in the original entry will be shifted
  7078. to past the last clone.
  7079. I this way you can spell out a number of instances of a repeating task,
  7080. and still retain the repeater to cover future instances of the task."
  7081. (interactive "nNumber of clones to produce: \nsDate shift per clone (e.g. +1w, empty to copy unchanged): ")
  7082. (let (beg end template task idprop
  7083. shift-n shift-what doshift nmin nmax (n-no-remove -1))
  7084. (if (not (and (integerp n) (> n 0)))
  7085. (error "Invalid number of replications %s" n))
  7086. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  7087. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([dwmy]\\)[ \t]*\\'"
  7088. shift)))
  7089. (error "Invalid shift specification %s" shift))
  7090. (when doshift
  7091. (setq shift-n (string-to-number (match-string 1 shift))
  7092. shift-what (cdr (assoc (match-string 2 shift)
  7093. '(("d" . day) ("w" . week)
  7094. ("m" . month) ("y" . year))))))
  7095. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  7096. (setq nmin 1 nmax n)
  7097. (org-back-to-heading t)
  7098. (setq beg (point))
  7099. (setq idprop (org-entry-get nil "ID"))
  7100. (org-end-of-subtree t t)
  7101. (or (bolp) (insert "\n"))
  7102. (setq end (point))
  7103. (setq template (buffer-substring beg end))
  7104. ;; Remove clocks and empty drawers
  7105. (with-temp-buffer
  7106. (insert template)
  7107. (goto-char (point-min))
  7108. (while (re-search-forward
  7109. "^[ \t]*CLOCK:.*$" (save-excursion (org-end-of-subtree t t)) t)
  7110. (replace-match "")
  7111. (kill-whole-line))
  7112. (goto-char (point-min))
  7113. (while (re-search-forward
  7114. (concat "^[ \t]*:" (regexp-opt org-drawers) ":[ \t]*$") nil t)
  7115. (mapc (lambda(d) (org-remove-empty-drawer-at d (point))) org-drawers))
  7116. (setq template (buffer-substring (point-min) (point-max))))
  7117. (when (and doshift
  7118. (string-match "<[^<>\n]+ \\+[0-9]+[dwmy][^<>\n]*>" template))
  7119. (delete-region beg end)
  7120. (setq end beg)
  7121. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  7122. (goto-char end)
  7123. (loop for n from nmin to nmax do
  7124. ;; prepare clone
  7125. (with-temp-buffer
  7126. (insert template)
  7127. (org-mode)
  7128. (goto-char (point-min))
  7129. (and idprop (if org-clone-delete-id
  7130. (org-entry-delete nil "ID")
  7131. (org-id-get-create t)))
  7132. (while (re-search-forward org-property-start-re nil t)
  7133. (org-remove-empty-drawer-at "PROPERTIES" (point)))
  7134. (goto-char (point-min))
  7135. (when doshift
  7136. (while (re-search-forward org-ts-regexp-both nil t)
  7137. (org-timestamp-change (* n shift-n) shift-what))
  7138. (unless (= n n-no-remove)
  7139. (goto-char (point-min))
  7140. (while (re-search-forward org-ts-regexp nil t)
  7141. (save-excursion
  7142. (goto-char (match-beginning 0))
  7143. (if (looking-at "<[^<>\n]+\\( +\\+[0-9]+[dwmy]\\)")
  7144. (delete-region (match-beginning 1) (match-end 1)))))))
  7145. (setq task (buffer-string)))
  7146. (insert task))
  7147. (goto-char beg)))
  7148. ;;; Outline Sorting
  7149. (defun org-sort (with-case)
  7150. "Call `org-sort-entries', `org-table-sort-lines' or `org-sort-list'.
  7151. Optional argument WITH-CASE means sort case-sensitively.
  7152. With a double prefix argument, also remove duplicate entries."
  7153. (interactive "P")
  7154. (cond
  7155. ((org-at-table-p) (org-call-with-arg 'org-table-sort-lines with-case))
  7156. ((org-at-item-p) (org-call-with-arg 'org-sort-list with-case))
  7157. (t
  7158. (org-call-with-arg 'org-sort-entries with-case))))
  7159. (defun org-sort-remove-invisible (s)
  7160. (remove-text-properties 0 (length s) org-rm-props s)
  7161. (while (string-match org-bracket-link-regexp s)
  7162. (setq s (replace-match (if (match-end 2)
  7163. (match-string 3 s)
  7164. (match-string 1 s)) t t s)))
  7165. s)
  7166. (defvar org-priority-regexp) ; defined later in the file
  7167. (defvar org-after-sorting-entries-or-items-hook nil
  7168. "Hook that is run after a bunch of entries or items have been sorted.
  7169. When children are sorted, the cursor is in the parent line when this
  7170. hook gets called. When a region or a plain list is sorted, the cursor
  7171. will be in the first entry of the sorted region/list.")
  7172. (defun org-sort-entries
  7173. (&optional with-case sorting-type getkey-func compare-func property)
  7174. "Sort entries on a certain level of an outline tree.
  7175. If there is an active region, the entries in the region are sorted.
  7176. Else, if the cursor is before the first entry, sort the top-level items.
  7177. Else, the children of the entry at point are sorted.
  7178. Sorting can be alphabetically, numerically, by date/time as given by
  7179. a time stamp, by a property or by priority.
  7180. The command prompts for the sorting type unless it has been given to the
  7181. function through the SORTING-TYPE argument, which needs to be a character,
  7182. \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?r ?R ?f ?F). Here is the
  7183. precise meaning of each character:
  7184. n Numerically, by converting the beginning of the entry/item to a number.
  7185. a Alphabetically, ignoring the TODO keyword and the priority, if any.
  7186. t By date/time, either the first active time stamp in the entry, or, if
  7187. none exist, by the first inactive one.
  7188. s By the scheduled date/time.
  7189. d By deadline date/time.
  7190. c By creation time, which is assumed to be the first inactive time stamp
  7191. at the beginning of a line.
  7192. p By priority according to the cookie.
  7193. r By the value of a property.
  7194. Capital letters will reverse the sort order.
  7195. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  7196. called with point at the beginning of the record. It must return either
  7197. a string or a number that should serve as the sorting key for that record.
  7198. Comparing entries ignores case by default. However, with an optional argument
  7199. WITH-CASE, the sorting considers case as well."
  7200. (interactive "P")
  7201. (let ((case-func (if with-case 'identity 'downcase))
  7202. start beg end stars re re2
  7203. txt what tmp)
  7204. ;; Find beginning and end of region to sort
  7205. (cond
  7206. ((org-region-active-p)
  7207. ;; we will sort the region
  7208. (setq end (region-end)
  7209. what "region")
  7210. (goto-char (region-beginning))
  7211. (if (not (org-on-heading-p)) (outline-next-heading))
  7212. (setq start (point)))
  7213. ((or (org-on-heading-p)
  7214. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  7215. ;; we will sort the children of the current headline
  7216. (org-back-to-heading)
  7217. (setq start (point)
  7218. end (progn (org-end-of-subtree t t)
  7219. (or (bolp) (insert "\n"))
  7220. (org-back-over-empty-lines)
  7221. (point))
  7222. what "children")
  7223. (goto-char start)
  7224. (show-subtree)
  7225. (outline-next-heading))
  7226. (t
  7227. ;; we will sort the top-level entries in this file
  7228. (goto-char (point-min))
  7229. (or (org-on-heading-p) (outline-next-heading))
  7230. (setq start (point))
  7231. (goto-char (point-max))
  7232. (beginning-of-line 1)
  7233. (when (looking-at ".*?\\S-")
  7234. ;; File ends in a non-white line
  7235. (end-of-line 1)
  7236. (insert "\n"))
  7237. (setq end (point-max))
  7238. (setq what "top-level")
  7239. (goto-char start)
  7240. (show-all)))
  7241. (setq beg (point))
  7242. (if (>= beg end) (error "Nothing to sort"))
  7243. (looking-at "\\(\\*+\\)")
  7244. (setq stars (match-string 1)
  7245. re (concat "^" (regexp-quote stars) " +")
  7246. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[ \t\n]")
  7247. txt (buffer-substring beg end))
  7248. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  7249. (if (and (not (equal stars "*")) (string-match re2 txt))
  7250. (error "Region to sort contains a level above the first entry"))
  7251. (unless sorting-type
  7252. (message
  7253. "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
  7254. [t]ime [s]cheduled [d]eadline [c]reated
  7255. A/N/T/S/D/C/P/O/F means reversed:"
  7256. what)
  7257. (setq sorting-type (read-char-exclusive))
  7258. (and (= (downcase sorting-type) ?f)
  7259. (setq getkey-func
  7260. (org-icompleting-read "Sort using function: "
  7261. obarray 'fboundp t nil nil))
  7262. (setq getkey-func (intern getkey-func)))
  7263. (and (= (downcase sorting-type) ?r)
  7264. (setq property
  7265. (org-icompleting-read "Property: "
  7266. (mapcar 'list (org-buffer-property-keys t))
  7267. nil t))))
  7268. (message "Sorting entries...")
  7269. (save-restriction
  7270. (narrow-to-region start end)
  7271. (let ((dcst (downcase sorting-type))
  7272. (case-fold-search nil)
  7273. (now (current-time)))
  7274. (sort-subr
  7275. (/= dcst sorting-type)
  7276. ;; This function moves to the beginning character of the "record" to
  7277. ;; be sorted.
  7278. (lambda nil
  7279. (if (re-search-forward re nil t)
  7280. (goto-char (match-beginning 0))
  7281. (goto-char (point-max))))
  7282. ;; This function moves to the last character of the "record" being
  7283. ;; sorted.
  7284. (lambda nil
  7285. (save-match-data
  7286. (condition-case nil
  7287. (outline-forward-same-level 1)
  7288. (error
  7289. (goto-char (point-max))))))
  7290. ;; This function returns the value that gets sorted against.
  7291. (lambda nil
  7292. (cond
  7293. ((= dcst ?n)
  7294. (if (looking-at org-complex-heading-regexp)
  7295. (string-to-number (match-string 4))
  7296. nil))
  7297. ((= dcst ?a)
  7298. (if (looking-at org-complex-heading-regexp)
  7299. (funcall case-func (match-string 4))
  7300. nil))
  7301. ((= dcst ?t)
  7302. (let ((end (save-excursion (outline-next-heading) (point))))
  7303. (if (or (re-search-forward org-ts-regexp end t)
  7304. (re-search-forward org-ts-regexp-both end t))
  7305. (org-time-string-to-seconds (match-string 0))
  7306. (org-float-time now))))
  7307. ((= dcst ?c)
  7308. (let ((end (save-excursion (outline-next-heading) (point))))
  7309. (if (re-search-forward
  7310. (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
  7311. end t)
  7312. (org-time-string-to-seconds (match-string 0))
  7313. (org-float-time now))))
  7314. ((= dcst ?s)
  7315. (let ((end (save-excursion (outline-next-heading) (point))))
  7316. (if (re-search-forward org-scheduled-time-regexp end t)
  7317. (org-time-string-to-seconds (match-string 1))
  7318. (org-float-time now))))
  7319. ((= dcst ?d)
  7320. (let ((end (save-excursion (outline-next-heading) (point))))
  7321. (if (re-search-forward org-deadline-time-regexp end t)
  7322. (org-time-string-to-seconds (match-string 1))
  7323. (org-float-time now))))
  7324. ((= dcst ?p)
  7325. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  7326. (string-to-char (match-string 2))
  7327. org-default-priority))
  7328. ((= dcst ?r)
  7329. (or (org-entry-get nil property) ""))
  7330. ((= dcst ?o)
  7331. (if (looking-at org-complex-heading-regexp)
  7332. (- 9999 (length (member (match-string 2)
  7333. org-todo-keywords-1)))))
  7334. ((= dcst ?f)
  7335. (if getkey-func
  7336. (progn
  7337. (setq tmp (funcall getkey-func))
  7338. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  7339. tmp)
  7340. (error "Invalid key function `%s'" getkey-func)))
  7341. (t (error "Invalid sorting type `%c'" sorting-type))))
  7342. nil
  7343. (cond
  7344. ((= dcst ?a) 'string<)
  7345. ((= dcst ?f) compare-func)
  7346. ((member dcst '(?p ?t ?s ?d ?c)) '<)
  7347. (t nil)))))
  7348. (run-hooks 'org-after-sorting-entries-or-items-hook)
  7349. (message "Sorting entries...done")))
  7350. (defun org-do-sort (table what &optional with-case sorting-type)
  7351. "Sort TABLE of WHAT according to SORTING-TYPE.
  7352. The user will be prompted for the SORTING-TYPE if the call to this
  7353. function does not specify it. WHAT is only for the prompt, to indicate
  7354. what is being sorted. The sorting key will be extracted from
  7355. the car of the elements of the table.
  7356. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  7357. (unless sorting-type
  7358. (message
  7359. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  7360. what)
  7361. (setq sorting-type (read-char-exclusive)))
  7362. (let ((dcst (downcase sorting-type))
  7363. extractfun comparefun)
  7364. ;; Define the appropriate functions
  7365. (cond
  7366. ((= dcst ?n)
  7367. (setq extractfun 'string-to-number
  7368. comparefun (if (= dcst sorting-type) '< '>)))
  7369. ((= dcst ?a)
  7370. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  7371. (lambda(x) (downcase (org-sort-remove-invisible x))))
  7372. comparefun (if (= dcst sorting-type)
  7373. 'string<
  7374. (lambda (a b) (and (not (string< a b))
  7375. (not (string= a b)))))))
  7376. ((= dcst ?t)
  7377. (setq extractfun
  7378. (lambda (x)
  7379. (if (or (string-match org-ts-regexp x)
  7380. (string-match org-ts-regexp-both x))
  7381. (org-float-time
  7382. (org-time-string-to-time (match-string 0 x)))
  7383. 0))
  7384. comparefun (if (= dcst sorting-type) '< '>)))
  7385. (t (error "Invalid sorting type `%c'" sorting-type)))
  7386. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  7387. table)
  7388. (lambda (a b) (funcall comparefun (car a) (car b))))))
  7389. ;;; The orgstruct minor mode
  7390. ;; Define a minor mode which can be used in other modes in order to
  7391. ;; integrate the org-mode structure editing commands.
  7392. ;; This is really a hack, because the org-mode structure commands use
  7393. ;; keys which normally belong to the major mode. Here is how it
  7394. ;; works: The minor mode defines all the keys necessary to operate the
  7395. ;; structure commands, but wraps the commands into a function which
  7396. ;; tests if the cursor is currently at a headline or a plain list
  7397. ;; item. If that is the case, the structure command is used,
  7398. ;; temporarily setting many Org-mode variables like regular
  7399. ;; expressions for filling etc. However, when any of those keys is
  7400. ;; used at a different location, function uses `key-binding' to look
  7401. ;; up if the key has an associated command in another currently active
  7402. ;; keymap (minor modes, major mode, global), and executes that
  7403. ;; command. There might be problems if any of the keys is otherwise
  7404. ;; used as a prefix key.
  7405. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  7406. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  7407. ;; addresses this by checking explicitly for both bindings.
  7408. (defvar orgstruct-mode-map (make-sparse-keymap)
  7409. "Keymap for the minor `orgstruct-mode'.")
  7410. (defvar org-local-vars nil
  7411. "List of local variables, for use by `orgstruct-mode'.")
  7412. ;;;###autoload
  7413. (define-minor-mode orgstruct-mode
  7414. "Toggle the minor mode `orgstruct-mode'.
  7415. This mode is for using Org-mode structure commands in other
  7416. modes. The following keys behave as if Org-mode were active, if
  7417. the cursor is on a headline, or on a plain list item (both as
  7418. defined by Org-mode).
  7419. M-up Move entry/item up
  7420. M-down Move entry/item down
  7421. M-left Promote
  7422. M-right Demote
  7423. M-S-up Move entry/item up
  7424. M-S-down Move entry/item down
  7425. M-S-left Promote subtree
  7426. M-S-right Demote subtree
  7427. M-q Fill paragraph and items like in Org-mode
  7428. C-c ^ Sort entries
  7429. C-c - Cycle list bullet
  7430. TAB Cycle item visibility
  7431. M-RET Insert new heading/item
  7432. S-M-RET Insert new TODO heading / Checkbox item
  7433. C-c C-c Set tags / toggle checkbox"
  7434. nil " OrgStruct" nil
  7435. (org-load-modules-maybe)
  7436. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  7437. ;;;###autoload
  7438. (defun turn-on-orgstruct ()
  7439. "Unconditionally turn on `orgstruct-mode'."
  7440. (orgstruct-mode 1))
  7441. (defun orgstruct++-mode (&optional arg)
  7442. "Toggle `orgstruct-mode', the enhanced version of it.
  7443. In addition to setting orgstruct-mode, this also exports all indentation
  7444. and autofilling variables from org-mode into the buffer. It will also
  7445. recognize item context in multiline items.
  7446. Note that turning off orgstruct-mode will *not* remove the
  7447. indentation/paragraph settings. This can only be done by refreshing the
  7448. major mode, for example with \\[normal-mode]."
  7449. (interactive "P")
  7450. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  7451. (if (< arg 1)
  7452. (orgstruct-mode -1)
  7453. (orgstruct-mode 1)
  7454. (let (var val)
  7455. (mapc
  7456. (lambda (x)
  7457. (when (string-match
  7458. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  7459. (symbol-name (car x)))
  7460. (setq var (car x) val (nth 1 x))
  7461. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  7462. org-local-vars)
  7463. (org-set-local 'orgstruct-is-++ t))))
  7464. (defvar orgstruct-is-++ nil
  7465. "Is `orgstruct-mode' in ++ version in the current-buffer?")
  7466. (make-variable-buffer-local 'orgstruct-is-++)
  7467. ;;;###autoload
  7468. (defun turn-on-orgstruct++ ()
  7469. "Unconditionally turn on `orgstruct++-mode'."
  7470. (orgstruct++-mode 1))
  7471. (defun orgstruct-error ()
  7472. "Error when there is no default binding for a structure key."
  7473. (interactive)
  7474. (error "This key has no function outside structure elements"))
  7475. (defun orgstruct-setup ()
  7476. "Setup orgstruct keymaps."
  7477. (let ((nfunc 0)
  7478. (bindings
  7479. (list
  7480. '([(meta up)] org-metaup)
  7481. '([(meta down)] org-metadown)
  7482. '([(meta left)] org-metaleft)
  7483. '([(meta right)] org-metaright)
  7484. '([(meta shift up)] org-shiftmetaup)
  7485. '([(meta shift down)] org-shiftmetadown)
  7486. '([(meta shift left)] org-shiftmetaleft)
  7487. '([(meta shift right)] org-shiftmetaright)
  7488. '([?\e (up)] org-metaup)
  7489. '([?\e (down)] org-metadown)
  7490. '([?\e (left)] org-metaleft)
  7491. '([?\e (right)] org-metaright)
  7492. '([?\e (shift up)] org-shiftmetaup)
  7493. '([?\e (shift down)] org-shiftmetadown)
  7494. '([?\e (shift left)] org-shiftmetaleft)
  7495. '([?\e (shift right)] org-shiftmetaright)
  7496. '([(shift up)] org-shiftup)
  7497. '([(shift down)] org-shiftdown)
  7498. '([(shift left)] org-shiftleft)
  7499. '([(shift right)] org-shiftright)
  7500. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  7501. '("\M-q" fill-paragraph)
  7502. '("\C-c^" org-sort)
  7503. '("\C-c-" org-cycle-list-bullet)))
  7504. elt key fun cmd)
  7505. (while (setq elt (pop bindings))
  7506. (setq nfunc (1+ nfunc))
  7507. (setq key (org-key (car elt))
  7508. fun (nth 1 elt)
  7509. cmd (orgstruct-make-binding fun nfunc key))
  7510. (org-defkey orgstruct-mode-map key cmd))
  7511. ;; Special treatment needed for TAB and RET
  7512. (org-defkey orgstruct-mode-map [(tab)]
  7513. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  7514. (org-defkey orgstruct-mode-map "\C-i"
  7515. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  7516. (org-defkey orgstruct-mode-map "\M-\C-m"
  7517. (orgstruct-make-binding 'org-insert-heading 105
  7518. "\M-\C-m" [(meta return)]))
  7519. (org-defkey orgstruct-mode-map [(meta return)]
  7520. (orgstruct-make-binding 'org-insert-heading 106
  7521. [(meta return)] "\M-\C-m"))
  7522. (org-defkey orgstruct-mode-map [(shift meta return)]
  7523. (orgstruct-make-binding 'org-insert-todo-heading 107
  7524. [(meta return)] "\M-\C-m"))
  7525. (org-defkey orgstruct-mode-map "\e\C-m"
  7526. (orgstruct-make-binding 'org-insert-heading 108
  7527. "\e\C-m" [?\e (return)]))
  7528. (org-defkey orgstruct-mode-map [?\e (return)]
  7529. (orgstruct-make-binding 'org-insert-heading 109
  7530. [?\e (return)] "\e\C-m"))
  7531. (org-defkey orgstruct-mode-map [?\e (shift return)]
  7532. (orgstruct-make-binding 'org-insert-todo-heading 110
  7533. [?\e (return)] "\e\C-m"))
  7534. (unless org-local-vars
  7535. (setq org-local-vars (org-get-local-variables)))
  7536. t))
  7537. (defun orgstruct-make-binding (fun n &rest keys)
  7538. "Create a function for binding in the structure minor mode.
  7539. FUN is the command to call inside a table. N is used to create a unique
  7540. command name. KEYS are keys that should be checked in for a command
  7541. to execute outside of tables."
  7542. (eval
  7543. (list 'defun
  7544. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  7545. '(arg)
  7546. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  7547. "Outside of structure, run the binding of `"
  7548. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  7549. "'.")
  7550. '(interactive "p")
  7551. (list 'if
  7552. `(org-context-p 'headline 'item
  7553. (and orgstruct-is-++
  7554. ,(and (memq fun '(org-insert-heading org-insert-todo-heading)) t)
  7555. 'item-body))
  7556. (list 'org-run-like-in-org-mode (list 'quote fun))
  7557. (list 'let '(orgstruct-mode)
  7558. (list 'call-interactively
  7559. (append '(or)
  7560. (mapcar (lambda (k)
  7561. (list 'key-binding k))
  7562. keys)
  7563. '('orgstruct-error))))))))
  7564. (defun org-context-p (&rest contexts)
  7565. "Check if local context is any of CONTEXTS.
  7566. Possible values in the list of contexts are `table', `headline', and `item'."
  7567. (let ((pos (point)))
  7568. (goto-char (point-at-bol))
  7569. (prog1 (or (and (memq 'table contexts)
  7570. (looking-at "[ \t]*|"))
  7571. (and (memq 'headline contexts)
  7572. (looking-at org-outline-regexp))
  7573. (and (memq 'item contexts)
  7574. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  7575. (and (memq 'item-body contexts)
  7576. (org-in-item-p)))
  7577. (goto-char pos))))
  7578. (defun org-get-local-variables ()
  7579. "Return a list of all local variables in an org-mode buffer."
  7580. (let (varlist)
  7581. (with-current-buffer (get-buffer-create "*Org tmp*")
  7582. (erase-buffer)
  7583. (org-mode)
  7584. (setq varlist (buffer-local-variables)))
  7585. (kill-buffer "*Org tmp*")
  7586. (delq nil
  7587. (mapcar
  7588. (lambda (x)
  7589. (setq x
  7590. (if (symbolp x)
  7591. (list x)
  7592. (list (car x) (list 'quote (cdr x)))))
  7593. (if (string-match
  7594. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  7595. (symbol-name (car x)))
  7596. x nil))
  7597. varlist))))
  7598. (defun org-clone-local-variables (from-buffer &optional regexp)
  7599. "Clone local variables from FROM-BUFFER.
  7600. Optional argument REGEXP selects variables to clone."
  7601. (mapc
  7602. (lambda (pair)
  7603. (and (symbolp (car pair))
  7604. (or (null regexp)
  7605. (string-match regexp (symbol-name (car pair))))
  7606. (set (make-local-variable (car pair))
  7607. (cdr pair))))
  7608. (buffer-local-variables from-buffer)))
  7609. ;;;###autoload
  7610. (defun org-run-like-in-org-mode (cmd)
  7611. "Run a command, pretending that the current buffer is in Org-mode.
  7612. This will temporarily bind local variables that are typically bound in
  7613. Org-mode to the values they have in Org-mode, and then interactively
  7614. call CMD."
  7615. (org-load-modules-maybe)
  7616. (unless org-local-vars
  7617. (setq org-local-vars (org-get-local-variables)))
  7618. (eval (list 'let org-local-vars
  7619. (list 'call-interactively (list 'quote cmd)))))
  7620. ;;;; Archiving
  7621. (defun org-get-category (&optional pos force-refresh)
  7622. "Get the category applying to position POS."
  7623. (if force-refresh (org-refresh-category-properties))
  7624. (let ((pos (or pos (point))))
  7625. (or (get-text-property pos 'org-category)
  7626. (progn (org-refresh-category-properties)
  7627. (get-text-property pos 'org-category)))))
  7628. (defun org-refresh-category-properties ()
  7629. "Refresh category text properties in the buffer."
  7630. (let ((def-cat (cond
  7631. ((null org-category)
  7632. (if buffer-file-name
  7633. (file-name-sans-extension
  7634. (file-name-nondirectory buffer-file-name))
  7635. "???"))
  7636. ((symbolp org-category) (symbol-name org-category))
  7637. (t org-category)))
  7638. beg end cat pos optionp)
  7639. (org-unmodified
  7640. (save-excursion
  7641. (save-restriction
  7642. (widen)
  7643. (goto-char (point-min))
  7644. (put-text-property (point) (point-max) 'org-category def-cat)
  7645. (while (re-search-forward
  7646. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  7647. (setq pos (match-end 0)
  7648. optionp (equal (char-after (match-beginning 0)) ?#)
  7649. cat (org-trim (match-string 2)))
  7650. (if optionp
  7651. (setq beg (point-at-bol) end (point-max))
  7652. (org-back-to-heading t)
  7653. (setq beg (point) end (org-end-of-subtree t t)))
  7654. (put-text-property beg end 'org-category cat)
  7655. (put-text-property beg end 'org-category-position beg)
  7656. (goto-char pos)))))))
  7657. ;;;; Link Stuff
  7658. ;;; Link abbreviations
  7659. (defun org-link-expand-abbrev (link)
  7660. "Apply replacements as defined in `org-link-abbrev-alist."
  7661. (if (string-match "^\\([^:]*\\)\\(::?\\(.*\\)\\)?$" link)
  7662. (let* ((key (match-string 1 link))
  7663. (as (or (assoc key org-link-abbrev-alist-local)
  7664. (assoc key org-link-abbrev-alist)))
  7665. (tag (and (match-end 2) (match-string 3 link)))
  7666. rpl)
  7667. (if (not as)
  7668. link
  7669. (setq rpl (cdr as))
  7670. (cond
  7671. ((symbolp rpl) (funcall rpl tag))
  7672. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  7673. ((string-match "%h" rpl)
  7674. (replace-match (url-hexify-string (or tag "")) t t rpl))
  7675. (t (concat rpl tag)))))
  7676. link))
  7677. ;;; Storing and inserting links
  7678. (defvar org-insert-link-history nil
  7679. "Minibuffer history for links inserted with `org-insert-link'.")
  7680. (defvar org-stored-links nil
  7681. "Contains the links stored with `org-store-link'.")
  7682. (defvar org-store-link-plist nil
  7683. "Plist with info about the most recently link created with `org-store-link'.")
  7684. (defvar org-link-protocols nil
  7685. "Link protocols added to Org-mode using `org-add-link-type'.")
  7686. (defvar org-store-link-functions nil
  7687. "List of functions that are called to create and store a link.
  7688. Each function will be called in turn until one returns a non-nil
  7689. value. Each function should check if it is responsible for creating
  7690. this link (for example by looking at the major mode).
  7691. If not, it must exit and return nil.
  7692. If yes, it should return a non-nil value after a calling
  7693. `org-store-link-props' with a list of properties and values.
  7694. Special properties are:
  7695. :type The link prefix, like \"http\". This must be given.
  7696. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  7697. This is obligatory as well.
  7698. :description Optional default description for the second pair
  7699. of brackets in an Org-mode link. The user can still change
  7700. this when inserting this link into an Org-mode buffer.
  7701. In addition to these, any additional properties can be specified
  7702. and then used in remember templates.")
  7703. (defun org-add-link-type (type &optional follow export)
  7704. "Add TYPE to the list of `org-link-types'.
  7705. Re-compute all regular expressions depending on `org-link-types'
  7706. FOLLOW and EXPORT are two functions.
  7707. FOLLOW should take the link path as the single argument and do whatever
  7708. is necessary to follow the link, for example find a file or display
  7709. a mail message.
  7710. EXPORT should format the link path for export to one of the export formats.
  7711. It should be a function accepting three arguments:
  7712. path the path of the link, the text after the prefix (like \"http:\")
  7713. desc the description of the link, if any, or a description added by
  7714. org-export-normalize-links if there is none
  7715. format the export format, a symbol like `html' or `latex' or `ascii'..
  7716. The function may use the FORMAT information to return different values
  7717. depending on the format. The return value will be put literally into
  7718. the exported file. If the return value is nil, this means Org should
  7719. do what it normally does with links which do not have EXPORT defined.
  7720. Org-mode has a built-in default for exporting links. If you are happy with
  7721. this default, there is no need to define an export function for the link
  7722. type. For a simple example of an export function, see `org-bbdb.el'."
  7723. (add-to-list 'org-link-types type t)
  7724. (org-make-link-regexps)
  7725. (if (assoc type org-link-protocols)
  7726. (setcdr (assoc type org-link-protocols) (list follow export))
  7727. (push (list type follow export) org-link-protocols)))
  7728. (defvar org-agenda-buffer-name)
  7729. ;;;###autoload
  7730. (defun org-store-link (arg)
  7731. "\\<org-mode-map>Store an org-link to the current location.
  7732. This link is added to `org-stored-links' and can later be inserted
  7733. into an org-buffer with \\[org-insert-link].
  7734. For some link types, a prefix arg is interpreted:
  7735. For links to usenet articles, arg negates `org-gnus-prefer-web-links'.
  7736. For file links, arg negates `org-context-in-file-links'."
  7737. (interactive "P")
  7738. (org-load-modules-maybe)
  7739. (setq org-store-link-plist nil) ; reset
  7740. (org-with-limited-levels
  7741. (let (link cpltxt desc description search txt custom-id agenda-link)
  7742. (cond
  7743. ((run-hook-with-args-until-success 'org-store-link-functions)
  7744. (setq link (plist-get org-store-link-plist :link)
  7745. desc (or (plist-get org-store-link-plist :description) link)))
  7746. ((equal (buffer-name) "*Org Edit Src Example*")
  7747. (let (label gc)
  7748. (while (or (not label)
  7749. (save-excursion
  7750. (save-restriction
  7751. (widen)
  7752. (goto-char (point-min))
  7753. (re-search-forward
  7754. (regexp-quote (format org-coderef-label-format label))
  7755. nil t))))
  7756. (when label (message "Label exists already") (sit-for 2))
  7757. (setq label (read-string "Code line label: " label)))
  7758. (end-of-line 1)
  7759. (setq link (format org-coderef-label-format label))
  7760. (setq gc (- 79 (length link)))
  7761. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  7762. (insert link)
  7763. (setq link (concat "(" label ")") desc nil)))
  7764. ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
  7765. ;; We are in the agenda, link to referenced location
  7766. (let ((m (or (get-text-property (point) 'org-hd-marker)
  7767. (get-text-property (point) 'org-marker))))
  7768. (when m
  7769. (org-with-point-at m
  7770. (setq agenda-link
  7771. (if (org-called-interactively-p 'any)
  7772. (call-interactively 'org-store-link)
  7773. (org-store-link nil)))))))
  7774. ((eq major-mode 'calendar-mode)
  7775. (let ((cd (calendar-cursor-to-date)))
  7776. (setq link
  7777. (format-time-string
  7778. (car org-time-stamp-formats)
  7779. (apply 'encode-time
  7780. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  7781. nil nil nil))))
  7782. (org-store-link-props :type "calendar" :date cd)))
  7783. ((eq major-mode 'w3-mode)
  7784. (setq cpltxt (if (and (buffer-name)
  7785. (not (string-match "Untitled" (buffer-name))))
  7786. (buffer-name)
  7787. (url-view-url t))
  7788. link (org-make-link (url-view-url t)))
  7789. (org-store-link-props :type "w3" :url (url-view-url t)))
  7790. ((eq major-mode 'w3m-mode)
  7791. (setq cpltxt (or w3m-current-title w3m-current-url)
  7792. link (org-make-link w3m-current-url))
  7793. (org-store-link-props :type "w3m" :url (url-view-url t)))
  7794. ((setq search (run-hook-with-args-until-success
  7795. 'org-create-file-search-functions))
  7796. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  7797. "::" search))
  7798. (setq cpltxt (or description link)))
  7799. ((eq major-mode 'image-mode)
  7800. (setq cpltxt (concat "file:"
  7801. (abbreviate-file-name buffer-file-name))
  7802. link (org-make-link cpltxt))
  7803. (org-store-link-props :type "image" :file buffer-file-name))
  7804. ((eq major-mode 'dired-mode)
  7805. ;; link to the file in the current line
  7806. (let ((file (dired-get-filename nil t)))
  7807. (setq file (if file
  7808. (abbreviate-file-name
  7809. (expand-file-name (dired-get-filename nil t)))
  7810. ;; otherwise, no file so use current directory.
  7811. default-directory))
  7812. (setq cpltxt (concat "file:" file)
  7813. link (org-make-link cpltxt))))
  7814. ((and (buffer-file-name (buffer-base-buffer)) (eq major-mode 'org-mode))
  7815. (setq custom-id (org-entry-get nil "CUSTOM_ID"))
  7816. (cond
  7817. ((org-in-regexp "<<\\(.*?\\)>>")
  7818. (setq cpltxt
  7819. (concat "file:"
  7820. (abbreviate-file-name
  7821. (buffer-file-name (buffer-base-buffer)))
  7822. "::" (match-string 1))
  7823. link (org-make-link cpltxt)))
  7824. ((and (featurep 'org-id)
  7825. (or (eq org-link-to-org-use-id t)
  7826. (and (eq org-link-to-org-use-id 'create-if-interactive)
  7827. (org-called-interactively-p 'any))
  7828. (and (eq org-link-to-org-use-id
  7829. 'create-if-interactive-and-no-custom-id)
  7830. (org-called-interactively-p 'any)
  7831. (not custom-id))
  7832. (and org-link-to-org-use-id
  7833. (org-entry-get nil "ID"))))
  7834. ;; We can make a link using the ID.
  7835. (setq link (condition-case nil
  7836. (prog1 (org-id-store-link)
  7837. (setq desc (plist-get org-store-link-plist
  7838. :description)))
  7839. (error
  7840. ;; probably before first headline, link to file only
  7841. (concat "file:"
  7842. (abbreviate-file-name
  7843. (buffer-file-name (buffer-base-buffer))))))))
  7844. (t
  7845. ;; Just link to current headline
  7846. (setq cpltxt (concat "file:"
  7847. (abbreviate-file-name
  7848. (buffer-file-name (buffer-base-buffer)))))
  7849. ;; Add a context search string
  7850. (when (org-xor org-context-in-file-links arg)
  7851. (setq txt (cond
  7852. ((org-on-heading-p) nil)
  7853. ((org-region-active-p)
  7854. (buffer-substring (region-beginning) (region-end)))
  7855. (t nil)))
  7856. (when (or (null txt) (string-match "\\S-" txt))
  7857. (setq cpltxt
  7858. (concat cpltxt "::"
  7859. (condition-case nil
  7860. (org-make-org-heading-search-string txt)
  7861. (error "")))
  7862. desc (or (nth 4 (ignore-errors
  7863. (org-heading-components))) "NONE"))))
  7864. (if (string-match "::\\'" cpltxt)
  7865. (setq cpltxt (substring cpltxt 0 -2)))
  7866. (setq link (org-make-link cpltxt)))))
  7867. ((buffer-file-name (buffer-base-buffer))
  7868. ;; Just link to this file here.
  7869. (setq cpltxt (concat "file:"
  7870. (abbreviate-file-name
  7871. (buffer-file-name (buffer-base-buffer)))))
  7872. ;; Add a context string
  7873. (when (org-xor org-context-in-file-links arg)
  7874. (setq txt (if (org-region-active-p)
  7875. (buffer-substring (region-beginning) (region-end))
  7876. (buffer-substring (point-at-bol) (point-at-eol))))
  7877. ;; Only use search option if there is some text.
  7878. (when (string-match "\\S-" txt)
  7879. (setq cpltxt
  7880. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  7881. desc "NONE")))
  7882. (setq link (org-make-link cpltxt)))
  7883. ((org-called-interactively-p 'interactive)
  7884. (error "Cannot link to a buffer which is not visiting a file"))
  7885. (t (setq link nil)))
  7886. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  7887. (setq link (or link cpltxt)
  7888. desc (or desc cpltxt))
  7889. (if (equal desc "NONE") (setq desc nil))
  7890. (if (and (or (org-called-interactively-p 'any) executing-kbd-macro) link)
  7891. (progn
  7892. (setq org-stored-links
  7893. (cons (list link desc) org-stored-links))
  7894. (message "Stored: %s" (or desc link))
  7895. (when custom-id
  7896. (setq link (concat "file:" (abbreviate-file-name (buffer-file-name))
  7897. "::#" custom-id))
  7898. (setq org-stored-links
  7899. (cons (list link desc) org-stored-links))))
  7900. (or agenda-link (and link (org-make-link-string link desc)))))))
  7901. (defun org-store-link-props (&rest plist)
  7902. "Store link properties, extract names and addresses."
  7903. (let (x adr)
  7904. (when (setq x (plist-get plist :from))
  7905. (setq adr (mail-extract-address-components x))
  7906. (setq plist (plist-put plist :fromname (car adr)))
  7907. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  7908. (when (setq x (plist-get plist :to))
  7909. (setq adr (mail-extract-address-components x))
  7910. (setq plist (plist-put plist :toname (car adr)))
  7911. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  7912. (let ((from (plist-get plist :from))
  7913. (to (plist-get plist :to)))
  7914. (when (and from to org-from-is-user-regexp)
  7915. (setq plist
  7916. (plist-put plist :fromto
  7917. (if (string-match org-from-is-user-regexp from)
  7918. (concat "to %t")
  7919. (concat "from %f"))))))
  7920. (setq org-store-link-plist plist))
  7921. (defun org-add-link-props (&rest plist)
  7922. "Add these properties to the link property list."
  7923. (let (key value)
  7924. (while plist
  7925. (setq key (pop plist) value (pop plist))
  7926. (setq org-store-link-plist
  7927. (plist-put org-store-link-plist key value)))))
  7928. (defun org-email-link-description (&optional fmt)
  7929. "Return the description part of an email link.
  7930. This takes information from `org-store-link-plist' and formats it
  7931. according to FMT (default from `org-email-link-description-format')."
  7932. (setq fmt (or fmt org-email-link-description-format))
  7933. (let* ((p org-store-link-plist)
  7934. (to (plist-get p :toaddress))
  7935. (from (plist-get p :fromaddress))
  7936. (table
  7937. (list
  7938. (cons "%c" (plist-get p :fromto))
  7939. (cons "%F" (plist-get p :from))
  7940. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  7941. (cons "%T" (plist-get p :to))
  7942. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  7943. (cons "%s" (plist-get p :subject))
  7944. (cons "%d" (plist-get p :date))
  7945. (cons "%m" (plist-get p :message-id)))))
  7946. (when (string-match "%c" fmt)
  7947. ;; Check if the user wrote this message
  7948. (if (and org-from-is-user-regexp from to
  7949. (save-match-data (string-match org-from-is-user-regexp from)))
  7950. (setq fmt (replace-match "to %t" t t fmt))
  7951. (setq fmt (replace-match "from %f" t t fmt))))
  7952. (org-replace-escapes fmt table)))
  7953. (defun org-make-org-heading-search-string (&optional string heading)
  7954. "Make search string for STRING or current headline."
  7955. (interactive)
  7956. (let ((s (or string (org-get-heading)))
  7957. (lines org-context-in-file-links))
  7958. (unless (and string (not heading))
  7959. ;; We are using a headline, clean up garbage in there.
  7960. (if (string-match org-todo-regexp s)
  7961. (setq s (replace-match "" t t s)))
  7962. (if (string-match (org-re ":[[:alnum:]_@#%:]+:[ \t]*$") s)
  7963. (setq s (replace-match "" t t s)))
  7964. (setq s (org-trim s))
  7965. (if (string-match (concat "^\\(" org-quote-string "\\|"
  7966. org-comment-string "\\)") s)
  7967. (setq s (replace-match "" t t s)))
  7968. (while (string-match org-ts-regexp s)
  7969. (setq s (replace-match "" t t s))))
  7970. (or string (setq s (concat "*" s))) ; Add * for headlines
  7971. (when (and string (integerp lines) (> lines 0))
  7972. (let ((slines (org-split-string s "\n")))
  7973. (when (< lines (length slines))
  7974. (setq s (mapconcat
  7975. 'identity
  7976. (reverse (nthcdr (- (length slines) lines)
  7977. (reverse slines))) "\n")))))
  7978. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  7979. (defun org-make-link (&rest strings)
  7980. "Concatenate STRINGS."
  7981. (apply 'concat strings))
  7982. (defun org-make-link-string (link &optional description)
  7983. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  7984. (unless (string-match "\\S-" link)
  7985. (error "Empty link"))
  7986. (when (and description
  7987. (stringp description)
  7988. (not (string-match "\\S-" description)))
  7989. (setq description nil))
  7990. (when (stringp description)
  7991. ;; Remove brackets from the description, they are fatal.
  7992. (while (string-match "\\[" description)
  7993. (setq description (replace-match "{" t t description)))
  7994. (while (string-match "\\]" description)
  7995. (setq description (replace-match "}" t t description))))
  7996. (when (equal link description)
  7997. ;; No description needed, it is identical
  7998. (setq description nil))
  7999. (when (and (not description)
  8000. (not (string-match (org-image-file-name-regexp) link))
  8001. (not (equal link (org-link-escape link))))
  8002. (setq description (org-extract-attributes link)))
  8003. (setq link
  8004. (cond ((string-match (org-image-file-name-regexp) link) link)
  8005. ((string-match org-link-types-re link)
  8006. (concat (match-string 1 link)
  8007. (org-link-escape (substring link (match-end 1)))))
  8008. (t (org-link-escape link))))
  8009. (concat "[[" link "]"
  8010. (if description (concat "[" description "]") "")
  8011. "]"))
  8012. (defconst org-link-escape-chars
  8013. '(?\ ?\[ ?\] ?\; ?\= ?\+)
  8014. "List of characters that should be escaped in link.
  8015. This is the list that is used for internal purposes.")
  8016. (defvar org-url-encoding-use-url-hexify nil)
  8017. (defconst org-link-escape-chars-browser
  8018. '(?\ )
  8019. "List of escapes for characters that are problematic in links.
  8020. This is the list that is used before handing over to the browser.")
  8021. (defun org-link-escape (text &optional table merge)
  8022. "Return percent escaped representation of TEXT.
  8023. TEXT is a string with the text to escape.
  8024. Optional argument TABLE is a list with characters that should be
  8025. escaped. When nil, `org-link-escape-chars' is used.
  8026. If optional argument MERGE is set, merge TABLE into
  8027. `org-link-escape-chars'."
  8028. (if (and org-url-encoding-use-url-hexify (not table))
  8029. (url-hexify-string text)
  8030. (cond
  8031. ((and table merge)
  8032. (mapc (lambda (defchr)
  8033. (unless (member defchr table)
  8034. (setq table (cons defchr table)))) org-link-escape-chars))
  8035. ((null table)
  8036. (setq table org-link-escape-chars)))
  8037. (mapconcat
  8038. (lambda (char)
  8039. (if (or (member char table)
  8040. (< char 32) (= char 37) (> char 126))
  8041. (mapconcat (lambda (sequence-element)
  8042. (format "%%%.2X" sequence-element))
  8043. (or (encode-coding-char char 'utf-8)
  8044. (error "Unable to percent escape character: %s"
  8045. (char-to-string char))) "")
  8046. (char-to-string char))) text "")))
  8047. (defun org-link-unescape (str)
  8048. "Unhex hexified unicode strings as returned from the JavaScript function
  8049. encodeURIComponent. E.g. `%C3%B6' is the german Umlaut `ö'."
  8050. (unless (and (null str) (string= "" str))
  8051. (let ((pos 0) (case-fold-search t) unhexed)
  8052. (while (setq pos (string-match "\\(%[0-9a-f][0-9a-f]\\)+" str pos))
  8053. (setq unhexed (org-link-unescape-compound (match-string 0 str)))
  8054. (setq str (replace-match unhexed t t str))
  8055. (setq pos (+ pos (length unhexed))))))
  8056. str)
  8057. (defun org-link-unescape-compound (hex)
  8058. "Unhexify unicode hex-chars. E.g. `%C3%B6' is the German Umlaut `ö'.
  8059. Note: this function also decodes single byte encodings like
  8060. `%E1' (\"á\") if not followed by another `%[A-F0-9]{2}' group."
  8061. (save-match-data
  8062. (let* ((bytes (cdr (split-string hex "%")))
  8063. (ret "")
  8064. (eat 0)
  8065. (sum 0))
  8066. (while bytes
  8067. (let* ((val (string-to-number (pop bytes) 16))
  8068. (shift-xor
  8069. (if (= 0 eat)
  8070. (cond
  8071. ((>= val 252) (cons 6 252))
  8072. ((>= val 248) (cons 5 248))
  8073. ((>= val 240) (cons 4 240))
  8074. ((>= val 224) (cons 3 224))
  8075. ((>= val 192) (cons 2 192))
  8076. (t (cons 0 0)))
  8077. (cons 6 128))))
  8078. (if (>= val 192) (setq eat (car shift-xor)))
  8079. (setq val (logxor val (cdr shift-xor)))
  8080. (setq sum (+ (lsh sum (car shift-xor)) val))
  8081. (if (> eat 0) (setq eat (- eat 1)))
  8082. (cond
  8083. ((= 0 eat) ;multi byte
  8084. (setq ret (concat ret (org-char-to-string sum)))
  8085. (setq sum 0))
  8086. ((not bytes) ; single byte(s)
  8087. (setq ret (org-link-unescape-single-byte-sequence hex))))
  8088. )) ;; end (while bytes
  8089. ret )))
  8090. (defun org-link-unescape-single-byte-sequence (hex)
  8091. "Unhexify hex-encoded single byte character sequences."
  8092. (mapconcat (lambda (byte)
  8093. (char-to-string (string-to-number byte 16)))
  8094. (cdr (split-string hex "%")) ""))
  8095. (defun org-xor (a b)
  8096. "Exclusive or."
  8097. (if a (not b) b))
  8098. (defun org-fixup-message-id-for-http (s)
  8099. "Replace special characters in a message id, so it can be used in an http query."
  8100. (when (string-match "%" s)
  8101. (setq s (mapconcat (lambda (c)
  8102. (if (eq c ?%)
  8103. "%25"
  8104. (char-to-string c)))
  8105. s "")))
  8106. (while (string-match "<" s)
  8107. (setq s (replace-match "%3C" t t s)))
  8108. (while (string-match ">" s)
  8109. (setq s (replace-match "%3E" t t s)))
  8110. (while (string-match "@" s)
  8111. (setq s (replace-match "%40" t t s)))
  8112. s)
  8113. ;;;###autoload
  8114. (defun org-insert-link-global ()
  8115. "Insert a link like Org-mode does.
  8116. This command can be called in any mode to insert a link in Org-mode syntax."
  8117. (interactive)
  8118. (org-load-modules-maybe)
  8119. (org-run-like-in-org-mode 'org-insert-link))
  8120. (defun org-insert-link (&optional complete-file link-location default-description)
  8121. "Insert a link. At the prompt, enter the link.
  8122. Completion can be used to insert any of the link protocol prefixes like
  8123. http or ftp in use.
  8124. The history can be used to select a link previously stored with
  8125. `org-store-link'. When the empty string is entered (i.e. if you just
  8126. press RET at the prompt), the link defaults to the most recently
  8127. stored link. As SPC triggers completion in the minibuffer, you need to
  8128. use M-SPC or C-q SPC to force the insertion of a space character.
  8129. You will also be prompted for a description, and if one is given, it will
  8130. be displayed in the buffer instead of the link.
  8131. If there is already a link at point, this command will allow you to edit link
  8132. and description parts.
  8133. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  8134. be selected using completion. The path to the file will be relative to the
  8135. current directory if the file is in the current directory or a subdirectory.
  8136. Otherwise, the link will be the absolute path as completed in the minibuffer
  8137. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  8138. option `org-link-file-path-type'.
  8139. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  8140. the current directory or below.
  8141. With three \\[universal-argument] prefixes, negate the meaning of
  8142. `org-keep-stored-link-after-insertion'.
  8143. If `org-make-link-description-function' is non-nil, this function will be
  8144. called with the link target, and the result will be the default
  8145. link description.
  8146. If the LINK-LOCATION parameter is non-nil, this value will be
  8147. used as the link location instead of reading one interactively.
  8148. If the DEFAULT-DESCRIPTION parameter is non-nil, this value will
  8149. be used as the default description."
  8150. (interactive "P")
  8151. (let* ((wcf (current-window-configuration))
  8152. (region (if (org-region-active-p)
  8153. (buffer-substring (region-beginning) (region-end))))
  8154. (remove (and region (list (region-beginning) (region-end))))
  8155. (desc region)
  8156. tmphist ; byte-compile incorrectly complains about this
  8157. (link link-location)
  8158. entry file all-prefixes)
  8159. (cond
  8160. (link-location) ; specified by arg, just use it.
  8161. ((org-in-regexp org-bracket-link-regexp 1)
  8162. ;; We do have a link at point, and we are going to edit it.
  8163. (setq remove (list (match-beginning 0) (match-end 0)))
  8164. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  8165. (setq link (read-string "Link: "
  8166. (org-link-unescape
  8167. (org-match-string-no-properties 1)))))
  8168. ((or (org-in-regexp org-angle-link-re)
  8169. (org-in-regexp org-plain-link-re))
  8170. ;; Convert to bracket link
  8171. (setq remove (list (match-beginning 0) (match-end 0))
  8172. link (read-string "Link: "
  8173. (org-remove-angle-brackets (match-string 0)))))
  8174. ((member complete-file '((4) (16)))
  8175. ;; Completing read for file names.
  8176. (setq link (org-file-complete-link complete-file)))
  8177. (t
  8178. ;; Read link, with completion for stored links.
  8179. (with-output-to-temp-buffer "*Org Links*"
  8180. (princ "Insert a link.
  8181. Use TAB to complete link prefixes, then RET for type-specific completion support\n")
  8182. (when org-stored-links
  8183. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  8184. (princ (mapconcat
  8185. (lambda (x)
  8186. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  8187. (reverse org-stored-links) "\n"))))
  8188. (let ((cw (selected-window)))
  8189. (select-window (get-buffer-window "*Org Links*" 'visible))
  8190. (with-current-buffer "*Org Links*" (setq truncate-lines t))
  8191. (unless (pos-visible-in-window-p (point-max))
  8192. (org-fit-window-to-buffer))
  8193. (and (window-live-p cw) (select-window cw)))
  8194. ;; Fake a link history, containing the stored links.
  8195. (setq tmphist (append (mapcar 'car org-stored-links)
  8196. org-insert-link-history))
  8197. (setq all-prefixes (append (mapcar 'car org-link-abbrev-alist-local)
  8198. (mapcar 'car org-link-abbrev-alist)
  8199. org-link-types))
  8200. (unwind-protect
  8201. (progn
  8202. (setq link
  8203. (let ((org-completion-use-ido nil)
  8204. (org-completion-use-iswitchb nil))
  8205. (org-completing-read
  8206. "Link: "
  8207. (append
  8208. (mapcar (lambda (x) (list (concat x ":")))
  8209. all-prefixes)
  8210. (mapcar 'car org-stored-links))
  8211. nil nil nil
  8212. 'tmphist
  8213. (car (car org-stored-links)))))
  8214. (if (not (string-match "\\S-" link))
  8215. (error "No link selected"))
  8216. (if (or (member link all-prefixes)
  8217. (and (equal ":" (substring link -1))
  8218. (member (substring link 0 -1) all-prefixes)
  8219. (setq link (substring link 0 -1))))
  8220. (setq link (org-link-try-special-completion link))))
  8221. (set-window-configuration wcf)
  8222. (kill-buffer "*Org Links*"))
  8223. (setq entry (assoc link org-stored-links))
  8224. (or entry (push link org-insert-link-history))
  8225. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  8226. (not org-keep-stored-link-after-insertion))
  8227. (setq org-stored-links (delq (assoc link org-stored-links)
  8228. org-stored-links)))
  8229. (setq desc (or desc (nth 1 entry)))))
  8230. (if (string-match org-plain-link-re link)
  8231. ;; URL-like link, normalize the use of angular brackets.
  8232. (setq link (org-make-link (org-remove-angle-brackets link))))
  8233. ;; Check if we are linking to the current file with a search option
  8234. ;; If yes, simplify the link by using only the search option.
  8235. (when (and buffer-file-name
  8236. (string-match "^file:\\(.+?\\)::\\([^>]+\\)" link))
  8237. (let* ((path (match-string 1 link))
  8238. (case-fold-search nil)
  8239. (search (match-string 2 link)))
  8240. (save-match-data
  8241. (if (equal (file-truename buffer-file-name) (file-truename path))
  8242. ;; We are linking to this same file, with a search option
  8243. (setq link search)))))
  8244. ;; Check if we can/should use a relative path. If yes, simplify the link
  8245. (when (string-match "^\\(file:\\|docview:\\)\\(.*\\)" link)
  8246. (let* ((type (match-string 1 link))
  8247. (path (match-string 2 link))
  8248. (origpath path)
  8249. (case-fold-search nil))
  8250. (cond
  8251. ((or (eq org-link-file-path-type 'absolute)
  8252. (equal complete-file '(16)))
  8253. (setq path (abbreviate-file-name (expand-file-name path))))
  8254. ((eq org-link-file-path-type 'noabbrev)
  8255. (setq path (expand-file-name path)))
  8256. ((eq org-link-file-path-type 'relative)
  8257. (setq path (file-relative-name path)))
  8258. (t
  8259. (save-match-data
  8260. (if (string-match (concat "^" (regexp-quote
  8261. (expand-file-name
  8262. (file-name-as-directory
  8263. default-directory))))
  8264. (expand-file-name path))
  8265. ;; We are linking a file with relative path name.
  8266. (setq path (substring (expand-file-name path)
  8267. (match-end 0)))
  8268. (setq path (abbreviate-file-name (expand-file-name path)))))))
  8269. (setq link (concat type path))
  8270. (if (equal desc origpath)
  8271. (setq desc path))))
  8272. (if org-make-link-description-function
  8273. (setq desc (funcall org-make-link-description-function link desc))
  8274. (if default-description (setq desc default-description)))
  8275. (setq desc (read-string "Description: " desc))
  8276. (unless (string-match "\\S-" desc) (setq desc nil))
  8277. (if remove (apply 'delete-region remove))
  8278. (insert (org-make-link-string link desc))))
  8279. (defun org-link-try-special-completion (type)
  8280. "If there is completion support for link type TYPE, offer it."
  8281. (let ((fun (intern (concat "org-" type "-complete-link"))))
  8282. (if (functionp fun)
  8283. (funcall fun)
  8284. (read-string "Link (no completion support): " (concat type ":")))))
  8285. (defun org-file-complete-link (&optional arg)
  8286. "Create a file link using completion."
  8287. (let (file link)
  8288. (setq file (read-file-name "File: "))
  8289. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  8290. (pwd1 (file-name-as-directory (abbreviate-file-name
  8291. (expand-file-name ".")))))
  8292. (cond
  8293. ((equal arg '(16))
  8294. (setq link (org-make-link
  8295. "file:"
  8296. (abbreviate-file-name (expand-file-name file)))))
  8297. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  8298. (setq link (org-make-link "file:" (match-string 1 file))))
  8299. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  8300. (expand-file-name file))
  8301. (setq link (org-make-link
  8302. "file:" (match-string 1 (expand-file-name file)))))
  8303. (t (setq link (org-make-link "file:" file)))))
  8304. link))
  8305. (defun org-completing-read (&rest args)
  8306. "Completing-read with SPACE being a normal character."
  8307. (let ((enable-recursive-minibuffers t)
  8308. (minibuffer-local-completion-map
  8309. (copy-keymap minibuffer-local-completion-map)))
  8310. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  8311. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  8312. (org-defkey minibuffer-local-completion-map (kbd "C-c !") 'org-time-stamp-inactive)
  8313. (apply 'org-icompleting-read args)))
  8314. (defun org-completing-read-no-i (&rest args)
  8315. (let (org-completion-use-ido org-completion-use-iswitchb)
  8316. (apply 'org-completing-read args)))
  8317. (defun org-iswitchb-completing-read (prompt choices &rest args)
  8318. "Use iswitch as a completing-read replacement to choose from choices.
  8319. PROMPT is a string to prompt with. CHOICES is a list of strings to choose
  8320. from."
  8321. (let* ((iswitchb-use-virtual-buffers nil)
  8322. (iswitchb-make-buflist-hook
  8323. (lambda ()
  8324. (setq iswitchb-temp-buflist choices))))
  8325. (iswitchb-read-buffer prompt)))
  8326. (defun org-icompleting-read (&rest args)
  8327. "Completing-read using `ido-mode' or `iswitchb' speedups if available."
  8328. (org-without-partial-completion
  8329. (if (and org-completion-use-ido
  8330. (fboundp 'ido-completing-read)
  8331. (boundp 'ido-mode) ido-mode
  8332. (listp (second args)))
  8333. (let ((ido-enter-matching-directory nil))
  8334. (apply 'ido-completing-read (concat (car args))
  8335. (if (consp (car (nth 1 args)))
  8336. (mapcar 'car (nth 1 args))
  8337. (nth 1 args))
  8338. (cddr args)))
  8339. (if (and org-completion-use-iswitchb
  8340. (boundp 'iswitchb-mode) iswitchb-mode
  8341. (listp (second args)))
  8342. (apply 'org-iswitchb-completing-read (concat (car args))
  8343. (if (consp (car (nth 1 args)))
  8344. (mapcar 'car (nth 1 args))
  8345. (nth 1 args))
  8346. (cddr args))
  8347. (apply 'completing-read args)))))
  8348. (defun org-extract-attributes (s)
  8349. "Extract the attributes cookie from a string and set as text property."
  8350. (let (a attr (start 0) key value)
  8351. (save-match-data
  8352. (when (string-match "{{\\([^}]+\\)}}$" s)
  8353. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  8354. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  8355. (setq key (match-string 1 a) value (match-string 2 a)
  8356. start (match-end 0)
  8357. attr (plist-put attr (intern key) value))))
  8358. (org-add-props s nil 'org-attr attr))
  8359. s))
  8360. (defun org-extract-attributes-from-string (tag)
  8361. (let (key value attr)
  8362. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"\\s-?" tag)
  8363. (setq key (match-string 1 tag) value (match-string 2 tag)
  8364. tag (replace-match "" t t tag)
  8365. attr (plist-put attr (intern key) value)))
  8366. (cons tag attr)))
  8367. (defun org-attributes-to-string (plist)
  8368. "Format a property list into an HTML attribute list."
  8369. (let ((s "") key value)
  8370. (while plist
  8371. (setq key (pop plist) value (pop plist))
  8372. (and value
  8373. (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
  8374. s))
  8375. ;;; Opening/following a link
  8376. (defvar org-link-search-failed nil)
  8377. (defvar org-open-link-functions nil
  8378. "Hook for functions finding a plain text link.
  8379. These functions must take a single argument, the link content.
  8380. They will be called for links that look like [[link text][description]]
  8381. when LINK TEXT does not have a protocol like \"http:\" and does not look
  8382. like a filename (e.g. \"./blue.png\").
  8383. These functions will be called *before* Org attempts to resolve the
  8384. link by doing text searches in the current buffer - so if you want a
  8385. link \"[[target]]\" to still find \"<<target>>\", your function should
  8386. handle this as a special case.
  8387. When the function does handle the link, it must return a non-nil value.
  8388. If it decides that it is not responsible for this link, it must return
  8389. nil to indicate that that Org-mode can continue with other options
  8390. like exact and fuzzy text search.")
  8391. (defun org-next-link ()
  8392. "Move forward to the next link.
  8393. If the link is in hidden text, expose it."
  8394. (interactive)
  8395. (when (and org-link-search-failed (eq this-command last-command))
  8396. (goto-char (point-min))
  8397. (message "Link search wrapped back to beginning of buffer"))
  8398. (setq org-link-search-failed nil)
  8399. (let* ((pos (point))
  8400. (ct (org-context))
  8401. (a (assoc :link ct)))
  8402. (if a (goto-char (nth 2 a)))
  8403. (if (re-search-forward org-any-link-re nil t)
  8404. (progn
  8405. (goto-char (match-beginning 0))
  8406. (if (outline-invisible-p) (org-show-context)))
  8407. (goto-char pos)
  8408. (setq org-link-search-failed t)
  8409. (error "No further link found"))))
  8410. (defun org-previous-link ()
  8411. "Move backward to the previous link.
  8412. If the link is in hidden text, expose it."
  8413. (interactive)
  8414. (when (and org-link-search-failed (eq this-command last-command))
  8415. (goto-char (point-max))
  8416. (message "Link search wrapped back to end of buffer"))
  8417. (setq org-link-search-failed nil)
  8418. (let* ((pos (point))
  8419. (ct (org-context))
  8420. (a (assoc :link ct)))
  8421. (if a (goto-char (nth 1 a)))
  8422. (if (re-search-backward org-any-link-re nil t)
  8423. (progn
  8424. (goto-char (match-beginning 0))
  8425. (if (outline-invisible-p) (org-show-context)))
  8426. (goto-char pos)
  8427. (setq org-link-search-failed t)
  8428. (error "No further link found"))))
  8429. (defun org-translate-link (s)
  8430. "Translate a link string if a translation function has been defined."
  8431. (if (and org-link-translation-function
  8432. (fboundp org-link-translation-function)
  8433. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  8434. (progn
  8435. (setq s (funcall org-link-translation-function
  8436. (match-string 1) (match-string 2)))
  8437. (concat (car s) ":" (cdr s)))
  8438. s))
  8439. (defun org-translate-link-from-planner (type path)
  8440. "Translate a link from Emacs Planner syntax so that Org can follow it.
  8441. This is still an experimental function, your mileage may vary."
  8442. (cond
  8443. ((member type '("http" "https" "news" "ftp"))
  8444. ;; standard Internet links are the same.
  8445. nil)
  8446. ((and (equal type "irc") (string-match "^//" path))
  8447. ;; Planner has two / at the beginning of an irc link, we have 1.
  8448. ;; We should have zero, actually....
  8449. (setq path (substring path 1)))
  8450. ((and (equal type "lisp") (string-match "^/" path))
  8451. ;; Planner has a slash, we do not.
  8452. (setq type "elisp" path (substring path 1)))
  8453. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  8454. ;; A typical message link. Planner has the id after the final slash,
  8455. ;; we separate it with a hash mark
  8456. (setq path (concat (match-string 1 path) "#"
  8457. (org-remove-angle-brackets (match-string 2 path)))))
  8458. )
  8459. (cons type path))
  8460. (defun org-find-file-at-mouse (ev)
  8461. "Open file link or URL at mouse."
  8462. (interactive "e")
  8463. (mouse-set-point ev)
  8464. (org-open-at-point 'in-emacs))
  8465. (defun org-open-at-mouse (ev)
  8466. "Open file link or URL at mouse.
  8467. See the docstring of `org-open-file' for details."
  8468. (interactive "e")
  8469. (mouse-set-point ev)
  8470. (if (eq major-mode 'org-agenda-mode)
  8471. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  8472. (org-open-at-point))
  8473. (defvar org-window-config-before-follow-link nil
  8474. "The window configuration before following a link.
  8475. This is saved in case the need arises to restore it.")
  8476. (defvar org-open-link-marker (make-marker)
  8477. "Marker pointing to the location where `org-open-at-point; was called.")
  8478. ;;;###autoload
  8479. (defun org-open-at-point-global ()
  8480. "Follow a link like Org-mode does.
  8481. This command can be called in any mode to follow a link that has
  8482. Org-mode syntax."
  8483. (interactive)
  8484. (org-run-like-in-org-mode 'org-open-at-point))
  8485. ;;;###autoload
  8486. (defun org-open-link-from-string (s &optional arg reference-buffer)
  8487. "Open a link in the string S, as if it was in Org-mode."
  8488. (interactive "sLink: \nP")
  8489. (let ((reference-buffer (or reference-buffer (current-buffer))))
  8490. (with-temp-buffer
  8491. (let ((org-inhibit-startup t))
  8492. (org-mode)
  8493. (insert s)
  8494. (goto-char (point-min))
  8495. (when reference-buffer
  8496. (setq org-link-abbrev-alist-local
  8497. (with-current-buffer reference-buffer
  8498. org-link-abbrev-alist-local)))
  8499. (org-open-at-point arg reference-buffer)))))
  8500. (defvar org-open-at-point-functions nil
  8501. "Hook that is run when following a link at point.
  8502. Functions in this hook must return t if they identify and follow
  8503. a link at point. If they don't find anything interesting at point,
  8504. they must return nil.")
  8505. (defun org-open-at-point (&optional arg reference-buffer)
  8506. "Open link at or after point.
  8507. If there is no link at point, this function will search forward up to
  8508. the end of the current line.
  8509. Normally, files will be opened by an appropriate application. If the
  8510. optional prefix argument ARG is non-nil, Emacs will visit the file.
  8511. With a double prefix argument, try to open outside of Emacs, in the
  8512. application the system uses for this file type."
  8513. (interactive "P")
  8514. ;; if in a code block, then open the block's results
  8515. (unless (call-interactively #'org-babel-open-src-block-result)
  8516. (org-load-modules-maybe)
  8517. (move-marker org-open-link-marker (point))
  8518. (setq org-window-config-before-follow-link (current-window-configuration))
  8519. (org-remove-occur-highlights nil nil t)
  8520. (cond
  8521. ((and (org-on-heading-p)
  8522. (not (org-in-regexp
  8523. (concat org-plain-link-re "\\|"
  8524. org-bracket-link-regexp "\\|"
  8525. org-angle-link-re "\\|"
  8526. "[ \t]:[^ \t\n]+:[ \t]*$")))
  8527. (not (get-text-property (point) 'org-linked-text)))
  8528. (or (org-offer-links-in-entry arg)
  8529. (progn (require 'org-attach) (org-attach-reveal 'if-exists))))
  8530. ((run-hook-with-args-until-success 'org-open-at-point-functions))
  8531. ((org-at-timestamp-p t) (org-follow-timestamp-link))
  8532. ((and (or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  8533. (not (org-in-regexp org-bracket-link-regexp)))
  8534. (org-footnote-action))
  8535. (t
  8536. (let (type path link line search (pos (point)))
  8537. (catch 'match
  8538. (save-excursion
  8539. (skip-chars-forward "^]\n\r")
  8540. (when (org-in-regexp org-bracket-link-regexp 1)
  8541. (setq link (org-extract-attributes
  8542. (org-link-unescape (org-match-string-no-properties 1))))
  8543. (while (string-match " *\n *" link)
  8544. (setq link (replace-match " " t t link)))
  8545. (setq link (org-link-expand-abbrev link))
  8546. (cond
  8547. ((or (file-name-absolute-p link)
  8548. (string-match "^\\.\\.?/" link))
  8549. (setq type "file" path link))
  8550. ((string-match org-link-re-with-space3 link)
  8551. (setq type (match-string 1 link) path (match-string 2 link)))
  8552. (t (setq type "thisfile" path link)))
  8553. (throw 'match t)))
  8554. (when (get-text-property (point) 'org-linked-text)
  8555. (setq type "thisfile"
  8556. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  8557. (1+ (point)) (point))
  8558. path (buffer-substring
  8559. (or (previous-single-property-change pos 'org-linked-text)
  8560. (point-min))
  8561. (or (next-single-property-change pos 'org-linked-text)
  8562. (point-max))))
  8563. (throw 'match t))
  8564. (save-excursion
  8565. (when (or (org-in-regexp org-angle-link-re)
  8566. (org-in-regexp org-plain-link-re))
  8567. (setq type (match-string 1)
  8568. path (org-link-unescape (match-string 2)))
  8569. (throw 'match t)))
  8570. (save-excursion
  8571. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@#%:]+\\):[ \t]*$"))
  8572. (setq type "tags"
  8573. path (match-string 1))
  8574. (while (string-match ":" path)
  8575. (setq path (replace-match "+" t t path)))
  8576. (throw 'match t)))
  8577. (when (org-in-regexp "<\\([^><\n]+\\)>")
  8578. (setq type "tree-match"
  8579. path (match-string 1))
  8580. (throw 'match t)))
  8581. (unless path
  8582. (error "No link found"))
  8583. ;; switch back to reference buffer
  8584. ;; needed when if called in a temporary buffer through
  8585. ;; org-open-link-from-string
  8586. (with-current-buffer (or reference-buffer (current-buffer))
  8587. ;; Remove any trailing spaces in path
  8588. (if (string-match " +\\'" path)
  8589. (setq path (replace-match "" t t path)))
  8590. (if (and org-link-translation-function
  8591. (fboundp org-link-translation-function))
  8592. ;; Check if we need to translate the link
  8593. (let ((tmp (funcall org-link-translation-function type path)))
  8594. (setq type (car tmp) path (cdr tmp))))
  8595. (cond
  8596. ((assoc type org-link-protocols)
  8597. (funcall (nth 1 (assoc type org-link-protocols)) path))
  8598. ((equal type "mailto")
  8599. (let ((cmd (car org-link-mailto-program))
  8600. (args (cdr org-link-mailto-program)) args1
  8601. (address path) (subject "") a)
  8602. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  8603. (setq address (match-string 1 path)
  8604. subject (org-link-escape (match-string 2 path))))
  8605. (while args
  8606. (cond
  8607. ((not (stringp (car args))) (push (pop args) args1))
  8608. (t (setq a (pop args))
  8609. (if (string-match "%a" a)
  8610. (setq a (replace-match address t t a)))
  8611. (if (string-match "%s" a)
  8612. (setq a (replace-match subject t t a)))
  8613. (push a args1))))
  8614. (apply cmd (nreverse args1))))
  8615. ((member type '("http" "https" "ftp" "news"))
  8616. (browse-url (concat type ":" (org-link-escape
  8617. path org-link-escape-chars-browser))))
  8618. ((string= type "doi")
  8619. (browse-url (concat "http://dx.doi.org/"
  8620. (org-link-escape
  8621. path org-link-escape-chars-browser))))
  8622. ((member type '("message"))
  8623. (browse-url (concat type ":" path)))
  8624. ((string= type "tags")
  8625. (org-tags-view arg path))
  8626. ((string= type "tree-match")
  8627. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  8628. ((string= type "file")
  8629. (if (string-match "::\\([0-9]+\\)\\'" path)
  8630. (setq line (string-to-number (match-string 1 path))
  8631. path (substring path 0 (match-beginning 0)))
  8632. (if (string-match "::\\(.+\\)\\'" path)
  8633. (setq search (match-string 1 path)
  8634. path (substring path 0 (match-beginning 0)))))
  8635. (if (string-match "[*?{]" (file-name-nondirectory path))
  8636. (dired path)
  8637. (org-open-file path arg line search)))
  8638. ((string= type "shell")
  8639. (let ((cmd path))
  8640. (if (or (and (not (string= org-confirm-shell-link-not-regexp ""))
  8641. (string-match org-confirm-shell-link-not-regexp cmd))
  8642. (not org-confirm-shell-link-function)
  8643. (funcall org-confirm-shell-link-function
  8644. (format "Execute \"%s\" in shell? "
  8645. (org-add-props cmd nil
  8646. 'face 'org-warning))))
  8647. (progn
  8648. (message "Executing %s" cmd)
  8649. (shell-command cmd))
  8650. (error "Abort"))))
  8651. ((string= type "elisp")
  8652. (let ((cmd path))
  8653. (if (or (and (not (string= org-confirm-elisp-link-not-regexp ""))
  8654. (string-match org-confirm-elisp-link-not-regexp cmd))
  8655. (not org-confirm-elisp-link-function)
  8656. (funcall org-confirm-elisp-link-function
  8657. (format "Execute \"%s\" as elisp? "
  8658. (org-add-props cmd nil
  8659. 'face 'org-warning))))
  8660. (message "%s => %s" cmd
  8661. (if (equal (string-to-char cmd) ?\()
  8662. (eval (read cmd))
  8663. (call-interactively (read cmd))))
  8664. (error "Abort"))))
  8665. ((and (string= type "thisfile")
  8666. (run-hook-with-args-until-success
  8667. 'org-open-link-functions path)))
  8668. ((string= type "thisfile")
  8669. (if arg
  8670. (switch-to-buffer-other-window
  8671. (org-get-buffer-for-internal-link (current-buffer)))
  8672. (org-mark-ring-push))
  8673. (let ((cmd `(org-link-search
  8674. ,path
  8675. ,(cond ((equal arg '(4)) ''occur)
  8676. ((equal arg '(16)) ''org-occur)
  8677. (t nil))
  8678. ,pos)))
  8679. (condition-case nil (let ((org-link-search-inhibit-query t))
  8680. (eval cmd))
  8681. (error (progn (widen) (eval cmd))))))
  8682. (t
  8683. (browse-url-at-point)))))))
  8684. (move-marker org-open-link-marker nil)
  8685. (run-hook-with-args 'org-follow-link-hook)))
  8686. (defun org-offer-links-in-entry (&optional nth zero)
  8687. "Offer links in the current entry and follow the selected link.
  8688. If there is only one link, follow it immediately as well.
  8689. If NTH is an integer, immediately pick the NTH link found.
  8690. If ZERO is a string, check also this string for a link, and if
  8691. there is one, offer it as link number zero."
  8692. (let ((re (concat "\\(" org-bracket-link-regexp "\\)\\|"
  8693. "\\(" org-angle-link-re "\\)\\|"
  8694. "\\(" org-plain-link-re "\\)"))
  8695. (cnt ?0)
  8696. (in-emacs (if (integerp nth) nil nth))
  8697. have-zero end links link c)
  8698. (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
  8699. (push (match-string 0 zero) links)
  8700. (setq cnt (1- cnt) have-zero t))
  8701. (save-excursion
  8702. (org-back-to-heading t)
  8703. (setq end (save-excursion (outline-next-heading) (point)))
  8704. (while (re-search-forward re end t)
  8705. (push (match-string 0) links))
  8706. (setq links (org-uniquify (reverse links))))
  8707. (cond
  8708. ((null links)
  8709. (message "No links"))
  8710. ((equal (length links) 1)
  8711. (setq link (list (car links))))
  8712. ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
  8713. (setq link (nth (if have-zero nth (1- nth)) links)))
  8714. (t ; we have to select a link
  8715. (save-excursion
  8716. (save-window-excursion
  8717. (delete-other-windows)
  8718. (with-output-to-temp-buffer "*Select Link*"
  8719. (mapc (lambda (l)
  8720. (if (not (string-match org-bracket-link-regexp l))
  8721. (princ (format "[%c] %s\n" (incf cnt)
  8722. (org-remove-angle-brackets l)))
  8723. (if (match-end 3)
  8724. (princ (format "[%c] %s (%s)\n" (incf cnt)
  8725. (match-string 3 l) (match-string 1 l)))
  8726. (princ (format "[%c] %s\n" (incf cnt)
  8727. (match-string 1 l))))))
  8728. links))
  8729. (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
  8730. (message "Select link to open, RET to open all:")
  8731. (setq c (read-char-exclusive))
  8732. (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
  8733. (when (equal c ?q) (error "Abort"))
  8734. (if (equal c ?\C-m)
  8735. (setq link links)
  8736. (setq nth (- c ?0))
  8737. (if have-zero (setq nth (1+ nth)))
  8738. (unless (and (integerp nth) (>= (length links) nth))
  8739. (error "Invalid link selection"))
  8740. (setq link (list (nth (1- nth) links))))))
  8741. (if link
  8742. (let ((buf (current-buffer)))
  8743. (dolist (l link)
  8744. (org-open-link-from-string l in-emacs buf))
  8745. t)
  8746. nil)))
  8747. ;; Add special file links that specify the way of opening
  8748. (org-add-link-type "file+sys" 'org-open-file-with-system)
  8749. (org-add-link-type "file+emacs" 'org-open-file-with-emacs)
  8750. (defun org-open-file-with-system (path)
  8751. "Open file at PATH using the system way of opening it."
  8752. (org-open-file path 'system))
  8753. (defun org-open-file-with-emacs (path)
  8754. "Open file at PATH in Emacs."
  8755. (org-open-file path 'emacs))
  8756. (defun org-remove-file-link-modifiers ()
  8757. "Remove the file link modifiers in `file+sys:' and `file+emacs:' links."
  8758. (goto-char (point-min))
  8759. (while (re-search-forward "\\<file\\+\\(sys\\|emacs\\):" nil t)
  8760. (org-if-unprotected
  8761. (replace-match "file:" t t))))
  8762. (eval-after-load "org-exp"
  8763. '(add-hook 'org-export-preprocess-before-normalizing-links-hook
  8764. 'org-remove-file-link-modifiers))
  8765. ;;;; Time estimates
  8766. (defun org-get-effort (&optional pom)
  8767. "Get the effort estimate for the current entry."
  8768. (org-entry-get pom org-effort-property))
  8769. ;;; File search
  8770. (defvar org-create-file-search-functions nil
  8771. "List of functions to construct the right search string for a file link.
  8772. These functions are called in turn with point at the location to
  8773. which the link should point.
  8774. A function in the hook should first test if it would like to
  8775. handle this file type, for example by checking the `major-mode'
  8776. or the file extension. If it decides not to handle this file, it
  8777. should just return nil to give other functions a chance. If it
  8778. does handle the file, it must return the search string to be used
  8779. when following the link. The search string will be part of the
  8780. file link, given after a double colon, and `org-open-at-point'
  8781. will automatically search for it. If special measures must be
  8782. taken to make the search successful, another function should be
  8783. added to the companion hook `org-execute-file-search-functions',
  8784. which see.
  8785. A function in this hook may also use `setq' to set the variable
  8786. `description' to provide a suggestion for the descriptive text to
  8787. be used for this link when it gets inserted into an Org-mode
  8788. buffer with \\[org-insert-link].")
  8789. (defvar org-execute-file-search-functions nil
  8790. "List of functions to execute a file search triggered by a link.
  8791. Functions added to this hook must accept a single argument, the
  8792. search string that was part of the file link, the part after the
  8793. double colon. The function must first check if it would like to
  8794. handle this search, for example by checking the `major-mode' or
  8795. the file extension. If it decides not to handle this search, it
  8796. should just return nil to give other functions a chance. If it
  8797. does handle the search, it must return a non-nil value to keep
  8798. other functions from trying.
  8799. Each function can access the current prefix argument through the
  8800. variable `current-prefix-argument'. Note that a single prefix is
  8801. used to force opening a link in Emacs, so it may be good to only
  8802. use a numeric or double prefix to guide the search function.
  8803. In case this is needed, a function in this hook can also restore
  8804. the window configuration before `org-open-at-point' was called using:
  8805. (set-window-configuration org-window-config-before-follow-link)")
  8806. (defvar org-link-search-inhibit-query nil) ;; dynamically scoped
  8807. (defun org-link-search (s &optional type avoid-pos stealth)
  8808. "Search for a link search option.
  8809. If S is surrounded by forward slashes, it is interpreted as a
  8810. regular expression. In org-mode files, this will create an `org-occur'
  8811. sparse tree. In ordinary files, `occur' will be used to list matches.
  8812. If the current buffer is in `dired-mode', grep will be used to search
  8813. in all files. If AVOID-POS is given, ignore matches near that position.
  8814. When optional argument STEALTH is non-nil, do not modify
  8815. visibility around point, thus ignoring
  8816. `org-show-hierarchy-above', `org-show-following-heading' and
  8817. `org-show-siblings' variables."
  8818. (let ((case-fold-search t)
  8819. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  8820. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  8821. (append '(("") (" ") ("\t") ("\n"))
  8822. org-emphasis-alist)
  8823. "\\|") "\\)"))
  8824. (pos (point))
  8825. (pre nil) (post nil)
  8826. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  8827. (cond
  8828. ;; First check if there are any special search functions
  8829. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  8830. ;; Now try the builtin stuff
  8831. ((and (equal (string-to-char s0) ?#)
  8832. (> (length s0) 1)
  8833. (save-excursion
  8834. (goto-char (point-min))
  8835. (and
  8836. (re-search-forward
  8837. (concat "^[ \t]*:CUSTOM_ID:[ \t]+" (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
  8838. (setq type 'dedicated
  8839. pos (match-beginning 0))))
  8840. ;; There is an exact target for this
  8841. (goto-char pos)
  8842. (org-back-to-heading t)))
  8843. ((save-excursion
  8844. (goto-char (point-min))
  8845. (and
  8846. (re-search-forward
  8847. (concat "<<" (regexp-quote s0) ">>") nil t)
  8848. (setq type 'dedicated
  8849. pos (match-beginning 0))))
  8850. ;; There is an exact target for this
  8851. (goto-char pos))
  8852. ((and (string-match "^(\\(.*\\))$" s0)
  8853. (save-excursion
  8854. (goto-char (point-min))
  8855. (and
  8856. (re-search-forward
  8857. (concat "[^[]" (regexp-quote
  8858. (format org-coderef-label-format
  8859. (match-string 1 s0))))
  8860. nil t)
  8861. (setq type 'dedicated
  8862. pos (1+ (match-beginning 0))))))
  8863. ;; There is a coderef target for this
  8864. (goto-char pos))
  8865. ((string-match "^/\\(.*\\)/$" s)
  8866. ;; A regular expression
  8867. (cond
  8868. ((eq major-mode 'org-mode)
  8869. (org-occur (match-string 1 s)))
  8870. ;;((eq major-mode 'dired-mode)
  8871. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  8872. (t (org-do-occur (match-string 1 s)))))
  8873. ((and (eq major-mode 'org-mode) org-link-search-must-match-exact-headline)
  8874. (and (equal (string-to-char s) ?*) (setq s (substring s 1)))
  8875. (goto-char (point-min))
  8876. (cond
  8877. ((let (case-fold-search)
  8878. (re-search-forward (format org-complex-heading-regexp-format
  8879. (regexp-quote s))
  8880. nil t))
  8881. ;; OK, found a match
  8882. (setq type 'dedicated)
  8883. (goto-char (match-beginning 0)))
  8884. ((and (not org-link-search-inhibit-query)
  8885. (eq org-link-search-must-match-exact-headline 'query-to-create)
  8886. (y-or-n-p "No match - create this as a new heading? "))
  8887. (goto-char (point-max))
  8888. (or (bolp) (newline))
  8889. (insert "* " s "\n")
  8890. (beginning-of-line 0))
  8891. (t
  8892. (goto-char pos)
  8893. (error "No match"))))
  8894. (t
  8895. ;; A normal search string
  8896. (when (equal (string-to-char s) ?*)
  8897. ;; Anchor on headlines, post may include tags.
  8898. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  8899. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@#%:+]:[ \t]*\\)?$")
  8900. s (substring s 1)))
  8901. (remove-text-properties
  8902. 0 (length s)
  8903. '(face nil mouse-face nil keymap nil fontified nil) s)
  8904. ;; Make a series of regular expressions to find a match
  8905. (setq words (org-split-string s "[ \n\r\t]+")
  8906. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  8907. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  8908. "\\)" markers)
  8909. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  8910. re2a (concat "[ \t\r\n]" re2a_)
  8911. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  8912. re4 (concat "[^a-zA-Z_]" re4_)
  8913. re1 (concat pre re2 post)
  8914. re3 (concat pre (if pre re4_ re4) post)
  8915. re5 (concat pre ".*" re4)
  8916. re2 (concat pre re2)
  8917. re2a (concat pre (if pre re2a_ re2a))
  8918. re4 (concat pre (if pre re4_ re4))
  8919. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  8920. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  8921. re5 "\\)"
  8922. ))
  8923. (cond
  8924. ((eq type 'org-occur) (org-occur reall))
  8925. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  8926. (t (goto-char (point-min))
  8927. (setq type 'fuzzy)
  8928. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  8929. (org-search-not-self 1 re1 nil t)
  8930. (org-search-not-self 1 re2 nil t)
  8931. (org-search-not-self 1 re2a nil t)
  8932. (org-search-not-self 1 re3 nil t)
  8933. (org-search-not-self 1 re4 nil t)
  8934. (org-search-not-self 1 re5 nil t)
  8935. )
  8936. (goto-char (match-beginning 1))
  8937. (goto-char pos)
  8938. (error "No match"))))))
  8939. (and (eq major-mode 'org-mode)
  8940. (not stealth)
  8941. (org-show-context 'link-search))
  8942. type))
  8943. (defun org-search-not-self (group &rest args)
  8944. "Execute `re-search-forward', but only accept matches that do not
  8945. enclose the position of `org-open-link-marker'."
  8946. (let ((m org-open-link-marker))
  8947. (catch 'exit
  8948. (while (apply 're-search-forward args)
  8949. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  8950. (goto-char (match-end group))
  8951. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  8952. (> (match-beginning 0) (marker-position m))
  8953. (< (match-end 0) (marker-position m)))
  8954. (save-match-data
  8955. (or (not (org-in-regexp
  8956. org-bracket-link-analytic-regexp 1))
  8957. (not (match-end 4)) ; no description
  8958. (and (<= (match-beginning 4) (point))
  8959. (>= (match-end 4) (point))))))
  8960. (throw 'exit (point))))))))
  8961. (defun org-get-buffer-for-internal-link (buffer)
  8962. "Return a buffer to be used for displaying the link target of internal links."
  8963. (cond
  8964. ((not org-display-internal-link-with-indirect-buffer)
  8965. buffer)
  8966. ((string-match "(Clone)$" (buffer-name buffer))
  8967. (message "Buffer is already a clone, not making another one")
  8968. ;; we also do not modify visibility in this case
  8969. buffer)
  8970. (t ; make a new indirect buffer for displaying the link
  8971. (let* ((bn (buffer-name buffer))
  8972. (ibn (concat bn "(Clone)"))
  8973. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  8974. (with-current-buffer ib (org-overview))
  8975. ib))))
  8976. (defun org-do-occur (regexp &optional cleanup)
  8977. "Call the Emacs command `occur'.
  8978. If CLEANUP is non-nil, remove the printout of the regular expression
  8979. in the *Occur* buffer. This is useful if the regex is long and not useful
  8980. to read."
  8981. (occur regexp)
  8982. (when cleanup
  8983. (let ((cwin (selected-window)) win beg end)
  8984. (when (setq win (get-buffer-window "*Occur*"))
  8985. (select-window win))
  8986. (goto-char (point-min))
  8987. (when (re-search-forward "match[a-z]+" nil t)
  8988. (setq beg (match-end 0))
  8989. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  8990. (setq end (1- (match-beginning 0)))))
  8991. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  8992. (goto-char (point-min))
  8993. (select-window cwin))))
  8994. ;;; The mark ring for links jumps
  8995. (defvar org-mark-ring nil
  8996. "Mark ring for positions before jumps in Org-mode.")
  8997. (defvar org-mark-ring-last-goto nil
  8998. "Last position in the mark ring used to go back.")
  8999. ;; Fill and close the ring
  9000. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  9001. (loop for i from 1 to org-mark-ring-length do
  9002. (push (make-marker) org-mark-ring))
  9003. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  9004. org-mark-ring)
  9005. (defun org-mark-ring-push (&optional pos buffer)
  9006. "Put the current position or POS into the mark ring and rotate it."
  9007. (interactive)
  9008. (setq pos (or pos (point)))
  9009. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  9010. (move-marker (car org-mark-ring)
  9011. (or pos (point))
  9012. (or buffer (current-buffer)))
  9013. (message "%s"
  9014. (substitute-command-keys
  9015. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  9016. (defun org-mark-ring-goto (&optional n)
  9017. "Jump to the previous position in the mark ring.
  9018. With prefix arg N, jump back that many stored positions. When
  9019. called several times in succession, walk through the entire ring.
  9020. Org-mode commands jumping to a different position in the current file,
  9021. or to another Org-mode file, automatically push the old position
  9022. onto the ring."
  9023. (interactive "p")
  9024. (let (p m)
  9025. (if (eq last-command this-command)
  9026. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  9027. (setq p org-mark-ring))
  9028. (setq org-mark-ring-last-goto p)
  9029. (setq m (car p))
  9030. (org-pop-to-buffer-same-window (marker-buffer m))
  9031. (goto-char m)
  9032. (if (or (outline-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  9033. (defun org-remove-angle-brackets (s)
  9034. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  9035. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  9036. s)
  9037. (defun org-add-angle-brackets (s)
  9038. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  9039. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  9040. s)
  9041. (defun org-remove-double-quotes (s)
  9042. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  9043. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  9044. s)
  9045. ;;; Following specific links
  9046. (defun org-follow-timestamp-link ()
  9047. (cond
  9048. ((org-at-date-range-p t)
  9049. (let ((org-agenda-start-on-weekday)
  9050. (t1 (match-string 1))
  9051. (t2 (match-string 2)))
  9052. (setq t1 (time-to-days (org-time-string-to-time t1))
  9053. t2 (time-to-days (org-time-string-to-time t2)))
  9054. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  9055. ((org-at-timestamp-p t)
  9056. (org-agenda-list nil (time-to-days (org-time-string-to-time
  9057. (substring (match-string 1) 0 10)))
  9058. 1))
  9059. (t (error "This should not happen"))))
  9060. ;;; Following file links
  9061. (declare-function mailcap-parse-mailcaps "mailcap" (&optional path force))
  9062. (declare-function mailcap-extension-to-mime "mailcap" (extn))
  9063. (declare-function mailcap-mime-info
  9064. "mailcap" (string &optional request no-decode))
  9065. (defvar org-wait nil)
  9066. (defun org-open-file (path &optional in-emacs line search)
  9067. "Open the file at PATH.
  9068. First, this expands any special file name abbreviations. Then the
  9069. configuration variable `org-file-apps' is checked if it contains an
  9070. entry for this file type, and if yes, the corresponding command is launched.
  9071. If no application is found, Emacs simply visits the file.
  9072. With optional prefix argument IN-EMACS, Emacs will visit the file.
  9073. With a double \\[universal-argument] \\[universal-argument] \
  9074. prefix arg, Org tries to avoid opening in Emacs
  9075. and to use an external application to visit the file.
  9076. Optional LINE specifies a line to go to, optional SEARCH a string
  9077. to search for. If LINE or SEARCH is given, the file will be
  9078. opened in Emacs, unless an entry from org-file-apps that makes
  9079. use of groups in a regexp matches.
  9080. If you want to change the way frames are used when following a
  9081. link, please customize `org-link-frame-setup'.
  9082. If the file does not exist, an error is thrown."
  9083. (let* ((file (if (equal path "")
  9084. buffer-file-name
  9085. (substitute-in-file-name (expand-file-name path))))
  9086. (file-apps (append org-file-apps (org-default-apps)))
  9087. (apps (org-remove-if
  9088. 'org-file-apps-entry-match-against-dlink-p file-apps))
  9089. (apps-dlink (org-remove-if-not
  9090. 'org-file-apps-entry-match-against-dlink-p file-apps))
  9091. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  9092. (dirp (if remp nil (file-directory-p file)))
  9093. (file (if (and dirp org-open-directory-means-index-dot-org)
  9094. (concat (file-name-as-directory file) "index.org")
  9095. file))
  9096. (a-m-a-p (assq 'auto-mode apps))
  9097. (dfile (downcase file))
  9098. ;; reconstruct the original file: link from the PATH, LINE and SEARCH args
  9099. (link (cond ((and (eq line nil)
  9100. (eq search nil))
  9101. file)
  9102. (line
  9103. (concat file "::" (number-to-string line)))
  9104. (search
  9105. (concat file "::" search))))
  9106. (dlink (downcase link))
  9107. (old-buffer (current-buffer))
  9108. (old-pos (point))
  9109. (old-mode major-mode)
  9110. ext cmd link-match-data)
  9111. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  9112. (setq ext (match-string 1 dfile))
  9113. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  9114. (setq ext (match-string 1 dfile))))
  9115. (cond
  9116. ((member in-emacs '((16) system))
  9117. (setq cmd (cdr (assoc 'system apps))))
  9118. (in-emacs (setq cmd 'emacs))
  9119. (t
  9120. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  9121. (and dirp (cdr (assoc 'directory apps)))
  9122. ; first, try matching against apps-dlink
  9123. ; if we get a match here, store the match data for later
  9124. (let ((match (assoc-default dlink apps-dlink
  9125. 'string-match)))
  9126. (if match
  9127. (progn (setq link-match-data (match-data))
  9128. match)
  9129. (progn (setq in-emacs (or in-emacs line search))
  9130. nil))) ; if we have no match in apps-dlink,
  9131. ; always open the file in emacs if line or search
  9132. ; is given (for backwards compatibility)
  9133. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  9134. 'string-match)
  9135. (cdr (assoc ext apps))
  9136. (cdr (assoc t apps))))))
  9137. (when (eq cmd 'system)
  9138. (setq cmd (cdr (assoc 'system apps))))
  9139. (when (eq cmd 'default)
  9140. (setq cmd (cdr (assoc t apps))))
  9141. (when (eq cmd 'mailcap)
  9142. (require 'mailcap)
  9143. (mailcap-parse-mailcaps)
  9144. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  9145. (command (mailcap-mime-info mime-type)))
  9146. (if (stringp command)
  9147. (setq cmd command)
  9148. (setq cmd 'emacs))))
  9149. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  9150. (not (file-exists-p file))
  9151. (not org-open-non-existing-files))
  9152. (error "No such file: %s" file))
  9153. (cond
  9154. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  9155. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  9156. (while (string-match "['\"]%s['\"]" cmd)
  9157. (setq cmd (replace-match "%s" t t cmd)))
  9158. (while (string-match "%s" cmd)
  9159. (setq cmd (replace-match
  9160. (save-match-data
  9161. (shell-quote-argument
  9162. (convert-standard-filename file)))
  9163. t t cmd)))
  9164. ;; Replace "%1", "%2" etc. in command with group matches from regex
  9165. (save-match-data
  9166. (let ((match-index 1)
  9167. (number-of-groups (- (/ (length link-match-data) 2) 1)))
  9168. (set-match-data link-match-data)
  9169. (while (<= match-index number-of-groups)
  9170. (let ((regex (concat "%" (number-to-string match-index)))
  9171. (replace-with (match-string match-index dlink)))
  9172. (while (string-match regex cmd)
  9173. (setq cmd (replace-match replace-with t t cmd))))
  9174. (setq match-index (+ match-index 1)))))
  9175. (save-window-excursion
  9176. (start-process-shell-command cmd nil cmd)
  9177. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
  9178. ))
  9179. ((or (stringp cmd)
  9180. (eq cmd 'emacs))
  9181. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  9182. (widen)
  9183. (if line (org-goto-line line)
  9184. (if search (org-link-search search))))
  9185. ((consp cmd)
  9186. (let ((file (convert-standard-filename file)))
  9187. (save-match-data
  9188. (set-match-data link-match-data)
  9189. (eval cmd))))
  9190. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  9191. (and (eq major-mode 'org-mode) (eq old-mode 'org-mode)
  9192. (or (not (equal old-buffer (current-buffer)))
  9193. (not (equal old-pos (point))))
  9194. (org-mark-ring-push old-pos old-buffer))))
  9195. (defun org-file-apps-entry-match-against-dlink-p (entry)
  9196. "This function returns non-nil if `entry' uses a regular
  9197. expression which should be matched against the whole link by
  9198. org-open-file.
  9199. It assumes that is the case when the entry uses a regular
  9200. expression which has at least one grouping construct and the
  9201. action is either a lisp form or a command string containing
  9202. '%1', i.e. using at least one subexpression match as a
  9203. parameter."
  9204. (let ((selector (car entry))
  9205. (action (cdr entry)))
  9206. (if (stringp selector)
  9207. (and (> (regexp-opt-depth selector) 0)
  9208. (or (and (stringp action)
  9209. (string-match "%[0-9]" action))
  9210. (consp action)))
  9211. nil)))
  9212. (defun org-default-apps ()
  9213. "Return the default applications for this operating system."
  9214. (cond
  9215. ((eq system-type 'darwin)
  9216. org-file-apps-defaults-macosx)
  9217. ((eq system-type 'windows-nt)
  9218. org-file-apps-defaults-windowsnt)
  9219. (t org-file-apps-defaults-gnu)))
  9220. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  9221. "Convert extensions to regular expressions in the cars of LIST.
  9222. Also, weed out any non-string entries, because the return value is used
  9223. only for regexp matching.
  9224. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  9225. point to the symbol `emacs', indicating that the file should
  9226. be opened in Emacs."
  9227. (append
  9228. (delq nil
  9229. (mapcar (lambda (x)
  9230. (if (not (stringp (car x)))
  9231. nil
  9232. (if (string-match "\\W" (car x))
  9233. x
  9234. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  9235. list))
  9236. (if add-auto-mode
  9237. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  9238. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  9239. (defun org-file-remote-p (file)
  9240. "Test whether FILE specifies a location on a remote system.
  9241. Return non-nil if the location is indeed remote.
  9242. For example, the filename \"/user@host:/foo\" specifies a location
  9243. on the system \"/user@host:\"."
  9244. (cond ((fboundp 'file-remote-p)
  9245. (file-remote-p file))
  9246. ((fboundp 'tramp-handle-file-remote-p)
  9247. (tramp-handle-file-remote-p file))
  9248. ((and (boundp 'ange-ftp-name-format)
  9249. (string-match (car ange-ftp-name-format) file))
  9250. t)
  9251. (t nil)))
  9252. ;;;; Refiling
  9253. (defun org-get-org-file ()
  9254. "Read a filename, with default directory `org-directory'."
  9255. (let ((default (or org-default-notes-file remember-data-file)))
  9256. (read-file-name (format "File name [%s]: " default)
  9257. (file-name-as-directory org-directory)
  9258. default)))
  9259. (defun org-notes-order-reversed-p ()
  9260. "Check if the current file should receive notes in reversed order."
  9261. (cond
  9262. ((not org-reverse-note-order) nil)
  9263. ((eq t org-reverse-note-order) t)
  9264. ((not (listp org-reverse-note-order)) nil)
  9265. (t (catch 'exit
  9266. (let ((all org-reverse-note-order)
  9267. entry)
  9268. (while (setq entry (pop all))
  9269. (if (string-match (car entry) buffer-file-name)
  9270. (throw 'exit (cdr entry))))
  9271. nil)))))
  9272. (defvar org-refile-target-table nil
  9273. "The list of refile targets, created by `org-refile'.")
  9274. (defvar org-agenda-new-buffers nil
  9275. "Buffers created to visit agenda files.")
  9276. (defvar org-refile-cache nil
  9277. "Cache for refile targets.")
  9278. (defvar org-refile-markers nil
  9279. "All the markers used for caching refile locations.")
  9280. (defun org-refile-marker (pos)
  9281. "Get a new refile marker, but only if caching is in use."
  9282. (if (not org-refile-use-cache)
  9283. pos
  9284. (let ((m (make-marker)))
  9285. (move-marker m pos)
  9286. (push m org-refile-markers)
  9287. m)))
  9288. (defun org-refile-cache-clear ()
  9289. "Clear the refile cache and disable all the markers."
  9290. (mapc (lambda (m) (move-marker m nil)) org-refile-markers)
  9291. (setq org-refile-markers nil)
  9292. (setq org-refile-cache nil)
  9293. (message "Refile cache has been cleared"))
  9294. (defun org-refile-cache-check-set (set)
  9295. "Check if all the markers in the cache still have live buffers."
  9296. (let (marker)
  9297. (catch 'exit
  9298. (while (and set (setq marker (nth 3 (pop set))))
  9299. ;; if org-refile-use-outline-path is 'file, marker may be nil
  9300. (when (and marker (null (marker-buffer marker)))
  9301. (message "not found") (sit-for 3)
  9302. (throw 'exit nil)))
  9303. t)))
  9304. (defun org-refile-cache-put (set &rest identifiers)
  9305. "Push the refile targets SET into the cache, under IDENTIFIERS."
  9306. (let* ((key (sha1 (prin1-to-string identifiers)))
  9307. (entry (assoc key org-refile-cache)))
  9308. (if entry
  9309. (setcdr entry set)
  9310. (push (cons key set) org-refile-cache))))
  9311. (defun org-refile-cache-get (&rest identifiers)
  9312. "Retrieve the cached value for refile targets given by IDENTIFIERS."
  9313. (cond
  9314. ((not org-refile-cache) nil)
  9315. ((not org-refile-use-cache) (org-refile-cache-clear) nil)
  9316. (t
  9317. (let ((set (cdr (assoc (sha1 (prin1-to-string identifiers))
  9318. org-refile-cache))))
  9319. (and set (org-refile-cache-check-set set) set)))))
  9320. (defun org-refile-get-targets (&optional default-buffer excluded-entries)
  9321. "Produce a table with refile targets."
  9322. (let ((case-fold-search nil)
  9323. ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
  9324. (entries (or org-refile-targets '((nil . (:level . 1)))))
  9325. targets tgs txt re files f desc descre fast-path-p level pos0)
  9326. (message "Getting targets...")
  9327. (with-current-buffer (or default-buffer (current-buffer))
  9328. (while (setq entry (pop entries))
  9329. (setq files (car entry) desc (cdr entry))
  9330. (setq fast-path-p nil)
  9331. (cond
  9332. ((null files) (setq files (list (current-buffer))))
  9333. ((eq files 'org-agenda-files)
  9334. (setq files (org-agenda-files 'unrestricted)))
  9335. ((and (symbolp files) (fboundp files))
  9336. (setq files (funcall files)))
  9337. ((and (symbolp files) (boundp files))
  9338. (setq files (symbol-value files))))
  9339. (if (stringp files) (setq files (list files)))
  9340. (cond
  9341. ((eq (car desc) :tag)
  9342. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  9343. ((eq (car desc) :todo)
  9344. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  9345. ((eq (car desc) :regexp)
  9346. (setq descre (cdr desc)))
  9347. ((eq (car desc) :level)
  9348. (setq descre (concat "^\\*\\{" (number-to-string
  9349. (if org-odd-levels-only
  9350. (1- (* 2 (cdr desc)))
  9351. (cdr desc)))
  9352. "\\}[ \t]")))
  9353. ((eq (car desc) :maxlevel)
  9354. (setq fast-path-p t)
  9355. (setq descre (concat "^\\*\\{1," (number-to-string
  9356. (if org-odd-levels-only
  9357. (1- (* 2 (cdr desc)))
  9358. (cdr desc)))
  9359. "\\}[ \t]")))
  9360. (t (error "Bad refiling target description %s" desc)))
  9361. (while (setq f (pop files))
  9362. (with-current-buffer
  9363. (if (bufferp f) f (org-get-agenda-file-buffer f))
  9364. (or
  9365. (setq tgs (org-refile-cache-get (buffer-file-name) descre))
  9366. (progn
  9367. (if (bufferp f) (setq f (buffer-file-name
  9368. (buffer-base-buffer f))))
  9369. (setq f (and f (expand-file-name f)))
  9370. (if (eq org-refile-use-outline-path 'file)
  9371. (push (list (file-name-nondirectory f) f nil nil) tgs))
  9372. (save-excursion
  9373. (save-restriction
  9374. (widen)
  9375. (goto-char (point-min))
  9376. (while (re-search-forward descre nil t)
  9377. (goto-char (setq pos0 (point-at-bol)))
  9378. (catch 'next
  9379. (when org-refile-target-verify-function
  9380. (save-match-data
  9381. (or (funcall org-refile-target-verify-function)
  9382. (throw 'next t))))
  9383. (when (and (looking-at org-complex-heading-regexp)
  9384. (not (member (match-string 4) excluded-entries)))
  9385. (setq level (org-reduced-level
  9386. (- (match-end 1) (match-beginning 1)))
  9387. txt (org-link-display-format (match-string 4))
  9388. txt (replace-regexp-in-string "\\( *\[[0-9]+/?[0-9]*%?\]\\)+$" "" txt)
  9389. re (format org-complex-heading-regexp-format
  9390. (regexp-quote (match-string 4))))
  9391. (when org-refile-use-outline-path
  9392. (setq txt (mapconcat
  9393. 'org-protect-slash
  9394. (append
  9395. (if (eq org-refile-use-outline-path
  9396. 'file)
  9397. (list (file-name-nondirectory
  9398. (buffer-file-name
  9399. (buffer-base-buffer))))
  9400. (if (eq org-refile-use-outline-path
  9401. 'full-file-path)
  9402. (list (buffer-file-name
  9403. (buffer-base-buffer)))))
  9404. (org-get-outline-path fast-path-p
  9405. level txt)
  9406. (list txt))
  9407. "/")))
  9408. (push (list txt f re (org-refile-marker (point)))
  9409. tgs)))
  9410. (when (= (point) pos0)
  9411. ;; verification function has not moved point
  9412. (goto-char (point-at-eol))))))))
  9413. (when org-refile-use-cache
  9414. (org-refile-cache-put tgs (buffer-file-name) descre))
  9415. (setq targets (append tgs targets))
  9416. ))))
  9417. (message "Getting targets...done")
  9418. (nreverse targets)))
  9419. (defun org-protect-slash (s)
  9420. (while (string-match "/" s)
  9421. (setq s (replace-match "\\" t t s)))
  9422. s)
  9423. (defvar org-olpa (make-vector 20 nil))
  9424. (defun org-get-outline-path (&optional fastp level heading)
  9425. "Return the outline path to the current entry, as a list.
  9426. The parameters FASTP, LEVEL, and HEADING are for use by a scanner
  9427. routine which makes outline path derivations for an entire file,
  9428. avoiding backtracing. Refile target collection makes use of that."
  9429. (if fastp
  9430. (progn
  9431. (if (> level 19)
  9432. (error "Outline path failure, more than 19 levels"))
  9433. (loop for i from level upto 19 do
  9434. (aset org-olpa i nil))
  9435. (prog1
  9436. (delq nil (append org-olpa nil))
  9437. (aset org-olpa level heading)))
  9438. (let (rtn case-fold-search)
  9439. (save-excursion
  9440. (save-restriction
  9441. (widen)
  9442. (while (org-up-heading-safe)
  9443. (when (looking-at org-complex-heading-regexp)
  9444. (push (org-match-string-no-properties 4) rtn)))
  9445. rtn)))))
  9446. (defun org-format-outline-path (path &optional width prefix)
  9447. "Format the outline path PATH for display.
  9448. Width is the maximum number of characters that is available.
  9449. Prefix is a prefix to be included in the returned string,
  9450. such as the file name."
  9451. (setq width (or width 79))
  9452. (if prefix (setq width (- width (length prefix))))
  9453. (if (not path)
  9454. (or prefix "")
  9455. (let* ((nsteps (length path))
  9456. (total-width (+ nsteps (apply '+ (mapcar 'length path))))
  9457. (maxwidth (if (<= total-width width)
  9458. 10000 ;; everything fits
  9459. ;; we need to shorten the level headings
  9460. (/ (- width nsteps) nsteps)))
  9461. (org-odd-levels-only nil)
  9462. (n 0)
  9463. (total (1+ (length prefix))))
  9464. (setq maxwidth (max maxwidth 10))
  9465. (concat prefix
  9466. (mapconcat
  9467. (lambda (h)
  9468. (setq n (1+ n))
  9469. (if (and (= n nsteps) (< maxwidth 10000))
  9470. (setq maxwidth (- total-width total)))
  9471. (if (< (length h) maxwidth)
  9472. (progn (setq total (+ total (length h) 1)) h)
  9473. (setq h (substring h 0 (- maxwidth 2))
  9474. total (+ total maxwidth 1))
  9475. (if (string-match "[ \t]+\\'" h)
  9476. (setq h (substring h 0 (match-beginning 0))))
  9477. (setq h (concat h "..")))
  9478. (org-add-props h nil 'face
  9479. (nth (% (1- n) org-n-level-faces)
  9480. org-level-faces))
  9481. h)
  9482. path "/")))))
  9483. (defun org-display-outline-path (&optional file current)
  9484. "Display the current outline path in the echo area."
  9485. (interactive "P")
  9486. (let* ((bfn (buffer-file-name (buffer-base-buffer)))
  9487. (case-fold-search nil)
  9488. (path (and (eq major-mode 'org-mode) (org-get-outline-path))))
  9489. (if current (setq path (append path
  9490. (save-excursion
  9491. (org-back-to-heading t)
  9492. (if (looking-at org-complex-heading-regexp)
  9493. (list (match-string 4)))))))
  9494. (message "%s"
  9495. (org-format-outline-path
  9496. path
  9497. (1- (frame-width))
  9498. (and file bfn (concat (file-name-nondirectory bfn) "/"))))))
  9499. (defvar org-refile-history nil
  9500. "History for refiling operations.")
  9501. (defvar org-after-refile-insert-hook nil
  9502. "Hook run after `org-refile' has inserted its stuff at the new location.
  9503. Note that this is still *before* the stuff will be removed from
  9504. the *old* location.")
  9505. (defvar org-capture-last-stored-marker)
  9506. (defun org-refile (&optional goto default-buffer rfloc)
  9507. "Move the entry or entries at point to another heading.
  9508. The list of target headings is compiled using the information in
  9509. `org-refile-targets', which see.
  9510. At the target location, the entry is filed as a subitem of the target
  9511. heading. Depending on `org-reverse-note-order', the new subitem will
  9512. either be the first or the last subitem.
  9513. If there is an active region, all entries in that region will be moved.
  9514. However, the region must fulfill the requirement that the first heading
  9515. is the first one sets the top-level of the moved text - at most siblings
  9516. below it are allowed.
  9517. With prefix arg GOTO, the command will only visit the target location
  9518. and not actually move anything.
  9519. With a double prefix arg \\[universal-argument] \\[universal-argument], \
  9520. go to the location where the last refiling operation has put the subtree.
  9521. With a prefix argument of `2', refile to the running clock.
  9522. RFLOC can be a refile location obtained in a different way.
  9523. See also `org-refile-use-outline-path' and `org-completion-use-ido'.
  9524. If you are using target caching (see `org-refile-use-cache'),
  9525. You have to clear the target cache in order to find new targets.
  9526. This can be done with a 0 prefix (`C-0 C-c C-w') or a triple
  9527. prefix argument (`C-u C-u C-u C-c C-w')."
  9528. (interactive "P")
  9529. (if (member goto '(0 (64)))
  9530. (org-refile-cache-clear)
  9531. (let* ((cbuf (current-buffer))
  9532. (regionp (org-region-active-p))
  9533. (region-start (and regionp (region-beginning)))
  9534. (region-end (and regionp (region-end)))
  9535. (region-length (and regionp (- region-end region-start)))
  9536. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  9537. pos it nbuf file re level reversed)
  9538. (setq last-command nil)
  9539. (when regionp
  9540. (goto-char region-start)
  9541. (or (bolp) (goto-char (point-at-bol)))
  9542. (setq region-start (point))
  9543. (unless (or (org-kill-is-subtree-p
  9544. (buffer-substring region-start region-end))
  9545. (prog1 org-refile-active-region-within-subtree
  9546. (org-toggle-heading)))
  9547. (error "The region is not a (sequence of) subtree(s)")))
  9548. (if (equal goto '(16))
  9549. (org-refile-goto-last-stored)
  9550. (when (or
  9551. (and (equal goto 2)
  9552. org-clock-hd-marker (marker-buffer org-clock-hd-marker)
  9553. (prog1
  9554. (setq it (list (or org-clock-heading "running clock")
  9555. (buffer-file-name
  9556. (marker-buffer org-clock-hd-marker))
  9557. ""
  9558. (marker-position org-clock-hd-marker)))
  9559. (setq goto nil)))
  9560. (setq it (or rfloc
  9561. (let (heading-text)
  9562. (save-excursion
  9563. (unless goto
  9564. (org-back-to-heading t)
  9565. (setq heading-text
  9566. (nth 4 (org-heading-components))))
  9567. (org-refile-get-location
  9568. (cond (goto "Goto")
  9569. (regionp "Refile region to")
  9570. (t (concat "Refile subtree \""
  9571. heading-text "\" to")))
  9572. default-buffer
  9573. org-refile-allow-creating-parent-nodes
  9574. goto))))))
  9575. (setq file (nth 1 it)
  9576. re (nth 2 it)
  9577. pos (nth 3 it))
  9578. (if (and (not goto)
  9579. pos
  9580. (equal (buffer-file-name) file)
  9581. (if regionp
  9582. (and (>= pos region-start)
  9583. (<= pos region-end))
  9584. (and (>= pos (point))
  9585. (< pos (save-excursion
  9586. (org-end-of-subtree t t))))))
  9587. (error "Cannot refile to position inside the tree or region"))
  9588. (setq nbuf (or (find-buffer-visiting file)
  9589. (find-file-noselect file)))
  9590. (if goto
  9591. (progn
  9592. (org-pop-to-buffer-same-window nbuf)
  9593. (goto-char pos)
  9594. (org-show-context 'org-goto))
  9595. (if regionp
  9596. (progn
  9597. (org-kill-new (buffer-substring region-start region-end))
  9598. (org-save-markers-in-region region-start region-end))
  9599. (org-copy-subtree 1 nil t))
  9600. (with-current-buffer (setq nbuf (or (find-buffer-visiting file)
  9601. (find-file-noselect file)))
  9602. (setq reversed (org-notes-order-reversed-p))
  9603. (save-excursion
  9604. (save-restriction
  9605. (widen)
  9606. (if pos
  9607. (progn
  9608. (goto-char pos)
  9609. (looking-at org-outline-regexp)
  9610. (setq level (org-get-valid-level (funcall outline-level) 1))
  9611. (goto-char
  9612. (if reversed
  9613. (or (outline-next-heading) (point-max))
  9614. (or (save-excursion (org-get-next-sibling))
  9615. (org-end-of-subtree t t)
  9616. (point-max)))))
  9617. (setq level 1)
  9618. (if (not reversed)
  9619. (goto-char (point-max))
  9620. (goto-char (point-min))
  9621. (or (outline-next-heading) (goto-char (point-max)))))
  9622. (if (not (bolp)) (newline))
  9623. (org-paste-subtree level)
  9624. (when org-log-refile
  9625. (org-add-log-setup 'refile nil nil 'findpos
  9626. org-log-refile)
  9627. (unless (eq org-log-refile 'note)
  9628. (save-excursion (org-add-log-note))))
  9629. (and org-auto-align-tags (org-set-tags nil t))
  9630. (bookmark-set "org-refile-last-stored")
  9631. ;; If we are refiling for capture, make sure that the
  9632. ;; last-capture pointers point here
  9633. (when (org-bound-and-true-p org-refile-for-capture)
  9634. (bookmark-set "org-capture-last-stored-marker")
  9635. (move-marker org-capture-last-stored-marker (point)))
  9636. (if (fboundp 'deactivate-mark) (deactivate-mark))
  9637. (run-hooks 'org-after-refile-insert-hook))))
  9638. (if regionp
  9639. (delete-region (point) (+ (point) region-length))
  9640. (org-cut-subtree))
  9641. (when (featurep 'org-inlinetask)
  9642. (org-inlinetask-remove-END-maybe))
  9643. (setq org-markers-to-move nil)
  9644. (message "Refiled to \"%s\" in file %s" (car it) file)))))))
  9645. (defun org-refile-goto-last-stored ()
  9646. "Go to the location where the last refile was stored."
  9647. (interactive)
  9648. (bookmark-jump "org-refile-last-stored")
  9649. (message "This is the location of the last refile"))
  9650. (defun org-refile-get-location (&optional prompt default-buffer new-nodes
  9651. no-exclude)
  9652. "Prompt the user for a refile location, using PROMPT.
  9653. PROMPT should not be suffixed with a colon and a space, because
  9654. this function appends the default value from
  9655. `org-refile-history' automatically, if that is not empty.
  9656. When NO-EXCLUDE is set, do not exclude headlines in the current subtree,
  9657. this is used for the GOTO interface."
  9658. (let ((org-refile-targets org-refile-targets)
  9659. (org-refile-use-outline-path org-refile-use-outline-path)
  9660. excluded-entries)
  9661. (when (and (eq major-mode 'org-mode)
  9662. (not org-refile-use-cache)
  9663. (not no-exclude))
  9664. (org-map-tree
  9665. (lambda()
  9666. (setq excluded-entries
  9667. (append excluded-entries (list (org-get-heading t t)))))))
  9668. (setq org-refile-target-table
  9669. (org-refile-get-targets default-buffer excluded-entries)))
  9670. (unless org-refile-target-table
  9671. (error "No refile targets"))
  9672. (let* ((prompt (concat prompt
  9673. (and (car org-refile-history)
  9674. (concat " (default " (car org-refile-history) ")"))
  9675. ": "))
  9676. (cbuf (current-buffer))
  9677. (partial-completion-mode nil)
  9678. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  9679. (cfunc (if (and org-refile-use-outline-path
  9680. org-outline-path-complete-in-steps)
  9681. 'org-olpath-completing-read
  9682. 'org-icompleting-read))
  9683. (extra (if org-refile-use-outline-path "/" ""))
  9684. (filename (and cfn (expand-file-name cfn)))
  9685. (tbl (mapcar
  9686. (lambda (x)
  9687. (if (and (not (member org-refile-use-outline-path
  9688. '(file full-file-path)))
  9689. (not (equal filename (nth 1 x))))
  9690. (cons (concat (car x) extra " ("
  9691. (file-name-nondirectory (nth 1 x)) ")")
  9692. (cdr x))
  9693. (cons (concat (car x) extra) (cdr x))))
  9694. org-refile-target-table))
  9695. (completion-ignore-case t)
  9696. pa answ parent-target child parent old-hist)
  9697. (setq old-hist org-refile-history)
  9698. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  9699. nil 'org-refile-history (car org-refile-history)))
  9700. (setq pa (or (assoc answ tbl) (assoc (concat answ "/") tbl)))
  9701. (org-refile-check-position pa)
  9702. (if pa
  9703. (progn
  9704. (when (or (not org-refile-history)
  9705. (not (eq old-hist org-refile-history))
  9706. (not (equal (car pa) (car org-refile-history))))
  9707. (setq org-refile-history
  9708. (cons (car pa) (if (assoc (car org-refile-history) tbl)
  9709. org-refile-history
  9710. (cdr org-refile-history))))
  9711. (if (equal (car org-refile-history) (nth 1 org-refile-history))
  9712. (pop org-refile-history)))
  9713. pa)
  9714. (if (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  9715. (progn
  9716. (setq parent (match-string 1 answ)
  9717. child (match-string 2 answ))
  9718. (setq parent-target (or (assoc parent tbl)
  9719. (assoc (concat parent "/") tbl)))
  9720. (when (and parent-target
  9721. (or (eq new-nodes t)
  9722. (and (eq new-nodes 'confirm)
  9723. (y-or-n-p (format "Create new node \"%s\"? "
  9724. child)))))
  9725. (org-refile-new-child parent-target child)))
  9726. (error "Invalid target location")))))
  9727. (defun org-refile-check-position (refile-pointer)
  9728. "Check if the refile pointer matches the readline to which it points."
  9729. (let* ((file (nth 1 refile-pointer))
  9730. (re (nth 2 refile-pointer))
  9731. (pos (nth 3 refile-pointer))
  9732. buffer)
  9733. (when (org-string-nw-p re)
  9734. (setq buffer (if (markerp pos)
  9735. (marker-buffer pos)
  9736. (or (find-buffer-visiting file)
  9737. (find-file-noselect file))))
  9738. (with-current-buffer buffer
  9739. (save-excursion
  9740. (save-restriction
  9741. (widen)
  9742. (goto-char pos)
  9743. (beginning-of-line 1)
  9744. (unless (org-looking-at-p re)
  9745. (error "Invalid refile position, please clear the cache with `C-0 C-c C-w' before refiling"))))))))
  9746. (defun org-refile-new-child (parent-target child)
  9747. "Use refile target PARENT-TARGET to add new CHILD below it."
  9748. (unless parent-target
  9749. (error "Cannot find parent for new node"))
  9750. (let ((file (nth 1 parent-target))
  9751. (pos (nth 3 parent-target))
  9752. level)
  9753. (with-current-buffer (or (find-buffer-visiting file)
  9754. (find-file-noselect file))
  9755. (save-excursion
  9756. (save-restriction
  9757. (widen)
  9758. (if pos
  9759. (goto-char pos)
  9760. (goto-char (point-max))
  9761. (if (not (bolp)) (newline)))
  9762. (when (looking-at org-outline-regexp)
  9763. (setq level (funcall outline-level))
  9764. (org-end-of-subtree t t))
  9765. (org-back-over-empty-lines)
  9766. (insert "\n" (make-string
  9767. (if pos (org-get-valid-level level 1) 1) ?*)
  9768. " " child "\n")
  9769. (beginning-of-line 0)
  9770. (list (concat (car parent-target) "/" child) file "" (point)))))))
  9771. (defun org-olpath-completing-read (prompt collection &rest args)
  9772. "Read an outline path like a file name."
  9773. (let ((thetable collection)
  9774. (org-completion-use-ido nil) ; does not work with ido.
  9775. (org-completion-use-iswitchb nil)) ; or iswitchb
  9776. (apply
  9777. 'org-icompleting-read prompt
  9778. (lambda (string predicate &optional flag)
  9779. (let (rtn r f (l (length string)))
  9780. (cond
  9781. ((eq flag nil)
  9782. ;; try completion
  9783. (try-completion string thetable))
  9784. ((eq flag t)
  9785. ;; all-completions
  9786. (setq rtn (all-completions string thetable predicate))
  9787. (mapcar
  9788. (lambda (x)
  9789. (setq r (substring x l))
  9790. (if (string-match " ([^)]*)$" x)
  9791. (setq f (match-string 0 x))
  9792. (setq f ""))
  9793. (if (string-match "/" r)
  9794. (concat string (substring r 0 (match-end 0)) f)
  9795. x))
  9796. rtn))
  9797. ((eq flag 'lambda)
  9798. ;; exact match?
  9799. (assoc string thetable)))
  9800. ))
  9801. args)))
  9802. ;;;; Dynamic blocks
  9803. (defun org-find-dblock (name)
  9804. "Find the first dynamic block with name NAME in the buffer.
  9805. If not found, stay at current position and return nil."
  9806. (let (pos)
  9807. (save-excursion
  9808. (goto-char (point-min))
  9809. (setq pos (and (re-search-forward (concat "^[ \t]*#\\+BEGIN:[ \t]+" name "\\>")
  9810. nil t)
  9811. (match-beginning 0))))
  9812. (if pos (goto-char pos))
  9813. pos))
  9814. (defconst org-dblock-start-re
  9815. "^[ \t]*#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  9816. "Matches the start line of a dynamic block, with parameters.")
  9817. (defconst org-dblock-end-re "^[ \t]*#\\+END\\([: \t\r\n]\\|$\\)"
  9818. "Matches the end of a dynamic block.")
  9819. (defun org-create-dblock (plist)
  9820. "Create a dynamic block section, with parameters taken from PLIST.
  9821. PLIST must contain a :name entry which is used as name of the block."
  9822. (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
  9823. (end-of-line 1)
  9824. (newline))
  9825. (let ((col (current-column))
  9826. (name (plist-get plist :name)))
  9827. (insert "#+BEGIN: " name)
  9828. (while plist
  9829. (if (eq (car plist) :name)
  9830. (setq plist (cddr plist))
  9831. (insert " " (prin1-to-string (pop plist)))))
  9832. (insert "\n\n" (make-string col ?\ ) "#+END:\n")
  9833. (beginning-of-line -2)))
  9834. (defun org-prepare-dblock ()
  9835. "Prepare dynamic block for refresh.
  9836. This empties the block, puts the cursor at the insert position and returns
  9837. the property list including an extra property :name with the block name."
  9838. (unless (looking-at org-dblock-start-re)
  9839. (error "Not at a dynamic block"))
  9840. (let* ((begdel (1+ (match-end 0)))
  9841. (name (org-no-properties (match-string 1)))
  9842. (params (append (list :name name)
  9843. (read (concat "(" (match-string 3) ")")))))
  9844. (save-excursion
  9845. (beginning-of-line 1)
  9846. (skip-chars-forward " \t")
  9847. (setq params (plist-put params :indentation-column (current-column))))
  9848. (unless (re-search-forward org-dblock-end-re nil t)
  9849. (error "Dynamic block not terminated"))
  9850. (setq params
  9851. (append params
  9852. (list :content (buffer-substring
  9853. begdel (match-beginning 0)))))
  9854. (delete-region begdel (match-beginning 0))
  9855. (goto-char begdel)
  9856. (open-line 1)
  9857. params))
  9858. (defun org-map-dblocks (&optional command)
  9859. "Apply COMMAND to all dynamic blocks in the current buffer.
  9860. If COMMAND is not given, use `org-update-dblock'."
  9861. (let ((cmd (or command 'org-update-dblock)))
  9862. (save-excursion
  9863. (goto-char (point-min))
  9864. (while (re-search-forward org-dblock-start-re nil t)
  9865. (goto-char (match-beginning 0))
  9866. (save-excursion
  9867. (condition-case nil
  9868. (funcall cmd)
  9869. (error (message "Error during update of dynamic block"))))
  9870. (unless (re-search-forward org-dblock-end-re nil t)
  9871. (error "Dynamic block not terminated"))))))
  9872. (defun org-dblock-update (&optional arg)
  9873. "User command for updating dynamic blocks.
  9874. Update the dynamic block at point. With prefix ARG, update all dynamic
  9875. blocks in the buffer."
  9876. (interactive "P")
  9877. (if arg
  9878. (org-update-all-dblocks)
  9879. (or (looking-at org-dblock-start-re)
  9880. (org-beginning-of-dblock))
  9881. (org-update-dblock)))
  9882. (defun org-update-dblock ()
  9883. "Update the dynamic block at point.
  9884. This means to empty the block, parse for parameters and then call
  9885. the correct writing function."
  9886. (interactive)
  9887. (save-window-excursion
  9888. (let* ((pos (point))
  9889. (line (org-current-line))
  9890. (params (org-prepare-dblock))
  9891. (name (plist-get params :name))
  9892. (indent (plist-get params :indentation-column))
  9893. (cmd (intern (concat "org-dblock-write:" name))))
  9894. (message "Updating dynamic block `%s' at line %d..." name line)
  9895. (funcall cmd params)
  9896. (message "Updating dynamic block `%s' at line %d...done" name line)
  9897. (goto-char pos)
  9898. (when (and indent (> indent 0))
  9899. (setq indent (make-string indent ?\ ))
  9900. (save-excursion
  9901. (org-beginning-of-dblock)
  9902. (forward-line 1)
  9903. (while (not (looking-at org-dblock-end-re))
  9904. (insert indent)
  9905. (beginning-of-line 2))
  9906. (when (looking-at org-dblock-end-re)
  9907. (and (looking-at "[ \t]+")
  9908. (replace-match ""))
  9909. (insert indent)))))))
  9910. (defun org-beginning-of-dblock ()
  9911. "Find the beginning of the dynamic block at point.
  9912. Error if there is no such block at point."
  9913. (let ((pos (point))
  9914. beg)
  9915. (end-of-line 1)
  9916. (if (and (re-search-backward org-dblock-start-re nil t)
  9917. (setq beg (match-beginning 0))
  9918. (re-search-forward org-dblock-end-re nil t)
  9919. (> (match-end 0) pos))
  9920. (goto-char beg)
  9921. (goto-char pos)
  9922. (error "Not in a dynamic block"))))
  9923. (defun org-update-all-dblocks ()
  9924. "Update all dynamic blocks in the buffer.
  9925. This function can be used in a hook."
  9926. (interactive)
  9927. (when (eq major-mode 'org-mode)
  9928. (org-map-dblocks 'org-update-dblock)))
  9929. ;;;; Completion
  9930. (defconst org-additional-option-like-keywords
  9931. '("BEGIN_HTML" "END_HTML" "HTML:" "ATTR_HTML:"
  9932. "BEGIN_DocBook" "END_DocBook" "DocBook:" "ATTR_DocBook:"
  9933. "BEGIN_LaTeX" "END_LaTeX" "LaTeX:" "LATEX_HEADER:"
  9934. "LATEX_CLASS:" "LATEX_CLASS_OPTIONS:" "ATTR_LaTeX:"
  9935. "BEGIN:" "END:"
  9936. "ORGTBL" "TBLFM:" "TBLNAME:"
  9937. "BEGIN_EXAMPLE" "END_EXAMPLE"
  9938. "BEGIN_QUOTE" "END_QUOTE"
  9939. "BEGIN_VERSE" "END_VERSE"
  9940. "BEGIN_CENTER" "END_CENTER"
  9941. "BEGIN_SRC" "END_SRC"
  9942. "BEGIN_RESULT" "END_RESULT"
  9943. "NAME:" "RESULTS:"
  9944. "HEADER:" "HEADERS:"
  9945. "CATEGORY:" "COLUMNS:" "PROPERTY:"
  9946. "CAPTION:" "LABEL:"
  9947. "SETUPFILE:"
  9948. "INCLUDE:"
  9949. "BIND:"
  9950. "MACRO:"))
  9951. (defcustom org-structure-template-alist
  9952. '(
  9953. ("s" "#+begin_src ?\n\n#+end_src"
  9954. "<src lang=\"?\">\n\n</src>")
  9955. ("e" "#+begin_example\n?\n#+end_example"
  9956. "<example>\n?\n</example>")
  9957. ("q" "#+begin_quote\n?\n#+end_quote"
  9958. "<quote>\n?\n</quote>")
  9959. ("v" "#+begin_verse\n?\n#+end_verse"
  9960. "<verse>\n?\n/verse>")
  9961. ("c" "#+begin_center\n?\n#+end_center"
  9962. "<center>\n?\n/center>")
  9963. ("l" "#+begin_latex\n?\n#+end_latex"
  9964. "<literal style=\"latex\">\n?\n</literal>")
  9965. ("L" "#+latex: "
  9966. "<literal style=\"latex\">?</literal>")
  9967. ("h" "#+begin_html\n?\n#+end_html"
  9968. "<literal style=\"html\">\n?\n</literal>")
  9969. ("H" "#+html: "
  9970. "<literal style=\"html\">?</literal>")
  9971. ("a" "#+begin_ascii\n?\n#+end_ascii")
  9972. ("A" "#+ascii: ")
  9973. ("i" "#+index: ?"
  9974. "#+index: ?")
  9975. ("I" "#+include %file ?"
  9976. "<include file=%file markup=\"?\">")
  9977. )
  9978. "Structure completion elements.
  9979. This is a list of abbreviation keys and values. The value gets inserted
  9980. if you type `<' followed by the key and then press the completion key,
  9981. usually `M-TAB'. %file will be replaced by a file name after prompting
  9982. for the file using completion. The cursor will be placed at the position
  9983. of the `?` in the template.
  9984. There are two templates for each key, the first uses the original Org syntax,
  9985. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  9986. the default when the /org-mtags.el/ module has been loaded. See also the
  9987. variable `org-mtags-prefer-muse-templates'.
  9988. This is an experimental feature, it is undecided if it is going to stay in."
  9989. :group 'org-completion
  9990. :type '(repeat
  9991. (string :tag "Key")
  9992. (string :tag "Template")
  9993. (string :tag "Muse Template")))
  9994. (defun org-try-structure-completion ()
  9995. "Try to complete a structure template before point.
  9996. This looks for strings like \"<e\" on an otherwise empty line and
  9997. expands them."
  9998. (let ((l (buffer-substring (point-at-bol) (point)))
  9999. a)
  10000. (when (and (looking-at "[ \t]*$")
  10001. (string-match "^[ \t]*<\\([a-zA-Z]+\\)$" l)
  10002. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  10003. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  10004. (match-beginning 1)) a)
  10005. t)))
  10006. (defun org-complete-expand-structure-template (start cell)
  10007. "Expand a structure template."
  10008. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  10009. (rpl (nth (if musep 2 1) cell))
  10010. (ind ""))
  10011. (delete-region start (point))
  10012. (when (string-match "\\`#\\+" rpl)
  10013. (cond
  10014. ((bolp))
  10015. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  10016. (setq ind (buffer-substring (point-at-bol) (point))))
  10017. (t (newline))))
  10018. (setq start (point))
  10019. (if (string-match "%file" rpl)
  10020. (setq rpl (replace-match
  10021. (concat
  10022. "\""
  10023. (save-match-data
  10024. (abbreviate-file-name (read-file-name "Include file: ")))
  10025. "\"")
  10026. t t rpl)))
  10027. (setq rpl (mapconcat 'identity (split-string rpl "\n")
  10028. (concat "\n" ind)))
  10029. (insert rpl)
  10030. (if (re-search-backward "\\?" start t) (delete-char 1))))
  10031. ;;;; TODO, DEADLINE, Comments
  10032. (defun org-toggle-comment ()
  10033. "Change the COMMENT state of an entry."
  10034. (interactive)
  10035. (save-excursion
  10036. (org-back-to-heading)
  10037. (let (case-fold-search)
  10038. (cond
  10039. ((looking-at (format org-heading-keyword-regexp-format
  10040. org-comment-string))
  10041. (goto-char (match-end 1))
  10042. (looking-at (concat " +" org-comment-string))
  10043. (replace-match "" t t)
  10044. (when (eolp) (insert " ")))
  10045. ((looking-at org-outline-regexp)
  10046. (goto-char (match-end 0))
  10047. (insert org-comment-string " "))))))
  10048. (defvar org-last-todo-state-is-todo nil
  10049. "This is non-nil when the last TODO state change led to a TODO state.
  10050. If the last change removed the TODO tag or switched to DONE, then
  10051. this is nil.")
  10052. (defvar org-setting-tags nil) ; dynamically skipped
  10053. (defvar org-todo-setup-filter-hook nil
  10054. "Hook for functions that pre-filter todo specs.
  10055. Each function takes a todo spec and returns either nil or the spec
  10056. transformed into canonical form." )
  10057. (defvar org-todo-get-default-hook nil
  10058. "Hook for functions that get a default item for todo.
  10059. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  10060. nil or a string to be used for the todo mark." )
  10061. (defvar org-agenda-headline-snapshot-before-repeat)
  10062. (defun org-current-effective-time ()
  10063. "Return current time adjusted for `org-extend-today-until' variable"
  10064. (let* ((ct (org-current-time))
  10065. (dct (decode-time ct))
  10066. (ct1
  10067. (if (and org-use-effective-time
  10068. (< (nth 2 dct) org-extend-today-until))
  10069. (encode-time 0 59 23 (1- (nth 3 dct)) (nth 4 dct) (nth 5 dct))
  10070. ct)))
  10071. ct1))
  10072. (defun org-todo-yesterday (&optional arg)
  10073. "Like `org-todo' but the time of change will be 23:59 of yesterday"
  10074. (interactive "P")
  10075. (let* ((hour (third (decode-time
  10076. (org-current-time))))
  10077. (org-extend-today-until (1+ hour)))
  10078. (org-todo arg)))
  10079. (defun org-todo (&optional arg)
  10080. "Change the TODO state of an item.
  10081. The state of an item is given by a keyword at the start of the heading,
  10082. like
  10083. *** TODO Write paper
  10084. *** DONE Call mom
  10085. The different keywords are specified in the variable `org-todo-keywords'.
  10086. By default the available states are \"TODO\" and \"DONE\".
  10087. So for this example: when the item starts with TODO, it is changed to DONE.
  10088. When it starts with DONE, the DONE is removed. And when neither TODO nor
  10089. DONE are present, add TODO at the beginning of the heading.
  10090. With \\[universal-argument] prefix arg, use completion to determine the new \
  10091. state.
  10092. With numeric prefix arg, switch to that state.
  10093. With a double \\[universal-argument] prefix, switch to the next set of TODO \
  10094. keywords (nextset).
  10095. With a triple \\[universal-argument] prefix, circumvent any state blocking.
  10096. For calling through lisp, arg is also interpreted in the following way:
  10097. 'none -> empty state
  10098. \"\"(empty string) -> switch to empty state
  10099. 'done -> switch to DONE
  10100. 'nextset -> switch to the next set of keywords
  10101. 'previousset -> switch to the previous set of keywords
  10102. \"WAITING\" -> switch to the specified keyword, but only if it
  10103. really is a member of `org-todo-keywords'."
  10104. (interactive "P")
  10105. (if (equal arg '(16)) (setq arg 'nextset))
  10106. (let ((org-blocker-hook org-blocker-hook)
  10107. (case-fold-search nil))
  10108. (when (equal arg '(64))
  10109. (setq arg nil org-blocker-hook nil))
  10110. (when (and org-blocker-hook
  10111. (or org-inhibit-blocking
  10112. (org-entry-get nil "NOBLOCKING")))
  10113. (setq org-blocker-hook nil))
  10114. (save-excursion
  10115. (catch 'exit
  10116. (org-back-to-heading t)
  10117. (if (looking-at org-outline-regexp) (goto-char (1- (match-end 0))))
  10118. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|[ \t]*$\\)"))
  10119. (looking-at "\\(?: *\\|[ \t]*$\\)"))
  10120. (let* ((match-data (match-data))
  10121. (startpos (point-at-bol))
  10122. (logging (save-match-data (org-entry-get nil "LOGGING" t t)))
  10123. (org-log-done org-log-done)
  10124. (org-log-repeat org-log-repeat)
  10125. (org-todo-log-states org-todo-log-states)
  10126. (this (match-string 1))
  10127. (hl-pos (match-beginning 0))
  10128. (head (org-get-todo-sequence-head this))
  10129. (ass (assoc head org-todo-kwd-alist))
  10130. (interpret (nth 1 ass))
  10131. (done-word (nth 3 ass))
  10132. (final-done-word (nth 4 ass))
  10133. (last-state (or this ""))
  10134. (completion-ignore-case t)
  10135. (member (member this org-todo-keywords-1))
  10136. (tail (cdr member))
  10137. (state (cond
  10138. ((and org-todo-key-trigger
  10139. (or (and (equal arg '(4))
  10140. (eq org-use-fast-todo-selection 'prefix))
  10141. (and (not arg) org-use-fast-todo-selection
  10142. (not (eq org-use-fast-todo-selection
  10143. 'prefix)))))
  10144. ;; Use fast selection
  10145. (org-fast-todo-selection))
  10146. ((and (equal arg '(4))
  10147. (or (not org-use-fast-todo-selection)
  10148. (not org-todo-key-trigger)))
  10149. ;; Read a state with completion
  10150. (org-icompleting-read
  10151. "State: " (mapcar (lambda(x) (list x))
  10152. org-todo-keywords-1)
  10153. nil t))
  10154. ((eq arg 'right)
  10155. (if this
  10156. (if tail (car tail) nil)
  10157. (car org-todo-keywords-1)))
  10158. ((eq arg 'left)
  10159. (if (equal member org-todo-keywords-1)
  10160. nil
  10161. (if this
  10162. (nth (- (length org-todo-keywords-1)
  10163. (length tail) 2)
  10164. org-todo-keywords-1)
  10165. (org-last org-todo-keywords-1))))
  10166. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  10167. (setq arg nil))) ; hack to fall back to cycling
  10168. (arg
  10169. ;; user or caller requests a specific state
  10170. (cond
  10171. ((equal arg "") nil)
  10172. ((eq arg 'none) nil)
  10173. ((eq arg 'done) (or done-word (car org-done-keywords)))
  10174. ((eq arg 'nextset)
  10175. (or (car (cdr (member head org-todo-heads)))
  10176. (car org-todo-heads)))
  10177. ((eq arg 'previousset)
  10178. (let ((org-todo-heads (reverse org-todo-heads)))
  10179. (or (car (cdr (member head org-todo-heads)))
  10180. (car org-todo-heads))))
  10181. ((car (member arg org-todo-keywords-1)))
  10182. ((stringp arg)
  10183. (error "State `%s' not valid in this file" arg))
  10184. ((nth (1- (prefix-numeric-value arg))
  10185. org-todo-keywords-1))))
  10186. ((null member) (or head (car org-todo-keywords-1)))
  10187. ((equal this final-done-word) nil) ;; -> make empty
  10188. ((null tail) nil) ;; -> first entry
  10189. ((memq interpret '(type priority))
  10190. (if (eq this-command last-command)
  10191. (car tail)
  10192. (if (> (length tail) 0)
  10193. (or done-word (car org-done-keywords))
  10194. nil)))
  10195. (t
  10196. (car tail))))
  10197. (state (or
  10198. (run-hook-with-args-until-success
  10199. 'org-todo-get-default-hook state last-state)
  10200. state))
  10201. (next (if state (concat " " state " ") " "))
  10202. (change-plist (list :type 'todo-state-change :from this :to state
  10203. :position startpos))
  10204. dolog now-done-p)
  10205. (when org-blocker-hook
  10206. (setq org-last-todo-state-is-todo
  10207. (not (member this org-done-keywords)))
  10208. (unless (save-excursion
  10209. (save-match-data
  10210. (org-with-wide-buffer
  10211. (run-hook-with-args-until-failure
  10212. 'org-blocker-hook change-plist))))
  10213. (if (org-called-interactively-p 'interactive)
  10214. (error "TODO state change from %s to %s blocked" this state)
  10215. ;; fail silently
  10216. (message "TODO state change from %s to %s blocked" this state)
  10217. (throw 'exit nil))))
  10218. (store-match-data match-data)
  10219. (replace-match next t t)
  10220. (unless (pos-visible-in-window-p hl-pos)
  10221. (message "TODO state changed to %s" (org-trim next)))
  10222. (unless head
  10223. (setq head (org-get-todo-sequence-head state)
  10224. ass (assoc head org-todo-kwd-alist)
  10225. interpret (nth 1 ass)
  10226. done-word (nth 3 ass)
  10227. final-done-word (nth 4 ass)))
  10228. (when (memq arg '(nextset previousset))
  10229. (message "Keyword-Set %d/%d: %s"
  10230. (- (length org-todo-sets) -1
  10231. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  10232. (length org-todo-sets)
  10233. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  10234. (setq org-last-todo-state-is-todo
  10235. (not (member state org-done-keywords)))
  10236. (setq now-done-p (and (member state org-done-keywords)
  10237. (not (member this org-done-keywords))))
  10238. (and logging (org-local-logging logging))
  10239. (when (and (or org-todo-log-states org-log-done)
  10240. (not (eq org-inhibit-logging t))
  10241. (not (memq arg '(nextset previousset))))
  10242. ;; we need to look at recording a time and note
  10243. (setq dolog (or (nth 1 (assoc state org-todo-log-states))
  10244. (nth 2 (assoc this org-todo-log-states))))
  10245. (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
  10246. (setq dolog 'time))
  10247. (when (and state
  10248. (member state org-not-done-keywords)
  10249. (not (member this org-not-done-keywords)))
  10250. ;; This is now a todo state and was not one before
  10251. ;; If there was a CLOSED time stamp, get rid of it.
  10252. (org-add-planning-info nil nil 'closed))
  10253. (when (and now-done-p org-log-done)
  10254. ;; It is now done, and it was not done before
  10255. (org-add-planning-info 'closed (org-current-effective-time))
  10256. (if (and (not dolog) (eq 'note org-log-done))
  10257. (org-add-log-setup 'done state this 'findpos 'note)))
  10258. (when (and state dolog)
  10259. ;; This is a non-nil state, and we need to log it
  10260. (org-add-log-setup 'state state this 'findpos dolog)))
  10261. ;; Fixup tag positioning
  10262. (org-todo-trigger-tag-changes state)
  10263. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  10264. (when org-provide-todo-statistics
  10265. (org-update-parent-todo-statistics))
  10266. (run-hooks 'org-after-todo-state-change-hook)
  10267. (if (and arg (not (member state org-done-keywords)))
  10268. (setq head (org-get-todo-sequence-head state)))
  10269. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  10270. ;; Do we need to trigger a repeat?
  10271. (when now-done-p
  10272. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  10273. ;; This is for the agenda, take a snapshot of the headline.
  10274. (save-match-data
  10275. (setq org-agenda-headline-snapshot-before-repeat
  10276. (org-get-heading))))
  10277. (org-auto-repeat-maybe state))
  10278. ;; Fixup cursor location if close to the keyword
  10279. (if (and (outline-on-heading-p)
  10280. (not (bolp))
  10281. (save-excursion (beginning-of-line 1)
  10282. (looking-at org-todo-line-regexp))
  10283. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  10284. (progn
  10285. (goto-char (or (match-end 2) (match-end 1)))
  10286. (and (looking-at " ") (just-one-space))))
  10287. (when org-trigger-hook
  10288. (save-excursion
  10289. (run-hook-with-args 'org-trigger-hook change-plist))))))))
  10290. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  10291. "Block turning an entry into a TODO, using the hierarchy.
  10292. This checks whether the current task should be blocked from state
  10293. changes. Such blocking occurs when:
  10294. 1. The task has children which are not all in a completed state.
  10295. 2. A task has a parent with the property :ORDERED:, and there
  10296. are siblings prior to the current task with incomplete
  10297. status.
  10298. 3. The parent of the task is blocked because it has siblings that should
  10299. be done first, or is child of a block grandparent TODO entry."
  10300. (if (not org-enforce-todo-dependencies)
  10301. t ; if locally turned off don't block
  10302. (catch 'dont-block
  10303. ;; If this is not a todo state change, or if this entry is already DONE,
  10304. ;; do not block
  10305. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  10306. (member (plist-get change-plist :from)
  10307. (cons 'done org-done-keywords))
  10308. (member (plist-get change-plist :to)
  10309. (cons 'todo org-not-done-keywords))
  10310. (not (plist-get change-plist :to)))
  10311. (throw 'dont-block t))
  10312. ;; If this task has children, and any are undone, it's blocked
  10313. (save-excursion
  10314. (org-back-to-heading t)
  10315. (let ((this-level (funcall outline-level)))
  10316. (outline-next-heading)
  10317. (let ((child-level (funcall outline-level)))
  10318. (while (and (not (eobp))
  10319. (> child-level this-level))
  10320. ;; this todo has children, check whether they are all
  10321. ;; completed
  10322. (if (and (not (org-entry-is-done-p))
  10323. (org-entry-is-todo-p))
  10324. (throw 'dont-block nil))
  10325. (outline-next-heading)
  10326. (setq child-level (funcall outline-level))))))
  10327. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  10328. ;; any previous siblings are undone, it's blocked
  10329. (save-excursion
  10330. (org-back-to-heading t)
  10331. (let* ((pos (point))
  10332. (parent-pos (and (org-up-heading-safe) (point))))
  10333. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  10334. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  10335. (forward-line 1)
  10336. (re-search-forward org-not-done-heading-regexp pos t))
  10337. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  10338. ;; Search further up the hierarchy, to see if an anchestor is blocked
  10339. (while t
  10340. (goto-char parent-pos)
  10341. (if (not (looking-at org-not-done-heading-regexp))
  10342. (throw 'dont-block t)) ; do not block, parent is not a TODO
  10343. (setq pos (point))
  10344. (setq parent-pos (and (org-up-heading-safe) (point)))
  10345. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  10346. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  10347. (forward-line 1)
  10348. (re-search-forward org-not-done-heading-regexp pos t))
  10349. (throw 'dont-block nil)))))))) ; block, older sibling not done.
  10350. (defcustom org-track-ordered-property-with-tag nil
  10351. "Should the ORDERED property also be shown as a tag?
  10352. The ORDERED property decides if an entry should require subtasks to be
  10353. completed in sequence. Since a property is not very visible, setting
  10354. this option means that toggling the ORDERED property with the command
  10355. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  10356. not relevant for the behavior, but it makes things more visible.
  10357. Note that toggling the tag with tags commands will not change the property
  10358. and therefore not influence behavior!
  10359. This can be t, meaning the tag ORDERED should be used, It can also be a
  10360. string to select a different tag for this task."
  10361. :group 'org-todo
  10362. :type '(choice
  10363. (const :tag "No tracking" nil)
  10364. (const :tag "Track with ORDERED tag" t)
  10365. (string :tag "Use other tag")))
  10366. (defun org-toggle-ordered-property ()
  10367. "Toggle the ORDERED property of the current entry.
  10368. For better visibility, you can track the value of this property with a tag.
  10369. See variable `org-track-ordered-property-with-tag'."
  10370. (interactive)
  10371. (let* ((t1 org-track-ordered-property-with-tag)
  10372. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  10373. (save-excursion
  10374. (org-back-to-heading)
  10375. (if (org-entry-get nil "ORDERED")
  10376. (progn
  10377. (org-delete-property "ORDERED")
  10378. (and tag (org-toggle-tag tag 'off))
  10379. (message "Subtasks can be completed in arbitrary order"))
  10380. (org-entry-put nil "ORDERED" "t")
  10381. (and tag (org-toggle-tag tag 'on))
  10382. (message "Subtasks must be completed in sequence")))))
  10383. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  10384. (defun org-block-todo-from-checkboxes (change-plist)
  10385. "Block turning an entry into a TODO, using checkboxes.
  10386. This checks whether the current task should be blocked from state
  10387. changes because there are unchecked boxes in this entry."
  10388. (if (not org-enforce-todo-checkbox-dependencies)
  10389. t ; if locally turned off don't block
  10390. (catch 'dont-block
  10391. ;; If this is not a todo state change, or if this entry is already DONE,
  10392. ;; do not block
  10393. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  10394. (member (plist-get change-plist :from)
  10395. (cons 'done org-done-keywords))
  10396. (member (plist-get change-plist :to)
  10397. (cons 'todo org-not-done-keywords))
  10398. (not (plist-get change-plist :to)))
  10399. (throw 'dont-block t))
  10400. ;; If this task has checkboxes that are not checked, it's blocked
  10401. (save-excursion
  10402. (org-back-to-heading t)
  10403. (let ((beg (point)) end)
  10404. (outline-next-heading)
  10405. (setq end (point))
  10406. (goto-char beg)
  10407. (if (org-list-search-forward
  10408. (concat (org-item-beginning-re)
  10409. "\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?"
  10410. "\\[[- ]\\]")
  10411. end t)
  10412. (progn
  10413. (if (boundp 'org-blocked-by-checkboxes)
  10414. (setq org-blocked-by-checkboxes t))
  10415. (throw 'dont-block nil)))))
  10416. t))) ; do not block
  10417. (defun org-entry-blocked-p ()
  10418. "Is the current entry blocked?"
  10419. (if (org-entry-get nil "NOBLOCKING")
  10420. nil ;; Never block this entry
  10421. (not
  10422. (run-hook-with-args-until-failure
  10423. 'org-blocker-hook
  10424. (list :type 'todo-state-change
  10425. :position (point)
  10426. :from 'todo
  10427. :to 'done)))))
  10428. (defun org-update-statistics-cookies (all)
  10429. "Update the statistics cookie, either from TODO or from checkboxes.
  10430. This should be called with the cursor in a line with a statistics cookie."
  10431. (interactive "P")
  10432. (if all
  10433. (progn
  10434. (org-update-checkbox-count 'all)
  10435. (org-map-entries 'org-update-parent-todo-statistics))
  10436. (if (not (org-on-heading-p))
  10437. (org-update-checkbox-count)
  10438. (let ((pos (move-marker (make-marker) (point)))
  10439. end l1 l2)
  10440. (ignore-errors (org-back-to-heading t))
  10441. (if (not (org-on-heading-p))
  10442. (org-update-checkbox-count)
  10443. (setq l1 (org-outline-level))
  10444. (setq end (save-excursion
  10445. (outline-next-heading)
  10446. (if (org-on-heading-p) (setq l2 (org-outline-level)))
  10447. (point)))
  10448. (if (and (save-excursion
  10449. (re-search-forward
  10450. "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) \\[[- X]\\]" end t))
  10451. (not (save-excursion (re-search-forward
  10452. ":COOKIE_DATA:.*\\<todo\\>" end t))))
  10453. (org-update-checkbox-count)
  10454. (if (and l2 (> l2 l1))
  10455. (progn
  10456. (goto-char end)
  10457. (org-update-parent-todo-statistics))
  10458. (goto-char pos)
  10459. (beginning-of-line 1)
  10460. (while (re-search-forward
  10461. "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)"
  10462. (point-at-eol) t)
  10463. (replace-match (if (match-end 2) "[100%]" "[0/0]") t t)))))
  10464. (goto-char pos)
  10465. (move-marker pos nil)))))
  10466. (defvar org-entry-property-inherited-from) ;; defined below
  10467. (defun org-update-parent-todo-statistics ()
  10468. "Update any statistics cookie in the parent of the current headline.
  10469. When `org-hierarchical-todo-statistics' is nil, statistics will cover
  10470. the entire subtree and this will travel up the hierarchy and update
  10471. statistics everywhere."
  10472. (let* ((prop (save-excursion (org-up-heading-safe)
  10473. (org-entry-get nil "COOKIE_DATA" 'inherit)))
  10474. (recursive (or (not org-hierarchical-todo-statistics)
  10475. (and prop (string-match "\\<recursive\\>" prop))))
  10476. (lim (or (and prop (marker-position org-entry-property-inherited-from))
  10477. 0))
  10478. (first t)
  10479. (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  10480. level ltoggle l1 new ndel
  10481. (cnt-all 0) (cnt-done 0) is-percent kwd
  10482. checkbox-beg ov ovs ove cookie-present)
  10483. (catch 'exit
  10484. (save-excursion
  10485. (beginning-of-line 1)
  10486. (setq ltoggle (funcall outline-level))
  10487. ;; Three situations are to consider:
  10488. ;; 1. if `org-hierarchical-todo-statistics' is nil, repeat up
  10489. ;; to the top-level ancestor on the headline;
  10490. ;; 2. If parent has "recursive" property, repeat up to the
  10491. ;; headline setting that property, taking inheritance into
  10492. ;; account;
  10493. ;; 3. Else, move up to direct parent and proceed only once.
  10494. (while (and (setq level (org-up-heading-safe))
  10495. (or recursive first)
  10496. (>= (point) lim))
  10497. (setq first nil cookie-present nil)
  10498. (unless (and level
  10499. (not (string-match
  10500. "\\<checkbox\\>"
  10501. (downcase (or (org-entry-get nil "COOKIE_DATA")
  10502. "")))))
  10503. (throw 'exit nil))
  10504. (while (re-search-forward box-re (point-at-eol) t)
  10505. (setq cnt-all 0 cnt-done 0 cookie-present t)
  10506. (setq is-percent (match-end 2) checkbox-beg (match-beginning 0))
  10507. (save-match-data
  10508. (unless (outline-next-heading) (throw 'exit nil))
  10509. (while (and (looking-at org-complex-heading-regexp)
  10510. (> (setq l1 (length (match-string 1))) level))
  10511. (setq kwd (and (or recursive (= l1 ltoggle))
  10512. (match-string 2)))
  10513. (if (or (eq org-provide-todo-statistics 'all-headlines)
  10514. (and (listp org-provide-todo-statistics)
  10515. (or (member kwd org-provide-todo-statistics)
  10516. (member kwd org-done-keywords))))
  10517. (setq cnt-all (1+ cnt-all))
  10518. (if (eq org-provide-todo-statistics t)
  10519. (and kwd (setq cnt-all (1+ cnt-all)))))
  10520. (and (member kwd org-done-keywords)
  10521. (setq cnt-done (1+ cnt-done)))
  10522. (outline-next-heading)))
  10523. (setq new
  10524. (if is-percent
  10525. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  10526. (format "[%d/%d]" cnt-done cnt-all))
  10527. ndel (- (match-end 0) checkbox-beg))
  10528. ;; handle overlays when updating cookie from column view
  10529. (when (setq ov (car (overlays-at checkbox-beg)))
  10530. (setq ovs (overlay-start ov) ove (overlay-end ov))
  10531. (delete-overlay ov))
  10532. (goto-char checkbox-beg)
  10533. (insert new)
  10534. (delete-region (point) (+ (point) ndel))
  10535. (when org-auto-align-tags (org-fix-tags-on-the-fly))
  10536. (when ov (move-overlay ov ovs ove)))
  10537. (when cookie-present
  10538. (run-hook-with-args 'org-after-todo-statistics-hook
  10539. cnt-done (- cnt-all cnt-done))))))
  10540. (run-hooks 'org-todo-statistics-hook)))
  10541. (defvar org-after-todo-statistics-hook nil
  10542. "Hook that is called after a TODO statistics cookie has been updated.
  10543. Each function is called with two arguments: the number of not-done entries
  10544. and the number of done entries.
  10545. For example, the following function, when added to this hook, will switch
  10546. an entry to DONE when all children are done, and back to TODO when new
  10547. entries are set to a TODO status. Note that this hook is only called
  10548. when there is a statistics cookie in the headline!
  10549. (defun org-summary-todo (n-done n-not-done)
  10550. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  10551. (let (org-log-done org-log-states) ; turn off logging
  10552. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  10553. ")
  10554. (defvar org-todo-statistics-hook nil
  10555. "Hook that is run whenever Org thinks TODO statistics should be updated.
  10556. This hook runs even if there is no statistics cookie present, in which case
  10557. `org-after-todo-statistics-hook' would not run.")
  10558. (defun org-todo-trigger-tag-changes (state)
  10559. "Apply the changes defined in `org-todo-state-tags-triggers'."
  10560. (let ((l org-todo-state-tags-triggers)
  10561. changes)
  10562. (when (or (not state) (equal state ""))
  10563. (setq changes (append changes (cdr (assoc "" l)))))
  10564. (when (and (stringp state) (> (length state) 0))
  10565. (setq changes (append changes (cdr (assoc state l)))))
  10566. (when (member state org-not-done-keywords)
  10567. (setq changes (append changes (cdr (assoc 'todo l)))))
  10568. (when (member state org-done-keywords)
  10569. (setq changes (append changes (cdr (assoc 'done l)))))
  10570. (dolist (c changes)
  10571. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  10572. (defun org-local-logging (value)
  10573. "Get logging settings from a property VALUE."
  10574. (let* (words w a)
  10575. ;; directly set the variables, they are already local.
  10576. (setq org-log-done nil
  10577. org-log-repeat nil
  10578. org-todo-log-states nil)
  10579. (setq words (org-split-string value))
  10580. (while (setq w (pop words))
  10581. (cond
  10582. ((setq a (assoc w org-startup-options))
  10583. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  10584. (set (nth 1 a) (nth 2 a))))
  10585. ((setq a (org-extract-log-state-settings w))
  10586. (and (member (car a) org-todo-keywords-1)
  10587. (push a org-todo-log-states)))))))
  10588. (defun org-get-todo-sequence-head (kwd)
  10589. "Return the head of the TODO sequence to which KWD belongs.
  10590. If KWD is not set, check if there is a text property remembering the
  10591. right sequence."
  10592. (let (p)
  10593. (cond
  10594. ((not kwd)
  10595. (or (get-text-property (point-at-bol) 'org-todo-head)
  10596. (progn
  10597. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  10598. nil (point-at-eol)))
  10599. (get-text-property p 'org-todo-head))))
  10600. ((not (member kwd org-todo-keywords-1))
  10601. (car org-todo-keywords-1))
  10602. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  10603. (defun org-fast-todo-selection ()
  10604. "Fast TODO keyword selection with single keys.
  10605. Returns the new TODO keyword, or nil if no state change should occur."
  10606. (let* ((fulltable org-todo-key-alist)
  10607. (done-keywords org-done-keywords) ;; needed for the faces.
  10608. (maxlen (apply 'max (mapcar
  10609. (lambda (x)
  10610. (if (stringp (car x)) (string-width (car x)) 0))
  10611. fulltable)))
  10612. (expert nil)
  10613. (fwidth (+ maxlen 3 1 3))
  10614. (ncol (/ (- (window-width) 4) fwidth))
  10615. tg cnt e c tbl
  10616. groups ingroup)
  10617. (save-excursion
  10618. (save-window-excursion
  10619. (if expert
  10620. (set-buffer (get-buffer-create " *Org todo*"))
  10621. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  10622. (erase-buffer)
  10623. (org-set-local 'org-done-keywords done-keywords)
  10624. (setq tbl fulltable cnt 0)
  10625. (while (setq e (pop tbl))
  10626. (cond
  10627. ((equal e '(:startgroup))
  10628. (push '() groups) (setq ingroup t)
  10629. (when (not (= cnt 0))
  10630. (setq cnt 0)
  10631. (insert "\n"))
  10632. (insert "{ "))
  10633. ((equal e '(:endgroup))
  10634. (setq ingroup nil cnt 0)
  10635. (insert "}\n"))
  10636. ((equal e '(:newline))
  10637. (when (not (= cnt 0))
  10638. (setq cnt 0)
  10639. (insert "\n")
  10640. (setq e (car tbl))
  10641. (while (equal (car tbl) '(:newline))
  10642. (insert "\n")
  10643. (setq tbl (cdr tbl)))))
  10644. (t
  10645. (setq tg (car e) c (cdr e))
  10646. (if ingroup (push tg (car groups)))
  10647. (setq tg (org-add-props tg nil 'face
  10648. (org-get-todo-face tg)))
  10649. (if (and (= cnt 0) (not ingroup)) (insert " "))
  10650. (insert "[" c "] " tg (make-string
  10651. (- fwidth 4 (length tg)) ?\ ))
  10652. (when (= (setq cnt (1+ cnt)) ncol)
  10653. (insert "\n")
  10654. (if ingroup (insert " "))
  10655. (setq cnt 0)))))
  10656. (insert "\n")
  10657. (goto-char (point-min))
  10658. (if (not expert) (org-fit-window-to-buffer))
  10659. (message "[a-z..]:Set [SPC]:clear")
  10660. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  10661. (cond
  10662. ((or (= c ?\C-g)
  10663. (and (= c ?q) (not (rassoc c fulltable))))
  10664. (setq quit-flag t))
  10665. ((= c ?\ ) nil)
  10666. ((setq e (rassoc c fulltable) tg (car e))
  10667. tg)
  10668. (t (setq quit-flag t)))))))
  10669. (defun org-entry-is-todo-p ()
  10670. (member (org-get-todo-state) org-not-done-keywords))
  10671. (defun org-entry-is-done-p ()
  10672. (member (org-get-todo-state) org-done-keywords))
  10673. (defun org-get-todo-state ()
  10674. (save-excursion
  10675. (org-back-to-heading t)
  10676. (and (looking-at org-todo-line-regexp)
  10677. (match-end 2)
  10678. (match-string 2))))
  10679. (defun org-at-date-range-p (&optional inactive-ok)
  10680. "Is the cursor inside a date range?"
  10681. (interactive)
  10682. (save-excursion
  10683. (catch 'exit
  10684. (let ((pos (point)))
  10685. (skip-chars-backward "^[<\r\n")
  10686. (skip-chars-backward "<[")
  10687. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  10688. (>= (match-end 0) pos)
  10689. (throw 'exit t))
  10690. (skip-chars-backward "^<[\r\n")
  10691. (skip-chars-backward "<[")
  10692. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  10693. (>= (match-end 0) pos)
  10694. (throw 'exit t)))
  10695. nil)))
  10696. (defun org-get-repeat (&optional tagline)
  10697. "Check if there is a deadline/schedule with repeater in this entry."
  10698. (save-match-data
  10699. (save-excursion
  10700. (org-back-to-heading t)
  10701. (and (re-search-forward (if tagline
  10702. (concat tagline "\\s-*" org-repeat-re)
  10703. org-repeat-re)
  10704. (org-entry-end-position) t)
  10705. (match-string-no-properties 1)))))
  10706. (defvar org-last-changed-timestamp)
  10707. (defvar org-last-inserted-timestamp)
  10708. (defvar org-log-post-message)
  10709. (defvar org-log-note-purpose)
  10710. (defvar org-log-note-how)
  10711. (defvar org-log-note-extra)
  10712. (defun org-auto-repeat-maybe (done-word)
  10713. "Check if the current headline contains a repeated deadline/schedule.
  10714. If yes, set TODO state back to what it was and change the base date
  10715. of repeating deadline/scheduled time stamps to new date.
  10716. This function is run automatically after each state change to a DONE state."
  10717. ;; last-state is dynamically scoped into this function
  10718. (let* ((repeat (org-get-repeat))
  10719. (aa (assoc last-state org-todo-kwd-alist))
  10720. (interpret (nth 1 aa))
  10721. (head (nth 2 aa))
  10722. (whata '(("d" . day) ("m" . month) ("y" . year)))
  10723. (msg "Entry repeats: ")
  10724. (org-log-done nil)
  10725. (org-todo-log-states nil)
  10726. re type n what ts time to-state)
  10727. (when repeat
  10728. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  10729. (setq to-state (or (org-entry-get nil "REPEAT_TO_STATE")
  10730. org-todo-repeat-to-state))
  10731. (unless (and to-state (member to-state org-todo-keywords-1))
  10732. (setq to-state (if (eq interpret 'type) last-state head)))
  10733. (org-todo to-state)
  10734. (when (or org-log-repeat (org-entry-get nil "CLOCK"))
  10735. (org-entry-put nil "LAST_REPEAT" (format-time-string
  10736. (org-time-stamp-format t t))))
  10737. (when org-log-repeat
  10738. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  10739. (memq 'org-add-log-note post-command-hook))
  10740. ;; OK, we are already setup for some record
  10741. (if (eq org-log-repeat 'note)
  10742. ;; make sure we take a note, not only a time stamp
  10743. (setq org-log-note-how 'note))
  10744. ;; Set up for taking a record
  10745. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  10746. last-state
  10747. 'findpos org-log-repeat)))
  10748. (org-back-to-heading t)
  10749. (org-add-planning-info nil nil 'closed)
  10750. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  10751. org-deadline-time-regexp "\\)\\|\\("
  10752. org-ts-regexp "\\)"))
  10753. (while (re-search-forward
  10754. re (save-excursion (outline-next-heading) (point)) t)
  10755. (setq type (if (match-end 1) org-scheduled-string
  10756. (if (match-end 3) org-deadline-string "Plain:"))
  10757. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  10758. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
  10759. (setq n (string-to-number (match-string 2 ts))
  10760. what (match-string 3 ts))
  10761. (if (equal what "w") (setq n (* n 7) what "d"))
  10762. ;; Preparation, see if we need to modify the start date for the change
  10763. (when (match-end 1)
  10764. (setq time (save-match-data (org-time-string-to-time ts)))
  10765. (cond
  10766. ((equal (match-string 1 ts) ".")
  10767. ;; Shift starting date to today
  10768. (org-timestamp-change
  10769. (- (org-today) (time-to-days time))
  10770. 'day))
  10771. ((equal (match-string 1 ts) "+")
  10772. (let ((nshiftmax 10) (nshift 0))
  10773. (while (or (= nshift 0)
  10774. (<= (time-to-days time)
  10775. (time-to-days (current-time))))
  10776. (when (= (incf nshift) nshiftmax)
  10777. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  10778. (error "Abort")))
  10779. (org-timestamp-change n (cdr (assoc what whata)))
  10780. (org-at-timestamp-p t)
  10781. (setq ts (match-string 1))
  10782. (setq time (save-match-data (org-time-string-to-time ts)))))
  10783. (org-timestamp-change (- n) (cdr (assoc what whata)))
  10784. ;; rematch, so that we have everything in place for the real shift
  10785. (org-at-timestamp-p t)
  10786. (setq ts (match-string 1))
  10787. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
  10788. (org-timestamp-change n (cdr (assoc what whata)))
  10789. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  10790. (setq org-log-post-message msg)
  10791. (message "%s" msg))))
  10792. (defun org-show-todo-tree (arg)
  10793. "Make a compact tree which shows all headlines marked with TODO.
  10794. The tree will show the lines where the regexp matches, and all higher
  10795. headlines above the match.
  10796. With a \\[universal-argument] prefix, prompt for a regexp to match.
  10797. With a numeric prefix N, construct a sparse tree for the Nth element
  10798. of `org-todo-keywords-1'."
  10799. (interactive "P")
  10800. (let ((case-fold-search nil)
  10801. (kwd-re
  10802. (cond ((null arg) org-not-done-regexp)
  10803. ((equal arg '(4))
  10804. (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
  10805. (mapcar 'list org-todo-keywords-1))))
  10806. (concat "\\("
  10807. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  10808. "\\)\\>")))
  10809. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  10810. (regexp-quote (nth (1- (prefix-numeric-value arg))
  10811. org-todo-keywords-1)))
  10812. (t (error "Invalid prefix argument: %s" arg)))))
  10813. (message "%d TODO entries found"
  10814. (org-occur (concat "^" org-outline-regexp " *" kwd-re )))))
  10815. (defun org-deadline (&optional remove time)
  10816. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  10817. With argument REMOVE, remove any deadline from the item.
  10818. With argument TIME, set the deadline at the corresponding date. TIME
  10819. can either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  10820. (interactive "P")
  10821. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  10822. (let (org-loop-over-headlines-in-active-region)
  10823. (org-map-entries
  10824. `(org-deadline ',remove ,time) org-loop-over-headlines-in-active-region 'region (if (outline-invisible-p) (org-end-of-subtree nil t))))
  10825. (let* ((old-date (org-entry-get nil "DEADLINE"))
  10826. (repeater (and old-date
  10827. (string-match
  10828. "\\([.+-]+[0-9]+[dwmy]\\(?:[/ ][-+]?[0-9]+[dwmy]\\)?\\) ?"
  10829. old-date)
  10830. (match-string 1 old-date))))
  10831. (if remove
  10832. (progn
  10833. (when (and old-date org-log-redeadline)
  10834. (org-add-log-setup 'deldeadline nil old-date 'findpos
  10835. org-log-redeadline))
  10836. (org-remove-timestamp-with-keyword org-deadline-string)
  10837. (message "Item no longer has a deadline."))
  10838. (org-add-planning-info 'deadline time 'closed)
  10839. (when (and old-date org-log-redeadline
  10840. (not (equal old-date
  10841. (substring org-last-inserted-timestamp 1 -1))))
  10842. (org-add-log-setup 'redeadline nil old-date 'findpos
  10843. org-log-redeadline))
  10844. (when repeater
  10845. (save-excursion
  10846. (org-back-to-heading t)
  10847. (when (re-search-forward (concat org-deadline-string " "
  10848. org-last-inserted-timestamp)
  10849. (save-excursion
  10850. (outline-next-heading) (point)) t)
  10851. (goto-char (1- (match-end 0)))
  10852. (insert " " repeater)
  10853. (setq org-last-inserted-timestamp
  10854. (concat (substring org-last-inserted-timestamp 0 -1)
  10855. " " repeater
  10856. (substring org-last-inserted-timestamp -1))))))
  10857. (message "Deadline on %s" org-last-inserted-timestamp)))))
  10858. (defun org-schedule (&optional remove time)
  10859. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  10860. With argument REMOVE, remove any scheduling date from the item.
  10861. With argument TIME, scheduled at the corresponding date. TIME can
  10862. either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  10863. (interactive "P")
  10864. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  10865. (let (org-loop-over-headlines-in-active-region)
  10866. (org-map-entries
  10867. `(org-schedule ',remove ,time) org-loop-over-headlines-in-active-region 'region (if (outline-invisible-p) (org-end-of-subtree nil t))))
  10868. (let* ((old-date (org-entry-get nil "SCHEDULED"))
  10869. (repeater (and old-date
  10870. (string-match
  10871. "\\([.+-]+[0-9]+[dwmy]\\(?:[/ ][-+]?[0-9]+[dwmy]\\)?\\) ?"
  10872. old-date)
  10873. (match-string 1 old-date))))
  10874. (if remove
  10875. (progn
  10876. (when (and old-date org-log-reschedule)
  10877. (org-add-log-setup 'delschedule nil old-date 'findpos
  10878. org-log-reschedule))
  10879. (org-remove-timestamp-with-keyword org-scheduled-string)
  10880. (message "Item is no longer scheduled."))
  10881. (org-add-planning-info 'scheduled time 'closed)
  10882. (when (and old-date org-log-reschedule
  10883. (not (equal old-date
  10884. (substring org-last-inserted-timestamp 1 -1))))
  10885. (org-add-log-setup 'reschedule nil old-date 'findpos
  10886. org-log-reschedule))
  10887. (when repeater
  10888. (save-excursion
  10889. (org-back-to-heading t)
  10890. (when (re-search-forward (concat org-scheduled-string " "
  10891. org-last-inserted-timestamp)
  10892. (save-excursion
  10893. (outline-next-heading) (point)) t)
  10894. (goto-char (1- (match-end 0)))
  10895. (insert " " repeater)
  10896. (setq org-last-inserted-timestamp
  10897. (concat (substring org-last-inserted-timestamp 0 -1)
  10898. " " repeater
  10899. (substring org-last-inserted-timestamp -1))))))
  10900. (message "Scheduled to %s" org-last-inserted-timestamp)))))
  10901. (defun org-get-scheduled-time (pom &optional inherit)
  10902. "Get the scheduled time as a time tuple, of a format suitable
  10903. for calling org-schedule with, or if there is no scheduling,
  10904. returns nil."
  10905. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  10906. (when time
  10907. (apply 'encode-time (org-parse-time-string time)))))
  10908. (defun org-get-deadline-time (pom &optional inherit)
  10909. "Get the deadline as a time tuple, of a format suitable for
  10910. calling org-deadline with, or if there is no scheduling, returns
  10911. nil."
  10912. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  10913. (when time
  10914. (apply 'encode-time (org-parse-time-string time)))))
  10915. (defun org-remove-timestamp-with-keyword (keyword)
  10916. "Remove all time stamps with KEYWORD in the current entry."
  10917. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  10918. beg)
  10919. (save-excursion
  10920. (org-back-to-heading t)
  10921. (setq beg (point))
  10922. (outline-next-heading)
  10923. (while (re-search-backward re beg t)
  10924. (replace-match "")
  10925. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  10926. (equal (char-before) ?\ ))
  10927. (backward-delete-char 1)
  10928. (if (string-match "^[ \t]*$" (buffer-substring
  10929. (point-at-bol) (point-at-eol)))
  10930. (delete-region (point-at-bol)
  10931. (min (point-max) (1+ (point-at-eol))))))))))
  10932. (defun org-add-planning-info (what &optional time &rest remove)
  10933. "Insert new timestamp with keyword in the line directly after the headline.
  10934. WHAT indicates what kind of time stamp to add. TIME indicates the time to use.
  10935. If non is given, the user is prompted for a date.
  10936. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  10937. be removed."
  10938. (interactive)
  10939. (let (org-time-was-given org-end-time-was-given ts
  10940. end default-time default-input)
  10941. (catch 'exit
  10942. (when (and (memq what '(scheduled deadline))
  10943. (or (not time)
  10944. (and (stringp time)
  10945. (string-match "^[-+]+[0-9]" time))))
  10946. ;; Try to get a default date/time from existing timestamp
  10947. (save-excursion
  10948. (org-back-to-heading t)
  10949. (setq end (save-excursion (outline-next-heading) (point)))
  10950. (when (re-search-forward (if (eq what 'scheduled)
  10951. org-scheduled-time-regexp
  10952. org-deadline-time-regexp)
  10953. end t)
  10954. (setq ts (match-string 1)
  10955. default-time
  10956. (apply 'encode-time (org-parse-time-string ts))
  10957. default-input (and ts (org-get-compact-tod ts))))))
  10958. (when what
  10959. (setq time
  10960. (if (and (stringp time)
  10961. (string-match "^[-+]+[0-9]" time))
  10962. ;; This is a relative time, set the proper date
  10963. (apply 'encode-time
  10964. (org-read-date-analyze
  10965. time default-time (decode-time default-time)))
  10966. ;; If necessary, get the time from the user
  10967. (or time (org-read-date nil 'to-time nil nil
  10968. default-time default-input)))))
  10969. (when (and org-insert-labeled-timestamps-at-point
  10970. (member what '(scheduled deadline)))
  10971. (insert
  10972. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  10973. (org-insert-time-stamp time org-time-was-given
  10974. nil nil nil (list org-end-time-was-given))
  10975. (setq what nil))
  10976. (save-excursion
  10977. (save-restriction
  10978. (let (col list elt ts buffer-invisibility-spec)
  10979. (org-back-to-heading t)
  10980. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"))
  10981. (goto-char (match-end 1))
  10982. (setq col (current-column))
  10983. (goto-char (match-end 0))
  10984. (if (eobp) (insert "\n") (forward-char 1))
  10985. (when (and (not what)
  10986. (not (looking-at
  10987. (concat "[ \t]*"
  10988. org-keyword-time-not-clock-regexp))))
  10989. ;; Nothing to add, nothing to remove...... :-)
  10990. (throw 'exit nil))
  10991. (if (and (not (looking-at org-outline-regexp))
  10992. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  10993. "[^\r\n]*"))
  10994. (not (equal (match-string 1) org-clock-string)))
  10995. (narrow-to-region (match-beginning 0) (match-end 0))
  10996. (insert-before-markers "\n")
  10997. (backward-char 1)
  10998. (narrow-to-region (point) (point))
  10999. (and org-adapt-indentation (org-indent-to-column col)))
  11000. ;; Check if we have to remove something.
  11001. (setq list (cons what remove))
  11002. (while list
  11003. (setq elt (pop list))
  11004. (when (or (and (eq elt 'scheduled)
  11005. (re-search-forward org-scheduled-time-regexp nil t))
  11006. (and (eq elt 'deadline)
  11007. (re-search-forward org-deadline-time-regexp nil t))
  11008. (and (eq elt 'closed)
  11009. (re-search-forward org-closed-time-regexp nil t)))
  11010. (replace-match "")
  11011. (if (looking-at "--+<[^>]+>") (replace-match ""))))
  11012. (and (looking-at "[ \t]+") (replace-match ""))
  11013. (and org-adapt-indentation (bolp) (org-indent-to-column col))
  11014. (when what
  11015. (insert
  11016. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  11017. (cond ((eq what 'scheduled) org-scheduled-string)
  11018. ((eq what 'deadline) org-deadline-string)
  11019. ((eq what 'closed) org-closed-string))
  11020. " ")
  11021. (setq ts (org-insert-time-stamp
  11022. time
  11023. (or org-time-was-given
  11024. (and (eq what 'closed) org-log-done-with-time))
  11025. (eq what 'closed)
  11026. nil nil (list org-end-time-was-given)))
  11027. (insert
  11028. (if (not (or (bolp) (eq (char-before) ?\ )
  11029. (memq (char-after) '(32 10))
  11030. (eobp))) " " ""))
  11031. (end-of-line 1))
  11032. (goto-char (point-min))
  11033. (widen)
  11034. (if (and (looking-at "[ \t]*\n")
  11035. (equal (char-before) ?\n))
  11036. (delete-region (1- (point)) (point-at-eol)))
  11037. ts))))))
  11038. (defvar org-log-note-marker (make-marker))
  11039. (defvar org-log-note-purpose nil)
  11040. (defvar org-log-note-state nil)
  11041. (defvar org-log-note-previous-state nil)
  11042. (defvar org-log-note-how nil)
  11043. (defvar org-log-note-extra nil)
  11044. (defvar org-log-note-window-configuration nil)
  11045. (defvar org-log-note-return-to (make-marker))
  11046. (defvar org-log-note-effective-time nil
  11047. "Remembered current time so that dynamically scoped
  11048. `org-extend-today-until' affects tha timestamps in state change
  11049. log")
  11050. (defvar org-log-post-message nil
  11051. "Message to be displayed after a log note has been stored.
  11052. The auto-repeater uses this.")
  11053. (defun org-add-note ()
  11054. "Add a note to the current entry.
  11055. This is done in the same way as adding a state change note."
  11056. (interactive)
  11057. (org-add-log-setup 'note nil nil 'findpos nil))
  11058. (defvar org-property-end-re)
  11059. (defun org-add-log-setup (&optional purpose state prev-state
  11060. findpos how extra)
  11061. "Set up the post command hook to take a note.
  11062. If this is about to TODO state change, the new state is expected in STATE.
  11063. When FINDPOS is non-nil, find the correct position for the note in
  11064. the current entry. If not, assume that it can be inserted at point.
  11065. HOW is an indicator what kind of note should be created.
  11066. EXTRA is additional text that will be inserted into the notes buffer."
  11067. (let* ((org-log-into-drawer (org-log-into-drawer))
  11068. (drawer (cond ((stringp org-log-into-drawer)
  11069. org-log-into-drawer)
  11070. (org-log-into-drawer "LOGBOOK")
  11071. (t nil))))
  11072. (save-restriction
  11073. (save-excursion
  11074. (when findpos
  11075. (org-back-to-heading t)
  11076. (narrow-to-region (point) (save-excursion
  11077. (outline-next-heading) (point)))
  11078. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"
  11079. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  11080. "[^\r\n]*\\)?"))
  11081. (goto-char (match-end 0))
  11082. (cond
  11083. (drawer
  11084. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  11085. nil t)
  11086. (progn
  11087. (goto-char (match-end 0))
  11088. (or org-log-states-order-reversed
  11089. (and (re-search-forward org-property-end-re nil t)
  11090. (goto-char (1- (match-beginning 0))))))
  11091. (insert "\n:" drawer ":\n:END:")
  11092. (beginning-of-line 0)
  11093. (org-indent-line-function)
  11094. (beginning-of-line 2)
  11095. (org-indent-line-function)
  11096. (end-of-line 0)))
  11097. ((and org-log-state-notes-insert-after-drawers
  11098. (save-excursion
  11099. (forward-line) (looking-at org-drawer-regexp)))
  11100. (forward-line)
  11101. (while (looking-at org-drawer-regexp)
  11102. (goto-char (match-end 0))
  11103. (re-search-forward org-property-end-re (point-max) t)
  11104. (forward-line))
  11105. (forward-line -1)))
  11106. (unless org-log-states-order-reversed
  11107. (and (= (char-after) ?\n) (forward-char 1))
  11108. (org-skip-over-state-notes)
  11109. (skip-chars-backward " \t\n\r")))
  11110. (move-marker org-log-note-marker (point))
  11111. (setq org-log-note-purpose purpose
  11112. org-log-note-state state
  11113. org-log-note-previous-state prev-state
  11114. org-log-note-how how
  11115. org-log-note-extra extra
  11116. org-log-note-effective-time (org-current-effective-time))
  11117. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  11118. (defun org-skip-over-state-notes ()
  11119. "Skip past the list of State notes in an entry."
  11120. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  11121. (when (ignore-errors (goto-char (org-in-item-p)))
  11122. (let* ((struct (org-list-struct))
  11123. (prevs (org-list-prevs-alist struct)))
  11124. (while (looking-at "[ \t]*- State")
  11125. (goto-char (or (org-list-get-next-item (point) struct prevs)
  11126. (org-list-get-item-end (point) struct)))))))
  11127. (defun org-add-log-note (&optional purpose)
  11128. "Pop up a window for taking a note, and add this note later at point."
  11129. (remove-hook 'post-command-hook 'org-add-log-note)
  11130. (setq org-log-note-window-configuration (current-window-configuration))
  11131. (delete-other-windows)
  11132. (move-marker org-log-note-return-to (point))
  11133. (org-pop-to-buffer-same-window (marker-buffer org-log-note-marker))
  11134. (goto-char org-log-note-marker)
  11135. (org-switch-to-buffer-other-window "*Org Note*")
  11136. (erase-buffer)
  11137. (if (memq org-log-note-how '(time state))
  11138. (let (current-prefix-arg) (org-store-log-note))
  11139. (let ((org-inhibit-startup t)) (org-mode))
  11140. (insert (format "# Insert note for %s.
  11141. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  11142. (cond
  11143. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  11144. ((eq org-log-note-purpose 'done) "closed todo item")
  11145. ((eq org-log-note-purpose 'state)
  11146. (format "state change from \"%s\" to \"%s\""
  11147. (or org-log-note-previous-state "")
  11148. (or org-log-note-state "")))
  11149. ((eq org-log-note-purpose 'reschedule)
  11150. "rescheduling")
  11151. ((eq org-log-note-purpose 'delschedule)
  11152. "no longer scheduled")
  11153. ((eq org-log-note-purpose 'redeadline)
  11154. "changing deadline")
  11155. ((eq org-log-note-purpose 'deldeadline)
  11156. "removing deadline")
  11157. ((eq org-log-note-purpose 'refile)
  11158. "refiling")
  11159. ((eq org-log-note-purpose 'note)
  11160. "this entry")
  11161. (t (error "This should not happen")))))
  11162. (if org-log-note-extra (insert org-log-note-extra))
  11163. (org-set-local 'org-finish-function 'org-store-log-note)
  11164. (run-hooks 'org-log-buffer-setup-hook)))
  11165. (defvar org-note-abort nil) ; dynamically scoped
  11166. (defun org-store-log-note ()
  11167. "Finish taking a log note, and insert it to where it belongs."
  11168. (let ((txt (buffer-string))
  11169. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  11170. lines ind bul)
  11171. (kill-buffer (current-buffer))
  11172. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  11173. (setq txt (replace-match "" t t txt)))
  11174. (if (string-match "\\s-+\\'" txt)
  11175. (setq txt (replace-match "" t t txt)))
  11176. (setq lines (org-split-string txt "\n"))
  11177. (when (and note (string-match "\\S-" note))
  11178. (setq note
  11179. (org-replace-escapes
  11180. note
  11181. (list (cons "%u" (user-login-name))
  11182. (cons "%U" user-full-name)
  11183. (cons "%t" (format-time-string
  11184. (org-time-stamp-format 'long 'inactive)
  11185. org-log-note-effective-time))
  11186. (cons "%T" (format-time-string
  11187. (org-time-stamp-format 'long nil)
  11188. org-log-note-effective-time))
  11189. (cons "%d" (format-time-string
  11190. (org-time-stamp-format nil 'inactive)
  11191. org-log-note-effective-time))
  11192. (cons "%D" (format-time-string
  11193. (org-time-stamp-format nil nil)
  11194. org-log-note-effective-time))
  11195. (cons "%s" (if org-log-note-state
  11196. (concat "\"" org-log-note-state "\"")
  11197. ""))
  11198. (cons "%S" (if org-log-note-previous-state
  11199. (concat "\"" org-log-note-previous-state "\"")
  11200. "\"\"")))))
  11201. (if lines (setq note (concat note " \\\\")))
  11202. (push note lines))
  11203. (when (or current-prefix-arg org-note-abort)
  11204. (when org-log-into-drawer
  11205. (org-remove-empty-drawer-at
  11206. (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
  11207. org-log-note-marker))
  11208. (setq lines nil))
  11209. (when lines
  11210. (with-current-buffer (marker-buffer org-log-note-marker)
  11211. (save-excursion
  11212. (goto-char org-log-note-marker)
  11213. (move-marker org-log-note-marker nil)
  11214. (end-of-line 1)
  11215. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  11216. (setq ind (save-excursion
  11217. (if (ignore-errors (goto-char (org-in-item-p)))
  11218. (let ((struct (org-list-struct)))
  11219. (org-list-get-ind
  11220. (org-list-get-top-point struct) struct))
  11221. (skip-chars-backward " \r\t\n")
  11222. (cond
  11223. ((and (org-at-heading-p)
  11224. org-adapt-indentation)
  11225. (1+ (org-current-level)))
  11226. ((org-at-heading-p) 0)
  11227. (t (org-get-indentation))))))
  11228. (setq bul (org-list-bullet-string "-"))
  11229. (org-indent-line-to ind)
  11230. (insert bul (pop lines))
  11231. (let ((ind-body (+ (length bul) ind)))
  11232. (while lines
  11233. (insert "\n")
  11234. (org-indent-line-to ind-body)
  11235. (insert (pop lines))))
  11236. (message "Note stored")
  11237. (org-back-to-heading t)
  11238. (org-cycle-hide-drawers 'children)))))
  11239. (set-window-configuration org-log-note-window-configuration)
  11240. (with-current-buffer (marker-buffer org-log-note-return-to)
  11241. (goto-char org-log-note-return-to))
  11242. (move-marker org-log-note-return-to nil)
  11243. (and org-log-post-message (message "%s" org-log-post-message)))
  11244. (defun org-remove-empty-drawer-at (drawer pos)
  11245. "Remove an empty drawer DRAWER at position POS.
  11246. POS may also be a marker."
  11247. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  11248. (save-excursion
  11249. (save-restriction
  11250. (widen)
  11251. (goto-char pos)
  11252. (if (org-in-regexp
  11253. (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
  11254. (replace-match ""))))))
  11255. (defun org-sparse-tree (&optional arg)
  11256. "Create a sparse tree, prompt for the details.
  11257. This command can create sparse trees. You first need to select the type
  11258. of match used to create the tree:
  11259. t Show all TODO entries.
  11260. T Show entries with a specific TODO keyword.
  11261. m Show entries selected by a tags/property match.
  11262. p Enter a property name and its value (both with completion on existing
  11263. names/values) and show entries with that property.
  11264. r Show entries matching a regular expression (`/' can be used as well)
  11265. d Show deadlines due within `org-deadline-warning-days'.
  11266. b Show deadlines and scheduled items before a date.
  11267. a Show deadlines and scheduled items after a date."
  11268. (interactive "P")
  11269. (let (ans kwd value)
  11270. (message "Sparse tree: [r]egexp [/]regexp [t]odo [T]odo-kwd [m]atch [p]roperty\n [d]eadlines [b]efore-date [a]fter-date")
  11271. (setq ans (read-char-exclusive))
  11272. (cond
  11273. ((equal ans ?d)
  11274. (call-interactively 'org-check-deadlines))
  11275. ((equal ans ?b)
  11276. (call-interactively 'org-check-before-date))
  11277. ((equal ans ?a)
  11278. (call-interactively 'org-check-after-date))
  11279. ((equal ans ?t)
  11280. (org-show-todo-tree nil))
  11281. ((equal ans ?T)
  11282. (org-show-todo-tree '(4)))
  11283. ((member ans '(?T ?m))
  11284. (call-interactively 'org-match-sparse-tree))
  11285. ((member ans '(?p ?P))
  11286. (setq kwd (org-icompleting-read "Property: "
  11287. (mapcar 'list (org-buffer-property-keys))))
  11288. (setq value (org-icompleting-read "Value: "
  11289. (mapcar 'list (org-property-values kwd))))
  11290. (unless (string-match "\\`{.*}\\'" value)
  11291. (setq value (concat "\"" value "\"")))
  11292. (org-match-sparse-tree arg (concat kwd "=" value)))
  11293. ((member ans '(?r ?R ?/))
  11294. (call-interactively 'org-occur))
  11295. (t (error "No such sparse tree command \"%c\"" ans)))))
  11296. (defvar org-occur-highlights nil
  11297. "List of overlays used for occur matches.")
  11298. (make-variable-buffer-local 'org-occur-highlights)
  11299. (defvar org-occur-parameters nil
  11300. "Parameters of the active org-occur calls.
  11301. This is a list, each call to org-occur pushes as cons cell,
  11302. containing the regular expression and the callback, onto the list.
  11303. The list can contain several entries if `org-occur' has been called
  11304. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  11305. will only contain one set of parameters. When the highlights are
  11306. removed (for example with `C-c C-c', or with the next edit (depending
  11307. on `org-remove-highlights-with-change'), this variable is emptied
  11308. as well.")
  11309. (make-variable-buffer-local 'org-occur-parameters)
  11310. (defun org-occur (regexp &optional keep-previous callback)
  11311. "Make a compact tree which shows all matches of REGEXP.
  11312. The tree will show the lines where the regexp matches, and all higher
  11313. headlines above the match. It will also show the heading after the match,
  11314. to make sure editing the matching entry is easy.
  11315. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  11316. call to `org-occur' will be kept, to allow stacking of calls to this
  11317. command.
  11318. If CALLBACK is non-nil, it is a function which is called to confirm
  11319. that the match should indeed be shown."
  11320. (interactive "sRegexp: \nP")
  11321. (when (equal regexp "")
  11322. (error "Regexp cannot be empty"))
  11323. (unless keep-previous
  11324. (org-remove-occur-highlights nil nil t))
  11325. (push (cons regexp callback) org-occur-parameters)
  11326. (let ((cnt 0))
  11327. (save-excursion
  11328. (goto-char (point-min))
  11329. (if (or (not keep-previous) ; do not want to keep
  11330. (not org-occur-highlights)) ; no previous matches
  11331. ;; hide everything
  11332. (org-overview))
  11333. (while (re-search-forward regexp nil t)
  11334. (when (or (not callback)
  11335. (save-match-data (funcall callback)))
  11336. (setq cnt (1+ cnt))
  11337. (when org-highlight-sparse-tree-matches
  11338. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  11339. (org-show-context 'occur-tree))))
  11340. (when org-remove-highlights-with-change
  11341. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  11342. nil 'local))
  11343. (unless org-sparse-tree-open-archived-trees
  11344. (org-hide-archived-subtrees (point-min) (point-max)))
  11345. (run-hooks 'org-occur-hook)
  11346. (if (org-called-interactively-p 'interactive)
  11347. (message "%d match(es) for regexp %s" cnt regexp))
  11348. cnt))
  11349. (defun org-occur-next-match (&optional n reset)
  11350. "Function for `next-error-function' to find sparse tree matches.
  11351. N is the number of matches to move, when negative move backwards.
  11352. RESET is entirely ignored - this function always goes back to the
  11353. starting point when no match is found."
  11354. (let* ((limit (if (< n 0) (point-min) (point-max)))
  11355. (search-func (if (< n 0)
  11356. 'previous-single-char-property-change
  11357. 'next-single-char-property-change))
  11358. (n (abs n))
  11359. (pos (point))
  11360. p1)
  11361. (catch 'exit
  11362. (while (setq p1 (funcall search-func (point) 'org-type))
  11363. (when (equal p1 limit)
  11364. (goto-char pos)
  11365. (error "No more matches"))
  11366. (when (equal (get-char-property p1 'org-type) 'org-occur)
  11367. (setq n (1- n))
  11368. (when (= n 0)
  11369. (goto-char p1)
  11370. (throw 'exit (point))))
  11371. (goto-char p1))
  11372. (goto-char p1)
  11373. (error "No more matches"))))
  11374. (defun org-show-context (&optional key)
  11375. "Make sure point and context are visible.
  11376. How much context is shown depends upon the variables
  11377. `org-show-hierarchy-above', `org-show-following-heading'. and
  11378. `org-show-siblings'."
  11379. (let ((heading-p (org-on-heading-p t))
  11380. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  11381. (following-p (org-get-alist-option org-show-following-heading key))
  11382. (entry-p (org-get-alist-option org-show-entry-below key))
  11383. (siblings-p (org-get-alist-option org-show-siblings key)))
  11384. (catch 'exit
  11385. ;; Show heading or entry text
  11386. (if (and heading-p (not entry-p))
  11387. (org-flag-heading nil) ; only show the heading
  11388. (and (or entry-p (outline-invisible-p) (org-invisible-p2))
  11389. (org-show-hidden-entry))) ; show entire entry
  11390. (when following-p
  11391. ;; Show next sibling, or heading below text
  11392. (save-excursion
  11393. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  11394. (org-flag-heading nil))))
  11395. (when siblings-p (org-show-siblings))
  11396. (when hierarchy-p
  11397. ;; show all higher headings, possibly with siblings
  11398. (save-excursion
  11399. (while (and (condition-case nil
  11400. (progn (org-up-heading-all 1) t)
  11401. (error nil))
  11402. (not (bobp)))
  11403. (org-flag-heading nil)
  11404. (when siblings-p (org-show-siblings))))))))
  11405. (defvar org-reveal-start-hook nil
  11406. "Hook run before revealing a location.")
  11407. (defun org-reveal (&optional siblings)
  11408. "Show current entry, hierarchy above it, and the following headline.
  11409. This can be used to show a consistent set of context around locations
  11410. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  11411. not t for the search context.
  11412. With optional argument SIBLINGS, on each level of the hierarchy all
  11413. siblings are shown. This repairs the tree structure to what it would
  11414. look like when opened with hierarchical calls to `org-cycle'.
  11415. With double optional argument \\[universal-argument] \\[universal-argument], \
  11416. go to the parent and show the
  11417. entire tree."
  11418. (interactive "P")
  11419. (run-hooks 'org-reveal-start-hook)
  11420. (let ((org-show-hierarchy-above t)
  11421. (org-show-following-heading t)
  11422. (org-show-siblings (if siblings t org-show-siblings)))
  11423. (org-show-context nil))
  11424. (when (equal siblings '(16))
  11425. (save-excursion
  11426. (when (org-up-heading-safe)
  11427. (org-show-subtree)
  11428. (run-hook-with-args 'org-cycle-hook 'subtree)))))
  11429. (defun org-highlight-new-match (beg end)
  11430. "Highlight from BEG to END and mark the highlight is an occur headline."
  11431. (let ((ov (make-overlay beg end)))
  11432. (overlay-put ov 'face 'secondary-selection)
  11433. (overlay-put ov 'org-type 'org-occur)
  11434. (push ov org-occur-highlights)))
  11435. (defun org-remove-occur-highlights (&optional beg end noremove)
  11436. "Remove the occur highlights from the buffer.
  11437. BEG and END are ignored. If NOREMOVE is nil, remove this function
  11438. from the `before-change-functions' in the current buffer."
  11439. (interactive)
  11440. (unless org-inhibit-highlight-removal
  11441. (mapc 'delete-overlay org-occur-highlights)
  11442. (setq org-occur-highlights nil)
  11443. (setq org-occur-parameters nil)
  11444. (unless noremove
  11445. (remove-hook 'before-change-functions
  11446. 'org-remove-occur-highlights 'local))))
  11447. ;;;; Priorities
  11448. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  11449. "Regular expression matching the priority indicator.")
  11450. (defvar org-remove-priority-next-time nil)
  11451. (defun org-priority-up ()
  11452. "Increase the priority of the current item."
  11453. (interactive)
  11454. (org-priority 'up))
  11455. (defun org-priority-down ()
  11456. "Decrease the priority of the current item."
  11457. (interactive)
  11458. (org-priority 'down))
  11459. (defun org-priority (&optional action)
  11460. "Change the priority of an item by ARG.
  11461. ACTION can be `set', `up', `down', or a character."
  11462. (interactive)
  11463. (unless org-enable-priority-commands
  11464. (error "Priority commands are disabled"))
  11465. (setq action (or action 'set))
  11466. (let (current new news have remove)
  11467. (save-excursion
  11468. (org-back-to-heading t)
  11469. (if (looking-at org-priority-regexp)
  11470. (setq current (string-to-char (match-string 2))
  11471. have t))
  11472. (cond
  11473. ((eq action 'remove)
  11474. (setq remove t new ?\ ))
  11475. ((or (eq action 'set)
  11476. (if (featurep 'xemacs) (characterp action) (integerp action)))
  11477. (if (not (eq action 'set))
  11478. (setq new action)
  11479. (message "Priority %c-%c, SPC to remove: "
  11480. org-highest-priority org-lowest-priority)
  11481. (save-match-data
  11482. (setq new (read-char-exclusive))))
  11483. (if (and (= (upcase org-highest-priority) org-highest-priority)
  11484. (= (upcase org-lowest-priority) org-lowest-priority))
  11485. (setq new (upcase new)))
  11486. (cond ((equal new ?\ ) (setq remove t))
  11487. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  11488. (error "Priority must be between `%c' and `%c'"
  11489. org-highest-priority org-lowest-priority))))
  11490. ((eq action 'up)
  11491. (setq new (if have
  11492. (1- current) ; normal cycling
  11493. ;; last priority was empty
  11494. (if (eq last-command this-command)
  11495. org-lowest-priority ; wrap around empty to lowest
  11496. ;; default
  11497. (if org-priority-start-cycle-with-default
  11498. org-default-priority
  11499. (1- org-default-priority))))))
  11500. ((eq action 'down)
  11501. (setq new (if have
  11502. (1+ current) ; normal cycling
  11503. ;; last priority was empty
  11504. (if (eq last-command this-command)
  11505. org-highest-priority ; wrap around empty to highest
  11506. ;; default
  11507. (if org-priority-start-cycle-with-default
  11508. org-default-priority
  11509. (1+ org-default-priority))))))
  11510. (t (error "Invalid action")))
  11511. (if (or (< (upcase new) org-highest-priority)
  11512. (> (upcase new) org-lowest-priority))
  11513. (if (and (memq action '(up down))
  11514. (not have) (not (eq last-command this-command)))
  11515. ;; `new' is from default priority
  11516. (error
  11517. "The default can not be set, see `org-default-priority' why")
  11518. ;; normal cycling: `new' is beyond highest/lowest priority
  11519. ;; and is wrapped around to the empty priority
  11520. (setq remove t)))
  11521. (setq news (format "%c" new))
  11522. (if have
  11523. (if remove
  11524. (replace-match "" t t nil 1)
  11525. (replace-match news t t nil 2))
  11526. (if remove
  11527. (error "No priority cookie found in line")
  11528. (let ((case-fold-search nil))
  11529. (looking-at org-todo-line-regexp))
  11530. (if (match-end 2)
  11531. (progn
  11532. (goto-char (match-end 2))
  11533. (insert " [#" news "]"))
  11534. (goto-char (match-beginning 3))
  11535. (insert "[#" news "] "))))
  11536. (org-preserve-lc (org-set-tags nil 'align)))
  11537. (if remove
  11538. (message "Priority removed")
  11539. (message "Priority of current item set to %s" news))))
  11540. (defun org-get-priority (s)
  11541. "Find priority cookie and return priority."
  11542. (if (functionp org-get-priority-function)
  11543. (funcall org-get-priority-function)
  11544. (save-match-data
  11545. (if (not (string-match org-priority-regexp s))
  11546. (* 1000 (- org-lowest-priority org-default-priority))
  11547. (* 1000 (- org-lowest-priority
  11548. (string-to-char (match-string 2 s))))))))
  11549. ;;;; Tags
  11550. (defvar org-agenda-archives-mode)
  11551. (defvar org-map-continue-from nil
  11552. "Position from where mapping should continue.
  11553. Can be set by the action argument to `org-scan-tag's and `org-map-entries'.")
  11554. (defvar org-scanner-tags nil
  11555. "The current tag list while the tags scanner is running.")
  11556. (defvar org-trust-scanner-tags nil
  11557. "Should `org-get-tags-at' use the tags for the scanner.
  11558. This is for internal dynamical scoping only.
  11559. When this is non-nil, the function `org-get-tags-at' will return the value
  11560. of `org-scanner-tags' instead of building the list by itself. This
  11561. can lead to large speed-ups when the tags scanner is used in a file with
  11562. many entries, and when the list of tags is retrieved, for example to
  11563. obtain a list of properties. Building the tags list for each entry in such
  11564. a file becomes an N^2 operation - but with this variable set, it scales
  11565. as N.")
  11566. (defun org-scan-tags (action matcher &optional todo-only)
  11567. "Scan headline tags with inheritance and produce output ACTION.
  11568. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  11569. or `agenda' to produce an entry list for an agenda view. It can also be
  11570. a Lisp form or a function that should be called at each matched headline, in
  11571. this case the return value is a list of all return values from these calls.
  11572. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  11573. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  11574. only lines with a TODO keyword are included in the output."
  11575. (require 'org-agenda)
  11576. (let* ((re (concat "^" org-outline-regexp " *\\(\\<\\("
  11577. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  11578. (org-re
  11579. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*$")))
  11580. (props (list 'face 'default
  11581. 'done-face 'org-agenda-done
  11582. 'undone-face 'default
  11583. 'mouse-face 'highlight
  11584. 'org-not-done-regexp org-not-done-regexp
  11585. 'org-todo-regexp org-todo-regexp
  11586. 'org-complex-heading-regexp org-complex-heading-regexp
  11587. 'help-echo
  11588. (format "mouse-2 or RET jump to org file %s"
  11589. (abbreviate-file-name
  11590. (or (buffer-file-name (buffer-base-buffer))
  11591. (buffer-name (buffer-base-buffer)))))))
  11592. (case-fold-search nil)
  11593. (org-map-continue-from nil)
  11594. lspos tags tags-list
  11595. (tags-alist (list (cons 0 org-file-tags)))
  11596. (llast 0) rtn rtn1 level category i txt
  11597. todo marker entry priority)
  11598. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  11599. (setq action (list 'lambda nil action)))
  11600. (save-excursion
  11601. (goto-char (point-min))
  11602. (when (eq action 'sparse-tree)
  11603. (org-overview)
  11604. (org-remove-occur-highlights))
  11605. (while (re-search-forward re nil t)
  11606. (catch :skip
  11607. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  11608. tags (if (match-end 4) (org-match-string-no-properties 4)))
  11609. (goto-char (setq lspos (match-beginning 0)))
  11610. (setq level (org-reduced-level (funcall outline-level))
  11611. category (org-get-category))
  11612. (setq i llast llast level)
  11613. ;; remove tag lists from same and sublevels
  11614. (while (>= i level)
  11615. (when (setq entry (assoc i tags-alist))
  11616. (setq tags-alist (delete entry tags-alist)))
  11617. (setq i (1- i)))
  11618. ;; add the next tags
  11619. (when tags
  11620. (setq tags (org-split-string tags ":")
  11621. tags-alist
  11622. (cons (cons level tags) tags-alist)))
  11623. ;; compile tags for current headline
  11624. (setq tags-list
  11625. (if org-use-tag-inheritance
  11626. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  11627. tags)
  11628. org-scanner-tags tags-list)
  11629. (when org-use-tag-inheritance
  11630. (setcdr (car tags-alist)
  11631. (mapcar (lambda (x)
  11632. (setq x (copy-sequence x))
  11633. (org-add-prop-inherited x))
  11634. (cdar tags-alist))))
  11635. (when (and tags org-use-tag-inheritance
  11636. (or (not (eq t org-use-tag-inheritance))
  11637. org-tags-exclude-from-inheritance))
  11638. ;; selective inheritance, remove uninherited ones
  11639. (setcdr (car tags-alist)
  11640. (org-remove-uninherited-tags (cdar tags-alist))))
  11641. (when (and
  11642. ;; eval matcher only when the todo condition is OK
  11643. (and (or (not todo-only) (member todo org-not-done-keywords))
  11644. (let ((case-fold-search t)) (eval matcher)))
  11645. ;; Call the skipper, but return t if it does not skip,
  11646. ;; so that the `and' form continues evaluating
  11647. (progn
  11648. (unless (eq action 'sparse-tree) (org-agenda-skip))
  11649. t)
  11650. ;; Check if timestamps are deselecting this entry
  11651. (or (not todo-only)
  11652. (and (member todo org-not-done-keywords)
  11653. (or (not org-agenda-tags-todo-honor-ignore-options)
  11654. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))))
  11655. ;; Extra check for the archive tag
  11656. ;; FIXME: Does the skipper already do this????
  11657. (or
  11658. (not (member org-archive-tag tags-list))
  11659. ;; we have an archive tag, should we use this anyway?
  11660. (or (not org-agenda-skip-archived-trees)
  11661. (and (eq action 'agenda) org-agenda-archives-mode))))
  11662. ;; select this headline
  11663. (cond
  11664. ((eq action 'sparse-tree)
  11665. (and org-highlight-sparse-tree-matches
  11666. (org-get-heading) (match-end 0)
  11667. (org-highlight-new-match
  11668. (match-beginning 0) (match-beginning 1)))
  11669. (org-show-context 'tags-tree))
  11670. ((eq action 'agenda)
  11671. (setq txt (org-agenda-format-item
  11672. ""
  11673. (concat
  11674. (if (eq org-tags-match-list-sublevels 'indented)
  11675. (make-string (1- level) ?.) "")
  11676. (org-get-heading))
  11677. category
  11678. tags-list
  11679. )
  11680. priority (org-get-priority txt))
  11681. (goto-char lspos)
  11682. (setq marker (org-agenda-new-marker))
  11683. (org-add-props txt props
  11684. 'org-marker marker 'org-hd-marker marker 'org-category category
  11685. 'todo-state todo
  11686. 'priority priority 'type "tagsmatch")
  11687. (push txt rtn))
  11688. ((functionp action)
  11689. (setq org-map-continue-from nil)
  11690. (save-excursion
  11691. (setq rtn1 (funcall action))
  11692. (push rtn1 rtn)))
  11693. (t (error "Invalid action")))
  11694. ;; if we are to skip sublevels, jump to end of subtree
  11695. (unless org-tags-match-list-sublevels
  11696. (org-end-of-subtree t)
  11697. (backward-char 1))))
  11698. ;; Get the correct position from where to continue
  11699. (if org-map-continue-from
  11700. (goto-char org-map-continue-from)
  11701. (and (= (point) lspos) (end-of-line 1)))))
  11702. (when (and (eq action 'sparse-tree)
  11703. (not org-sparse-tree-open-archived-trees))
  11704. (org-hide-archived-subtrees (point-min) (point-max)))
  11705. (nreverse rtn)))
  11706. (defun org-remove-uninherited-tags (tags)
  11707. "Remove all tags that are not inherited from the list TAGS."
  11708. (cond
  11709. ((eq org-use-tag-inheritance t)
  11710. (if org-tags-exclude-from-inheritance
  11711. (org-delete-all org-tags-exclude-from-inheritance tags)
  11712. tags))
  11713. ((not org-use-tag-inheritance) nil)
  11714. ((stringp org-use-tag-inheritance)
  11715. (delq nil (mapcar
  11716. (lambda (x)
  11717. (if (and (string-match org-use-tag-inheritance x)
  11718. (not (member x org-tags-exclude-from-inheritance)))
  11719. x nil))
  11720. tags)))
  11721. ((listp org-use-tag-inheritance)
  11722. (delq nil (mapcar
  11723. (lambda (x)
  11724. (if (member x org-use-tag-inheritance) x nil))
  11725. tags)))))
  11726. (defvar todo-only) ;; dynamically scoped
  11727. (defun org-match-sparse-tree (&optional todo-only match)
  11728. "Create a sparse tree according to tags string MATCH.
  11729. MATCH can contain positive and negative selection of tags, like
  11730. \"+WORK+URGENT-WITHBOSS\".
  11731. If optional argument TODO-ONLY is non-nil, only select lines that are
  11732. also TODO lines."
  11733. (interactive "P")
  11734. (org-prepare-agenda-buffers (list (current-buffer)))
  11735. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  11736. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  11737. (defvar org-cached-props nil)
  11738. (defun org-cached-entry-get (pom property)
  11739. (if (or (eq t org-use-property-inheritance)
  11740. (and (stringp org-use-property-inheritance)
  11741. (string-match org-use-property-inheritance property))
  11742. (and (listp org-use-property-inheritance)
  11743. (member property org-use-property-inheritance)))
  11744. ;; Caching is not possible, check it directly
  11745. (org-entry-get pom property 'inherit)
  11746. ;; Get all properties, so that we can do complicated checks easily
  11747. (cdr (assoc property (or org-cached-props
  11748. (setq org-cached-props
  11749. (org-entry-properties pom)))))))
  11750. (defun org-global-tags-completion-table (&optional files)
  11751. "Return the list of all tags in all agenda buffer/files.
  11752. Optional FILES argument is a list of files to which can be used
  11753. instead of the agenda files."
  11754. (save-excursion
  11755. (org-uniquify
  11756. (delq nil
  11757. (apply 'append
  11758. (mapcar
  11759. (lambda (file)
  11760. (set-buffer (find-file-noselect file))
  11761. (append (org-get-buffer-tags)
  11762. (mapcar (lambda (x) (if (stringp (car-safe x))
  11763. (list (car-safe x)) nil))
  11764. org-tag-alist)))
  11765. (if (and files (car files))
  11766. files
  11767. (org-agenda-files))))))))
  11768. (defun org-make-tags-matcher (match)
  11769. "Create the TAGS/TODO matcher form for the selection string MATCH."
  11770. ;; todo-only is scoped dynamically into this function, and the function
  11771. ;; may change it if the matcher asks for it.
  11772. (unless match
  11773. ;; Get a new match request, with completion
  11774. (let ((org-last-tags-completion-table
  11775. (org-global-tags-completion-table)))
  11776. (setq match (org-completing-read-no-i
  11777. "Match: " 'org-tags-completion-function nil nil nil
  11778. 'org-tags-history))))
  11779. ;; Parse the string and create a lisp form
  11780. (let ((match0 match)
  11781. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\(\\(?:[[:alnum:]_]+\\(?:\\\\-\\)*\\)+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@#%]+\\)"))
  11782. minus tag mm
  11783. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  11784. orterms term orlist re-p str-p level-p level-op time-p
  11785. prop-p pn pv po gv rest)
  11786. (if (string-match "/+" match)
  11787. ;; match contains also a todo-matching request
  11788. (progn
  11789. (setq tagsmatch (substring match 0 (match-beginning 0))
  11790. todomatch (substring match (match-end 0)))
  11791. (if (string-match "^!" todomatch)
  11792. (setq todo-only t todomatch (substring todomatch 1)))
  11793. (if (string-match "^\\s-*$" todomatch)
  11794. (setq todomatch nil)))
  11795. ;; only matching tags
  11796. (setq tagsmatch match todomatch nil))
  11797. ;; Make the tags matcher
  11798. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  11799. (setq tagsmatcher t)
  11800. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  11801. (while (setq term (pop orterms))
  11802. (while (and (equal (substring term -1) "\\") orterms)
  11803. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  11804. (while (string-match re term)
  11805. (setq rest (substring term (match-end 0))
  11806. minus (and (match-end 1)
  11807. (equal (match-string 1 term) "-"))
  11808. tag (save-match-data (replace-regexp-in-string
  11809. "\\\\-" "-"
  11810. (match-string 2 term)))
  11811. re-p (equal (string-to-char tag) ?{)
  11812. level-p (match-end 4)
  11813. prop-p (match-end 5)
  11814. mm (cond
  11815. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  11816. (level-p
  11817. (setq level-op (org-op-to-function (match-string 3 term)))
  11818. `(,level-op level ,(string-to-number
  11819. (match-string 4 term))))
  11820. (prop-p
  11821. (setq pn (match-string 5 term)
  11822. po (match-string 6 term)
  11823. pv (match-string 7 term)
  11824. re-p (equal (string-to-char pv) ?{)
  11825. str-p (equal (string-to-char pv) ?\")
  11826. time-p (save-match-data
  11827. (string-match "^\"[[<].*[]>]\"$" pv))
  11828. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  11829. (if time-p (setq pv (org-matcher-time pv)))
  11830. (setq po (org-op-to-function po (if time-p 'time str-p)))
  11831. (cond
  11832. ((equal pn "CATEGORY")
  11833. (setq gv '(get-text-property (point) 'org-category)))
  11834. ((equal pn "TODO")
  11835. (setq gv 'todo))
  11836. (t
  11837. (setq gv `(org-cached-entry-get nil ,pn))))
  11838. (if re-p
  11839. (if (eq po 'org<>)
  11840. `(not (string-match ,pv (or ,gv "")))
  11841. `(string-match ,pv (or ,gv "")))
  11842. (if str-p
  11843. `(,po (or ,gv "") ,pv)
  11844. `(,po (string-to-number (or ,gv ""))
  11845. ,(string-to-number pv) ))))
  11846. (t `(member ,tag tags-list)))
  11847. mm (if minus (list 'not mm) mm)
  11848. term rest)
  11849. (push mm tagsmatcher))
  11850. (push (if (> (length tagsmatcher) 1)
  11851. (cons 'and tagsmatcher)
  11852. (car tagsmatcher))
  11853. orlist)
  11854. (setq tagsmatcher nil))
  11855. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  11856. (setq tagsmatcher
  11857. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  11858. ;; Make the todo matcher
  11859. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  11860. (setq todomatcher t)
  11861. (setq orterms (org-split-string todomatch "|") orlist nil)
  11862. (while (setq term (pop orterms))
  11863. (while (string-match re term)
  11864. (setq minus (and (match-end 1)
  11865. (equal (match-string 1 term) "-"))
  11866. kwd (match-string 2 term)
  11867. re-p (equal (string-to-char kwd) ?{)
  11868. term (substring term (match-end 0))
  11869. mm (if re-p
  11870. `(string-match ,(substring kwd 1 -1) todo)
  11871. (list 'equal 'todo kwd))
  11872. mm (if minus (list 'not mm) mm))
  11873. (push mm todomatcher))
  11874. (push (if (> (length todomatcher) 1)
  11875. (cons 'and todomatcher)
  11876. (car todomatcher))
  11877. orlist)
  11878. (setq todomatcher nil))
  11879. (setq todomatcher (if (> (length orlist) 1)
  11880. (cons 'or orlist) (car orlist))))
  11881. ;; Return the string and lisp forms of the matcher
  11882. (setq matcher (if todomatcher
  11883. (list 'and tagsmatcher todomatcher)
  11884. tagsmatcher))
  11885. (cons match0 matcher)))
  11886. (defun org-op-to-function (op &optional stringp)
  11887. "Turn an operator into the appropriate function."
  11888. (setq op
  11889. (cond
  11890. ((equal op "<" ) '(< string< org-time<))
  11891. ((equal op ">" ) '(> org-string> org-time>))
  11892. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  11893. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  11894. ((member op '("=" "==")) '(= string= org-time=))
  11895. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  11896. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  11897. (defun org<> (a b) (not (= a b)))
  11898. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  11899. (defun org-string>= (a b) (not (string< a b)))
  11900. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  11901. (defun org-string<> (a b) (not (string= a b)))
  11902. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  11903. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  11904. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  11905. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  11906. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  11907. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  11908. (defun org-2ft (s)
  11909. "Convert S to a floating point time.
  11910. If S is already a number, just return it. If it is a string, parse
  11911. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  11912. (cond
  11913. ((numberp s) s)
  11914. ((stringp s)
  11915. (condition-case nil
  11916. (float-time (apply 'encode-time (org-parse-time-string s)))
  11917. (error 0.)))
  11918. (t 0.)))
  11919. (defun org-time-today ()
  11920. "Time in seconds today at 0:00.
  11921. Returns the float number of seconds since the beginning of the
  11922. epoch to the beginning of today (00:00)."
  11923. (float-time (apply 'encode-time
  11924. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  11925. (defun org-matcher-time (s)
  11926. "Interpret a time comparison value."
  11927. (save-match-data
  11928. (cond
  11929. ((string= s "<now>") (float-time))
  11930. ((string= s "<today>") (org-time-today))
  11931. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  11932. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  11933. ((string-match "^<\\([-+][0-9]+\\)\\([dwmy]\\)>$" s)
  11934. (+ (org-time-today)
  11935. (* (string-to-number (match-string 1 s))
  11936. (cdr (assoc (match-string 2 s)
  11937. '(("d" . 86400.0) ("w" . 604800.0)
  11938. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  11939. (t (org-2ft s)))))
  11940. (defun org-match-any-p (re list)
  11941. "Does re match any element of list?"
  11942. (setq list (mapcar (lambda (x) (string-match re x)) list))
  11943. (delq nil list))
  11944. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  11945. (defvar org-tags-overlay (make-overlay 1 1))
  11946. (org-detach-overlay org-tags-overlay)
  11947. (defun org-get-local-tags-at (&optional pos)
  11948. "Get a list of tags defined in the current headline."
  11949. (org-get-tags-at pos 'local))
  11950. (defun org-get-local-tags ()
  11951. "Get a list of tags defined in the current headline."
  11952. (org-get-tags-at nil 'local))
  11953. (defun org-get-tags-at (&optional pos local)
  11954. "Get a list of all headline tags applicable at POS.
  11955. POS defaults to point. If tags are inherited, the list contains
  11956. the targets in the same sequence as the headlines appear, i.e.
  11957. the tags of the current headline come last.
  11958. When LOCAL is non-nil, only return tags from the current headline,
  11959. ignore inherited ones."
  11960. (interactive)
  11961. (if (and org-trust-scanner-tags
  11962. (or (not pos) (equal pos (point)))
  11963. (not local))
  11964. org-scanner-tags
  11965. (let (tags ltags lastpos parent)
  11966. (save-excursion
  11967. (save-restriction
  11968. (widen)
  11969. (goto-char (or pos (point)))
  11970. (save-match-data
  11971. (catch 'done
  11972. (condition-case nil
  11973. (progn
  11974. (org-back-to-heading t)
  11975. (while (not (equal lastpos (point)))
  11976. (setq lastpos (point))
  11977. (when (looking-at
  11978. (org-re "[^\r\n]+?:\\([[:alnum:]_@#%:]+\\):[ \t]*$"))
  11979. (setq ltags (org-split-string
  11980. (org-match-string-no-properties 1) ":"))
  11981. (when parent
  11982. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  11983. (setq tags (append
  11984. (if parent
  11985. (org-remove-uninherited-tags ltags)
  11986. ltags)
  11987. tags)))
  11988. (or org-use-tag-inheritance (throw 'done t))
  11989. (if local (throw 'done t))
  11990. (or (org-up-heading-safe) (error nil))
  11991. (setq parent t)))
  11992. (error nil)))))
  11993. (if local
  11994. tags
  11995. (append (org-remove-uninherited-tags org-file-tags) tags))))))
  11996. (defun org-add-prop-inherited (s)
  11997. (add-text-properties 0 (length s) '(inherited t) s)
  11998. s)
  11999. (defun org-toggle-tag (tag &optional onoff)
  12000. "Toggle the tag TAG for the current line.
  12001. If ONOFF is `on' or `off', don't toggle but set to this state."
  12002. (let (res current)
  12003. (save-excursion
  12004. (org-back-to-heading t)
  12005. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t]*$")
  12006. (point-at-eol) t)
  12007. (progn
  12008. (setq current (match-string 1))
  12009. (replace-match ""))
  12010. (setq current ""))
  12011. (setq current (nreverse (org-split-string current ":")))
  12012. (cond
  12013. ((eq onoff 'on)
  12014. (setq res t)
  12015. (or (member tag current) (push tag current)))
  12016. ((eq onoff 'off)
  12017. (or (not (member tag current)) (setq current (delete tag current))))
  12018. (t (if (member tag current)
  12019. (setq current (delete tag current))
  12020. (setq res t)
  12021. (push tag current))))
  12022. (end-of-line 1)
  12023. (if current
  12024. (progn
  12025. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  12026. (org-set-tags nil t))
  12027. (delete-horizontal-space))
  12028. (run-hooks 'org-after-tags-change-hook))
  12029. res))
  12030. (defun org-align-tags-here (to-col)
  12031. ;; Assumes that this is a headline
  12032. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  12033. (beginning-of-line 1)
  12034. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  12035. (< pos (match-beginning 2)))
  12036. (progn
  12037. (setq tags-l (- (match-end 2) (match-beginning 2)))
  12038. (goto-char (match-beginning 1))
  12039. (insert " ")
  12040. (delete-region (point) (1+ (match-beginning 2)))
  12041. (setq ncol (max (current-column)
  12042. (1+ col)
  12043. (if (> to-col 0)
  12044. to-col
  12045. (- (abs to-col) tags-l))))
  12046. (setq p (point))
  12047. (insert (make-string (- ncol (current-column)) ?\ ))
  12048. (setq ncol (current-column))
  12049. (when indent-tabs-mode (tabify p (point-at-eol)))
  12050. (org-move-to-column (min ncol col) t))
  12051. (goto-char pos))))
  12052. (defun org-set-tags-command (&optional arg just-align)
  12053. "Call the set-tags command for the current entry."
  12054. (interactive "P")
  12055. (if (org-on-heading-p)
  12056. (org-set-tags arg just-align)
  12057. (save-excursion
  12058. (org-back-to-heading t)
  12059. (org-set-tags arg just-align))))
  12060. (defun org-set-tags-to (data)
  12061. "Set the tags of the current entry to DATA, replacing the current tags.
  12062. DATA may be a tags string like :aa:bb:cc:, or a list of tags.
  12063. If DATA is nil or the empty string, any tags will be removed."
  12064. (interactive "sTags: ")
  12065. (setq data
  12066. (cond
  12067. ((eq data nil) "")
  12068. ((equal data "") "")
  12069. ((stringp data)
  12070. (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
  12071. ":"))
  12072. ((listp data)
  12073. (concat ":" (mapconcat 'identity data ":") ":"))
  12074. (t nil)))
  12075. (when data
  12076. (save-excursion
  12077. (org-back-to-heading t)
  12078. (when (looking-at org-complex-heading-regexp)
  12079. (if (match-end 5)
  12080. (progn
  12081. (goto-char (match-beginning 5))
  12082. (insert data)
  12083. (delete-region (point) (point-at-eol))
  12084. (org-set-tags nil 'align))
  12085. (goto-char (point-at-eol))
  12086. (insert " " data)
  12087. (org-set-tags nil 'align)))
  12088. (beginning-of-line 1)
  12089. (if (looking-at ".*?\\([ \t]+\\)$")
  12090. (delete-region (match-beginning 1) (match-end 1))))))
  12091. (defun org-align-all-tags ()
  12092. "Align the tags i all headings."
  12093. (interactive)
  12094. (save-excursion
  12095. (or (ignore-errors (org-back-to-heading t))
  12096. (outline-next-heading))
  12097. (if (org-on-heading-p)
  12098. (org-set-tags t)
  12099. (message "No headings"))))
  12100. (defvar org-indent-indentation-per-level)
  12101. (defun org-set-tags (&optional arg just-align)
  12102. "Set the tags for the current headline.
  12103. With prefix ARG, realign all tags in headings in the current buffer."
  12104. (interactive "P")
  12105. (let* ((re org-outline-regexp-bol)
  12106. (current (org-get-tags-string))
  12107. (col (current-column))
  12108. (org-setting-tags t)
  12109. table current-tags inherited-tags ; computed below when needed
  12110. tags p0 c0 c1 rpl di tc level)
  12111. (if arg
  12112. (save-excursion
  12113. (goto-char (point-min))
  12114. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  12115. (while (re-search-forward re nil t)
  12116. (org-set-tags nil t)
  12117. (end-of-line 1)))
  12118. (message "All tags realigned to column %d" org-tags-column))
  12119. (if just-align
  12120. (setq tags current)
  12121. ;; Get a new set of tags from the user
  12122. (save-excursion
  12123. (setq table (append org-tag-persistent-alist
  12124. (or org-tag-alist (org-get-buffer-tags))
  12125. (and
  12126. org-complete-tags-always-offer-all-agenda-tags
  12127. (org-global-tags-completion-table
  12128. (org-agenda-files))))
  12129. org-last-tags-completion-table table
  12130. current-tags (org-split-string current ":")
  12131. inherited-tags (nreverse
  12132. (nthcdr (length current-tags)
  12133. (nreverse (org-get-tags-at))))
  12134. tags
  12135. (if (or (eq t org-use-fast-tag-selection)
  12136. (and org-use-fast-tag-selection
  12137. (delq nil (mapcar 'cdr table))))
  12138. (org-fast-tag-selection
  12139. current-tags inherited-tags table
  12140. (if org-fast-tag-selection-include-todo
  12141. org-todo-key-alist))
  12142. (let ((org-add-colon-after-tag-completion t))
  12143. (org-trim
  12144. (org-icompleting-read "Tags: "
  12145. 'org-tags-completion-function
  12146. nil nil current 'org-tags-history))))))
  12147. (while (string-match "[-+&]+" tags)
  12148. ;; No boolean logic, just a list
  12149. (setq tags (replace-match ":" t t tags))))
  12150. (setq tags (replace-regexp-in-string "[,]" ":" tags))
  12151. (if org-tags-sort-function
  12152. (setq tags (mapconcat 'identity
  12153. (sort (org-split-string
  12154. tags (org-re "[^[:alnum:]_@#%]+"))
  12155. org-tags-sort-function) ":")))
  12156. (if (string-match "\\`[\t ]*\\'" tags)
  12157. (setq tags "")
  12158. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  12159. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  12160. ;; Insert new tags at the correct column
  12161. (beginning-of-line 1)
  12162. (setq level (or (and (looking-at org-outline-regexp)
  12163. (- (match-end 0) (point) 1))
  12164. 1))
  12165. (cond
  12166. ((and (equal current "") (equal tags "")))
  12167. ((re-search-forward
  12168. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  12169. (point-at-eol) t)
  12170. (if (equal tags "")
  12171. (setq rpl "")
  12172. (goto-char (match-beginning 0))
  12173. (setq c0 (current-column)
  12174. ;; compute offset for the case of org-indent-mode active
  12175. di (if org-indent-mode
  12176. (* (1- org-indent-indentation-per-level) (1- level))
  12177. 0)
  12178. p0 (if (equal (char-before) ?*) (1+ (point)) (point))
  12179. tc (+ org-tags-column (if (> org-tags-column 0) (- di) di))
  12180. c1 (max (1+ c0) (if (> tc 0) tc (- (- tc) (length tags))))
  12181. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  12182. (replace-match rpl t t)
  12183. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  12184. tags)
  12185. (t (error "Tags alignment failed")))
  12186. (org-move-to-column col)
  12187. (unless just-align
  12188. (run-hooks 'org-after-tags-change-hook)))))
  12189. (defun org-change-tag-in-region (beg end tag off)
  12190. "Add or remove TAG for each entry in the region.
  12191. This works in the agenda, and also in an org-mode buffer."
  12192. (interactive
  12193. (list (region-beginning) (region-end)
  12194. (let ((org-last-tags-completion-table
  12195. (if (eq major-mode 'org-mode)
  12196. (org-get-buffer-tags)
  12197. (org-global-tags-completion-table))))
  12198. (org-icompleting-read
  12199. "Tag: " 'org-tags-completion-function nil nil nil
  12200. 'org-tags-history))
  12201. (progn
  12202. (message "[s]et or [r]emove? ")
  12203. (equal (read-char-exclusive) ?r))))
  12204. (if (fboundp 'deactivate-mark) (deactivate-mark))
  12205. (let ((agendap (equal major-mode 'org-agenda-mode))
  12206. l1 l2 m buf pos newhead (cnt 0))
  12207. (goto-char end)
  12208. (setq l2 (1- (org-current-line)))
  12209. (goto-char beg)
  12210. (setq l1 (org-current-line))
  12211. (loop for l from l1 to l2 do
  12212. (org-goto-line l)
  12213. (setq m (get-text-property (point) 'org-hd-marker))
  12214. (when (or (and (eq major-mode 'org-mode) (org-on-heading-p))
  12215. (and agendap m))
  12216. (setq buf (if agendap (marker-buffer m) (current-buffer))
  12217. pos (if agendap m (point)))
  12218. (with-current-buffer buf
  12219. (save-excursion
  12220. (save-restriction
  12221. (goto-char pos)
  12222. (setq cnt (1+ cnt))
  12223. (org-toggle-tag tag (if off 'off 'on))
  12224. (setq newhead (org-get-heading)))))
  12225. (and agendap (org-agenda-change-all-lines newhead m))))
  12226. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  12227. (defun org-tags-completion-function (string predicate &optional flag)
  12228. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  12229. (confirm (lambda (x) (stringp (car x)))))
  12230. (if (string-match "^\\(.*[-+:&,|]\\)\\([^-+:&,|]*\\)$" string)
  12231. (setq s1 (match-string 1 string)
  12232. s2 (match-string 2 string))
  12233. (setq s1 "" s2 string))
  12234. (cond
  12235. ((eq flag nil)
  12236. ;; try completion
  12237. (setq rtn (try-completion s2 ctable confirm))
  12238. (if (stringp rtn)
  12239. (setq rtn
  12240. (concat s1 s2 (substring rtn (length s2))
  12241. (if (and org-add-colon-after-tag-completion
  12242. (assoc rtn ctable))
  12243. ":" ""))))
  12244. rtn)
  12245. ((eq flag t)
  12246. ;; all-completions
  12247. (all-completions s2 ctable confirm)
  12248. )
  12249. ((eq flag 'lambda)
  12250. ;; exact match?
  12251. (assoc s2 ctable)))
  12252. ))
  12253. (defun org-fast-tag-insert (kwd tags face &optional end)
  12254. "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
  12255. (insert (format "%-12s" (concat kwd ":"))
  12256. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  12257. (or end "")))
  12258. (defun org-fast-tag-show-exit (flag)
  12259. (save-excursion
  12260. (org-goto-line 3)
  12261. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  12262. (replace-match ""))
  12263. (when flag
  12264. (end-of-line 1)
  12265. (org-move-to-column (- (window-width) 19) t)
  12266. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  12267. (defun org-set-current-tags-overlay (current prefix)
  12268. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  12269. (if (featurep 'xemacs)
  12270. (org-overlay-display org-tags-overlay (concat prefix s)
  12271. 'secondary-selection)
  12272. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  12273. (org-overlay-display org-tags-overlay (concat prefix s)))))
  12274. (defvar org-last-tag-selection-key nil)
  12275. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  12276. "Fast tag selection with single keys.
  12277. CURRENT is the current list of tags in the headline, INHERITED is the
  12278. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  12279. possibly with grouping information. TODO-TABLE is a similar table with
  12280. TODO keywords, should these have keys assigned to them.
  12281. If the keys are nil, a-z are automatically assigned.
  12282. Returns the new tags string, or nil to not change the current settings."
  12283. (let* ((fulltable (append table todo-table))
  12284. (maxlen (apply 'max (mapcar
  12285. (lambda (x)
  12286. (if (stringp (car x)) (string-width (car x)) 0))
  12287. fulltable)))
  12288. (buf (current-buffer))
  12289. (expert (eq org-fast-tag-selection-single-key 'expert))
  12290. (buffer-tags nil)
  12291. (fwidth (+ maxlen 3 1 3))
  12292. (ncol (/ (- (window-width) 4) fwidth))
  12293. (i-face 'org-done)
  12294. (c-face 'org-todo)
  12295. tg cnt e c char c1 c2 ntable tbl rtn
  12296. ov-start ov-end ov-prefix
  12297. (exit-after-next org-fast-tag-selection-single-key)
  12298. (done-keywords org-done-keywords)
  12299. groups ingroup)
  12300. (save-excursion
  12301. (beginning-of-line 1)
  12302. (if (looking-at
  12303. (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  12304. (setq ov-start (match-beginning 1)
  12305. ov-end (match-end 1)
  12306. ov-prefix "")
  12307. (setq ov-start (1- (point-at-eol))
  12308. ov-end (1+ ov-start))
  12309. (skip-chars-forward "^\n\r")
  12310. (setq ov-prefix
  12311. (concat
  12312. (buffer-substring (1- (point)) (point))
  12313. (if (> (current-column) org-tags-column)
  12314. " "
  12315. (make-string (- org-tags-column (current-column)) ?\ ))))))
  12316. (move-overlay org-tags-overlay ov-start ov-end)
  12317. (save-window-excursion
  12318. (if expert
  12319. (set-buffer (get-buffer-create " *Org tags*"))
  12320. (delete-other-windows)
  12321. (split-window-vertically)
  12322. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  12323. (erase-buffer)
  12324. (org-set-local 'org-done-keywords done-keywords)
  12325. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  12326. (org-fast-tag-insert "Current" current c-face "\n\n")
  12327. (org-fast-tag-show-exit exit-after-next)
  12328. (org-set-current-tags-overlay current ov-prefix)
  12329. (setq tbl fulltable char ?a cnt 0)
  12330. (while (setq e (pop tbl))
  12331. (cond
  12332. ((equal (car e) :startgroup)
  12333. (push '() groups) (setq ingroup t)
  12334. (when (not (= cnt 0))
  12335. (setq cnt 0)
  12336. (insert "\n"))
  12337. (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
  12338. ((equal (car e) :endgroup)
  12339. (setq ingroup nil cnt 0)
  12340. (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
  12341. ((equal e '(:newline))
  12342. (when (not (= cnt 0))
  12343. (setq cnt 0)
  12344. (insert "\n")
  12345. (setq e (car tbl))
  12346. (while (equal (car tbl) '(:newline))
  12347. (insert "\n")
  12348. (setq tbl (cdr tbl)))))
  12349. (t
  12350. (setq tg (copy-sequence (car e)) c2 nil)
  12351. (if (cdr e)
  12352. (setq c (cdr e))
  12353. ;; automatically assign a character.
  12354. (setq c1 (string-to-char
  12355. (downcase (substring
  12356. tg (if (= (string-to-char tg) ?@) 1 0)))))
  12357. (if (or (rassoc c1 ntable) (rassoc c1 table))
  12358. (while (or (rassoc char ntable) (rassoc char table))
  12359. (setq char (1+ char)))
  12360. (setq c2 c1))
  12361. (setq c (or c2 char)))
  12362. (if ingroup (push tg (car groups)))
  12363. (setq tg (org-add-props tg nil 'face
  12364. (cond
  12365. ((not (assoc tg table))
  12366. (org-get-todo-face tg))
  12367. ((member tg current) c-face)
  12368. ((member tg inherited) i-face)
  12369. (t nil))))
  12370. (if (and (= cnt 0) (not ingroup)) (insert " "))
  12371. (insert "[" c "] " tg (make-string
  12372. (- fwidth 4 (length tg)) ?\ ))
  12373. (push (cons tg c) ntable)
  12374. (when (= (setq cnt (1+ cnt)) ncol)
  12375. (insert "\n")
  12376. (if ingroup (insert " "))
  12377. (setq cnt 0)))))
  12378. (setq ntable (nreverse ntable))
  12379. (insert "\n")
  12380. (goto-char (point-min))
  12381. (if (not expert) (org-fit-window-to-buffer))
  12382. (setq rtn
  12383. (catch 'exit
  12384. (while t
  12385. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
  12386. (if (not groups) "no " "")
  12387. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  12388. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  12389. (setq org-last-tag-selection-key c)
  12390. (cond
  12391. ((= c ?\r) (throw 'exit t))
  12392. ((= c ?!)
  12393. (setq groups (not groups))
  12394. (goto-char (point-min))
  12395. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  12396. ((= c ?\C-c)
  12397. (if (not expert)
  12398. (org-fast-tag-show-exit
  12399. (setq exit-after-next (not exit-after-next)))
  12400. (setq expert nil)
  12401. (delete-other-windows)
  12402. (set-window-buffer (split-window-vertically) " *Org tags*")
  12403. (org-switch-to-buffer-other-window " *Org tags*")
  12404. (org-fit-window-to-buffer)))
  12405. ((or (= c ?\C-g)
  12406. (and (= c ?q) (not (rassoc c ntable))))
  12407. (org-detach-overlay org-tags-overlay)
  12408. (setq quit-flag t))
  12409. ((= c ?\ )
  12410. (setq current nil)
  12411. (if exit-after-next (setq exit-after-next 'now)))
  12412. ((= c ?\t)
  12413. (condition-case nil
  12414. (setq tg (org-icompleting-read
  12415. "Tag: "
  12416. (or buffer-tags
  12417. (with-current-buffer buf
  12418. (org-get-buffer-tags)))))
  12419. (quit (setq tg "")))
  12420. (when (string-match "\\S-" tg)
  12421. (add-to-list 'buffer-tags (list tg))
  12422. (if (member tg current)
  12423. (setq current (delete tg current))
  12424. (push tg current)))
  12425. (if exit-after-next (setq exit-after-next 'now)))
  12426. ((setq e (rassoc c todo-table) tg (car e))
  12427. (with-current-buffer buf
  12428. (save-excursion (org-todo tg)))
  12429. (if exit-after-next (setq exit-after-next 'now)))
  12430. ((setq e (rassoc c ntable) tg (car e))
  12431. (if (member tg current)
  12432. (setq current (delete tg current))
  12433. (loop for g in groups do
  12434. (if (member tg g)
  12435. (mapc (lambda (x)
  12436. (setq current (delete x current)))
  12437. g)))
  12438. (push tg current))
  12439. (if exit-after-next (setq exit-after-next 'now))))
  12440. ;; Create a sorted list
  12441. (setq current
  12442. (sort current
  12443. (lambda (a b)
  12444. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  12445. (if (eq exit-after-next 'now) (throw 'exit t))
  12446. (goto-char (point-min))
  12447. (beginning-of-line 2)
  12448. (delete-region (point) (point-at-eol))
  12449. (org-fast-tag-insert "Current" current c-face)
  12450. (org-set-current-tags-overlay current ov-prefix)
  12451. (while (re-search-forward
  12452. (org-re "\\[.\\] \\([[:alnum:]_@#%]+\\)") nil t)
  12453. (setq tg (match-string 1))
  12454. (add-text-properties
  12455. (match-beginning 1) (match-end 1)
  12456. (list 'face
  12457. (cond
  12458. ((member tg current) c-face)
  12459. ((member tg inherited) i-face)
  12460. (t (get-text-property (match-beginning 1) 'face))))))
  12461. (goto-char (point-min)))))
  12462. (org-detach-overlay org-tags-overlay)
  12463. (if rtn
  12464. (mapconcat 'identity current ":")
  12465. nil))))
  12466. (defun org-get-tags-string ()
  12467. "Get the TAGS string in the current headline."
  12468. (unless (org-on-heading-p t)
  12469. (error "Not on a heading"))
  12470. (save-excursion
  12471. (beginning-of-line 1)
  12472. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  12473. (org-match-string-no-properties 1)
  12474. "")))
  12475. (defun org-get-tags ()
  12476. "Get the list of tags specified in the current headline."
  12477. (org-split-string (org-get-tags-string) ":"))
  12478. (defun org-get-buffer-tags ()
  12479. "Get a table of all tags used in the buffer, for completion."
  12480. (let (tags)
  12481. (save-excursion
  12482. (goto-char (point-min))
  12483. (while (re-search-forward
  12484. (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t\r\n]") nil t)
  12485. (when (equal (char-after (point-at-bol 0)) ?*)
  12486. (mapc (lambda (x) (add-to-list 'tags x))
  12487. (org-split-string (org-match-string-no-properties 1) ":")))))
  12488. (mapc (lambda (s) (add-to-list 'tags s)) org-file-tags)
  12489. (mapcar 'list tags)))
  12490. ;;;; The mapping API
  12491. ;;;###autoload
  12492. (defun org-map-entries (func &optional match scope &rest skip)
  12493. "Call FUNC at each headline selected by MATCH in SCOPE.
  12494. FUNC is a function or a lisp form. The function will be called without
  12495. arguments, with the cursor positioned at the beginning of the headline.
  12496. The return values of all calls to the function will be collected and
  12497. returned as a list.
  12498. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  12499. does not need to preserve point. After evaluation, the cursor will be
  12500. moved to the end of the line (presumably of the headline of the
  12501. processed entry) and search continues from there. Under some
  12502. circumstances, this may not produce the wanted results. For example,
  12503. if you have removed (e.g. archived) the current (sub)tree it could
  12504. mean that the next entry will be skipped entirely. In such cases, you
  12505. can specify the position from where search should continue by making
  12506. FUNC set the variable `org-map-continue-from' to the desired buffer
  12507. position.
  12508. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  12509. Only headlines that are matched by this query will be considered during
  12510. the iteration. When MATCH is nil or t, all headlines will be
  12511. visited by the iteration.
  12512. SCOPE determines the scope of this command. It can be any of:
  12513. nil The current buffer, respecting the restriction if any
  12514. tree The subtree started with the entry at point
  12515. region The entries within the active region, if any
  12516. file The current buffer, without restriction
  12517. file-with-archives
  12518. The current buffer, and any archives associated with it
  12519. agenda All agenda files
  12520. agenda-with-archives
  12521. All agenda files with any archive files associated with them
  12522. \(file1 file2 ...)
  12523. If this is a list, all files in the list will be scanned
  12524. The remaining args are treated as settings for the skipping facilities of
  12525. the scanner. The following items can be given here:
  12526. archive skip trees with the archive tag.
  12527. comment skip trees with the COMMENT keyword
  12528. function or Emacs Lisp form:
  12529. will be used as value for `org-agenda-skip-function', so whenever
  12530. the function returns t, FUNC will not be called for that
  12531. entry and search will continue from the point where the
  12532. function leaves it.
  12533. If your function needs to retrieve the tags including inherited tags
  12534. at the *current* entry, you can use the value of the variable
  12535. `org-scanner-tags' which will be much faster than getting the value
  12536. with `org-get-tags-at'. If your function gets properties with
  12537. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  12538. to t around the call to `org-entry-properties' to get the same speedup.
  12539. Note that if your function moves around to retrieve tags and properties at
  12540. a *different* entry, you cannot use these techniques."
  12541. (unless (and (eq scope 'region) (not (org-region-active-p)))
  12542. (let* ((org-agenda-archives-mode nil) ; just to make sure
  12543. (org-agenda-skip-archived-trees (memq 'archive skip))
  12544. (org-agenda-skip-comment-trees (memq 'comment skip))
  12545. (org-agenda-skip-function
  12546. (car (org-delete-all '(comment archive) skip)))
  12547. (org-tags-match-list-sublevels t)
  12548. matcher file res
  12549. org-todo-keywords-for-agenda
  12550. org-done-keywords-for-agenda
  12551. org-todo-keyword-alist-for-agenda
  12552. org-drawers-for-agenda
  12553. org-tag-alist-for-agenda)
  12554. (cond
  12555. ((eq match t) (setq matcher t))
  12556. ((eq match nil) (setq matcher t))
  12557. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  12558. (save-excursion
  12559. (save-restriction
  12560. (cond ((eq scope 'tree)
  12561. (org-back-to-heading t)
  12562. (org-narrow-to-subtree)
  12563. (setq scope nil))
  12564. ((and (eq scope 'region) (org-region-active-p))
  12565. (narrow-to-region (region-beginning)
  12566. (save-excursion
  12567. (goto-char (region-end))
  12568. (unless (and (bolp) (org-at-heading-p))
  12569. (outline-next-heading))
  12570. (point)))
  12571. (setq scope nil)))
  12572. (if (not scope)
  12573. (progn
  12574. (org-prepare-agenda-buffers
  12575. (list (buffer-file-name (current-buffer))))
  12576. (setq res (org-scan-tags func matcher)))
  12577. ;; Get the right scope
  12578. (cond
  12579. ((and scope (listp scope) (symbolp (car scope)))
  12580. (setq scope (eval scope)))
  12581. ((eq scope 'agenda)
  12582. (setq scope (org-agenda-files t)))
  12583. ((eq scope 'agenda-with-archives)
  12584. (setq scope (org-agenda-files t))
  12585. (setq scope (org-add-archive-files scope)))
  12586. ((eq scope 'file)
  12587. (setq scope (list (buffer-file-name))))
  12588. ((eq scope 'file-with-archives)
  12589. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  12590. (org-prepare-agenda-buffers scope)
  12591. (while (setq file (pop scope))
  12592. (with-current-buffer (org-find-base-buffer-visiting file)
  12593. (save-excursion
  12594. (save-restriction
  12595. (widen)
  12596. (goto-char (point-min))
  12597. (setq res (append res (org-scan-tags func matcher))))))))))
  12598. res)))
  12599. ;;;; Properties
  12600. ;;; Setting and retrieving properties
  12601. (defconst org-special-properties
  12602. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  12603. "TIMESTAMP" "TIMESTAMP_IA" "BLOCKED" "FILE" "CLOCKSUM")
  12604. "The special properties valid in Org-mode.
  12605. These are properties that are not defined in the property drawer,
  12606. but in some other way.")
  12607. (defconst org-default-properties
  12608. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  12609. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  12610. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  12611. "EXPORT_OPTIONS" "EXPORT_TEXT" "EXPORT_FILE_NAME"
  12612. "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
  12613. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER" "REPEAT_TO_STATE"
  12614. "CLOCK_MODELINE_TOTAL" "STYLE" "HTML_CONTAINER_CLASS")
  12615. "Some properties that are used by Org-mode for various purposes.
  12616. Being in this list makes sure that they are offered for completion.")
  12617. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  12618. "Regular expression matching the first line of a property drawer.")
  12619. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  12620. "Regular expression matching the last line of a property drawer.")
  12621. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  12622. "Regular expression matching the first line of a property drawer.")
  12623. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  12624. "Regular expression matching the first line of a property drawer.")
  12625. (defconst org-property-drawer-re
  12626. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  12627. org-property-end-re "\\)\n?")
  12628. "Matches an entire property drawer.")
  12629. (defconst org-clock-drawer-re
  12630. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  12631. org-property-end-re "\\)\n?")
  12632. "Matches an entire clock drawer.")
  12633. (defsubst org-re-property (property)
  12634. "Return a regexp matching PROPERTY.
  12635. Match group 1 will be set to the value "
  12636. (concat "^[ \t]*:" (regexp-quote property) ":[ \t]*\\(\\S-.*\\)"))
  12637. (defun org-property-action ()
  12638. "Do an action on properties."
  12639. (interactive)
  12640. (let (c)
  12641. (org-at-property-p)
  12642. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  12643. (setq c (read-char-exclusive))
  12644. (cond
  12645. ((equal c ?s)
  12646. (call-interactively 'org-set-property))
  12647. ((equal c ?d)
  12648. (call-interactively 'org-delete-property))
  12649. ((equal c ?D)
  12650. (call-interactively 'org-delete-property-globally))
  12651. ((equal c ?c)
  12652. (call-interactively 'org-compute-property-at-point))
  12653. (t (error "No such property action %c" c)))))
  12654. (defun org-set-effort (&optional value)
  12655. "Set the effort property of the current entry.
  12656. With numerical prefix arg, use the nth allowed value, 0 stands for the 10th
  12657. allowed value."
  12658. (interactive "P")
  12659. (if (equal value 0) (setq value 10))
  12660. (let* ((completion-ignore-case t)
  12661. (prop org-effort-property)
  12662. (cur (org-entry-get nil prop))
  12663. (allowed (org-property-get-allowed-values nil prop 'table))
  12664. (existing (mapcar 'list (org-property-values prop)))
  12665. rpl
  12666. (val (cond
  12667. ((stringp value) value)
  12668. ((and allowed (integerp value))
  12669. (or (car (nth (1- value) allowed))
  12670. (car (org-last allowed))))
  12671. (allowed
  12672. (message "Select 1-9,0, [RET%s]: %s"
  12673. (if cur (concat "=" cur) "")
  12674. (mapconcat 'car allowed " "))
  12675. (setq rpl (read-char-exclusive))
  12676. (if (equal rpl ?\r)
  12677. cur
  12678. (setq rpl (- rpl ?0))
  12679. (if (equal rpl 0) (setq rpl 10))
  12680. (if (and (> rpl 0) (<= rpl (length allowed)))
  12681. (car (nth (1- rpl) allowed))
  12682. (org-completing-read "Effort: " allowed nil))))
  12683. (t
  12684. (let (org-completion-use-ido org-completion-use-iswitchb)
  12685. (org-completing-read
  12686. (concat "Effort " (if (and cur (string-match "\\S-" cur))
  12687. (concat "[" cur "]") "")
  12688. ": ")
  12689. existing nil nil "" nil cur))))))
  12690. (unless (equal (org-entry-get nil prop) val)
  12691. (org-entry-put nil prop val))
  12692. (message "%s is now %s" prop val)))
  12693. (defun org-at-property-p ()
  12694. "Is cursor inside a property drawer?"
  12695. (save-excursion
  12696. (beginning-of-line 1)
  12697. (when (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))
  12698. (save-match-data ;; Used by calling procedures
  12699. (let ((p (point))
  12700. (range (unless (org-before-first-heading-p)
  12701. (org-get-property-block))))
  12702. (and range (<= (car range) p) (< p (cdr range))))))))
  12703. (defun org-get-property-block (&optional beg end force)
  12704. "Return the (beg . end) range of the body of the property drawer.
  12705. BEG and END can be beginning and end of subtree, if not given
  12706. they will be found.
  12707. If the drawer does not exist and FORCE is non-nil, create the drawer."
  12708. (catch 'exit
  12709. (save-excursion
  12710. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  12711. (end (or end (progn (outline-next-heading) (point)))))
  12712. (goto-char beg)
  12713. (if (re-search-forward org-property-start-re end t)
  12714. (setq beg (1+ (match-end 0)))
  12715. (if force
  12716. (save-excursion
  12717. (org-insert-property-drawer)
  12718. (setq end (progn (outline-next-heading) (point))))
  12719. (throw 'exit nil))
  12720. (goto-char beg)
  12721. (if (re-search-forward org-property-start-re end t)
  12722. (setq beg (1+ (match-end 0)))))
  12723. (if (re-search-forward org-property-end-re end t)
  12724. (setq end (match-beginning 0))
  12725. (or force (throw 'exit nil))
  12726. (goto-char beg)
  12727. (setq end beg)
  12728. (org-indent-line-function)
  12729. (insert ":END:\n"))
  12730. (cons beg end)))))
  12731. (defun org-entry-properties (&optional pom which specific)
  12732. "Get all properties of the entry at point-or-marker POM.
  12733. This includes the TODO keyword, the tags, time strings for deadline,
  12734. scheduled, and clocking, and any additional properties defined in the
  12735. entry. The return value is an alist, keys may occur multiple times
  12736. if the property key was used several times.
  12737. POM may also be nil, in which case the current entry is used.
  12738. If WHICH is nil or `all', get all properties. If WHICH is
  12739. `special' or `standard', only get that subclass. If WHICH
  12740. is a string only get exactly this property. SPECIFIC can be a string, the
  12741. specific property we are interested in. Specifying it can speed
  12742. things up because then unnecessary parsing is avoided."
  12743. (setq which (or which 'all))
  12744. (org-with-point-at pom
  12745. (let ((clockstr (substring org-clock-string 0 -1))
  12746. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY" "BLOCKED"))
  12747. (case-fold-search nil)
  12748. beg end range props sum-props key key1 value string clocksum)
  12749. (save-excursion
  12750. (when (condition-case nil
  12751. (and (eq major-mode 'org-mode) (org-back-to-heading t))
  12752. (error nil))
  12753. (setq beg (point))
  12754. (setq sum-props (get-text-property (point) 'org-summaries))
  12755. (setq clocksum (get-text-property (point) :org-clock-minutes))
  12756. (outline-next-heading)
  12757. (setq end (point))
  12758. (when (memq which '(all special))
  12759. ;; Get the special properties, like TODO and tags
  12760. (goto-char beg)
  12761. (when (and (or (not specific) (string= specific "TODO"))
  12762. (looking-at org-todo-line-regexp) (match-end 2))
  12763. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  12764. (when (and (or (not specific) (string= specific "PRIORITY"))
  12765. (looking-at org-priority-regexp))
  12766. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  12767. (when (or (not specific) (string= specific "FILE"))
  12768. (push (cons "FILE" buffer-file-name) props))
  12769. (when (and (or (not specific) (string= specific "TAGS"))
  12770. (setq value (org-get-tags-string))
  12771. (string-match "\\S-" value))
  12772. (push (cons "TAGS" value) props))
  12773. (when (and (or (not specific) (string= specific "ALLTAGS"))
  12774. (setq value (org-get-tags-at)))
  12775. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":")
  12776. ":"))
  12777. props))
  12778. (when (or (not specific) (string= specific "BLOCKED"))
  12779. (push (cons "BLOCKED" (if (org-entry-blocked-p) "t" "")) props))
  12780. (when (or (not specific)
  12781. (member specific
  12782. '("SCHEDULED" "DEADLINE" "CLOCK" "CLOSED"
  12783. "TIMESTAMP" "TIMESTAMP_IA")))
  12784. (catch 'match
  12785. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  12786. (setq key (if (match-end 1)
  12787. (substring (org-match-string-no-properties 1)
  12788. 0 -1))
  12789. string (if (equal key clockstr)
  12790. (org-no-properties
  12791. (org-trim
  12792. (buffer-substring
  12793. (match-beginning 3) (goto-char
  12794. (point-at-eol)))))
  12795. (substring (org-match-string-no-properties 3)
  12796. 1 -1)))
  12797. ;; Get the correct property name from the key. This is
  12798. ;; necessary if the user has configured time keywords.
  12799. (setq key1 (concat key ":"))
  12800. (cond
  12801. ((not key)
  12802. (setq key
  12803. (if (= (char-after (match-beginning 3)) ?\[)
  12804. "TIMESTAMP_IA" "TIMESTAMP")))
  12805. ((equal key1 org-scheduled-string) (setq key "SCHEDULED"))
  12806. ((equal key1 org-deadline-string) (setq key "DEADLINE"))
  12807. ((equal key1 org-closed-string) (setq key "CLOSED"))
  12808. ((equal key1 org-clock-string) (setq key "CLOCK")))
  12809. (if (and specific (equal key specific) (not (equal key "CLOCK")))
  12810. (progn
  12811. (push (cons key string) props)
  12812. ;; no need to search further if match is found
  12813. (throw 'match t))
  12814. (when (or (equal key "CLOCK") (not (assoc key props)))
  12815. (push (cons key string) props))))))
  12816. )
  12817. (when (memq which '(all standard))
  12818. ;; Get the standard properties, like :PROP: ...
  12819. (setq range (org-get-property-block beg end))
  12820. (when range
  12821. (goto-char (car range))
  12822. (while (re-search-forward
  12823. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  12824. (cdr range) t)
  12825. (setq key (org-match-string-no-properties 1)
  12826. value (org-trim (or (org-match-string-no-properties 2) "")))
  12827. (unless (member key excluded)
  12828. (push (cons key (or value "")) props)))))
  12829. (if clocksum
  12830. (push (cons "CLOCKSUM"
  12831. (org-columns-number-to-string (/ (float clocksum) 60.)
  12832. 'add_times))
  12833. props))
  12834. (unless (assoc "CATEGORY" props)
  12835. (push (cons "CATEGORY" (org-get-category)) props))
  12836. (append sum-props (nreverse props)))))))
  12837. (defun org-entry-get (pom property &optional inherit literal-nil)
  12838. "Get value of PROPERTY for entry at point-or-marker POM.
  12839. If INHERIT is non-nil and the entry does not have the property,
  12840. then also check higher levels of the hierarchy.
  12841. If INHERIT is the symbol `selective', use inheritance only if the setting
  12842. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  12843. If the property is present but empty, the return value is the empty string.
  12844. If the property is not present at all, nil is returned.
  12845. If LITERAL-NIL is set, return the string value \"nil\" as a string,
  12846. do not interpret it as the list atom nil. This is used for inheritance
  12847. when a \"nil\" value can supersede a non-nil value higher up the hierarchy."
  12848. (org-with-point-at pom
  12849. (if (and inherit (if (eq inherit 'selective)
  12850. (org-property-inherit-p property)
  12851. t))
  12852. (org-entry-get-with-inheritance property literal-nil)
  12853. (if (member property org-special-properties)
  12854. ;; We need a special property. Use `org-entry-properties' to
  12855. ;; retrieve it, but specify the wanted property
  12856. (cdr (assoc property (org-entry-properties nil 'special property)))
  12857. (let ((range (unless (org-before-first-heading-p)
  12858. (org-get-property-block))))
  12859. (when (and range (goto-char (car range)))
  12860. ((lambda (val) (when val (if literal-nil val (org-not-nil val))))
  12861. (cond
  12862. ((re-search-forward
  12863. (org-re-property property) (cdr range) t)
  12864. (if (match-end 1) (org-match-string-no-properties 1) ""))
  12865. ((re-search-forward
  12866. (org-re-property (concat property "+")) (cdr range) t)
  12867. (cdr (assoc
  12868. property
  12869. (org-update-property-plist
  12870. (concat property "+")
  12871. (if (match-end 1) (org-match-string-no-properties 1) "")
  12872. (list (or (assoc property org-file-properties)
  12873. (assoc property org-global-properties)
  12874. (assoc property org-global-properties-fixed)
  12875. ))))))))))))))
  12876. (defun org-property-or-variable-value (var &optional inherit)
  12877. "Check if there is a property fixing the value of VAR.
  12878. If yes, return this value. If not, return the current value of the variable."
  12879. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  12880. (if (and prop (stringp prop) (string-match "\\S-" prop))
  12881. (read prop)
  12882. (symbol-value var))))
  12883. (defun org-entry-delete (pom property)
  12884. "Delete the property PROPERTY from entry at point-or-marker POM."
  12885. (org-with-point-at pom
  12886. (if (member property org-special-properties)
  12887. nil ; cannot delete these properties.
  12888. (let ((range (org-get-property-block)))
  12889. (if (and range
  12890. (goto-char (car range))
  12891. (re-search-forward
  12892. (org-re-property property)
  12893. (cdr range) t))
  12894. (progn
  12895. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  12896. t)
  12897. nil)))))
  12898. ;; Multi-values properties are properties that contain multiple values
  12899. ;; These values are assumed to be single words, separated by whitespace.
  12900. (defun org-entry-add-to-multivalued-property (pom property value)
  12901. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  12902. (let* ((old (org-entry-get pom property))
  12903. (values (and old (org-split-string old "[ \t]"))))
  12904. (setq value (org-entry-protect-space value))
  12905. (unless (member value values)
  12906. (setq values (cons value values))
  12907. (org-entry-put pom property
  12908. (mapconcat 'identity values " ")))))
  12909. (defun org-entry-remove-from-multivalued-property (pom property value)
  12910. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  12911. (let* ((old (org-entry-get pom property))
  12912. (values (and old (org-split-string old "[ \t]"))))
  12913. (setq value (org-entry-protect-space value))
  12914. (when (member value values)
  12915. (setq values (delete value values))
  12916. (org-entry-put pom property
  12917. (mapconcat 'identity values " ")))))
  12918. (defun org-entry-member-in-multivalued-property (pom property value)
  12919. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  12920. (let* ((old (org-entry-get pom property))
  12921. (values (and old (org-split-string old "[ \t]"))))
  12922. (setq value (org-entry-protect-space value))
  12923. (member value values)))
  12924. (defun org-entry-get-multivalued-property (pom property)
  12925. "Return a list of values in a multivalued property."
  12926. (let* ((value (org-entry-get pom property))
  12927. (values (and value (org-split-string value "[ \t]"))))
  12928. (mapcar 'org-entry-restore-space values)))
  12929. (defun org-entry-put-multivalued-property (pom property &rest values)
  12930. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  12931. VALUES should be a list of strings. Spaces will be protected."
  12932. (org-entry-put pom property
  12933. (mapconcat 'org-entry-protect-space values " "))
  12934. (let* ((value (org-entry-get pom property))
  12935. (values (and value (org-split-string value "[ \t]"))))
  12936. (mapcar 'org-entry-restore-space values)))
  12937. (defun org-entry-protect-space (s)
  12938. "Protect spaces and newline in string S."
  12939. (while (string-match " " s)
  12940. (setq s (replace-match "%20" t t s)))
  12941. (while (string-match "\n" s)
  12942. (setq s (replace-match "%0A" t t s)))
  12943. s)
  12944. (defun org-entry-restore-space (s)
  12945. "Restore spaces and newline in string S."
  12946. (while (string-match "%20" s)
  12947. (setq s (replace-match " " t t s)))
  12948. (while (string-match "%0A" s)
  12949. (setq s (replace-match "\n" t t s)))
  12950. s)
  12951. (defvar org-entry-property-inherited-from (make-marker)
  12952. "Marker pointing to the entry from where a property was inherited.
  12953. Each call to `org-entry-get-with-inheritance' will set this marker to the
  12954. location of the entry where the inheritance search matched. If there was
  12955. no match, the marker will point nowhere.
  12956. Note that also `org-entry-get' calls this function, if the INHERIT flag
  12957. is set.")
  12958. (defun org-entry-get-with-inheritance (property &optional literal-nil)
  12959. "Get entry property, and search higher levels if not present.
  12960. The search will stop at the first ancestor which has the property defined.
  12961. If the value found is \"nil\", return nil to show that the property
  12962. should be considered as undefined (this is the meaning of nil here).
  12963. However, if LITERAL-NIL is set, return the string value \"nil\" instead."
  12964. (move-marker org-entry-property-inherited-from nil)
  12965. (let (tmp)
  12966. (unless (org-before-first-heading-p)
  12967. (save-excursion
  12968. (save-restriction
  12969. (widen)
  12970. (catch 'ex
  12971. (while t
  12972. (when (setq tmp (org-entry-get nil property nil 'literal-nil))
  12973. (org-back-to-heading t)
  12974. (move-marker org-entry-property-inherited-from (point))
  12975. (throw 'ex tmp))
  12976. (or (org-up-heading-safe) (throw 'ex nil)))))))
  12977. (setq tmp (or tmp
  12978. (cdr (assoc property org-file-properties))
  12979. (cdr (assoc property org-global-properties))
  12980. (cdr (assoc property org-global-properties-fixed))))
  12981. (if literal-nil tmp (org-not-nil tmp))))
  12982. (defvar org-property-changed-functions nil
  12983. "Hook called when the value of a property has changed.
  12984. Each hook function should accept two arguments, the name of the property
  12985. and the new value.")
  12986. (defun org-entry-put (pom property value)
  12987. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  12988. (org-with-point-at pom
  12989. (org-back-to-heading t)
  12990. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  12991. range)
  12992. (cond
  12993. ((equal property "TODO")
  12994. (when (and (stringp value) (string-match "\\S-" value)
  12995. (not (member value org-todo-keywords-1)))
  12996. (error "\"%s\" is not a valid TODO state" value))
  12997. (if (or (not value)
  12998. (not (string-match "\\S-" value)))
  12999. (setq value 'none))
  13000. (org-todo value)
  13001. (org-set-tags nil 'align))
  13002. ((equal property "PRIORITY")
  13003. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  13004. (string-to-char value) ?\ ))
  13005. (org-set-tags nil 'align))
  13006. ((equal property "SCHEDULED")
  13007. (if (re-search-forward org-scheduled-time-regexp end t)
  13008. (cond
  13009. ((eq value 'earlier) (org-timestamp-change -1 'day))
  13010. ((eq value 'later) (org-timestamp-change 1 'day))
  13011. (t (call-interactively 'org-schedule)))
  13012. (call-interactively 'org-schedule)))
  13013. ((equal property "DEADLINE")
  13014. (if (re-search-forward org-deadline-time-regexp end t)
  13015. (cond
  13016. ((eq value 'earlier) (org-timestamp-change -1 'day))
  13017. ((eq value 'later) (org-timestamp-change 1 'day))
  13018. (t (call-interactively 'org-deadline)))
  13019. (call-interactively 'org-deadline)))
  13020. ((member property org-special-properties)
  13021. (error "The %s property can not yet be set with `org-entry-put'"
  13022. property))
  13023. (t ; a non-special property
  13024. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  13025. (setq range (org-get-property-block beg end 'force))
  13026. (goto-char (car range))
  13027. (if (re-search-forward
  13028. (org-re-property property) (cdr range) t)
  13029. (progn
  13030. (delete-region (match-beginning 0) (match-end 0))
  13031. (goto-char (match-beginning 0)))
  13032. (goto-char (cdr range))
  13033. (insert "\n")
  13034. (backward-char 1)
  13035. (org-indent-line-function))
  13036. (insert ":" property ":")
  13037. (and value (insert " " value))
  13038. (org-indent-line-function)))))
  13039. (run-hook-with-args 'org-property-changed-functions property value)))
  13040. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  13041. "Get all property keys in the current buffer.
  13042. With INCLUDE-SPECIALS, also list the special properties that reflect things
  13043. like tags and TODO state.
  13044. With INCLUDE-DEFAULTS, also include properties that has special meaning
  13045. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING
  13046. and others.
  13047. With INCLUDE-COLUMNS, also include property names given in COLUMN
  13048. formats in the current buffer."
  13049. (let (rtn range cfmt s p)
  13050. (save-excursion
  13051. (save-restriction
  13052. (widen)
  13053. (goto-char (point-min))
  13054. (while (re-search-forward org-property-start-re nil t)
  13055. (setq range (org-get-property-block))
  13056. (goto-char (car range))
  13057. (while (re-search-forward
  13058. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  13059. (cdr range) t)
  13060. (add-to-list 'rtn (org-match-string-no-properties 1)))
  13061. (outline-next-heading))))
  13062. (when include-specials
  13063. (setq rtn (append org-special-properties rtn)))
  13064. (when include-defaults
  13065. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties)
  13066. (add-to-list 'rtn org-effort-property))
  13067. (when include-columns
  13068. (save-excursion
  13069. (save-restriction
  13070. (widen)
  13071. (goto-char (point-min))
  13072. (while (re-search-forward
  13073. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  13074. nil t)
  13075. (setq cfmt (match-string 2) s 0)
  13076. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  13077. cfmt s)
  13078. (setq s (match-end 0)
  13079. p (match-string 1 cfmt))
  13080. (unless (or (equal p "ITEM")
  13081. (member p org-special-properties))
  13082. (add-to-list 'rtn (match-string 1 cfmt))))))))
  13083. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  13084. (defun org-property-values (key)
  13085. "Return a list of all values of property KEY in the current buffer."
  13086. (save-excursion
  13087. (save-restriction
  13088. (widen)
  13089. (goto-char (point-min))
  13090. (let ((re (org-re-property key))
  13091. values)
  13092. (while (re-search-forward re nil t)
  13093. (add-to-list 'values (org-trim (match-string 1))))
  13094. (delete "" values)))))
  13095. (defun org-insert-property-drawer ()
  13096. "Insert a property drawer into the current entry."
  13097. (interactive)
  13098. (org-back-to-heading t)
  13099. (looking-at org-outline-regexp)
  13100. (let ((indent (if org-adapt-indentation
  13101. (- (match-end 0)(match-beginning 0))
  13102. 0))
  13103. (beg (point))
  13104. (re (concat "^[ \t]*" org-keyword-time-regexp))
  13105. end hiddenp)
  13106. (outline-next-heading)
  13107. (setq end (point))
  13108. (goto-char beg)
  13109. (while (re-search-forward re end t))
  13110. (setq hiddenp (outline-invisible-p))
  13111. (end-of-line 1)
  13112. (and (equal (char-after) ?\n) (forward-char 1))
  13113. (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
  13114. (if (member (match-string 1) '("CLOCK:" ":END:"))
  13115. ;; just skip this line
  13116. (beginning-of-line 2)
  13117. ;; Drawer start, find the end
  13118. (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
  13119. (beginning-of-line 1)))
  13120. (org-skip-over-state-notes)
  13121. (skip-chars-backward " \t\n\r")
  13122. (if (eq (char-before) ?*) (forward-char 1))
  13123. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  13124. (beginning-of-line 0)
  13125. (org-indent-to-column indent)
  13126. (beginning-of-line 2)
  13127. (org-indent-to-column indent)
  13128. (beginning-of-line 0)
  13129. (if hiddenp
  13130. (save-excursion
  13131. (org-back-to-heading t)
  13132. (hide-entry))
  13133. (org-flag-drawer t))))
  13134. (defvar org-property-set-functions-alist nil
  13135. "Property set function alist.
  13136. Each entry should have the following format:
  13137. (PROPERTY . READ-FUNCTION)
  13138. The read function will be called with the same argument as
  13139. `org-completing-read'.")
  13140. (defun org-set-property-function (property)
  13141. "Get the function that should be used to set PROPERTY.
  13142. This is computed according to `org-property-set-functions-alist'."
  13143. (or (cdr (assoc property org-property-set-functions-alist))
  13144. 'org-completing-read))
  13145. (defun org-read-property-value (property)
  13146. "Read PROPERTY value from user."
  13147. (let* ((completion-ignore-case t)
  13148. (allowed (org-property-get-allowed-values nil property 'table))
  13149. (cur (org-entry-get nil property))
  13150. (prompt (concat property " value"
  13151. (if (and cur (string-match "\\S-" cur))
  13152. (concat " [" cur "]") "") ": "))
  13153. (set-function (org-set-property-function property))
  13154. (val (if allowed
  13155. (funcall set-function prompt allowed nil
  13156. (not (get-text-property 0 'org-unrestricted
  13157. (caar allowed))))
  13158. (let (org-completion-use-ido org-completion-use-iswitchb)
  13159. (funcall set-function prompt
  13160. (mapcar 'list (org-property-values property))
  13161. nil nil "" nil cur)))))
  13162. (if (equal val "")
  13163. cur
  13164. val)))
  13165. (defvar org-last-set-property nil)
  13166. (defun org-read-property-name ()
  13167. "Read a property name."
  13168. (let* ((completion-ignore-case t)
  13169. (keys (org-buffer-property-keys nil t t))
  13170. (default-prop (or (save-excursion
  13171. (save-match-data
  13172. (beginning-of-line)
  13173. (and (looking-at "^\\s-*:\\([^:\n]+\\):")
  13174. (null (string= (match-string 1) "END"))
  13175. (match-string 1))))
  13176. org-last-set-property))
  13177. (property (org-icompleting-read
  13178. (concat "Property"
  13179. (if default-prop (concat " [" default-prop "]") "")
  13180. ": ")
  13181. (mapcar 'list keys)
  13182. nil nil nil nil
  13183. default-prop
  13184. )))
  13185. (if (member property keys)
  13186. property
  13187. (or (cdr (assoc (downcase property)
  13188. (mapcar (lambda (x) (cons (downcase x) x))
  13189. keys)))
  13190. property))))
  13191. (defun org-set-property (property value)
  13192. "In the current entry, set PROPERTY to VALUE.
  13193. When called interactively, this will prompt for a property name, offering
  13194. completion on existing and default properties. And then it will prompt
  13195. for a value, offering completion either on allowed values (via an inherited
  13196. xxx_ALL property) or on existing values in other instances of this property
  13197. in the current file."
  13198. (interactive (list nil nil))
  13199. (let* ((property (or property (org-read-property-name)))
  13200. (value (or value (org-read-property-value property)))
  13201. (fn (assoc property org-properties-postprocess-alist)))
  13202. (setq org-last-set-property property)
  13203. ;; Possibly postprocess the inserted value:
  13204. (when fn (setq value (funcall (cadr fn) value)))
  13205. (unless (equal (org-entry-get nil property) value)
  13206. (org-entry-put nil property value))))
  13207. (defun org-delete-property (property)
  13208. "In the current entry, delete PROPERTY."
  13209. (interactive
  13210. (let* ((completion-ignore-case t)
  13211. (prop (org-icompleting-read "Property: "
  13212. (org-entry-properties nil 'standard))))
  13213. (list prop)))
  13214. (message "Property %s %s" property
  13215. (if (org-entry-delete nil property)
  13216. "deleted"
  13217. "was not present in the entry")))
  13218. (defun org-delete-property-globally (property)
  13219. "Remove PROPERTY globally, from all entries."
  13220. (interactive
  13221. (let* ((completion-ignore-case t)
  13222. (prop (org-icompleting-read
  13223. "Globally remove property: "
  13224. (mapcar 'list (org-buffer-property-keys)))))
  13225. (list prop)))
  13226. (save-excursion
  13227. (save-restriction
  13228. (widen)
  13229. (goto-char (point-min))
  13230. (let ((cnt 0))
  13231. (while (re-search-forward
  13232. (org-re-property property)
  13233. nil t)
  13234. (setq cnt (1+ cnt))
  13235. (replace-match ""))
  13236. (message "Property \"%s\" removed from %d entries" property cnt)))))
  13237. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  13238. (defun org-compute-property-at-point ()
  13239. "Compute the property at point.
  13240. This looks for an enclosing column format, extracts the operator and
  13241. then applies it to the property in the column format's scope."
  13242. (interactive)
  13243. (unless (org-at-property-p)
  13244. (error "Not at a property"))
  13245. (let ((prop (org-match-string-no-properties 2)))
  13246. (org-columns-get-format-and-top-level)
  13247. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  13248. (error "No operator defined for property %s" prop))
  13249. (org-columns-compute prop)))
  13250. (defvar org-property-allowed-value-functions nil
  13251. "Hook for functions supplying allowed values for a specific property.
  13252. The functions must take a single argument, the name of the property, and
  13253. return a flat list of allowed values. If \":ETC\" is one of
  13254. the values, this means that these values are intended as defaults for
  13255. completion, but that other values should be allowed too.
  13256. The functions must return nil if they are not responsible for this
  13257. property.")
  13258. (defun org-property-get-allowed-values (pom property &optional table)
  13259. "Get allowed values for the property PROPERTY.
  13260. When TABLE is non-nil, return an alist that can directly be used for
  13261. completion."
  13262. (let (vals)
  13263. (cond
  13264. ((equal property "TODO")
  13265. (setq vals (org-with-point-at pom
  13266. (append org-todo-keywords-1 '("")))))
  13267. ((equal property "PRIORITY")
  13268. (let ((n org-lowest-priority))
  13269. (while (>= n org-highest-priority)
  13270. (push (char-to-string n) vals)
  13271. (setq n (1- n)))))
  13272. ((member property org-special-properties))
  13273. ((setq vals (run-hook-with-args-until-success
  13274. 'org-property-allowed-value-functions property)))
  13275. (t
  13276. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  13277. (when (and vals (string-match "\\S-" vals))
  13278. (setq vals (car (read-from-string (concat "(" vals ")"))))
  13279. (setq vals (mapcar (lambda (x)
  13280. (cond ((stringp x) x)
  13281. ((numberp x) (number-to-string x))
  13282. ((symbolp x) (symbol-name x))
  13283. (t "???")))
  13284. vals)))))
  13285. (when (member ":ETC" vals)
  13286. (setq vals (remove ":ETC" vals))
  13287. (org-add-props (car vals) '(org-unrestricted t)))
  13288. (if table (mapcar 'list vals) vals)))
  13289. (defun org-property-previous-allowed-value (&optional previous)
  13290. "Switch to the next allowed value for this property."
  13291. (interactive)
  13292. (org-property-next-allowed-value t))
  13293. (defun org-property-next-allowed-value (&optional previous)
  13294. "Switch to the next allowed value for this property."
  13295. (interactive)
  13296. (unless (org-at-property-p)
  13297. (error "Not at a property"))
  13298. (let* ((key (match-string 2))
  13299. (value (match-string 3))
  13300. (allowed (or (org-property-get-allowed-values (point) key)
  13301. (and (member value '("[ ]" "[-]" "[X]"))
  13302. '("[ ]" "[X]"))))
  13303. nval)
  13304. (unless allowed
  13305. (error "Allowed values for this property have not been defined"))
  13306. (if previous (setq allowed (reverse allowed)))
  13307. (if (member value allowed)
  13308. (setq nval (car (cdr (member value allowed)))))
  13309. (setq nval (or nval (car allowed)))
  13310. (if (equal nval value)
  13311. (error "Only one allowed value for this property"))
  13312. (org-at-property-p)
  13313. (replace-match (concat " :" key ": " nval) t t)
  13314. (org-indent-line-function)
  13315. (beginning-of-line 1)
  13316. (skip-chars-forward " \t")
  13317. (run-hook-with-args 'org-property-changed-functions key nval)))
  13318. (defun org-find-olp (path &optional this-buffer)
  13319. "Return a marker pointing to the entry at outline path OLP.
  13320. If anything goes wrong, throw an error.
  13321. You can wrap this call to catch the error like this:
  13322. (condition-case msg
  13323. (org-mobile-locate-entry (match-string 4))
  13324. (error (nth 1 msg)))
  13325. The return value will then be either a string with the error message,
  13326. or a marker if everything is OK.
  13327. If THIS-BUFFER is set, the outline path does not contain a file,
  13328. only headings."
  13329. (let* ((file (if this-buffer buffer-file-name (pop path)))
  13330. (buffer (if this-buffer (current-buffer) (find-file-noselect file)))
  13331. (level 1)
  13332. (lmin 1)
  13333. (lmax 1)
  13334. limit re end found pos heading cnt flevel)
  13335. (unless buffer (error "File not found :%s" file))
  13336. (with-current-buffer buffer
  13337. (save-excursion
  13338. (save-restriction
  13339. (widen)
  13340. (setq limit (point-max))
  13341. (goto-char (point-min))
  13342. (while (setq heading (pop path))
  13343. (setq re (format org-complex-heading-regexp-format
  13344. (regexp-quote heading)))
  13345. (setq cnt 0 pos (point))
  13346. (while (re-search-forward re end t)
  13347. (setq level (- (match-end 1) (match-beginning 1)))
  13348. (if (and (>= level lmin) (<= level lmax))
  13349. (setq found (match-beginning 0) flevel level cnt (1+ cnt))))
  13350. (when (= cnt 0) (error "Heading not found on level %d: %s"
  13351. lmax heading))
  13352. (when (> cnt 1) (error "Heading not unique on level %d: %s"
  13353. lmax heading))
  13354. (goto-char found)
  13355. (setq lmin (1+ flevel) lmax (+ lmin (if org-odd-levels-only 1 0)))
  13356. (setq end (save-excursion (org-end-of-subtree t t))))
  13357. (when (org-on-heading-p)
  13358. (move-marker (make-marker) (point))))))))
  13359. (defun org-find-exact-headline-in-buffer (heading &optional buffer pos-only)
  13360. "Find node HEADING in BUFFER.
  13361. Return a marker to the heading if it was found, or nil if not.
  13362. If POS-ONLY is set, return just the position instead of a marker.
  13363. The heading text must match exact, but it may have a TODO keyword,
  13364. a priority cookie and tags in the standard locations."
  13365. (with-current-buffer (or buffer (current-buffer))
  13366. (save-excursion
  13367. (save-restriction
  13368. (widen)
  13369. (goto-char (point-min))
  13370. (let (case-fold-search)
  13371. (if (re-search-forward
  13372. (format org-complex-heading-regexp-format
  13373. (regexp-quote heading)) nil t)
  13374. (if pos-only
  13375. (match-beginning 0)
  13376. (move-marker (make-marker) (match-beginning 0)))))))))
  13377. (defun org-find-exact-heading-in-directory (heading &optional dir)
  13378. "Find Org node headline HEADING in all .org files in directory DIR.
  13379. When the target headline is found, return a marker to this location."
  13380. (let ((files (directory-files (or dir default-directory)
  13381. nil "\\`[^.#].*\\.org\\'"))
  13382. file visiting m buffer)
  13383. (catch 'found
  13384. (while (setq file (pop files))
  13385. (message "trying %s" file)
  13386. (setq visiting (org-find-base-buffer-visiting file))
  13387. (setq buffer (or visiting (find-file-noselect file)))
  13388. (setq m (org-find-exact-headline-in-buffer
  13389. heading buffer))
  13390. (when (and (not m) (not visiting)) (kill-buffer buffer))
  13391. (and m (throw 'found m))))))
  13392. (defun org-find-entry-with-id (ident)
  13393. "Locate the entry that contains the ID property with exact value IDENT.
  13394. IDENT can be a string, a symbol or a number, this function will search for
  13395. the string representation of it.
  13396. Return the position where this entry starts, or nil if there is no such entry."
  13397. (interactive "sID: ")
  13398. (let ((id (cond
  13399. ((stringp ident) ident)
  13400. ((symbol-name ident) (symbol-name ident))
  13401. ((numberp ident) (number-to-string ident))
  13402. (t (error "IDENT %s must be a string, symbol or number" ident))))
  13403. (case-fold-search nil))
  13404. (save-excursion
  13405. (save-restriction
  13406. (widen)
  13407. (goto-char (point-min))
  13408. (when (re-search-forward
  13409. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  13410. nil t)
  13411. (org-back-to-heading t)
  13412. (point))))))
  13413. ;;;; Timestamps
  13414. (defvar org-last-changed-timestamp nil)
  13415. (defvar org-last-inserted-timestamp nil
  13416. "The last time stamp inserted with `org-insert-time-stamp'.")
  13417. (defvar org-time-was-given) ; dynamically scoped parameter
  13418. (defvar org-end-time-was-given) ; dynamically scoped parameter
  13419. (defvar org-ts-what) ; dynamically scoped parameter
  13420. (defun org-time-stamp (arg &optional inactive)
  13421. "Prompt for a date/time and insert a time stamp.
  13422. If the user specifies a time like HH:MM, or if this command is called
  13423. with a prefix argument, the time stamp will contain date and time.
  13424. Otherwise, only the date will be included. All parts of a date not
  13425. specified by the user will be filled in from the current date/time.
  13426. So if you press just return without typing anything, the time stamp
  13427. will represent the current date/time. If there is already a timestamp
  13428. at the cursor, it will be modified."
  13429. (interactive "P")
  13430. (let* ((ts nil)
  13431. (default-time
  13432. ;; Default time is either today, or, when entering a range,
  13433. ;; the range start.
  13434. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  13435. (save-excursion
  13436. (re-search-backward
  13437. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  13438. (- (point) 20) t)))
  13439. (apply 'encode-time (org-parse-time-string (match-string 1)))
  13440. (current-time)))
  13441. (default-input (and ts (org-get-compact-tod ts)))
  13442. (repeater (save-excursion
  13443. (save-match-data
  13444. (beginning-of-line)
  13445. (when (re-search-forward
  13446. "\\([.+-]+[0-9]+[dwmy] ?\\)+" ;;\\(?:[/ ][-+]?[0-9]+[dwmy]\\)?\\) ?"
  13447. (save-excursion (progn (end-of-line) (point))) t)
  13448. (match-string 0)))))
  13449. org-time-was-given org-end-time-was-given time)
  13450. (cond
  13451. ((and (org-at-timestamp-p t)
  13452. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  13453. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  13454. (insert "--")
  13455. (setq time (let ((this-command this-command))
  13456. (org-read-date arg 'totime nil nil
  13457. default-time default-input)))
  13458. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  13459. ((org-at-timestamp-p t)
  13460. (setq time (let ((this-command this-command))
  13461. (org-read-date arg 'totime nil nil default-time default-input)))
  13462. (when (org-at-timestamp-p t) ; just to get the match data
  13463. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  13464. (replace-match "")
  13465. (setq org-last-changed-timestamp
  13466. (org-insert-time-stamp
  13467. time (or org-time-was-given arg)
  13468. inactive nil nil (list org-end-time-was-given)))
  13469. (when repeater (goto-char (1- (point))) (insert " " repeater)
  13470. (setq org-last-changed-timestamp
  13471. (concat (substring org-last-inserted-timestamp 0 -1)
  13472. " " repeater ">"))))
  13473. (message "Timestamp updated"))
  13474. (t
  13475. (setq time (let ((this-command this-command))
  13476. (org-read-date arg 'totime nil nil default-time default-input)))
  13477. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  13478. nil nil (list org-end-time-was-given))))))
  13479. ;; FIXME: can we use this for something else, like computing time differences?
  13480. (defun org-get-compact-tod (s)
  13481. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  13482. (let* ((t1 (match-string 1 s))
  13483. (h1 (string-to-number (match-string 2 s)))
  13484. (m1 (string-to-number (match-string 3 s)))
  13485. (t2 (and (match-end 4) (match-string 5 s)))
  13486. (h2 (and t2 (string-to-number (match-string 6 s))))
  13487. (m2 (and t2 (string-to-number (match-string 7 s))))
  13488. dh dm)
  13489. (if (not t2)
  13490. t1
  13491. (setq dh (- h2 h1) dm (- m2 m1))
  13492. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  13493. (concat t1 "+" (number-to-string dh)
  13494. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  13495. (defun org-time-stamp-inactive (&optional arg)
  13496. "Insert an inactive time stamp.
  13497. An inactive time stamp is enclosed in square brackets instead of angle
  13498. brackets. It is inactive in the sense that it does not trigger agenda entries,
  13499. does not link to the calendar and cannot be changed with the S-cursor keys.
  13500. So these are more for recording a certain time/date."
  13501. (interactive "P")
  13502. (org-time-stamp arg 'inactive))
  13503. (defvar org-date-ovl (make-overlay 1 1))
  13504. (overlay-put org-date-ovl 'face 'org-warning)
  13505. (org-detach-overlay org-date-ovl)
  13506. (defvar org-ans1) ; dynamically scoped parameter
  13507. (defvar org-ans2) ; dynamically scoped parameter
  13508. (defvar org-plain-time-of-day-regexp) ; defined below
  13509. (defvar org-overriding-default-time nil) ; dynamically scoped
  13510. (defvar org-read-date-overlay nil)
  13511. (defvar org-dcst nil) ; dynamically scoped
  13512. (defvar org-read-date-history nil)
  13513. (defvar org-read-date-final-answer nil)
  13514. (defvar org-read-date-analyze-futurep nil)
  13515. (defvar org-read-date-analyze-forced-year nil)
  13516. (defun org-read-date (&optional with-time to-time from-string prompt
  13517. default-time default-input)
  13518. "Read a date, possibly a time, and make things smooth for the user.
  13519. The prompt will suggest to enter an ISO date, but you can also enter anything
  13520. which will at least partially be understood by `parse-time-string'.
  13521. Unrecognized parts of the date will default to the current day, month, year,
  13522. hour and minute. If this command is called to replace a timestamp at point,
  13523. of to enter the second timestamp of a range, the default time is taken
  13524. from the existing stamp. Furthermore, the command prefers the future,
  13525. so if you are giving a date where the year is not given, and the day-month
  13526. combination is already past in the current year, it will assume you
  13527. mean next year. For details, see the manual. A few examples:
  13528. 3-2-5 --> 2003-02-05
  13529. feb 15 --> currentyear-02-15
  13530. 2/15 --> currentyear-02-15
  13531. sep 12 9 --> 2009-09-12
  13532. 12:45 --> today 12:45
  13533. 22 sept 0:34 --> currentyear-09-22 0:34
  13534. 12 --> currentyear-currentmonth-12
  13535. Fri --> nearest Friday (today or later)
  13536. etc.
  13537. Furthermore you can specify a relative date by giving, as the *first* thing
  13538. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  13539. change in days weeks, months, years.
  13540. With a single plus or minus, the date is relative to today. With a double
  13541. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  13542. +4d --> four days from today
  13543. +4 --> same as above
  13544. +2w --> two weeks from today
  13545. ++5 --> five days from default date
  13546. The function understands only English month and weekday abbreviations,
  13547. but this can be configured with the variables `parse-time-months' and
  13548. `parse-time-weekdays'.
  13549. While prompting, a calendar is popped up - you can also select the
  13550. date with the mouse (button 1). The calendar shows a period of three
  13551. months. To scroll it to other months, use the keys `>' and `<'.
  13552. If you don't like the calendar, turn it off with
  13553. \(setq org-read-date-popup-calendar nil)
  13554. With optional argument TO-TIME, the date will immediately be converted
  13555. to an internal time.
  13556. With an optional argument WITH-TIME, the prompt will suggest to also
  13557. insert a time. Note that when WITH-TIME is not set, you can still
  13558. enter a time, and this function will inform the calling routine about
  13559. this change. The calling routine may then choose to change the format
  13560. used to insert the time stamp into the buffer to include the time.
  13561. With optional argument FROM-STRING, read from this string instead from
  13562. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  13563. the time/date that is used for everything that is not specified by the
  13564. user."
  13565. (require 'parse-time)
  13566. (let* ((org-time-stamp-rounding-minutes
  13567. (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  13568. (org-dcst org-display-custom-times)
  13569. (ct (org-current-time))
  13570. (def (or org-overriding-default-time default-time ct))
  13571. (defdecode (decode-time def))
  13572. (dummy (progn
  13573. (when (< (nth 2 defdecode) org-extend-today-until)
  13574. (setcar (nthcdr 2 defdecode) -1)
  13575. (setcar (nthcdr 1 defdecode) 59)
  13576. (setq def (apply 'encode-time defdecode)
  13577. defdecode (decode-time def)))))
  13578. (calendar-frame-setup nil)
  13579. (calendar-setup nil)
  13580. (calendar-move-hook nil)
  13581. (calendar-view-diary-initially-flag nil)
  13582. (calendar-view-holidays-initially-flag nil)
  13583. (timestr (format-time-string
  13584. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  13585. (prompt (concat (if prompt (concat prompt " ") "")
  13586. (format "Date+time [%s]: " timestr)))
  13587. ans (org-ans0 "") org-ans1 org-ans2 final)
  13588. (cond
  13589. (from-string (setq ans from-string))
  13590. (org-read-date-popup-calendar
  13591. (save-excursion
  13592. (save-window-excursion
  13593. (calendar)
  13594. (unwind-protect
  13595. (progn
  13596. (calendar-forward-day (- (time-to-days def)
  13597. (calendar-absolute-from-gregorian
  13598. (calendar-current-date))))
  13599. (org-eval-in-calendar nil t)
  13600. (let* ((old-map (current-local-map))
  13601. (map (copy-keymap calendar-mode-map))
  13602. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  13603. (org-defkey map (kbd "RET") 'org-calendar-select)
  13604. (org-defkey map [mouse-1] 'org-calendar-select-mouse)
  13605. (org-defkey map [mouse-2] 'org-calendar-select-mouse)
  13606. (org-defkey minibuffer-local-map [(meta shift left)]
  13607. (lambda () (interactive)
  13608. (org-eval-in-calendar '(calendar-backward-month 1))))
  13609. (org-defkey minibuffer-local-map [(meta shift right)]
  13610. (lambda () (interactive)
  13611. (org-eval-in-calendar '(calendar-forward-month 1))))
  13612. (org-defkey minibuffer-local-map [(meta shift up)]
  13613. (lambda () (interactive)
  13614. (org-eval-in-calendar '(calendar-backward-year 1))))
  13615. (org-defkey minibuffer-local-map [(meta shift down)]
  13616. (lambda () (interactive)
  13617. (org-eval-in-calendar '(calendar-forward-year 1))))
  13618. (org-defkey minibuffer-local-map [?\e (shift left)]
  13619. (lambda () (interactive)
  13620. (org-eval-in-calendar '(calendar-backward-month 1))))
  13621. (org-defkey minibuffer-local-map [?\e (shift right)]
  13622. (lambda () (interactive)
  13623. (org-eval-in-calendar '(calendar-forward-month 1))))
  13624. (org-defkey minibuffer-local-map [?\e (shift up)]
  13625. (lambda () (interactive)
  13626. (org-eval-in-calendar '(calendar-backward-year 1))))
  13627. (org-defkey minibuffer-local-map [?\e (shift down)]
  13628. (lambda () (interactive)
  13629. (org-eval-in-calendar '(calendar-forward-year 1))))
  13630. (org-defkey minibuffer-local-map [(shift up)]
  13631. (lambda () (interactive)
  13632. (org-eval-in-calendar '(calendar-backward-week 1))))
  13633. (org-defkey minibuffer-local-map [(shift down)]
  13634. (lambda () (interactive)
  13635. (org-eval-in-calendar '(calendar-forward-week 1))))
  13636. (org-defkey minibuffer-local-map [(shift left)]
  13637. (lambda () (interactive)
  13638. (org-eval-in-calendar '(calendar-backward-day 1))))
  13639. (org-defkey minibuffer-local-map [(shift right)]
  13640. (lambda () (interactive)
  13641. (org-eval-in-calendar '(calendar-forward-day 1))))
  13642. (org-defkey minibuffer-local-map ">"
  13643. (lambda () (interactive)
  13644. (org-eval-in-calendar '(scroll-calendar-left 1))))
  13645. (org-defkey minibuffer-local-map "<"
  13646. (lambda () (interactive)
  13647. (org-eval-in-calendar '(scroll-calendar-right 1))))
  13648. (org-defkey minibuffer-local-map "\C-v"
  13649. (lambda () (interactive)
  13650. (org-eval-in-calendar
  13651. '(calendar-scroll-left-three-months 1))))
  13652. (org-defkey minibuffer-local-map "\M-v"
  13653. (lambda () (interactive)
  13654. (org-eval-in-calendar
  13655. '(calendar-scroll-right-three-months 1))))
  13656. (run-hooks 'org-read-date-minibuffer-setup-hook)
  13657. (unwind-protect
  13658. (progn
  13659. (use-local-map map)
  13660. (add-hook 'post-command-hook 'org-read-date-display)
  13661. (setq org-ans0 (read-string prompt default-input
  13662. 'org-read-date-history nil))
  13663. ;; org-ans0: from prompt
  13664. ;; org-ans1: from mouse click
  13665. ;; org-ans2: from calendar motion
  13666. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  13667. (remove-hook 'post-command-hook 'org-read-date-display)
  13668. (use-local-map old-map)
  13669. (when org-read-date-overlay
  13670. (delete-overlay org-read-date-overlay)
  13671. (setq org-read-date-overlay nil)))))
  13672. (bury-buffer "*Calendar*")))))
  13673. (t ; Naked prompt only
  13674. (unwind-protect
  13675. (setq ans (read-string prompt default-input
  13676. 'org-read-date-history timestr))
  13677. (when org-read-date-overlay
  13678. (delete-overlay org-read-date-overlay)
  13679. (setq org-read-date-overlay nil)))))
  13680. (setq final (org-read-date-analyze ans def defdecode))
  13681. (when org-read-date-analyze-forced-year
  13682. (message "Year was forced into %s"
  13683. (if org-read-date-force-compatible-dates
  13684. "compatible range (1970-2037)"
  13685. "range representable on this machine"))
  13686. (ding))
  13687. ;; One round trip to get rid of 34th of August and stuff like that....
  13688. (setq final (decode-time (apply 'encode-time final)))
  13689. (setq org-read-date-final-answer ans)
  13690. (if to-time
  13691. (apply 'encode-time final)
  13692. (if (and (boundp 'org-time-was-given) org-time-was-given)
  13693. (format "%04d-%02d-%02d %02d:%02d"
  13694. (nth 5 final) (nth 4 final) (nth 3 final)
  13695. (nth 2 final) (nth 1 final))
  13696. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  13697. (defvar def)
  13698. (defvar defdecode)
  13699. (defvar with-time)
  13700. (defun org-read-date-display ()
  13701. "Display the current date prompt interpretation in the minibuffer."
  13702. (when org-read-date-display-live
  13703. (when org-read-date-overlay
  13704. (delete-overlay org-read-date-overlay))
  13705. (let ((p (point)))
  13706. (end-of-line 1)
  13707. (while (not (equal (buffer-substring
  13708. (max (point-min) (- (point) 4)) (point))
  13709. " "))
  13710. (insert " "))
  13711. (goto-char p))
  13712. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  13713. " " (or org-ans1 org-ans2)))
  13714. (org-end-time-was-given nil)
  13715. (f (org-read-date-analyze ans def defdecode))
  13716. (fmts (if org-dcst
  13717. org-time-stamp-custom-formats
  13718. org-time-stamp-formats))
  13719. (fmt (if (or with-time
  13720. (and (boundp 'org-time-was-given) org-time-was-given))
  13721. (cdr fmts)
  13722. (car fmts)))
  13723. (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
  13724. (when (and org-end-time-was-given
  13725. (string-match org-plain-time-of-day-regexp txt))
  13726. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  13727. org-end-time-was-given
  13728. (substring txt (match-end 0)))))
  13729. (when org-read-date-analyze-futurep
  13730. (setq txt (concat txt " (=>F)")))
  13731. (setq org-read-date-overlay
  13732. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  13733. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  13734. (defun org-read-date-analyze (ans def defdecode)
  13735. "Analyze the combined answer of the date prompt."
  13736. ;; FIXME: cleanup and comment
  13737. (let ((nowdecode (decode-time (current-time)))
  13738. delta deltan deltaw deltadef year month day
  13739. hour minute second wday pm h2 m2 tl wday1
  13740. iso-year iso-weekday iso-week iso-year iso-date futurep kill-year)
  13741. (setq org-read-date-analyze-futurep nil
  13742. org-read-date-analyze-forced-year nil)
  13743. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  13744. (setq ans "+0"))
  13745. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  13746. (setq ans (replace-match "" t t ans)
  13747. deltan (car delta)
  13748. deltaw (nth 1 delta)
  13749. deltadef (nth 2 delta)))
  13750. ;; Check if there is an iso week date in there
  13751. ;; If yes, store the info and postpone interpreting it until the rest
  13752. ;; of the parsing is done
  13753. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  13754. (setq iso-year (if (match-end 1)
  13755. (org-small-year-to-year
  13756. (string-to-number (match-string 1 ans))))
  13757. iso-weekday (if (match-end 3)
  13758. (string-to-number (match-string 3 ans)))
  13759. iso-week (string-to-number (match-string 2 ans)))
  13760. (setq ans (replace-match "" t t ans)))
  13761. ;; Help matching ISO dates with single digit month or day, like 2006-8-11.
  13762. (when (string-match
  13763. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  13764. (setq year (if (match-end 2)
  13765. (string-to-number (match-string 2 ans))
  13766. (progn (setq kill-year t)
  13767. (string-to-number (format-time-string "%Y"))))
  13768. month (string-to-number (match-string 3 ans))
  13769. day (string-to-number (match-string 4 ans)))
  13770. (if (< year 100) (setq year (+ 2000 year)))
  13771. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  13772. t nil ans)))
  13773. ;; Help matching dottet european dates
  13774. (when (string-match
  13775. "^ *\\(3[01]\\|0?[1-9]\\|[12][0-9]\\)\\. ?\\(0?[1-9]\\|1[012]\\)\\. ?\\([1-9][0-9][0-9][0-9]\\)?" ans)
  13776. (setq year (if (match-end 3)
  13777. (string-to-number (match-string 3 ans))
  13778. (progn (setq kill-year t)
  13779. (string-to-number (format-time-string "%Y"))))
  13780. day (string-to-number (match-string 1 ans))
  13781. month (string-to-number (match-string 2 ans))
  13782. ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  13783. t nil ans)))
  13784. ;; Help matching american dates, like 5/30 or 5/30/7
  13785. (when (string-match
  13786. "^ *\\(0?[1-9]\\|1[012]\\)/\\(0?[1-9]\\|[12][0-9]\\|3[01]\\)\\(/\\([0-9]+\\)\\)?\\([^/0-9]\\|$\\)" ans)
  13787. (setq year (if (match-end 4)
  13788. (string-to-number (match-string 4 ans))
  13789. (progn (setq kill-year t)
  13790. (string-to-number (format-time-string "%Y"))))
  13791. month (string-to-number (match-string 1 ans))
  13792. day (string-to-number (match-string 2 ans)))
  13793. (if (< year 100) (setq year (+ 2000 year)))
  13794. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  13795. t nil ans)))
  13796. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  13797. ;; If there is a time with am/pm, and *no* time without it, we convert
  13798. ;; so that matching will be successful.
  13799. (loop for i from 1 to 2 do ; twice, for end time as well
  13800. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  13801. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  13802. (setq hour (string-to-number (match-string 1 ans))
  13803. minute (if (match-end 3)
  13804. (string-to-number (match-string 3 ans))
  13805. 0)
  13806. pm (equal ?p
  13807. (string-to-char (downcase (match-string 4 ans)))))
  13808. (if (and (= hour 12) (not pm))
  13809. (setq hour 0)
  13810. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  13811. (setq ans (replace-match (format "%02d:%02d" hour minute)
  13812. t t ans))))
  13813. ;; Check if a time range is given as a duration
  13814. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  13815. (setq hour (string-to-number (match-string 1 ans))
  13816. h2 (+ hour (string-to-number (match-string 3 ans)))
  13817. minute (string-to-number (match-string 2 ans))
  13818. m2 (+ minute (if (match-end 5) (string-to-number
  13819. (match-string 5 ans))0)))
  13820. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  13821. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  13822. t t ans)))
  13823. ;; Check if there is a time range
  13824. (when (boundp 'org-end-time-was-given)
  13825. (setq org-time-was-given nil)
  13826. (when (and (string-match org-plain-time-of-day-regexp ans)
  13827. (match-end 8))
  13828. (setq org-end-time-was-given (match-string 8 ans))
  13829. (setq ans (concat (substring ans 0 (match-beginning 7))
  13830. (substring ans (match-end 7))))))
  13831. (setq tl (parse-time-string ans)
  13832. day (or (nth 3 tl) (nth 3 defdecode))
  13833. month (or (nth 4 tl)
  13834. (if (and org-read-date-prefer-future
  13835. (nth 3 tl) (< (nth 3 tl) (nth 3 nowdecode)))
  13836. (prog1 (1+ (nth 4 nowdecode)) (setq futurep t))
  13837. (nth 4 defdecode)))
  13838. year (or (and (not kill-year) (nth 5 tl))
  13839. (if (and org-read-date-prefer-future
  13840. (nth 4 tl) (< (nth 4 tl) (nth 4 nowdecode)))
  13841. (prog1 (1+ (nth 5 nowdecode)) (setq futurep t))
  13842. (nth 5 defdecode)))
  13843. hour (or (nth 2 tl) (nth 2 defdecode))
  13844. minute (or (nth 1 tl) (nth 1 defdecode))
  13845. second (or (nth 0 tl) 0)
  13846. wday (nth 6 tl))
  13847. (when (and (eq org-read-date-prefer-future 'time)
  13848. (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
  13849. (equal day (nth 3 nowdecode))
  13850. (equal month (nth 4 nowdecode))
  13851. (equal year (nth 5 nowdecode))
  13852. (nth 2 tl)
  13853. (or (< (nth 2 tl) (nth 2 nowdecode))
  13854. (and (= (nth 2 tl) (nth 2 nowdecode))
  13855. (nth 1 tl)
  13856. (< (nth 1 tl) (nth 1 nowdecode)))))
  13857. (setq day (1+ day)
  13858. futurep t))
  13859. ;; Special date definitions below
  13860. (cond
  13861. (iso-week
  13862. ;; There was an iso week
  13863. (require 'cal-iso)
  13864. (setq futurep nil)
  13865. (setq year (or iso-year year)
  13866. day (or iso-weekday wday 1)
  13867. wday nil ; to make sure that the trigger below does not match
  13868. iso-date (calendar-gregorian-from-absolute
  13869. (calendar-absolute-from-iso
  13870. (list iso-week day year))))
  13871. ; FIXME: Should we also push ISO weeks into the future?
  13872. ; (when (and org-read-date-prefer-future
  13873. ; (not iso-year)
  13874. ; (< (calendar-absolute-from-gregorian iso-date)
  13875. ; (time-to-days (current-time))))
  13876. ; (setq year (1+ year)
  13877. ; iso-date (calendar-gregorian-from-absolute
  13878. ; (calendar-absolute-from-iso
  13879. ; (list iso-week day year)))))
  13880. (setq month (car iso-date)
  13881. year (nth 2 iso-date)
  13882. day (nth 1 iso-date)))
  13883. (deltan
  13884. (setq futurep nil)
  13885. (unless deltadef
  13886. (let ((now (decode-time (current-time))))
  13887. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  13888. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  13889. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  13890. ((equal deltaw "m") (setq month (+ month deltan)))
  13891. ((equal deltaw "y") (setq year (+ year deltan)))))
  13892. ((and wday (not (nth 3 tl)))
  13893. (setq futurep nil)
  13894. ;; Weekday was given, but no day, so pick that day in the week
  13895. ;; on or after the derived date.
  13896. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  13897. (unless (equal wday wday1)
  13898. (setq day (+ day (% (- wday wday1 -7) 7))))))
  13899. (if (and (boundp 'org-time-was-given)
  13900. (nth 2 tl))
  13901. (setq org-time-was-given t))
  13902. (if (< year 100) (setq year (+ 2000 year)))
  13903. ;; Check of the date is representable
  13904. (if org-read-date-force-compatible-dates
  13905. (progn
  13906. (if (< year 1970)
  13907. (setq year 1970 org-read-date-analyze-forced-year t))
  13908. (if (> year 2037)
  13909. (setq year 2037 org-read-date-analyze-forced-year t)))
  13910. (condition-case nil
  13911. (ignore (encode-time second minute hour day month year))
  13912. (error
  13913. (setq year (nth 5 defdecode))
  13914. (setq org-read-date-analyze-forced-year t))))
  13915. (setq org-read-date-analyze-futurep futurep)
  13916. (list second minute hour day month year)))
  13917. (defvar parse-time-weekdays)
  13918. (defun org-read-date-get-relative (s today default)
  13919. "Check string S for special relative date string.
  13920. TODAY and DEFAULT are internal times, for today and for a default.
  13921. Return shift list (N what def-flag)
  13922. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  13923. N is the number of WHATs to shift.
  13924. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  13925. the DEFAULT date rather than TODAY."
  13926. (when (and
  13927. (string-match
  13928. (concat
  13929. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  13930. "\\([0-9]+\\)?"
  13931. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  13932. "\\([ \t]\\|$\\)") s)
  13933. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  13934. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  13935. (string-to-char (substring (match-string 1 s) -1))
  13936. ?+))
  13937. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  13938. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  13939. (what (if (match-end 3) (match-string 3 s) "d"))
  13940. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  13941. (date (if rel default today))
  13942. (wday (nth 6 (decode-time date)))
  13943. delta)
  13944. (if wday1
  13945. (progn
  13946. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  13947. (if (= dir ?-) (setq delta (- delta 7)))
  13948. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  13949. (list delta "d" rel))
  13950. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  13951. (defun org-order-calendar-date-args (arg1 arg2 arg3)
  13952. "Turn a user-specified date into the internal representation.
  13953. The internal representation needed by the calendar is (month day year).
  13954. This is a wrapper to handle the brain-dead convention in calendar that
  13955. user function argument order change dependent on argument order."
  13956. (if (boundp 'calendar-date-style)
  13957. (cond
  13958. ((eq calendar-date-style 'american)
  13959. (list arg1 arg2 arg3))
  13960. ((eq calendar-date-style 'european)
  13961. (list arg2 arg1 arg3))
  13962. ((eq calendar-date-style 'iso)
  13963. (list arg2 arg3 arg1)))
  13964. (with-no-warnings ;; european-calendar-style is obsolete as of version 23.1
  13965. (if (org-bound-and-true-p european-calendar-style)
  13966. (list arg2 arg1 arg3)
  13967. (list arg1 arg2 arg3)))))
  13968. (defun org-eval-in-calendar (form &optional keepdate)
  13969. "Eval FORM in the calendar window and return to current window.
  13970. Also, store the cursor date in variable org-ans2."
  13971. (let ((sf (selected-frame))
  13972. (sw (selected-window)))
  13973. (select-window (get-buffer-window "*Calendar*" t))
  13974. (eval form)
  13975. (when (and (not keepdate) (calendar-cursor-to-date))
  13976. (let* ((date (calendar-cursor-to-date))
  13977. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  13978. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  13979. (move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  13980. (select-window sw)
  13981. (org-select-frame-set-input-focus sf)))
  13982. (defun org-calendar-select ()
  13983. "Return to `org-read-date' with the date currently selected.
  13984. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  13985. (interactive)
  13986. (when (calendar-cursor-to-date)
  13987. (let* ((date (calendar-cursor-to-date))
  13988. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  13989. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  13990. (if (active-minibuffer-window) (exit-minibuffer))))
  13991. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  13992. "Insert a date stamp for the date given by the internal TIME.
  13993. WITH-HM means use the stamp format that includes the time of the day.
  13994. INACTIVE means use square brackets instead of angular ones, so that the
  13995. stamp will not contribute to the agenda.
  13996. PRE and POST are optional strings to be inserted before and after the
  13997. stamp.
  13998. The command returns the inserted time stamp."
  13999. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  14000. stamp)
  14001. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  14002. (insert-before-markers (or pre ""))
  14003. (when (listp extra)
  14004. (setq extra (car extra))
  14005. (if (and (stringp extra)
  14006. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  14007. (setq extra (format "-%02d:%02d"
  14008. (string-to-number (match-string 1 extra))
  14009. (string-to-number (match-string 2 extra))))
  14010. (setq extra nil)))
  14011. (when extra
  14012. (setq fmt (concat (substring fmt 0 -1) extra (substring fmt -1))))
  14013. (insert-before-markers (setq stamp (format-time-string fmt time)))
  14014. (insert-before-markers (or post ""))
  14015. (setq org-last-inserted-timestamp stamp)))
  14016. (defun org-toggle-time-stamp-overlays ()
  14017. "Toggle the use of custom time stamp formats."
  14018. (interactive)
  14019. (setq org-display-custom-times (not org-display-custom-times))
  14020. (unless org-display-custom-times
  14021. (let ((p (point-min)) (bmp (buffer-modified-p)))
  14022. (while (setq p (next-single-property-change p 'display))
  14023. (if (and (get-text-property p 'display)
  14024. (eq (get-text-property p 'face) 'org-date))
  14025. (remove-text-properties
  14026. p (setq p (next-single-property-change p 'display))
  14027. '(display t))))
  14028. (set-buffer-modified-p bmp)))
  14029. (if (featurep 'xemacs)
  14030. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  14031. (org-restart-font-lock)
  14032. (setq org-table-may-need-update t)
  14033. (if org-display-custom-times
  14034. (message "Time stamps are overlayed with custom format")
  14035. (message "Time stamp overlays removed")))
  14036. (defun org-display-custom-time (beg end)
  14037. "Overlay modified time stamp format over timestamp between BEG and END."
  14038. (let* ((ts (buffer-substring beg end))
  14039. t1 w1 with-hm tf time str w2 (off 0))
  14040. (save-match-data
  14041. (setq t1 (org-parse-time-string ts t))
  14042. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)?\\'" ts)
  14043. (setq off (- (match-end 0) (match-beginning 0)))))
  14044. (setq end (- end off))
  14045. (setq w1 (- end beg)
  14046. with-hm (and (nth 1 t1) (nth 2 t1))
  14047. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  14048. time (org-fix-decoded-time t1)
  14049. str (org-add-props
  14050. (format-time-string
  14051. (substring tf 1 -1) (apply 'encode-time time))
  14052. nil 'mouse-face 'highlight)
  14053. w2 (length str))
  14054. (if (not (= w2 w1))
  14055. (add-text-properties (1+ beg) (+ 2 beg)
  14056. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  14057. (if (featurep 'xemacs)
  14058. (progn
  14059. (put-text-property beg end 'invisible t)
  14060. (put-text-property beg end 'end-glyph (make-glyph str)))
  14061. (put-text-property beg end 'display str))))
  14062. (defun org-translate-time (string)
  14063. "Translate all timestamps in STRING to custom format.
  14064. But do this only if the variable `org-display-custom-times' is set."
  14065. (when org-display-custom-times
  14066. (save-match-data
  14067. (let* ((start 0)
  14068. (re org-ts-regexp-both)
  14069. t1 with-hm inactive tf time str beg end)
  14070. (while (setq start (string-match re string start))
  14071. (setq beg (match-beginning 0)
  14072. end (match-end 0)
  14073. t1 (save-match-data
  14074. (org-parse-time-string (substring string beg end) t))
  14075. with-hm (and (nth 1 t1) (nth 2 t1))
  14076. inactive (equal (substring string beg (1+ beg)) "[")
  14077. tf (funcall (if with-hm 'cdr 'car)
  14078. org-time-stamp-custom-formats)
  14079. time (org-fix-decoded-time t1)
  14080. str (format-time-string
  14081. (concat
  14082. (if inactive "[" "<") (substring tf 1 -1)
  14083. (if inactive "]" ">"))
  14084. (apply 'encode-time time))
  14085. string (replace-match str t t string)
  14086. start (+ start (length str)))))))
  14087. string)
  14088. (defun org-fix-decoded-time (time)
  14089. "Set 0 instead of nil for the first 6 elements of time.
  14090. Don't touch the rest."
  14091. (let ((n 0))
  14092. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  14093. (defun org-days-to-time (timestamp-string)
  14094. "Difference between TIMESTAMP-STRING and now in days."
  14095. (- (time-to-days (org-time-string-to-time timestamp-string))
  14096. (time-to-days (current-time))))
  14097. (defun org-deadline-close (timestamp-string &optional ndays)
  14098. "Is the time in TIMESTAMP-STRING close to the current date?"
  14099. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  14100. (and (< (org-days-to-time timestamp-string) ndays)
  14101. (not (org-entry-is-done-p))))
  14102. (defun org-get-wdays (ts)
  14103. "Get the deadline lead time appropriate for timestring TS."
  14104. (cond
  14105. ((<= org-deadline-warning-days 0)
  14106. ;; 0 or negative, enforce this value no matter what
  14107. (- org-deadline-warning-days))
  14108. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\| \\)" ts)
  14109. ;; lead time is specified.
  14110. (floor (* (string-to-number (match-string 1 ts))
  14111. (cdr (assoc (match-string 2 ts)
  14112. '(("d" . 1) ("w" . 7)
  14113. ("m" . 30.4) ("y" . 365.25)))))))
  14114. ;; go for the default.
  14115. (t org-deadline-warning-days)))
  14116. (defun org-calendar-select-mouse (ev)
  14117. "Return to `org-read-date' with the date currently selected.
  14118. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  14119. (interactive "e")
  14120. (mouse-set-point ev)
  14121. (when (calendar-cursor-to-date)
  14122. (let* ((date (calendar-cursor-to-date))
  14123. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14124. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  14125. (if (active-minibuffer-window) (exit-minibuffer))))
  14126. (defun org-check-deadlines (ndays)
  14127. "Check if there are any deadlines due or past due.
  14128. A deadline is considered due if it happens within `org-deadline-warning-days'
  14129. days from today's date. If the deadline appears in an entry marked DONE,
  14130. it is not shown. The prefix arg NDAYS can be used to test that many
  14131. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  14132. (interactive "P")
  14133. (let* ((org-warn-days
  14134. (cond
  14135. ((equal ndays '(4)) 100000)
  14136. (ndays (prefix-numeric-value ndays))
  14137. (t (abs org-deadline-warning-days))))
  14138. (case-fold-search nil)
  14139. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  14140. (callback
  14141. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  14142. (message "%d deadlines past-due or due within %d days"
  14143. (org-occur regexp nil callback)
  14144. org-warn-days)))
  14145. (defun org-check-before-date (date)
  14146. "Check if there are deadlines or scheduled entries before DATE."
  14147. (interactive (list (org-read-date)))
  14148. (let ((case-fold-search nil)
  14149. (regexp (concat "\\<\\(" org-deadline-string
  14150. "\\|" org-scheduled-string
  14151. "\\) *<\\([^>]+\\)>"))
  14152. (callback
  14153. (lambda () (time-less-p
  14154. (org-time-string-to-time (match-string 2))
  14155. (org-time-string-to-time date)))))
  14156. (message "%d entries before %s"
  14157. (org-occur regexp nil callback) date)))
  14158. (defun org-check-after-date (date)
  14159. "Check if there are deadlines or scheduled entries after DATE."
  14160. (interactive (list (org-read-date)))
  14161. (let ((case-fold-search nil)
  14162. (regexp (concat "\\<\\(" org-deadline-string
  14163. "\\|" org-scheduled-string
  14164. "\\) *<\\([^>]+\\)>"))
  14165. (callback
  14166. (lambda () (not
  14167. (time-less-p
  14168. (org-time-string-to-time (match-string 2))
  14169. (org-time-string-to-time date))))))
  14170. (message "%d entries after %s"
  14171. (org-occur regexp nil callback) date)))
  14172. (defun org-evaluate-time-range (&optional to-buffer)
  14173. "Evaluate a time range by computing the difference between start and end.
  14174. Normally the result is just printed in the echo area, but with prefix arg
  14175. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  14176. If the time range is actually in a table, the result is inserted into the
  14177. next column.
  14178. For time difference computation, a year is assumed to be exactly 365
  14179. days in order to avoid rounding problems."
  14180. (interactive "P")
  14181. (or
  14182. (org-clock-update-time-maybe)
  14183. (save-excursion
  14184. (unless (org-at-date-range-p t)
  14185. (goto-char (point-at-bol))
  14186. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  14187. (if (not (org-at-date-range-p t))
  14188. (error "Not at a time-stamp range, and none found in current line")))
  14189. (let* ((ts1 (match-string 1))
  14190. (ts2 (match-string 2))
  14191. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  14192. (match-end (match-end 0))
  14193. (time1 (org-time-string-to-time ts1))
  14194. (time2 (org-time-string-to-time ts2))
  14195. (t1 (org-float-time time1))
  14196. (t2 (org-float-time time2))
  14197. (diff (abs (- t2 t1)))
  14198. (negative (< (- t2 t1) 0))
  14199. ;; (ys (floor (* 365 24 60 60)))
  14200. (ds (* 24 60 60))
  14201. (hs (* 60 60))
  14202. (fy "%dy %dd %02d:%02d")
  14203. (fy1 "%dy %dd")
  14204. (fd "%dd %02d:%02d")
  14205. (fd1 "%dd")
  14206. (fh "%02d:%02d")
  14207. y d h m align)
  14208. (if havetime
  14209. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  14210. y 0
  14211. d (floor (/ diff ds)) diff (mod diff ds)
  14212. h (floor (/ diff hs)) diff (mod diff hs)
  14213. m (floor (/ diff 60)))
  14214. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  14215. y 0
  14216. d (floor (+ (/ diff ds) 0.5))
  14217. h 0 m 0))
  14218. (if (not to-buffer)
  14219. (message "%s" (org-make-tdiff-string y d h m))
  14220. (if (org-at-table-p)
  14221. (progn
  14222. (goto-char match-end)
  14223. (setq align t)
  14224. (and (looking-at " *|") (goto-char (match-end 0))))
  14225. (goto-char match-end))
  14226. (if (looking-at
  14227. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  14228. (replace-match ""))
  14229. (if negative (insert " -"))
  14230. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  14231. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  14232. (insert " " (format fh h m))))
  14233. (if align (org-table-align))
  14234. (message "Time difference inserted")))))
  14235. (defun org-make-tdiff-string (y d h m)
  14236. (let ((fmt "")
  14237. (l nil))
  14238. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  14239. l (push y l)))
  14240. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  14241. l (push d l)))
  14242. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  14243. l (push h l)))
  14244. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  14245. l (push m l)))
  14246. (apply 'format fmt (nreverse l))))
  14247. (defun org-time-string-to-time (s &optional buffer pos)
  14248. (condition-case errdata
  14249. (apply 'encode-time (org-parse-time-string s))
  14250. (error (error "Bad timestamp `%s'%s\nError was: %s"
  14251. s (if (not (and buffer pos))
  14252. ""
  14253. (format " at %d in buffer `%s'" pos buffer))
  14254. (cdr errdata)))))
  14255. (defun org-time-string-to-seconds (s)
  14256. (org-float-time (org-time-string-to-time s)))
  14257. (defun org-time-string-to-absolute (s &optional daynr prefer show-all buffer pos)
  14258. "Convert a time stamp to an absolute day number.
  14259. If there is a specifier for a cyclic time stamp, get the closest date to
  14260. DAYNR.
  14261. PREFER and SHOW-ALL are passed through to `org-closest-date'.
  14262. The variable date is bound by the calendar when this is called."
  14263. (cond
  14264. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  14265. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  14266. daynr
  14267. (+ daynr 1000)))
  14268. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  14269. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  14270. (time-to-days (current-time))) (match-string 0 s)
  14271. prefer show-all))
  14272. (t (time-to-days
  14273. (condition-case errdata
  14274. (apply 'encode-time (org-parse-time-string s))
  14275. (error (error "Bad timestamp `%s'%s\nError was: %s"
  14276. s (if (not (and buffer pos))
  14277. ""
  14278. (format " at %d in buffer `%s'" pos buffer))
  14279. (cdr errdata))))))))
  14280. (defun org-days-to-iso-week (days)
  14281. "Return the iso week number."
  14282. (require 'cal-iso)
  14283. (car (calendar-iso-from-absolute days)))
  14284. (defun org-small-year-to-year (year)
  14285. "Convert 2-digit years into 4-digit years.
  14286. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  14287. The year 2000 cannot be abbreviated. Any year larger than 99
  14288. is returned unchanged."
  14289. (if (< year 38)
  14290. (setq year (+ 2000 year))
  14291. (if (< year 100)
  14292. (setq year (+ 1900 year))))
  14293. year)
  14294. (defun org-time-from-absolute (d)
  14295. "Return the time corresponding to date D.
  14296. D may be an absolute day number, or a calendar-type list (month day year)."
  14297. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  14298. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  14299. (defun org-calendar-holiday ()
  14300. "List of holidays, for Diary display in Org-mode."
  14301. (require 'holidays)
  14302. (let ((hl (funcall
  14303. (if (fboundp 'calendar-check-holidays)
  14304. 'calendar-check-holidays 'check-calendar-holidays) date)))
  14305. (if hl (mapconcat 'identity hl "; "))))
  14306. (defun org-diary-sexp-entry (sexp entry date)
  14307. "Process a SEXP diary ENTRY for DATE."
  14308. (require 'diary-lib)
  14309. (let ((result (if calendar-debug-sexp
  14310. (let ((stack-trace-on-error t))
  14311. (eval (car (read-from-string sexp))))
  14312. (condition-case nil
  14313. (eval (car (read-from-string sexp)))
  14314. (error
  14315. (beep)
  14316. (message "Bad sexp at line %d in %s: %s"
  14317. (org-current-line)
  14318. (buffer-file-name) sexp)
  14319. (sleep-for 2))))))
  14320. (cond ((stringp result) (split-string result "; "))
  14321. ((and (consp result)
  14322. (not (consp (cdr result)))
  14323. (stringp (cdr result))) (cdr result))
  14324. ((and (consp result)
  14325. (stringp (car result))) result)
  14326. (result entry)
  14327. (t nil))))
  14328. (defun org-diary-to-ical-string (frombuf)
  14329. "Get iCalendar entries from diary entries in buffer FROMBUF.
  14330. This uses the icalendar.el library."
  14331. (let* ((tmpdir (if (featurep 'xemacs)
  14332. (temp-directory)
  14333. temporary-file-directory))
  14334. (tmpfile (make-temp-name
  14335. (expand-file-name "orgics" tmpdir)))
  14336. buf rtn b e)
  14337. (with-current-buffer frombuf
  14338. (icalendar-export-region (point-min) (point-max) tmpfile)
  14339. (setq buf (find-buffer-visiting tmpfile))
  14340. (set-buffer buf)
  14341. (goto-char (point-min))
  14342. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  14343. (setq b (match-beginning 0)))
  14344. (goto-char (point-max))
  14345. (if (re-search-backward "^END:VEVENT" nil t)
  14346. (setq e (match-end 0)))
  14347. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  14348. (kill-buffer buf)
  14349. (delete-file tmpfile)
  14350. rtn))
  14351. (defun org-closest-date (start current change prefer show-all)
  14352. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  14353. When PREFER is `past', return a date that is either CURRENT or past.
  14354. When PREFER is `future', return a date that is either CURRENT or future.
  14355. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  14356. ;; Make the proper lists from the dates
  14357. (catch 'exit
  14358. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  14359. dn dw sday cday n1 n2 n0
  14360. d m y y1 y2 date1 date2 nmonths nm ny m2)
  14361. (setq start (org-date-to-gregorian start)
  14362. current (org-date-to-gregorian
  14363. (if show-all
  14364. current
  14365. (time-to-days (current-time))))
  14366. sday (calendar-absolute-from-gregorian start)
  14367. cday (calendar-absolute-from-gregorian current))
  14368. (if (<= cday sday) (throw 'exit sday))
  14369. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  14370. (setq dn (string-to-number (match-string 1 change))
  14371. dw (cdr (assoc (match-string 2 change) a1)))
  14372. (error "Invalid change specifier: %s" change))
  14373. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  14374. (cond
  14375. ((eq dw 'day)
  14376. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  14377. n2 (+ n1 dn)))
  14378. ((eq dw 'year)
  14379. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  14380. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  14381. (setq date1 (list m d y1)
  14382. n1 (calendar-absolute-from-gregorian date1)
  14383. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  14384. n2 (calendar-absolute-from-gregorian date2)))
  14385. ((eq dw 'month)
  14386. ;; approx number of month between the two dates
  14387. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  14388. ;; How often does dn fit in there?
  14389. (setq d (nth 1 start) m (car start) y (nth 2 start)
  14390. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  14391. m (+ m nm)
  14392. ny (floor (/ m 12))
  14393. y (+ y ny)
  14394. m (- m (* ny 12)))
  14395. (while (> m 12) (setq m (- m 12) y (1+ y)))
  14396. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  14397. (setq m2 (+ m dn) y2 y)
  14398. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  14399. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  14400. (while (<= n2 cday)
  14401. (setq n1 n2 m m2 y y2)
  14402. (setq m2 (+ m dn) y2 y)
  14403. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  14404. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  14405. ;; Make sure n1 is the earlier date
  14406. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  14407. (if show-all
  14408. (cond
  14409. ((eq prefer 'past) (if (= cday n2) n2 n1))
  14410. ((eq prefer 'future) (if (= cday n1) n1 n2))
  14411. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  14412. (cond
  14413. ((eq prefer 'past) (if (= cday n2) n2 n1))
  14414. ((eq prefer 'future) (if (= cday n1) n1 n2))
  14415. (t (if (= cday n1) n1 n2)))))))
  14416. (defun org-date-to-gregorian (date)
  14417. "Turn any specification of DATE into a Gregorian date for the calendar."
  14418. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  14419. ((and (listp date) (= (length date) 3)) date)
  14420. ((stringp date)
  14421. (setq date (org-parse-time-string date))
  14422. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  14423. ((listp date)
  14424. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  14425. (defun org-parse-time-string (s &optional nodefault)
  14426. "Parse the standard Org-mode time string.
  14427. This should be a lot faster than the normal `parse-time-string'.
  14428. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  14429. hour and minute fields will be nil if not given."
  14430. (if (string-match org-ts-regexp0 s)
  14431. (list 0
  14432. (if (or (match-beginning 8) (not nodefault))
  14433. (string-to-number (or (match-string 8 s) "0")))
  14434. (if (or (match-beginning 7) (not nodefault))
  14435. (string-to-number (or (match-string 7 s) "0")))
  14436. (string-to-number (match-string 4 s))
  14437. (string-to-number (match-string 3 s))
  14438. (string-to-number (match-string 2 s))
  14439. nil nil nil)
  14440. (error "Not a standard Org-mode time string: %s" s)))
  14441. (defun org-timestamp-up (&optional arg)
  14442. "Increase the date item at the cursor by one.
  14443. If the cursor is on the year, change the year. If it is on the month,
  14444. the day or the time, change that.
  14445. With prefix ARG, change by that many units."
  14446. (interactive "p")
  14447. (org-timestamp-change (prefix-numeric-value arg) nil 'updown))
  14448. (defun org-timestamp-down (&optional arg)
  14449. "Decrease the date item at the cursor by one.
  14450. If the cursor is on the year, change the year. If it is on the month,
  14451. the day or the time, change that.
  14452. With prefix ARG, change by that many units."
  14453. (interactive "p")
  14454. (org-timestamp-change (- (prefix-numeric-value arg)) nil 'updown))
  14455. (defun org-timestamp-up-day (&optional arg)
  14456. "Increase the date in the time stamp by one day.
  14457. With prefix ARG, change that many days."
  14458. (interactive "p")
  14459. (if (and (not (org-at-timestamp-p t))
  14460. (org-on-heading-p))
  14461. (org-todo 'up)
  14462. (org-timestamp-change (prefix-numeric-value arg) 'day 'updown)))
  14463. (defun org-timestamp-down-day (&optional arg)
  14464. "Decrease the date in the time stamp by one day.
  14465. With prefix ARG, change that many days."
  14466. (interactive "p")
  14467. (if (and (not (org-at-timestamp-p t))
  14468. (org-on-heading-p))
  14469. (org-todo 'down)
  14470. (org-timestamp-change (- (prefix-numeric-value arg)) 'day) 'updown))
  14471. (defun org-at-timestamp-p (&optional inactive-ok)
  14472. "Determine if the cursor is in or at a timestamp."
  14473. (interactive)
  14474. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  14475. (pos (point))
  14476. (ans (or (looking-at tsr)
  14477. (save-excursion
  14478. (skip-chars-backward "^[<\n\r\t")
  14479. (if (> (point) (point-min)) (backward-char 1))
  14480. (and (looking-at tsr)
  14481. (> (- (match-end 0) pos) -1))))))
  14482. (and ans
  14483. (boundp 'org-ts-what)
  14484. (setq org-ts-what
  14485. (cond
  14486. ((= pos (match-beginning 0)) 'bracket)
  14487. ;; Point is considered to be "on the bracket" whether
  14488. ;; it's really on it or right after it.
  14489. ((or (= pos (1- (match-end 0)))
  14490. (= pos (match-end 0))) 'bracket)
  14491. ((org-pos-in-match-range pos 2) 'year)
  14492. ((org-pos-in-match-range pos 3) 'month)
  14493. ((org-pos-in-match-range pos 7) 'hour)
  14494. ((org-pos-in-match-range pos 8) 'minute)
  14495. ((or (org-pos-in-match-range pos 4)
  14496. (org-pos-in-match-range pos 5)) 'day)
  14497. ((and (> pos (or (match-end 8) (match-end 5)))
  14498. (< pos (match-end 0)))
  14499. (- pos (or (match-end 8) (match-end 5))))
  14500. (t 'day))))
  14501. ans))
  14502. (defun org-toggle-timestamp-type ()
  14503. "Toggle the type (<active> or [inactive]) of a time stamp."
  14504. (interactive)
  14505. (when (org-at-timestamp-p t)
  14506. (let ((beg (match-beginning 0)) (end (match-end 0))
  14507. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  14508. (save-excursion
  14509. (goto-char beg)
  14510. (while (re-search-forward "[][<>]" end t)
  14511. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  14512. t t)))
  14513. (message "Timestamp is now %sactive"
  14514. (if (equal (char-after beg) ?<) "" "in")))))
  14515. (defun org-timestamp-change (n &optional what updown)
  14516. "Change the date in the time stamp at point.
  14517. The date will be changed by N times WHAT. WHAT can be `day', `month',
  14518. `year', `minute', `second'. If WHAT is not given, the cursor position
  14519. in the timestamp determines what will be changed."
  14520. (let ((origin (point)) origin-cat
  14521. with-hm inactive
  14522. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  14523. org-ts-what
  14524. extra rem
  14525. ts time time0)
  14526. (if (not (org-at-timestamp-p t))
  14527. (error "Not at a timestamp"))
  14528. (if (and (not what) (eq org-ts-what 'bracket))
  14529. (org-toggle-timestamp-type)
  14530. ;; Point isn't on brackets. Remember the part of the time-stamp
  14531. ;; the point was in. Indeed, size of time-stamps may change,
  14532. ;; but point must be kept in the same category nonetheless.
  14533. (setq origin-cat org-ts-what)
  14534. (if (and (not what) (not (eq org-ts-what 'day))
  14535. org-display-custom-times
  14536. (get-text-property (point) 'display)
  14537. (not (get-text-property (1- (point)) 'display)))
  14538. (setq org-ts-what 'day))
  14539. (setq org-ts-what (or what org-ts-what)
  14540. inactive (= (char-after (match-beginning 0)) ?\[)
  14541. ts (match-string 0))
  14542. (replace-match "")
  14543. (if (string-match
  14544. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)*\\)[]>]"
  14545. ts)
  14546. (setq extra (match-string 1 ts)))
  14547. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  14548. (setq with-hm t))
  14549. (setq time0 (org-parse-time-string ts))
  14550. (when (and updown
  14551. (eq org-ts-what 'minute)
  14552. (not current-prefix-arg))
  14553. ;; This looks like s-up and s-down. Change by one rounding step.
  14554. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  14555. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  14556. (setcar (cdr time0) (+ (nth 1 time0)
  14557. (if (> n 0) (- rem) (- dm rem))))))
  14558. (setq time
  14559. (encode-time (or (car time0) 0)
  14560. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  14561. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  14562. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  14563. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  14564. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  14565. (nthcdr 6 time0)))
  14566. (when (and (member org-ts-what '(hour minute))
  14567. extra
  14568. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  14569. (setq extra (org-modify-ts-extra
  14570. extra
  14571. (if (eq org-ts-what 'hour) 2 5)
  14572. n dm)))
  14573. (when (integerp org-ts-what)
  14574. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  14575. (if (eq what 'calendar)
  14576. (let ((cal-date (org-get-date-from-calendar)))
  14577. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  14578. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  14579. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  14580. (setcar time0 (or (car time0) 0))
  14581. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  14582. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  14583. (setq time (apply 'encode-time time0))))
  14584. ;; Insert the new time-stamp, and ensure point stays in the same
  14585. ;; category as before (i.e. not after the last position in that
  14586. ;; category).
  14587. (let ((pos (point)))
  14588. ;; Stay before inserted string. `save-excursion' is of no use.
  14589. (setq org-last-changed-timestamp
  14590. (org-insert-time-stamp time with-hm inactive nil nil extra))
  14591. (goto-char pos))
  14592. (save-match-data
  14593. (looking-at org-ts-regexp3)
  14594. (goto-char (cond
  14595. ;; `day' category ends before `hour' if any, or at
  14596. ;; the end of the day name.
  14597. ((eq origin-cat 'day)
  14598. (min (or (match-beginning 7) (1- (match-end 5))) origin))
  14599. ((eq origin-cat 'hour) (min (match-end 7) origin))
  14600. ((eq origin-cat 'minute) (min (1- (match-end 8)) origin))
  14601. ((integerp origin-cat) (min (1- (match-end 0)) origin))
  14602. ;; `year' and `month' have both fixed size: point
  14603. ;; couldn't have moved into another part.
  14604. (t origin))))
  14605. ;; Update clock if on a CLOCK line.
  14606. (org-clock-update-time-maybe)
  14607. ;; Try to recenter the calendar window, if any.
  14608. (if (and org-calendar-follow-timestamp-change
  14609. (get-buffer-window "*Calendar*" t)
  14610. (memq org-ts-what '(day month year)))
  14611. (org-recenter-calendar (time-to-days time))))))
  14612. (defun org-modify-ts-extra (s pos n dm)
  14613. "Change the different parts of the lead-time and repeat fields in timestamp."
  14614. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  14615. ng h m new rem)
  14616. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  14617. (cond
  14618. ((or (org-pos-in-match-range pos 2)
  14619. (org-pos-in-match-range pos 3))
  14620. (setq m (string-to-number (match-string 3 s))
  14621. h (string-to-number (match-string 2 s)))
  14622. (if (org-pos-in-match-range pos 2)
  14623. (setq h (+ h n))
  14624. (setq n (* dm (org-no-warnings (signum n))))
  14625. (when (not (= 0 (setq rem (% m dm))))
  14626. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  14627. (setq m (+ m n)))
  14628. (if (< m 0) (setq m (+ m 60) h (1- h)))
  14629. (if (> m 59) (setq m (- m 60) h (1+ h)))
  14630. (setq h (min 24 (max 0 h)))
  14631. (setq ng 1 new (format "-%02d:%02d" h m)))
  14632. ((org-pos-in-match-range pos 6)
  14633. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  14634. ((org-pos-in-match-range pos 5)
  14635. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  14636. ((org-pos-in-match-range pos 9)
  14637. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  14638. ((org-pos-in-match-range pos 8)
  14639. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  14640. (when ng
  14641. (setq s (concat
  14642. (substring s 0 (match-beginning ng))
  14643. new
  14644. (substring s (match-end ng))))))
  14645. s))
  14646. (defun org-recenter-calendar (date)
  14647. "If the calendar is visible, recenter it to DATE."
  14648. (let* ((win (selected-window))
  14649. (cwin (get-buffer-window "*Calendar*" t))
  14650. (calendar-move-hook nil))
  14651. (when cwin
  14652. (select-window cwin)
  14653. (calendar-goto-date (if (listp date) date
  14654. (calendar-gregorian-from-absolute date)))
  14655. (select-window win))))
  14656. (defun org-goto-calendar (&optional arg)
  14657. "Go to the Emacs calendar at the current date.
  14658. If there is a time stamp in the current line, go to that date.
  14659. A prefix ARG can be used to force the current date."
  14660. (interactive "P")
  14661. (let ((tsr org-ts-regexp) diff
  14662. (calendar-move-hook nil)
  14663. (calendar-view-holidays-initially-flag nil)
  14664. (calendar-view-diary-initially-flag nil))
  14665. (if (or (org-at-timestamp-p)
  14666. (save-excursion
  14667. (beginning-of-line 1)
  14668. (looking-at (concat ".*" tsr))))
  14669. (let ((d1 (time-to-days (current-time)))
  14670. (d2 (time-to-days
  14671. (org-time-string-to-time (match-string 1)))))
  14672. (setq diff (- d2 d1))))
  14673. (calendar)
  14674. (calendar-goto-today)
  14675. (if (and diff (not arg)) (calendar-forward-day diff))))
  14676. (defun org-get-date-from-calendar ()
  14677. "Return a list (month day year) of date at point in calendar."
  14678. (with-current-buffer "*Calendar*"
  14679. (save-match-data
  14680. (calendar-cursor-to-date))))
  14681. (defun org-date-from-calendar ()
  14682. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  14683. If there is already a time stamp at the cursor position, update it."
  14684. (interactive)
  14685. (if (org-at-timestamp-p t)
  14686. (org-timestamp-change 0 'calendar)
  14687. (let ((cal-date (org-get-date-from-calendar)))
  14688. (org-insert-time-stamp
  14689. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  14690. (defun org-minutes-to-hh:mm-string (m)
  14691. "Compute H:MM from a number of minutes."
  14692. (let ((h (/ m 60)))
  14693. (setq m (- m (* 60 h)))
  14694. (format org-time-clocksum-format h m)))
  14695. (defun org-hh:mm-string-to-minutes (s)
  14696. "Convert a string H:MM to a number of minutes.
  14697. If the string is just a number, interpret it as minutes.
  14698. In fact, the first hh:mm or number in the string will be taken,
  14699. there can be extra stuff in the string.
  14700. If no number is found, the return value is 0."
  14701. (cond
  14702. ((integerp s) s)
  14703. ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  14704. (+ (* (string-to-number (match-string 1 s)) 60)
  14705. (string-to-number (match-string 2 s))))
  14706. ((string-match "\\([0-9]+\\)" s)
  14707. (string-to-number (match-string 1 s)))
  14708. (t 0)))
  14709. (defcustom org-effort-durations
  14710. `(("h" . 60)
  14711. ("d" . ,(* 60 8))
  14712. ("w" . ,(* 60 8 5))
  14713. ("m" . ,(* 60 8 5 4))
  14714. ("y" . ,(* 60 8 5 40)))
  14715. "Conversion factor to minutes for an effort modifier.
  14716. Each entry has the form (MODIFIER . MINUTES).
  14717. In an effort string, a number followed by MODIFIER is multiplied
  14718. by the specified number of MINUTES to obtain an effort in
  14719. minutes.
  14720. For example, if the value of this variable is ((\"hours\" . 60)), then an
  14721. effort string \"2hours\" is equivalent to 120 minutes."
  14722. :group 'org-agenda
  14723. :type '(alist :key-type (string :tag "Modifier")
  14724. :value-type (number :tag "Minutes")))
  14725. (defun org-duration-string-to-minutes (s)
  14726. "Convert a duration string S to minutes.
  14727. A bare number is interpreted as minutes, modifiers can be set by
  14728. customizing `org-effort-durations' (which see).
  14729. Entries containing a colon are interpreted as H:MM by
  14730. `org-hh:mm-string-to-minutes'."
  14731. (let ((result 0)
  14732. (re (concat "\\([0-9]+\\) *\\("
  14733. (regexp-opt (mapcar 'car org-effort-durations))
  14734. "\\)")))
  14735. (while (string-match re s)
  14736. (incf result (* (cdr (assoc (match-string 2 s) org-effort-durations))
  14737. (string-to-number (match-string 1 s))))
  14738. (setq s (replace-match "" nil t s)))
  14739. (incf result (org-hh:mm-string-to-minutes s))
  14740. result))
  14741. ;;;; Files
  14742. (defun org-save-all-org-buffers ()
  14743. "Save all Org-mode buffers without user confirmation."
  14744. (interactive)
  14745. (message "Saving all Org-mode buffers...")
  14746. (save-some-buffers t (lambda () (eq major-mode 'org-mode)))
  14747. (when (featurep 'org-id) (org-id-locations-save))
  14748. (message "Saving all Org-mode buffers... done"))
  14749. (defun org-revert-all-org-buffers ()
  14750. "Revert all Org-mode buffers.
  14751. Prompt for confirmation when there are unsaved changes.
  14752. Be sure you know what you are doing before letting this function
  14753. overwrite your changes.
  14754. This function is useful in a setup where one tracks org files
  14755. with a version control system, to revert on one machine after pulling
  14756. changes from another. I believe the procedure must be like this:
  14757. 1. M-x org-save-all-org-buffers
  14758. 2. Pull changes from the other machine, resolve conflicts
  14759. 3. M-x org-revert-all-org-buffers"
  14760. (interactive)
  14761. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  14762. (error "Abort"))
  14763. (save-excursion
  14764. (save-window-excursion
  14765. (mapc
  14766. (lambda (b)
  14767. (when (and (with-current-buffer b (eq major-mode 'org-mode))
  14768. (with-current-buffer b buffer-file-name))
  14769. (org-pop-to-buffer-same-window b)
  14770. (revert-buffer t 'no-confirm)))
  14771. (buffer-list))
  14772. (when (and (featurep 'org-id) org-id-track-globally)
  14773. (org-id-locations-load)))))
  14774. ;;;; Agenda files
  14775. ;;;###autoload
  14776. (defun org-switchb (&optional arg)
  14777. "Switch between Org buffers.
  14778. With one prefix argument, restrict available buffers to files.
  14779. With two prefix arguments, restrict available buffers to agenda files.
  14780. Defaults to `iswitchb' for buffer name completion.
  14781. Set `org-completion-use-ido' to make it use ido instead."
  14782. (interactive "P")
  14783. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  14784. ((equal arg '(16)) (org-buffer-list 'agenda))
  14785. (t (org-buffer-list))))
  14786. (org-completion-use-iswitchb org-completion-use-iswitchb)
  14787. (org-completion-use-ido org-completion-use-ido))
  14788. (unless (or org-completion-use-ido org-completion-use-iswitchb)
  14789. (setq org-completion-use-iswitchb t))
  14790. (org-pop-to-buffer-same-window
  14791. (org-icompleting-read "Org buffer: "
  14792. (mapcar 'list (mapcar 'buffer-name blist))
  14793. nil t))))
  14794. ;;; Define some older names previously used for this functionality
  14795. ;;;###autoload
  14796. (defalias 'org-ido-switchb 'org-switchb)
  14797. ;;;###autoload
  14798. (defalias 'org-iswitchb 'org-switchb)
  14799. (defun org-buffer-list (&optional predicate exclude-tmp)
  14800. "Return a list of Org buffers.
  14801. PREDICATE can be `export', `files' or `agenda'.
  14802. export restrict the list to Export buffers.
  14803. files restrict the list to buffers visiting Org files.
  14804. agenda restrict the list to buffers visiting agenda files.
  14805. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  14806. (let* ((bfn nil)
  14807. (agenda-files (and (eq predicate 'agenda)
  14808. (mapcar 'file-truename (org-agenda-files t))))
  14809. (filter
  14810. (cond
  14811. ((eq predicate 'files)
  14812. (lambda (b) (with-current-buffer b (eq major-mode 'org-mode))))
  14813. ((eq predicate 'export)
  14814. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  14815. ((eq predicate 'agenda)
  14816. (lambda (b)
  14817. (with-current-buffer b
  14818. (and (eq major-mode 'org-mode)
  14819. (setq bfn (buffer-file-name b))
  14820. (member (file-truename bfn) agenda-files)))))
  14821. (t (lambda (b) (with-current-buffer b
  14822. (or (eq major-mode 'org-mode)
  14823. (string-match "\*Org .*Export"
  14824. (buffer-name b)))))))))
  14825. (delq nil
  14826. (mapcar
  14827. (lambda(b)
  14828. (if (and (funcall filter b)
  14829. (or (not exclude-tmp)
  14830. (not (string-match "tmp" (buffer-name b)))))
  14831. b
  14832. nil))
  14833. (buffer-list)))))
  14834. (defun org-agenda-files (&optional unrestricted archives)
  14835. "Get the list of agenda files.
  14836. Optional UNRESTRICTED means return the full list even if a restriction
  14837. is currently in place.
  14838. When ARCHIVES is t, include all archive files that are really being
  14839. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  14840. `org-agenda-archives-mode' is t."
  14841. (let ((files
  14842. (cond
  14843. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  14844. ((stringp org-agenda-files) (org-read-agenda-file-list))
  14845. ((listp org-agenda-files) org-agenda-files)
  14846. (t (error "Invalid value of `org-agenda-files'")))))
  14847. (setq files (apply 'append
  14848. (mapcar (lambda (f)
  14849. (if (file-directory-p f)
  14850. (directory-files
  14851. f t org-agenda-file-regexp)
  14852. (list f)))
  14853. files)))
  14854. (when org-agenda-skip-unavailable-files
  14855. (setq files (delq nil
  14856. (mapcar (function
  14857. (lambda (file)
  14858. (and (file-readable-p file) file)))
  14859. files))))
  14860. (when (or (eq archives t)
  14861. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  14862. (setq files (org-add-archive-files files)))
  14863. files))
  14864. (defun org-agenda-file-p (&optional file)
  14865. "Return non-nil, if FILE is an agenda file.
  14866. If FILE is omitted, use the file associated with the current
  14867. buffer."
  14868. (member (or file (buffer-file-name))
  14869. (org-agenda-files t)))
  14870. (defun org-edit-agenda-file-list ()
  14871. "Edit the list of agenda files.
  14872. Depending on setup, this either uses customize to edit the variable
  14873. `org-agenda-files', or it visits the file that is holding the list. In the
  14874. latter case, the buffer is set up in a way that saving it automatically kills
  14875. the buffer and restores the previous window configuration."
  14876. (interactive)
  14877. (if (stringp org-agenda-files)
  14878. (let ((cw (current-window-configuration)))
  14879. (find-file org-agenda-files)
  14880. (org-set-local 'org-window-configuration cw)
  14881. (org-add-hook 'after-save-hook
  14882. (lambda ()
  14883. (set-window-configuration
  14884. (prog1 org-window-configuration
  14885. (kill-buffer (current-buffer))))
  14886. (org-install-agenda-files-menu)
  14887. (message "New agenda file list installed"))
  14888. nil 'local)
  14889. (message "%s" (substitute-command-keys
  14890. "Edit list and finish with \\[save-buffer]")))
  14891. (customize-variable 'org-agenda-files)))
  14892. (defun org-store-new-agenda-file-list (list)
  14893. "Set new value for the agenda file list and save it correctly."
  14894. (if (stringp org-agenda-files)
  14895. (let ((fe (org-read-agenda-file-list t)) b u)
  14896. (while (setq b (find-buffer-visiting org-agenda-files))
  14897. (kill-buffer b))
  14898. (with-temp-file org-agenda-files
  14899. (insert
  14900. (mapconcat
  14901. (lambda (f) ;; Keep un-expanded entries.
  14902. (if (setq u (assoc f fe))
  14903. (cdr u)
  14904. f))
  14905. list "\n")
  14906. "\n")))
  14907. (let ((org-mode-hook nil) (org-inhibit-startup t)
  14908. (org-insert-mode-line-in-empty-file nil))
  14909. (setq org-agenda-files list)
  14910. (customize-save-variable 'org-agenda-files org-agenda-files))))
  14911. (defun org-read-agenda-file-list (&optional pair-with-expansion)
  14912. "Read the list of agenda files from a file.
  14913. If PAIR-WITH-EXPANSION is t return pairs with un-expanded
  14914. filenames, used by `org-store-new-agenda-file-list' to write back
  14915. un-expanded file names."
  14916. (when (file-directory-p org-agenda-files)
  14917. (error "`org-agenda-files' cannot be a single directory"))
  14918. (when (stringp org-agenda-files)
  14919. (with-temp-buffer
  14920. (insert-file-contents org-agenda-files)
  14921. (mapcar
  14922. (lambda (f)
  14923. (let ((e (expand-file-name (substitute-in-file-name f)
  14924. org-directory)))
  14925. (if pair-with-expansion
  14926. (cons e f)
  14927. e)))
  14928. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*")))))
  14929. ;;;###autoload
  14930. (defun org-cycle-agenda-files ()
  14931. "Cycle through the files in `org-agenda-files'.
  14932. If the current buffer visits an agenda file, find the next one in the list.
  14933. If the current buffer does not, find the first agenda file."
  14934. (interactive)
  14935. (let* ((fs (org-agenda-files t))
  14936. (files (append fs (list (car fs))))
  14937. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  14938. file)
  14939. (unless files (error "No agenda files"))
  14940. (catch 'exit
  14941. (while (setq file (pop files))
  14942. (if (equal (file-truename file) tcf)
  14943. (when (car files)
  14944. (find-file (car files))
  14945. (throw 'exit t))))
  14946. (find-file (car fs)))
  14947. (if (buffer-base-buffer) (org-pop-to-buffer-same-window (buffer-base-buffer)))))
  14948. (defun org-agenda-file-to-front (&optional to-end)
  14949. "Move/add the current file to the top of the agenda file list.
  14950. If the file is not present in the list, it is added to the front. If it is
  14951. present, it is moved there. With optional argument TO-END, add/move to the
  14952. end of the list."
  14953. (interactive "P")
  14954. (let ((org-agenda-skip-unavailable-files nil)
  14955. (file-alist (mapcar (lambda (x)
  14956. (cons (file-truename x) x))
  14957. (org-agenda-files t)))
  14958. (ctf (file-truename buffer-file-name))
  14959. x had)
  14960. (setq x (assoc ctf file-alist) had x)
  14961. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  14962. (if to-end
  14963. (setq file-alist (append (delq x file-alist) (list x)))
  14964. (setq file-alist (cons x (delq x file-alist))))
  14965. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  14966. (org-install-agenda-files-menu)
  14967. (message "File %s to %s of agenda file list"
  14968. (if had "moved" "added") (if to-end "end" "front"))))
  14969. (defun org-remove-file (&optional file)
  14970. "Remove current file from the list of files in variable `org-agenda-files'.
  14971. These are the files which are being checked for agenda entries.
  14972. Optional argument FILE means use this file instead of the current."
  14973. (interactive)
  14974. (let* ((org-agenda-skip-unavailable-files nil)
  14975. (file (or file buffer-file-name))
  14976. (true-file (file-truename file))
  14977. (afile (abbreviate-file-name file))
  14978. (files (delq nil (mapcar
  14979. (lambda (x)
  14980. (if (equal true-file
  14981. (file-truename x))
  14982. nil x))
  14983. (org-agenda-files t)))))
  14984. (if (not (= (length files) (length (org-agenda-files t))))
  14985. (progn
  14986. (org-store-new-agenda-file-list files)
  14987. (org-install-agenda-files-menu)
  14988. (message "Removed file: %s" afile))
  14989. (message "File was not in list: %s (not removed)" afile))))
  14990. (defun org-file-menu-entry (file)
  14991. (vector file (list 'find-file file) t))
  14992. (defun org-check-agenda-file (file)
  14993. "Make sure FILE exists. If not, ask user what to do."
  14994. (when (not (file-exists-p file))
  14995. (message "non-existent agenda file %s. [R]emove from list or [A]bort?"
  14996. (abbreviate-file-name file))
  14997. (let ((r (downcase (read-char-exclusive))))
  14998. (cond
  14999. ((equal r ?r)
  15000. (org-remove-file file)
  15001. (throw 'nextfile t))
  15002. (t (error "Abort"))))))
  15003. (defun org-get-agenda-file-buffer (file)
  15004. "Get a buffer visiting FILE. If the buffer needs to be created, add
  15005. it to the list of buffers which might be released later."
  15006. (let ((buf (org-find-base-buffer-visiting file)))
  15007. (if buf
  15008. buf ; just return it
  15009. ;; Make a new buffer and remember it
  15010. (setq buf (find-file-noselect file))
  15011. (if buf (push buf org-agenda-new-buffers))
  15012. buf)))
  15013. (defun org-release-buffers (blist)
  15014. "Release all buffers in list, asking the user for confirmation when needed.
  15015. When a buffer is unmodified, it is just killed. When modified, it is saved
  15016. \(if the user agrees) and then killed."
  15017. (let (buf file)
  15018. (while (setq buf (pop blist))
  15019. (setq file (buffer-file-name buf))
  15020. (when (and (buffer-modified-p buf)
  15021. file
  15022. (y-or-n-p (format "Save file %s? " file)))
  15023. (with-current-buffer buf (save-buffer)))
  15024. (kill-buffer buf))))
  15025. (defun org-prepare-agenda-buffers (files)
  15026. "Create buffers for all agenda files, protect archived trees and comments."
  15027. (interactive)
  15028. (let ((pa '(:org-archived t))
  15029. (pc '(:org-comment t))
  15030. (pall '(:org-archived t :org-comment t))
  15031. (inhibit-read-only t)
  15032. (rea (concat ":" org-archive-tag ":"))
  15033. bmp file re)
  15034. (save-excursion
  15035. (save-restriction
  15036. (while (setq file (pop files))
  15037. (catch 'nextfile
  15038. (if (bufferp file)
  15039. (set-buffer file)
  15040. (org-check-agenda-file file)
  15041. (set-buffer (org-get-agenda-file-buffer file)))
  15042. (widen)
  15043. (setq bmp (buffer-modified-p))
  15044. (org-refresh-category-properties)
  15045. (setq org-todo-keywords-for-agenda
  15046. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  15047. (setq org-done-keywords-for-agenda
  15048. (append org-done-keywords-for-agenda org-done-keywords))
  15049. (setq org-todo-keyword-alist-for-agenda
  15050. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  15051. (setq org-drawers-for-agenda
  15052. (append org-drawers-for-agenda org-drawers))
  15053. (setq org-tag-alist-for-agenda
  15054. (append org-tag-alist-for-agenda org-tag-alist))
  15055. (save-excursion
  15056. (remove-text-properties (point-min) (point-max) pall)
  15057. (when org-agenda-skip-archived-trees
  15058. (goto-char (point-min))
  15059. (while (re-search-forward rea nil t)
  15060. (if (org-on-heading-p t)
  15061. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  15062. (goto-char (point-min))
  15063. (setq re (format org-heading-keyword-regexp-format
  15064. org-comment-string))
  15065. (while (re-search-forward re nil t)
  15066. (add-text-properties
  15067. (match-beginning 0) (org-end-of-subtree t) pc)))
  15068. (set-buffer-modified-p bmp)))))
  15069. (setq org-todo-keywords-for-agenda
  15070. (org-uniquify org-todo-keywords-for-agenda))
  15071. (setq org-todo-keyword-alist-for-agenda
  15072. (org-uniquify org-todo-keyword-alist-for-agenda)
  15073. org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
  15074. ;;;; Embedded LaTeX
  15075. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  15076. "Keymap for the minor `org-cdlatex-mode'.")
  15077. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  15078. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  15079. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  15080. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  15081. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  15082. (defvar org-cdlatex-texmathp-advice-is-done nil
  15083. "Flag remembering if we have applied the advice to texmathp already.")
  15084. (define-minor-mode org-cdlatex-mode
  15085. "Toggle the minor `org-cdlatex-mode'.
  15086. This mode supports entering LaTeX environment and math in LaTeX fragments
  15087. in Org-mode.
  15088. \\{org-cdlatex-mode-map}"
  15089. nil " OCDL" nil
  15090. (when org-cdlatex-mode
  15091. (require 'cdlatex)
  15092. (run-hooks 'cdlatex-mode-hook)
  15093. (cdlatex-compute-tables))
  15094. (unless org-cdlatex-texmathp-advice-is-done
  15095. (setq org-cdlatex-texmathp-advice-is-done t)
  15096. (defadvice texmathp (around org-math-always-on activate)
  15097. "Always return t in org-mode buffers.
  15098. This is because we want to insert math symbols without dollars even outside
  15099. the LaTeX math segments. If Orgmode thinks that point is actually inside
  15100. an embedded LaTeX fragment, let texmathp do its job.
  15101. \\[org-cdlatex-mode-map]"
  15102. (interactive)
  15103. (let (p)
  15104. (cond
  15105. ((not (eq major-mode 'org-mode)) ad-do-it)
  15106. ((eq this-command 'cdlatex-math-symbol)
  15107. (setq ad-return-value t
  15108. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  15109. (t
  15110. (let ((p (org-inside-LaTeX-fragment-p)))
  15111. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  15112. (setq ad-return-value t
  15113. texmathp-why '("Org-mode embedded math" . 0))
  15114. (if p ad-do-it)))))))))
  15115. (defun turn-on-org-cdlatex ()
  15116. "Unconditionally turn on `org-cdlatex-mode'."
  15117. (org-cdlatex-mode 1))
  15118. (defun org-inside-LaTeX-fragment-p ()
  15119. "Test if point is inside a LaTeX fragment.
  15120. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  15121. sequence appearing also before point.
  15122. Even though the matchers for math are configurable, this function assumes
  15123. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  15124. delimiters are skipped when they have been removed by customization.
  15125. The return value is nil, or a cons cell with the delimiter and the
  15126. position of this delimiter.
  15127. This function does a reasonably good job, but can locally be fooled by
  15128. for example currency specifications. For example it will assume being in
  15129. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  15130. fragments that are properly closed, but during editing, we have to live
  15131. with the uncertainty caused by missing closing delimiters. This function
  15132. looks only before point, not after."
  15133. (catch 'exit
  15134. (let ((pos (point))
  15135. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  15136. (lim (progn
  15137. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  15138. (point)))
  15139. dd-on str (start 0) m re)
  15140. (goto-char pos)
  15141. (when dodollar
  15142. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  15143. re (nth 1 (assoc "$" org-latex-regexps)))
  15144. (while (string-match re str start)
  15145. (cond
  15146. ((= (match-end 0) (length str))
  15147. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  15148. ((= (match-end 0) (- (length str) 5))
  15149. (throw 'exit nil))
  15150. (t (setq start (match-end 0))))))
  15151. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  15152. (goto-char pos)
  15153. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  15154. (and (match-beginning 2) (throw 'exit nil))
  15155. ;; count $$
  15156. (while (re-search-backward "\\$\\$" lim t)
  15157. (setq dd-on (not dd-on)))
  15158. (goto-char pos)
  15159. (if dd-on (cons "$$" m))))))
  15160. (defun org-inside-latex-macro-p ()
  15161. "Is point inside a LaTeX macro or its arguments?"
  15162. (save-match-data
  15163. (org-in-regexp
  15164. "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")))
  15165. (defun org-try-cdlatex-tab ()
  15166. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  15167. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  15168. - inside a LaTeX fragment, or
  15169. - after the first word in a line, where an abbreviation expansion could
  15170. insert a LaTeX environment."
  15171. (when org-cdlatex-mode
  15172. (cond
  15173. ;; Before any word on the line: No expansion possible.
  15174. ((save-excursion (skip-chars-backward " \t") (bolp)) nil)
  15175. ;; Just after first word on the line: Expand it. Make sure it
  15176. ;; cannot happen on headlines, though.
  15177. ((save-excursion
  15178. (skip-chars-backward "a-zA-Z0-9*")
  15179. (skip-chars-backward " \t")
  15180. (and (bolp) (not (org-at-heading-p))))
  15181. (cdlatex-tab) t)
  15182. ((org-inside-LaTeX-fragment-p) (cdlatex-tab) t))))
  15183. (defun org-cdlatex-underscore-caret (&optional arg)
  15184. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  15185. Revert to the normal definition outside of these fragments."
  15186. (interactive "P")
  15187. (if (org-inside-LaTeX-fragment-p)
  15188. (call-interactively 'cdlatex-sub-superscript)
  15189. (let (org-cdlatex-mode)
  15190. (call-interactively (key-binding (vector last-input-event))))))
  15191. (defun org-cdlatex-math-modify (&optional arg)
  15192. "Execute `cdlatex-math-modify' in LaTeX fragments.
  15193. Revert to the normal definition outside of these fragments."
  15194. (interactive "P")
  15195. (if (org-inside-LaTeX-fragment-p)
  15196. (call-interactively 'cdlatex-math-modify)
  15197. (let (org-cdlatex-mode)
  15198. (call-interactively (key-binding (vector last-input-event))))))
  15199. (defvar org-latex-fragment-image-overlays nil
  15200. "List of overlays carrying the images of latex fragments.")
  15201. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  15202. (defun org-remove-latex-fragment-image-overlays ()
  15203. "Remove all overlays with LaTeX fragment images in current buffer."
  15204. (mapc 'delete-overlay org-latex-fragment-image-overlays)
  15205. (setq org-latex-fragment-image-overlays nil))
  15206. (defun org-preview-latex-fragment (&optional subtree)
  15207. "Preview the LaTeX fragment at point, or all locally or globally.
  15208. If the cursor is in a LaTeX fragment, create the image and overlay
  15209. it over the source code. If there is no fragment at point, display
  15210. all fragments in the current text, from one headline to the next. With
  15211. prefix SUBTREE, display all fragments in the current subtree. With a
  15212. double prefix arg \\[universal-argument] \\[universal-argument], or when \
  15213. the cursor is before the first headline,
  15214. display all fragments in the buffer.
  15215. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  15216. (interactive "P")
  15217. (org-remove-latex-fragment-image-overlays)
  15218. (save-excursion
  15219. (save-restriction
  15220. (let (beg end at msg)
  15221. (cond
  15222. ((or (equal subtree '(16))
  15223. (not (save-excursion
  15224. (re-search-backward org-outline-regexp-bol nil t))))
  15225. (setq beg (point-min) end (point-max)
  15226. msg "Creating images for buffer...%s"))
  15227. ((equal subtree '(4))
  15228. (org-back-to-heading)
  15229. (setq beg (point) end (org-end-of-subtree t)
  15230. msg "Creating images for subtree...%s"))
  15231. (t
  15232. (if (setq at (org-inside-LaTeX-fragment-p))
  15233. (goto-char (max (point-min) (- (cdr at) 2)))
  15234. (org-back-to-heading))
  15235. (setq beg (point) end (progn (outline-next-heading) (point))
  15236. msg (if at "Creating image...%s"
  15237. "Creating images for entry...%s"))))
  15238. (message msg "")
  15239. (narrow-to-region beg end)
  15240. (goto-char beg)
  15241. (org-format-latex
  15242. (concat "ltxpng/" (file-name-sans-extension
  15243. (file-name-nondirectory
  15244. buffer-file-name)))
  15245. default-directory 'overlays msg at 'forbuffer 'dvipng)
  15246. (message msg "done. Use `C-c C-c' to remove images.")))))
  15247. (defvar org-latex-regexps
  15248. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  15249. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  15250. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  15251. ("$1" "\\([^$]\\|^\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  15252. ("$" "\\([^$]\\|^\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  15253. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  15254. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
  15255. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
  15256. "Regular expressions for matching embedded LaTeX.")
  15257. (defvar org-export-have-math nil) ;; dynamic scoping
  15258. (defun org-format-latex (prefix &optional dir overlays msg at
  15259. forbuffer processing-type)
  15260. "Replace LaTeX fragments with links to an image, and produce images.
  15261. Some of the options can be changed using the variable
  15262. `org-format-latex-options'."
  15263. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  15264. (let* ((prefixnodir (file-name-nondirectory prefix))
  15265. (absprefix (expand-file-name prefix dir))
  15266. (todir (file-name-directory absprefix))
  15267. (opt org-format-latex-options)
  15268. (matchers (plist-get opt :matchers))
  15269. (re-list org-latex-regexps)
  15270. (org-format-latex-header-extra
  15271. (plist-get (org-infile-export-plist) :latex-header-extra))
  15272. (cnt 0) txt hash link beg end re e checkdir
  15273. executables-checked string
  15274. m n block-type block linkfile movefile ov)
  15275. ;; Check the different regular expressions
  15276. (while (setq e (pop re-list))
  15277. (setq m (car e) re (nth 1 e) n (nth 2 e) block-type (nth 3 e)
  15278. block (if block-type "\n\n" ""))
  15279. (when (member m matchers)
  15280. (goto-char (point-min))
  15281. (while (re-search-forward re nil t)
  15282. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  15283. (not (get-text-property (match-beginning n)
  15284. 'org-protected))
  15285. (or (not overlays)
  15286. (not (eq (get-char-property (match-beginning n)
  15287. 'org-overlay-type)
  15288. 'org-latex-overlay))))
  15289. (setq org-export-have-math t)
  15290. (cond
  15291. ((eq processing-type 'verbatim)
  15292. ;; Leave the text verbatim, just protect it
  15293. (add-text-properties (match-beginning n) (match-end n)
  15294. '(org-protected t)))
  15295. ((eq processing-type 'mathjax)
  15296. ;; Prepare for MathJax processing
  15297. (setq string (match-string n))
  15298. (if (member m '("$" "$1"))
  15299. (save-excursion
  15300. (delete-region (match-beginning n) (match-end n))
  15301. (goto-char (match-beginning n))
  15302. (insert (org-add-props (concat "\\(" (substring string 1 -1)
  15303. "\\)")
  15304. '(org-protected t))))
  15305. (add-text-properties (match-beginning n) (match-end n)
  15306. '(org-protected t))))
  15307. ((eq processing-type 'dvipng)
  15308. ;; Process to an image
  15309. (setq txt (match-string n)
  15310. beg (match-beginning n) end (match-end n)
  15311. cnt (1+ cnt))
  15312. (let (print-length print-level) ; make sure full list is printed
  15313. (setq hash (sha1 (prin1-to-string
  15314. (list org-format-latex-header
  15315. org-format-latex-header-extra
  15316. org-export-latex-default-packages-alist
  15317. org-export-latex-packages-alist
  15318. org-format-latex-options
  15319. forbuffer txt)))
  15320. linkfile (format "%s_%s.png" prefix hash)
  15321. movefile (format "%s_%s.png" absprefix hash)))
  15322. (setq link (concat block "[[file:" linkfile "]]" block))
  15323. (if msg (message msg cnt))
  15324. (goto-char beg)
  15325. (unless checkdir ; make sure the directory exists
  15326. (setq checkdir t)
  15327. (or (file-directory-p todir) (make-directory todir t)))
  15328. (unless executables-checked
  15329. (org-check-external-command
  15330. "latex" "needed to convert LaTeX fragments to images")
  15331. (org-check-external-command
  15332. "dvipng" "needed to convert LaTeX fragments to images")
  15333. (setq executables-checked t))
  15334. (unless (file-exists-p movefile)
  15335. (org-create-formula-image
  15336. txt movefile opt forbuffer))
  15337. (if overlays
  15338. (progn
  15339. (mapc (lambda (o)
  15340. (if (eq (overlay-get o 'org-overlay-type)
  15341. 'org-latex-overlay)
  15342. (delete-overlay o)))
  15343. (overlays-in beg end))
  15344. (setq ov (make-overlay beg end))
  15345. (overlay-put ov 'org-overlay-type 'org-latex-overlay)
  15346. (if (featurep 'xemacs)
  15347. (progn
  15348. (overlay-put ov 'invisible t)
  15349. (overlay-put
  15350. ov 'end-glyph
  15351. (make-glyph (vector 'png :file movefile))))
  15352. (overlay-put
  15353. ov 'display
  15354. (list 'image :type 'png :file movefile :ascent 'center)))
  15355. (push ov org-latex-fragment-image-overlays)
  15356. (goto-char end))
  15357. (delete-region beg end)
  15358. (insert (org-add-props link
  15359. (list 'org-latex-src
  15360. (replace-regexp-in-string
  15361. "\"" "" txt)
  15362. 'org-latex-src-embed-type
  15363. (if block-type 'paragraph 'character))))))
  15364. ((eq processing-type 'mathml)
  15365. ;; Process to MathML
  15366. (unless executables-checked
  15367. (unless (save-match-data (org-format-latex-mathml-available-p))
  15368. (error "LaTeX to MathML converter not configured"))
  15369. (setq executables-checked t))
  15370. (setq txt (match-string n)
  15371. beg (match-beginning n) end (match-end n)
  15372. cnt (1+ cnt))
  15373. (if msg (message msg cnt))
  15374. (goto-char beg)
  15375. (delete-region beg end)
  15376. (insert (org-format-latex-as-mathml
  15377. txt block-type prefix dir)))
  15378. (t
  15379. (error "Unknown conversion type %s for latex fragments"
  15380. processing-type)))))))))
  15381. (defun org-create-math-formula (latex-frag &optional mathml-file)
  15382. "Convert LATEX-FRAG to MathML and store it in MATHML-FILE.
  15383. Use `org-latex-to-mathml-convert-command'. If the conversion is
  15384. sucessful, return the portion between \"<math...> </math>\"
  15385. elements otherwise return nil. When MATHML-FILE is specified,
  15386. write the results in to that file. When invoked as an
  15387. interactive command, prompt for LATEX-FRAG, with initial value
  15388. set to the current active region and echo the results for user
  15389. inspection."
  15390. (interactive (list (let ((frag (when (region-active-p)
  15391. (buffer-substring-no-properties
  15392. (region-beginning) (region-end)))))
  15393. (read-string "LaTeX Fragment: " frag nil frag))))
  15394. (unless latex-frag (error "Invalid latex-frag"))
  15395. (let* ((tmp-in-file (file-relative-name
  15396. (make-temp-name (expand-file-name "ltxmathml-in"))))
  15397. (ignore (write-region latex-frag nil tmp-in-file))
  15398. (tmp-out-file (file-relative-name
  15399. (make-temp-name (expand-file-name "ltxmathml-out"))))
  15400. (cmd (format-spec
  15401. org-latex-to-mathml-convert-command
  15402. `((?j . ,(shell-quote-argument
  15403. (expand-file-name org-latex-to-mathml-jar-file)))
  15404. (?I . ,(shell-quote-argument tmp-in-file))
  15405. (?o . ,(shell-quote-argument tmp-out-file)))))
  15406. mathml shell-command-output)
  15407. (when (org-called-interactively-p 'any)
  15408. (unless (org-format-latex-mathml-available-p)
  15409. (error "LaTeX to MathML converter not configured")))
  15410. (message "Running %s" cmd)
  15411. (setq shell-command-output (shell-command-to-string cmd))
  15412. (setq mathml
  15413. (when (file-readable-p tmp-out-file)
  15414. (with-current-buffer (find-file-noselect tmp-out-file t)
  15415. (goto-char (point-min))
  15416. (when (re-search-forward
  15417. (concat
  15418. (regexp-quote
  15419. "<math xmlns=\"http://www.w3.org/1998/Math/MathML\">")
  15420. "\\(.\\|\n\\)*"
  15421. (regexp-quote "</math>")) nil t)
  15422. (prog1 (match-string 0) (kill-buffer))))))
  15423. (cond
  15424. (mathml
  15425. (setq mathml
  15426. (concat "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" mathml))
  15427. (when mathml-file
  15428. (write-region mathml nil mathml-file))
  15429. (when (org-called-interactively-p 'any)
  15430. (message mathml)))
  15431. ((message "LaTeX to MathML conversion failed")
  15432. (message shell-command-output)))
  15433. (delete-file tmp-in-file)
  15434. (when (file-exists-p tmp-out-file)
  15435. (delete-file tmp-out-file))
  15436. mathml))
  15437. (defun org-format-latex-as-mathml (latex-frag latex-frag-type
  15438. prefix &optional dir)
  15439. "Use `org-create-math-formula' but check local cache first."
  15440. (let* ((absprefix (expand-file-name prefix dir))
  15441. (print-length nil) (print-level nil)
  15442. (formula-id (concat
  15443. "formula-"
  15444. (sha1
  15445. (prin1-to-string
  15446. (list latex-frag
  15447. org-latex-to-mathml-convert-command)))))
  15448. (formula-cache (format "%s-%s.mathml" absprefix formula-id))
  15449. (formula-cache-dir (file-name-directory formula-cache)))
  15450. (unless (file-directory-p formula-cache-dir)
  15451. (make-directory formula-cache-dir t))
  15452. (unless (file-exists-p formula-cache)
  15453. (org-create-math-formula latex-frag formula-cache))
  15454. (if (file-exists-p formula-cache)
  15455. ;; Successful conversion. Return the link to MathML file.
  15456. (org-add-props
  15457. (format "[[file:%s]]" (file-relative-name formula-cache dir))
  15458. (list 'org-latex-src (replace-regexp-in-string "\"" "" latex-frag)
  15459. 'org-latex-src-embed-type (if latex-frag-type
  15460. 'paragraph 'character)))
  15461. ;; Failed conversion. Return the LaTeX fragment verbatim
  15462. (add-text-properties
  15463. 0 (1- (length latex-frag)) '(org-protected t) latex-frag)
  15464. latex-frag)))
  15465. ;; This function borrows from Ganesh Swami's latex2png.el
  15466. (defun org-create-formula-image (string tofile options buffer)
  15467. "This calls dvipng."
  15468. (require 'org-latex)
  15469. (let* ((tmpdir (if (featurep 'xemacs)
  15470. (temp-directory)
  15471. temporary-file-directory))
  15472. (texfilebase (make-temp-name
  15473. (expand-file-name "orgtex" tmpdir)))
  15474. (texfile (concat texfilebase ".tex"))
  15475. (dvifile (concat texfilebase ".dvi"))
  15476. (pngfile (concat texfilebase ".png"))
  15477. (fnh (if (featurep 'xemacs)
  15478. (font-height (get-face-font 'default))
  15479. (face-attribute 'default :height nil)))
  15480. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  15481. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  15482. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  15483. "Black"))
  15484. (bg (or (plist-get options (if buffer :background :html-background))
  15485. "Transparent")))
  15486. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  15487. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  15488. (with-temp-file texfile
  15489. (insert (org-splice-latex-header
  15490. org-format-latex-header
  15491. org-export-latex-default-packages-alist
  15492. org-export-latex-packages-alist t
  15493. org-format-latex-header-extra))
  15494. (insert "\n\\begin{document}\n" string "\n\\end{document}\n")
  15495. (require 'org-latex)
  15496. (org-export-latex-fix-inputenc))
  15497. (let ((dir default-directory))
  15498. (condition-case nil
  15499. (progn
  15500. (cd tmpdir)
  15501. (call-process "latex" nil nil nil texfile))
  15502. (error nil))
  15503. (cd dir))
  15504. (if (not (file-exists-p dvifile))
  15505. (progn (message "Failed to create dvi file from %s" texfile) nil)
  15506. (condition-case nil
  15507. (call-process "dvipng" nil nil nil
  15508. "-fg" fg "-bg" bg
  15509. "-D" dpi
  15510. ;;"-x" scale "-y" scale
  15511. "-T" "tight"
  15512. "-o" pngfile
  15513. dvifile)
  15514. (error nil))
  15515. (if (not (file-exists-p pngfile))
  15516. (if org-format-latex-signal-error
  15517. (error "Failed to create png file from %s" texfile)
  15518. (message "Failed to create png file from %s" texfile)
  15519. nil)
  15520. ;; Use the requested file name and clean up
  15521. (copy-file pngfile tofile 'replace)
  15522. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  15523. (delete-file (concat texfilebase e)))
  15524. pngfile))))
  15525. (defun org-splice-latex-header (tpl def-pkg pkg snippets-p &optional extra)
  15526. "Fill a LaTeX header template TPL.
  15527. In the template, the following place holders will be recognized:
  15528. [DEFAULT-PACKAGES] \\usepackage statements for DEF-PKG
  15529. [NO-DEFAULT-PACKAGES] do not include DEF-PKG
  15530. [PACKAGES] \\usepackage statements for PKG
  15531. [NO-PACKAGES] do not include PKG
  15532. [EXTRA] the string EXTRA
  15533. [NO-EXTRA] do not include EXTRA
  15534. For backward compatibility, if both the positive and the negative place
  15535. holder is missing, the positive one (without the \"NO-\") will be
  15536. assumed to be present at the end of the template.
  15537. DEF-PKG and PKG are assumed to be alists of options/packagename lists.
  15538. EXTRA is a string.
  15539. SNIPPETS-P indicates if this is run to create snippet images for HTML."
  15540. (let (rpl (end ""))
  15541. (if (string-match "^[ \t]*\\[\\(NO-\\)?DEFAULT-PACKAGES\\][ \t]*\n?" tpl)
  15542. (setq rpl (if (or (match-end 1) (not def-pkg))
  15543. "" (org-latex-packages-to-string def-pkg snippets-p t))
  15544. tpl (replace-match rpl t t tpl))
  15545. (if def-pkg (setq end (org-latex-packages-to-string def-pkg snippets-p))))
  15546. (if (string-match "\\[\\(NO-\\)?PACKAGES\\][ \t]*\n?" tpl)
  15547. (setq rpl (if (or (match-end 1) (not pkg))
  15548. "" (org-latex-packages-to-string pkg snippets-p t))
  15549. tpl (replace-match rpl t t tpl))
  15550. (if pkg (setq end
  15551. (concat end "\n"
  15552. (org-latex-packages-to-string pkg snippets-p)))))
  15553. (if (string-match "\\[\\(NO-\\)?EXTRA\\][ \t]*\n?" tpl)
  15554. (setq rpl (if (or (match-end 1) (not extra))
  15555. "" (concat extra "\n"))
  15556. tpl (replace-match rpl t t tpl))
  15557. (if (and extra (string-match "\\S-" extra))
  15558. (setq end (concat end "\n" extra))))
  15559. (if (string-match "\\S-" end)
  15560. (concat tpl "\n" end)
  15561. tpl)))
  15562. (defun org-latex-packages-to-string (pkg &optional snippets-p newline)
  15563. "Turn an alist of packages into a string with the \\usepackage macros."
  15564. (setq pkg (mapconcat (lambda(p)
  15565. (cond
  15566. ((stringp p) p)
  15567. ((and snippets-p (>= (length p) 3) (not (nth 2 p)))
  15568. (format "%% Package %s omitted" (cadr p)))
  15569. ((equal "" (car p))
  15570. (format "\\usepackage{%s}" (cadr p)))
  15571. (t
  15572. (format "\\usepackage[%s]{%s}"
  15573. (car p) (cadr p)))))
  15574. pkg
  15575. "\n"))
  15576. (if newline (concat pkg "\n") pkg))
  15577. (defun org-dvipng-color (attr)
  15578. "Return an rgb color specification for dvipng."
  15579. (apply 'format "rgb %s %s %s"
  15580. (mapcar 'org-normalize-color
  15581. (color-values (face-attribute 'default attr nil)))))
  15582. (defun org-normalize-color (value)
  15583. "Return string to be used as color value for an RGB component."
  15584. (format "%g" (/ value 65535.0)))
  15585. ;; Image display
  15586. (defvar org-inline-image-overlays nil)
  15587. (make-variable-buffer-local 'org-inline-image-overlays)
  15588. (defun org-toggle-inline-images (&optional include-linked)
  15589. "Toggle the display of inline images.
  15590. INCLUDE-LINKED is passed to `org-display-inline-images'."
  15591. (interactive "P")
  15592. (if org-inline-image-overlays
  15593. (progn
  15594. (org-remove-inline-images)
  15595. (message "Inline image display turned off"))
  15596. (org-display-inline-images include-linked)
  15597. (if org-inline-image-overlays
  15598. (message "%d images displayed inline"
  15599. (length org-inline-image-overlays))
  15600. (message "No images to display inline"))))
  15601. (defun org-display-inline-images (&optional include-linked refresh beg end)
  15602. "Display inline images.
  15603. Normally only links without a description part are inlined, because this
  15604. is how it will work for export. When INCLUDE-LINKED is set, also links
  15605. with a description part will be inlined. This can be nice for a quick
  15606. look at those images, but it does not reflect what exported files will look
  15607. like.
  15608. When REFRESH is set, refresh existing images between BEG and END.
  15609. This will create new image displays only if necessary.
  15610. BEG and END default to the buffer boundaries."
  15611. (interactive "P")
  15612. (unless refresh
  15613. (org-remove-inline-images)
  15614. (if (fboundp 'clear-image-cache) (clear-image-cache)))
  15615. (save-excursion
  15616. (save-restriction
  15617. (widen)
  15618. (setq beg (or beg (point-min)) end (or end (point-max)))
  15619. (goto-char (point-min))
  15620. (let ((re (concat "\\[\\[\\(\\(file:\\)\\|\\([./~]\\)\\)\\([^]\n]+?"
  15621. (substring (org-image-file-name-regexp) 0 -2)
  15622. "\\)\\]" (if include-linked "" "\\]")))
  15623. old file ov img)
  15624. (while (re-search-forward re end t)
  15625. (setq old (get-char-property-and-overlay (match-beginning 1)
  15626. 'org-image-overlay))
  15627. (setq file (expand-file-name
  15628. (concat (or (match-string 3) "") (match-string 4))))
  15629. (when (file-exists-p file)
  15630. (if (and (car-safe old) refresh)
  15631. (image-refresh (overlay-get (cdr old) 'display))
  15632. (setq img (save-match-data (create-image file)))
  15633. (when img
  15634. (setq ov (make-overlay (match-beginning 0) (match-end 0)))
  15635. (overlay-put ov 'display img)
  15636. (overlay-put ov 'face 'default)
  15637. (overlay-put ov 'org-image-overlay t)
  15638. (overlay-put ov 'modification-hooks
  15639. (list 'org-display-inline-modification-hook))
  15640. (push ov org-inline-image-overlays)))))))))
  15641. (defun org-display-inline-modification-hook (ov after beg end &optional len)
  15642. "Remove inline-display overlay if a corresponding region is modified."
  15643. (let ((inhibit-modification-hooks t))
  15644. (when (and ov after)
  15645. (delete ov org-inline-image-overlays)
  15646. (delete-overlay ov))))
  15647. (defun org-remove-inline-images ()
  15648. "Remove inline display of images."
  15649. (interactive)
  15650. (mapc 'delete-overlay org-inline-image-overlays)
  15651. (setq org-inline-image-overlays nil))
  15652. ;;;; Key bindings
  15653. ;; Remap outline keys
  15654. (define-key org-mode-map [remap outline-promote] 'org-promote-subtree)
  15655. (define-key org-mode-map [remap outline-demote] 'org-demote-subtree)
  15656. ;; Make `C-c C-x' a prefix key
  15657. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  15658. ;; TAB key with modifiers
  15659. (org-defkey org-mode-map "\C-i" 'org-cycle)
  15660. (org-defkey org-mode-map [(tab)] 'org-cycle)
  15661. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  15662. (org-defkey org-mode-map [(meta tab)] 'pcomplete)
  15663. (org-defkey org-mode-map "\M-\t" 'pcomplete)
  15664. (org-defkey org-mode-map "\M-\C-i" 'pcomplete)
  15665. ;; The following line is necessary under Suse GNU/Linux
  15666. (unless (featurep 'xemacs)
  15667. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  15668. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  15669. (define-key org-mode-map [backtab] 'org-shifttab)
  15670. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  15671. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  15672. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  15673. ;; Cursor keys with modifiers
  15674. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  15675. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  15676. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  15677. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  15678. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  15679. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  15680. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  15681. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  15682. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  15683. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  15684. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  15685. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  15686. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  15687. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  15688. (org-defkey org-mode-map [(control shift up)] 'org-shiftcontrolup)
  15689. (org-defkey org-mode-map [(control shift down)] 'org-shiftcontroldown)
  15690. ;; Babel keys
  15691. (define-key org-mode-map org-babel-key-prefix org-babel-map)
  15692. (mapc (lambda (pair)
  15693. (define-key org-babel-map (car pair) (cdr pair)))
  15694. org-babel-key-bindings)
  15695. ;;; Extra keys for tty access.
  15696. ;; We only set them when really needed because otherwise the
  15697. ;; menus don't show the simple keys
  15698. (when (or org-use-extra-keys
  15699. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  15700. (not window-system))
  15701. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  15702. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  15703. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  15704. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  15705. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  15706. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  15707. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  15708. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  15709. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  15710. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  15711. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  15712. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  15713. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  15714. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  15715. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  15716. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  15717. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  15718. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  15719. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  15720. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  15721. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  15722. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
  15723. (org-defkey org-mode-map [?\e (tab)] 'pcomplete)
  15724. (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
  15725. (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
  15726. (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
  15727. (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
  15728. (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
  15729. ;; All the other keys
  15730. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  15731. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  15732. (if (boundp 'narrow-map)
  15733. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  15734. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  15735. (if (boundp 'narrow-map)
  15736. (org-defkey narrow-map "b" 'org-narrow-to-block)
  15737. (org-defkey org-mode-map "\C-xnb" 'org-narrow-to-block))
  15738. (org-defkey org-mode-map "\C-c\C-f" 'org-forward-same-level)
  15739. (org-defkey org-mode-map "\C-c\C-b" 'org-backward-same-level)
  15740. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  15741. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  15742. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
  15743. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  15744. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  15745. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  15746. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  15747. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  15748. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  15749. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  15750. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  15751. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  15752. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  15753. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  15754. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  15755. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  15756. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  15757. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  15758. (org-defkey org-mode-map "\C-c\C-xv" 'org-copy-visible)
  15759. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  15760. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  15761. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  15762. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  15763. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  15764. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  15765. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  15766. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  15767. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  15768. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  15769. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  15770. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  15771. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  15772. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  15773. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  15774. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  15775. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  15776. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  15777. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  15778. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  15779. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  15780. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  15781. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  15782. (org-defkey org-mode-map "\C-c^" 'org-sort)
  15783. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  15784. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  15785. (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
  15786. (org-defkey org-mode-map "\C-m" 'org-return)
  15787. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  15788. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  15789. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  15790. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  15791. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  15792. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  15793. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  15794. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  15795. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  15796. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  15797. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  15798. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  15799. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  15800. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  15801. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  15802. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  15803. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  15804. (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
  15805. (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
  15806. (org-defkey org-mode-map "\C-c@" 'org-mark-subtree)
  15807. (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
  15808. ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
  15809. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-mark-entry-for-agenda-action)
  15810. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  15811. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  15812. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  15813. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  15814. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  15815. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  15816. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  15817. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  15818. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  15819. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  15820. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  15821. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  15822. (org-defkey org-mode-map "\C-c\C-x\C-v" 'org-toggle-inline-images)
  15823. (org-defkey org-mode-map "\C-c\C-x\\" 'org-toggle-pretty-entities)
  15824. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  15825. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  15826. (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
  15827. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  15828. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  15829. (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
  15830. (org-defkey org-mode-map [(control ?c) (control ?x) ?\:] 'org-timer-cancel-timer)
  15831. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  15832. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  15833. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  15834. (org-defkey org-mode-map "\C-c\C-x_" 'org-timer-stop)
  15835. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  15836. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  15837. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  15838. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  15839. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  15840. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  15841. (when (featurep 'xemacs)
  15842. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  15843. (defconst org-speed-commands-default
  15844. '(
  15845. ("Outline Navigation")
  15846. ("n" . (org-speed-move-safe 'outline-next-visible-heading))
  15847. ("p" . (org-speed-move-safe 'outline-previous-visible-heading))
  15848. ("f" . (org-speed-move-safe 'org-forward-same-level))
  15849. ("b" . (org-speed-move-safe 'org-backward-same-level))
  15850. ("u" . (org-speed-move-safe 'outline-up-heading))
  15851. ("j" . org-goto)
  15852. ("g" . (org-refile t))
  15853. ("Outline Visibility")
  15854. ("c" . org-cycle)
  15855. ("C" . org-shifttab)
  15856. (" " . org-display-outline-path)
  15857. ("Outline Structure Editing")
  15858. ("U" . org-shiftmetaup)
  15859. ("D" . org-shiftmetadown)
  15860. ("r" . org-metaright)
  15861. ("l" . org-metaleft)
  15862. ("R" . org-shiftmetaright)
  15863. ("L" . org-shiftmetaleft)
  15864. ("i" . (progn (forward-char 1) (call-interactively
  15865. 'org-insert-heading-respect-content)))
  15866. ("^" . org-sort)
  15867. ("w" . org-refile)
  15868. ("a" . org-archive-subtree-default-with-confirmation)
  15869. ("." . org-mark-subtree)
  15870. ("Clock Commands")
  15871. ("I" . org-clock-in)
  15872. ("O" . org-clock-out)
  15873. ("Meta Data Editing")
  15874. ("t" . org-todo)
  15875. ("0" . (org-priority ?\ ))
  15876. ("1" . (org-priority ?A))
  15877. ("2" . (org-priority ?B))
  15878. ("3" . (org-priority ?C))
  15879. (";" . org-set-tags-command)
  15880. ("e" . org-set-effort)
  15881. ("Agenda Views etc")
  15882. ("v" . org-agenda)
  15883. ("/" . org-sparse-tree)
  15884. ("Misc")
  15885. ("o" . org-open-at-point)
  15886. ("?" . org-speed-command-help)
  15887. ("<" . (org-agenda-set-restriction-lock 'subtree))
  15888. (">" . (org-agenda-remove-restriction-lock))
  15889. )
  15890. "The default speed commands.")
  15891. (defun org-print-speed-command (e)
  15892. (if (> (length (car e)) 1)
  15893. (progn
  15894. (princ "\n")
  15895. (princ (car e))
  15896. (princ "\n")
  15897. (princ (make-string (length (car e)) ?-))
  15898. (princ "\n"))
  15899. (princ (car e))
  15900. (princ " ")
  15901. (if (symbolp (cdr e))
  15902. (princ (symbol-name (cdr e)))
  15903. (prin1 (cdr e)))
  15904. (princ "\n")))
  15905. (defun org-speed-command-help ()
  15906. "Show the available speed commands."
  15907. (interactive)
  15908. (if (not org-use-speed-commands)
  15909. (error "Speed commands are not activated, customize `org-use-speed-commands'")
  15910. (with-output-to-temp-buffer "*Help*"
  15911. (princ "User-defined Speed commands\n===========================\n")
  15912. (mapc 'org-print-speed-command org-speed-commands-user)
  15913. (princ "\n")
  15914. (princ "Built-in Speed commands\n=======================\n")
  15915. (mapc 'org-print-speed-command org-speed-commands-default))
  15916. (with-current-buffer "*Help*"
  15917. (setq truncate-lines t))))
  15918. (defun org-speed-move-safe (cmd)
  15919. "Execute CMD, but make sure that the cursor always ends up in a headline.
  15920. If not, return to the original position and throw an error."
  15921. (interactive)
  15922. (let ((pos (point)))
  15923. (call-interactively cmd)
  15924. (unless (and (bolp) (org-on-heading-p))
  15925. (goto-char pos)
  15926. (error "Boundary reached while executing %s" cmd))))
  15927. (defvar org-self-insert-command-undo-counter 0)
  15928. (defvar org-table-auto-blank-field) ; defined in org-table.el
  15929. (defvar org-speed-command nil)
  15930. (defun org-speed-command-default-hook (keys)
  15931. "Hook for activating single-letter speed commands.
  15932. `org-speed-commands-default' specifies a minimal command set.
  15933. Use `org-speed-commands-user' for further customization."
  15934. (when (or (and (bolp) (looking-at org-outline-regexp))
  15935. (and (functionp org-use-speed-commands)
  15936. (funcall org-use-speed-commands)))
  15937. (cdr (assoc keys (append org-speed-commands-user
  15938. org-speed-commands-default)))))
  15939. (defun org-babel-speed-command-hook (keys)
  15940. "Hook for activating single-letter code block commands."
  15941. (when (and (bolp) (looking-at org-babel-src-block-regexp))
  15942. (cdr (assoc keys org-babel-key-bindings))))
  15943. (defcustom org-speed-command-hook
  15944. '(org-speed-command-default-hook org-babel-speed-command-hook)
  15945. "Hook for activating speed commands at strategic locations.
  15946. Hook functions are called in sequence until a valid handler is
  15947. found.
  15948. Each hook takes a single argument, a user-pressed command key
  15949. which is also a `self-insert-command' from the global map.
  15950. Within the hook, examine the cursor position and the command key
  15951. and return nil or a valid handler as appropriate. Handler could
  15952. be one of an interactive command, a function, or a form.
  15953. Set `org-use-speed-commands' to non-nil value to enable this
  15954. hook. The default setting is `org-speed-command-default-hook'."
  15955. :group 'org-structure
  15956. :type 'hook)
  15957. (defun org-self-insert-command (N)
  15958. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  15959. If the cursor is in a table looking at whitespace, the whitespace is
  15960. overwritten, and the table is not marked as requiring realignment."
  15961. (interactive "p")
  15962. (org-check-before-invisible-edit 'insert)
  15963. (cond
  15964. ((and org-use-speed-commands
  15965. (setq org-speed-command
  15966. (run-hook-with-args-until-success
  15967. 'org-speed-command-hook (this-command-keys))))
  15968. (cond
  15969. ((commandp org-speed-command)
  15970. (setq this-command org-speed-command)
  15971. (call-interactively org-speed-command))
  15972. ((functionp org-speed-command)
  15973. (funcall org-speed-command))
  15974. ((and org-speed-command (listp org-speed-command))
  15975. (eval org-speed-command))
  15976. (t (let (org-use-speed-commands)
  15977. (call-interactively 'org-self-insert-command)))))
  15978. ((and
  15979. (org-table-p)
  15980. (progn
  15981. ;; check if we blank the field, and if that triggers align
  15982. (and (featurep 'org-table) org-table-auto-blank-field
  15983. (member last-command
  15984. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c yas/expand))
  15985. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  15986. ;; got extra space, this field does not determine column width
  15987. (let (org-table-may-need-update) (org-table-blank-field))
  15988. ;; no extra space, this field may determine column width
  15989. (org-table-blank-field)))
  15990. t)
  15991. (eq N 1)
  15992. (looking-at "[^|\n]* |"))
  15993. (let (org-table-may-need-update)
  15994. (goto-char (1- (match-end 0)))
  15995. (delete-char -1)
  15996. (goto-char (match-beginning 0))
  15997. (self-insert-command N)))
  15998. (t
  15999. (setq org-table-may-need-update t)
  16000. (self-insert-command N)
  16001. (org-fix-tags-on-the-fly)
  16002. (if org-self-insert-cluster-for-undo
  16003. (if (not (eq last-command 'org-self-insert-command))
  16004. (setq org-self-insert-command-undo-counter 1)
  16005. (if (>= org-self-insert-command-undo-counter 20)
  16006. (setq org-self-insert-command-undo-counter 1)
  16007. (and (> org-self-insert-command-undo-counter 0)
  16008. buffer-undo-list (listp buffer-undo-list)
  16009. (not (cadr buffer-undo-list)) ; remove nil entry
  16010. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  16011. (setq org-self-insert-command-undo-counter
  16012. (1+ org-self-insert-command-undo-counter))))))))
  16013. (defun org-check-before-invisible-edit (kind)
  16014. "Check is editing if kind KIND would be dangerous with invisible text around.
  16015. The detailed reaction depends on the user option `org-catch-invisible-edits'."
  16016. ;; First, try to get out of here as quickly as possible, to reduce overhead
  16017. (if (and org-catch-invisible-edits
  16018. (or (not (boundp 'visible-mode)) (not visible-mode))
  16019. (or (get-char-property (point) 'invisible)
  16020. (get-char-property (max (point-min) (1- (point))) 'invisible)))
  16021. ;; OK, we need to take a closer look
  16022. (let* ((invisible-at-point (get-char-property (point) 'invisible))
  16023. (invisible-before-point (if (bobp) nil (get-char-property
  16024. (1- (point)) 'invisible)))
  16025. (border-and-ok-direction
  16026. (or
  16027. ;; Check if we are acting predictably before invisible text
  16028. (and invisible-at-point (not invisible-before-point)
  16029. (memq kind '(insert delete-backward)))
  16030. ;; Check if we are acting predictably after invisible text
  16031. ;; This works not well, and I have turned it off. It seems
  16032. ;; better to always show and stop after invisible text.
  16033. ;; (and (not invisible-at-point) invisible-before-point
  16034. ;; (memq kind '(insert delete)))
  16035. )))
  16036. (when (or (memq invisible-at-point '(outline org-hide-block))
  16037. (memq invisible-before-point '(outline org-hide-block)))
  16038. (if (eq org-catch-invisible-edits 'error)
  16039. (error "Editing in invisible areas is prohibited - make visible first"))
  16040. ;; Make the area visible
  16041. (save-excursion
  16042. (if invisible-before-point
  16043. (goto-char (previous-single-char-property-change
  16044. (point) 'invisible)))
  16045. (org-cycle))
  16046. (cond
  16047. ((eq org-catch-invisible-edits 'show)
  16048. ;; That's it, we do the edit after showing
  16049. (message
  16050. "Unfolding invisible region around point before editing")
  16051. (sit-for 1))
  16052. ((and (eq org-catch-invisible-edits 'smart)
  16053. border-and-ok-direction)
  16054. (message "Unfolding invisible region around point before editing"))
  16055. (t
  16056. ;; Don't do the edit, make the user repeat it in full visibility
  16057. (error "Edit in invisible region aborted, repeat to confirm with text visible")))))))
  16058. (defun org-fix-tags-on-the-fly ()
  16059. (when (and (equal (char-after (point-at-bol)) ?*)
  16060. (org-on-heading-p))
  16061. (org-align-tags-here org-tags-column)))
  16062. (defun org-delete-backward-char (N)
  16063. "Like `delete-backward-char', insert whitespace at field end in tables.
  16064. When deleting backwards, in tables this function will insert whitespace in
  16065. front of the next \"|\" separator, to keep the table aligned. The table will
  16066. still be marked for re-alignment if the field did fill the entire column,
  16067. because, in this case the deletion might narrow the column."
  16068. (interactive "p")
  16069. (org-check-before-invisible-edit 'delete-backward)
  16070. (if (and (org-table-p)
  16071. (eq N 1)
  16072. (string-match "|" (buffer-substring (point-at-bol) (point)))
  16073. (looking-at ".*?|"))
  16074. (let ((pos (point))
  16075. (noalign (looking-at "[^|\n\r]* |"))
  16076. (c org-table-may-need-update))
  16077. (backward-delete-char N)
  16078. (if (not overwrite-mode)
  16079. (progn
  16080. (skip-chars-forward "^|")
  16081. (insert " ")
  16082. (goto-char (1- pos))))
  16083. ;; noalign: if there were two spaces at the end, this field
  16084. ;; does not determine the width of the column.
  16085. (if noalign (setq org-table-may-need-update c)))
  16086. (backward-delete-char N)
  16087. (org-fix-tags-on-the-fly)))
  16088. (defun org-delete-char (N)
  16089. "Like `delete-char', but insert whitespace at field end in tables.
  16090. When deleting characters, in tables this function will insert whitespace in
  16091. front of the next \"|\" separator, to keep the table aligned. The table will
  16092. still be marked for re-alignment if the field did fill the entire column,
  16093. because, in this case the deletion might narrow the column."
  16094. (interactive "p")
  16095. (org-check-before-invisible-edit 'delete)
  16096. (if (and (org-table-p)
  16097. (not (bolp))
  16098. (not (= (char-after) ?|))
  16099. (eq N 1))
  16100. (if (looking-at ".*?|")
  16101. (let ((pos (point))
  16102. (noalign (looking-at "[^|\n\r]* |"))
  16103. (c org-table-may-need-update))
  16104. (replace-match (concat
  16105. (substring (match-string 0) 1 -1)
  16106. " |"))
  16107. (goto-char pos)
  16108. ;; noalign: if there were two spaces at the end, this field
  16109. ;; does not determine the width of the column.
  16110. (if noalign (setq org-table-may-need-update c)))
  16111. (delete-char N))
  16112. (delete-char N)
  16113. (org-fix-tags-on-the-fly)))
  16114. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  16115. (put 'org-self-insert-command 'delete-selection t)
  16116. (put 'orgtbl-self-insert-command 'delete-selection t)
  16117. (put 'org-delete-char 'delete-selection 'supersede)
  16118. (put 'org-delete-backward-char 'delete-selection 'supersede)
  16119. (put 'org-yank 'delete-selection 'yank)
  16120. ;; Make `flyspell-mode' delay after some commands
  16121. (put 'org-self-insert-command 'flyspell-delayed t)
  16122. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  16123. (put 'org-delete-char 'flyspell-delayed t)
  16124. (put 'org-delete-backward-char 'flyspell-delayed t)
  16125. ;; Make pabbrev-mode expand after org-mode commands
  16126. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  16127. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  16128. ;; How to do this: Measure non-white length of current string
  16129. ;; If equal to column width, we should realign.
  16130. (defun org-remap (map &rest commands)
  16131. "In MAP, remap the functions given in COMMANDS.
  16132. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  16133. (let (new old)
  16134. (while commands
  16135. (setq old (pop commands) new (pop commands))
  16136. (if (fboundp 'command-remapping)
  16137. (org-defkey map (vector 'remap old) new)
  16138. (substitute-key-definition old new map global-map)))))
  16139. (when (eq org-enable-table-editor 'optimized)
  16140. ;; If the user wants maximum table support, we need to hijack
  16141. ;; some standard editing functions
  16142. (org-remap org-mode-map
  16143. 'self-insert-command 'org-self-insert-command
  16144. 'delete-char 'org-delete-char
  16145. 'delete-backward-char 'org-delete-backward-char)
  16146. (org-defkey org-mode-map "|" 'org-force-self-insert))
  16147. (defvar org-ctrl-c-ctrl-c-hook nil
  16148. "Hook for functions attaching themselves to `C-c C-c'.
  16149. This can be used to add additional functionality to the C-c C-c
  16150. key which executes context-dependent commands. This hook is run
  16151. before any other test, while `org-ctrl-c-ctrl-c-final-hook' is
  16152. run after the last test.
  16153. Each function will be called with no arguments. The function
  16154. must check if the context is appropriate for it to act. If yes,
  16155. it should do its thing and then return a non-nil value. If the
  16156. context is wrong, just do nothing and return nil.")
  16157. (defvar org-ctrl-c-ctrl-c-final-hook nil
  16158. "Hook for functions attaching themselves to `C-c C-c'.
  16159. This can be used to add additional functionality to the C-c C-c
  16160. key which executes context-dependent commands. This hook is run
  16161. after any other test, while `org-ctrl-c-ctrl-c-hook' is run
  16162. before the first test.
  16163. Each function will be called with no arguments. The function
  16164. must check if the context is appropriate for it to act. If yes,
  16165. it should do its thing and then return a non-nil value. If the
  16166. context is wrong, just do nothing and return nil.")
  16167. (defvar org-tab-first-hook nil
  16168. "Hook for functions to attach themselves to TAB.
  16169. See `org-ctrl-c-ctrl-c-hook' for more information.
  16170. This hook runs as the first action when TAB is pressed, even before
  16171. `org-cycle' messes around with the `outline-regexp' to cater for
  16172. inline tasks and plain list item folding.
  16173. If any function in this hook returns t, any other actions that
  16174. would have been caused by TAB (such as table field motion or visibility
  16175. cycling) will not occur.")
  16176. (defvar org-tab-after-check-for-table-hook nil
  16177. "Hook for functions to attach themselves to TAB.
  16178. See `org-ctrl-c-ctrl-c-hook' for more information.
  16179. This hook runs after it has been established that the cursor is not in a
  16180. table, but before checking if the cursor is in a headline or if global cycling
  16181. should be done.
  16182. If any function in this hook returns t, not other actions like visibility
  16183. cycling will be done.")
  16184. (defvar org-tab-after-check-for-cycling-hook nil
  16185. "Hook for functions to attach themselves to TAB.
  16186. See `org-ctrl-c-ctrl-c-hook' for more information.
  16187. This hook runs after it has been established that not table field motion and
  16188. not visibility should be done because of current context. This is probably
  16189. the place where a package like yasnippets can hook in.")
  16190. (defvar org-tab-before-tab-emulation-hook nil
  16191. "Hook for functions to attach themselves to TAB.
  16192. See `org-ctrl-c-ctrl-c-hook' for more information.
  16193. This hook runs after every other options for TAB have been exhausted, but
  16194. before indentation and \t insertion takes place.")
  16195. (defvar org-metaleft-hook nil
  16196. "Hook for functions attaching themselves to `M-left'.
  16197. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16198. (defvar org-metaright-hook nil
  16199. "Hook for functions attaching themselves to `M-right'.
  16200. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16201. (defvar org-metaup-hook nil
  16202. "Hook for functions attaching themselves to `M-up'.
  16203. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16204. (defvar org-metadown-hook nil
  16205. "Hook for functions attaching themselves to `M-down'.
  16206. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16207. (defvar org-shiftmetaleft-hook nil
  16208. "Hook for functions attaching themselves to `M-S-left'.
  16209. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16210. (defvar org-shiftmetaright-hook nil
  16211. "Hook for functions attaching themselves to `M-S-right'.
  16212. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16213. (defvar org-shiftmetaup-hook nil
  16214. "Hook for functions attaching themselves to `M-S-up'.
  16215. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16216. (defvar org-shiftmetadown-hook nil
  16217. "Hook for functions attaching themselves to `M-S-down'.
  16218. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16219. (defvar org-metareturn-hook nil
  16220. "Hook for functions attaching themselves to `M-RET'.
  16221. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16222. (defvar org-shiftup-hook nil
  16223. "Hook for functions attaching themselves to `S-up'.
  16224. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16225. (defvar org-shiftup-final-hook nil
  16226. "Hook for functions attaching themselves to `S-up'.
  16227. This one runs after all other options except shift-select have been excluded.
  16228. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16229. (defvar org-shiftdown-hook nil
  16230. "Hook for functions attaching themselves to `S-down'.
  16231. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16232. (defvar org-shiftdown-final-hook nil
  16233. "Hook for functions attaching themselves to `S-down'.
  16234. This one runs after all other options except shift-select have been excluded.
  16235. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16236. (defvar org-shiftleft-hook nil
  16237. "Hook for functions attaching themselves to `S-left'.
  16238. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16239. (defvar org-shiftleft-final-hook nil
  16240. "Hook for functions attaching themselves to `S-left'.
  16241. This one runs after all other options except shift-select have been excluded.
  16242. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16243. (defvar org-shiftright-hook nil
  16244. "Hook for functions attaching themselves to `S-right'.
  16245. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16246. (defvar org-shiftright-final-hook nil
  16247. "Hook for functions attaching themselves to `S-right'.
  16248. This one runs after all other options except shift-select have been excluded.
  16249. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16250. (defun org-modifier-cursor-error ()
  16251. "Throw an error, a modified cursor command was applied in wrong context."
  16252. (error "This command is active in special context like tables, headlines or items"))
  16253. (defun org-shiftselect-error ()
  16254. "Throw an error because Shift-Cursor command was applied in wrong context."
  16255. (if (and (boundp 'shift-select-mode) shift-select-mode)
  16256. (error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
  16257. (error "This command works only in special context like headlines or timestamps")))
  16258. (defun org-call-for-shift-select (cmd)
  16259. (let ((this-command-keys-shift-translated t))
  16260. (call-interactively cmd)))
  16261. (defun org-shifttab (&optional arg)
  16262. "Global visibility cycling or move to previous table field.
  16263. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  16264. on context.
  16265. See the individual commands for more information."
  16266. (interactive "P")
  16267. (cond
  16268. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  16269. ((integerp arg)
  16270. (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
  16271. (message "Content view to level: %d" arg)
  16272. (org-content (prefix-numeric-value arg2))
  16273. (setq org-cycle-global-status 'overview)))
  16274. (t (call-interactively 'org-global-cycle))))
  16275. (defun org-shiftmetaleft ()
  16276. "Promote subtree or delete table column.
  16277. Calls `org-promote-subtree', `org-outdent-item',
  16278. or `org-table-delete-column', depending on context.
  16279. See the individual commands for more information."
  16280. (interactive)
  16281. (cond
  16282. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  16283. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  16284. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  16285. ((org-at-item-p) (call-interactively 'org-outdent-item-tree))
  16286. (t (org-modifier-cursor-error))))
  16287. (defun org-shiftmetaright ()
  16288. "Demote subtree or insert table column.
  16289. Calls `org-demote-subtree', `org-indent-item',
  16290. or `org-table-insert-column', depending on context.
  16291. See the individual commands for more information."
  16292. (interactive)
  16293. (cond
  16294. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  16295. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  16296. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  16297. ((org-at-item-p) (call-interactively 'org-indent-item-tree))
  16298. (t (org-modifier-cursor-error))))
  16299. (defun org-shiftmetaup (&optional arg)
  16300. "Move subtree up or kill table row.
  16301. Calls `org-move-subtree-up' or `org-table-kill-row' or
  16302. `org-move-item-up' depending on context. See the individual commands
  16303. for more information."
  16304. (interactive "P")
  16305. (cond
  16306. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  16307. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  16308. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  16309. ((org-at-item-p) (call-interactively 'org-move-item-up))
  16310. (t (org-modifier-cursor-error))))
  16311. (defun org-shiftmetadown (&optional arg)
  16312. "Move subtree down or insert table row.
  16313. Calls `org-move-subtree-down' or `org-table-insert-row' or
  16314. `org-move-item-down', depending on context. See the individual
  16315. commands for more information."
  16316. (interactive "P")
  16317. (cond
  16318. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  16319. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  16320. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  16321. ((org-at-item-p) (call-interactively 'org-move-item-down))
  16322. (t (org-modifier-cursor-error))))
  16323. (defsubst org-hidden-tree-error ()
  16324. (error
  16325. "Hidden subtree, open with TAB or use subtree command M-S-<left>/<right>"))
  16326. (defun org-metaleft (&optional arg)
  16327. "Promote heading or move table column to left.
  16328. Calls `org-do-promote' or `org-table-move-column', depending on context.
  16329. With no specific context, calls the Emacs default `backward-word'.
  16330. See the individual commands for more information."
  16331. (interactive "P")
  16332. (cond
  16333. ((run-hook-with-args-until-success 'org-metaleft-hook))
  16334. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  16335. ((org-with-limited-levels
  16336. (or (org-on-heading-p)
  16337. (and (org-region-active-p)
  16338. (save-excursion
  16339. (goto-char (region-beginning))
  16340. (org-on-heading-p)))))
  16341. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  16342. (call-interactively 'org-do-promote))
  16343. ;; At an inline task.
  16344. ((org-on-heading-p)
  16345. (call-interactively 'org-inlinetask-promote))
  16346. ((or (org-at-item-p)
  16347. (and (org-region-active-p)
  16348. (save-excursion
  16349. (goto-char (region-beginning))
  16350. (org-at-item-p))))
  16351. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  16352. (call-interactively 'org-outdent-item))
  16353. (t (call-interactively 'backward-word))))
  16354. (defun org-metaright (&optional arg)
  16355. "Demote subtree or move table column to right.
  16356. Calls `org-do-demote' or `org-table-move-column', depending on context.
  16357. With no specific context, calls the Emacs default `forward-word'.
  16358. See the individual commands for more information."
  16359. (interactive "P")
  16360. (cond
  16361. ((run-hook-with-args-until-success 'org-metaright-hook))
  16362. ((org-at-table-p) (call-interactively 'org-table-move-column))
  16363. ((org-with-limited-levels
  16364. (or (org-on-heading-p)
  16365. (and (org-region-active-p)
  16366. (save-excursion
  16367. (goto-char (region-beginning))
  16368. (org-on-heading-p)))))
  16369. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  16370. (call-interactively 'org-do-demote))
  16371. ;; At an inline task.
  16372. ((org-on-heading-p)
  16373. (call-interactively 'org-inlinetask-demote))
  16374. ((or (org-at-item-p)
  16375. (and (org-region-active-p)
  16376. (save-excursion
  16377. (goto-char (region-beginning))
  16378. (org-at-item-p))))
  16379. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  16380. (call-interactively 'org-indent-item))
  16381. (t (call-interactively 'forward-word))))
  16382. (defun org-check-for-hidden (what)
  16383. "Check if there are hidden headlines/items in the current visual line.
  16384. WHAT can be either `headlines' or `items'. If the current line is
  16385. an outline or item heading and it has a folded subtree below it,
  16386. this function returns t, nil otherwise."
  16387. (let ((re (cond
  16388. ((eq what 'headlines) org-outline-regexp-bol)
  16389. ((eq what 'items) (org-item-beginning-re))
  16390. (t (error "This should not happen"))))
  16391. beg end)
  16392. (save-excursion
  16393. (catch 'exit
  16394. (unless (org-region-active-p)
  16395. (setq beg (point-at-bol))
  16396. (beginning-of-line 2)
  16397. (while (and (not (eobp)) ;; this is like `next-line'
  16398. (get-char-property (1- (point)) 'invisible))
  16399. (beginning-of-line 2))
  16400. (setq end (point))
  16401. (goto-char beg)
  16402. (goto-char (point-at-eol))
  16403. (setq end (max end (point)))
  16404. (while (re-search-forward re end t)
  16405. (if (get-char-property (match-beginning 0) 'invisible)
  16406. (throw 'exit t))))
  16407. nil))))
  16408. (defun org-metaup (&optional arg)
  16409. "Move subtree up or move table row up.
  16410. Calls `org-move-subtree-up' or `org-table-move-row' or
  16411. `org-move-item-up', depending on context. See the individual commands
  16412. for more information."
  16413. (interactive "P")
  16414. (cond
  16415. ((run-hook-with-args-until-success 'org-metaup-hook))
  16416. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  16417. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  16418. ((org-at-item-p) (call-interactively 'org-move-item-up))
  16419. (t (transpose-lines 1) (beginning-of-line -1))))
  16420. (defun org-metadown (&optional arg)
  16421. "Move subtree down or move table row down.
  16422. Calls `org-move-subtree-down' or `org-table-move-row' or
  16423. `org-move-item-down', depending on context. See the individual
  16424. commands for more information."
  16425. (interactive "P")
  16426. (cond
  16427. ((run-hook-with-args-until-success 'org-metadown-hook))
  16428. ((org-at-table-p) (call-interactively 'org-table-move-row))
  16429. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  16430. ((org-at-item-p) (call-interactively 'org-move-item-down))
  16431. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  16432. (defun org-shiftup (&optional arg)
  16433. "Increase item in timestamp or increase priority of current headline.
  16434. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  16435. depending on context. See the individual commands for more information."
  16436. (interactive "P")
  16437. (cond
  16438. ((run-hook-with-args-until-success 'org-shiftup-hook))
  16439. ((and org-support-shift-select (org-region-active-p))
  16440. (org-call-for-shift-select 'previous-line))
  16441. ((org-at-timestamp-p t)
  16442. (call-interactively (if org-edit-timestamp-down-means-later
  16443. 'org-timestamp-down 'org-timestamp-up)))
  16444. ((and (not (eq org-support-shift-select 'always))
  16445. org-enable-priority-commands
  16446. (org-on-heading-p))
  16447. (call-interactively 'org-priority-up))
  16448. ((and (not org-support-shift-select) (org-at-item-p))
  16449. (call-interactively 'org-previous-item))
  16450. ((org-clocktable-try-shift 'up arg))
  16451. ((run-hook-with-args-until-success 'org-shiftup-final-hook))
  16452. (org-support-shift-select
  16453. (org-call-for-shift-select 'previous-line))
  16454. (t (org-shiftselect-error))))
  16455. (defun org-shiftdown (&optional arg)
  16456. "Decrease item in timestamp or decrease priority of current headline.
  16457. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  16458. depending on context. See the individual commands for more information."
  16459. (interactive "P")
  16460. (cond
  16461. ((run-hook-with-args-until-success 'org-shiftdown-hook))
  16462. ((and org-support-shift-select (org-region-active-p))
  16463. (org-call-for-shift-select 'next-line))
  16464. ((org-at-timestamp-p t)
  16465. (call-interactively (if org-edit-timestamp-down-means-later
  16466. 'org-timestamp-up 'org-timestamp-down)))
  16467. ((and (not (eq org-support-shift-select 'always))
  16468. org-enable-priority-commands
  16469. (org-on-heading-p))
  16470. (call-interactively 'org-priority-down))
  16471. ((and (not org-support-shift-select) (org-at-item-p))
  16472. (call-interactively 'org-next-item))
  16473. ((org-clocktable-try-shift 'down arg))
  16474. ((run-hook-with-args-until-success 'org-shiftdown-final-hook))
  16475. (org-support-shift-select
  16476. (org-call-for-shift-select 'next-line))
  16477. (t (org-shiftselect-error))))
  16478. (defun org-shiftright (&optional arg)
  16479. "Cycle the thing at point or in the current line, depending on context.
  16480. Depending on context, this does one of the following:
  16481. - switch a timestamp at point one day into the future
  16482. - on a headline, switch to the next TODO keyword.
  16483. - on an item, switch entire list to the next bullet type
  16484. - on a property line, switch to the next allowed value
  16485. - on a clocktable definition line, move time block into the future"
  16486. (interactive "P")
  16487. (cond
  16488. ((run-hook-with-args-until-success 'org-shiftright-hook))
  16489. ((and org-support-shift-select (org-region-active-p))
  16490. (org-call-for-shift-select 'forward-char))
  16491. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  16492. ((and (not (eq org-support-shift-select 'always))
  16493. (org-on-heading-p))
  16494. (let ((org-inhibit-logging
  16495. (not org-treat-S-cursor-todo-selection-as-state-change))
  16496. (org-inhibit-blocking
  16497. (not org-treat-S-cursor-todo-selection-as-state-change)))
  16498. (org-call-with-arg 'org-todo 'right)))
  16499. ((or (and org-support-shift-select
  16500. (not (eq org-support-shift-select 'always))
  16501. (org-at-item-bullet-p))
  16502. (and (not org-support-shift-select) (org-at-item-p)))
  16503. (org-call-with-arg 'org-cycle-list-bullet nil))
  16504. ((and (not (eq org-support-shift-select 'always))
  16505. (org-at-property-p))
  16506. (call-interactively 'org-property-next-allowed-value))
  16507. ((org-clocktable-try-shift 'right arg))
  16508. ((run-hook-with-args-until-success 'org-shiftright-final-hook))
  16509. (org-support-shift-select
  16510. (org-call-for-shift-select 'forward-char))
  16511. (t (org-shiftselect-error))))
  16512. (defun org-shiftleft (&optional arg)
  16513. "Cycle the thing at point or in the current line, depending on context.
  16514. Depending on context, this does one of the following:
  16515. - switch a timestamp at point one day into the past
  16516. - on a headline, switch to the previous TODO keyword.
  16517. - on an item, switch entire list to the previous bullet type
  16518. - on a property line, switch to the previous allowed value
  16519. - on a clocktable definition line, move time block into the past"
  16520. (interactive "P")
  16521. (cond
  16522. ((run-hook-with-args-until-success 'org-shiftleft-hook))
  16523. ((and org-support-shift-select (org-region-active-p))
  16524. (org-call-for-shift-select 'backward-char))
  16525. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  16526. ((and (not (eq org-support-shift-select 'always))
  16527. (org-on-heading-p))
  16528. (let ((org-inhibit-logging
  16529. (not org-treat-S-cursor-todo-selection-as-state-change))
  16530. (org-inhibit-blocking
  16531. (not org-treat-S-cursor-todo-selection-as-state-change)))
  16532. (org-call-with-arg 'org-todo 'left)))
  16533. ((or (and org-support-shift-select
  16534. (not (eq org-support-shift-select 'always))
  16535. (org-at-item-bullet-p))
  16536. (and (not org-support-shift-select) (org-at-item-p)))
  16537. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  16538. ((and (not (eq org-support-shift-select 'always))
  16539. (org-at-property-p))
  16540. (call-interactively 'org-property-previous-allowed-value))
  16541. ((org-clocktable-try-shift 'left arg))
  16542. ((run-hook-with-args-until-success 'org-shiftleft-final-hook))
  16543. (org-support-shift-select
  16544. (org-call-for-shift-select 'backward-char))
  16545. (t (org-shiftselect-error))))
  16546. (defun org-shiftcontrolright ()
  16547. "Switch to next TODO set."
  16548. (interactive)
  16549. (cond
  16550. ((and org-support-shift-select (org-region-active-p))
  16551. (org-call-for-shift-select 'forward-word))
  16552. ((and (not (eq org-support-shift-select 'always))
  16553. (org-on-heading-p))
  16554. (org-call-with-arg 'org-todo 'nextset))
  16555. (org-support-shift-select
  16556. (org-call-for-shift-select 'forward-word))
  16557. (t (org-shiftselect-error))))
  16558. (defun org-shiftcontrolleft ()
  16559. "Switch to previous TODO set."
  16560. (interactive)
  16561. (cond
  16562. ((and org-support-shift-select (org-region-active-p))
  16563. (org-call-for-shift-select 'backward-word))
  16564. ((and (not (eq org-support-shift-select 'always))
  16565. (org-on-heading-p))
  16566. (org-call-with-arg 'org-todo 'previousset))
  16567. (org-support-shift-select
  16568. (org-call-for-shift-select 'backward-word))
  16569. (t (org-shiftselect-error))))
  16570. (defun org-shiftcontrolup ()
  16571. "Change timestamps synchronously up in CLOCK log lines."
  16572. (interactive)
  16573. (cond ((and (not org-support-shift-select)
  16574. (org-at-clock-log-p)
  16575. (org-at-timestamp-p t))
  16576. (org-clock-timestamps-up))
  16577. (t (org-shiftselect-error))))
  16578. (defun org-shiftcontroldown ()
  16579. "Change timestamps synchronously down in CLOCK log lines."
  16580. (interactive)
  16581. (cond ((and (not org-support-shift-select)
  16582. (org-at-clock-log-p)
  16583. (org-at-timestamp-p t))
  16584. (org-clock-timestamps-down))
  16585. (t (org-shiftselect-error))))
  16586. (defun org-ctrl-c-ret ()
  16587. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  16588. (interactive)
  16589. (cond
  16590. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  16591. (t (call-interactively 'org-insert-heading))))
  16592. (defun org-find-visible ()
  16593. (let ((s (point)))
  16594. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  16595. (get-char-property s 'invisible)))
  16596. s))
  16597. (defun org-find-invisible ()
  16598. (let ((s (point)))
  16599. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  16600. (not (get-char-property s 'invisible))))
  16601. s))
  16602. (defun org-copy-visible (beg end)
  16603. "Copy the visible parts of the region."
  16604. (interactive "r")
  16605. (let (snippets s)
  16606. (save-excursion
  16607. (save-restriction
  16608. (narrow-to-region beg end)
  16609. (setq s (goto-char (point-min)))
  16610. (while (not (= (point) (point-max)))
  16611. (goto-char (org-find-invisible))
  16612. (push (buffer-substring s (point)) snippets)
  16613. (setq s (goto-char (org-find-visible))))))
  16614. (kill-new (apply 'concat (nreverse snippets)))))
  16615. (defun org-copy-special ()
  16616. "Copy region in table or copy current subtree.
  16617. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  16618. See the individual commands for more information."
  16619. (interactive)
  16620. (call-interactively
  16621. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  16622. (defun org-cut-special ()
  16623. "Cut region in table or cut current subtree.
  16624. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  16625. See the individual commands for more information."
  16626. (interactive)
  16627. (call-interactively
  16628. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  16629. (defun org-paste-special (arg)
  16630. "Paste rectangular region into table, or past subtree relative to level.
  16631. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  16632. See the individual commands for more information."
  16633. (interactive "P")
  16634. (if (org-at-table-p)
  16635. (org-table-paste-rectangle)
  16636. (org-paste-subtree arg)))
  16637. (defun org-edit-special (&optional arg)
  16638. "Call a special editor for the stuff at point.
  16639. When at a table, call the formula editor with `org-table-edit-formulas'.
  16640. When at the first line of an src example, call `org-edit-src-code'.
  16641. When in an #+include line, visit the include file. Otherwise call
  16642. `ffap' to visit the file at point."
  16643. (interactive)
  16644. ;; possibly prep session before editing source
  16645. (when arg
  16646. (let* ((info (org-babel-get-src-block-info))
  16647. (lang (nth 0 info))
  16648. (params (nth 2 info))
  16649. (session (cdr (assoc :session params))))
  16650. (when (and info session) ;; we are in a source-code block with a session
  16651. (funcall
  16652. (intern (concat "org-babel-prep-session:" lang)) session params))))
  16653. (cond ;; proceed with `org-edit-special'
  16654. ((save-excursion
  16655. (beginning-of-line 1)
  16656. (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
  16657. (find-file (org-trim (match-string 1))))
  16658. ((org-edit-src-code))
  16659. ((org-edit-fixed-width-region))
  16660. ((org-at-table.el-p)
  16661. (org-edit-src-code))
  16662. ((or (org-at-table-p)
  16663. (save-excursion
  16664. (beginning-of-line 1)
  16665. (looking-at "[ \t]*#\\+TBLFM:")))
  16666. (call-interactively 'org-table-edit-formulas))
  16667. (t (call-interactively 'ffap))))
  16668. (defvar org-table-coordinate-overlays) ; defined in org-table.el
  16669. (defun org-ctrl-c-ctrl-c (&optional arg)
  16670. "Set tags in headline, or update according to changed information at point.
  16671. This command does many different things, depending on context:
  16672. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  16673. this is what we do.
  16674. - If the cursor is on a statistics cookie, update it.
  16675. - If the cursor is in a headline, prompt for tags and insert them
  16676. into the current line, aligned to `org-tags-column'. When called
  16677. with prefix arg, realign all tags in the current buffer.
  16678. - If the cursor is in one of the special #+KEYWORD lines, this
  16679. triggers scanning the buffer for these lines and updating the
  16680. information.
  16681. - If the cursor is inside a table, realign the table. This command
  16682. works even if the automatic table editor has been turned off.
  16683. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  16684. the entire table.
  16685. - If the cursor is at a footnote reference or definition, jump to
  16686. the corresponding definition or references, respectively.
  16687. - If the cursor is a the beginning of a dynamic block, update it.
  16688. - If the current buffer is a capture buffer, close note and file it.
  16689. - If the cursor is on a <<<target>>>, update radio targets and
  16690. corresponding links in this buffer.
  16691. - If the cursor is on a numbered item in a plain list, renumber the
  16692. ordered list.
  16693. - If the cursor is on a checkbox, toggle it.
  16694. - If the cursor is on a code block, evaluate it. The variable
  16695. `org-confirm-babel-evaluate' can be used to control prompting
  16696. before code block evaluation, by default every code block
  16697. evaluation requires confirmation. Code block evaluation can be
  16698. inhibited by setting `org-babel-no-eval-on-ctrl-c-ctrl-c'."
  16699. (interactive "P")
  16700. (let ((org-enable-table-editor t))
  16701. (cond
  16702. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  16703. org-occur-highlights
  16704. org-latex-fragment-image-overlays)
  16705. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  16706. (org-remove-occur-highlights)
  16707. (org-remove-latex-fragment-image-overlays)
  16708. (message "Temporary highlights/overlays removed from current buffer"))
  16709. ((and (local-variable-p 'org-finish-function (current-buffer))
  16710. (fboundp org-finish-function))
  16711. (funcall org-finish-function))
  16712. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  16713. ((org-in-regexp org-ts-regexp-both)
  16714. (org-timestamp-change 0 'day))
  16715. ((or (looking-at org-property-start-re)
  16716. (org-at-property-p))
  16717. (call-interactively 'org-property-action))
  16718. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  16719. ((and (org-in-regexp "\\[\\([0-9]*%\\|[0-9]*/[0-9]*\\)\\]")
  16720. (or (org-on-heading-p) (org-at-item-p)))
  16721. (call-interactively 'org-update-statistics-cookies))
  16722. ((org-on-heading-p) (call-interactively 'org-set-tags))
  16723. ((org-at-table.el-p)
  16724. (message "Use C-c ' to edit table.el tables"))
  16725. ((org-at-table-p)
  16726. (org-table-maybe-eval-formula)
  16727. (if arg
  16728. (call-interactively 'org-table-recalculate)
  16729. (org-table-maybe-recalculate-line))
  16730. (call-interactively 'org-table-align)
  16731. (orgtbl-send-table 'maybe))
  16732. ((or (org-footnote-at-reference-p)
  16733. (org-footnote-at-definition-p))
  16734. (call-interactively 'org-footnote-action))
  16735. ((org-at-item-checkbox-p)
  16736. ;; Cursor at a checkbox: repair list and update checkboxes. Send
  16737. ;; list only if at top item.
  16738. (let* ((cbox (match-string 1))
  16739. (struct (org-list-struct))
  16740. (old-struct (copy-tree struct))
  16741. (parents (org-list-parents-alist struct))
  16742. (orderedp (org-entry-get nil "ORDERED"))
  16743. (firstp (= (org-list-get-top-point struct) (point-at-bol)))
  16744. block-item)
  16745. ;; Use a light version of `org-toggle-checkbox' to avoid
  16746. ;; computing list structure twice.
  16747. (org-list-set-checkbox (point-at-bol) struct
  16748. (cond
  16749. ((equal arg '(16)) "[-]")
  16750. ((equal arg '(4)) nil)
  16751. ((equal "[X]" cbox) "[ ]")
  16752. (t "[X]")))
  16753. ;; Replicate `org-list-write-struct', while grabbing a return
  16754. ;; value from `org-list-struct-fix-box'.
  16755. (org-list-struct-fix-ind struct parents 2)
  16756. (org-list-struct-fix-item-end struct)
  16757. (let ((prevs (org-list-prevs-alist struct)))
  16758. (org-list-struct-fix-bul struct prevs)
  16759. (org-list-struct-fix-ind struct parents)
  16760. (setq block-item
  16761. (org-list-struct-fix-box struct parents prevs orderedp)))
  16762. (org-list-struct-apply-struct struct old-struct)
  16763. (org-update-checkbox-count-maybe)
  16764. (when block-item
  16765. (message
  16766. "Checkboxes were removed due to unchecked box at line %d"
  16767. (org-current-line block-item)))
  16768. (when firstp (org-list-send-list 'maybe))))
  16769. ((org-at-item-p)
  16770. ;; Cursor at an item: repair list. Do checkbox related actions
  16771. ;; only if function was called with an argument. Send list only
  16772. ;; if at top item.
  16773. (let* ((struct (org-list-struct))
  16774. (firstp (= (org-list-get-top-point struct) (point-at-bol))))
  16775. (when arg (org-list-set-checkbox (point-at-bol) struct "[ ]"))
  16776. (org-list-write-struct struct (org-list-parents-alist struct))
  16777. (when arg (org-update-checkbox-count-maybe))
  16778. (when firstp (org-list-send-list 'maybe))))
  16779. ((save-excursion (beginning-of-line 1) (looking-at org-dblock-start-re))
  16780. ;; Dynamic block
  16781. (beginning-of-line 1)
  16782. (save-excursion (org-update-dblock)))
  16783. ((save-excursion
  16784. (beginning-of-line 1)
  16785. (looking-at "[ \t]*#\\+\\([A-Z]+\\)"))
  16786. (cond
  16787. ((equal (match-string 1) "TBLFM")
  16788. ;; Recalculate the table before this line
  16789. (save-excursion
  16790. (beginning-of-line 1)
  16791. (skip-chars-backward " \r\n\t")
  16792. (if (org-at-table-p)
  16793. (org-call-with-arg 'org-table-recalculate (or arg t)))))
  16794. (t
  16795. (let ((org-inhibit-startup-visibility-stuff t)
  16796. (org-startup-align-all-tables nil))
  16797. (when (boundp 'org-table-coordinate-overlays)
  16798. (mapc 'delete-overlay org-table-coordinate-overlays)
  16799. (setq org-table-coordinate-overlays nil))
  16800. (org-save-outline-visibility 'use-markers (org-mode-restart)))
  16801. (message "Local setup has been refreshed"))))
  16802. ((org-clock-update-time-maybe))
  16803. (t
  16804. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  16805. (error "C-c C-c can do nothing useful at this location"))))))
  16806. (defun org-mode-restart ()
  16807. "Restart Org-mode, to scan again for special lines.
  16808. Also updates the keyword regular expressions."
  16809. (interactive)
  16810. (org-mode)
  16811. (message "Org-mode restarted"))
  16812. (defun org-kill-note-or-show-branches ()
  16813. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  16814. (interactive)
  16815. (if (not org-finish-function)
  16816. (progn
  16817. (hide-subtree)
  16818. (call-interactively 'show-branches))
  16819. (let ((org-note-abort t))
  16820. (funcall org-finish-function))))
  16821. (defun org-return (&optional indent)
  16822. "Goto next table row or insert a newline.
  16823. Calls `org-table-next-row' or `newline', depending on context.
  16824. See the individual commands for more information."
  16825. (interactive)
  16826. (cond
  16827. ((bobp) (if indent (newline-and-indent) (newline)))
  16828. ((org-at-table-p)
  16829. (org-table-justify-field-maybe)
  16830. (call-interactively 'org-table-next-row))
  16831. ;; when `newline-and-indent' is called within a list, make sure
  16832. ;; text moved stays inside the item.
  16833. ((and (org-in-item-p) indent)
  16834. (if (and (org-at-item-p) (>= (point) (match-end 0)))
  16835. (progn
  16836. (save-match-data (newline))
  16837. (org-indent-line-to (length (match-string 0))))
  16838. (let ((ind (org-get-indentation)))
  16839. (newline)
  16840. (if (org-looking-back org-list-end-re)
  16841. (org-indent-line-function)
  16842. (org-indent-line-to ind)))))
  16843. ((and org-return-follows-link
  16844. (let ((tprop (get-text-property (point) 'face)))
  16845. (or (eq tprop 'org-link)
  16846. (and (listp tprop) (memq 'org-link tprop)))))
  16847. (call-interactively 'org-open-at-point))
  16848. ((and (org-at-heading-p)
  16849. (looking-at
  16850. (org-re "\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$")))
  16851. (org-show-entry)
  16852. (end-of-line 1)
  16853. (newline))
  16854. (t (if indent (newline-and-indent) (newline)))))
  16855. (defun org-return-indent ()
  16856. "Goto next table row or insert a newline and indent.
  16857. Calls `org-table-next-row' or `newline-and-indent', depending on
  16858. context. See the individual commands for more information."
  16859. (interactive)
  16860. (org-return t))
  16861. (defun org-ctrl-c-star ()
  16862. "Compute table, or change heading status of lines.
  16863. Calls `org-table-recalculate' or `org-toggle-heading',
  16864. depending on context."
  16865. (interactive)
  16866. (cond
  16867. ((org-at-table-p)
  16868. (call-interactively 'org-table-recalculate))
  16869. (t
  16870. ;; Convert all lines in region to list items
  16871. (call-interactively 'org-toggle-heading))))
  16872. (defun org-ctrl-c-minus ()
  16873. "Insert separator line in table or modify bullet status of line.
  16874. Also turns a plain line or a region of lines into list items.
  16875. Calls `org-table-insert-hline', `org-toggle-item', or
  16876. `org-cycle-list-bullet', depending on context."
  16877. (interactive)
  16878. (cond
  16879. ((org-at-table-p)
  16880. (call-interactively 'org-table-insert-hline))
  16881. ((org-region-active-p)
  16882. (call-interactively 'org-toggle-item))
  16883. ((org-in-item-p)
  16884. (call-interactively 'org-cycle-list-bullet))
  16885. (t
  16886. (call-interactively 'org-toggle-item))))
  16887. (defun org-toggle-item (arg)
  16888. "Convert headings or normal lines to items, items to normal lines.
  16889. If there is no active region, only the current line is considered.
  16890. If the first non blank line in the region is an headline, convert
  16891. all headlines to items, shifting text accordingly.
  16892. If it is an item, convert all items to normal lines.
  16893. If it is normal text, change region into an item. With a prefix
  16894. argument ARG, change each line in region into an item."
  16895. (interactive "P")
  16896. (let ((shift-text
  16897. (function
  16898. ;; Shift text in current section to IND, from point to END.
  16899. ;; The function leaves point to END line.
  16900. (lambda (ind end)
  16901. (let ((min-i 1000) (end (copy-marker end)))
  16902. ;; First determine the minimum indentation (MIN-I) of
  16903. ;; the text.
  16904. (save-excursion
  16905. (catch 'exit
  16906. (while (< (point) end)
  16907. (let ((i (org-get-indentation)))
  16908. (cond
  16909. ;; Skip blank lines and inline tasks.
  16910. ((looking-at "^[ \t]*$"))
  16911. ((looking-at org-outline-regexp-bol))
  16912. ;; We can't find less than 0 indentation.
  16913. ((zerop i) (throw 'exit (setq min-i 0)))
  16914. ((< i min-i) (setq min-i i))))
  16915. (forward-line))))
  16916. ;; Then indent each line so that a line indented to
  16917. ;; MIN-I becomes indented to IND. Ignore blank lines
  16918. ;; and inline tasks in the process.
  16919. (let ((delta (- ind min-i)))
  16920. (while (< (point) end)
  16921. (unless (or (looking-at "^[ \t]*$")
  16922. (looking-at org-outline-regexp-bol))
  16923. (org-indent-line-to (+ (org-get-indentation) delta)))
  16924. (forward-line)))))))
  16925. (skip-blanks
  16926. (function
  16927. ;; Return beginning of first non-blank line, starting from
  16928. ;; line at POS.
  16929. (lambda (pos)
  16930. (save-excursion
  16931. (goto-char pos)
  16932. (skip-chars-forward " \r\t\n")
  16933. (point-at-bol)))))
  16934. beg end)
  16935. ;; Determine boundaries of changes.
  16936. (if (org-region-active-p)
  16937. (setq beg (funcall skip-blanks (region-beginning))
  16938. end (copy-marker (region-end)))
  16939. (setq beg (funcall skip-blanks (point-at-bol))
  16940. end (copy-marker (point-at-eol))))
  16941. ;; Depending on the starting line, choose an action on the text
  16942. ;; between BEG and END.
  16943. (org-with-limited-levels
  16944. (save-excursion
  16945. (goto-char beg)
  16946. (cond
  16947. ;; Case 1. Start at an item: de-itemize. Note that it only
  16948. ;; happens when a region is active: `org-ctrl-c-minus'
  16949. ;; would call `org-cycle-list-bullet' otherwise.
  16950. ((org-at-item-p)
  16951. (while (< (point) end)
  16952. (when (org-at-item-p)
  16953. (skip-chars-forward " \t")
  16954. (delete-region (point) (match-end 0)))
  16955. (forward-line)))
  16956. ;; Case 2. Start at an heading: convert to items.
  16957. ((org-on-heading-p)
  16958. (let* ((bul (org-list-bullet-string "-"))
  16959. (bul-len (length bul))
  16960. ;; Indentation of the first heading. It should be
  16961. ;; relative to the indentation of its parent, if any.
  16962. (start-ind (save-excursion
  16963. (cond
  16964. ((not org-adapt-indentation) 0)
  16965. ((not (outline-previous-heading)) 0)
  16966. (t (length (match-string 0))))))
  16967. ;; Level of first heading. Further headings will be
  16968. ;; compared to it to determine hierarchy in the list.
  16969. (ref-level (org-reduced-level (org-outline-level))))
  16970. (while (< (point) end)
  16971. (let* ((level (org-reduced-level (org-outline-level)))
  16972. (delta (max 0 (- level ref-level))))
  16973. ;; If current headline is less indented than the first
  16974. ;; one, set it as reference, in order to preserve
  16975. ;; subtrees.
  16976. (when (< level ref-level) (setq ref-level level))
  16977. (replace-match bul t t)
  16978. (org-indent-line-to (+ start-ind (* delta bul-len)))
  16979. ;; Ensure all text down to END (or SECTION-END) belongs
  16980. ;; to the newly created item.
  16981. (let ((section-end (save-excursion
  16982. (or (outline-next-heading) (point)))))
  16983. (forward-line)
  16984. (funcall shift-text
  16985. (+ start-ind (* (1+ delta) bul-len))
  16986. (min end section-end)))))))
  16987. ;; Case 3. Normal line with ARG: turn each non-item line into
  16988. ;; an item.
  16989. (arg
  16990. (while (< (point) end)
  16991. (unless (or (org-on-heading-p) (org-at-item-p))
  16992. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  16993. (replace-match
  16994. (concat "\\1" (org-list-bullet-string "-") "\\2"))))
  16995. (forward-line)))
  16996. ;; Case 4. Normal line without ARG: make the first line of
  16997. ;; region an item, and shift indentation of others
  16998. ;; lines to set them as item's body.
  16999. (t (let* ((bul (org-list-bullet-string "-"))
  17000. (bul-len (length bul))
  17001. (ref-ind (org-get-indentation)))
  17002. (skip-chars-forward " \t")
  17003. (insert bul)
  17004. (forward-line)
  17005. (while (< (point) end)
  17006. ;; Ensure that lines less indented than first one
  17007. ;; still get included in item body.
  17008. (funcall shift-text
  17009. (+ ref-ind bul-len)
  17010. (min end (save-excursion (or (outline-next-heading)
  17011. (point)))))
  17012. (forward-line)))))))))
  17013. (defun org-toggle-heading (&optional nstars)
  17014. "Convert headings to normal text, or items or text to headings.
  17015. If there is no active region, only the current line is considered.
  17016. If the first non blank line is an headline, remove the stars from
  17017. all headlines in the region.
  17018. If it is a plain list item, turn all plain list items into headings.
  17019. If it is a normal line, turn each and every normal line (i.e. not
  17020. an heading or an item) in the region into a heading.
  17021. When converting a line into a heading, the number of stars is chosen
  17022. such that the lines become children of the current entry. However,
  17023. when a prefix argument is given, its value determines the number of
  17024. stars to add."
  17025. (interactive "P")
  17026. (let ((skip-blanks
  17027. (function
  17028. ;; Return beginning of first non-blank line, starting from
  17029. ;; line at POS.
  17030. (lambda (pos)
  17031. (save-excursion
  17032. (goto-char pos)
  17033. (skip-chars-forward " \r\t\n")
  17034. (point-at-bol)))))
  17035. beg end)
  17036. ;; Determine boundaries of changes. If region ends at a bol, do
  17037. ;; not consider the last line to be in the region.
  17038. (if (org-region-active-p)
  17039. (setq beg (funcall skip-blanks (region-beginning))
  17040. end (copy-marker (save-excursion
  17041. (goto-char (region-end))
  17042. (if (bolp) (point) (point-at-eol)))))
  17043. (setq beg (funcall skip-blanks (point-at-bol))
  17044. end (copy-marker (point-at-eol))))
  17045. ;; Ensure inline tasks don't count as headings.
  17046. (org-with-limited-levels
  17047. (save-excursion
  17048. (goto-char beg)
  17049. (cond
  17050. ;; Case 1. Started at an heading: de-star headings.
  17051. ((org-on-heading-p)
  17052. (while (< (point) end)
  17053. (when (org-on-heading-p t)
  17054. (looking-at org-outline-regexp) (replace-match ""))
  17055. (forward-line)))
  17056. ;; Case 2. Started at an item: change items into headlines.
  17057. ;; One star will be added by `org-list-to-subtree'.
  17058. ((org-at-item-p)
  17059. (let* ((stars (make-string
  17060. (if nstars
  17061. ;; subtract the star that will be added again by
  17062. ;; `org-list-to-subtree'
  17063. (1- (prefix-numeric-value current-prefix-arg))
  17064. (or (org-current-level) 0))
  17065. ?*))
  17066. (add-stars
  17067. (cond (nstars "") ; stars from prefix only
  17068. ((equal stars "") "") ; before first heading
  17069. (org-odd-levels-only "*") ; inside heading, odd
  17070. (t "")))) ; inside heading, oddeven
  17071. (while (< (point) end)
  17072. (when (org-at-item-p)
  17073. ;; Pay attention to cases when region ends before list.
  17074. (let* ((struct (org-list-struct))
  17075. (list-end (min (org-list-get-bottom-point struct) (1+ end))))
  17076. (save-restriction
  17077. (narrow-to-region (point) list-end)
  17078. (insert
  17079. (org-list-to-subtree
  17080. (org-list-parse-list t)
  17081. '(:istart (concat stars add-stars (funcall get-stars depth))
  17082. :icount (concat stars add-stars (funcall get-stars depth))))))))
  17083. (forward-line))))
  17084. ;; Case 3. Started at normal text: make every line an heading,
  17085. ;; skipping headlines and items.
  17086. (t (let* ((stars (make-string
  17087. (if nstars
  17088. (prefix-numeric-value current-prefix-arg)
  17089. (or (org-current-level) 0))
  17090. ?*))
  17091. (add-stars
  17092. (cond (nstars "") ; stars from prefix only
  17093. ((equal stars "") "*") ; before first heading
  17094. (org-odd-levels-only "**") ; inside heading, odd
  17095. (t "*"))) ; inside heading, oddeven
  17096. (rpl (concat stars add-stars " ")))
  17097. (while (< (point) end)
  17098. (when (and (not (org-on-heading-p)) (not (org-at-item-p))
  17099. (looking-at "\\([ \t]*\\)\\(\\S-\\)"))
  17100. (replace-match (concat rpl (match-string 2))))
  17101. (forward-line)))))))))
  17102. (defun org-meta-return (&optional arg)
  17103. "Insert a new heading or wrap a region in a table.
  17104. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  17105. See the individual commands for more information."
  17106. (interactive "P")
  17107. (cond
  17108. ((run-hook-with-args-until-success 'org-metareturn-hook))
  17109. ((org-at-table-p)
  17110. (call-interactively 'org-table-wrap-region))
  17111. (t (call-interactively 'org-insert-heading))))
  17112. ;;; Menu entries
  17113. ;; Define the Org-mode menus
  17114. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  17115. '("Tbl"
  17116. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  17117. ["Next Field" org-cycle (org-at-table-p)]
  17118. ["Previous Field" org-shifttab (org-at-table-p)]
  17119. ["Next Row" org-return (org-at-table-p)]
  17120. "--"
  17121. ["Blank Field" org-table-blank-field (org-at-table-p)]
  17122. ["Edit Field" org-table-edit-field (org-at-table-p)]
  17123. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  17124. "--"
  17125. ("Column"
  17126. ["Move Column Left" org-metaleft (org-at-table-p)]
  17127. ["Move Column Right" org-metaright (org-at-table-p)]
  17128. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  17129. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  17130. ("Row"
  17131. ["Move Row Up" org-metaup (org-at-table-p)]
  17132. ["Move Row Down" org-metadown (org-at-table-p)]
  17133. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  17134. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  17135. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  17136. "--"
  17137. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  17138. ("Rectangle"
  17139. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  17140. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  17141. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  17142. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  17143. "--"
  17144. ("Calculate"
  17145. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  17146. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  17147. ["Edit Formulas" org-edit-special (org-at-table-p)]
  17148. "--"
  17149. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  17150. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  17151. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  17152. "--"
  17153. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  17154. "--"
  17155. ["Sum Column/Rectangle" org-table-sum
  17156. (or (org-at-table-p) (org-region-active-p))]
  17157. ["Which Column?" org-table-current-column (org-at-table-p)])
  17158. ["Debug Formulas"
  17159. org-table-toggle-formula-debugger
  17160. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  17161. ["Show Col/Row Numbers"
  17162. org-table-toggle-coordinate-overlays
  17163. :style toggle
  17164. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  17165. "--"
  17166. ["Create" org-table-create (and (not (org-at-table-p))
  17167. org-enable-table-editor)]
  17168. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  17169. ["Import from File" org-table-import (not (org-at-table-p))]
  17170. ["Export to File" org-table-export (org-at-table-p)]
  17171. "--"
  17172. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  17173. (easy-menu-define org-org-menu org-mode-map "Org menu"
  17174. '("Org"
  17175. ("Show/Hide"
  17176. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  17177. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  17178. ["Sparse Tree..." org-sparse-tree t]
  17179. ["Reveal Context" org-reveal t]
  17180. ["Show All" show-all t]
  17181. "--"
  17182. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  17183. "--"
  17184. ["New Heading" org-insert-heading t]
  17185. ("Navigate Headings"
  17186. ["Up" outline-up-heading t]
  17187. ["Next" outline-next-visible-heading t]
  17188. ["Previous" outline-previous-visible-heading t]
  17189. ["Next Same Level" outline-forward-same-level t]
  17190. ["Previous Same Level" outline-backward-same-level t]
  17191. "--"
  17192. ["Jump" org-goto t])
  17193. ("Edit Structure"
  17194. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  17195. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  17196. "--"
  17197. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  17198. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  17199. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  17200. "--"
  17201. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  17202. "--"
  17203. ["Copy visible text" org-copy-visible t]
  17204. "--"
  17205. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  17206. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  17207. ["Demote Heading" org-metaright (not (org-at-table-p))]
  17208. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  17209. "--"
  17210. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  17211. "--"
  17212. ["Convert to odd levels" org-convert-to-odd-levels t]
  17213. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  17214. ("Editing"
  17215. ["Emphasis..." org-emphasize t]
  17216. ["Edit Source Example" org-edit-special t]
  17217. "--"
  17218. ["Footnote new/jump" org-footnote-action t]
  17219. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  17220. ("Archive"
  17221. ["Archive (default method)" org-archive-subtree-default t]
  17222. "--"
  17223. ["Move Subtree to Archive file" org-advertized-archive-subtree t]
  17224. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  17225. ["Move subtree to Archive sibling" org-archive-to-archive-sibling t]
  17226. )
  17227. "--"
  17228. ("Hyperlinks"
  17229. ["Store Link (Global)" org-store-link t]
  17230. ["Find existing link to here" org-occur-link-in-agenda-files t]
  17231. ["Insert Link" org-insert-link t]
  17232. ["Follow Link" org-open-at-point t]
  17233. "--"
  17234. ["Next link" org-next-link t]
  17235. ["Previous link" org-previous-link t]
  17236. "--"
  17237. ["Descriptive Links"
  17238. org-toggle-link-display
  17239. :style radio
  17240. :selected org-descriptive-links
  17241. ]
  17242. ["Literal Links"
  17243. org-toggle-link-display
  17244. :style radio
  17245. :selected (not org-descriptive-links)])
  17246. "--"
  17247. ("TODO Lists"
  17248. ["TODO/DONE/-" org-todo t]
  17249. ("Select keyword"
  17250. ["Next keyword" org-shiftright (org-on-heading-p)]
  17251. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  17252. ["Complete Keyword" pcomplete (assq :todo-keyword (org-context))]
  17253. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  17254. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  17255. ["Show TODO Tree" org-show-todo-tree :active t :keys "C-c / t"]
  17256. ["Global TODO list" org-todo-list :active t :keys "C-c a t"]
  17257. "--"
  17258. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  17259. :selected org-enforce-todo-dependencies :style toggle :active t]
  17260. "Settings for tree at point"
  17261. ["Do Children sequentially" org-toggle-ordered-property :style radio
  17262. :selected (org-entry-get nil "ORDERED")
  17263. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  17264. ["Do Children parallel" org-toggle-ordered-property :style radio
  17265. :selected (not (org-entry-get nil "ORDERED"))
  17266. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  17267. "--"
  17268. ["Set Priority" org-priority t]
  17269. ["Priority Up" org-shiftup t]
  17270. ["Priority Down" org-shiftdown t]
  17271. "--"
  17272. ["Get news from all feeds" org-feed-update-all t]
  17273. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  17274. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  17275. ("TAGS and Properties"
  17276. ["Set Tags" org-set-tags-command t]
  17277. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  17278. "--"
  17279. ["Set property" org-set-property t]
  17280. ["Column view of properties" org-columns t]
  17281. ["Insert Column View DBlock" org-insert-columns-dblock t])
  17282. ("Dates and Scheduling"
  17283. ["Timestamp" org-time-stamp t]
  17284. ["Timestamp (inactive)" org-time-stamp-inactive t]
  17285. ("Change Date"
  17286. ["1 Day Later" org-shiftright t]
  17287. ["1 Day Earlier" org-shiftleft t]
  17288. ["1 ... Later" org-shiftup t]
  17289. ["1 ... Earlier" org-shiftdown t])
  17290. ["Compute Time Range" org-evaluate-time-range t]
  17291. ["Schedule Item" org-schedule t]
  17292. ["Deadline" org-deadline t]
  17293. "--"
  17294. ["Custom time format" org-toggle-time-stamp-overlays
  17295. :style radio :selected org-display-custom-times]
  17296. "--"
  17297. ["Goto Calendar" org-goto-calendar t]
  17298. ["Date from Calendar" org-date-from-calendar t]
  17299. "--"
  17300. ["Start/Restart Timer" org-timer-start t]
  17301. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  17302. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  17303. ["Insert Timer String" org-timer t]
  17304. ["Insert Timer Item" org-timer-item t])
  17305. ("Logging work"
  17306. ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
  17307. ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
  17308. ["Clock out" org-clock-out t]
  17309. ["Clock cancel" org-clock-cancel t]
  17310. "--"
  17311. ["Mark as default task" org-clock-mark-default-task t]
  17312. ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
  17313. ["Goto running clock" org-clock-goto t]
  17314. "--"
  17315. ["Display times" org-clock-display t]
  17316. ["Create clock table" org-clock-report t]
  17317. "--"
  17318. ["Record DONE time"
  17319. (progn (setq org-log-done (not org-log-done))
  17320. (message "Switching to %s will %s record a timestamp"
  17321. (car org-done-keywords)
  17322. (if org-log-done "automatically" "not")))
  17323. :style toggle :selected org-log-done])
  17324. "--"
  17325. ["Agenda Command..." org-agenda t]
  17326. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  17327. ("File List for Agenda")
  17328. ("Special views current file"
  17329. ["TODO Tree" org-show-todo-tree t]
  17330. ["Check Deadlines" org-check-deadlines t]
  17331. ["Timeline" org-timeline t]
  17332. ["Tags/Property tree" org-match-sparse-tree t])
  17333. "--"
  17334. ["Export/Publish..." org-export t]
  17335. ("LaTeX"
  17336. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  17337. :selected org-cdlatex-mode]
  17338. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  17339. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  17340. ["Modify math symbol" org-cdlatex-math-modify
  17341. (org-inside-LaTeX-fragment-p)]
  17342. ["Insert citation" org-reftex-citation t]
  17343. "--"
  17344. ["Template for BEAMER" org-insert-beamer-options-template t])
  17345. "--"
  17346. ("MobileOrg"
  17347. ["Push Files and Views" org-mobile-push t]
  17348. ["Get Captured and Flagged" org-mobile-pull t]
  17349. ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
  17350. "--"
  17351. ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
  17352. "--"
  17353. ("Documentation"
  17354. ["Show Version" org-version t]
  17355. ["Info Documentation" org-info t])
  17356. ("Customize"
  17357. ["Browse Org Group" org-customize t]
  17358. "--"
  17359. ["Expand This Menu" org-create-customize-menu
  17360. (fboundp 'customize-menu-create)])
  17361. ["Send bug report" org-submit-bug-report t]
  17362. "--"
  17363. ("Refresh/Reload"
  17364. ["Refresh setup current buffer" org-mode-restart t]
  17365. ["Reload Org (after update)" org-reload t]
  17366. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x r"])
  17367. ))
  17368. (defun org-info (&optional node)
  17369. "Read documentation for Org-mode in the info system.
  17370. With optional NODE, go directly to that node."
  17371. (interactive)
  17372. (info (format "(org)%s" (or node ""))))
  17373. ;;;###autoload
  17374. (defun org-submit-bug-report ()
  17375. "Submit a bug report on Org-mode via mail.
  17376. Don't hesitate to report any problems or inaccurate documentation.
  17377. If you don't have setup sending mail from (X)Emacs, please copy the
  17378. output buffer into your mail program, as it gives us important
  17379. information about your Org-mode version and configuration."
  17380. (interactive)
  17381. (require 'reporter)
  17382. (org-load-modules-maybe)
  17383. (org-require-autoloaded-modules)
  17384. (let ((reporter-prompt-for-summary-p "Bug report subject: "))
  17385. (reporter-submit-bug-report
  17386. "emacs-orgmode@gnu.org"
  17387. (org-version)
  17388. (let (list)
  17389. (save-window-excursion
  17390. (org-pop-to-buffer-same-window (get-buffer-create "*Warn about privacy*"))
  17391. (delete-other-windows)
  17392. (erase-buffer)
  17393. (insert "You are about to submit a bug report to the Org-mode mailing list.
  17394. We would like to add your full Org-mode and Outline configuration to the
  17395. bug report. This greatly simplifies the work of the maintainer and
  17396. other experts on the mailing list.
  17397. HOWEVER, some variables you have customized may contain private
  17398. information. The names of customers, colleagues, or friends, might
  17399. appear in the form of file names, tags, todo states, or search strings.
  17400. If you answer yes to the prompt, you might want to check and remove
  17401. such private information before sending the email.")
  17402. (add-text-properties (point-min) (point-max) '(face org-warning))
  17403. (when (yes-or-no-p "Include your Org-mode configuration ")
  17404. (mapatoms
  17405. (lambda (v)
  17406. (and (boundp v)
  17407. (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
  17408. (or (and (symbol-value v)
  17409. (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
  17410. (and
  17411. (get v 'custom-type) (get v 'standard-value)
  17412. (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
  17413. (push v list)))))
  17414. (kill-buffer (get-buffer "*Warn about privacy*"))
  17415. list))
  17416. nil nil
  17417. "Remember to cover the basics, that is, what you expected to happen and
  17418. what in fact did happen. You don't know how to make a good report? See
  17419. http://orgmode.org/manual/Feedback.html#Feedback
  17420. Your bug report will be posted to the Org-mode mailing list.
  17421. ------------------------------------------------------------------------")
  17422. (save-excursion
  17423. (if (re-search-backward "^\\(Subject: \\)Org-mode version \\(.*?\\);[ \t]*\\(.*\\)" nil t)
  17424. (replace-match "\\1Bug: \\3 [\\2]")))))
  17425. (defun org-install-agenda-files-menu ()
  17426. (let ((bl (buffer-list)))
  17427. (save-excursion
  17428. (while bl
  17429. (set-buffer (pop bl))
  17430. (if (eq major-mode 'org-mode) (setq bl nil)))
  17431. (when (eq major-mode 'org-mode)
  17432. (easy-menu-change
  17433. '("Org") "File List for Agenda"
  17434. (append
  17435. (list
  17436. ["Edit File List" (org-edit-agenda-file-list) t]
  17437. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  17438. ["Remove Current File from List" org-remove-file t]
  17439. ["Cycle through agenda files" org-cycle-agenda-files t]
  17440. ["Occur in all agenda files" org-occur-in-agenda-files t]
  17441. "--")
  17442. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  17443. ;;;; Documentation
  17444. ;;;###autoload
  17445. (defun org-require-autoloaded-modules ()
  17446. (interactive)
  17447. (mapc 'require
  17448. '(org-agenda org-archive org-ascii org-attach org-clock org-colview
  17449. org-docbook org-exp org-html org-icalendar
  17450. org-id org-latex
  17451. org-publish org-remember org-table
  17452. org-timer org-xoxo)))
  17453. ;;;###autoload
  17454. (defun org-reload (&optional uncompiled)
  17455. "Reload all org lisp files.
  17456. With prefix arg UNCOMPILED, load the uncompiled versions."
  17457. (interactive "P")
  17458. (require 'find-func)
  17459. (let* ((file-re "^\\(org\\|orgtbl\\)\\(\\.el\\|-.*\\.el\\)")
  17460. (dir-org (file-name-directory (org-find-library-name "org")))
  17461. (dir-org-contrib (ignore-errors
  17462. (file-name-directory
  17463. (org-find-library-name "org-contribdir"))))
  17464. (babel-files
  17465. (mapcar (lambda (el) (concat "ob" (when el (format "-%s" el)) ".el"))
  17466. (append (list nil "comint" "eval" "exp" "keys"
  17467. "lob" "ref" "table" "tangle")
  17468. (delq nil
  17469. (mapcar
  17470. (lambda (lang)
  17471. (when (cdr lang) (symbol-name (car lang))))
  17472. org-babel-load-languages)))))
  17473. (files
  17474. (append (directory-files dir-org t file-re)
  17475. babel-files
  17476. (and dir-org-contrib
  17477. (directory-files dir-org-contrib t file-re))))
  17478. (remove-re (concat (if (featurep 'xemacs)
  17479. "org-colview" "org-colview-xemacs")
  17480. "\\'")))
  17481. (setq files (mapcar 'file-name-sans-extension files))
  17482. (setq files (mapcar
  17483. (lambda (x) (if (string-match remove-re x) nil x))
  17484. files))
  17485. (setq files (delq nil files))
  17486. (mapc
  17487. (lambda (f)
  17488. (when (featurep (intern (file-name-nondirectory f)))
  17489. (if (and (not uncompiled)
  17490. (file-exists-p (concat f ".elc")))
  17491. (load (concat f ".elc") nil nil t)
  17492. (load (concat f ".el") nil nil t))))
  17493. files))
  17494. (org-version))
  17495. ;;;###autoload
  17496. (defun org-customize ()
  17497. "Call the customize function with org as argument."
  17498. (interactive)
  17499. (org-load-modules-maybe)
  17500. (org-require-autoloaded-modules)
  17501. (customize-browse 'org))
  17502. (defun org-create-customize-menu ()
  17503. "Create a full customization menu for Org-mode, insert it into the menu."
  17504. (interactive)
  17505. (org-load-modules-maybe)
  17506. (org-require-autoloaded-modules)
  17507. (if (fboundp 'customize-menu-create)
  17508. (progn
  17509. (easy-menu-change
  17510. '("Org") "Customize"
  17511. `(["Browse Org group" org-customize t]
  17512. "--"
  17513. ,(customize-menu-create 'org)
  17514. ["Set" Custom-set t]
  17515. ["Save" Custom-save t]
  17516. ["Reset to Current" Custom-reset-current t]
  17517. ["Reset to Saved" Custom-reset-saved t]
  17518. ["Reset to Standard Settings" Custom-reset-standard t]))
  17519. (message "\"Org\"-menu now contains full customization menu"))
  17520. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  17521. ;;;; Miscellaneous stuff
  17522. ;;; Generally useful functions
  17523. (defun org-get-at-bol (property)
  17524. "Get text property PROPERTY at beginning of line."
  17525. (get-text-property (point-at-bol) property))
  17526. (defun org-find-text-property-in-string (prop s)
  17527. "Return the first non-nil value of property PROP in string S."
  17528. (or (get-text-property 0 prop s)
  17529. (get-text-property (or (next-single-property-change 0 prop s) 0)
  17530. prop s)))
  17531. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  17532. "Display the given MESSAGE as a warning."
  17533. (if (fboundp 'display-warning)
  17534. (display-warning 'org message
  17535. (if (featurep 'xemacs) 'warning :warning))
  17536. (let ((buf (get-buffer-create "*Org warnings*")))
  17537. (with-current-buffer buf
  17538. (goto-char (point-max))
  17539. (insert "Warning (Org): " message)
  17540. (unless (bolp)
  17541. (newline)))
  17542. (display-buffer buf)
  17543. (sit-for 0))))
  17544. (defun org-eval (form)
  17545. "Eval FORM and return result."
  17546. (condition-case error
  17547. (eval form)
  17548. (error (format "%%![Error: %s]" error))))
  17549. (defun org-in-commented-line ()
  17550. "Is point in a line starting with `#'?"
  17551. (equal (char-after (point-at-bol)) ?#))
  17552. (defun org-in-indented-comment-line ()
  17553. "Is point in a line starting with `#' after some white space?"
  17554. (save-excursion
  17555. (save-match-data
  17556. (goto-char (point-at-bol))
  17557. (looking-at "[ \t]*#"))))
  17558. (defun org-in-verbatim-emphasis ()
  17559. (save-match-data
  17560. (and (org-in-regexp org-emph-re 2) (member (match-string 3) '("=" "~")))))
  17561. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  17562. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  17563. (if (and marker (marker-buffer marker)
  17564. (buffer-live-p (marker-buffer marker)))
  17565. (progn
  17566. (org-pop-to-buffer-same-window (marker-buffer marker))
  17567. (if (or (> marker (point-max)) (< marker (point-min)))
  17568. (widen))
  17569. (goto-char marker)
  17570. (org-show-context 'org-goto))
  17571. (if bookmark
  17572. (bookmark-jump bookmark)
  17573. (error "Cannot find location"))))
  17574. (defun org-quote-csv-field (s)
  17575. "Quote field for inclusion in CSV material."
  17576. (if (string-match "[\",]" s)
  17577. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  17578. s))
  17579. (defun org-force-self-insert (N)
  17580. "Needed to enforce self-insert under remapping."
  17581. (interactive "p")
  17582. (self-insert-command N))
  17583. (defun org-string-width (s)
  17584. "Compute width of string, ignoring invisible characters.
  17585. This ignores character with invisibility property `org-link', and also
  17586. characters with property `org-cwidth', because these will become invisible
  17587. upon the next fontification round."
  17588. (let (b l)
  17589. (when (or (eq t buffer-invisibility-spec)
  17590. (assq 'org-link buffer-invisibility-spec))
  17591. (while (setq b (text-property-any 0 (length s)
  17592. 'invisible 'org-link s))
  17593. (setq s (concat (substring s 0 b)
  17594. (substring s (or (next-single-property-change
  17595. b 'invisible s) (length s)))))))
  17596. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  17597. (setq s (concat (substring s 0 b)
  17598. (substring s (or (next-single-property-change
  17599. b 'org-cwidth s) (length s))))))
  17600. (setq l (string-width s) b -1)
  17601. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  17602. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  17603. l))
  17604. (defun org-shorten-string (s maxlength)
  17605. "Shorten string S so tht it is no longer than MAXLENGTH characters.
  17606. If the string is shorter or has length MAXLENGTH, just return the
  17607. original string. If it is longer, the functions finds a space in the
  17608. string, breaks this string off at that locations and adds three dots
  17609. as ellipsis. Including the ellipsis, the string will not be longer
  17610. than MAXLENGTH. If finding a good breaking point in the string does
  17611. not work, the string is just chopped off in the middle of a word
  17612. if necessary."
  17613. (if (<= (length s) maxlength)
  17614. s
  17615. (let* ((n (max (- maxlength 4) 1))
  17616. (re (concat "\\`\\(.\\{1," (int-to-string n) "\\}[^ ]\\)\\([ ]\\|\\'\\)")))
  17617. (if (string-match re s)
  17618. (concat (match-string 1 s) "...")
  17619. (concat (substring s 0 (max (- maxlength 3) 0)) "...")))))
  17620. (defun org-get-indentation (&optional line)
  17621. "Get the indentation of the current line, interpreting tabs.
  17622. When LINE is given, assume it represents a line and compute its indentation."
  17623. (if line
  17624. (if (string-match "^ *" (org-remove-tabs line))
  17625. (match-end 0))
  17626. (save-excursion
  17627. (beginning-of-line 1)
  17628. (skip-chars-forward " \t")
  17629. (current-column))))
  17630. (defun org-get-string-indentation (s)
  17631. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  17632. (let ((n -1) (i 0) (w tab-width) c)
  17633. (catch 'exit
  17634. (while (< (setq n (1+ n)) (length s))
  17635. (setq c (aref s n))
  17636. (cond ((= c ?\ ) (setq i (1+ i)))
  17637. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  17638. (t (throw 'exit t)))))
  17639. i))
  17640. (defun org-remove-tabs (s &optional width)
  17641. "Replace tabulators in S with spaces.
  17642. Assumes that s is a single line, starting in column 0."
  17643. (setq width (or width tab-width))
  17644. (while (string-match "\t" s)
  17645. (setq s (replace-match
  17646. (make-string
  17647. (- (* width (/ (+ (match-beginning 0) width) width))
  17648. (match-beginning 0)) ?\ )
  17649. t t s)))
  17650. s)
  17651. (defun org-fix-indentation (line ind)
  17652. "Fix indentation in LINE.
  17653. IND is a cons cell with target and minimum indentation.
  17654. If the current indentation in LINE is smaller than the minimum,
  17655. leave it alone. If it is larger than ind, set it to the target."
  17656. (let* ((l (org-remove-tabs line))
  17657. (i (org-get-indentation l))
  17658. (i1 (car ind)) (i2 (cdr ind)))
  17659. (if (>= i i2) (setq l (substring line i2)))
  17660. (if (> i1 0)
  17661. (concat (make-string i1 ?\ ) l)
  17662. l)))
  17663. (defun org-remove-indentation (code &optional n)
  17664. "Remove the maximum common indentation from the lines in CODE.
  17665. N may optionally be the number of spaces to remove."
  17666. (with-temp-buffer
  17667. (insert code)
  17668. (org-do-remove-indentation n)
  17669. (buffer-string)))
  17670. (defun org-do-remove-indentation (&optional n)
  17671. "Remove the maximum common indentation from the buffer."
  17672. (untabify (point-min) (point-max))
  17673. (let ((min 10000) re)
  17674. (if n
  17675. (setq min n)
  17676. (goto-char (point-min))
  17677. (while (re-search-forward "^ *[^ \n]" nil t)
  17678. (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
  17679. (unless (or (= min 0) (= min 10000))
  17680. (setq re (format "^ \\{%d\\}" min))
  17681. (goto-char (point-min))
  17682. (while (re-search-forward re nil t)
  17683. (replace-match "")
  17684. (end-of-line 1))
  17685. min)))
  17686. (defun org-fill-template (template alist)
  17687. "Find each %key of ALIST in TEMPLATE and replace it."
  17688. (let ((case-fold-search nil)
  17689. entry key value)
  17690. (setq alist (sort (copy-sequence alist)
  17691. (lambda (a b) (< (length (car a)) (length (car b))))))
  17692. (while (setq entry (pop alist))
  17693. (setq template
  17694. (replace-regexp-in-string
  17695. (concat "%" (regexp-quote (car entry)))
  17696. (cdr entry) template t t)))
  17697. template))
  17698. (defun org-base-buffer (buffer)
  17699. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  17700. (if (not buffer)
  17701. buffer
  17702. (or (buffer-base-buffer buffer)
  17703. buffer)))
  17704. (defun org-trim (s)
  17705. "Remove whitespace at beginning and end of string."
  17706. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  17707. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  17708. s)
  17709. (defun org-wrap (string &optional width lines)
  17710. "Wrap string to either a number of lines, or a width in characters.
  17711. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  17712. that costs. If there is a word longer than WIDTH, the text is actually
  17713. wrapped to the length of that word.
  17714. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  17715. many lines, whatever width that takes.
  17716. The return value is a list of lines, without newlines at the end."
  17717. (let* ((words (org-split-string string "[ \t\n]+"))
  17718. (maxword (apply 'max (mapcar 'org-string-width words)))
  17719. w ll)
  17720. (cond (width
  17721. (org-do-wrap words (max maxword width)))
  17722. (lines
  17723. (setq w maxword)
  17724. (setq ll (org-do-wrap words maxword))
  17725. (if (<= (length ll) lines)
  17726. ll
  17727. (setq ll words)
  17728. (while (> (length ll) lines)
  17729. (setq w (1+ w))
  17730. (setq ll (org-do-wrap words w)))
  17731. ll))
  17732. (t (error "Cannot wrap this")))))
  17733. (defun org-do-wrap (words width)
  17734. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  17735. (let (lines line)
  17736. (while words
  17737. (setq line (pop words))
  17738. (while (and words (< (+ (length line) (length (car words))) width))
  17739. (setq line (concat line " " (pop words))))
  17740. (setq lines (push line lines)))
  17741. (nreverse lines)))
  17742. (defun org-split-string (string &optional separators)
  17743. "Splits STRING into substrings at SEPARATORS.
  17744. No empty strings are returned if there are matches at the beginning
  17745. and end of string."
  17746. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  17747. (start 0)
  17748. notfirst
  17749. (list nil))
  17750. (while (and (string-match rexp string
  17751. (if (and notfirst
  17752. (= start (match-beginning 0))
  17753. (< start (length string)))
  17754. (1+ start) start))
  17755. (< (match-beginning 0) (length string)))
  17756. (setq notfirst t)
  17757. (or (eq (match-beginning 0) 0)
  17758. (and (eq (match-beginning 0) (match-end 0))
  17759. (eq (match-beginning 0) start))
  17760. (setq list
  17761. (cons (substring string start (match-beginning 0))
  17762. list)))
  17763. (setq start (match-end 0)))
  17764. (or (eq start (length string))
  17765. (setq list
  17766. (cons (substring string start)
  17767. list)))
  17768. (nreverse list)))
  17769. (defun org-quote-vert (s)
  17770. "Replace \"|\" with \"\\vert\"."
  17771. (while (string-match "|" s)
  17772. (setq s (replace-match "\\vert" t t s)))
  17773. s)
  17774. (defun org-uuidgen-p (s)
  17775. "Is S an ID created by UUIDGEN?"
  17776. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  17777. (defun org-context ()
  17778. "Return a list of contexts of the current cursor position.
  17779. If several contexts apply, all are returned.
  17780. Each context entry is a list with a symbol naming the context, and
  17781. two positions indicating start and end of the context. Possible
  17782. contexts are:
  17783. :headline anywhere in a headline
  17784. :headline-stars on the leading stars in a headline
  17785. :todo-keyword on a TODO keyword (including DONE) in a headline
  17786. :tags on the TAGS in a headline
  17787. :priority on the priority cookie in a headline
  17788. :item on the first line of a plain list item
  17789. :item-bullet on the bullet/number of a plain list item
  17790. :checkbox on the checkbox in a plain list item
  17791. :table in an org-mode table
  17792. :table-special on a special filed in a table
  17793. :table-table in a table.el table
  17794. :link on a hyperlink
  17795. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  17796. :target on a <<target>>
  17797. :radio-target on a <<<radio-target>>>
  17798. :latex-fragment on a LaTeX fragment
  17799. :latex-preview on a LaTeX fragment with overlayed preview image
  17800. This function expects the position to be visible because it uses font-lock
  17801. faces as a help to recognize the following contexts: :table-special, :link,
  17802. and :keyword."
  17803. (let* ((f (get-text-property (point) 'face))
  17804. (faces (if (listp f) f (list f)))
  17805. (p (point)) clist o)
  17806. ;; First the large context
  17807. (cond
  17808. ((org-on-heading-p t)
  17809. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  17810. (when (progn
  17811. (beginning-of-line 1)
  17812. (looking-at org-todo-line-tags-regexp))
  17813. (push (org-point-in-group p 1 :headline-stars) clist)
  17814. (push (org-point-in-group p 2 :todo-keyword) clist)
  17815. (push (org-point-in-group p 4 :tags) clist))
  17816. (goto-char p)
  17817. (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
  17818. (if (looking-at "\\[#[A-Z0-9]\\]")
  17819. (push (org-point-in-group p 0 :priority) clist)))
  17820. ((org-at-item-p)
  17821. (push (org-point-in-group p 2 :item-bullet) clist)
  17822. (push (list :item (point-at-bol)
  17823. (save-excursion (org-end-of-item) (point)))
  17824. clist)
  17825. (and (org-at-item-checkbox-p)
  17826. (push (org-point-in-group p 0 :checkbox) clist)))
  17827. ((org-at-table-p)
  17828. (push (list :table (org-table-begin) (org-table-end)) clist)
  17829. (if (memq 'org-formula faces)
  17830. (push (list :table-special
  17831. (previous-single-property-change p 'face)
  17832. (next-single-property-change p 'face)) clist)))
  17833. ((org-at-table-p 'any)
  17834. (push (list :table-table) clist)))
  17835. (goto-char p)
  17836. ;; Now the small context
  17837. (cond
  17838. ((org-at-timestamp-p)
  17839. (push (org-point-in-group p 0 :timestamp) clist))
  17840. ((memq 'org-link faces)
  17841. (push (list :link
  17842. (previous-single-property-change p 'face)
  17843. (next-single-property-change p 'face)) clist))
  17844. ((memq 'org-special-keyword faces)
  17845. (push (list :keyword
  17846. (previous-single-property-change p 'face)
  17847. (next-single-property-change p 'face)) clist))
  17848. ((org-on-target-p)
  17849. (push (org-point-in-group p 0 :target) clist)
  17850. (goto-char (1- (match-beginning 0)))
  17851. (if (looking-at org-radio-target-regexp)
  17852. (push (org-point-in-group p 0 :radio-target) clist))
  17853. (goto-char p))
  17854. ((setq o (car (delq nil
  17855. (mapcar
  17856. (lambda (x)
  17857. (if (memq x org-latex-fragment-image-overlays) x))
  17858. (overlays-at (point))))))
  17859. (push (list :latex-fragment
  17860. (overlay-start o) (overlay-end o)) clist)
  17861. (push (list :latex-preview
  17862. (overlay-start o) (overlay-end o)) clist))
  17863. ((org-inside-LaTeX-fragment-p)
  17864. ;; FIXME: positions wrong.
  17865. (push (list :latex-fragment (point) (point)) clist)))
  17866. (setq clist (nreverse (delq nil clist)))
  17867. clist))
  17868. ;; FIXME: Compare with at-regexp-p Do we need both?
  17869. (defun org-in-regexp (re &optional nlines visually)
  17870. "Check if point is inside a match of regexp.
  17871. Normally only the current line is checked, but you can include NLINES extra
  17872. lines both before and after point into the search.
  17873. If VISUALLY is set, require that the cursor is not after the match but
  17874. really on, so that the block visually is on the match."
  17875. (catch 'exit
  17876. (let ((pos (point))
  17877. (eol (point-at-eol (+ 1 (or nlines 0))))
  17878. (inc (if visually 1 0)))
  17879. (save-excursion
  17880. (beginning-of-line (- 1 (or nlines 0)))
  17881. (while (re-search-forward re eol t)
  17882. (if (and (<= (match-beginning 0) pos)
  17883. (>= (+ inc (match-end 0)) pos))
  17884. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  17885. (defun org-at-regexp-p (regexp)
  17886. "Is point inside a match of REGEXP in the current line?"
  17887. (catch 'exit
  17888. (save-excursion
  17889. (let ((pos (point)) (end (point-at-eol)))
  17890. (beginning-of-line 1)
  17891. (while (re-search-forward regexp end t)
  17892. (if (and (<= (match-beginning 0) pos)
  17893. (>= (match-end 0) pos))
  17894. (throw 'exit t)))
  17895. nil))))
  17896. (defun org-between-regexps-p (start-re end-re &optional lim-up lim-down)
  17897. "Non-nil when point is between matches of START-RE and END-RE.
  17898. Also return a non-nil value when point is on one of the matches.
  17899. Optional arguments LIM-UP and LIM-DOWN bound the search; they are
  17900. buffer positions. Default values are the positions of headlines
  17901. surrounding the point.
  17902. The functions returns a cons cell whose car (resp. cdr) is the
  17903. position before START-RE (resp. after END-RE)."
  17904. (save-match-data
  17905. (let ((pos (point))
  17906. (limit-up (or lim-up (save-excursion (outline-previous-heading))))
  17907. (limit-down (or lim-down (save-excursion (outline-next-heading))))
  17908. beg end)
  17909. (save-excursion
  17910. ;; Point is on a block when on START-RE or if START-RE can be
  17911. ;; found before it...
  17912. (and (or (org-at-regexp-p start-re)
  17913. (re-search-backward start-re limit-up t))
  17914. (setq beg (match-beginning 0))
  17915. ;; ... and END-RE after it...
  17916. (goto-char (match-end 0))
  17917. (re-search-forward end-re limit-down t)
  17918. (> (setq end (match-end 0)) pos)
  17919. ;; ... without another START-RE in-between.
  17920. (goto-char (match-beginning 0))
  17921. (not (re-search-backward start-re (1+ beg) t))
  17922. ;; Return value.
  17923. (cons beg end))))))
  17924. (defun org-in-block-p (names)
  17925. "Non-nil when point belongs to a block whose name belongs to NAMES.
  17926. NAMES is a list of strings containing names of blocks.
  17927. Return first block name matched, or nil. Beware that in case of
  17928. nested blocks, the returned name may not belong to the closest
  17929. block from point."
  17930. (save-match-data
  17931. (catch 'exit
  17932. (let ((case-fold-search t)
  17933. (lim-up (save-excursion (outline-previous-heading)))
  17934. (lim-down (save-excursion (outline-next-heading))))
  17935. (mapc (lambda (name)
  17936. (let ((n (regexp-quote name)))
  17937. (when (org-between-regexps-p
  17938. (concat "^[ \t]*#\\+begin_" n)
  17939. (concat "^[ \t]*#\\+end_" n)
  17940. lim-up lim-down)
  17941. (throw 'exit n))))
  17942. names))
  17943. nil)))
  17944. (defun org-occur-in-agenda-files (regexp &optional nlines)
  17945. "Call `multi-occur' with buffers for all agenda files."
  17946. (interactive "sOrg-files matching: \np")
  17947. (let* ((files (org-agenda-files))
  17948. (tnames (mapcar 'file-truename files))
  17949. (extra org-agenda-text-search-extra-files)
  17950. f)
  17951. (when (eq (car extra) 'agenda-archives)
  17952. (setq extra (cdr extra))
  17953. (setq files (org-add-archive-files files)))
  17954. (while (setq f (pop extra))
  17955. (unless (member (file-truename f) tnames)
  17956. (add-to-list 'files f 'append)
  17957. (add-to-list 'tnames (file-truename f) 'append)))
  17958. (multi-occur
  17959. (mapcar (lambda (x)
  17960. (with-current-buffer
  17961. (or (get-file-buffer x) (find-file-noselect x))
  17962. (widen)
  17963. (current-buffer)))
  17964. files)
  17965. regexp)))
  17966. (if (boundp 'occur-mode-find-occurrence-hook)
  17967. ;; Emacs 23
  17968. (add-hook 'occur-mode-find-occurrence-hook
  17969. (lambda ()
  17970. (when (eq major-mode 'org-mode)
  17971. (org-reveal))))
  17972. ;; Emacs 22
  17973. (defadvice occur-mode-goto-occurrence
  17974. (after org-occur-reveal activate)
  17975. (and (eq major-mode 'org-mode) (org-reveal)))
  17976. (defadvice occur-mode-goto-occurrence-other-window
  17977. (after org-occur-reveal activate)
  17978. (and (eq major-mode 'org-mode) (org-reveal)))
  17979. (defadvice occur-mode-display-occurrence
  17980. (after org-occur-reveal activate)
  17981. (when (eq major-mode 'org-mode)
  17982. (let ((pos (occur-mode-find-occurrence)))
  17983. (with-current-buffer (marker-buffer pos)
  17984. (save-excursion
  17985. (goto-char pos)
  17986. (org-reveal)))))))
  17987. (defun org-occur-link-in-agenda-files ()
  17988. "Create a link and search for it in the agendas.
  17989. The link is not stored in `org-stored-links', it is just created
  17990. for the search purpose."
  17991. (interactive)
  17992. (let ((link (condition-case nil
  17993. (org-store-link nil)
  17994. (error "Unable to create a link to here"))))
  17995. (org-occur-in-agenda-files (regexp-quote link))))
  17996. (defun org-uniquify (list)
  17997. "Remove duplicate elements from LIST."
  17998. (let (res)
  17999. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  18000. res))
  18001. (defun org-delete-all (elts list)
  18002. "Remove all elements in ELTS from LIST."
  18003. (while elts
  18004. (setq list (delete (pop elts) list)))
  18005. list)
  18006. (defun org-count (cl-item cl-seq)
  18007. "Count the number of occurrences of ITEM in SEQ.
  18008. Taken from `count' in cl-seq.el with all keyword arguments removed."
  18009. (let ((cl-end (length cl-seq)) (cl-start 0) (cl-count 0) cl-x)
  18010. (when (consp cl-seq) (setq cl-seq (nthcdr cl-start cl-seq)))
  18011. (while (< cl-start cl-end)
  18012. (setq cl-x (if (consp cl-seq) (pop cl-seq) (aref cl-seq cl-start)))
  18013. (if (equal cl-item cl-x) (setq cl-count (1+ cl-count)))
  18014. (setq cl-start (1+ cl-start)))
  18015. cl-count))
  18016. (defun org-remove-if (predicate seq)
  18017. "Remove everything from SEQ that fulfills PREDICATE."
  18018. (let (res e)
  18019. (while seq
  18020. (setq e (pop seq))
  18021. (if (not (funcall predicate e)) (push e res)))
  18022. (nreverse res)))
  18023. (defun org-remove-if-not (predicate seq)
  18024. "Remove everything from SEQ that does not fulfill PREDICATE."
  18025. (let (res e)
  18026. (while seq
  18027. (setq e (pop seq))
  18028. (if (funcall predicate e) (push e res)))
  18029. (nreverse res)))
  18030. (defun org-back-over-empty-lines ()
  18031. "Move backwards over whitespace, to the beginning of the first empty line.
  18032. Returns the number of empty lines passed."
  18033. (let ((pos (point)))
  18034. (if (cdr (assoc 'heading org-blank-before-new-entry))
  18035. (skip-chars-backward " \t\n\r")
  18036. (unless (eobp)
  18037. (forward-line -1)))
  18038. (beginning-of-line 2)
  18039. (goto-char (min (point) pos))
  18040. (count-lines (point) pos)))
  18041. (defun org-skip-whitespace ()
  18042. (skip-chars-forward " \t\n\r"))
  18043. (defun org-point-in-group (point group &optional context)
  18044. "Check if POINT is in match-group GROUP.
  18045. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  18046. match. If the match group does not exist or point is not inside it,
  18047. return nil."
  18048. (and (match-beginning group)
  18049. (>= point (match-beginning group))
  18050. (<= point (match-end group))
  18051. (if context
  18052. (list context (match-beginning group) (match-end group))
  18053. t)))
  18054. (defun org-switch-to-buffer-other-window (&rest args)
  18055. "Switch to buffer in a second window on the current frame.
  18056. In particular, do not allow pop-up frames.
  18057. Returns the newly created buffer."
  18058. (let (pop-up-frames special-display-buffer-names special-display-regexps
  18059. special-display-function)
  18060. (apply 'switch-to-buffer-other-window args)))
  18061. (defun org-combine-plists (&rest plists)
  18062. "Create a single property list from all plists in PLISTS.
  18063. The process starts by copying the first list, and then setting properties
  18064. from the other lists. Settings in the last list are the most significant
  18065. ones and overrule settings in the other lists."
  18066. (let ((rtn (copy-sequence (pop plists)))
  18067. p v ls)
  18068. (while plists
  18069. (setq ls (pop plists))
  18070. (while ls
  18071. (setq p (pop ls) v (pop ls))
  18072. (setq rtn (plist-put rtn p v))))
  18073. rtn))
  18074. (defun org-move-line-down (arg)
  18075. "Move the current line down. With prefix argument, move it past ARG lines."
  18076. (interactive "p")
  18077. (let ((col (current-column))
  18078. beg end pos)
  18079. (beginning-of-line 1) (setq beg (point))
  18080. (beginning-of-line 2) (setq end (point))
  18081. (beginning-of-line (+ 1 arg))
  18082. (setq pos (move-marker (make-marker) (point)))
  18083. (insert (delete-and-extract-region beg end))
  18084. (goto-char pos)
  18085. (org-move-to-column col)))
  18086. (defun org-move-line-up (arg)
  18087. "Move the current line up. With prefix argument, move it past ARG lines."
  18088. (interactive "p")
  18089. (let ((col (current-column))
  18090. beg end pos)
  18091. (beginning-of-line 1) (setq beg (point))
  18092. (beginning-of-line 2) (setq end (point))
  18093. (beginning-of-line (- arg))
  18094. (setq pos (move-marker (make-marker) (point)))
  18095. (insert (delete-and-extract-region beg end))
  18096. (goto-char pos)
  18097. (org-move-to-column col)))
  18098. (defun org-replace-escapes (string table)
  18099. "Replace %-escapes in STRING with values in TABLE.
  18100. TABLE is an association list with keys like \"%a\" and string values.
  18101. The sequences in STRING may contain normal field width and padding information,
  18102. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  18103. so values can contain further %-escapes if they are define later in TABLE."
  18104. (let ((tbl (copy-alist table))
  18105. (case-fold-search nil)
  18106. (pchg 0)
  18107. e re rpl)
  18108. (while (setq e (pop tbl))
  18109. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  18110. (when (and (cdr e) (string-match re (cdr e)))
  18111. (let ((sref (substring (cdr e) (match-beginning 0) (match-end 0)))
  18112. (safe "SREF"))
  18113. (add-text-properties 0 3 (list 'sref sref) safe)
  18114. (setcdr e (replace-match safe t t (cdr e)))))
  18115. (while (string-match re string)
  18116. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  18117. (cdr e)))
  18118. (setq string (replace-match rpl t t string))))
  18119. (while (setq pchg (next-property-change pchg string))
  18120. (let ((sref (get-text-property pchg 'sref string)))
  18121. (when (and sref (string-match "SREF" string pchg))
  18122. (setq string (replace-match sref t t string)))))
  18123. string))
  18124. (defun org-sublist (list start end)
  18125. "Return a section of LIST, from START to END.
  18126. Counting starts at 1."
  18127. (let (rtn (c start))
  18128. (setq list (nthcdr (1- start) list))
  18129. (while (and list (<= c end))
  18130. (push (pop list) rtn)
  18131. (setq c (1+ c)))
  18132. (nreverse rtn)))
  18133. (defun org-find-base-buffer-visiting (file)
  18134. "Like `find-buffer-visiting' but always return the base buffer and
  18135. not an indirect buffer."
  18136. (let ((buf (or (get-file-buffer file)
  18137. (find-buffer-visiting file))))
  18138. (if buf
  18139. (or (buffer-base-buffer buf) buf)
  18140. nil)))
  18141. (defun org-image-file-name-regexp (&optional extensions)
  18142. "Return regexp matching the file names of images.
  18143. If EXTENSIONS is given, only match these."
  18144. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  18145. (image-file-name-regexp)
  18146. (let ((image-file-name-extensions
  18147. (or extensions
  18148. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  18149. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  18150. (concat "\\."
  18151. (regexp-opt (nconc (mapcar 'upcase
  18152. image-file-name-extensions)
  18153. image-file-name-extensions)
  18154. t)
  18155. "\\'"))))
  18156. (defun org-file-image-p (file &optional extensions)
  18157. "Return non-nil if FILE is an image."
  18158. (save-match-data
  18159. (string-match (org-image-file-name-regexp extensions) file)))
  18160. (defun org-get-cursor-date ()
  18161. "Return the date at cursor in as a time.
  18162. This works in the calendar and in the agenda, anywhere else it just
  18163. returns the current time."
  18164. (let (date day defd)
  18165. (cond
  18166. ((eq major-mode 'calendar-mode)
  18167. (setq date (calendar-cursor-to-date)
  18168. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  18169. ((eq major-mode 'org-agenda-mode)
  18170. (setq day (get-text-property (point) 'day))
  18171. (if day
  18172. (setq date (calendar-gregorian-from-absolute day)
  18173. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
  18174. (nth 2 date))))))
  18175. (or defd (current-time))))
  18176. (defvar org-agenda-action-marker (make-marker)
  18177. "Marker pointing to the entry for the next agenda action.")
  18178. (defun org-mark-entry-for-agenda-action ()
  18179. "Mark the current entry as target of an agenda action.
  18180. Agenda actions are actions executed from the agenda with the key `k',
  18181. which make use of the date at the cursor."
  18182. (interactive)
  18183. (move-marker org-agenda-action-marker
  18184. (save-excursion (org-back-to-heading t) (point))
  18185. (current-buffer))
  18186. (message
  18187. "Entry marked for action; press `k' at desired date in agenda or calendar"))
  18188. (defun org-mark-subtree ()
  18189. "Mark the current subtree.
  18190. This puts point at the start of the current subtree, and mark at the end.
  18191. If point is in an inline task, mark that task instead."
  18192. (interactive)
  18193. (let ((inline-task-p
  18194. (and (featurep 'org-inlinetask)
  18195. (org-inlinetask-in-task-p)))
  18196. (beg))
  18197. ;; Get beginning of subtree
  18198. (cond
  18199. (inline-task-p (org-inlinetask-goto-beginning))
  18200. ((org-at-heading-p) (beginning-of-line))
  18201. (t (org-with-limited-levels (outline-previous-visible-heading 1))))
  18202. (setq beg (point))
  18203. ;; Get end of it
  18204. (if inline-task-p
  18205. (org-inlinetask-goto-end)
  18206. (org-end-of-subtree))
  18207. ;; Mark zone
  18208. (push-mark (point) nil t)
  18209. (goto-char beg)))
  18210. ;;; Paragraph filling stuff.
  18211. ;; We want this to be just right, so use the full arsenal.
  18212. (defun org-indent-line-function ()
  18213. "Indent line depending on context."
  18214. (interactive)
  18215. (let* ((pos (point))
  18216. (itemp (org-at-item-p))
  18217. (case-fold-search t)
  18218. (org-drawer-regexp (or org-drawer-regexp "\000"))
  18219. (inline-task-p (and (featurep 'org-inlinetask)
  18220. (org-inlinetask-in-task-p)))
  18221. (inline-re (and inline-task-p
  18222. (org-inlinetask-outline-regexp)))
  18223. column)
  18224. (beginning-of-line 1)
  18225. (cond
  18226. ;; Comments
  18227. ((looking-at "# ") (setq column 0))
  18228. ;; Headings
  18229. ((looking-at org-outline-regexp) (setq column 0))
  18230. ;; Included files
  18231. ((looking-at "#\\+include:") (setq column 0))
  18232. ;; Footnote definition
  18233. ((looking-at org-footnote-definition-re) (setq column 0))
  18234. ;; Literal examples
  18235. ((looking-at "[ \t]*:\\( \\|$\\)")
  18236. (setq column (org-get-indentation))) ; do nothing
  18237. ;; Lists
  18238. ((ignore-errors (goto-char (org-in-item-p)))
  18239. (setq column (if itemp
  18240. (org-get-indentation)
  18241. (org-list-item-body-column (point))))
  18242. (goto-char pos))
  18243. ;; Drawers
  18244. ((and (looking-at "[ \t]*:END:")
  18245. (save-excursion (re-search-backward org-drawer-regexp nil t)))
  18246. (save-excursion
  18247. (goto-char (1- (match-beginning 1)))
  18248. (setq column (current-column))))
  18249. ;; Special blocks
  18250. ((and (looking-at "[ \t]*#\\+end_\\([a-z]+\\)")
  18251. (save-excursion
  18252. (re-search-backward
  18253. (concat "^[ \t]*#\\+begin_" (downcase (match-string 1))) nil t)))
  18254. (setq column (org-get-indentation (match-string 0))))
  18255. ((and (not (looking-at "[ \t]*#\\+begin_"))
  18256. (org-between-regexps-p "^[ \t]*#\\+begin_" "[ \t]*#\\+end_"))
  18257. (save-excursion
  18258. (re-search-backward "^[ \t]*#\\+begin_\\([a-z]+\\)" nil t))
  18259. (setq column
  18260. (cond ((equal (downcase (match-string 1)) "src")
  18261. ;; src blocks: let `org-edit-src-exit' handle them
  18262. (org-get-indentation))
  18263. ((equal (downcase (match-string 1)) "example")
  18264. (max (org-get-indentation)
  18265. (org-get-indentation (match-string 0))))
  18266. (t
  18267. (org-get-indentation (match-string 0))))))
  18268. ;; This line has nothing special, look at the previous relevant
  18269. ;; line to compute indentation
  18270. (t
  18271. (beginning-of-line 0)
  18272. (while (and (not (bobp))
  18273. (not (looking-at org-drawer-regexp))
  18274. ;; When point started in an inline task, do not move
  18275. ;; above task starting line.
  18276. (not (and inline-task-p (looking-at inline-re)))
  18277. ;; Skip drawers, blocks, empty lines, verbatim,
  18278. ;; comments, tables, footnotes definitions, lists,
  18279. ;; inline tasks.
  18280. (or (and (looking-at "[ \t]*:END:")
  18281. (re-search-backward org-drawer-regexp nil t))
  18282. (and (looking-at "[ \t]*#\\+end_")
  18283. (re-search-backward "[ \t]*#\\+begin_"nil t))
  18284. (looking-at "[ \t]*[\n:#|]")
  18285. (looking-at org-footnote-definition-re)
  18286. (and (ignore-errors (goto-char (org-in-item-p)))
  18287. (goto-char
  18288. (org-list-get-top-point (org-list-struct))))
  18289. (and (not inline-task-p)
  18290. (featurep 'org-inlinetask)
  18291. (org-inlinetask-in-task-p)
  18292. (or (org-inlinetask-goto-beginning) t))))
  18293. (beginning-of-line 0))
  18294. (cond
  18295. ;; There was an heading above.
  18296. ((looking-at "\\*+[ \t]+")
  18297. (if (not org-adapt-indentation)
  18298. (setq column 0)
  18299. (goto-char (match-end 0))
  18300. (setq column (current-column))))
  18301. ;; A drawer had started and is unfinished
  18302. ((looking-at org-drawer-regexp)
  18303. (goto-char (1- (match-beginning 1)))
  18304. (setq column (current-column)))
  18305. ;; Else, nothing noticeable found: get indentation and go on.
  18306. (t (setq column (org-get-indentation))))))
  18307. ;; Now apply indentation and move cursor accordingly
  18308. (goto-char pos)
  18309. (if (<= (current-column) (current-indentation))
  18310. (org-indent-line-to column)
  18311. (save-excursion (org-indent-line-to column)))
  18312. ;; Special polishing for properties, see `org-property-format'
  18313. (setq column (current-column))
  18314. (beginning-of-line 1)
  18315. (if (looking-at
  18316. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  18317. (replace-match (concat (match-string 1)
  18318. (format org-property-format
  18319. (match-string 2) (match-string 3)))
  18320. t t))
  18321. (org-move-to-column column)))
  18322. (defvar org-adaptive-fill-regexp-backup adaptive-fill-regexp
  18323. "Variable to store copy of `adaptive-fill-regexp'.
  18324. Since `adaptive-fill-regexp' is set to never match, we need to
  18325. store a backup of its value before entering `org-mode' so that
  18326. the functionality can be provided as a fall-back.")
  18327. (defun org-set-autofill-regexps ()
  18328. (interactive)
  18329. ;; In the paragraph separator we include headlines, because filling
  18330. ;; text in a line directly attached to a headline would otherwise
  18331. ;; fill the headline as well.
  18332. (org-set-local 'comment-start-skip "^#+[ \t]*")
  18333. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|#]")
  18334. ;; The paragraph starter includes hand-formatted lists.
  18335. (org-set-local
  18336. 'paragraph-start
  18337. (concat
  18338. "\f" "\\|"
  18339. "[ ]*$" "\\|"
  18340. org-outline-regexp "\\|"
  18341. "[ \t]*#" "\\|"
  18342. (org-item-re) "\\|"
  18343. "[ \t]*[:|]" "\\|"
  18344. "\\$\\$" "\\|"
  18345. "\\\\\\(begin\\|end\\|[][]\\)"))
  18346. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  18347. ;; But only if the user has not turned off tables or fixed-width regions
  18348. (org-set-local
  18349. 'auto-fill-inhibit-regexp
  18350. (concat org-outline-regexp
  18351. "\\|#\\+"
  18352. "\\|[ \t]*" org-keyword-time-regexp
  18353. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  18354. (concat
  18355. "\\|[ \t]*["
  18356. (if org-enable-table-editor "|" "")
  18357. (if org-enable-fixed-width-editor ":" "")
  18358. "]"))))
  18359. ;; We use our own fill-paragraph function, to make sure that tables
  18360. ;; and fixed-width regions are not wrapped. That function will pass
  18361. ;; through to `fill-paragraph' when appropriate.
  18362. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  18363. ;; Prevent auto-fill from inserting unwanted new items.
  18364. (if (boundp 'fill-nobreak-predicate)
  18365. (org-set-local 'fill-nobreak-predicate
  18366. (if (memq 'org-fill-item-nobreak-p fill-nobreak-predicate)
  18367. fill-nobreak-predicate
  18368. (cons 'org-fill-item-nobreak-p fill-nobreak-predicate))))
  18369. ;; Adaptive filling: To get full control, first make sure that
  18370. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  18371. (unless (local-variable-p 'adaptive-fill-regexp (current-buffer))
  18372. (org-set-local 'org-adaptive-fill-regexp-backup
  18373. adaptive-fill-regexp))
  18374. (org-set-local 'adaptive-fill-regexp "\000")
  18375. (org-set-local 'normal-auto-fill-function 'org-auto-fill-function)
  18376. (org-set-local 'adaptive-fill-function
  18377. 'org-adaptive-fill-function)
  18378. (org-set-local
  18379. 'align-mode-rules-list
  18380. '((org-in-buffer-settings
  18381. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  18382. (modes . '(org-mode))))))
  18383. (defun org-fill-item-nobreak-p ()
  18384. "Non-nil when a line break at point would insert a new item."
  18385. (and (looking-at (org-item-re)) (org-list-in-valid-context-p)))
  18386. (defun org-fill-paragraph (&optional justify)
  18387. "Re-align a table, pass through to fill-paragraph if no table."
  18388. (let ((table-p (org-at-table-p))
  18389. (table.el-p (org-at-table.el-p))
  18390. (itemp (org-in-item-p)))
  18391. (cond ((and (equal (char-after (point-at-bol)) ?*)
  18392. (save-excursion (goto-char (point-at-bol))
  18393. (looking-at org-outline-regexp)))
  18394. t) ; skip headlines
  18395. (table.el-p t) ; skip table.el tables
  18396. (table-p (org-table-align) t) ; align Org tables
  18397. (itemp ; align text in items
  18398. (let* ((struct (save-excursion (goto-char itemp)
  18399. (org-list-struct)))
  18400. (parents (org-list-parents-alist struct))
  18401. (children (org-list-get-children itemp struct parents))
  18402. beg end prev next prefix)
  18403. ;; Determine in which part of item point is: before
  18404. ;; first child, after last child, between two
  18405. ;; sub-lists, or simply in item if there's no child.
  18406. (cond
  18407. ((not children)
  18408. (setq prefix (make-string (org-list-item-body-column itemp) ?\ )
  18409. beg itemp
  18410. end (org-list-get-item-end itemp struct)))
  18411. ((< (point) (setq next (car children)))
  18412. (setq prefix (make-string (org-list-item-body-column itemp) ?\ )
  18413. beg itemp
  18414. end next))
  18415. ((> (point) (setq prev (car (last children))))
  18416. (setq beg (org-list-get-item-end prev struct)
  18417. end (org-list-get-item-end itemp struct)
  18418. prefix (save-excursion
  18419. (goto-char beg)
  18420. (skip-chars-forward " \t")
  18421. (make-string (current-column) ?\ ))))
  18422. (t (catch 'exit
  18423. (while (setq next (pop children))
  18424. (if (> (point) next)
  18425. (setq prev next)
  18426. (setq beg (org-list-get-item-end prev struct)
  18427. end next
  18428. prefix (save-excursion
  18429. (goto-char beg)
  18430. (skip-chars-forward " \t")
  18431. (make-string (current-column) ?\ )))
  18432. (throw 'exit nil))))))
  18433. ;; Use `fill-paragraph' with buffer narrowed to item
  18434. ;; without any child, and with our computed PREFIX.
  18435. (flet ((fill-context-prefix (from to &optional flr) prefix))
  18436. (save-restriction
  18437. (narrow-to-region beg end)
  18438. (save-excursion (fill-paragraph justify)))) t))
  18439. ;; Special case where point is not in a list but is on
  18440. ;; a paragraph adjacent to a list: make sure this paragraph
  18441. ;; doesn't get merged with the end of the list by narrowing
  18442. ;; buffer first.
  18443. ((save-excursion (forward-paragraph -1)
  18444. (setq itemp (org-in-item-p)))
  18445. (let ((struct (save-excursion (goto-char itemp)
  18446. (org-list-struct))))
  18447. (save-restriction
  18448. (narrow-to-region (org-list-get-bottom-point struct)
  18449. (save-excursion (forward-paragraph 1)
  18450. (point)))
  18451. (fill-paragraph justify) t)))
  18452. ;; Else simply call `fill-paragraph'.
  18453. (t nil))))
  18454. ;; For reference, this is the default value of adaptive-fill-regexp
  18455. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  18456. (defun org-adaptive-fill-function ()
  18457. "Return a fill prefix for org-mode files."
  18458. (let (itemp)
  18459. (save-excursion
  18460. (cond
  18461. ;; Comment line
  18462. ((looking-at "#[ \t]+")
  18463. (match-string-no-properties 0))
  18464. ;; Plain list item
  18465. ((org-at-item-p)
  18466. (make-string (org-list-item-body-column (point-at-bol)) ?\ ))
  18467. ;; Point is in a list after `backward-paragraph': original
  18468. ;; point wasn't in the list, or filling would have been taken
  18469. ;; care of by `org-auto-fill-function', but the list and the
  18470. ;; real paragraph are not separated by a blank line. Thus, move
  18471. ;; point after the list to go back to real paragraph and
  18472. ;; determine fill-prefix.
  18473. ((setq itemp (org-in-item-p))
  18474. (goto-char itemp)
  18475. (let* ((struct (org-list-struct))
  18476. (bottom (org-list-get-bottom-point struct)))
  18477. (goto-char bottom)
  18478. (make-string (org-get-indentation) ?\ )))
  18479. ;; Other text
  18480. ((looking-at org-adaptive-fill-regexp-backup)
  18481. (match-string-no-properties 0))))))
  18482. (defun org-auto-fill-function ()
  18483. "Auto-fill function."
  18484. (let (itemp prefix)
  18485. ;; When in a list, compute an appropriate fill-prefix and make
  18486. ;; sure it will be used by `do-auto-fill'.
  18487. (if (setq itemp (org-in-item-p))
  18488. (progn
  18489. (setq prefix (make-string (org-list-item-body-column itemp) ?\ ))
  18490. (flet ((fill-context-prefix (from to &optional flr) prefix))
  18491. (do-auto-fill)))
  18492. ;; Else just use `do-auto-fill'.
  18493. (do-auto-fill))))
  18494. ;;; Other stuff.
  18495. (defun org-toggle-fixed-width-section (arg)
  18496. "Toggle the fixed-width export.
  18497. If there is no active region, the QUOTE keyword at the current headline is
  18498. inserted or removed. When present, it causes the text between this headline
  18499. and the next to be exported as fixed-width text, and unmodified.
  18500. If there is an active region, this command adds or removes a colon as the
  18501. first character of this line. If the first character of a line is a colon,
  18502. this line is also exported in fixed-width font."
  18503. (interactive "P")
  18504. (let* ((cc 0)
  18505. (regionp (org-region-active-p))
  18506. (beg (if regionp (region-beginning) (point)))
  18507. (end (if regionp (region-end)))
  18508. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  18509. (case-fold-search nil)
  18510. (re "[ \t]*\\(:\\(?: \\|$\\)\\)")
  18511. off)
  18512. (if regionp
  18513. (save-excursion
  18514. (goto-char beg)
  18515. (setq cc (current-column))
  18516. (beginning-of-line 1)
  18517. (setq off (looking-at re))
  18518. (while (> nlines 0)
  18519. (setq nlines (1- nlines))
  18520. (beginning-of-line 1)
  18521. (cond
  18522. (arg
  18523. (org-move-to-column cc t)
  18524. (insert ": \n")
  18525. (forward-line -1))
  18526. ((and off (looking-at re))
  18527. (replace-match "" t t nil 1))
  18528. ((not off) (org-move-to-column cc t) (insert ": ")))
  18529. (forward-line 1)))
  18530. (save-excursion
  18531. (org-back-to-heading)
  18532. (cond
  18533. ((looking-at (format org-heading-keyword-regexp-format
  18534. org-quote-string))
  18535. (goto-char (match-end 1))
  18536. (looking-at (concat " +" org-quote-string))
  18537. (replace-match "" t t)
  18538. (when (eolp) (insert " ")))
  18539. ((looking-at org-outline-regexp)
  18540. (goto-char (match-end 0))
  18541. (insert org-quote-string " ")))))))
  18542. (defun org-reftex-citation ()
  18543. "Use reftex-citation to insert a citation into the buffer.
  18544. This looks for a line like
  18545. #+BIBLIOGRAPHY: foo plain option:-d
  18546. and derives from it that foo.bib is the bibliography file relevant
  18547. for this document. It then installs the necessary environment for RefTeX
  18548. to work in this buffer and calls `reftex-citation' to insert a citation
  18549. into the buffer.
  18550. Export of such citations to both LaTeX and HTML is handled by the contributed
  18551. package org-exp-bibtex by Taru Karttunen."
  18552. (interactive)
  18553. (let ((reftex-docstruct-symbol 'rds)
  18554. (reftex-cite-format "\\cite{%l}")
  18555. rds bib)
  18556. (save-excursion
  18557. (save-restriction
  18558. (widen)
  18559. (let ((case-fold-search t)
  18560. (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
  18561. (if (not (save-excursion
  18562. (or (re-search-forward re nil t)
  18563. (re-search-backward re nil t))))
  18564. (error "No bibliography defined in file")
  18565. (setq bib (concat (match-string 1) ".bib")
  18566. rds (list (list 'bib bib)))))))
  18567. (call-interactively 'reftex-citation)))
  18568. ;;;; Functions extending outline functionality
  18569. (defun org-beginning-of-line (&optional arg)
  18570. "Go to the beginning of the current line. If that is invisible, continue
  18571. to a visible line beginning. This makes the function of C-a more intuitive.
  18572. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  18573. first attempt, and only move to after the tags when the cursor is already
  18574. beyond the end of the headline."
  18575. (interactive "P")
  18576. (let ((pos (point))
  18577. (special (if (consp org-special-ctrl-a/e)
  18578. (car org-special-ctrl-a/e)
  18579. org-special-ctrl-a/e))
  18580. refpos)
  18581. (if (org-bound-and-true-p line-move-visual)
  18582. (beginning-of-visual-line 1)
  18583. (beginning-of-line 1))
  18584. (if (and arg (fboundp 'move-beginning-of-line))
  18585. (call-interactively 'move-beginning-of-line)
  18586. (if (bobp)
  18587. nil
  18588. (backward-char 1)
  18589. (if (org-truely-invisible-p)
  18590. (while (and (not (bobp)) (org-truely-invisible-p))
  18591. (backward-char 1)
  18592. (beginning-of-line 1))
  18593. (forward-char 1))))
  18594. (when special
  18595. (cond
  18596. ((and (looking-at org-complex-heading-regexp)
  18597. (= (char-after (match-end 1)) ?\ ))
  18598. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  18599. (point-at-eol)))
  18600. (goto-char
  18601. (if (eq special t)
  18602. (cond ((> pos refpos) refpos)
  18603. ((= pos (point)) refpos)
  18604. (t (point)))
  18605. (cond ((> pos (point)) (point))
  18606. ((not (eq last-command this-command)) (point))
  18607. (t refpos)))))
  18608. ((org-at-item-p)
  18609. (goto-char
  18610. (if (eq special t)
  18611. (cond ((> pos (match-end 0)) (match-end 0))
  18612. ((= pos (point)) (match-end 0))
  18613. (t (point)))
  18614. (cond ((> pos (point)) (point))
  18615. ((not (eq last-command this-command)) (point))
  18616. (t (match-end 0))))))))
  18617. (org-no-warnings
  18618. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  18619. (defun org-end-of-line (&optional arg)
  18620. "Go to the end of the line.
  18621. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  18622. first attempt, and only move to after the tags when the cursor is already
  18623. beyond the end of the headline."
  18624. (interactive "P")
  18625. (let ((special (if (consp org-special-ctrl-a/e)
  18626. (cdr org-special-ctrl-a/e)
  18627. org-special-ctrl-a/e)))
  18628. (cond
  18629. ((or (not special) arg
  18630. (not (or (org-on-heading-p) (org-at-item-p))))
  18631. (call-interactively
  18632. (cond ((org-bound-and-true-p line-move-visual) 'end-of-visual-line)
  18633. ((fboundp 'move-end-of-line) 'move-end-of-line)
  18634. (t 'end-of-line))))
  18635. ((org-on-heading-p)
  18636. (let ((pos (point)))
  18637. (beginning-of-line 1)
  18638. (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*\\)?$"))
  18639. (if (eq special t)
  18640. (if (or (< pos (match-beginning 1))
  18641. (= pos (match-end 0)))
  18642. (goto-char (match-beginning 1))
  18643. (goto-char (match-end 0)))
  18644. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  18645. (goto-char (match-end 0))
  18646. (goto-char (match-beginning 1))))
  18647. (call-interactively (if (fboundp 'move-end-of-line)
  18648. 'move-end-of-line
  18649. 'end-of-line)))))
  18650. ;; At an item: Move before any hidden text.
  18651. (t (call-interactively 'end-of-line)))
  18652. (org-no-warnings
  18653. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  18654. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  18655. (define-key org-mode-map "\C-e" 'org-end-of-line)
  18656. (defun org-backward-sentence (&optional arg)
  18657. "Go to beginning of sentence, or beginning of table field.
  18658. This will call `backward-sentence' or `org-table-beginning-of-field',
  18659. depending on context."
  18660. (interactive "P")
  18661. (cond
  18662. ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
  18663. (t (call-interactively 'backward-sentence))))
  18664. (defun org-forward-sentence (&optional arg)
  18665. "Go to end of sentence, or end of table field.
  18666. This will call `forward-sentence' or `org-table-end-of-field',
  18667. depending on context."
  18668. (interactive "P")
  18669. (cond
  18670. ((org-at-table-p) (call-interactively 'org-table-end-of-field))
  18671. (t (call-interactively 'forward-sentence))))
  18672. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  18673. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  18674. (defun org-kill-line (&optional arg)
  18675. "Kill line, to tags or end of line."
  18676. (interactive "P")
  18677. (cond
  18678. ((or (not org-special-ctrl-k)
  18679. (bolp)
  18680. (not (org-on-heading-p)))
  18681. (if (and (get-char-property (min (point-max) (point-at-eol)) 'invisible)
  18682. org-ctrl-k-protect-subtree)
  18683. (if (or (eq org-ctrl-k-protect-subtree 'error)
  18684. (not (y-or-n-p "Kill hidden subtree along with headline? ")))
  18685. (error "C-k aborted - would kill hidden subtree")))
  18686. (call-interactively 'kill-line))
  18687. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$"))
  18688. (kill-region (point) (match-beginning 1))
  18689. (org-set-tags nil t))
  18690. (t (kill-region (point) (point-at-eol)))))
  18691. (define-key org-mode-map "\C-k" 'org-kill-line)
  18692. (defun org-yank (&optional arg)
  18693. "Yank. If the kill is a subtree, treat it specially.
  18694. This command will look at the current kill and check if is a single
  18695. subtree, or a series of subtrees[1]. If it passes the test, and if the
  18696. cursor is at the beginning of a line or after the stars of a currently
  18697. empty headline, then the yank is handled specially. How exactly depends
  18698. on the value of the following variables, both set by default.
  18699. org-yank-folded-subtrees
  18700. When set, the subtree(s) will be folded after insertion, but only
  18701. if doing so would now swallow text after the yanked text.
  18702. org-yank-adjusted-subtrees
  18703. When set, the subtree will be promoted or demoted in order to
  18704. fit into the local outline tree structure, which means that the level
  18705. will be adjusted so that it becomes the smaller one of the two
  18706. *visible* surrounding headings.
  18707. Any prefix to this command will cause `yank' to be called directly with
  18708. no special treatment. In particular, a simple \\[universal-argument] prefix \
  18709. will just
  18710. plainly yank the text as it is.
  18711. \[1] The test checks if the first non-white line is a heading
  18712. and if there are no other headings with fewer stars."
  18713. (interactive "P")
  18714. (org-yank-generic 'yank arg))
  18715. (defun org-yank-generic (command arg)
  18716. "Perform some yank-like command.
  18717. This function implements the behavior described in the `org-yank'
  18718. documentation. However, it has been generalized to work for any
  18719. interactive command with similar behavior."
  18720. ;; pretend to be command COMMAND
  18721. (setq this-command command)
  18722. (if arg
  18723. (call-interactively command)
  18724. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  18725. (and (org-kill-is-subtree-p)
  18726. (or (bolp)
  18727. (and (looking-at "[ \t]*$")
  18728. (string-match
  18729. "\\`\\*+\\'"
  18730. (buffer-substring (point-at-bol) (point)))))))
  18731. swallowp)
  18732. (cond
  18733. ((and subtreep org-yank-folded-subtrees)
  18734. (let ((beg (point))
  18735. end)
  18736. (if (and subtreep org-yank-adjusted-subtrees)
  18737. (org-paste-subtree nil nil 'for-yank)
  18738. (call-interactively command))
  18739. (setq end (point))
  18740. (goto-char beg)
  18741. (when (and (bolp) subtreep
  18742. (not (setq swallowp
  18743. (org-yank-folding-would-swallow-text beg end))))
  18744. (org-with-limited-levels
  18745. (or (looking-at org-outline-regexp)
  18746. (re-search-forward org-outline-regexp-bol end t))
  18747. (while (and (< (point) end) (looking-at org-outline-regexp))
  18748. (hide-subtree)
  18749. (org-cycle-show-empty-lines 'folded)
  18750. (condition-case nil
  18751. (outline-forward-same-level 1)
  18752. (error (goto-char end))))))
  18753. (when swallowp
  18754. (message
  18755. "Inserted text not folded because that would swallow text"))
  18756. (goto-char end)
  18757. (skip-chars-forward " \t\n\r")
  18758. (beginning-of-line 1)
  18759. (push-mark beg 'nomsg)))
  18760. ((and subtreep org-yank-adjusted-subtrees)
  18761. (let ((beg (point-at-bol)))
  18762. (org-paste-subtree nil nil 'for-yank)
  18763. (push-mark beg 'nomsg)))
  18764. (t
  18765. (call-interactively command))))))
  18766. (defun org-yank-folding-would-swallow-text (beg end)
  18767. "Would hide-subtree at BEG swallow any text after END?"
  18768. (let (level)
  18769. (org-with-limited-levels
  18770. (save-excursion
  18771. (goto-char beg)
  18772. (when (or (looking-at org-outline-regexp)
  18773. (re-search-forward org-outline-regexp-bol end t))
  18774. (setq level (org-outline-level)))
  18775. (goto-char end)
  18776. (skip-chars-forward " \t\r\n\v\f")
  18777. (if (or (eobp)
  18778. (and (bolp) (looking-at org-outline-regexp)
  18779. (<= (org-outline-level) level)))
  18780. nil ; Nothing would be swallowed
  18781. t))))) ; something would swallow
  18782. (define-key org-mode-map "\C-y" 'org-yank)
  18783. (defun org-truely-invisible-p ()
  18784. "Check if point is at a character currently not visible.
  18785. This version does not only check the character property, but also
  18786. `visible-mode'."
  18787. ;; Early versions of noutline don't have `outline-invisible-p'.
  18788. (if (org-bound-and-true-p visible-mode)
  18789. nil
  18790. (outline-invisible-p)))
  18791. (defun org-invisible-p2 ()
  18792. "Check if point is at a character currently not visible."
  18793. (save-excursion
  18794. (if (and (eolp) (not (bobp))) (backward-char 1))
  18795. ;; Early versions of noutline don't have `outline-invisible-p'.
  18796. (outline-invisible-p)))
  18797. (defun org-back-to-heading (&optional invisible-ok)
  18798. "Call `outline-back-to-heading', but provide a better error message."
  18799. (condition-case nil
  18800. (outline-back-to-heading invisible-ok)
  18801. (error (error "Before first headline at position %d in buffer %s"
  18802. (point) (current-buffer)))))
  18803. (defun org-beginning-of-defun ()
  18804. "Go to the beginning of the subtree, i.e. back to the heading."
  18805. (org-back-to-heading))
  18806. (defun org-end-of-defun ()
  18807. "Go to the end of the subtree."
  18808. (org-end-of-subtree nil t))
  18809. (defun org-before-first-heading-p ()
  18810. "Before first heading?"
  18811. (save-excursion
  18812. (end-of-line)
  18813. (null (re-search-backward org-outline-regexp-bol nil t))))
  18814. (defun org-on-heading-p (&optional ignored)
  18815. (outline-on-heading-p t))
  18816. (defun org-at-heading-p (&optional ignored)
  18817. (outline-on-heading-p t))
  18818. (defun org-point-at-end-of-empty-headline ()
  18819. "If point is at the end of an empty headline, return t, else nil.
  18820. If the heading only contains a TODO keyword, it is still still considered
  18821. empty."
  18822. (and (looking-at "[ \t]*$")
  18823. (save-excursion
  18824. (beginning-of-line 1)
  18825. (let ((case-fold-search nil))
  18826. (looking-at org-todo-line-regexp)))
  18827. (string= (match-string 3) "")))
  18828. (defun org-at-heading-or-item-p ()
  18829. (or (org-on-heading-p) (org-at-item-p)))
  18830. (defun org-on-target-p ()
  18831. (or (org-in-regexp org-radio-target-regexp)
  18832. (org-in-regexp org-target-regexp)))
  18833. (defun org-up-heading-all (arg)
  18834. "Move to the heading line of which the present line is a subheading.
  18835. This function considers both visible and invisible heading lines.
  18836. With argument, move up ARG levels."
  18837. (if (fboundp 'outline-up-heading-all)
  18838. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  18839. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  18840. (defun org-up-heading-safe ()
  18841. "Move to the heading line of which the present line is a subheading.
  18842. This version will not throw an error. It will return the level of the
  18843. headline found, or nil if no higher level is found.
  18844. Also, this function will be a lot faster than `outline-up-heading',
  18845. because it relies on stars being the outline starters. This can really
  18846. make a significant difference in outlines with very many siblings."
  18847. (let (start-level re)
  18848. (org-back-to-heading t)
  18849. (setq start-level (funcall outline-level))
  18850. (if (equal start-level 1)
  18851. nil
  18852. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  18853. (if (re-search-backward re nil t)
  18854. (funcall outline-level)))))
  18855. (defun org-first-sibling-p ()
  18856. "Is this heading the first child of its parents?"
  18857. (interactive)
  18858. (let ((re org-outline-regexp-bol)
  18859. level l)
  18860. (unless (org-at-heading-p t)
  18861. (error "Not at a heading"))
  18862. (setq level (funcall outline-level))
  18863. (save-excursion
  18864. (if (not (re-search-backward re nil t))
  18865. t
  18866. (setq l (funcall outline-level))
  18867. (< l level)))))
  18868. (defun org-goto-sibling (&optional previous)
  18869. "Goto the next sibling, even if it is invisible.
  18870. When PREVIOUS is set, go to the previous sibling instead. Returns t
  18871. when a sibling was found. When none is found, return nil and don't
  18872. move point."
  18873. (let ((fun (if previous 're-search-backward 're-search-forward))
  18874. (pos (point))
  18875. (re org-outline-regexp-bol)
  18876. level l)
  18877. (when (condition-case nil (org-back-to-heading t) (error nil))
  18878. (setq level (funcall outline-level))
  18879. (catch 'exit
  18880. (or previous (forward-char 1))
  18881. (while (funcall fun re nil t)
  18882. (setq l (funcall outline-level))
  18883. (when (< l level) (goto-char pos) (throw 'exit nil))
  18884. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  18885. (goto-char pos)
  18886. nil))))
  18887. (defun org-show-siblings ()
  18888. "Show all siblings of the current headline."
  18889. (save-excursion
  18890. (while (org-goto-sibling) (org-flag-heading nil)))
  18891. (save-excursion
  18892. (while (org-goto-sibling 'previous)
  18893. (org-flag-heading nil))))
  18894. (defun org-goto-first-child ()
  18895. "Goto the first child, even if it is invisible.
  18896. Return t when a child was found. Otherwise don't move point and
  18897. return nil."
  18898. (let (level (pos (point)) (re org-outline-regexp-bol))
  18899. (when (condition-case nil (org-back-to-heading t) (error nil))
  18900. (setq level (outline-level))
  18901. (forward-char 1)
  18902. (if (and (re-search-forward re nil t) (> (outline-level) level))
  18903. (progn (goto-char (match-beginning 0)) t)
  18904. (goto-char pos) nil))))
  18905. (defun org-show-hidden-entry ()
  18906. "Show an entry where even the heading is hidden."
  18907. (save-excursion
  18908. (org-show-entry)))
  18909. (defun org-flag-heading (flag &optional entry)
  18910. "Flag the current heading. FLAG non-nil means make invisible.
  18911. When ENTRY is non-nil, show the entire entry."
  18912. (save-excursion
  18913. (org-back-to-heading t)
  18914. ;; Check if we should show the entire entry
  18915. (if entry
  18916. (progn
  18917. (org-show-entry)
  18918. (save-excursion
  18919. (and (outline-next-heading)
  18920. (org-flag-heading nil))))
  18921. (outline-flag-region (max (point-min) (1- (point)))
  18922. (save-excursion (outline-end-of-heading) (point))
  18923. flag))))
  18924. (defun org-get-next-sibling ()
  18925. "Move to next heading of the same level, and return point.
  18926. If there is no such heading, return nil.
  18927. This is like outline-next-sibling, but invisible headings are ok."
  18928. (let ((level (funcall outline-level)))
  18929. (outline-next-heading)
  18930. (while (and (not (eobp)) (> (funcall outline-level) level))
  18931. (outline-next-heading))
  18932. (if (or (eobp) (< (funcall outline-level) level))
  18933. nil
  18934. (point))))
  18935. (defun org-get-last-sibling ()
  18936. "Move to previous heading of the same level, and return point.
  18937. If there is no such heading, return nil."
  18938. (let ((opoint (point))
  18939. (level (funcall outline-level)))
  18940. (outline-previous-heading)
  18941. (when (and (/= (point) opoint) (outline-on-heading-p t))
  18942. (while (and (> (funcall outline-level) level)
  18943. (not (bobp)))
  18944. (outline-previous-heading))
  18945. (if (< (funcall outline-level) level)
  18946. nil
  18947. (point)))))
  18948. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  18949. ;; This contains an exact copy of the original function, but it uses
  18950. ;; `org-back-to-heading', to make it work also in invisible
  18951. ;; trees. And is uses an invisible-OK argument.
  18952. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  18953. ;; Furthermore, when used inside Org, finding the end of a large subtree
  18954. ;; with many children and grandchildren etc, this can be much faster
  18955. ;; than the outline version.
  18956. (org-back-to-heading invisible-OK)
  18957. (let ((first t)
  18958. (level (funcall outline-level)))
  18959. (if (and (eq major-mode 'org-mode) (< level 1000))
  18960. ;; A true heading (not a plain list item), in Org-mode
  18961. ;; This means we can easily find the end by looking
  18962. ;; only for the right number of stars. Using a regexp to do
  18963. ;; this is so much faster than using a Lisp loop.
  18964. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  18965. (forward-char 1)
  18966. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  18967. ;; something else, do it the slow way
  18968. (while (and (not (eobp))
  18969. (or first (> (funcall outline-level) level)))
  18970. (setq first nil)
  18971. (outline-next-heading)))
  18972. (unless to-heading
  18973. (if (memq (preceding-char) '(?\n ?\^M))
  18974. (progn
  18975. ;; Go to end of line before heading
  18976. (forward-char -1)
  18977. (if (memq (preceding-char) '(?\n ?\^M))
  18978. ;; leave blank line before heading
  18979. (forward-char -1))))))
  18980. (point))
  18981. (defadvice outline-end-of-subtree (around prefer-org-version activate compile)
  18982. "Use Org version in org-mode, for dramatic speed-up."
  18983. (if (eq major-mode 'org-mode)
  18984. (progn
  18985. (org-end-of-subtree nil t)
  18986. (unless (eobp) (backward-char 1)))
  18987. ad-do-it))
  18988. (defun org-end-of-meta-data-and-drawers ()
  18989. "Jump to the first text after meta data and drawers in the current entry.
  18990. This will move over empty lines, lines with planning time stamps,
  18991. clocking lines, and drawers."
  18992. (org-back-to-heading t)
  18993. (let ((end (save-excursion (outline-next-heading) (point)))
  18994. (re (concat "\\(" org-drawer-regexp "\\)"
  18995. "\\|" "[ \t]*" org-keyword-time-regexp)))
  18996. (forward-line 1)
  18997. (while (re-search-forward re end t)
  18998. (if (not (match-end 1))
  18999. ;; empty or planning line
  19000. (forward-line 1)
  19001. ;; a drawer, find the end
  19002. (re-search-forward "^[ \t]*:END:" end 'move)
  19003. (forward-line 1)))
  19004. (and (re-search-forward "[^\n]" nil t) (backward-char 1))
  19005. (point)))
  19006. (defun org-forward-same-level (arg &optional invisible-ok)
  19007. "Move forward to the arg'th subheading at same level as this one.
  19008. Stop at the first and last subheadings of a superior heading.
  19009. Normally this only looks at visible headings, but when INVISIBLE-OK is non-nil
  19010. it wil also look at invisible ones."
  19011. (interactive "p")
  19012. (org-back-to-heading invisible-ok)
  19013. (org-on-heading-p)
  19014. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  19015. (re (format "^\\*\\{1,%d\\} " level))
  19016. l)
  19017. (forward-char 1)
  19018. (while (> arg 0)
  19019. (while (and (re-search-forward re nil 'move)
  19020. (setq l (- (match-end 0) (match-beginning 0) 1))
  19021. (= l level)
  19022. (not invisible-ok)
  19023. (progn (backward-char 1) (outline-invisible-p)))
  19024. (if (< l level) (setq arg 1)))
  19025. (setq arg (1- arg)))
  19026. (beginning-of-line 1)))
  19027. (defun org-backward-same-level (arg &optional invisible-ok)
  19028. "Move backward to the arg'th subheading at same level as this one.
  19029. Stop at the first and last subheadings of a superior heading."
  19030. (interactive "p")
  19031. (org-back-to-heading)
  19032. (org-on-heading-p)
  19033. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  19034. (re (format "^\\*\\{1,%d\\} " level))
  19035. l)
  19036. (while (> arg 0)
  19037. (while (and (re-search-backward re nil 'move)
  19038. (setq l (- (match-end 0) (match-beginning 0) 1))
  19039. (= l level)
  19040. (not invisible-ok)
  19041. (outline-invisible-p))
  19042. (if (< l level) (setq arg 1)))
  19043. (setq arg (1- arg)))))
  19044. (defun org-show-subtree ()
  19045. "Show everything after this heading at deeper levels."
  19046. (outline-flag-region
  19047. (point)
  19048. (save-excursion
  19049. (org-end-of-subtree t t))
  19050. nil))
  19051. (defun org-show-entry ()
  19052. "Show the body directly following this heading.
  19053. Show the heading too, if it is currently invisible."
  19054. (interactive)
  19055. (save-excursion
  19056. (condition-case nil
  19057. (progn
  19058. (org-back-to-heading t)
  19059. (outline-flag-region
  19060. (max (point-min) (1- (point)))
  19061. (save-excursion
  19062. (if (re-search-forward
  19063. (concat "[\r\n]\\(" org-outline-regexp "\\)") nil t)
  19064. (match-beginning 1)
  19065. (point-max)))
  19066. nil)
  19067. (org-cycle-hide-drawers 'children))
  19068. (error nil))))
  19069. (defun org-make-options-regexp (kwds &optional extra)
  19070. "Make a regular expression for keyword lines."
  19071. (concat
  19072. "^"
  19073. "#?[ \t]*\\+\\("
  19074. (mapconcat 'regexp-quote kwds "\\|")
  19075. (if extra (concat "\\|" extra))
  19076. "\\):[ \t]*"
  19077. "\\(.*\\)"))
  19078. ;; Make isearch reveal the necessary context
  19079. (defun org-isearch-end ()
  19080. "Reveal context after isearch exits."
  19081. (when isearch-success ; only if search was successful
  19082. (if (featurep 'xemacs)
  19083. ;; Under XEmacs, the hook is run in the correct place,
  19084. ;; we directly show the context.
  19085. (org-show-context 'isearch)
  19086. ;; In Emacs the hook runs *before* restoring the overlays.
  19087. ;; So we have to use a one-time post-command-hook to do this.
  19088. ;; (Emacs 22 has a special variable, see function `org-mode')
  19089. (unless (and (boundp 'isearch-mode-end-hook-quit)
  19090. isearch-mode-end-hook-quit)
  19091. ;; Only when the isearch was not quitted.
  19092. (org-add-hook 'post-command-hook 'org-isearch-post-command
  19093. 'append 'local)))))
  19094. (defun org-isearch-post-command ()
  19095. "Remove self from hook, and show context."
  19096. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  19097. (org-show-context 'isearch))
  19098. ;;;; Integration with and fixes for other packages
  19099. ;;; Imenu support
  19100. (defvar org-imenu-markers nil
  19101. "All markers currently used by Imenu.")
  19102. (make-variable-buffer-local 'org-imenu-markers)
  19103. (defun org-imenu-new-marker (&optional pos)
  19104. "Return a new marker for use by Imenu, and remember the marker."
  19105. (let ((m (make-marker)))
  19106. (move-marker m (or pos (point)))
  19107. (push m org-imenu-markers)
  19108. m))
  19109. (defun org-imenu-get-tree ()
  19110. "Produce the index for Imenu."
  19111. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  19112. (setq org-imenu-markers nil)
  19113. (let* ((n org-imenu-depth)
  19114. (re (concat "^" (org-get-limited-outline-regexp)))
  19115. (subs (make-vector (1+ n) nil))
  19116. (last-level 0)
  19117. m level head)
  19118. (save-excursion
  19119. (save-restriction
  19120. (widen)
  19121. (goto-char (point-max))
  19122. (while (re-search-backward re nil t)
  19123. (setq level (org-reduced-level (funcall outline-level)))
  19124. (when (<= level n)
  19125. (looking-at org-complex-heading-regexp)
  19126. (setq head (org-link-display-format
  19127. (org-match-string-no-properties 4))
  19128. m (org-imenu-new-marker))
  19129. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  19130. (if (>= level last-level)
  19131. (push (cons head m) (aref subs level))
  19132. (push (cons head (aref subs (1+ level))) (aref subs level))
  19133. (loop for i from (1+ level) to n do (aset subs i nil)))
  19134. (setq last-level level)))))
  19135. (aref subs 1)))
  19136. (eval-after-load "imenu"
  19137. '(progn
  19138. (add-hook 'imenu-after-jump-hook
  19139. (lambda ()
  19140. (if (eq major-mode 'org-mode)
  19141. (org-show-context 'org-goto))))))
  19142. (defun org-link-display-format (link)
  19143. "Replace a link with either the description, or the link target
  19144. if no description is present"
  19145. (save-match-data
  19146. (if (string-match org-bracket-link-analytic-regexp link)
  19147. (replace-match (if (match-end 5)
  19148. (match-string 5 link)
  19149. (concat (match-string 1 link)
  19150. (match-string 3 link)))
  19151. nil t link)
  19152. link)))
  19153. (defun org-toggle-link-display ()
  19154. "Toggle the literal or descriptive display of links."
  19155. (interactive)
  19156. (if org-descriptive-links
  19157. (progn (org-remove-from-invisibility-spec '(org-link))
  19158. (org-restart-font-lock)
  19159. (setq org-descriptive-links nil))
  19160. (progn (add-to-invisibility-spec '(org-link))
  19161. (org-restart-font-lock)
  19162. (setq org-descriptive-links t))))
  19163. ;; Speedbar support
  19164. (defvar org-speedbar-restriction-lock-overlay (make-overlay 1 1)
  19165. "Overlay marking the agenda restriction line in speedbar.")
  19166. (overlay-put org-speedbar-restriction-lock-overlay
  19167. 'face 'org-agenda-restriction-lock)
  19168. (overlay-put org-speedbar-restriction-lock-overlay
  19169. 'help-echo "Agendas are currently limited to this item.")
  19170. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  19171. (defun org-speedbar-set-agenda-restriction ()
  19172. "Restrict future agenda commands to the location at point in speedbar.
  19173. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  19174. (interactive)
  19175. (require 'org-agenda)
  19176. (let (p m tp np dir txt)
  19177. (cond
  19178. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  19179. 'org-imenu t))
  19180. (setq m (get-text-property p 'org-imenu-marker))
  19181. (with-current-buffer (marker-buffer m)
  19182. (goto-char m)
  19183. (org-agenda-set-restriction-lock 'subtree)))
  19184. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  19185. 'speedbar-function 'speedbar-find-file))
  19186. (setq tp (previous-single-property-change
  19187. (1+ p) 'speedbar-function)
  19188. np (next-single-property-change
  19189. tp 'speedbar-function)
  19190. dir (speedbar-line-directory)
  19191. txt (buffer-substring-no-properties (or tp (point-min))
  19192. (or np (point-max))))
  19193. (with-current-buffer (find-file-noselect
  19194. (let ((default-directory dir))
  19195. (expand-file-name txt)))
  19196. (unless (eq major-mode 'org-mode)
  19197. (error "Cannot restrict to non-Org-mode file"))
  19198. (org-agenda-set-restriction-lock 'file)))
  19199. (t (error "Don't know how to restrict Org-mode's agenda")))
  19200. (move-overlay org-speedbar-restriction-lock-overlay
  19201. (point-at-bol) (point-at-eol))
  19202. (setq current-prefix-arg nil)
  19203. (org-agenda-maybe-redo)))
  19204. (eval-after-load "speedbar"
  19205. '(progn
  19206. (speedbar-add-supported-extension ".org")
  19207. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  19208. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  19209. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  19210. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  19211. (add-hook 'speedbar-visiting-tag-hook
  19212. (lambda () (and (eq major-mode 'org-mode) (org-show-context 'org-goto))))))
  19213. ;;; Fixes and Hacks for problems with other packages
  19214. ;; Make flyspell not check words in links, to not mess up our keymap
  19215. (defun org-mode-flyspell-verify ()
  19216. "Don't let flyspell put overlays at active buttons, or on
  19217. {todo,all-time,additional-option-like}-keywords."
  19218. (let ((pos (max (1- (point)) (point-min)))
  19219. (word (thing-at-point 'word)))
  19220. (and (not (get-text-property pos 'keymap))
  19221. (not (get-text-property pos 'org-no-flyspell))
  19222. (not (member word org-todo-keywords-1))
  19223. (not (member word org-all-time-keywords))
  19224. (not (member word org-additional-option-like-keywords)))))
  19225. (defun org-remove-flyspell-overlays-in (beg end)
  19226. "Remove flyspell overlays in region."
  19227. (and (org-bound-and-true-p flyspell-mode)
  19228. (fboundp 'flyspell-delete-region-overlays)
  19229. (flyspell-delete-region-overlays beg end))
  19230. (add-text-properties beg end '(org-no-flyspell t)))
  19231. ;; Make `bookmark-jump' shows the jump location if it was hidden.
  19232. (eval-after-load "bookmark"
  19233. '(if (boundp 'bookmark-after-jump-hook)
  19234. ;; We can use the hook
  19235. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  19236. ;; Hook not available, use advice
  19237. (defadvice bookmark-jump (after org-make-visible activate)
  19238. "Make the position visible."
  19239. (org-bookmark-jump-unhide))))
  19240. ;; Make sure saveplace shows the location if it was hidden
  19241. (eval-after-load "saveplace"
  19242. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  19243. "Make the position visible."
  19244. (org-bookmark-jump-unhide)))
  19245. ;; Make sure ecb shows the location if it was hidden
  19246. (eval-after-load "ecb"
  19247. '(defadvice ecb-method-clicked (after esf/org-show-context activate)
  19248. "Make hierarchy visible when jumping into location from ECB tree buffer."
  19249. (if (eq major-mode 'org-mode)
  19250. (org-show-context))))
  19251. (defun org-bookmark-jump-unhide ()
  19252. "Unhide the current position, to show the bookmark location."
  19253. (and (eq major-mode 'org-mode)
  19254. (or (outline-invisible-p)
  19255. (save-excursion (goto-char (max (point-min) (1- (point))))
  19256. (outline-invisible-p)))
  19257. (org-show-context 'bookmark-jump)))
  19258. ;; Make session.el ignore our circular variable
  19259. (eval-after-load "session"
  19260. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  19261. ;;;; Experimental code
  19262. (defun org-closed-in-range ()
  19263. "Sparse tree of items closed in a certain time range.
  19264. Still experimental, may disappear in the future."
  19265. (interactive)
  19266. ;; Get the time interval from the user.
  19267. (let* ((time1 (org-float-time
  19268. (org-read-date nil 'to-time nil "Starting date: ")))
  19269. (time2 (org-float-time
  19270. (org-read-date nil 'to-time nil "End date:")))
  19271. ;; callback function
  19272. (callback (lambda ()
  19273. (let ((time
  19274. (org-float-time
  19275. (apply 'encode-time
  19276. (org-parse-time-string
  19277. (match-string 1))))))
  19278. ;; check if time in interval
  19279. (and (>= time time1) (<= time time2))))))
  19280. ;; make tree, check each match with the callback
  19281. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  19282. ;;;; Finish up
  19283. (provide 'org)
  19284. (run-hooks 'org-load-hook)
  19285. ;;; org.el ends here