org.el 804 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046170471704817049170501705117052170531705417055170561705717058170591706017061170621706317064170651706617067170681706917070170711707217073170741707517076170771707817079170801708117082170831708417085170861708717088170891709017091170921709317094170951709617097170981709917100171011710217103171041710517106171071710817109171101711117112171131711417115171161711717118171191712017121171221712317124171251712617127171281712917130171311713217133171341713517136171371713817139171401714117142171431714417145171461714717148171491715017151171521715317154171551715617157171581715917160171611716217163171641716517166171671716817169171701717117172171731717417175171761717717178171791718017181171821718317184171851718617187171881718917190171911719217193171941719517196171971719817199172001720117202172031720417205172061720717208172091721017211172121721317214172151721617217172181721917220172211722217223172241722517226172271722817229172301723117232172331723417235172361723717238172391724017241172421724317244172451724617247172481724917250172511725217253172541725517256172571725817259172601726117262172631726417265172661726717268172691727017271172721727317274172751727617277172781727917280172811728217283172841728517286172871728817289172901729117292172931729417295172961729717298172991730017301173021730317304173051730617307173081730917310173111731217313173141731517316173171731817319173201732117322173231732417325173261732717328173291733017331173321733317334173351733617337173381733917340173411734217343173441734517346173471734817349173501735117352173531735417355173561735717358173591736017361173621736317364173651736617367173681736917370173711737217373173741737517376173771737817379173801738117382173831738417385173861738717388173891739017391173921739317394173951739617397173981739917400174011740217403174041740517406174071740817409174101741117412174131741417415174161741717418174191742017421174221742317424174251742617427174281742917430174311743217433174341743517436174371743817439174401744117442174431744417445174461744717448174491745017451174521745317454174551745617457174581745917460174611746217463174641746517466174671746817469174701747117472174731747417475174761747717478174791748017481174821748317484174851748617487174881748917490174911749217493174941749517496174971749817499175001750117502175031750417505175061750717508175091751017511175121751317514175151751617517175181751917520175211752217523175241752517526175271752817529175301753117532175331753417535175361753717538175391754017541175421754317544175451754617547175481754917550175511755217553175541755517556175571755817559175601756117562175631756417565175661756717568175691757017571175721757317574175751757617577175781757917580175811758217583175841758517586175871758817589175901759117592175931759417595175961759717598175991760017601176021760317604176051760617607176081760917610176111761217613176141761517616176171761817619176201762117622176231762417625176261762717628176291763017631176321763317634176351763617637176381763917640176411764217643176441764517646176471764817649176501765117652176531765417655176561765717658176591766017661176621766317664176651766617667176681766917670176711767217673176741767517676176771767817679176801768117682176831768417685176861768717688176891769017691176921769317694176951769617697176981769917700177011770217703177041770517706177071770817709177101771117712177131771417715177161771717718177191772017721177221772317724177251772617727177281772917730177311773217733177341773517736177371773817739177401774117742177431774417745177461774717748177491775017751177521775317754177551775617757177581775917760177611776217763177641776517766177671776817769177701777117772177731777417775177761777717778177791778017781177821778317784177851778617787177881778917790177911779217793177941779517796177971779817799178001780117802178031780417805178061780717808178091781017811178121781317814178151781617817178181781917820178211782217823178241782517826178271782817829178301783117832178331783417835178361783717838178391784017841178421784317844178451784617847178481784917850178511785217853178541785517856178571785817859178601786117862178631786417865178661786717868178691787017871178721787317874178751787617877178781787917880178811788217883178841788517886178871788817889178901789117892178931789417895178961789717898178991790017901179021790317904179051790617907179081790917910179111791217913179141791517916179171791817919179201792117922179231792417925179261792717928179291793017931179321793317934179351793617937179381793917940179411794217943179441794517946179471794817949179501795117952179531795417955179561795717958179591796017961179621796317964179651796617967179681796917970179711797217973179741797517976179771797817979179801798117982179831798417985179861798717988179891799017991179921799317994179951799617997179981799918000180011800218003180041800518006180071800818009180101801118012180131801418015180161801718018180191802018021180221802318024180251802618027180281802918030180311803218033180341803518036180371803818039180401804118042180431804418045180461804718048180491805018051180521805318054180551805618057180581805918060180611806218063180641806518066180671806818069180701807118072180731807418075180761807718078180791808018081180821808318084180851808618087180881808918090180911809218093180941809518096180971809818099181001810118102181031810418105181061810718108181091811018111181121811318114181151811618117181181811918120181211812218123181241812518126181271812818129181301813118132181331813418135181361813718138181391814018141181421814318144181451814618147181481814918150181511815218153181541815518156181571815818159181601816118162181631816418165181661816718168181691817018171181721817318174181751817618177181781817918180181811818218183181841818518186181871818818189181901819118192181931819418195181961819718198181991820018201182021820318204182051820618207182081820918210182111821218213182141821518216182171821818219182201822118222182231822418225182261822718228182291823018231182321823318234182351823618237182381823918240182411824218243182441824518246182471824818249182501825118252182531825418255182561825718258182591826018261182621826318264182651826618267182681826918270182711827218273182741827518276182771827818279182801828118282182831828418285182861828718288182891829018291182921829318294182951829618297182981829918300183011830218303183041830518306183071830818309183101831118312183131831418315183161831718318183191832018321183221832318324183251832618327183281832918330183311833218333183341833518336183371833818339183401834118342183431834418345183461834718348183491835018351183521835318354183551835618357183581835918360183611836218363183641836518366183671836818369183701837118372183731837418375183761837718378183791838018381183821838318384183851838618387183881838918390183911839218393183941839518396183971839818399184001840118402184031840418405184061840718408184091841018411184121841318414184151841618417184181841918420184211842218423184241842518426184271842818429184301843118432184331843418435184361843718438184391844018441184421844318444184451844618447184481844918450184511845218453184541845518456184571845818459184601846118462184631846418465184661846718468184691847018471184721847318474184751847618477184781847918480184811848218483184841848518486184871848818489184901849118492184931849418495184961849718498184991850018501185021850318504185051850618507185081850918510185111851218513185141851518516185171851818519185201852118522185231852418525185261852718528185291853018531185321853318534185351853618537185381853918540185411854218543185441854518546185471854818549185501855118552185531855418555185561855718558185591856018561185621856318564185651856618567185681856918570185711857218573185741857518576185771857818579185801858118582185831858418585185861858718588185891859018591185921859318594185951859618597185981859918600186011860218603186041860518606186071860818609186101861118612186131861418615186161861718618186191862018621186221862318624186251862618627186281862918630186311863218633186341863518636186371863818639186401864118642186431864418645186461864718648186491865018651186521865318654186551865618657186581865918660186611866218663186641866518666186671866818669186701867118672186731867418675186761867718678186791868018681186821868318684186851868618687186881868918690186911869218693186941869518696186971869818699187001870118702187031870418705187061870718708187091871018711187121871318714187151871618717187181871918720187211872218723187241872518726187271872818729187301873118732187331873418735187361873718738187391874018741187421874318744187451874618747187481874918750187511875218753187541875518756187571875818759187601876118762187631876418765187661876718768187691877018771187721877318774187751877618777187781877918780187811878218783187841878518786187871878818789187901879118792187931879418795187961879718798187991880018801188021880318804188051880618807188081880918810188111881218813188141881518816188171881818819188201882118822188231882418825188261882718828188291883018831188321883318834188351883618837188381883918840188411884218843188441884518846188471884818849188501885118852188531885418855188561885718858188591886018861188621886318864188651886618867188681886918870188711887218873188741887518876188771887818879188801888118882188831888418885188861888718888188891889018891188921889318894188951889618897188981889918900189011890218903189041890518906189071890818909189101891118912189131891418915189161891718918189191892018921189221892318924189251892618927189281892918930189311893218933189341893518936189371893818939189401894118942189431894418945189461894718948189491895018951189521895318954189551895618957189581895918960189611896218963189641896518966189671896818969189701897118972189731897418975189761897718978189791898018981189821898318984189851898618987189881898918990189911899218993189941899518996189971899818999190001900119002190031900419005190061900719008190091901019011190121901319014190151901619017190181901919020190211902219023190241902519026190271902819029190301903119032190331903419035190361903719038190391904019041190421904319044190451904619047190481904919050190511905219053190541905519056190571905819059190601906119062190631906419065190661906719068190691907019071190721907319074190751907619077190781907919080190811908219083190841908519086190871908819089190901909119092190931909419095190961909719098190991910019101191021910319104191051910619107191081910919110191111911219113191141911519116191171911819119191201912119122191231912419125191261912719128191291913019131191321913319134191351913619137191381913919140191411914219143191441914519146191471914819149191501915119152191531915419155191561915719158191591916019161191621916319164191651916619167191681916919170191711917219173191741917519176191771917819179191801918119182191831918419185191861918719188191891919019191191921919319194191951919619197191981919919200192011920219203192041920519206192071920819209192101921119212192131921419215192161921719218192191922019221192221922319224192251922619227192281922919230192311923219233192341923519236192371923819239192401924119242192431924419245192461924719248192491925019251192521925319254192551925619257192581925919260192611926219263192641926519266192671926819269192701927119272192731927419275192761927719278192791928019281192821928319284192851928619287192881928919290192911929219293192941929519296192971929819299193001930119302193031930419305193061930719308193091931019311193121931319314193151931619317193181931919320193211932219323193241932519326193271932819329193301933119332193331933419335193361933719338193391934019341193421934319344193451934619347193481934919350193511935219353193541935519356193571935819359193601936119362193631936419365193661936719368193691937019371193721937319374193751937619377193781937919380193811938219383193841938519386193871938819389193901939119392193931939419395193961939719398193991940019401194021940319404194051940619407194081940919410194111941219413194141941519416194171941819419194201942119422194231942419425194261942719428194291943019431194321943319434194351943619437194381943919440194411944219443194441944519446194471944819449194501945119452194531945419455194561945719458194591946019461194621946319464194651946619467194681946919470194711947219473194741947519476194771947819479194801948119482194831948419485194861948719488194891949019491194921949319494194951949619497194981949919500195011950219503195041950519506195071950819509195101951119512195131951419515195161951719518195191952019521195221952319524195251952619527195281952919530195311953219533195341953519536195371953819539195401954119542195431954419545195461954719548195491955019551195521955319554195551955619557195581955919560195611956219563195641956519566195671956819569195701957119572195731957419575195761957719578195791958019581195821958319584195851958619587195881958919590195911959219593195941959519596195971959819599196001960119602196031960419605196061960719608196091961019611196121961319614196151961619617196181961919620196211962219623196241962519626196271962819629196301963119632196331963419635196361963719638196391964019641196421964319644196451964619647196481964919650196511965219653196541965519656196571965819659196601966119662196631966419665196661966719668196691967019671196721967319674196751967619677196781967919680196811968219683196841968519686196871968819689196901969119692196931969419695196961969719698196991970019701197021970319704197051970619707197081970919710197111971219713197141971519716197171971819719197201972119722197231972419725197261972719728197291973019731197321973319734197351973619737197381973919740197411974219743197441974519746197471974819749197501975119752197531975419755197561975719758197591976019761197621976319764197651976619767197681976919770197711977219773197741977519776197771977819779197801978119782197831978419785197861978719788197891979019791197921979319794197951979619797197981979919800198011980219803198041980519806198071980819809198101981119812198131981419815198161981719818198191982019821198221982319824198251982619827198281982919830198311983219833198341983519836198371983819839198401984119842198431984419845198461984719848198491985019851198521985319854198551985619857198581985919860198611986219863198641986519866198671986819869198701987119872198731987419875198761987719878198791988019881198821988319884198851988619887198881988919890198911989219893198941989519896198971989819899199001990119902199031990419905199061990719908199091991019911199121991319914199151991619917199181991919920199211992219923199241992519926199271992819929199301993119932199331993419935199361993719938199391994019941199421994319944199451994619947199481994919950199511995219953199541995519956199571995819959199601996119962199631996419965199661996719968199691997019971199721997319974199751997619977199781997919980199811998219983199841998519986199871998819989199901999119992199931999419995199961999719998199992000020001200022000320004200052000620007200082000920010200112001220013200142001520016200172001820019200202002120022200232002420025200262002720028200292003020031200322003320034200352003620037200382003920040200412004220043200442004520046200472004820049200502005120052200532005420055200562005720058200592006020061200622006320064200652006620067200682006920070200712007220073200742007520076200772007820079200802008120082200832008420085200862008720088200892009020091200922009320094200952009620097200982009920100201012010220103201042010520106201072010820109201102011120112201132011420115201162011720118201192012020121201222012320124201252012620127201282012920130201312013220133201342013520136201372013820139201402014120142201432014420145201462014720148201492015020151201522015320154201552015620157201582015920160201612016220163201642016520166201672016820169201702017120172201732017420175201762017720178201792018020181201822018320184201852018620187201882018920190201912019220193201942019520196201972019820199202002020120202202032020420205202062020720208202092021020211202122021320214202152021620217202182021920220202212022220223202242022520226202272022820229202302023120232202332023420235202362023720238202392024020241202422024320244202452024620247202482024920250202512025220253202542025520256202572025820259202602026120262202632026420265202662026720268202692027020271202722027320274202752027620277202782027920280202812028220283202842028520286202872028820289202902029120292202932029420295202962029720298202992030020301203022030320304203052030620307203082030920310203112031220313203142031520316203172031820319203202032120322203232032420325203262032720328203292033020331203322033320334203352033620337203382033920340203412034220343203442034520346203472034820349203502035120352203532035420355203562035720358203592036020361203622036320364203652036620367203682036920370203712037220373203742037520376203772037820379203802038120382203832038420385203862038720388203892039020391203922039320394203952039620397203982039920400204012040220403204042040520406204072040820409204102041120412204132041420415204162041720418204192042020421204222042320424204252042620427204282042920430204312043220433204342043520436204372043820439204402044120442204432044420445204462044720448204492045020451204522045320454204552045620457204582045920460204612046220463204642046520466204672046820469204702047120472204732047420475204762047720478204792048020481204822048320484204852048620487204882048920490204912049220493204942049520496204972049820499205002050120502205032050420505205062050720508205092051020511205122051320514205152051620517205182051920520205212052220523205242052520526205272052820529205302053120532205332053420535205362053720538205392054020541205422054320544205452054620547205482054920550205512055220553205542055520556205572055820559205602056120562205632056420565205662056720568205692057020571205722057320574205752057620577205782057920580205812058220583205842058520586205872058820589205902059120592205932059420595205962059720598205992060020601206022060320604206052060620607206082060920610206112061220613206142061520616206172061820619206202062120622206232062420625206262062720628206292063020631206322063320634206352063620637206382063920640206412064220643206442064520646206472064820649206502065120652206532065420655206562065720658206592066020661206622066320664206652066620667206682066920670206712067220673206742067520676206772067820679206802068120682206832068420685206862068720688206892069020691206922069320694206952069620697206982069920700207012070220703207042070520706207072070820709207102071120712207132071420715207162071720718207192072020721207222072320724207252072620727207282072920730207312073220733207342073520736207372073820739207402074120742207432074420745207462074720748207492075020751207522075320754207552075620757207582075920760207612076220763207642076520766207672076820769207702077120772207732077420775207762077720778207792078020781207822078320784207852078620787207882078920790207912079220793207942079520796207972079820799208002080120802208032080420805208062080720808208092081020811208122081320814208152081620817208182081920820208212082220823208242082520826208272082820829208302083120832208332083420835208362083720838208392084020841208422084320844208452084620847208482084920850208512085220853208542085520856208572085820859208602086120862208632086420865208662086720868208692087020871208722087320874208752087620877208782087920880208812088220883208842088520886208872088820889208902089120892208932089420895208962089720898208992090020901209022090320904209052090620907209082090920910209112091220913209142091520916209172091820919209202092120922209232092420925209262092720928209292093020931209322093320934209352093620937209382093920940209412094220943209442094520946209472094820949209502095120952209532095420955209562095720958209592096020961209622096320964209652096620967209682096920970209712097220973209742097520976209772097820979209802098120982209832098420985209862098720988209892099020991209922099320994209952099620997209982099921000210012100221003210042100521006210072100821009210102101121012210132101421015210162101721018210192102021021210222102321024210252102621027210282102921030210312103221033210342103521036210372103821039210402104121042210432104421045210462104721048210492105021051210522105321054210552105621057210582105921060210612106221063210642106521066210672106821069210702107121072210732107421075210762107721078210792108021081210822108321084210852108621087210882108921090210912109221093210942109521096210972109821099211002110121102211032110421105211062110721108211092111021111211122111321114211152111621117211182111921120211212112221123211242112521126211272112821129211302113121132211332113421135211362113721138211392114021141211422114321144211452114621147211482114921150211512115221153211542115521156211572115821159211602116121162211632116421165211662116721168211692117021171211722117321174211752117621177211782117921180211812118221183211842118521186211872118821189211902119121192211932119421195211962119721198211992120021201212022120321204212052120621207212082120921210212112121221213212142121521216212172121821219212202122121222212232122421225212262122721228212292123021231212322123321234212352123621237212382123921240212412124221243212442124521246212472124821249212502125121252212532125421255212562125721258212592126021261212622126321264212652126621267212682126921270212712127221273212742127521276212772127821279212802128121282212832128421285212862128721288212892129021291212922129321294212952129621297212982129921300213012130221303213042130521306213072130821309213102131121312213132131421315213162131721318213192132021321213222132321324213252132621327213282132921330213312133221333213342133521336213372133821339213402134121342213432134421345213462134721348213492135021351213522135321354213552135621357213582135921360213612136221363213642136521366213672136821369213702137121372213732137421375213762137721378213792138021381213822138321384213852138621387213882138921390213912139221393213942139521396213972139821399
  1. ;;; org.el --- Outline-based notes management and organizer
  2. ;; Carstens outline-mode for keeping track of everything.
  3. ;; Copyright (C) 2004-2012 Free Software Foundation, Inc.
  4. ;;
  5. ;; Author: Carsten Dominik <carsten at orgmode dot org>
  6. ;; Maintainer: Bastien Guerry <bzg at gnu dot org>
  7. ;; Keywords: outlines, hypermedia, calendar, wp
  8. ;; Homepage: http://orgmode.org
  9. ;; Version: 7.8.03
  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. :version "24.1"
  142. :type '(alist :tag "Babel Languages"
  143. :key-type
  144. (choice
  145. (const :tag "Awk" awk)
  146. (const :tag "C" C)
  147. (const :tag "R" R)
  148. (const :tag "Asymptote" asymptote)
  149. (const :tag "Calc" calc)
  150. (const :tag "Clojure" clojure)
  151. (const :tag "CSS" css)
  152. (const :tag "Ditaa" ditaa)
  153. (const :tag "Dot" dot)
  154. (const :tag "Emacs Lisp" emacs-lisp)
  155. (const :tag "Fortran" fortran)
  156. (const :tag "Gnuplot" gnuplot)
  157. (const :tag "Haskell" haskell)
  158. (const :tag "Java" java)
  159. (const :tag "Javascript" js)
  160. (const :tag "LaTeX" latex)
  161. (const :tag "Ledger" ledger)
  162. (const :tag "Lilypond" lilypond)
  163. (const :tag "Maxima" maxima)
  164. (const :tag "Matlab" matlab)
  165. (const :tag "Mscgen" mscgen)
  166. (const :tag "Ocaml" ocaml)
  167. (const :tag "Octave" octave)
  168. (const :tag "Org" org)
  169. (const :tag "Perl" perl)
  170. (const :tag "Pico Lisp" picolisp)
  171. (const :tag "PlantUML" plantuml)
  172. (const :tag "Python" python)
  173. (const :tag "Ruby" ruby)
  174. (const :tag "Sass" sass)
  175. (const :tag "Scheme" scheme)
  176. (const :tag "Screen" screen)
  177. (const :tag "Shell Script" sh)
  178. (const :tag "Shen" shen)
  179. (const :tag "Sql" sql)
  180. (const :tag "Sqlite" sqlite))
  181. :value-type (boolean :tag "Activate" :value t)))
  182. ;;;; Customization variables
  183. (defcustom org-clone-delete-id nil
  184. "Remove ID property of clones of a subtree.
  185. When non-nil, clones of a subtree don't inherit the ID property.
  186. Otherwise they inherit the ID property with a new unique
  187. identifier."
  188. :type 'boolean
  189. :version "24.1"
  190. :group 'org-id)
  191. ;;; Version
  192. (defconst org-version "7.8.03"
  193. "The version number of the file org.el.")
  194. (defun org-version (&optional here)
  195. "Show the org-mode version in the echo area.
  196. With prefix arg HERE, insert it at point."
  197. (interactive "P")
  198. (let* ((origin default-directory)
  199. (version org-version)
  200. (git-version)
  201. (dir (concat (file-name-directory (locate-library "org")) "../" )))
  202. (when (and (file-exists-p (expand-file-name ".git" dir))
  203. (executable-find "git"))
  204. (unwind-protect
  205. (progn
  206. (cd dir)
  207. (when (eql 0 (shell-command "git describe --abbrev=4 HEAD"))
  208. (with-current-buffer "*Shell Command Output*"
  209. (goto-char (point-min))
  210. (setq git-version (buffer-substring (point) (point-at-eol))))
  211. (subst-char-in-string ?- ?. git-version t)
  212. (when (string-match "\\S-"
  213. (shell-command-to-string
  214. "git diff-index --name-only HEAD --"))
  215. (setq git-version (concat git-version ".dirty")))
  216. (setq version (concat version " (" git-version ")"))))
  217. (cd origin)))
  218. (setq version (format "Org-mode version %s" version))
  219. (if here (insert version))
  220. (message version)))
  221. ;;; Compatibility constants
  222. ;;; The custom variables
  223. (defgroup org nil
  224. "Outline-based notes management and organizer."
  225. :tag "Org"
  226. :group 'outlines
  227. :group 'calendar)
  228. (defcustom org-mode-hook nil
  229. "Mode hook for Org-mode, run after the mode was turned on."
  230. :group 'org
  231. :type 'hook)
  232. (defcustom org-load-hook nil
  233. "Hook that is run after org.el has been loaded."
  234. :group 'org
  235. :type 'hook)
  236. (defcustom org-log-buffer-setup-hook nil
  237. "Hook that is run after an Org log buffer is created."
  238. :group 'org
  239. :version "24.1"
  240. :type 'hook)
  241. (defvar org-modules) ; defined below
  242. (defvar org-modules-loaded nil
  243. "Have the modules been loaded already?")
  244. (defun org-load-modules-maybe (&optional force)
  245. "Load all extensions listed in `org-modules'."
  246. (when (or force (not org-modules-loaded))
  247. (mapc (lambda (ext)
  248. (condition-case nil (require ext)
  249. (error (message "Problems while trying to load feature `%s'" ext))))
  250. org-modules)
  251. (setq org-modules-loaded t)))
  252. (defun org-set-modules (var value)
  253. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  254. (set var value)
  255. (when (featurep 'org)
  256. (org-load-modules-maybe 'force)))
  257. (when (org-bound-and-true-p org-modules)
  258. (let ((a (member 'org-infojs org-modules)))
  259. (and a (setcar a 'org-jsinfo))))
  260. (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)
  261. "Modules that should always be loaded together with org.el.
  262. If a description starts with <C>, the file is not part of Emacs
  263. and loading it will require that you have downloaded and properly installed
  264. the org-mode distribution.
  265. You can also use this system to load external packages (i.e. neither Org
  266. core modules, nor modules from the CONTRIB directory). Just add symbols
  267. to the end of the list. If the package is called org-xyz.el, then you need
  268. to add the symbol `xyz', and the package must have a call to
  269. (provide 'org-xyz)"
  270. :group 'org
  271. :set 'org-set-modules
  272. :type
  273. '(set :greedy t
  274. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  275. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  276. (const :tag " crypt: Encryption of subtrees" org-crypt)
  277. (const :tag " ctags: Access to Emacs tags with links" org-ctags)
  278. (const :tag " docview: Links to doc-view buffers" org-docview)
  279. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  280. (const :tag " id: Global IDs for identifying entries" org-id)
  281. (const :tag " info: Links to Info nodes" org-info)
  282. (const :tag " jsinfo: Set up Sebastian Rose's JavaScript org-info.js" org-jsinfo)
  283. (const :tag " habit: Track your consistency with habits" org-habit)
  284. (const :tag " inlinetask: Tasks independent of outline hierarchy" org-inlinetask)
  285. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  286. (const :tag " mac-message: Links to messages in Apple Mail" org-mac-message)
  287. (const :tag " mew Links to Mew folders/messages" org-mew)
  288. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  289. (const :tag " protocol: Intercept calls from emacsclient" org-protocol)
  290. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  291. (const :tag " special-blocks: Turn blocks into LaTeX envs and HTML divs" org-special-blocks)
  292. (const :tag " vm: Links to VM folders/messages" org-vm)
  293. (const :tag " wl: Links to Wanderlust folders/messages" org-wl)
  294. (const :tag " w3m: Special cut/paste from w3m to Org-mode." org-w3m)
  295. (const :tag " mouse: Additional mouse support" org-mouse)
  296. (const :tag " TaskJuggler: Export tasks to a TaskJuggler project" org-taskjuggler)
  297. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  298. (const :tag "C bookmark: Org-mode links to bookmarks" org-bookmark)
  299. (const :tag "C checklist: Extra functions for checklists in repeated tasks" org-checklist)
  300. (const :tag "C choose: Use TODO keywords to mark decisions states" org-choose)
  301. (const :tag "C collector: Collect properties into tables" org-collector)
  302. (const :tag "C depend: TODO dependencies for Org-mode\n\t\t\t(PARTIALLY OBSOLETE, see built-in dependency support))" org-depend)
  303. (const :tag "C drill: Flashcards and spaced repetition for Org-mode" org-drill)
  304. (const :tag "C elisp-symbol: Org-mode links to emacs-lisp symbols" org-elisp-symbol)
  305. (const :tag "C eshell Support for links to working directories in eshell" org-eshell)
  306. (const :tag "C eval: Include command output as text" org-eval)
  307. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  308. (const :tag "C expiry: Expiry mechanism for Org-mode entries" org-expiry)
  309. (const :tag "C exp-bibtex: Export citations using BibTeX" org-exp-bibtex)
  310. (const :tag "C git-link: Provide org links to specific file version" org-git-link)
  311. (const :tag "C interactive-query: Interactive modification of tags query\n\t\t\t(PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query)
  312. (const :tag "C invoice: Help manage client invoices in Org-mode" org-invoice)
  313. (const :tag "C jira: Add a jira:ticket protocol to Org-mode" org-jira)
  314. (const :tag "C learn: SuperMemo's incremental learning algorithm" org-learn)
  315. (const :tag "C mairix: Hook mairix search into Org-mode for different MUAs" org-mairix)
  316. (const :tag "C notmuch: Provide org links to notmuch searches or messages" org-notmuch)
  317. (const :tag "C mac-iCal Imports events from iCal.app to the Emacs diary" org-mac-iCal)
  318. (const :tag "C mac-link-grabber Grab links and URLs from various Mac applications" org-mac-link-grabber)
  319. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  320. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  321. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  322. (const :tag "C registry: A registry for Org-mode links" org-registry)
  323. (const :tag "C org2rem: Convert org appointments into reminders" org2rem)
  324. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  325. (const :tag "C secretary: Team management with org-mode" org-secretary)
  326. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  327. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  328. (const :tag "C track: Keep up with Org-mode development" org-track)
  329. (const :tag "C velocity Something like Notational Velocity for Org" org-velocity)
  330. (const :tag "C wikinodes: CamelCase wiki-like links" org-wikinodes)
  331. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  332. (defcustom org-support-shift-select nil
  333. "Non-nil means make shift-cursor commands select text when possible.
  334. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys
  335. start selecting a region, or enlarge regions started in this way.
  336. In Org-mode, in special contexts, these same keys are used for
  337. other purposes, important enough to compete with shift selection.
  338. Org tries to balance these needs by supporting `shift-select-mode'
  339. outside these special contexts, under control of this variable.
  340. The default of this variable is nil, to avoid confusing behavior. Shifted
  341. cursor keys will then execute Org commands in the following contexts:
  342. - on a headline, changing TODO state (left/right) and priority (up/down)
  343. - on a time stamp, changing the time
  344. - in a plain list item, changing the bullet type
  345. - in a property definition line, switching between allowed values
  346. - in the BEGIN line of a clock table (changing the time block).
  347. Outside these contexts, the commands will throw an error.
  348. When this variable is t and the cursor is not in a special
  349. context, Org-mode will support shift-selection for making and
  350. enlarging regions. To make this more effective, the bullet
  351. cycling will no longer happen anywhere in an item line, but only
  352. if the cursor is exactly on the bullet.
  353. If you set this variable to the symbol `always', then the keys
  354. will not be special in headlines, property lines, and item lines,
  355. to make shift selection work there as well. If this is what you
  356. want, you can use the following alternative commands: `C-c C-t'
  357. and `C-c ,' to change TODO state and priority, `C-u C-u C-c C-t'
  358. can be used to switch TODO sets, `C-c -' to cycle item bullet
  359. types, and properties can be edited by hand or in column view.
  360. However, when the cursor is on a timestamp, shift-cursor commands
  361. will still edit the time stamp - this is just too good to give up.
  362. XEmacs user should have this variable set to nil, because
  363. `shift-select-mode' is in Emacs 23 or later only."
  364. :group 'org
  365. :type '(choice
  366. (const :tag "Never" nil)
  367. (const :tag "When outside special context" t)
  368. (const :tag "Everywhere except timestamps" always)))
  369. (defcustom org-loop-over-headlines-in-active-region nil
  370. "Shall some commands act upon headlines in the active region?
  371. When set to `t', some commands will be performed in all headlines
  372. within the active region.
  373. When set to `start-level', some commands will be performed in all
  374. headlines within the active region, provided that these headlines
  375. are of the same level than the first one.
  376. When set to a string, those commands will be performed on the
  377. matching headlines within the active region. Such string must be
  378. a tags/property/todo match as it is used in the agenda tags view.
  379. The list of commands is: `org-schedule', `org-deadline',
  380. `org-todo', `org-archive-subtree', `org-archive-set-tag' and
  381. `org-archive-to-archive-sibling'. The archiving commands skip
  382. already archived entries."
  383. :type '(choice (const :tag "Don't loop" nil)
  384. (const :tag "All headlines in active region" t)
  385. (const :tag "In active region, headlines at the same level than the first one" 'start-level)
  386. (string :tag "Tags/Property/Todo matcher"))
  387. :version "24.1"
  388. :group 'org-todo
  389. :group 'org-archive)
  390. (defgroup org-startup nil
  391. "Options concerning startup of Org-mode."
  392. :tag "Org Startup"
  393. :group 'org)
  394. (defcustom org-startup-folded t
  395. "Non-nil means entering Org-mode will switch to OVERVIEW.
  396. This can also be configured on a per-file basis by adding one of
  397. the following lines anywhere in the buffer:
  398. #+STARTUP: fold (or `overview', this is equivalent)
  399. #+STARTUP: nofold (or `showall', this is equivalent)
  400. #+STARTUP: content
  401. #+STARTUP: showeverything"
  402. :group 'org-startup
  403. :type '(choice
  404. (const :tag "nofold: show all" nil)
  405. (const :tag "fold: overview" t)
  406. (const :tag "content: all headlines" content)
  407. (const :tag "show everything, even drawers" showeverything)))
  408. (defcustom org-startup-truncated t
  409. "Non-nil means entering Org-mode will set `truncate-lines'.
  410. This is useful since some lines containing links can be very long and
  411. uninteresting. Also tables look terrible when wrapped."
  412. :group 'org-startup
  413. :type 'boolean)
  414. (defcustom org-startup-indented nil
  415. "Non-nil means turn on `org-indent-mode' on startup.
  416. This can also be configured on a per-file basis by adding one of
  417. the following lines anywhere in the buffer:
  418. #+STARTUP: indent
  419. #+STARTUP: noindent"
  420. :group 'org-structure
  421. :type '(choice
  422. (const :tag "Not" nil)
  423. (const :tag "Globally (slow on startup in large files)" t)))
  424. (defcustom org-use-sub-superscripts t
  425. "Non-nil means interpret \"_\" and \"^\" for export.
  426. When this option is turned on, you can use TeX-like syntax for sub- and
  427. superscripts. Several characters after \"_\" or \"^\" will be
  428. considered as a single item - so grouping with {} is normally not
  429. needed. For example, the following things will be parsed as single
  430. sub- or superscripts.
  431. 10^24 or 10^tau several digits will be considered 1 item.
  432. 10^-12 or 10^-tau a leading sign with digits or a word
  433. x^2-y^3 will be read as x^2 - y^3, because items are
  434. terminated by almost any nonword/nondigit char.
  435. x_{i^2} or x^(2-i) braces or parenthesis do grouping.
  436. Still, ambiguity is possible - so when in doubt use {} to enclose the
  437. sub/superscript. If you set this variable to the symbol `{}',
  438. the braces are *required* in order to trigger interpretations as
  439. sub/superscript. This can be helpful in documents that need \"_\"
  440. frequently in plain text.
  441. Not all export backends support this, but HTML does.
  442. This option can also be set with the +OPTIONS line, e.g. \"^:nil\"."
  443. :group 'org-startup
  444. :group 'org-export-translation
  445. :version "24.1"
  446. :type '(choice
  447. (const :tag "Always interpret" t)
  448. (const :tag "Only with braces" {})
  449. (const :tag "Never interpret" nil)))
  450. (if (fboundp 'defvaralias)
  451. (defvaralias 'org-export-with-sub-superscripts 'org-use-sub-superscripts))
  452. (defcustom org-startup-with-beamer-mode nil
  453. "Non-nil means turn on `org-beamer-mode' on startup.
  454. This can also be configured on a per-file basis by adding one of
  455. the following lines anywhere in the buffer:
  456. #+STARTUP: beamer"
  457. :group 'org-startup
  458. :version "24.1"
  459. :type 'boolean)
  460. (defcustom org-startup-align-all-tables nil
  461. "Non-nil means align all tables when visiting a file.
  462. This is useful when the column width in tables is forced with <N> cookies
  463. in table fields. Such tables will look correct only after the first re-align.
  464. This can also be configured on a per-file basis by adding one of
  465. the following lines anywhere in the buffer:
  466. #+STARTUP: align
  467. #+STARTUP: noalign"
  468. :group 'org-startup
  469. :type 'boolean)
  470. (defcustom org-startup-with-inline-images nil
  471. "Non-nil means show inline images when loading a new Org file.
  472. This can also be configured on a per-file basis by adding one of
  473. the following lines anywhere in the buffer:
  474. #+STARTUP: inlineimages
  475. #+STARTUP: noinlineimages"
  476. :group 'org-startup
  477. :version "24.1"
  478. :type 'boolean)
  479. (defcustom org-insert-mode-line-in-empty-file nil
  480. "Non-nil means insert the first line setting Org-mode in empty files.
  481. When the function `org-mode' is called interactively in an empty file, this
  482. normally means that the file name does not automatically trigger Org-mode.
  483. To ensure that the file will always be in Org-mode in the future, a
  484. line enforcing Org-mode will be inserted into the buffer, if this option
  485. has been set."
  486. :group 'org-startup
  487. :type 'boolean)
  488. (defcustom org-replace-disputed-keys nil
  489. "Non-nil means use alternative key bindings for some keys.
  490. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  491. These keys are also used by other packages like shift-selection-mode'
  492. \(built into Emacs 23), `CUA-mode' or `windmove.el'.
  493. If you want to use Org-mode together with one of these other modes,
  494. or more generally if you would like to move some Org-mode commands to
  495. other keys, set this variable and configure the keys with the variable
  496. `org-disputed-keys'.
  497. This option is only relevant at load-time of Org-mode, and must be set
  498. *before* org.el is loaded. Changing it requires a restart of Emacs to
  499. become effective."
  500. :group 'org-startup
  501. :type 'boolean)
  502. (defcustom org-use-extra-keys nil
  503. "Non-nil means use extra key sequence definitions for certain commands.
  504. This happens automatically if you run XEmacs or if `window-system'
  505. is nil. This variable lets you do the same manually. You must
  506. set it before loading org.
  507. Example: on Carbon Emacs 22 running graphically, with an external
  508. keyboard on a Powerbook, the default way of setting M-left might
  509. not work for either Alt or ESC. Setting this variable will make
  510. it work for ESC."
  511. :group 'org-startup
  512. :type 'boolean)
  513. (if (fboundp 'defvaralias)
  514. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  515. (defcustom org-disputed-keys
  516. '(([(shift up)] . [(meta p)])
  517. ([(shift down)] . [(meta n)])
  518. ([(shift left)] . [(meta -)])
  519. ([(shift right)] . [(meta +)])
  520. ([(control shift right)] . [(meta shift +)])
  521. ([(control shift left)] . [(meta shift -)]))
  522. "Keys for which Org-mode and other modes compete.
  523. This is an alist, cars are the default keys, second element specifies
  524. the alternative to use when `org-replace-disputed-keys' is t.
  525. Keys can be specified in any syntax supported by `define-key'.
  526. The value of this option takes effect only at Org-mode's startup,
  527. therefore you'll have to restart Emacs to apply it after changing."
  528. :group 'org-startup
  529. :type 'alist)
  530. (defun org-key (key)
  531. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  532. Or return the original if not disputed.
  533. Also apply the translations defined in `org-xemacs-key-equivalents'."
  534. (when org-replace-disputed-keys
  535. (let* ((nkey (key-description key))
  536. (x (org-find-if (lambda (x)
  537. (equal (key-description (car x)) nkey))
  538. org-disputed-keys)))
  539. (setq key (if x (cdr x) key))))
  540. (when (featurep 'xemacs)
  541. (setq key (or (cdr (assoc key org-xemacs-key-equivalents)) key)))
  542. key)
  543. (defun org-find-if (predicate seq)
  544. (catch 'exit
  545. (while seq
  546. (if (funcall predicate (car seq))
  547. (throw 'exit (car seq))
  548. (pop seq)))))
  549. (defun org-defkey (keymap key def)
  550. "Define a key, possibly translated, as returned by `org-key'."
  551. (define-key keymap (org-key key) def))
  552. (defcustom org-ellipsis nil
  553. "The ellipsis to use in the Org-mode outline.
  554. When nil, just use the standard three dots. When a string, use that instead,
  555. When a face, use the standard 3 dots, but with the specified face.
  556. The change affects only Org-mode (which will then use its own display table).
  557. Changing this requires executing `M-x org-mode' in a buffer to become
  558. effective."
  559. :group 'org-startup
  560. :type '(choice (const :tag "Default" nil)
  561. (face :tag "Face" :value org-warning)
  562. (string :tag "String" :value "...#")))
  563. (defvar org-display-table nil
  564. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  565. (defgroup org-keywords nil
  566. "Keywords in Org-mode."
  567. :tag "Org Keywords"
  568. :group 'org)
  569. (defcustom org-deadline-string "DEADLINE:"
  570. "String to mark deadline entries.
  571. A deadline is this string, followed by a time stamp. Should be a word,
  572. terminated by a colon. You can insert a schedule keyword and
  573. a timestamp with \\[org-deadline].
  574. Changes become only effective after restarting Emacs."
  575. :group 'org-keywords
  576. :type 'string)
  577. (defcustom org-scheduled-string "SCHEDULED:"
  578. "String to mark scheduled TODO entries.
  579. A schedule is this string, followed by a time stamp. Should be a word,
  580. terminated by a colon. You can insert a schedule keyword and
  581. a timestamp with \\[org-schedule].
  582. Changes become only effective after restarting Emacs."
  583. :group 'org-keywords
  584. :type 'string)
  585. (defcustom org-closed-string "CLOSED:"
  586. "String used as the prefix for timestamps logging closing a TODO entry."
  587. :group 'org-keywords
  588. :type 'string)
  589. (defcustom org-clock-string "CLOCK:"
  590. "String used as prefix for timestamps clocking work hours on an item."
  591. :group 'org-keywords
  592. :type 'string)
  593. (defcustom org-comment-string "COMMENT"
  594. "Entries starting with this keyword will never be exported.
  595. An entry can be toggled between COMMENT and normal with
  596. \\[org-toggle-comment].
  597. Changes become only effective after restarting Emacs."
  598. :group 'org-keywords
  599. :type 'string)
  600. (defcustom org-quote-string "QUOTE"
  601. "Entries starting with this keyword will be exported in fixed-width font.
  602. Quoting applies only to the text in the entry following the headline, and does
  603. not extend beyond the next headline, even if that is lower level.
  604. An entry can be toggled between QUOTE and normal with
  605. \\[org-toggle-fixed-width-section]."
  606. :group 'org-keywords
  607. :type 'string)
  608. (defconst org-repeat-re
  609. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*?\\([.+]?\\+[0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)"
  610. "Regular expression for specifying repeated events.
  611. After a match, group 1 contains the repeat expression.")
  612. (defgroup org-structure nil
  613. "Options concerning the general structure of Org-mode files."
  614. :tag "Org Structure"
  615. :group 'org)
  616. (defgroup org-reveal-location nil
  617. "Options about how to make context of a location visible."
  618. :tag "Org Reveal Location"
  619. :group 'org-structure)
  620. (defconst org-context-choice
  621. '(choice
  622. (const :tag "Always" t)
  623. (const :tag "Never" nil)
  624. (repeat :greedy t :tag "Individual contexts"
  625. (cons
  626. (choice :tag "Context"
  627. (const agenda)
  628. (const org-goto)
  629. (const occur-tree)
  630. (const tags-tree)
  631. (const link-search)
  632. (const mark-goto)
  633. (const bookmark-jump)
  634. (const isearch)
  635. (const default))
  636. (boolean))))
  637. "Contexts for the reveal options.")
  638. (defcustom org-show-hierarchy-above '((default . t))
  639. "Non-nil means show full hierarchy when revealing a location.
  640. Org-mode often shows locations in an org-mode file which might have
  641. been invisible before. When this is set, the hierarchy of headings
  642. above the exposed location is shown.
  643. Turning this off for example for sparse trees makes them very compact.
  644. Instead of t, this can also be an alist specifying this option for different
  645. contexts. Valid contexts are
  646. agenda when exposing an entry from the agenda
  647. org-goto when using the command `org-goto' on key C-c C-j
  648. occur-tree when using the command `org-occur' on key C-c /
  649. tags-tree when constructing a sparse tree based on tags matches
  650. link-search when exposing search matches associated with a link
  651. mark-goto when exposing the jump goal of a mark
  652. bookmark-jump when exposing a bookmark location
  653. isearch when exiting from an incremental search
  654. default default for all contexts not set explicitly"
  655. :group 'org-reveal-location
  656. :type org-context-choice)
  657. (defcustom org-show-following-heading '((default . nil))
  658. "Non-nil means show following 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 heading following the
  661. match is shown.
  662. Turning this off for example for sparse trees makes them very compact,
  663. but makes it harder to edit the location of the match. In such a case,
  664. use the command \\[org-reveal] to show more context.
  665. Instead of t, this can also be an alist specifying this option for different
  666. contexts. See `org-show-hierarchy-above' for valid contexts."
  667. :group 'org-reveal-location
  668. :type org-context-choice)
  669. (defcustom org-show-siblings '((default . nil) (isearch t))
  670. "Non-nil means show all sibling heading when revealing a location.
  671. Org-mode often shows locations in an org-mode file which might have
  672. been invisible before. When this is set, the sibling of the current entry
  673. heading are all made visible. If `org-show-hierarchy-above' is t,
  674. the same happens on each level of the hierarchy above the current entry.
  675. By default this is on for the isearch context, off for all other contexts.
  676. Turning this off for example for sparse trees makes them very compact,
  677. but makes it harder to edit the location of the match. In such a case,
  678. use the command \\[org-reveal] to show more context.
  679. Instead of t, this can also be an alist specifying this option for different
  680. contexts. See `org-show-hierarchy-above' for valid contexts."
  681. :group 'org-reveal-location
  682. :type org-context-choice)
  683. (defcustom org-show-entry-below '((default . nil))
  684. "Non-nil means show the entry below a headline when revealing a location.
  685. Org-mode often shows locations in an org-mode file which might have
  686. been invisible before. When this is set, the text below the headline that is
  687. exposed is also shown.
  688. By default this is off for all contexts.
  689. Instead of t, this can also be an alist specifying this option for different
  690. contexts. See `org-show-hierarchy-above' for valid contexts."
  691. :group 'org-reveal-location
  692. :type org-context-choice)
  693. (defcustom org-indirect-buffer-display 'other-window
  694. "How should indirect tree buffers be displayed?
  695. This applies to indirect buffers created with the commands
  696. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  697. Valid values are:
  698. current-window Display in the current window
  699. other-window Just display in another window.
  700. dedicated-frame Create one new frame, and re-use it each time.
  701. new-frame Make a new frame each time. Note that in this case
  702. previously-made indirect buffers are kept, and you need to
  703. kill these buffers yourself."
  704. :group 'org-structure
  705. :group 'org-agenda-windows
  706. :type '(choice
  707. (const :tag "In current window" current-window)
  708. (const :tag "In current frame, other window" other-window)
  709. (const :tag "Each time a new frame" new-frame)
  710. (const :tag "One dedicated frame" dedicated-frame)))
  711. (defcustom org-use-speed-commands nil
  712. "Non-nil means activate single letter commands at beginning of a headline.
  713. This may also be a function to test for appropriate locations where speed
  714. commands should be active."
  715. :group 'org-structure
  716. :type '(choice
  717. (const :tag "Never" nil)
  718. (const :tag "At beginning of headline stars" t)
  719. (function)))
  720. (defcustom org-speed-commands-user nil
  721. "Alist of additional speed commands.
  722. This list will be checked before `org-speed-commands-default'
  723. when the variable `org-use-speed-commands' is non-nil
  724. and when the cursor is at the beginning of a headline.
  725. The car if each entry is a string with a single letter, which must
  726. be assigned to `self-insert-command' in the global map.
  727. The cdr is either a command to be called interactively, a function
  728. to be called, or a form to be evaluated.
  729. An entry that is just a list with a single string will be interpreted
  730. as a descriptive headline that will be added when listing the speed
  731. commands in the Help buffer using the `?' speed command."
  732. :group 'org-structure
  733. :type '(repeat :value ("k" . ignore)
  734. (choice :value ("k" . ignore)
  735. (list :tag "Descriptive Headline" (string :tag "Headline"))
  736. (cons :tag "Letter and Command"
  737. (string :tag "Command letter")
  738. (choice
  739. (function)
  740. (sexp))))))
  741. (defgroup org-cycle nil
  742. "Options concerning visibility cycling in Org-mode."
  743. :tag "Org Cycle"
  744. :group 'org-structure)
  745. (defcustom org-cycle-skip-children-state-if-no-children t
  746. "Non-nil means skip CHILDREN state in entries that don't have any."
  747. :group 'org-cycle
  748. :type 'boolean)
  749. (defcustom org-cycle-max-level nil
  750. "Maximum level which should still be subject to visibility cycling.
  751. Levels higher than this will, for cycling, be treated as text, not a headline.
  752. When `org-odd-levels-only' is set, a value of N in this variable actually
  753. means 2N-1 stars as the limiting headline.
  754. When nil, cycle all levels.
  755. Note that the limiting level of cycling is also influenced by
  756. `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
  757. `org-inlinetask-min-level' is, cycling will be limited to levels one less
  758. than its value."
  759. :group 'org-cycle
  760. :type '(choice
  761. (const :tag "No limit" nil)
  762. (integer :tag "Maximum level")))
  763. (defcustom org-drawers '("PROPERTIES" "CLOCK" "LOGBOOK" "RESULTS")
  764. "Names of drawers. Drawers are not opened by cycling on the headline above.
  765. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  766. this:
  767. :DRAWERNAME:
  768. .....
  769. :END:
  770. The drawer \"PROPERTIES\" is special for capturing properties through
  771. the property API.
  772. Drawers can be defined on the per-file basis with a line like:
  773. #+DRAWERS: HIDDEN STATE PROPERTIES"
  774. :group 'org-structure
  775. :group 'org-cycle
  776. :type '(repeat (string :tag "Drawer Name")))
  777. (defcustom org-hide-block-startup nil
  778. "Non-nil means entering Org-mode will fold all blocks.
  779. This can also be set in on a per-file basis with
  780. #+STARTUP: hideblocks
  781. #+STARTUP: showblocks"
  782. :group 'org-startup
  783. :group 'org-cycle
  784. :type 'boolean)
  785. (defcustom org-cycle-global-at-bob nil
  786. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  787. This makes it possible to do global cycling without having to use S-TAB or
  788. \\[universal-argument] TAB. For this special case to work, the first line \
  789. of the buffer
  790. must not be a headline - it may be empty or some other text. When used in
  791. this way, `org-cycle-hook' is disables temporarily, to make sure the
  792. cursor stays at the beginning of the buffer.
  793. When this option is nil, don't do anything special at the beginning
  794. of the buffer."
  795. :group 'org-cycle
  796. :type 'boolean)
  797. (defcustom org-cycle-level-after-item/entry-creation t
  798. "Non-nil means cycle entry level or item indentation in new empty entries.
  799. When the cursor is at the end of an empty headline, i.e with only stars
  800. and maybe a TODO keyword, TAB will then switch the entry to become a child,
  801. and then all possible ancestor states, before returning to the original state.
  802. This makes data entry extremely fast: M-RET to create a new headline,
  803. on TAB to make it a child, two or more tabs to make it a (grand-)uncle.
  804. When the cursor is at the end of an empty plain list item, one TAB will
  805. make it a subitem, two or more tabs will back up to make this an item
  806. higher up in the item hierarchy."
  807. :group 'org-cycle
  808. :type 'boolean)
  809. (defcustom org-cycle-emulate-tab t
  810. "Where should `org-cycle' emulate TAB.
  811. nil Never
  812. white Only in completely white lines
  813. whitestart Only at the beginning of lines, before the first non-white char
  814. t Everywhere except in headlines
  815. exc-hl-bol Everywhere except at the start of a headline
  816. If TAB is used in a place where it does not emulate TAB, the current subtree
  817. visibility is cycled."
  818. :group 'org-cycle
  819. :type '(choice (const :tag "Never" nil)
  820. (const :tag "Only in completely white lines" white)
  821. (const :tag "Before first char in a line" whitestart)
  822. (const :tag "Everywhere except in headlines" t)
  823. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  824. ))
  825. (defcustom org-cycle-separator-lines 2
  826. "Number of empty lines needed to keep an empty line between collapsed trees.
  827. If you leave an empty line between the end of a subtree and the following
  828. headline, this empty line is hidden when the subtree is folded.
  829. Org-mode will leave (exactly) one empty line visible if the number of
  830. empty lines is equal or larger to the number given in this variable.
  831. So the default 2 means at least 2 empty lines after the end of a subtree
  832. are needed to produce free space between a collapsed subtree and the
  833. following headline.
  834. If the number is negative, and the number of empty lines is at least -N,
  835. all empty lines are shown.
  836. Special case: when 0, never leave empty lines in collapsed view."
  837. :group 'org-cycle
  838. :type 'integer)
  839. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  840. (defcustom org-pre-cycle-hook nil
  841. "Hook that is run before visibility cycling is happening.
  842. The function(s) in this hook must accept a single argument which indicates
  843. the new state that will be set right after running this hook. The
  844. argument is a symbol. Before a global state change, it can have the values
  845. `overview', `content', or `all'. Before a local state change, it can have
  846. the values `folded', `children', or `subtree'."
  847. :group 'org-cycle
  848. :type 'hook)
  849. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  850. org-cycle-hide-drawers
  851. org-cycle-show-empty-lines
  852. org-optimize-window-after-visibility-change)
  853. "Hook that is run after `org-cycle' has changed the buffer visibility.
  854. The function(s) in this hook must accept a single argument which indicates
  855. the new state that was set by the most recent `org-cycle' command. The
  856. argument is a symbol. After a global state change, it can have the values
  857. `overview', `content', or `all'. After a local state change, it can have
  858. the values `folded', `children', or `subtree'."
  859. :group 'org-cycle
  860. :type 'hook)
  861. (defgroup org-edit-structure nil
  862. "Options concerning structure editing in Org-mode."
  863. :tag "Org Edit Structure"
  864. :group 'org-structure)
  865. (defcustom org-odd-levels-only nil
  866. "Non-nil means skip even levels and only use odd levels for the outline.
  867. This has the effect that two stars are being added/taken away in
  868. promotion/demotion commands. It also influences how levels are
  869. handled by the exporters.
  870. Changing it requires restart of `font-lock-mode' to become effective
  871. for fontification also in regions already fontified.
  872. You may also set this on a per-file basis by adding one of the following
  873. lines to the buffer:
  874. #+STARTUP: odd
  875. #+STARTUP: oddeven"
  876. :group 'org-edit-structure
  877. :group 'org-appearance
  878. :type 'boolean)
  879. (defcustom org-adapt-indentation t
  880. "Non-nil means adapt indentation to outline node level.
  881. When this variable is set, Org assumes that you write outlines by
  882. indenting text in each node to align with the headline (after the stars).
  883. The following issues are influenced by this variable:
  884. - When this is set and the *entire* text in an entry is indented, the
  885. indentation is increased by one space in a demotion command, and
  886. decreased by one in a promotion command. If any line in the entry
  887. body starts with text at column 0, indentation is not changed at all.
  888. - Property drawers and planning information is inserted indented when
  889. this variable s set. When nil, they will not be indented.
  890. - TAB indents a line relative to context. The lines below a headline
  891. will be indented when this variable is set.
  892. Note that this is all about true indentation, by adding and removing
  893. space characters. See also `org-indent.el' which does level-dependent
  894. indentation in a virtual way, i.e. at display time in Emacs."
  895. :group 'org-edit-structure
  896. :type 'boolean)
  897. (defcustom org-special-ctrl-a/e nil
  898. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  899. When t, `C-a' will bring back the cursor to the beginning of the
  900. headline text, i.e. after the stars and after a possible TODO
  901. keyword. In an item, this will be the position after bullet and
  902. check-box, if any. When the cursor is already at that position,
  903. another `C-a' will bring it to the beginning of the line.
  904. `C-e' will jump to the end of the headline, ignoring the presence
  905. of tags in the headline. A second `C-e' will then jump to the
  906. true end of the line, after any tags. This also means that, when
  907. this variable is non-nil, `C-e' also will never jump beyond the
  908. end of the heading of a folded section, i.e. not after the
  909. ellipses.
  910. When set to the symbol `reversed', the first `C-a' or `C-e' works
  911. normally, going to the true line boundary first. Only a directly
  912. following, identical keypress will bring the cursor to the
  913. special positions.
  914. This may also be a cons cell where the behavior for `C-a' and
  915. `C-e' is set separately."
  916. :group 'org-edit-structure
  917. :type '(choice
  918. (const :tag "off" nil)
  919. (const :tag "on: after stars/bullet and before tags first" t)
  920. (const :tag "reversed: true line boundary first" reversed)
  921. (cons :tag "Set C-a and C-e separately"
  922. (choice :tag "Special C-a"
  923. (const :tag "off" nil)
  924. (const :tag "on: after stars/bullet first" t)
  925. (const :tag "reversed: before stars/bullet first" reversed))
  926. (choice :tag "Special C-e"
  927. (const :tag "off" nil)
  928. (const :tag "on: before tags first" t)
  929. (const :tag "reversed: after tags first" reversed)))))
  930. (if (fboundp 'defvaralias)
  931. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  932. (defcustom org-special-ctrl-k nil
  933. "Non-nil means `C-k' will behave specially in headlines.
  934. When nil, `C-k' will call the default `kill-line' command.
  935. When t, the following will happen while the cursor is in the headline:
  936. - When the cursor is at the beginning of a headline, kill the entire
  937. line and possible the folded subtree below the line.
  938. - When in the middle of the headline text, kill the headline up to the tags.
  939. - When after the headline text, kill the tags."
  940. :group 'org-edit-structure
  941. :type 'boolean)
  942. (defcustom org-ctrl-k-protect-subtree nil
  943. "Non-nil means, do not delete a hidden subtree with C-k.
  944. When set to the symbol `error', simply throw an error when C-k is
  945. used to kill (part-of) a headline that has hidden text behind it.
  946. Any other non-nil value will result in a query to the user, if it is
  947. OK to kill that hidden subtree. When nil, kill without remorse."
  948. :group 'org-edit-structure
  949. :version "24.1"
  950. :type '(choice
  951. (const :tag "Do not protect hidden subtrees" nil)
  952. (const :tag "Protect hidden subtrees with a security query" t)
  953. (const :tag "Never kill a hidden subtree with C-k" error)))
  954. (defcustom org-catch-invisible-edits nil
  955. "Check if in invisible region before inserting or deleting a character.
  956. Valid values are:
  957. nil Do not check, so just do invisible edits.
  958. error Throw an error and do nothing.
  959. show Make point visible, and do the requested edit.
  960. show-and-error Make point visible, then throw an error and abort the edit.
  961. smart Make point visible, and do insertion/deletion if it is
  962. adjacent to visible text and the change feels predictable.
  963. Never delete a previously invisible character or add in the
  964. middle or right after an invisible region. Basically, this
  965. allows insertion and backward-delete right before ellipses.
  966. FIXME: maybe in this case we should not even show?"
  967. :group 'org-edit-structure
  968. :version "24.1"
  969. :type '(choice
  970. (const :tag "Do not check" nil)
  971. (const :tag "Throw error when trying to edit" error)
  972. (const :tag "Unhide, but do not do the edit" show-and-error)
  973. (const :tag "Show invisible part and do the edit" show)
  974. (const :tag "Be smart and do the right thing" smart)))
  975. (defcustom org-yank-folded-subtrees t
  976. "Non-nil means when yanking subtrees, fold them.
  977. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  978. it starts with a heading and all other headings in it are either children
  979. or siblings, then fold all the subtrees. However, do this only if no
  980. text after the yank would be swallowed into a folded tree by this action."
  981. :group 'org-edit-structure
  982. :type 'boolean)
  983. (defcustom org-yank-adjusted-subtrees nil
  984. "Non-nil means when yanking subtrees, adjust the level.
  985. With this setting, `org-paste-subtree' is used to insert the subtree, see
  986. this function for details."
  987. :group 'org-edit-structure
  988. :type 'boolean)
  989. (defcustom org-M-RET-may-split-line '((default . t))
  990. "Non-nil means M-RET will split the line at the cursor position.
  991. When nil, it will go to the end of the line before making a
  992. new line.
  993. You may also set this option in a different way for different
  994. contexts. Valid contexts are:
  995. headline when creating a new headline
  996. item when creating a new item
  997. table in a table field
  998. default the value to be used for all contexts not explicitly
  999. customized"
  1000. :group 'org-structure
  1001. :group 'org-table
  1002. :type '(choice
  1003. (const :tag "Always" t)
  1004. (const :tag "Never" nil)
  1005. (repeat :greedy t :tag "Individual contexts"
  1006. (cons
  1007. (choice :tag "Context"
  1008. (const headline)
  1009. (const item)
  1010. (const table)
  1011. (const default))
  1012. (boolean)))))
  1013. (defcustom org-insert-heading-respect-content nil
  1014. "Non-nil means insert new headings after the current subtree.
  1015. When nil, the new heading is created directly after the current line.
  1016. The commands \\[org-insert-heading-respect-content] and
  1017. \\[org-insert-todo-heading-respect-content] turn this variable on
  1018. for the duration of the command."
  1019. :group 'org-structure
  1020. :type 'boolean)
  1021. (defcustom org-blank-before-new-entry '((heading . auto)
  1022. (plain-list-item . auto))
  1023. "Should `org-insert-heading' leave a blank line before new heading/item?
  1024. The value is an alist, with `heading' and `plain-list-item' as CAR,
  1025. and a boolean flag as CDR. The cdr may also be the symbol `auto', in
  1026. which case Org will look at the surrounding headings/items and try to
  1027. make an intelligent decision whether to insert a blank line or not.
  1028. For plain lists, if the variable `org-empty-line-terminates-plain-lists' is
  1029. set, the setting here is ignored and no empty line is inserted, to avoid
  1030. breaking the list structure."
  1031. :group 'org-edit-structure
  1032. :type '(list
  1033. (cons (const heading)
  1034. (choice (const :tag "Never" nil)
  1035. (const :tag "Always" t)
  1036. (const :tag "Auto" auto)))
  1037. (cons (const plain-list-item)
  1038. (choice (const :tag "Never" nil)
  1039. (const :tag "Always" t)
  1040. (const :tag "Auto" auto)))))
  1041. (defcustom org-insert-heading-hook nil
  1042. "Hook being run after inserting a new heading."
  1043. :group 'org-edit-structure
  1044. :type 'hook)
  1045. (defcustom org-enable-fixed-width-editor t
  1046. "Non-nil means lines starting with \":\" are treated as fixed-width.
  1047. This currently only means they are never auto-wrapped.
  1048. When nil, such lines will be treated like ordinary lines.
  1049. See also the QUOTE keyword."
  1050. :group 'org-edit-structure
  1051. :type 'boolean)
  1052. (defcustom org-goto-auto-isearch t
  1053. "Non-nil means typing characters in `org-goto' starts incremental search."
  1054. :group 'org-edit-structure
  1055. :type 'boolean)
  1056. (defgroup org-sparse-trees nil
  1057. "Options concerning sparse trees in Org-mode."
  1058. :tag "Org Sparse Trees"
  1059. :group 'org-structure)
  1060. (defcustom org-highlight-sparse-tree-matches t
  1061. "Non-nil means highlight all matches that define a sparse tree.
  1062. The highlights will automatically disappear the next time the buffer is
  1063. changed by an edit command."
  1064. :group 'org-sparse-trees
  1065. :type 'boolean)
  1066. (defcustom org-remove-highlights-with-change t
  1067. "Non-nil means any change to the buffer will remove temporary highlights.
  1068. Such highlights are created by `org-occur' and `org-clock-display'.
  1069. When nil, `C-c C-c needs to be used to get rid of the highlights.
  1070. The highlights created by `org-preview-latex-fragment' always need
  1071. `C-c C-c' to be removed."
  1072. :group 'org-sparse-trees
  1073. :group 'org-time
  1074. :type 'boolean)
  1075. (defcustom org-occur-hook '(org-first-headline-recenter)
  1076. "Hook that is run after `org-occur' has constructed a sparse tree.
  1077. This can be used to recenter the window to show as much of the structure
  1078. as possible."
  1079. :group 'org-sparse-trees
  1080. :type 'hook)
  1081. (defgroup org-imenu-and-speedbar nil
  1082. "Options concerning imenu and speedbar in Org-mode."
  1083. :tag "Org Imenu and Speedbar"
  1084. :group 'org-structure)
  1085. (defcustom org-imenu-depth 2
  1086. "The maximum level for Imenu access to Org-mode headlines.
  1087. This also applied for speedbar access."
  1088. :group 'org-imenu-and-speedbar
  1089. :type 'integer)
  1090. (defgroup org-table nil
  1091. "Options concerning tables in Org-mode."
  1092. :tag "Org Table"
  1093. :group 'org)
  1094. (defcustom org-enable-table-editor 'optimized
  1095. "Non-nil means lines starting with \"|\" are handled by the table editor.
  1096. When nil, such lines will be treated like ordinary lines.
  1097. When equal to the symbol `optimized', the table editor will be optimized to
  1098. do the following:
  1099. - Automatic overwrite mode in front of whitespace in table fields.
  1100. This makes the structure of the table stay in tact as long as the edited
  1101. field does not exceed the column width.
  1102. - Minimize the number of realigns. Normally, the table is aligned each time
  1103. TAB or RET are pressed to move to another field. With optimization this
  1104. happens only if changes to a field might have changed the column width.
  1105. Optimization requires replacing the functions `self-insert-command',
  1106. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  1107. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  1108. very good at guessing when a re-align will be necessary, but you can always
  1109. force one with \\[org-ctrl-c-ctrl-c].
  1110. If you would like to use the optimized version in Org-mode, but the
  1111. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  1112. This variable can be used to turn on and off the table editor during a session,
  1113. but in order to toggle optimization, a restart is required.
  1114. See also the variable `org-table-auto-blank-field'."
  1115. :group 'org-table
  1116. :type '(choice
  1117. (const :tag "off" nil)
  1118. (const :tag "on" t)
  1119. (const :tag "on, optimized" optimized)))
  1120. (defcustom org-self-insert-cluster-for-undo t
  1121. "Non-nil means cluster self-insert commands for undo when possible.
  1122. If this is set, then, like in the Emacs command loop, 20 consecutive
  1123. characters will be undone together.
  1124. This is configurable, because there is some impact on typing performance."
  1125. :group 'org-table
  1126. :type 'boolean)
  1127. (defcustom org-table-tab-recognizes-table.el t
  1128. "Non-nil means TAB will automatically notice a table.el table.
  1129. When it sees such a table, it moves point into it and - if necessary -
  1130. calls `table-recognize-table'."
  1131. :group 'org-table-editing
  1132. :type 'boolean)
  1133. (defgroup org-link nil
  1134. "Options concerning links in Org-mode."
  1135. :tag "Org Link"
  1136. :group 'org)
  1137. (defvar org-link-abbrev-alist-local nil
  1138. "Buffer-local version of `org-link-abbrev-alist', which see.
  1139. The value of this is taken from the #+LINK lines.")
  1140. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  1141. (defcustom org-link-abbrev-alist nil
  1142. "Alist of link abbreviations.
  1143. The car of each element is a string, to be replaced at the start of a link.
  1144. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  1145. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  1146. [[linkkey:tag][description]]
  1147. The 'linkkey' must be a word word, starting with a letter, followed
  1148. by letters, numbers, '-' or '_'.
  1149. If REPLACE is a string, the tag will simply be appended to create the link.
  1150. If the string contains \"%s\", the tag will be inserted there. Alternatively,
  1151. the placeholder \"%h\" will cause a url-encoded version of the tag to
  1152. be inserted at that point (see the function `url-hexify-string').
  1153. REPLACE may also be a function that will be called with the tag as the
  1154. only argument to create the link, which should be returned as a string.
  1155. See the manual for examples."
  1156. :group 'org-link
  1157. :type '(repeat
  1158. (cons
  1159. (string :tag "Protocol")
  1160. (choice
  1161. (string :tag "Format")
  1162. (function)))))
  1163. (defcustom org-descriptive-links t
  1164. "Non-nil means Org will display descriptive links.
  1165. E.g. [[http://orgmode.org][Org website]] will be displayed as
  1166. \"Org Website\", hiding the link itself and just displaying its
  1167. description. When set to `nil', Org will display the full links
  1168. literally.
  1169. You can interactively set the value of this variable by calling
  1170. `org-toggle-link-display' or from the menu Org>Hyperlinks menu."
  1171. :group 'org-link
  1172. :type 'boolean)
  1173. (defcustom org-link-file-path-type 'adaptive
  1174. "How the path name in file links should be stored.
  1175. Valid values are:
  1176. relative Relative to the current directory, i.e. the directory of the file
  1177. into which the link is being inserted.
  1178. absolute Absolute path, if possible with ~ for home directory.
  1179. noabbrev Absolute path, no abbreviation of home directory.
  1180. adaptive Use relative path for files in the current directory and sub-
  1181. directories of it. For other files, use an absolute path."
  1182. :group 'org-link
  1183. :type '(choice
  1184. (const relative)
  1185. (const absolute)
  1186. (const noabbrev)
  1187. (const adaptive)))
  1188. (defcustom org-activate-links '(bracket angle plain radio tag date footnote)
  1189. "Types of links that should be activated in Org-mode files.
  1190. This is a list of symbols, each leading to the activation of a certain link
  1191. type. In principle, it does not hurt to turn on most link types - there may
  1192. be a small gain when turning off unused link types. The types are:
  1193. bracket The recommended [[link][description]] or [[link]] links with hiding.
  1194. angle Links in angular brackets that may contain whitespace like
  1195. <bbdb:Carsten Dominik>.
  1196. plain Plain links in normal text, no whitespace, like http://google.com.
  1197. radio Text that is matched by a radio target, see manual for details.
  1198. tag Tag settings in a headline (link to tag search).
  1199. date Time stamps (link to calendar).
  1200. footnote Footnote labels.
  1201. Changing this variable requires a restart of Emacs to become effective."
  1202. :group 'org-link
  1203. :type '(set :greedy t
  1204. (const :tag "Double bracket links" bracket)
  1205. (const :tag "Angular bracket links" angle)
  1206. (const :tag "Plain text links" plain)
  1207. (const :tag "Radio target matches" radio)
  1208. (const :tag "Tags" tag)
  1209. (const :tag "Timestamps" date)
  1210. (const :tag "Footnotes" footnote)))
  1211. (defcustom org-make-link-description-function nil
  1212. "Function to use to generate link descriptions from links.
  1213. If nil the link location will be used. This function must take
  1214. two parameters; the first is the link and the second the
  1215. description `org-insert-link' has generated, and should return the
  1216. description to use."
  1217. :group 'org-link
  1218. :type 'function)
  1219. (defgroup org-link-store nil
  1220. "Options concerning storing links in Org-mode."
  1221. :tag "Org Store Link"
  1222. :group 'org-link)
  1223. (defcustom org-email-link-description-format "Email %c: %.30s"
  1224. "Format of the description part of a link to an email or usenet message.
  1225. The following %-escapes will be replaced by corresponding information:
  1226. %F full \"From\" field
  1227. %f name, taken from \"From\" field, address if no name
  1228. %T full \"To\" field
  1229. %t first name in \"To\" field, address if no name
  1230. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  1231. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  1232. %s subject
  1233. %d date
  1234. %m message-id.
  1235. You may use normal field width specification between the % and the letter.
  1236. This is for example useful to limit the length of the subject.
  1237. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  1238. :group 'org-link-store
  1239. :type 'string)
  1240. (defcustom org-from-is-user-regexp
  1241. (let (r1 r2)
  1242. (when (and user-mail-address (not (string= user-mail-address "")))
  1243. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  1244. (when (and user-full-name (not (string= user-full-name "")))
  1245. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  1246. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  1247. "Regexp matched against the \"From:\" header of an email or usenet message.
  1248. It should match if the message is from the user him/herself."
  1249. :group 'org-link-store
  1250. :type 'regexp)
  1251. (defcustom org-link-to-org-use-id 'create-if-interactive-and-no-custom-id
  1252. "Non-nil means storing a link to an Org file will use entry IDs.
  1253. Note that before this variable is even considered, org-id must be loaded,
  1254. so please customize `org-modules' and turn it on.
  1255. The variable can have the following values:
  1256. t Create an ID if needed to make a link to the current entry.
  1257. create-if-interactive
  1258. If `org-store-link' is called directly (interactively, as a user
  1259. command), do create an ID to support the link. But when doing the
  1260. job for remember, only use the ID if it already exists. The
  1261. purpose of this setting is to avoid proliferation of unwanted
  1262. IDs, just because you happen to be in an Org file when you
  1263. call `org-remember' that automatically and preemptively
  1264. creates a link. If you do want to get an ID link in a remember
  1265. template to an entry not having an ID, create it first by
  1266. explicitly creating a link to it, using `C-c C-l' first.
  1267. create-if-interactive-and-no-custom-id
  1268. Like create-if-interactive, but do not create an ID if there is
  1269. a CUSTOM_ID property defined in the entry. This is the default.
  1270. use-existing
  1271. Use existing ID, do not create one.
  1272. nil Never use an ID to make a link, instead link using a text search for
  1273. the headline text."
  1274. :group 'org-link-store
  1275. :type '(choice
  1276. (const :tag "Create ID to make link" t)
  1277. (const :tag "Create if storing link interactively"
  1278. create-if-interactive)
  1279. (const :tag "Create if storing link interactively and no CUSTOM_ID is present"
  1280. create-if-interactive-and-no-custom-id)
  1281. (const :tag "Only use existing" use-existing)
  1282. (const :tag "Do not use ID to create link" nil)))
  1283. (defcustom org-context-in-file-links t
  1284. "Non-nil means file links from `org-store-link' contain context.
  1285. A search string will be added to the file name with :: as separator and
  1286. used to find the context when the link is activated by the command
  1287. `org-open-at-point'. When this option is t, the entire active region
  1288. will be placed in the search string of the file link. If set to a
  1289. positive integer, only the first n lines of context will be stored.
  1290. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1291. negates this setting for the duration of the command."
  1292. :group 'org-link-store
  1293. :type '(choice boolean integer))
  1294. (defcustom org-keep-stored-link-after-insertion nil
  1295. "Non-nil means keep link in list for entire session.
  1296. The command `org-store-link' adds a link pointing to the current
  1297. location to an internal list. These links accumulate during a session.
  1298. The command `org-insert-link' can be used to insert links into any
  1299. Org-mode file (offering completion for all stored links). When this
  1300. option is nil, every link which has been inserted once using \\[org-insert-link]
  1301. will be removed from the list, to make completing the unused links
  1302. more efficient."
  1303. :group 'org-link-store
  1304. :type 'boolean)
  1305. (defgroup org-link-follow nil
  1306. "Options concerning following links in Org-mode."
  1307. :tag "Org Follow Link"
  1308. :group 'org-link)
  1309. (defcustom org-link-translation-function nil
  1310. "Function to translate links with different syntax to Org syntax.
  1311. This can be used to translate links created for example by the Planner
  1312. or emacs-wiki packages to Org syntax.
  1313. The function must accept two parameters, a TYPE containing the link
  1314. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  1315. which is everything after the link protocol. It should return a cons
  1316. with possibly modified values of type and path.
  1317. Org contains a function for this, so if you set this variable to
  1318. `org-translate-link-from-planner', you should be able follow many
  1319. links created by planner."
  1320. :group 'org-link-follow
  1321. :type 'function)
  1322. (defcustom org-follow-link-hook nil
  1323. "Hook that is run after a link has been followed."
  1324. :group 'org-link-follow
  1325. :type 'hook)
  1326. (defcustom org-tab-follows-link nil
  1327. "Non-nil means on links TAB will follow the link.
  1328. Needs to be set before org.el is loaded.
  1329. This really should not be used, it does not make sense, and the
  1330. implementation is bad."
  1331. :group 'org-link-follow
  1332. :type 'boolean)
  1333. (defcustom org-return-follows-link nil
  1334. "Non-nil means on links RET will follow the link."
  1335. :group 'org-link-follow
  1336. :type 'boolean)
  1337. (defcustom org-mouse-1-follows-link
  1338. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1339. "Non-nil means mouse-1 on a link will follow the link.
  1340. A longer mouse click will still set point. Does not work on XEmacs.
  1341. Needs to be set before org.el is loaded."
  1342. :group 'org-link-follow
  1343. :type 'boolean)
  1344. (defcustom org-mark-ring-length 4
  1345. "Number of different positions to be recorded in the ring.
  1346. Changing this requires a restart of Emacs to work correctly."
  1347. :group 'org-link-follow
  1348. :type 'integer)
  1349. (defcustom org-link-search-must-match-exact-headline 'query-to-create
  1350. "Non-nil means internal links in Org files must exactly match a headline.
  1351. When nil, the link search tries to match a phrase with all words
  1352. in the search text."
  1353. :group 'org-link-follow
  1354. :version "24.1"
  1355. :type '(choice
  1356. (const :tag "Use fuzzy text search" nil)
  1357. (const :tag "Match only exact headline" t)
  1358. (const :tag "Match exact headline or query to create it"
  1359. query-to-create)))
  1360. (defcustom org-link-frame-setup
  1361. '((vm . vm-visit-folder-other-frame)
  1362. (vm-imap . vm-visit-imap-folder-other-frame)
  1363. (gnus . org-gnus-no-new-news)
  1364. (file . find-file-other-window)
  1365. (wl . wl-other-frame))
  1366. "Setup the frame configuration for following links.
  1367. When following a link with Emacs, it may often be useful to display
  1368. this link in another window or frame. This variable can be used to
  1369. set this up for the different types of links.
  1370. For VM, use any of
  1371. `vm-visit-folder'
  1372. `vm-visit-folder-other-window'
  1373. `vm-visit-folder-other-frame'
  1374. For Gnus, use any of
  1375. `gnus'
  1376. `gnus-other-frame'
  1377. `org-gnus-no-new-news'
  1378. For FILE, use any of
  1379. `find-file'
  1380. `find-file-other-window'
  1381. `find-file-other-frame'
  1382. For Wanderlust use any of
  1383. `wl'
  1384. `wl-other-frame'
  1385. For the calendar, use the variable `calendar-setup'.
  1386. For BBDB, it is currently only possible to display the matches in
  1387. another window."
  1388. :group 'org-link-follow
  1389. :type '(list
  1390. (cons (const vm)
  1391. (choice
  1392. (const vm-visit-folder)
  1393. (const vm-visit-folder-other-window)
  1394. (const vm-visit-folder-other-frame)))
  1395. (cons (const gnus)
  1396. (choice
  1397. (const gnus)
  1398. (const gnus-other-frame)
  1399. (const org-gnus-no-new-news)))
  1400. (cons (const file)
  1401. (choice
  1402. (const find-file)
  1403. (const find-file-other-window)
  1404. (const find-file-other-frame)))
  1405. (cons (const wl)
  1406. (choice
  1407. (const wl)
  1408. (const wl-other-frame)))))
  1409. (defcustom org-display-internal-link-with-indirect-buffer nil
  1410. "Non-nil means use indirect buffer to display infile links.
  1411. Activating internal links (from one location in a file to another location
  1412. in the same file) normally just jumps to the location. When the link is
  1413. activated with a \\[universal-argument] prefix (or with mouse-3), the link \
  1414. is displayed in
  1415. another window. When this option is set, the other window actually displays
  1416. an indirect buffer clone of the current buffer, to avoid any visibility
  1417. changes to the current buffer."
  1418. :group 'org-link-follow
  1419. :type 'boolean)
  1420. (defcustom org-open-non-existing-files nil
  1421. "Non-nil means `org-open-file' will open non-existing files.
  1422. When nil, an error will be generated.
  1423. This variable applies only to external applications because they
  1424. might choke on non-existing files. If the link is to a file that
  1425. will be opened in Emacs, the variable is ignored."
  1426. :group 'org-link-follow
  1427. :type 'boolean)
  1428. (defcustom org-open-directory-means-index-dot-org nil
  1429. "Non-nil means a link to a directory really means to index.org.
  1430. When nil, following a directory link will run dired or open a finder/explorer
  1431. window on that directory."
  1432. :group 'org-link-follow
  1433. :type 'boolean)
  1434. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1435. "Function and arguments to call for following mailto links.
  1436. This is a list with the first element being a Lisp function, and the
  1437. remaining elements being arguments to the function. In string arguments,
  1438. %a will be replaced by the address, and %s will be replaced by the subject
  1439. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1440. :group 'org-link-follow
  1441. :type '(choice
  1442. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1443. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1444. (const :tag "message-mail" (message-mail "%a" "%s"))
  1445. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1446. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1447. "Non-nil means ask for confirmation before executing shell links.
  1448. Shell links can be dangerous: just think about a link
  1449. [[shell:rm -rf ~/*][Google Search]]
  1450. This link would show up in your Org-mode document as \"Google Search\",
  1451. but really it would remove your entire home directory.
  1452. Therefore we advise against setting this variable to nil.
  1453. Just change it to `y-or-n-p' if you want to confirm with a
  1454. single keystroke rather than having to type \"yes\"."
  1455. :group 'org-link-follow
  1456. :type '(choice
  1457. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1458. (const :tag "with y-or-n (faster)" y-or-n-p)
  1459. (const :tag "no confirmation (dangerous)" nil)))
  1460. (put 'org-confirm-shell-link-function
  1461. 'safe-local-variable
  1462. #'(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1463. (defcustom org-confirm-shell-link-not-regexp ""
  1464. "A regexp to skip confirmation for shell links."
  1465. :group 'org-link-follow
  1466. :version "24.1"
  1467. :type 'regexp)
  1468. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1469. "Non-nil means ask for confirmation before executing Emacs Lisp links.
  1470. Elisp links can be dangerous: just think about a link
  1471. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1472. This link would show up in your Org-mode document as \"Google Search\",
  1473. but really it would remove your entire home directory.
  1474. Therefore we advise against setting this variable to nil.
  1475. Just change it to `y-or-n-p' if you want to confirm with a
  1476. single keystroke rather than having to type \"yes\"."
  1477. :group 'org-link-follow
  1478. :type '(choice
  1479. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1480. (const :tag "with y-or-n (faster)" y-or-n-p)
  1481. (const :tag "no confirmation (dangerous)" nil)))
  1482. (put 'org-confirm-shell-link-function
  1483. 'safe-local-variable
  1484. #'(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1485. (defcustom org-confirm-elisp-link-not-regexp ""
  1486. "A regexp to skip confirmation for Elisp links."
  1487. :group 'org-link-follow
  1488. :version "24.1"
  1489. :type 'regexp)
  1490. (defconst org-file-apps-defaults-gnu
  1491. '((remote . emacs)
  1492. (system . mailcap)
  1493. (t . mailcap))
  1494. "Default file applications on a UNIX or GNU/Linux system.
  1495. See `org-file-apps'.")
  1496. (defconst org-file-apps-defaults-macosx
  1497. '((remote . emacs)
  1498. (t . "open %s")
  1499. (system . "open %s")
  1500. ("ps.gz" . "gv %s")
  1501. ("eps.gz" . "gv %s")
  1502. ("dvi" . "xdvi %s")
  1503. ("fig" . "xfig %s"))
  1504. "Default file applications on a MacOS X system.
  1505. The system \"open\" is known as a default, but we use X11 applications
  1506. for some files for which the OS does not have a good default.
  1507. See `org-file-apps'.")
  1508. (defconst org-file-apps-defaults-windowsnt
  1509. (list
  1510. '(remote . emacs)
  1511. (cons t
  1512. (list (if (featurep 'xemacs)
  1513. 'mswindows-shell-execute
  1514. 'w32-shell-execute)
  1515. "open" 'file))
  1516. (cons 'system
  1517. (list (if (featurep 'xemacs)
  1518. 'mswindows-shell-execute
  1519. 'w32-shell-execute)
  1520. "open" 'file)))
  1521. "Default file applications on a Windows NT system.
  1522. The system \"open\" is used for most files.
  1523. See `org-file-apps'.")
  1524. (defcustom org-file-apps
  1525. '(
  1526. (auto-mode . emacs)
  1527. ("\\.mm\\'" . default)
  1528. ("\\.x?html?\\'" . default)
  1529. ("\\.pdf\\'" . default)
  1530. )
  1531. "External applications for opening `file:path' items in a document.
  1532. Org-mode uses system defaults for different file types, but
  1533. you can use this variable to set the application for a given file
  1534. extension. The entries in this list are cons cells where the car identifies
  1535. files and the cdr the corresponding command. Possible values for the
  1536. file identifier are
  1537. \"string\" A string as a file identifier can be interpreted in different
  1538. ways, depending on its contents:
  1539. - Alphanumeric characters only:
  1540. Match links with this file extension.
  1541. Example: (\"pdf\" . \"evince %s\")
  1542. to open PDFs with evince.
  1543. - Regular expression: Match links where the
  1544. filename matches the regexp. If you want to
  1545. use groups here, use shy groups.
  1546. Example: (\"\\.x?html\\'\" . \"firefox %s\")
  1547. (\"\\(?:xhtml\\|html\\)\" . \"firefox %s\")
  1548. to open *.html and *.xhtml with firefox.
  1549. - Regular expression which contains (non-shy) groups:
  1550. Match links where the whole link, including \"::\", and
  1551. anything after that, matches the regexp.
  1552. In a custom command string, %1, %2, etc. are replaced with
  1553. the parts of the link that were matched by the groups.
  1554. For backwards compatibility, if a command string is given
  1555. that does not use any of the group matches, this case is
  1556. handled identically to the second one (i.e. match against
  1557. file name only).
  1558. In a custom lisp form, you can access the group matches with
  1559. (match-string n link).
  1560. Example: (\"\\.pdf::\\(\\d+\\)\\'\" . \"evince -p %1 %s\")
  1561. to open [[file:document.pdf::5]] with evince at page 5.
  1562. `directory' Matches a directory
  1563. `remote' Matches a remote file, accessible through tramp or efs.
  1564. Remote files most likely should be visited through Emacs
  1565. because external applications cannot handle such paths.
  1566. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1567. so all files Emacs knows how to handle. Using this with
  1568. command `emacs' will open most files in Emacs. Beware that this
  1569. will also open html files inside Emacs, unless you add
  1570. (\"html\" . default) to the list as well.
  1571. t Default for files not matched by any of the other options.
  1572. `system' The system command to open files, like `open' on Windows
  1573. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1574. that will be selected if you call `C-c C-o' with a double
  1575. \\[universal-argument] \\[universal-argument] prefix.
  1576. Possible values for the command are:
  1577. `emacs' The file will be visited by the current Emacs process.
  1578. `default' Use the default application for this file type, which is the
  1579. association for t in the list, most likely in the system-specific
  1580. part.
  1581. This can be used to overrule an unwanted setting in the
  1582. system-specific variable.
  1583. `system' Use the system command for opening files, like \"open\".
  1584. This command is specified by the entry whose car is `system'.
  1585. Most likely, the system-specific version of this variable
  1586. does define this command, but you can overrule/replace it
  1587. here.
  1588. string A command to be executed by a shell; %s will be replaced
  1589. by the path to the file.
  1590. sexp A Lisp form which will be evaluated. The file path will
  1591. be available in the Lisp variable `file'.
  1592. For more examples, see the system specific constants
  1593. `org-file-apps-defaults-macosx'
  1594. `org-file-apps-defaults-windowsnt'
  1595. `org-file-apps-defaults-gnu'."
  1596. :group 'org-link-follow
  1597. :type '(repeat
  1598. (cons (choice :value ""
  1599. (string :tag "Extension")
  1600. (const :tag "System command to open files" system)
  1601. (const :tag "Default for unrecognized files" t)
  1602. (const :tag "Remote file" remote)
  1603. (const :tag "Links to a directory" directory)
  1604. (const :tag "Any files that have Emacs modes"
  1605. auto-mode))
  1606. (choice :value ""
  1607. (const :tag "Visit with Emacs" emacs)
  1608. (const :tag "Use default" default)
  1609. (const :tag "Use the system command" system)
  1610. (string :tag "Command")
  1611. (sexp :tag "Lisp form")))))
  1612. (defgroup org-refile nil
  1613. "Options concerning refiling entries in Org-mode."
  1614. :tag "Org Refile"
  1615. :group 'org)
  1616. (defcustom org-directory "~/org"
  1617. "Directory with org files.
  1618. This is just a default location to look for Org files. There is no need
  1619. at all to put your files into this directory. It is only used in the
  1620. following situations:
  1621. 1. When a remember template specifies a target file that is not an
  1622. absolute path. The path will then be interpreted relative to
  1623. `org-directory'
  1624. 2. When a remember note is filed away in an interactive way (when exiting the
  1625. note buffer with `C-1 C-c C-c'. The user is prompted for an org file,
  1626. with `org-directory' as the default path."
  1627. :group 'org-refile
  1628. :group 'org-remember
  1629. :type 'directory)
  1630. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1631. "Default target for storing notes.
  1632. Used as a fall back file for org-remember.el and org-capture.el, for
  1633. templates that do not specify a target file."
  1634. :group 'org-refile
  1635. :group 'org-remember
  1636. :type '(choice
  1637. (const :tag "Default from remember-data-file" nil)
  1638. file))
  1639. (defcustom org-goto-interface 'outline
  1640. "The default interface to be used for `org-goto'.
  1641. Allowed values are:
  1642. outline The interface shows an outline of the relevant file
  1643. and the correct heading is found by moving through
  1644. the outline or by searching with incremental search.
  1645. outline-path-completion Headlines in the current buffer are offered via
  1646. completion. This is the interface also used by
  1647. the refile command."
  1648. :group 'org-refile
  1649. :type '(choice
  1650. (const :tag "Outline" outline)
  1651. (const :tag "Outline-path-completion" outline-path-completion)))
  1652. (defcustom org-goto-max-level 5
  1653. "Maximum target level when running `org-goto' with refile interface."
  1654. :group 'org-refile
  1655. :type 'integer)
  1656. (defcustom org-reverse-note-order nil
  1657. "Non-nil means store new notes at the beginning of a file or entry.
  1658. When nil, new notes will be filed to the end of a file or entry.
  1659. This can also be a list with cons cells of regular expressions that
  1660. are matched against file names, and values."
  1661. :group 'org-remember
  1662. :group 'org-refile
  1663. :type '(choice
  1664. (const :tag "Reverse always" t)
  1665. (const :tag "Reverse never" nil)
  1666. (repeat :tag "By file name regexp"
  1667. (cons regexp boolean))))
  1668. (defcustom org-log-refile nil
  1669. "Information to record when a task is refiled.
  1670. Possible values are:
  1671. nil Don't add anything
  1672. time Add a time stamp to the task
  1673. note Prompt for a note and add it with template `org-log-note-headings'
  1674. This option can also be set with on a per-file-basis with
  1675. #+STARTUP: nologrefile
  1676. #+STARTUP: logrefile
  1677. #+STARTUP: lognoterefile
  1678. You can have local logging settings for a subtree by setting the LOGGING
  1679. property to one or more of these keywords.
  1680. When bulk-refiling from the agenda, the value `note' is forbidden and
  1681. will temporarily be changed to `time'."
  1682. :group 'org-refile
  1683. :group 'org-progress
  1684. :version "24.1"
  1685. :type '(choice
  1686. (const :tag "No logging" nil)
  1687. (const :tag "Record timestamp" time)
  1688. (const :tag "Record timestamp with note." note)))
  1689. (defcustom org-refile-targets nil
  1690. "Targets for refiling entries with \\[org-refile].
  1691. This is a list of cons cells. Each cell contains:
  1692. - a specification of the files to be considered, either a list of files,
  1693. or a symbol whose function or variable value will be used to retrieve
  1694. a file name or a list of file names. If you use `org-agenda-files' for
  1695. that, all agenda files will be scanned for targets. Nil means consider
  1696. headings in the current buffer.
  1697. - A specification of how to find candidate refile targets. This may be
  1698. any of:
  1699. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1700. This tag has to be present in all target headlines, inheritance will
  1701. not be considered.
  1702. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1703. todo keyword.
  1704. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1705. headlines that are refiling targets.
  1706. - a cons cell (:level . N). Any headline of level N is considered a target.
  1707. Note that, when `org-odd-levels-only' is set, level corresponds to
  1708. order in hierarchy, not to the number of stars.
  1709. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1710. Note that, when `org-odd-levels-only' is set, level corresponds to
  1711. order in hierarchy, not to the number of stars.
  1712. Each element of this list generates a set of possible targets.
  1713. The union of these sets is presented (with completion) to
  1714. the user by `org-refile'.
  1715. You can set the variable `org-refile-target-verify-function' to a function
  1716. to verify each headline found by the simple criteria above.
  1717. When this variable is nil, all top-level headlines in the current buffer
  1718. are used, equivalent to the value `((nil . (:level . 1))'."
  1719. :group 'org-refile
  1720. :type '(repeat
  1721. (cons
  1722. (choice :value org-agenda-files
  1723. (const :tag "All agenda files" org-agenda-files)
  1724. (const :tag "Current buffer" nil)
  1725. (function) (variable) (file))
  1726. (choice :tag "Identify target headline by"
  1727. (cons :tag "Specific tag" (const :value :tag) (string))
  1728. (cons :tag "TODO keyword" (const :value :todo) (string))
  1729. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  1730. (cons :tag "Level number" (const :value :level) (integer))
  1731. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  1732. (defcustom org-refile-target-verify-function nil
  1733. "Function to verify if the headline at point should be a refile target.
  1734. The function will be called without arguments, with point at the
  1735. beginning of the headline. It should return t and leave point
  1736. where it is if the headline is a valid target for refiling.
  1737. If the target should not be selected, the function must return nil.
  1738. In addition to this, it may move point to a place from where the search
  1739. should be continued. For example, the function may decide that the entire
  1740. subtree of the current entry should be excluded and move point to the end
  1741. of the subtree."
  1742. :group 'org-refile
  1743. :type 'function)
  1744. (defcustom org-refile-use-cache nil
  1745. "Non-nil means cache refile targets to speed up the process.
  1746. The cache for a particular file will be updated automatically when
  1747. the buffer has been killed, or when any of the marker used for flagging
  1748. refile targets no longer points at a live buffer.
  1749. If you have added new entries to a buffer that might themselves be targets,
  1750. you need to clear the cache manually by pressing `C-0 C-c C-w' or, if you
  1751. find that easier, `C-u C-u C-u C-c C-w'."
  1752. :group 'org-refile
  1753. :version "24.1"
  1754. :type 'boolean)
  1755. (defcustom org-refile-use-outline-path nil
  1756. "Non-nil means provide refile targets as paths.
  1757. So a level 3 headline will be available as level1/level2/level3.
  1758. When the value is `file', also include the file name (without directory)
  1759. into the path. In this case, you can also stop the completion after
  1760. the file name, to get entries inserted as top level in the file.
  1761. When `full-file-path', include the full file path."
  1762. :group 'org-refile
  1763. :type '(choice
  1764. (const :tag "Not" nil)
  1765. (const :tag "Yes" t)
  1766. (const :tag "Start with file name" file)
  1767. (const :tag "Start with full file path" full-file-path)))
  1768. (defcustom org-outline-path-complete-in-steps t
  1769. "Non-nil means complete the outline path in hierarchical steps.
  1770. When Org-mode uses the refile interface to select an outline path
  1771. \(see variable `org-refile-use-outline-path'), the completion of
  1772. the path can be done is a single go, or if can be done in steps down
  1773. the headline hierarchy. Going in steps is probably the best if you
  1774. do not use a special completion package like `ido' or `icicles'.
  1775. However, when using these packages, going in one step can be very
  1776. fast, while still showing the whole path to the entry."
  1777. :group 'org-refile
  1778. :type 'boolean)
  1779. (defcustom org-refile-allow-creating-parent-nodes nil
  1780. "Non-nil means allow to create new nodes as refile targets.
  1781. New nodes are then created by adding \"/new node name\" to the completion
  1782. of an existing node. When the value of this variable is `confirm',
  1783. new node creation must be confirmed by the user (recommended)
  1784. When nil, the completion must match an existing entry.
  1785. Note that, if the new heading is not seen by the criteria
  1786. listed in `org-refile-targets', multiple instances of the same
  1787. heading would be created by trying again to file under the new
  1788. heading."
  1789. :group 'org-refile
  1790. :type '(choice
  1791. (const :tag "Never" nil)
  1792. (const :tag "Always" t)
  1793. (const :tag "Prompt for confirmation" confirm)))
  1794. (defcustom org-refile-active-region-within-subtree nil
  1795. "Non-nil means also refile active region within a subtree.
  1796. By default `org-refile' doesn't allow refiling regions if they
  1797. don't contain a set of subtrees, but it might be convenient to
  1798. do so sometimes: in that case, the first line of the region is
  1799. converted to a headline before refiling."
  1800. :group 'org-refile
  1801. :version "24.1"
  1802. :type 'boolean)
  1803. (defgroup org-todo nil
  1804. "Options concerning TODO items in Org-mode."
  1805. :tag "Org TODO"
  1806. :group 'org)
  1807. (defgroup org-progress nil
  1808. "Options concerning Progress logging in Org-mode."
  1809. :tag "Org Progress"
  1810. :group 'org-time)
  1811. (defvar org-todo-interpretation-widgets
  1812. '((:tag "Sequence (cycling hits every state)" sequence)
  1813. (:tag "Type (cycling directly to DONE)" type))
  1814. "The available interpretation symbols for customizing `org-todo-keywords'.
  1815. Interested libraries should add to this list.")
  1816. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1817. "List of TODO entry keyword sequences and their interpretation.
  1818. \\<org-mode-map>This is a list of sequences.
  1819. Each sequence starts with a symbol, either `sequence' or `type',
  1820. indicating if the keywords should be interpreted as a sequence of
  1821. action steps, or as different types of TODO items. The first
  1822. keywords are states requiring action - these states will select a headline
  1823. for inclusion into the global TODO list Org-mode produces. If one of
  1824. the \"keywords\" is the vertical bar, \"|\", the remaining keywords
  1825. signify that no further action is necessary. If \"|\" is not found,
  1826. the last keyword is treated as the only DONE state of the sequence.
  1827. The command \\[org-todo] cycles an entry through these states, and one
  1828. additional state where no keyword is present. For details about this
  1829. cycling, see the manual.
  1830. TODO keywords and interpretation can also be set on a per-file basis with
  1831. the special #+SEQ_TODO and #+TYP_TODO lines.
  1832. Each keyword can optionally specify a character for fast state selection
  1833. \(in combination with the variable `org-use-fast-todo-selection')
  1834. and specifiers for state change logging, using the same syntax
  1835. that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
  1836. that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1837. indicates to record a time stamp each time this state is selected.
  1838. Each keyword may also specify if a timestamp or a note should be
  1839. recorded when entering or leaving the state, by adding additional
  1840. characters in the parenthesis after the keyword. This looks like this:
  1841. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1842. record only the time of the state change. With X and Y being either
  1843. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1844. Y when leaving the state if and only if the *target* state does not
  1845. define X. You may omit any of the fast-selection key or X or /Y,
  1846. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1847. For backward compatibility, this variable may also be just a list
  1848. of keywords - in this case the interpretation (sequence or type) will be
  1849. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1850. :group 'org-todo
  1851. :group 'org-keywords
  1852. :type '(choice
  1853. (repeat :tag "Old syntax, just keywords"
  1854. (string :tag "Keyword"))
  1855. (repeat :tag "New syntax"
  1856. (cons
  1857. (choice
  1858. :tag "Interpretation"
  1859. ;;Quick and dirty way to see
  1860. ;;`org-todo-interpretations'. This takes the
  1861. ;;place of item arguments
  1862. :convert-widget
  1863. (lambda (widget)
  1864. (widget-put widget
  1865. :args (mapcar
  1866. #'(lambda (x)
  1867. (widget-convert
  1868. (cons 'const x)))
  1869. org-todo-interpretation-widgets))
  1870. widget))
  1871. (repeat
  1872. (string :tag "Keyword"))))))
  1873. (defvar org-todo-keywords-1 nil
  1874. "All TODO and DONE keywords active in a buffer.")
  1875. (make-variable-buffer-local 'org-todo-keywords-1)
  1876. (defvar org-todo-keywords-for-agenda nil)
  1877. (defvar org-done-keywords-for-agenda nil)
  1878. (defvar org-drawers-for-agenda nil)
  1879. (defvar org-todo-keyword-alist-for-agenda nil)
  1880. (defvar org-tag-alist-for-agenda nil)
  1881. (defvar org-agenda-contributing-files nil)
  1882. (defvar org-not-done-keywords nil)
  1883. (make-variable-buffer-local 'org-not-done-keywords)
  1884. (defvar org-done-keywords nil)
  1885. (make-variable-buffer-local 'org-done-keywords)
  1886. (defvar org-todo-heads nil)
  1887. (make-variable-buffer-local 'org-todo-heads)
  1888. (defvar org-todo-sets nil)
  1889. (make-variable-buffer-local 'org-todo-sets)
  1890. (defvar org-todo-log-states nil)
  1891. (make-variable-buffer-local 'org-todo-log-states)
  1892. (defvar org-todo-kwd-alist nil)
  1893. (make-variable-buffer-local 'org-todo-kwd-alist)
  1894. (defvar org-todo-key-alist nil)
  1895. (make-variable-buffer-local 'org-todo-key-alist)
  1896. (defvar org-todo-key-trigger nil)
  1897. (make-variable-buffer-local 'org-todo-key-trigger)
  1898. (defcustom org-todo-interpretation 'sequence
  1899. "Controls how TODO keywords are interpreted.
  1900. This variable is in principle obsolete and is only used for
  1901. backward compatibility, if the interpretation of todo keywords is
  1902. not given already in `org-todo-keywords'. See that variable for
  1903. more information."
  1904. :group 'org-todo
  1905. :group 'org-keywords
  1906. :type '(choice (const sequence)
  1907. (const type)))
  1908. (defcustom org-use-fast-todo-selection t
  1909. "Non-nil means use the fast todo selection scheme with C-c C-t.
  1910. This variable describes if and under what circumstances the cycling
  1911. mechanism for TODO keywords will be replaced by a single-key, direct
  1912. selection scheme.
  1913. When nil, fast selection is never used.
  1914. When the symbol `prefix', it will be used when `org-todo' is called with
  1915. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1916. in an agenda buffer.
  1917. When t, fast selection is used by default. In this case, the prefix
  1918. argument forces cycling instead.
  1919. In all cases, the special interface is only used if access keys have actually
  1920. been assigned by the user, i.e. if keywords in the configuration are followed
  1921. by a letter in parenthesis, like TODO(t)."
  1922. :group 'org-todo
  1923. :type '(choice
  1924. (const :tag "Never" nil)
  1925. (const :tag "By default" t)
  1926. (const :tag "Only with C-u C-c C-t" prefix)))
  1927. (defcustom org-provide-todo-statistics t
  1928. "Non-nil means update todo statistics after insert and toggle.
  1929. ALL-HEADLINES means update todo statistics by including headlines
  1930. with no TODO keyword as well, counting them as not done.
  1931. A list of TODO keywords means the same, but skip keywords that are
  1932. not in this list.
  1933. When this is set, todo statistics is updated in the parent of the
  1934. current entry each time a todo state is changed."
  1935. :group 'org-todo
  1936. :type '(choice
  1937. (const :tag "Yes, only for TODO entries" t)
  1938. (const :tag "Yes, including all entries" 'all-headlines)
  1939. (repeat :tag "Yes, for TODOs in this list"
  1940. (string :tag "TODO keyword"))
  1941. (other :tag "No TODO statistics" nil)))
  1942. (defcustom org-hierarchical-todo-statistics t
  1943. "Non-nil means TODO statistics covers just direct children.
  1944. When nil, all entries in the subtree are considered.
  1945. This has only an effect if `org-provide-todo-statistics' is set.
  1946. To set this to nil for only a single subtree, use a COOKIE_DATA
  1947. property and include the word \"recursive\" into the value."
  1948. :group 'org-todo
  1949. :type 'boolean)
  1950. (defcustom org-after-todo-state-change-hook nil
  1951. "Hook which is run after the state of a TODO item was changed.
  1952. The new state (a string with a TODO keyword, or nil) is available in the
  1953. Lisp variable `state'."
  1954. :group 'org-todo
  1955. :type 'hook)
  1956. (defvar org-blocker-hook nil
  1957. "Hook for functions that are allowed to block a state change.
  1958. Each function gets as its single argument a property list, see
  1959. `org-trigger-hook' for more information about this list.
  1960. If any of the functions in this hook returns nil, the state change
  1961. is blocked.")
  1962. (defvar org-trigger-hook nil
  1963. "Hook for functions that are triggered by a state change.
  1964. Each function gets as its single argument a property list with at least
  1965. the following elements:
  1966. (:type type-of-change :position pos-at-entry-start
  1967. :from old-state :to new-state)
  1968. Depending on the type, more properties may be present.
  1969. This mechanism is currently implemented for:
  1970. TODO state changes
  1971. ------------------
  1972. :type todo-state-change
  1973. :from previous state (keyword as a string), or nil, or a symbol
  1974. 'todo' or 'done', to indicate the general type of state.
  1975. :to new state, like in :from")
  1976. (defcustom org-enforce-todo-dependencies nil
  1977. "Non-nil means undone TODO entries will block switching the parent to DONE.
  1978. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  1979. be blocked if any prior sibling is not yet done.
  1980. Finally, if the parent is blocked because of ordered siblings of its own,
  1981. the child will also be blocked.
  1982. This variable needs to be set before org.el is loaded, and you need to
  1983. restart Emacs after a change to make the change effective. The only way
  1984. to change is while Emacs is running is through the customize interface."
  1985. :set (lambda (var val)
  1986. (set var val)
  1987. (if val
  1988. (add-hook 'org-blocker-hook
  1989. 'org-block-todo-from-children-or-siblings-or-parent)
  1990. (remove-hook 'org-blocker-hook
  1991. 'org-block-todo-from-children-or-siblings-or-parent)))
  1992. :group 'org-todo
  1993. :type 'boolean)
  1994. (defcustom org-enforce-todo-checkbox-dependencies nil
  1995. "Non-nil means unchecked boxes will block switching the parent to DONE.
  1996. When this is nil, checkboxes have no influence on switching TODO states.
  1997. When non-nil, you first need to check off all check boxes before the TODO
  1998. entry can be switched to DONE.
  1999. This variable needs to be set before org.el is loaded, and you need to
  2000. restart Emacs after a change to make the change effective. The only way
  2001. to change is while Emacs is running is through the customize interface."
  2002. :set (lambda (var val)
  2003. (set var val)
  2004. (if val
  2005. (add-hook 'org-blocker-hook
  2006. 'org-block-todo-from-checkboxes)
  2007. (remove-hook 'org-blocker-hook
  2008. 'org-block-todo-from-checkboxes)))
  2009. :group 'org-todo
  2010. :type 'boolean)
  2011. (defcustom org-treat-insert-todo-heading-as-state-change nil
  2012. "Non-nil means inserting a TODO heading is treated as state change.
  2013. So when the command \\[org-insert-todo-heading] is used, state change
  2014. logging will apply if appropriate. When nil, the new TODO item will
  2015. be inserted directly, and no logging will take place."
  2016. :group 'org-todo
  2017. :type 'boolean)
  2018. (defcustom org-treat-S-cursor-todo-selection-as-state-change t
  2019. "Non-nil means switching TODO states with S-cursor counts as state change.
  2020. This is the default behavior. However, setting this to nil allows a
  2021. convenient way to select a TODO state and bypass any logging associated
  2022. with that."
  2023. :group 'org-todo
  2024. :type 'boolean)
  2025. (defcustom org-todo-state-tags-triggers nil
  2026. "Tag changes that should be triggered by TODO state changes.
  2027. This is a list. Each entry is
  2028. (state-change (tag . flag) .......)
  2029. State-change can be a string with a state, and empty string to indicate the
  2030. state that has no TODO keyword, or it can be one of the symbols `todo'
  2031. or `done', meaning any not-done or done state, respectively."
  2032. :group 'org-todo
  2033. :group 'org-tags
  2034. :type '(repeat
  2035. (cons (choice :tag "When changing to"
  2036. (const :tag "Not-done state" todo)
  2037. (const :tag "Done state" done)
  2038. (string :tag "State"))
  2039. (repeat
  2040. (cons :tag "Tag action"
  2041. (string :tag "Tag")
  2042. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  2043. (defcustom org-log-done nil
  2044. "Information to record when a task moves to the DONE state.
  2045. Possible values are:
  2046. nil Don't add anything, just change the keyword
  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: nologdone
  2051. #+STARTUP: logdone
  2052. #+STARTUP: lognotedone
  2053. You can have local logging settings for a subtree by setting the LOGGING
  2054. property to one or more of these keywords."
  2055. :group 'org-todo
  2056. :group 'org-progress
  2057. :type '(choice
  2058. (const :tag "No logging" nil)
  2059. (const :tag "Record CLOSED timestamp" time)
  2060. (const :tag "Record CLOSED timestamp with note." note)))
  2061. ;; Normalize old uses of org-log-done.
  2062. (cond
  2063. ((eq org-log-done t) (setq org-log-done 'time))
  2064. ((and (listp org-log-done) (memq 'done org-log-done))
  2065. (setq org-log-done 'note)))
  2066. (defcustom org-log-reschedule nil
  2067. "Information to record when the scheduling date of a tasks is modified.
  2068. Possible values are:
  2069. nil Don't add anything, just change the date
  2070. time Add a time stamp to the task
  2071. note Prompt for a note and add it with template `org-log-note-headings'
  2072. This option can also be set with on a per-file-basis with
  2073. #+STARTUP: nologreschedule
  2074. #+STARTUP: logreschedule
  2075. #+STARTUP: lognotereschedule"
  2076. :group 'org-todo
  2077. :group 'org-progress
  2078. :type '(choice
  2079. (const :tag "No logging" nil)
  2080. (const :tag "Record timestamp" time)
  2081. (const :tag "Record timestamp with note." note)))
  2082. (defcustom org-log-redeadline nil
  2083. "Information to record when the deadline date of a tasks is modified.
  2084. Possible values are:
  2085. nil Don't add anything, just change the date
  2086. time Add a time stamp to the task
  2087. note Prompt for a note and add it with template `org-log-note-headings'
  2088. This option can also be set with on a per-file-basis with
  2089. #+STARTUP: nologredeadline
  2090. #+STARTUP: logredeadline
  2091. #+STARTUP: lognoteredeadline
  2092. You can have local logging settings for a subtree by setting the LOGGING
  2093. property to one or more of these keywords."
  2094. :group 'org-todo
  2095. :group 'org-progress
  2096. :type '(choice
  2097. (const :tag "No logging" nil)
  2098. (const :tag "Record timestamp" time)
  2099. (const :tag "Record timestamp with note." note)))
  2100. (defcustom org-log-note-clock-out nil
  2101. "Non-nil means record a note when clocking out of an item.
  2102. This can also be configured on a per-file basis by adding one of
  2103. the following lines anywhere in the buffer:
  2104. #+STARTUP: lognoteclock-out
  2105. #+STARTUP: nolognoteclock-out"
  2106. :group 'org-todo
  2107. :group 'org-progress
  2108. :type 'boolean)
  2109. (defcustom org-log-done-with-time t
  2110. "Non-nil means the CLOSED time stamp will contain date and time.
  2111. When nil, only the date will be recorded."
  2112. :group 'org-progress
  2113. :type 'boolean)
  2114. (defcustom org-log-note-headings
  2115. '((done . "CLOSING NOTE %t")
  2116. (state . "State %-12s from %-12S %t")
  2117. (note . "Note taken on %t")
  2118. (reschedule . "Rescheduled from %S on %t")
  2119. (delschedule . "Not scheduled, was %S on %t")
  2120. (redeadline . "New deadline from %S on %t")
  2121. (deldeadline . "Removed deadline, was %S on %t")
  2122. (refile . "Refiled on %t")
  2123. (clock-out . ""))
  2124. "Headings for notes added to entries.
  2125. The value is an alist, with the car being a symbol indicating the note
  2126. context, and the cdr is the heading to be used. The heading may also be the
  2127. empty string.
  2128. %t in the heading will be replaced by a time stamp.
  2129. %T will be an active time stamp instead the default inactive one
  2130. %d will be replaced by a short-format time stamp.
  2131. %D will be replaced by an active short-format time stamp.
  2132. %s will be replaced by the new TODO state, in double quotes.
  2133. %S will be replaced by the old TODO state, in double quotes.
  2134. %u will be replaced by the user name.
  2135. %U will be replaced by the full user name.
  2136. In fact, it is not a good idea to change the `state' entry, because
  2137. agenda log mode depends on the format of these entries."
  2138. :group 'org-todo
  2139. :group 'org-progress
  2140. :type '(list :greedy t
  2141. (cons (const :tag "Heading when closing an item" done) string)
  2142. (cons (const :tag
  2143. "Heading when changing todo state (todo sequence only)"
  2144. state) string)
  2145. (cons (const :tag "Heading when just taking a note" note) string)
  2146. (cons (const :tag "Heading when clocking out" clock-out) string)
  2147. (cons (const :tag "Heading when an item is no longer scheduled" delschedule) string)
  2148. (cons (const :tag "Heading when rescheduling" reschedule) string)
  2149. (cons (const :tag "Heading when changing deadline" redeadline) string)
  2150. (cons (const :tag "Heading when deleting a deadline" deldeadline) string)
  2151. (cons (const :tag "Heading when refiling" refile) string)))
  2152. (unless (assq 'note org-log-note-headings)
  2153. (push '(note . "%t") org-log-note-headings))
  2154. (defcustom org-log-into-drawer nil
  2155. "Non-nil means insert state change notes and time stamps into a drawer.
  2156. When nil, state changes notes will be inserted after the headline and
  2157. any scheduling and clock lines, but not inside a drawer.
  2158. The value of this variable should be the name of the drawer to use.
  2159. LOGBOOK is proposed as the default drawer for this purpose, you can
  2160. also set this to a string to define the drawer of your choice.
  2161. A value of t is also allowed, representing \"LOGBOOK\".
  2162. If this variable is set, `org-log-state-notes-insert-after-drawers'
  2163. will be ignored.
  2164. You can set the property LOG_INTO_DRAWER to overrule this setting for
  2165. a subtree."
  2166. :group 'org-todo
  2167. :group 'org-progress
  2168. :type '(choice
  2169. (const :tag "Not into a drawer" nil)
  2170. (const :tag "LOGBOOK" t)
  2171. (string :tag "Other")))
  2172. (if (fboundp 'defvaralias)
  2173. (defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer))
  2174. (defun org-log-into-drawer ()
  2175. "Return the value of `org-log-into-drawer', but let properties overrule.
  2176. If the current entry has or inherits a LOG_INTO_DRAWER property, it will be
  2177. used instead of the default value."
  2178. (let ((p (org-entry-get nil "LOG_INTO_DRAWER" 'inherit)))
  2179. (cond
  2180. ((or (not p) (equal p "nil")) org-log-into-drawer)
  2181. ((equal p "t") "LOGBOOK")
  2182. (t p))))
  2183. (defcustom org-log-state-notes-insert-after-drawers nil
  2184. "Non-nil means insert state change notes after any drawers in entry.
  2185. Only the drawers that *immediately* follow the headline and the
  2186. deadline/scheduled line are skipped.
  2187. When nil, insert notes right after the heading and perhaps the line
  2188. with deadline/scheduling if present.
  2189. This variable will have no effect if `org-log-into-drawer' is
  2190. set."
  2191. :group 'org-todo
  2192. :group 'org-progress
  2193. :type 'boolean)
  2194. (defcustom org-log-states-order-reversed t
  2195. "Non-nil means the latest state note will be directly after heading.
  2196. When nil, the state change notes will be ordered according to time."
  2197. :group 'org-todo
  2198. :group 'org-progress
  2199. :type 'boolean)
  2200. (defcustom org-todo-repeat-to-state nil
  2201. "The TODO state to which a repeater should return the repeating task.
  2202. By default this is the first task in a TODO sequence, or the previous state
  2203. in a TODO_TYP set. But you can specify another task here.
  2204. alternatively, set the :REPEAT_TO_STATE: property of the entry."
  2205. :group 'org-todo
  2206. :version "24.1"
  2207. :type '(choice (const :tag "Head of sequence" nil)
  2208. (string :tag "Specific state")))
  2209. (defcustom org-log-repeat 'time
  2210. "Non-nil means record moving through the DONE state when triggering repeat.
  2211. An auto-repeating task is immediately switched back to TODO when
  2212. marked DONE. If you are not logging state changes (by adding \"@\"
  2213. or \"!\" to the TODO keyword definition), or set `org-log-done' to
  2214. record a closing note, there will be no record of the task moving
  2215. through DONE. This variable forces taking a note anyway.
  2216. nil Don't force a record
  2217. time Record a time stamp
  2218. note Record a note
  2219. This option can also be set with on a per-file-basis with
  2220. #+STARTUP: logrepeat
  2221. #+STARTUP: lognoterepeat
  2222. #+STARTUP: nologrepeat
  2223. You can have local logging settings for a subtree by setting the LOGGING
  2224. property to one or more of these keywords."
  2225. :group 'org-todo
  2226. :group 'org-progress
  2227. :type '(choice
  2228. (const :tag "Don't force a record" nil)
  2229. (const :tag "Force recording the DONE state" time)
  2230. (const :tag "Force recording a note with the DONE state" note)))
  2231. (defgroup org-priorities nil
  2232. "Priorities in Org-mode."
  2233. :tag "Org Priorities"
  2234. :group 'org-todo)
  2235. (defcustom org-enable-priority-commands t
  2236. "Non-nil means priority commands are active.
  2237. When nil, these commands will be disabled, so that you never accidentally
  2238. set a priority."
  2239. :group 'org-priorities
  2240. :type 'boolean)
  2241. (defcustom org-highest-priority ?A
  2242. "The highest priority of TODO items. A character like ?A, ?B etc.
  2243. Must have a smaller ASCII number than `org-lowest-priority'."
  2244. :group 'org-priorities
  2245. :type 'character)
  2246. (defcustom org-lowest-priority ?C
  2247. "The lowest priority of TODO items. A character like ?A, ?B etc.
  2248. Must have a larger ASCII number than `org-highest-priority'."
  2249. :group 'org-priorities
  2250. :type 'character)
  2251. (defcustom org-default-priority ?B
  2252. "The default priority of TODO items.
  2253. This is the priority an item gets if no explicit priority is given.
  2254. When starting to cycle on an empty priority the first step in the cycle
  2255. depends on `org-priority-start-cycle-with-default'. The resulting first
  2256. step priority must not exceed the range from `org-highest-priority' to
  2257. `org-lowest-priority' which means that `org-default-priority' has to be
  2258. in this range exclusive or inclusive the range boundaries. Else the
  2259. first step refuses to set the default and the second will fall back
  2260. to (depending on the command used) the highest or lowest priority."
  2261. :group 'org-priorities
  2262. :type 'character)
  2263. (defcustom org-priority-start-cycle-with-default t
  2264. "Non-nil means start with default priority when starting to cycle.
  2265. When this is nil, the first step in the cycle will be (depending on the
  2266. command used) one higher or lower than the default priority.
  2267. See also `org-default-priority'."
  2268. :group 'org-priorities
  2269. :type 'boolean)
  2270. (defcustom org-get-priority-function nil
  2271. "Function to extract the priority from a string.
  2272. The string is normally the headline. If this is nil Org computes the
  2273. priority from the priority cookie like [#A] in the headline. It returns
  2274. an integer, increasing by 1000 for each priority level.
  2275. The user can set a different function here, which should take a string
  2276. as an argument and return the numeric priority."
  2277. :group 'org-priorities
  2278. :version "24.1"
  2279. :type 'function)
  2280. (defgroup org-time nil
  2281. "Options concerning time stamps and deadlines in Org-mode."
  2282. :tag "Org Time"
  2283. :group 'org)
  2284. (defcustom org-insert-labeled-timestamps-at-point nil
  2285. "Non-nil means SCHEDULED and DEADLINE timestamps are inserted at point.
  2286. When nil, these labeled time stamps are forces into the second line of an
  2287. entry, just after the headline. When scheduling from the global TODO list,
  2288. the time stamp will always be forced into the second line."
  2289. :group 'org-time
  2290. :type 'boolean)
  2291. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  2292. "Formats for `format-time-string' which are used for time stamps.
  2293. It is not recommended to change this constant.")
  2294. (defcustom org-time-stamp-rounding-minutes '(0 5)
  2295. "Number of minutes to round time stamps to.
  2296. These are two values, the first applies when first creating a time stamp.
  2297. The second applies when changing it with the commands `S-up' and `S-down'.
  2298. When changing the time stamp, this means that it will change in steps
  2299. of N minutes, as given by the second value.
  2300. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  2301. numbers should be factors of 60, so for example 5, 10, 15.
  2302. When this is larger than 1, you can still force an exact time stamp by using
  2303. a double prefix argument to a time stamp command like `C-c .' or `C-c !',
  2304. and by using a prefix arg to `S-up/down' to specify the exact number
  2305. of minutes to shift."
  2306. :group 'org-time
  2307. :get #'(lambda (var) ; Make sure both elements are there
  2308. (if (integerp (default-value var))
  2309. (list (default-value var) 5)
  2310. (default-value var)))
  2311. :type '(list
  2312. (integer :tag "when inserting times")
  2313. (integer :tag "when modifying times")))
  2314. ;; Normalize old customizations of this variable.
  2315. (when (integerp org-time-stamp-rounding-minutes)
  2316. (setq org-time-stamp-rounding-minutes
  2317. (list org-time-stamp-rounding-minutes
  2318. org-time-stamp-rounding-minutes)))
  2319. (defcustom org-display-custom-times nil
  2320. "Non-nil means overlay custom formats over all time stamps.
  2321. The formats are defined through the variable `org-time-stamp-custom-formats'.
  2322. To turn this on on a per-file basis, insert anywhere in the file:
  2323. #+STARTUP: customtime"
  2324. :group 'org-time
  2325. :set 'set-default
  2326. :type 'sexp)
  2327. (make-variable-buffer-local 'org-display-custom-times)
  2328. (defcustom org-time-stamp-custom-formats
  2329. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  2330. "Custom formats for time stamps. See `format-time-string' for the syntax.
  2331. These are overlaid over the default ISO format if the variable
  2332. `org-display-custom-times' is set. Time like %H:%M should be at the
  2333. end of the second format. The custom formats are also honored by export
  2334. commands, if custom time display is turned on at the time of export."
  2335. :group 'org-time
  2336. :type 'sexp)
  2337. (defun org-time-stamp-format (&optional long inactive)
  2338. "Get the right format for a time string."
  2339. (let ((f (if long (cdr org-time-stamp-formats)
  2340. (car org-time-stamp-formats))))
  2341. (if inactive
  2342. (concat "[" (substring f 1 -1) "]")
  2343. f)))
  2344. (defcustom org-time-clocksum-format "%d:%02d"
  2345. "The format string used when creating CLOCKSUM lines.
  2346. This is also used when org-mode generates a time duration."
  2347. :group 'org-time
  2348. :type 'string)
  2349. (defcustom org-time-clocksum-use-fractional nil
  2350. "If non-nil, \\[org-clock-display] uses fractional times.
  2351. org-mode generates a time duration."
  2352. :group 'org-time
  2353. :type 'boolean)
  2354. (defcustom org-time-clocksum-fractional-format "%.2f"
  2355. "The format string used when creating CLOCKSUM lines, or when
  2356. org-mode generates a time duration."
  2357. :group 'org-time
  2358. :type 'string)
  2359. (defcustom org-deadline-warning-days 14
  2360. "No. of days before expiration during which a deadline becomes active.
  2361. This variable governs the display in sparse trees and in the agenda.
  2362. When 0 or negative, it means use this number (the absolute value of it)
  2363. even if a deadline has a different individual lead time specified.
  2364. Custom commands can set this variable in the options section."
  2365. :group 'org-time
  2366. :group 'org-agenda-daily/weekly
  2367. :type 'integer)
  2368. (defcustom org-read-date-prefer-future t
  2369. "Non-nil means assume future for incomplete date input from user.
  2370. This affects the following situations:
  2371. 1. The user gives a month but not a year.
  2372. For example, if it is April and you enter \"feb 2\", this will be read
  2373. as Feb 2, *next* year. \"May 5\", however, will be this year.
  2374. 2. The user gives a day, but no month.
  2375. For example, if today is the 15th, and you enter \"3\", Org-mode will
  2376. read this as the third of *next* month. However, if you enter \"17\",
  2377. it will be considered as *this* month.
  2378. If you set this variable to the symbol `time', then also the following
  2379. will work:
  2380. 3. If the user gives a time, but no day. If the time is before now,
  2381. to will be interpreted as tomorrow.
  2382. Currently none of this works for ISO week specifications.
  2383. When this option is nil, the current day, month and year will always be
  2384. used as defaults.
  2385. See also `org-agenda-jump-prefer-future'."
  2386. :group 'org-time
  2387. :type '(choice
  2388. (const :tag "Never" nil)
  2389. (const :tag "Check month and day" t)
  2390. (const :tag "Check month, day, and time" time)))
  2391. (defcustom org-agenda-jump-prefer-future 'org-read-date-prefer-future
  2392. "Should the agenda jump command prefer the future for incomplete dates?
  2393. The default is to do the same as configured in `org-read-date-prefer-future'.
  2394. But you can also set a deviating value here.
  2395. This may t or nil, or the symbol `org-read-date-prefer-future'."
  2396. :group 'org-agenda
  2397. :group 'org-time
  2398. :version "24.1"
  2399. :type '(choice
  2400. (const :tag "Use org-read-date-prefer-future"
  2401. org-read-date-prefer-future)
  2402. (const :tag "Never" nil)
  2403. (const :tag "Always" t)))
  2404. (defcustom org-read-date-force-compatible-dates t
  2405. "Should date/time prompt force dates that are guaranteed to work in Emacs?
  2406. Depending on the system Emacs is running on, certain dates cannot
  2407. be represented with the type used internally to represent time.
  2408. Dates between 1970-1-1 and 2038-1-1 can always be represented
  2409. correctly. Some systems allow for earlier dates, some for later,
  2410. some for both. One way to find out it to insert any date into an
  2411. Org buffer, putting the cursor on the year and hitting S-up and
  2412. S-down to test the range.
  2413. When this variable is set to t, the date/time prompt will not let
  2414. you specify dates outside the 1970-2037 range, so it is certain that
  2415. these dates will work in whatever version of Emacs you are
  2416. running, and also that you can move a file from one Emacs implementation
  2417. to another. WHenever Org is forcing the year for you, it will display
  2418. a message and beep.
  2419. When this variable is nil, Org will check if the date is
  2420. representable in the specific Emacs implementation you are using.
  2421. If not, it will force a year, usually the current year, and beep
  2422. to remind you. Currently this setting is not recommended because
  2423. the likelihood that you will open your Org files in an Emacs that
  2424. has limited date range is not negligible.
  2425. A workaround for this problem is to use diary sexp dates for time
  2426. stamps outside of this range."
  2427. :group 'org-time
  2428. :version "24.1"
  2429. :type 'boolean)
  2430. (defcustom org-read-date-display-live t
  2431. "Non-nil means display current interpretation of date prompt live.
  2432. This display will be in an overlay, in the minibuffer."
  2433. :group 'org-time
  2434. :type 'boolean)
  2435. (defcustom org-read-date-popup-calendar t
  2436. "Non-nil means pop up a calendar when prompting for a date.
  2437. In the calendar, the date can be selected with mouse-1. However, the
  2438. minibuffer will also be active, and you can simply enter the date as well.
  2439. When nil, only the minibuffer will be available."
  2440. :group 'org-time
  2441. :type 'boolean)
  2442. (if (fboundp 'defvaralias)
  2443. (defvaralias 'org-popup-calendar-for-date-prompt
  2444. 'org-read-date-popup-calendar))
  2445. (defcustom org-read-date-minibuffer-setup-hook nil
  2446. "Hook to be used to set up keys for the date/time interface.
  2447. Add key definitions to `minibuffer-local-map', which will be a temporary
  2448. copy."
  2449. :group 'org-time
  2450. :type 'hook)
  2451. (defcustom org-extend-today-until 0
  2452. "The hour when your day really ends. Must be an integer.
  2453. This has influence for the following applications:
  2454. - When switching the agenda to \"today\". It it is still earlier than
  2455. the time given here, the day recognized as TODAY is actually yesterday.
  2456. - When a date is read from the user and it is still before the time given
  2457. here, the current date and time will be assumed to be yesterday, 23:59.
  2458. Also, timestamps inserted in remember templates follow this rule.
  2459. IMPORTANT: This is a feature whose implementation is and likely will
  2460. remain incomplete. Really, it is only here because past midnight seems to
  2461. be the favorite working time of John Wiegley :-)"
  2462. :group 'org-time
  2463. :type 'integer)
  2464. (defcustom org-use-effective-time nil
  2465. "If non-nil, consider `org-extend-today-until' when creating timestamps.
  2466. For example, if `org-extend-today-until' is 8, and it's 4am, then the
  2467. \"effective time\" of any timestamps between midnight and 8am will be
  2468. 23:59 of the previous day."
  2469. :group 'boolean
  2470. :version "24.1"
  2471. :type 'integer)
  2472. (defcustom org-edit-timestamp-down-means-later nil
  2473. "Non-nil means S-down will increase the time in a time stamp.
  2474. When nil, S-up will increase."
  2475. :group 'org-time
  2476. :type 'boolean)
  2477. (defcustom org-calendar-follow-timestamp-change t
  2478. "Non-nil means make the calendar window follow timestamp changes.
  2479. When a timestamp is modified and the calendar window is visible, it will be
  2480. moved to the new date."
  2481. :group 'org-time
  2482. :type 'boolean)
  2483. (defgroup org-tags nil
  2484. "Options concerning tags in Org-mode."
  2485. :tag "Org Tags"
  2486. :group 'org)
  2487. (defcustom org-tag-alist nil
  2488. "List of tags allowed in Org-mode files.
  2489. When this list is nil, Org-mode will base TAG input on what is already in the
  2490. buffer.
  2491. The value of this variable is an alist, the car of each entry must be a
  2492. keyword as a string, the cdr may be a character that is used to select
  2493. that tag through the fast-tag-selection interface.
  2494. See the manual for details."
  2495. :group 'org-tags
  2496. :type '(repeat
  2497. (choice
  2498. (cons (string :tag "Tag name")
  2499. (character :tag "Access char"))
  2500. (list :tag "Start radio group"
  2501. (const :startgroup)
  2502. (option (string :tag "Group description")))
  2503. (list :tag "End radio group"
  2504. (const :endgroup)
  2505. (option (string :tag "Group description")))
  2506. (const :tag "New line" (:newline)))))
  2507. (defcustom org-tag-persistent-alist nil
  2508. "List of tags that will always appear in all Org-mode files.
  2509. This is in addition to any in buffer settings or customizations
  2510. of `org-tag-alist'.
  2511. When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
  2512. The value of this variable is an alist, the car of each entry must be a
  2513. keyword as a string, the cdr may be a character that is used to select
  2514. that tag through the fast-tag-selection interface.
  2515. See the manual for details.
  2516. To disable these tags on a per-file basis, insert anywhere in the file:
  2517. #+STARTUP: noptag"
  2518. :group 'org-tags
  2519. :type '(repeat
  2520. (choice
  2521. (cons (string :tag "Tag name")
  2522. (character :tag "Access char"))
  2523. (const :tag "Start radio group" (:startgroup))
  2524. (const :tag "End radio group" (:endgroup))
  2525. (const :tag "New line" (:newline)))))
  2526. (defcustom org-complete-tags-always-offer-all-agenda-tags nil
  2527. "If non-nil, always offer completion for all tags of all agenda files.
  2528. Instead of customizing this variable directly, you might want to
  2529. set it locally for capture buffers, because there no list of
  2530. tags in that file can be created dynamically (there are none).
  2531. (add-hook 'org-capture-mode-hook
  2532. (lambda ()
  2533. (set (make-local-variable
  2534. 'org-complete-tags-always-offer-all-agenda-tags)
  2535. t)))"
  2536. :group 'org-tags
  2537. :version "24.1"
  2538. :type 'boolean)
  2539. (defvar org-file-tags nil
  2540. "List of tags that can be inherited by all entries in the file.
  2541. The tags will be inherited if the variable `org-use-tag-inheritance'
  2542. says they should be.
  2543. This variable is populated from #+FILETAGS lines.")
  2544. (defcustom org-use-fast-tag-selection 'auto
  2545. "Non-nil means use fast tag selection scheme.
  2546. This is a special interface to select and deselect tags with single keys.
  2547. When nil, fast selection is never used.
  2548. When the symbol `auto', fast selection is used if and only if selection
  2549. characters for tags have been configured, either through the variable
  2550. `org-tag-alist' or through a #+TAGS line in the buffer.
  2551. When t, fast selection is always used and selection keys are assigned
  2552. automatically if necessary."
  2553. :group 'org-tags
  2554. :type '(choice
  2555. (const :tag "Always" t)
  2556. (const :tag "Never" nil)
  2557. (const :tag "When selection characters are configured" 'auto)))
  2558. (defcustom org-fast-tag-selection-single-key nil
  2559. "Non-nil means fast tag selection exits after first change.
  2560. When nil, you have to press RET to exit it.
  2561. During fast tag selection, you can toggle this flag with `C-c'.
  2562. This variable can also have the value `expert'. In this case, the window
  2563. displaying the tags menu is not even shown, until you press C-c again."
  2564. :group 'org-tags
  2565. :type '(choice
  2566. (const :tag "No" nil)
  2567. (const :tag "Yes" t)
  2568. (const :tag "Expert" expert)))
  2569. (defvar org-fast-tag-selection-include-todo nil
  2570. "Non-nil means fast tags selection interface will also offer TODO states.
  2571. This is an undocumented feature, you should not rely on it.")
  2572. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  2573. "The column to which tags should be indented in a headline.
  2574. If this number is positive, it specifies the column. If it is negative,
  2575. it means that the tags should be flushright to that column. For example,
  2576. -80 works well for a normal 80 character screen.
  2577. When 0, place tags directly after headline text, with only one space in
  2578. between."
  2579. :group 'org-tags
  2580. :type 'integer)
  2581. (defcustom org-auto-align-tags t
  2582. "Non-nil keeps tags aligned when modifying headlines.
  2583. Some operations (i.e. demoting) change the length of a headline and
  2584. therefore shift the tags around. With this option turned on, after
  2585. each such operation the tags are again aligned to `org-tags-column'."
  2586. :group 'org-tags
  2587. :type 'boolean)
  2588. (defcustom org-use-tag-inheritance t
  2589. "Non-nil means tags in levels apply also for sublevels.
  2590. When nil, only the tags directly given in a specific line apply there.
  2591. This may also be a list of tags that should be inherited, or a regexp that
  2592. matches tags that should be inherited. Additional control is possible
  2593. with the variable `org-tags-exclude-from-inheritance' which gives an
  2594. explicit list of tags to be excluded from inheritance., even if the value of
  2595. `org-use-tag-inheritance' would select it for inheritance.
  2596. If this option is t, a match early-on in a tree can lead to a large
  2597. number of matches in the subtree when constructing the agenda or creating
  2598. a sparse tree. If you only want to see the first match in a tree during
  2599. a search, check out the variable `org-tags-match-list-sublevels'."
  2600. :group 'org-tags
  2601. :type '(choice
  2602. (const :tag "Not" nil)
  2603. (const :tag "Always" t)
  2604. (repeat :tag "Specific tags" (string :tag "Tag"))
  2605. (regexp :tag "Tags matched by regexp")))
  2606. (defcustom org-tags-exclude-from-inheritance nil
  2607. "List of tags that should never be inherited.
  2608. This is a way to exclude a few tags from inheritance. For way to do
  2609. the opposite, to actively allow inheritance for selected tags,
  2610. see the variable `org-use-tag-inheritance'."
  2611. :group 'org-tags
  2612. :type '(repeat (string :tag "Tag")))
  2613. (defun org-tag-inherit-p (tag)
  2614. "Check if TAG is one that should be inherited."
  2615. (cond
  2616. ((member tag org-tags-exclude-from-inheritance) nil)
  2617. ((eq org-use-tag-inheritance t) t)
  2618. ((not org-use-tag-inheritance) nil)
  2619. ((stringp org-use-tag-inheritance)
  2620. (string-match org-use-tag-inheritance tag))
  2621. ((listp org-use-tag-inheritance)
  2622. (member tag org-use-tag-inheritance))
  2623. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  2624. (defcustom org-tags-match-list-sublevels t
  2625. "Non-nil means list also sublevels of headlines matching a search.
  2626. This variable applies to tags/property searches, and also to stuck
  2627. projects because this search is based on a tags match as well.
  2628. When set to the symbol `indented', sublevels are indented with
  2629. leading dots.
  2630. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  2631. the sublevels of a headline matching a tag search often also match
  2632. the same search. Listing all of them can create very long lists.
  2633. Setting this variable to nil causes subtrees of a match to be skipped.
  2634. This variable is semi-obsolete and probably should always be true. It
  2635. is better to limit inheritance to certain tags using the variables
  2636. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  2637. :group 'org-tags
  2638. :type '(choice
  2639. (const :tag "No, don't list them" nil)
  2640. (const :tag "Yes, do list them" t)
  2641. (const :tag "List them, indented with leading dots" indented)))
  2642. (defcustom org-tags-sort-function nil
  2643. "When set, tags are sorted using this function as a comparator."
  2644. :group 'org-tags
  2645. :type '(choice
  2646. (const :tag "No sorting" nil)
  2647. (const :tag "Alphabetical" string<)
  2648. (const :tag "Reverse alphabetical" string>)
  2649. (function :tag "Custom function" nil)))
  2650. (defvar org-tags-history nil
  2651. "History of minibuffer reads for tags.")
  2652. (defvar org-last-tags-completion-table nil
  2653. "The last used completion table for tags.")
  2654. (defvar org-after-tags-change-hook nil
  2655. "Hook that is run after the tags in a line have changed.")
  2656. (defgroup org-properties nil
  2657. "Options concerning properties in Org-mode."
  2658. :tag "Org Properties"
  2659. :group 'org)
  2660. (defcustom org-property-format "%-10s %s"
  2661. "How property key/value pairs should be formatted by `indent-line'.
  2662. When `indent-line' hits a property definition, it will format the line
  2663. according to this format, mainly to make sure that the values are
  2664. lined-up with respect to each other."
  2665. :group 'org-properties
  2666. :type 'string)
  2667. (defcustom org-properties-postprocess-alist nil
  2668. "Alist of properties and functions to adjust inserted values.
  2669. Elements of this alist must be of the form
  2670. ([string] [function])
  2671. where [string] must be a property name and [function] must be a
  2672. lambda expression: this lambda expression must take one argument,
  2673. the value to adjust, and return the new value as a string.
  2674. For example, this element will allow the property \"Remaining\"
  2675. to be updated wrt the relation between the \"Effort\" property
  2676. and the clock summary:
  2677. ((\"Remaining\" (lambda(value)
  2678. (let ((clocksum (org-clock-sum-current-item))
  2679. (effort (org-duration-string-to-minutes
  2680. (org-entry-get (point) \"Effort\"))))
  2681. (org-minutes-to-hh:mm-string (- effort clocksum))))))"
  2682. :group 'org-properties
  2683. :version "24.1"
  2684. :type 'alist)
  2685. (defcustom org-use-property-inheritance nil
  2686. "Non-nil means properties apply also for sublevels.
  2687. This setting is chiefly used during property searches. Turning it on can
  2688. cause significant overhead when doing a search, which is why it is not
  2689. on by default.
  2690. When nil, only the properties directly given in the current entry count.
  2691. When t, every property is inherited. The value may also be a list of
  2692. properties that should have inheritance, or a regular expression matching
  2693. properties that should be inherited.
  2694. However, note that some special properties use inheritance under special
  2695. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  2696. and the properties ending in \"_ALL\" when they are used as descriptor
  2697. for valid values of a property.
  2698. Note for programmers:
  2699. When querying an entry with `org-entry-get', you can control if inheritance
  2700. should be used. By default, `org-entry-get' looks only at the local
  2701. properties. You can request inheritance by setting the inherit argument
  2702. to t (to force inheritance) or to `selective' (to respect the setting
  2703. in this variable)."
  2704. :group 'org-properties
  2705. :type '(choice
  2706. (const :tag "Not" nil)
  2707. (const :tag "Always" t)
  2708. (repeat :tag "Specific properties" (string :tag "Property"))
  2709. (regexp :tag "Properties matched by regexp")))
  2710. (defun org-property-inherit-p (property)
  2711. "Check if PROPERTY is one that should be inherited."
  2712. (cond
  2713. ((eq org-use-property-inheritance t) t)
  2714. ((not org-use-property-inheritance) nil)
  2715. ((stringp org-use-property-inheritance)
  2716. (string-match org-use-property-inheritance property))
  2717. ((listp org-use-property-inheritance)
  2718. (member property org-use-property-inheritance))
  2719. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  2720. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  2721. "The default column format, if no other format has been defined.
  2722. This variable can be set on the per-file basis by inserting a line
  2723. #+COLUMNS: %25ITEM ....."
  2724. :group 'org-properties
  2725. :type 'string)
  2726. (defcustom org-columns-ellipses ".."
  2727. "The ellipses to be used when a field in column view is truncated.
  2728. When this is the empty string, as many characters as possible are shown,
  2729. but then there will be no visual indication that the field has been truncated.
  2730. When this is a string of length N, the last N characters of a truncated
  2731. field are replaced by this string. If the column is narrower than the
  2732. ellipses string, only part of the ellipses string will be shown."
  2733. :group 'org-properties
  2734. :type 'string)
  2735. (defcustom org-columns-modify-value-for-display-function nil
  2736. "Function that modifies values for display in column view.
  2737. For example, it can be used to cut out a certain part from a time stamp.
  2738. The function must take 2 arguments:
  2739. column-title The title of the column (*not* the property name)
  2740. value The value that should be modified.
  2741. The function should return the value that should be displayed,
  2742. or nil if the normal value should be used."
  2743. :group 'org-properties
  2744. :type 'function)
  2745. (defcustom org-effort-property "Effort"
  2746. "The property that is being used to keep track of effort estimates.
  2747. Effort estimates given in this property need to have the format H:MM."
  2748. :group 'org-properties
  2749. :group 'org-progress
  2750. :type '(string :tag "Property"))
  2751. (defconst org-global-properties-fixed
  2752. '(("VISIBILITY_ALL" . "folded children content all")
  2753. ("CLOCK_MODELINE_TOTAL_ALL" . "current today repeat all auto"))
  2754. "List of property/value pairs that can be inherited by any entry.
  2755. These are fixed values, for the preset properties. The user variable
  2756. that can be used to add to this list is `org-global-properties'.
  2757. The entries in this list are cons cells where the car is a property
  2758. name and cdr is a string with the value. If the value represents
  2759. multiple items like an \"_ALL\" property, separate the items by
  2760. spaces.")
  2761. (defcustom org-global-properties nil
  2762. "List of property/value pairs that can be inherited by any entry.
  2763. This list will be combined with the constant `org-global-properties-fixed'.
  2764. The entries in this list are cons cells where the car is a property
  2765. name and cdr is a string with the value.
  2766. You can set buffer-local values for the same purpose in the variable
  2767. `org-file-properties' this by adding lines like
  2768. #+PROPERTY: NAME VALUE"
  2769. :group 'org-properties
  2770. :type '(repeat
  2771. (cons (string :tag "Property")
  2772. (string :tag "Value"))))
  2773. (defvar org-file-properties nil
  2774. "List of property/value pairs that can be inherited by any entry.
  2775. Valid for the current buffer.
  2776. This variable is populated from #+PROPERTY lines.")
  2777. (make-variable-buffer-local 'org-file-properties)
  2778. (defgroup org-agenda nil
  2779. "Options concerning agenda views in Org-mode."
  2780. :tag "Org Agenda"
  2781. :group 'org)
  2782. (defvar org-category nil
  2783. "Variable used by org files to set a category for agenda display.
  2784. Such files should use a file variable to set it, for example
  2785. # -*- mode: org; org-category: \"ELisp\"
  2786. or contain a special line
  2787. #+CATEGORY: ELisp
  2788. If the file does not specify a category, then file's base name
  2789. is used instead.")
  2790. (make-variable-buffer-local 'org-category)
  2791. (put 'org-category 'safe-local-variable #'(lambda (x) (or (symbolp x) (stringp x))))
  2792. (defcustom org-agenda-files nil
  2793. "The files to be used for agenda display.
  2794. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  2795. \\[org-remove-file]. You can also use customize to edit the list.
  2796. If an entry is a directory, all files in that directory that are matched by
  2797. `org-agenda-file-regexp' will be part of the file list.
  2798. If the value of the variable is not a list but a single file name, then
  2799. the list of agenda files is actually stored and maintained in that file, one
  2800. agenda file per line. In this file paths can be given relative to
  2801. `org-directory'. Tilde expansion and environment variable substitution
  2802. are also made."
  2803. :group 'org-agenda
  2804. :type '(choice
  2805. (repeat :tag "List of files and directories" file)
  2806. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  2807. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  2808. "Regular expression to match files for `org-agenda-files'.
  2809. If any element in the list in that variable contains a directory instead
  2810. of a normal file, all files in that directory that are matched by this
  2811. regular expression will be included."
  2812. :group 'org-agenda
  2813. :type 'regexp)
  2814. (defcustom org-agenda-text-search-extra-files nil
  2815. "List of extra files to be searched by text search commands.
  2816. These files will be search in addition to the agenda files by the
  2817. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  2818. Note that these files will only be searched for text search commands,
  2819. not for the other agenda views like todo lists, tag searches or the weekly
  2820. agenda. This variable is intended to list notes and possibly archive files
  2821. that should also be searched by these two commands.
  2822. In fact, if the first element in the list is the symbol `agenda-archives',
  2823. than all archive files of all agenda files will be added to the search
  2824. scope."
  2825. :group 'org-agenda
  2826. :type '(set :greedy t
  2827. (const :tag "Agenda Archives" agenda-archives)
  2828. (repeat :inline t (file))))
  2829. (if (fboundp 'defvaralias)
  2830. (defvaralias 'org-agenda-multi-occur-extra-files
  2831. 'org-agenda-text-search-extra-files))
  2832. (defcustom org-agenda-skip-unavailable-files nil
  2833. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  2834. A nil value means to remove them, after a query, from the list."
  2835. :group 'org-agenda
  2836. :type 'boolean)
  2837. (defcustom org-calendar-to-agenda-key [?c]
  2838. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  2839. The command `org-calendar-goto-agenda' will be bound to this key. The
  2840. default is the character `c' because then `c' can be used to switch back and
  2841. forth between agenda and calendar."
  2842. :group 'org-agenda
  2843. :type 'sexp)
  2844. (defcustom org-calendar-agenda-action-key [?k]
  2845. "The key to be installed in `calendar-mode-map' for agenda-action.
  2846. The command `org-agenda-action' will be bound to this key. The
  2847. default is the character `k' because we use the same key in the agenda."
  2848. :group 'org-agenda
  2849. :type 'sexp)
  2850. (defcustom org-calendar-insert-diary-entry-key [?i]
  2851. "The key to be installed in `calendar-mode-map' for adding diary entries.
  2852. This option is irrelevant until `org-agenda-diary-file' has been configured
  2853. to point to an Org-mode file. When that is the case, the command
  2854. `org-agenda-diary-entry' will be bound to the key given here, by default
  2855. `i'. In the calendar, `i' normally adds entries to `diary-file'. So
  2856. if you want to continue doing this, you need to change this to a different
  2857. key."
  2858. :group 'org-agenda
  2859. :type 'sexp)
  2860. (defcustom org-agenda-diary-file 'diary-file
  2861. "File to which to add new entries with the `i' key in agenda and calendar.
  2862. When this is the symbol `diary-file', the functionality in the Emacs
  2863. calendar will be used to add entries to the `diary-file'. But when this
  2864. points to a file, `org-agenda-diary-entry' will be used instead."
  2865. :group 'org-agenda
  2866. :type '(choice
  2867. (const :tag "The standard Emacs diary file" diary-file)
  2868. (file :tag "Special Org file diary entries")))
  2869. (eval-after-load "calendar"
  2870. '(progn
  2871. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  2872. 'org-calendar-goto-agenda)
  2873. (org-defkey calendar-mode-map org-calendar-agenda-action-key
  2874. 'org-agenda-action)
  2875. (add-hook 'calendar-mode-hook
  2876. (lambda ()
  2877. (unless (eq org-agenda-diary-file 'diary-file)
  2878. (define-key calendar-mode-map
  2879. org-calendar-insert-diary-entry-key
  2880. 'org-agenda-diary-entry))))))
  2881. (defgroup org-latex nil
  2882. "Options for embedding LaTeX code into Org-mode."
  2883. :tag "Org LaTeX"
  2884. :group 'org)
  2885. (defcustom org-format-latex-options
  2886. '(:foreground default :background default :scale 1.0
  2887. :html-foreground "Black" :html-background "Transparent"
  2888. :html-scale 1.0 :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  2889. "Options for creating images from LaTeX fragments.
  2890. This is a property list with the following properties:
  2891. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  2892. `default' means use the foreground of the default face.
  2893. :background the background color, or \"Transparent\".
  2894. `default' means use the background of the default face.
  2895. :scale a scaling factor for the size of the images, to get more pixels
  2896. :html-foreground, :html-background, :html-scale
  2897. the same numbers for HTML export.
  2898. :matchers a list indicating which matchers should be used to
  2899. find LaTeX fragments. Valid members of this list are:
  2900. \"begin\" find environments
  2901. \"$1\" find single characters surrounded by $.$
  2902. \"$\" find math expressions surrounded by $...$
  2903. \"$$\" find math expressions surrounded by $$....$$
  2904. \"\\(\" find math expressions surrounded by \\(...\\)
  2905. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  2906. :group 'org-latex
  2907. :type 'plist)
  2908. (defcustom org-format-latex-signal-error t
  2909. "Non-nil means signal an error when image creation of LaTeX snippets fails.
  2910. When nil, just push out a message."
  2911. :group 'org-latex
  2912. :version "24.1"
  2913. :type 'boolean)
  2914. (defcustom org-latex-to-mathml-jar-file nil
  2915. "Value of\"%j\" in `org-latex-to-mathml-convert-command'.
  2916. Use this to specify additional executable file say a jar file.
  2917. When using MathToWeb as the converter, specify the full-path to
  2918. your mathtoweb.jar file."
  2919. :group 'org-latex
  2920. :version "24.1"
  2921. :type '(choice
  2922. (const :tag "None" nil)
  2923. (file :tag "JAR file" :must-match t)))
  2924. (defcustom org-latex-to-mathml-convert-command nil
  2925. "Command to convert LaTeX fragments to MathML.
  2926. Replace format-specifiers in the command as noted below and use
  2927. `shell-command' to convert LaTeX to MathML.
  2928. %j: Executable file in fully expanded form as specified by
  2929. `org-latex-to-mathml-jar-file'.
  2930. %I: Input LaTeX file in fully expanded form
  2931. %o: Output MathML file
  2932. This command is used by `org-create-math-formula'.
  2933. When using MathToWeb as the converter, set this to
  2934. \"java -jar %j -unicode -force -df %o %I\"."
  2935. :group 'org-latex
  2936. :version "24.1"
  2937. :type '(choice
  2938. (const :tag "None" nil)
  2939. (string :tag "\nShell command")))
  2940. (defun org-format-latex-mathml-available-p ()
  2941. "Return t if `org-latex-to-mathml-convert-command' is usable."
  2942. (save-match-data
  2943. (when (and (boundp 'org-latex-to-mathml-convert-command)
  2944. org-latex-to-mathml-convert-command)
  2945. (let ((executable (car (split-string
  2946. org-latex-to-mathml-convert-command))))
  2947. (when (executable-find executable)
  2948. (if (string-match
  2949. "%j" org-latex-to-mathml-convert-command)
  2950. (file-readable-p org-latex-to-mathml-jar-file)
  2951. t))))))
  2952. (defcustom org-format-latex-header "\\documentclass{article}
  2953. \\usepackage[usenames]{color}
  2954. \\usepackage{amsmath}
  2955. \\usepackage[mathscr]{eucal}
  2956. \\pagestyle{empty} % do not remove
  2957. \[PACKAGES]
  2958. \[DEFAULT-PACKAGES]
  2959. % The settings below are copied from fullpage.sty
  2960. \\setlength{\\textwidth}{\\paperwidth}
  2961. \\addtolength{\\textwidth}{-3cm}
  2962. \\setlength{\\oddsidemargin}{1.5cm}
  2963. \\addtolength{\\oddsidemargin}{-2.54cm}
  2964. \\setlength{\\evensidemargin}{\\oddsidemargin}
  2965. \\setlength{\\textheight}{\\paperheight}
  2966. \\addtolength{\\textheight}{-\\headheight}
  2967. \\addtolength{\\textheight}{-\\headsep}
  2968. \\addtolength{\\textheight}{-\\footskip}
  2969. \\addtolength{\\textheight}{-3cm}
  2970. \\setlength{\\topmargin}{1.5cm}
  2971. \\addtolength{\\topmargin}{-2.54cm}"
  2972. "The document header used for processing LaTeX fragments.
  2973. It is imperative that this header make sure that no page number
  2974. appears on the page. The package defined in the variables
  2975. `org-export-latex-default-packages-alist' and `org-export-latex-packages-alist'
  2976. will either replace the placeholder \"[PACKAGES]\" in this header, or they
  2977. will be appended."
  2978. :group 'org-latex
  2979. :type 'string)
  2980. (defvar org-format-latex-header-extra nil)
  2981. (defun org-set-packages-alist (var val)
  2982. "Set the packages alist and make sure it has 3 elements per entry."
  2983. (set var (mapcar (lambda (x)
  2984. (if (and (consp x) (= (length x) 2))
  2985. (list (car x) (nth 1 x) t)
  2986. x))
  2987. val)))
  2988. (defun org-get-packages-alist (var)
  2989. "Get the packages alist and make sure it has 3 elements per entry."
  2990. (mapcar (lambda (x)
  2991. (if (and (consp x) (= (length x) 2))
  2992. (list (car x) (nth 1 x) t)
  2993. x))
  2994. (default-value var)))
  2995. ;; The following variables are defined here because is it also used
  2996. ;; when formatting latex fragments. Originally it was part of the
  2997. ;; LaTeX exporter, which is why the name includes "export".
  2998. (defcustom org-export-latex-default-packages-alist
  2999. '(("AUTO" "inputenc" t)
  3000. ("T1" "fontenc" t)
  3001. ("" "fixltx2e" nil)
  3002. ("" "graphicx" t)
  3003. ("" "longtable" nil)
  3004. ("" "float" nil)
  3005. ("" "wrapfig" nil)
  3006. ("" "soul" t)
  3007. ("" "textcomp" t)
  3008. ("" "marvosym" t)
  3009. ("" "wasysym" t)
  3010. ("" "latexsym" t)
  3011. ("" "amssymb" t)
  3012. ("" "hyperref" nil)
  3013. "\\tolerance=1000"
  3014. )
  3015. "Alist of default packages to be inserted in the header.
  3016. Change this only if one of the packages here causes an incompatibility
  3017. with another package you are using.
  3018. The packages in this list are needed by one part or another of Org-mode
  3019. to function properly.
  3020. - inputenc, fontenc: for basic font and character selection
  3021. - textcomp, marvosymb, wasysym, latexsym, amssym: for various symbols used
  3022. for interpreting the entities in `org-entities'. You can skip some of these
  3023. packages if you don't use any of the symbols in it.
  3024. - graphicx: for including images
  3025. - float, wrapfig: for figure placement
  3026. - longtable: for long tables
  3027. - hyperref: for cross references
  3028. Therefore you should not modify this variable unless you know what you
  3029. are doing. The one reason to change it anyway is that you might be loading
  3030. some other package that conflicts with one of the default packages.
  3031. Each cell is of the format \( \"options\" \"package\" snippet-flag\).
  3032. If SNIPPET-FLAG is t, the package also needs to be included when
  3033. compiling LaTeX snippets into images for inclusion into HTML."
  3034. :group 'org-export-latex
  3035. :set 'org-set-packages-alist
  3036. :get 'org-get-packages-alist
  3037. :version "24.1"
  3038. :type '(repeat
  3039. (choice
  3040. (list :tag "options/package pair"
  3041. (string :tag "options")
  3042. (string :tag "package")
  3043. (boolean :tag "Snippet"))
  3044. (string :tag "A line of LaTeX"))))
  3045. (defcustom org-export-latex-packages-alist nil
  3046. "Alist of packages to be inserted in every LaTeX header.
  3047. These will be inserted after `org-export-latex-default-packages-alist'.
  3048. Each cell is of the format \( \"options\" \"package\" snippet-flag \).
  3049. SNIPPET-FLAG, when t, indicates that this package is also needed when
  3050. turning LaTeX snippets into images for inclusion into HTML.
  3051. Make sure that you only list packages here which:
  3052. - you want in every file
  3053. - do not conflict with the default packages in
  3054. `org-export-latex-default-packages-alist'
  3055. - do not conflict with the setup in `org-format-latex-header'."
  3056. :group 'org-export-latex
  3057. :set 'org-set-packages-alist
  3058. :get 'org-get-packages-alist
  3059. :type '(repeat
  3060. (choice
  3061. (list :tag "options/package pair"
  3062. (string :tag "options")
  3063. (string :tag "package")
  3064. (boolean :tag "Snippet"))
  3065. (string :tag "A line of LaTeX"))))
  3066. (defgroup org-appearance nil
  3067. "Settings for Org-mode appearance."
  3068. :tag "Org Appearance"
  3069. :group 'org)
  3070. (defcustom org-level-color-stars-only nil
  3071. "Non-nil means fontify only the stars in each headline.
  3072. When nil, the entire headline is fontified.
  3073. Changing it requires restart of `font-lock-mode' to become effective
  3074. also in regions already fontified."
  3075. :group 'org-appearance
  3076. :type 'boolean)
  3077. (defcustom org-hide-leading-stars nil
  3078. "Non-nil means hide the first N-1 stars in a headline.
  3079. This works by using the face `org-hide' for these stars. This
  3080. face is white for a light background, and black for a dark
  3081. background. You may have to customize the face `org-hide' to
  3082. make this work.
  3083. Changing it requires restart of `font-lock-mode' to become effective
  3084. also in regions already fontified.
  3085. You may also set this on a per-file basis by adding one of the following
  3086. lines to the buffer:
  3087. #+STARTUP: hidestars
  3088. #+STARTUP: showstars"
  3089. :group 'org-appearance
  3090. :type 'boolean)
  3091. (defcustom org-hidden-keywords nil
  3092. "List of symbols corresponding to keywords to be hidden the org buffer.
  3093. For example, a value '(title) for this list will make the document's title
  3094. appear in the buffer without the initial #+TITLE: keyword."
  3095. :group 'org-appearance
  3096. :version "24.1"
  3097. :type '(set (const :tag "#+AUTHOR" author)
  3098. (const :tag "#+DATE" date)
  3099. (const :tag "#+EMAIL" email)
  3100. (const :tag "#+TITLE" title)))
  3101. (defcustom org-fontify-done-headline nil
  3102. "Non-nil means change the face of a headline if it is marked DONE.
  3103. Normally, only the TODO/DONE keyword indicates the state of a headline.
  3104. When this is non-nil, the headline after the keyword is set to the
  3105. `org-headline-done' as an additional indication."
  3106. :group 'org-appearance
  3107. :type 'boolean)
  3108. (defcustom org-fontify-emphasized-text t
  3109. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  3110. Changing this variable requires a restart of Emacs to take effect."
  3111. :group 'org-appearance
  3112. :type 'boolean)
  3113. (defcustom org-fontify-whole-heading-line nil
  3114. "Non-nil means fontify the whole line for headings.
  3115. This is useful when setting a background color for the
  3116. org-level-* faces."
  3117. :group 'org-appearance
  3118. :type 'boolean)
  3119. (defcustom org-highlight-latex-fragments-and-specials nil
  3120. "Non-nil means fontify what is treated specially by the exporters."
  3121. :group 'org-appearance
  3122. :type 'boolean)
  3123. (defcustom org-hide-emphasis-markers nil
  3124. "Non-nil mean font-lock should hide the emphasis marker characters."
  3125. :group 'org-appearance
  3126. :type 'boolean)
  3127. (defcustom org-pretty-entities nil
  3128. "Non-nil means show entities as UTF8 characters.
  3129. When nil, the \\name form remains in the buffer."
  3130. :group 'org-appearance
  3131. :version "24.1"
  3132. :type 'boolean)
  3133. (defcustom org-pretty-entities-include-sub-superscripts t
  3134. "Non-nil means, pretty entity display includes formatting sub/superscripts."
  3135. :group 'org-appearance
  3136. :version "24.1"
  3137. :type 'boolean)
  3138. (defvar org-emph-re nil
  3139. "Regular expression for matching emphasis.
  3140. After a match, the match groups contain these elements:
  3141. 0 The match of the full regular expression, including the characters
  3142. before and after the proper match
  3143. 1 The character before the proper match, or empty at beginning of line
  3144. 2 The proper match, including the leading and trailing markers
  3145. 3 The leading marker like * or /, indicating the type of highlighting
  3146. 4 The text between the emphasis markers, not including the markers
  3147. 5 The character after the match, empty at the end of a line")
  3148. (defvar org-verbatim-re nil
  3149. "Regular expression for matching verbatim text.")
  3150. (defvar org-emphasis-regexp-components) ; defined just below
  3151. (defvar org-emphasis-alist) ; defined just below
  3152. (defun org-set-emph-re (var val)
  3153. "Set variable and compute the emphasis regular expression."
  3154. (set var val)
  3155. (when (and (boundp 'org-emphasis-alist)
  3156. (boundp 'org-emphasis-regexp-components)
  3157. org-emphasis-alist org-emphasis-regexp-components)
  3158. (let* ((e org-emphasis-regexp-components)
  3159. (pre (car e))
  3160. (post (nth 1 e))
  3161. (border (nth 2 e))
  3162. (body (nth 3 e))
  3163. (nl (nth 4 e))
  3164. (body1 (concat body "*?"))
  3165. (markers (mapconcat 'car org-emphasis-alist ""))
  3166. (vmarkers (mapconcat
  3167. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  3168. org-emphasis-alist "")))
  3169. ;; make sure special characters appear at the right position in the class
  3170. (if (string-match "\\^" markers)
  3171. (setq markers (concat (replace-match "" t t markers) "^")))
  3172. (if (string-match "-" markers)
  3173. (setq markers (concat (replace-match "" t t markers) "-")))
  3174. (if (string-match "\\^" vmarkers)
  3175. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  3176. (if (string-match "-" vmarkers)
  3177. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  3178. (if (> nl 0)
  3179. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  3180. (int-to-string nl) "\\}")))
  3181. ;; Make the regexp
  3182. (setq org-emph-re
  3183. (concat "\\([" pre "]\\|^\\)"
  3184. "\\("
  3185. "\\([" markers "]\\)"
  3186. "\\("
  3187. "[^" border "]\\|"
  3188. "[^" border "]"
  3189. body1
  3190. "[^" border "]"
  3191. "\\)"
  3192. "\\3\\)"
  3193. "\\([" post "]\\|$\\)"))
  3194. (setq org-verbatim-re
  3195. (concat "\\([" pre "]\\|^\\)"
  3196. "\\("
  3197. "\\([" vmarkers "]\\)"
  3198. "\\("
  3199. "[^" border "]\\|"
  3200. "[^" border "]"
  3201. body1
  3202. "[^" border "]"
  3203. "\\)"
  3204. "\\3\\)"
  3205. "\\([" post "]\\|$\\)")))))
  3206. (defcustom org-emphasis-regexp-components
  3207. '(" \t('\"{" "- \t.,:!?;'\")}\\" " \t\r\n,\"'" "." 1)
  3208. "Components used to build the regular expression for emphasis.
  3209. This is a list with five entries. Terminology: In an emphasis string
  3210. like \" *strong word* \", we call the initial space PREMATCH, the final
  3211. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  3212. and \"trong wor\" is the body. The different components in this variable
  3213. specify what is allowed/forbidden in each part:
  3214. pre Chars allowed as prematch. Beginning of line will be allowed too.
  3215. post Chars allowed as postmatch. End of line will be allowed too.
  3216. border The chars *forbidden* as border characters.
  3217. body-regexp A regexp like \".\" to match a body character. Don't use
  3218. non-shy groups here, and don't allow newline here.
  3219. newline The maximum number of newlines allowed in an emphasis exp.
  3220. Use customize to modify this, or restart Emacs after changing it."
  3221. :group 'org-appearance
  3222. :set 'org-set-emph-re
  3223. :type '(list
  3224. (sexp :tag "Allowed chars in pre ")
  3225. (sexp :tag "Allowed chars in post ")
  3226. (sexp :tag "Forbidden chars in border ")
  3227. (sexp :tag "Regexp for body ")
  3228. (integer :tag "number of newlines allowed")
  3229. (option (boolean :tag "Please ignore this button"))))
  3230. (defcustom org-emphasis-alist
  3231. `(("*" bold "<b>" "</b>")
  3232. ("/" italic "<i>" "</i>")
  3233. ("_" underline "<span style=\"text-decoration:underline;\">" "</span>")
  3234. ("=" org-code "<code>" "</code>" verbatim)
  3235. ("~" org-verbatim "<code>" "</code>" verbatim)
  3236. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  3237. "<del>" "</del>")
  3238. )
  3239. "Special syntax for emphasized text.
  3240. Text starting and ending with a special character will be emphasized, for
  3241. example *bold*, _underlined_ and /italic/. This variable sets the marker
  3242. characters, the face to be used by font-lock for highlighting in Org-mode
  3243. Emacs buffers, and the HTML tags to be used for this.
  3244. For LaTeX export, see the variable `org-export-latex-emphasis-alist'.
  3245. For DocBook export, see the variable `org-export-docbook-emphasis-alist'.
  3246. Use customize to modify this, or restart Emacs after changing it."
  3247. :group 'org-appearance
  3248. :set 'org-set-emph-re
  3249. :type '(repeat
  3250. (list
  3251. (string :tag "Marker character")
  3252. (choice
  3253. (face :tag "Font-lock-face")
  3254. (plist :tag "Face property list"))
  3255. (string :tag "HTML start tag")
  3256. (string :tag "HTML end tag")
  3257. (option (const verbatim)))))
  3258. (defvar org-protecting-blocks
  3259. '("src" "example" "latex" "ascii" "html" "docbook" "ditaa" "dot" "r" "R")
  3260. "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
  3261. This is needed for font-lock setup.")
  3262. ;;; Miscellaneous options
  3263. (defgroup org-completion nil
  3264. "Completion in Org-mode."
  3265. :tag "Org Completion"
  3266. :group 'org)
  3267. (defcustom org-completion-use-ido nil
  3268. "Non-nil means use ido completion wherever possible.
  3269. Note that `ido-mode' must be active for this variable to be relevant.
  3270. If you decide to turn this variable on, you might well want to turn off
  3271. `org-outline-path-complete-in-steps'.
  3272. See also `org-completion-use-iswitchb'."
  3273. :group 'org-completion
  3274. :type 'boolean)
  3275. (defcustom org-completion-use-iswitchb nil
  3276. "Non-nil means use iswitchb completion wherever possible.
  3277. Note that `iswitchb-mode' must be active for this variable to be relevant.
  3278. If you decide to turn this variable on, you might well want to turn off
  3279. `org-outline-path-complete-in-steps'.
  3280. Note that this variable has only an effect if `org-completion-use-ido' is nil."
  3281. :group 'org-completion
  3282. :type 'boolean)
  3283. (defcustom org-completion-fallback-command 'hippie-expand
  3284. "The expansion command called by \\[pcomplete] in normal context.
  3285. Normal means, no org-mode-specific context."
  3286. :group 'org-completion
  3287. :type 'function)
  3288. ;;; Functions and variables from their packages
  3289. ;; Declared here to avoid compiler warnings
  3290. ;; XEmacs only
  3291. (defvar outline-mode-menu-heading)
  3292. (defvar outline-mode-menu-show)
  3293. (defvar outline-mode-menu-hide)
  3294. (defvar zmacs-regions) ; XEmacs regions
  3295. ;; Emacs only
  3296. (defvar mark-active)
  3297. ;; Various packages
  3298. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  3299. (declare-function calendar-forward-day "cal-move" (arg))
  3300. (declare-function calendar-goto-date "cal-move" (date))
  3301. (declare-function calendar-goto-today "cal-move" ())
  3302. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  3303. (defvar calc-embedded-close-formula)
  3304. (defvar calc-embedded-open-formula)
  3305. (declare-function cdlatex-tab "ext:cdlatex" ())
  3306. (declare-function cdlatex-compute-tables "ext:cdlatex" ())
  3307. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  3308. (defvar font-lock-unfontify-region-function)
  3309. (declare-function iswitchb-read-buffer "iswitchb"
  3310. (prompt &optional default require-match start matches-set))
  3311. (defvar iswitchb-temp-buflist)
  3312. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  3313. (defvar org-agenda-tags-todo-honor-ignore-options)
  3314. (declare-function org-agenda-skip "org-agenda" ())
  3315. (declare-function
  3316. org-agenda-format-item "org-agenda"
  3317. (extra txt &optional category tags dotime noprefix remove-re habitp))
  3318. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  3319. (declare-function org-agenda-change-all-lines "org-agenda"
  3320. (newhead hdmarker &optional fixface just-this))
  3321. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  3322. (declare-function org-agenda-maybe-redo "org-agenda" ())
  3323. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  3324. (beg end))
  3325. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  3326. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  3327. "org-agenda" (&optional end))
  3328. (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
  3329. (declare-function org-inlinetask-in-task-p "org-inlinetask" ())
  3330. (declare-function org-inlinetask-goto-beginning "org-inlinetask" ())
  3331. (declare-function org-inlinetask-goto-end "org-inlinetask" ())
  3332. (declare-function org-indent-mode "org-indent" (&optional arg))
  3333. (declare-function parse-time-string "parse-time" (string))
  3334. (declare-function org-attach-reveal "org-attach" (&optional if-exists))
  3335. (declare-function org-export-latex-fix-inputenc "org-latex" ())
  3336. (declare-function orgtbl-send-table "org-table" (&optional maybe))
  3337. (defvar remember-data-file)
  3338. (defvar texmathp-why)
  3339. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  3340. (declare-function table--at-cell-p "table" (position &optional object at-column))
  3341. (defvar w3m-current-url)
  3342. (defvar w3m-current-title)
  3343. (defvar org-latex-regexps)
  3344. ;;; Autoload and prepare some org modules
  3345. ;; Some table stuff that needs to be defined here, because it is used
  3346. ;; by the functions setting up org-mode or checking for table context.
  3347. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  3348. "Detect an org-type or table-type table.")
  3349. (defconst org-table-line-regexp "^[ \t]*|"
  3350. "Detect an org-type table line.")
  3351. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  3352. "Detect an org-type table line.")
  3353. (defconst org-table-hline-regexp "^[ \t]*|-"
  3354. "Detect an org-type table hline.")
  3355. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  3356. "Detect a table-type table hline.")
  3357. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  3358. "Detect the first line outside a table when searching from within it.
  3359. This works for both table types.")
  3360. ;; Autoload the functions in org-table.el that are needed by functions here.
  3361. (eval-and-compile
  3362. (org-autoload "org-table"
  3363. '(org-table-align org-table-begin org-table-blank-field
  3364. org-table-convert org-table-convert-region org-table-copy-down
  3365. org-table-copy-region org-table-create
  3366. org-table-create-or-convert-from-region
  3367. org-table-create-with-table.el org-table-current-dline
  3368. org-table-cut-region org-table-delete-column org-table-edit-field
  3369. org-table-edit-formulas org-table-end org-table-eval-formula
  3370. org-table-export org-table-field-info
  3371. org-table-get-stored-formulas org-table-goto-column
  3372. org-table-hline-and-move org-table-import org-table-insert-column
  3373. org-table-insert-hline org-table-insert-row org-table-iterate
  3374. org-table-justify-field-maybe org-table-kill-row
  3375. org-table-maybe-eval-formula org-table-maybe-recalculate-line
  3376. org-table-move-column org-table-move-column-left
  3377. org-table-move-column-right org-table-move-row
  3378. org-table-move-row-down org-table-move-row-up
  3379. org-table-next-field org-table-next-row org-table-paste-rectangle
  3380. org-table-previous-field org-table-recalculate
  3381. org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
  3382. org-table-toggle-coordinate-overlays
  3383. org-table-toggle-formula-debugger org-table-wrap-region
  3384. orgtbl-mode turn-on-orgtbl org-table-to-lisp
  3385. orgtbl-to-generic orgtbl-to-tsv orgtbl-to-csv orgtbl-to-latex
  3386. orgtbl-to-orgtbl orgtbl-to-html orgtbl-to-texinfo)))
  3387. (defun org-at-table-p (&optional table-type)
  3388. "Return t if the cursor is inside an org-type table.
  3389. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  3390. (if org-enable-table-editor
  3391. (save-excursion
  3392. (beginning-of-line 1)
  3393. (looking-at (if table-type org-table-any-line-regexp
  3394. org-table-line-regexp)))
  3395. nil))
  3396. (defsubst org-table-p () (org-at-table-p))
  3397. (defun org-at-table.el-p ()
  3398. "Return t if and only if we are at a table.el table."
  3399. (and (org-at-table-p 'any)
  3400. (save-excursion
  3401. (goto-char (org-table-begin 'any))
  3402. (looking-at org-table1-hline-regexp))))
  3403. (defun org-table-recognize-table.el ()
  3404. "If there is a table.el table nearby, recognize it and move into it."
  3405. (if org-table-tab-recognizes-table.el
  3406. (if (org-at-table.el-p)
  3407. (progn
  3408. (beginning-of-line 1)
  3409. (if (looking-at org-table-dataline-regexp)
  3410. nil
  3411. (if (looking-at org-table1-hline-regexp)
  3412. (progn
  3413. (beginning-of-line 2)
  3414. (if (looking-at org-table-any-border-regexp)
  3415. (beginning-of-line -1)))))
  3416. (if (re-search-forward "|" (org-table-end t) t)
  3417. (progn
  3418. (require 'table)
  3419. (if (table--at-cell-p (point))
  3420. t
  3421. (message "recognizing table.el table...")
  3422. (table-recognize-table)
  3423. (message "recognizing table.el table...done")))
  3424. (error "This should not happen"))
  3425. t)
  3426. nil)
  3427. nil))
  3428. (defun org-at-table-hline-p ()
  3429. "Return t if the cursor is inside a hline in a table."
  3430. (if org-enable-table-editor
  3431. (save-excursion
  3432. (beginning-of-line 1)
  3433. (looking-at org-table-hline-regexp))
  3434. nil))
  3435. (defvar org-table-clean-did-remove-column nil)
  3436. (defun org-table-map-tables (function &optional quietly)
  3437. "Apply FUNCTION to the start of all tables in the buffer."
  3438. (save-excursion
  3439. (save-restriction
  3440. (widen)
  3441. (goto-char (point-min))
  3442. (while (re-search-forward org-table-any-line-regexp nil t)
  3443. (unless quietly
  3444. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size))))
  3445. (beginning-of-line 1)
  3446. (when (looking-at org-table-line-regexp)
  3447. (save-excursion (funcall function))
  3448. (or (looking-at org-table-line-regexp)
  3449. (forward-char 1)))
  3450. (re-search-forward org-table-any-border-regexp nil 1))))
  3451. (unless quietly (message "Mapping tables: done")))
  3452. ;; Declare and autoload functions from org-exp.el & Co
  3453. (declare-function org-default-export-plist "org-exp")
  3454. (declare-function org-infile-export-plist "org-exp")
  3455. (declare-function org-get-current-options "org-exp")
  3456. (eval-and-compile
  3457. (org-autoload "org-exp"
  3458. '(org-export org-export-visible
  3459. org-insert-export-options-template
  3460. org-table-clean-before-export))
  3461. (org-autoload "org-ascii"
  3462. '(org-export-as-ascii org-export-ascii-preprocess
  3463. org-export-as-ascii-to-buffer org-replace-region-by-ascii
  3464. org-export-region-as-ascii))
  3465. (org-autoload "org-latex"
  3466. '(org-export-as-latex-batch org-export-as-latex-to-buffer
  3467. org-replace-region-by-latex org-export-region-as-latex
  3468. org-export-as-latex org-export-as-pdf
  3469. org-export-as-pdf-and-open))
  3470. (org-autoload "org-html"
  3471. '(org-export-as-html-and-open
  3472. org-export-as-html-batch org-export-as-html-to-buffer
  3473. org-replace-region-by-html org-export-region-as-html
  3474. org-export-as-html))
  3475. (org-autoload "org-docbook"
  3476. '(org-export-as-docbook-batch org-export-as-docbook-to-buffer
  3477. org-replace-region-by-docbook org-export-region-as-docbook
  3478. org-export-as-docbook-pdf org-export-as-docbook-pdf-and-open
  3479. org-export-as-docbook))
  3480. (org-autoload "org-icalendar"
  3481. '(org-export-icalendar-this-file
  3482. org-export-icalendar-all-agenda-files
  3483. org-export-icalendar-combine-agenda-files))
  3484. (org-autoload "org-xoxo" '(org-export-as-xoxo))
  3485. (org-autoload "org-beamer" '(org-beamer-mode org-beamer-sectioning)))
  3486. ;; Declare and autoload functions from org-agenda.el
  3487. (eval-and-compile
  3488. (org-autoload "org-agenda"
  3489. '(org-agenda org-agenda-list org-search-view
  3490. org-todo-list org-tags-view org-agenda-list-stuck-projects
  3491. org-diary org-agenda-to-appt
  3492. org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
  3493. ;; Autoload org-remember
  3494. (eval-and-compile
  3495. (org-autoload "org-remember"
  3496. '(org-remember-insinuate org-remember-annotation
  3497. org-remember-apply-template org-remember org-remember-handler)))
  3498. (eval-and-compile
  3499. (org-autoload "org-capture"
  3500. '(org-capture org-capture-insert-template-here
  3501. org-capture-import-remember-templates)))
  3502. ;; Autoload org-clock.el
  3503. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock"
  3504. (beg end))
  3505. (declare-function org-clock-update-mode-line "org-clock" ())
  3506. (declare-function org-resolve-clocks "org-clock"
  3507. (&optional also-non-dangling-p prompt last-valid))
  3508. (defvar org-clock-start-time)
  3509. (defvar org-clock-marker (make-marker)
  3510. "Marker recording the last clock-in.")
  3511. (defvar org-clock-hd-marker (make-marker)
  3512. "Marker recording the last clock-in, but the headline position.")
  3513. (defvar org-clock-heading ""
  3514. "The heading of the current clock entry.")
  3515. (defun org-clock-is-active ()
  3516. "Return non-nil if clock is currently running.
  3517. The return value is actually the clock marker."
  3518. (marker-buffer org-clock-marker))
  3519. (eval-and-compile
  3520. (org-autoload
  3521. "org-clock"
  3522. '(org-clock-in org-clock-out org-clock-cancel
  3523. org-clock-goto org-clock-sum org-clock-display
  3524. org-clock-remove-overlays org-clock-report
  3525. org-clocktable-shift org-dblock-write:clocktable
  3526. org-get-clocktable org-resolve-clocks)))
  3527. (defun org-clock-update-time-maybe ()
  3528. "If this is a CLOCK line, update it and return t.
  3529. Otherwise, return nil."
  3530. (interactive)
  3531. (save-excursion
  3532. (beginning-of-line 1)
  3533. (skip-chars-forward " \t")
  3534. (when (looking-at org-clock-string)
  3535. (let ((re (concat "[ \t]*" org-clock-string
  3536. " *[[<]\\([^]>]+\\)[]>]\\(-+[[<]\\([^]>]+\\)[]>]"
  3537. "\\([ \t]*=>.*\\)?\\)?"))
  3538. ts te h m s neg)
  3539. (cond
  3540. ((not (looking-at re))
  3541. nil)
  3542. ((not (match-end 2))
  3543. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3544. (> org-clock-marker (point))
  3545. (<= org-clock-marker (point-at-eol)))
  3546. ;; The clock is running here
  3547. (setq org-clock-start-time
  3548. (apply 'encode-time
  3549. (org-parse-time-string (match-string 1))))
  3550. (org-clock-update-mode-line)))
  3551. (t
  3552. (and (match-end 4) (delete-region (match-beginning 4) (match-end 4)))
  3553. (end-of-line 1)
  3554. (setq ts (match-string 1)
  3555. te (match-string 3))
  3556. (setq s (- (org-float-time
  3557. (apply 'encode-time (org-parse-time-string te)))
  3558. (org-float-time
  3559. (apply 'encode-time (org-parse-time-string ts))))
  3560. neg (< s 0)
  3561. s (abs s)
  3562. h (floor (/ s 3600))
  3563. s (- s (* 3600 h))
  3564. m (floor (/ s 60))
  3565. s (- s (* 60 s)))
  3566. (insert " => " (format (if neg "-%d:%02d" "%2d:%02d") h m))
  3567. t))))))
  3568. (defun org-check-running-clock ()
  3569. "Check if the current buffer contains the running clock.
  3570. If yes, offer to stop it and to save the buffer with the changes."
  3571. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3572. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  3573. (buffer-name))))
  3574. (org-clock-out)
  3575. (when (y-or-n-p "Save changed buffer?")
  3576. (save-buffer))))
  3577. (defun org-clocktable-try-shift (dir n)
  3578. "Check if this line starts a clock table, if yes, shift the time block."
  3579. (when (org-match-line "^[ \t]*#\\+BEGIN:[ \t]+clocktable\\>")
  3580. (org-clocktable-shift dir n)))
  3581. ;; Autoload org-timer.el
  3582. (eval-and-compile
  3583. (org-autoload
  3584. "org-timer"
  3585. '(org-timer-start org-timer org-timer-item
  3586. org-timer-change-times-in-region
  3587. org-timer-set-timer
  3588. org-timer-reset-timers
  3589. org-timer-show-remaining-time)))
  3590. ;; Autoload org-feed.el
  3591. (eval-and-compile
  3592. (org-autoload
  3593. "org-feed"
  3594. '(org-feed-update org-feed-update-all org-feed-goto-inbox)))
  3595. ;; Autoload org-indent.el
  3596. ;; Define the variable already here, to make sure we have it.
  3597. (defvar org-indent-mode nil
  3598. "Non-nil if Org-Indent mode is enabled.
  3599. Use the command `org-indent-mode' to change this variable.")
  3600. (eval-and-compile
  3601. (org-autoload
  3602. "org-indent"
  3603. '(org-indent-mode)))
  3604. ;; Autoload org-mobile.el
  3605. (eval-and-compile
  3606. (org-autoload
  3607. "org-mobile"
  3608. '(org-mobile-push org-mobile-pull org-mobile-create-sumo-agenda)))
  3609. ;; Autoload archiving code
  3610. ;; The stuff that is needed for cycling and tags has to be defined here.
  3611. (defgroup org-archive nil
  3612. "Options concerning archiving in Org-mode."
  3613. :tag "Org Archive"
  3614. :group 'org-structure)
  3615. (defcustom org-archive-location "%s_archive::"
  3616. "The location where subtrees should be archived.
  3617. The value of this variable is a string, consisting of two parts,
  3618. separated by a double-colon. The first part is a filename and
  3619. the second part is a headline.
  3620. When the filename is omitted, archiving happens in the same file.
  3621. %s in the filename will be replaced by the current file
  3622. name (without the directory part). Archiving to a different file
  3623. is useful to keep archived entries from contributing to the
  3624. Org-mode Agenda.
  3625. The archived entries will be filed as subtrees of the specified
  3626. headline. When the headline is omitted, the subtrees are simply
  3627. filed away at the end of the file, as top-level entries. Also in
  3628. the heading you can use %s to represent the file name, this can be
  3629. useful when using the same archive for a number of different files.
  3630. Here are a few examples:
  3631. \"%s_archive::\"
  3632. If the current file is Projects.org, archive in file
  3633. Projects.org_archive, as top-level trees. This is the default.
  3634. \"::* Archived Tasks\"
  3635. Archive in the current file, under the top-level headline
  3636. \"* Archived Tasks\".
  3637. \"~/org/archive.org::\"
  3638. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  3639. \"~/org/archive.org::* From %s\"
  3640. Archive in file ~/org/archive.org (absolute path), under headlines
  3641. \"From FILENAME\" where file name is the current file name.
  3642. \"basement::** Finished Tasks\"
  3643. Archive in file ./basement (relative path), as level 3 trees
  3644. below the level 2 heading \"** Finished Tasks\".
  3645. You may set this option on a per-file basis by adding to the buffer a
  3646. line like
  3647. #+ARCHIVE: basement::** Finished Tasks
  3648. You may also define it locally for a subtree by setting an ARCHIVE property
  3649. in the entry. If such a property is found in an entry, or anywhere up
  3650. the hierarchy, it will be used."
  3651. :group 'org-archive
  3652. :type 'string)
  3653. (defcustom org-archive-tag "ARCHIVE"
  3654. "The tag that marks a subtree as archived.
  3655. An archived subtree does not open during visibility cycling, and does
  3656. not contribute to the agenda listings.
  3657. After changing this, font-lock must be restarted in the relevant buffers to
  3658. get the proper fontification."
  3659. :group 'org-archive
  3660. :group 'org-keywords
  3661. :type 'string)
  3662. (defcustom org-agenda-skip-archived-trees t
  3663. "Non-nil means the agenda will skip any items located in archived trees.
  3664. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  3665. variable is no longer recommended, you should leave it at the value t.
  3666. Instead, use the key `v' to cycle the archives-mode in the agenda."
  3667. :group 'org-archive
  3668. :group 'org-agenda-skip
  3669. :type 'boolean)
  3670. (defcustom org-columns-skip-archived-trees t
  3671. "Non-nil means ignore archived trees when creating column view."
  3672. :group 'org-archive
  3673. :group 'org-properties
  3674. :type 'boolean)
  3675. (defcustom org-cycle-open-archived-trees nil
  3676. "Non-nil means `org-cycle' will open archived trees.
  3677. An archived tree is a tree marked with the tag ARCHIVE.
  3678. When nil, archived trees will stay folded. You can still open them with
  3679. normal outline commands like `show-all', but not with the cycling commands."
  3680. :group 'org-archive
  3681. :group 'org-cycle
  3682. :type 'boolean)
  3683. (defcustom org-sparse-tree-open-archived-trees nil
  3684. "Non-nil means sparse tree construction shows matches in archived trees.
  3685. When nil, matches in these trees are highlighted, but the trees are kept in
  3686. collapsed state."
  3687. :group 'org-archive
  3688. :group 'org-sparse-trees
  3689. :type 'boolean)
  3690. (defun org-cycle-hide-archived-subtrees (state)
  3691. "Re-hide all archived subtrees after a visibility state change."
  3692. (when (and (not org-cycle-open-archived-trees)
  3693. (not (memq state '(overview folded))))
  3694. (save-excursion
  3695. (let* ((globalp (memq state '(contents all)))
  3696. (beg (if globalp (point-min) (point)))
  3697. (end (if globalp (point-max) (org-end-of-subtree t))))
  3698. (org-hide-archived-subtrees beg end)
  3699. (goto-char beg)
  3700. (if (looking-at (concat ".*:" org-archive-tag ":"))
  3701. (message "%s" (substitute-command-keys
  3702. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  3703. (defun org-force-cycle-archived ()
  3704. "Cycle subtree even if it is archived."
  3705. (interactive)
  3706. (setq this-command 'org-cycle)
  3707. (let ((org-cycle-open-archived-trees t))
  3708. (call-interactively 'org-cycle)))
  3709. (defun org-hide-archived-subtrees (beg end)
  3710. "Re-hide all archived subtrees after a visibility state change."
  3711. (save-excursion
  3712. (let* ((re (concat ":" org-archive-tag ":")))
  3713. (goto-char beg)
  3714. (while (re-search-forward re end t)
  3715. (when (org-at-heading-p)
  3716. (org-flag-subtree t)
  3717. (org-end-of-subtree t))))))
  3718. (defun org-flag-subtree (flag)
  3719. (save-excursion
  3720. (org-back-to-heading t)
  3721. (outline-end-of-heading)
  3722. (outline-flag-region (point)
  3723. (progn (org-end-of-subtree t) (point))
  3724. flag)))
  3725. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  3726. (eval-and-compile
  3727. (org-autoload "org-archive"
  3728. '(org-add-archive-files org-archive-subtree
  3729. org-archive-to-archive-sibling org-toggle-archive-tag
  3730. org-archive-subtree-default
  3731. org-archive-subtree-default-with-confirmation)))
  3732. ;; Autoload Column View Code
  3733. (declare-function org-columns-number-to-string "org-colview")
  3734. (declare-function org-columns-get-format-and-top-level "org-colview")
  3735. (declare-function org-columns-compute "org-colview")
  3736. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  3737. '(org-columns-number-to-string org-columns-get-format-and-top-level
  3738. org-columns-compute org-agenda-columns org-columns-remove-overlays
  3739. org-columns org-insert-columns-dblock org-dblock-write:columnview))
  3740. ;; Autoload ID code
  3741. (declare-function org-id-store-link "org-id")
  3742. (declare-function org-id-locations-load "org-id")
  3743. (declare-function org-id-locations-save "org-id")
  3744. (defvar org-id-track-globally)
  3745. (org-autoload "org-id"
  3746. '(org-id-get-create org-id-new org-id-copy org-id-get
  3747. org-id-get-with-outline-path-completion
  3748. org-id-get-with-outline-drilling org-id-store-link
  3749. org-id-goto org-id-find org-id-store-link))
  3750. ;; Autoload Plotting Code
  3751. (org-autoload "org-plot"
  3752. '(org-plot/gnuplot))
  3753. ;;; Variables for pre-computed regular expressions, all buffer local
  3754. (defvar org-drawer-regexp nil
  3755. "Matches first line of a hidden block.")
  3756. (make-variable-buffer-local 'org-drawer-regexp)
  3757. (defvar org-todo-regexp nil
  3758. "Matches any of the TODO state keywords.")
  3759. (make-variable-buffer-local 'org-todo-regexp)
  3760. (defvar org-not-done-regexp nil
  3761. "Matches any of the TODO state keywords except the last one.")
  3762. (make-variable-buffer-local 'org-not-done-regexp)
  3763. (defvar org-not-done-heading-regexp nil
  3764. "Matches a TODO headline that is not done.")
  3765. (make-variable-buffer-local 'org-not-done-regexp)
  3766. (defvar org-todo-line-regexp nil
  3767. "Matches a headline and puts TODO state into group 2 if present.")
  3768. (make-variable-buffer-local 'org-todo-line-regexp)
  3769. (defvar org-complex-heading-regexp nil
  3770. "Matches a headline and puts everything into groups:
  3771. group 1: the stars
  3772. group 2: The todo keyword, maybe
  3773. group 3: Priority cookie
  3774. group 4: True headline
  3775. group 5: Tags")
  3776. (make-variable-buffer-local 'org-complex-heading-regexp)
  3777. (defvar org-complex-heading-regexp-format nil
  3778. "Printf format to make regexp to match an exact headline.
  3779. This regexp will match the headline of any node which has the
  3780. exact headline text that is put into the format, but may have any
  3781. TODO state, priority and tags.")
  3782. (make-variable-buffer-local 'org-complex-heading-regexp-format)
  3783. (defvar org-todo-line-tags-regexp nil
  3784. "Matches a headline and puts TODO state into group 2 if present.
  3785. Also put tags into group 4 if tags are present.")
  3786. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  3787. (defvar org-ds-keyword-length 12
  3788. "Maximum length of the Deadline and SCHEDULED keywords.")
  3789. (make-variable-buffer-local 'org-ds-keyword-length)
  3790. (defvar org-deadline-regexp nil
  3791. "Matches the DEADLINE keyword.")
  3792. (make-variable-buffer-local 'org-deadline-regexp)
  3793. (defvar org-deadline-time-regexp nil
  3794. "Matches the DEADLINE keyword together with a time stamp.")
  3795. (make-variable-buffer-local 'org-deadline-time-regexp)
  3796. (defvar org-deadline-line-regexp nil
  3797. "Matches the DEADLINE keyword and the rest of the line.")
  3798. (make-variable-buffer-local 'org-deadline-line-regexp)
  3799. (defvar org-scheduled-regexp nil
  3800. "Matches the SCHEDULED keyword.")
  3801. (make-variable-buffer-local 'org-scheduled-regexp)
  3802. (defvar org-scheduled-time-regexp nil
  3803. "Matches the SCHEDULED keyword together with a time stamp.")
  3804. (make-variable-buffer-local 'org-scheduled-time-regexp)
  3805. (defvar org-closed-time-regexp nil
  3806. "Matches the CLOSED keyword together with a time stamp.")
  3807. (make-variable-buffer-local 'org-closed-time-regexp)
  3808. (defvar org-keyword-time-regexp nil
  3809. "Matches any of the 4 keywords, together with the time stamp.")
  3810. (make-variable-buffer-local 'org-keyword-time-regexp)
  3811. (defvar org-keyword-time-not-clock-regexp nil
  3812. "Matches any of the 3 keywords, together with the time stamp.")
  3813. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  3814. (defvar org-maybe-keyword-time-regexp nil
  3815. "Matches a timestamp, possibly preceded by a keyword.")
  3816. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  3817. (defvar org-planning-or-clock-line-re nil
  3818. "Matches a line with planning or clock info.")
  3819. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  3820. (defvar org-all-time-keywords nil
  3821. "List of time keywords.")
  3822. (make-variable-buffer-local 'org-all-time-keywords)
  3823. (defconst org-plain-time-of-day-regexp
  3824. (concat
  3825. "\\(\\<[012]?[0-9]"
  3826. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3827. "\\(--?"
  3828. "\\(\\<[012]?[0-9]"
  3829. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3830. "\\)?")
  3831. "Regular expression to match a plain time or time range.
  3832. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  3833. groups carry important information:
  3834. 0 the full match
  3835. 1 the first time, range or not
  3836. 8 the second time, if it is a range.")
  3837. (defconst org-plain-time-extension-regexp
  3838. (concat
  3839. "\\(\\<[012]?[0-9]"
  3840. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3841. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  3842. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  3843. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  3844. groups carry important information:
  3845. 0 the full match
  3846. 7 hours of duration
  3847. 9 minutes of duration")
  3848. (defconst org-stamp-time-of-day-regexp
  3849. (concat
  3850. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  3851. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  3852. "\\(--?"
  3853. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  3854. "Regular expression to match a timestamp time or time range.
  3855. After a match, the following groups carry important information:
  3856. 0 the full match
  3857. 1 date plus weekday, for back referencing to make sure both times are on the same day
  3858. 2 the first time, range or not
  3859. 4 the second time, if it is a range.")
  3860. (defconst org-startup-options
  3861. '(("fold" org-startup-folded t)
  3862. ("overview" org-startup-folded t)
  3863. ("nofold" org-startup-folded nil)
  3864. ("showall" org-startup-folded nil)
  3865. ("showeverything" org-startup-folded showeverything)
  3866. ("content" org-startup-folded content)
  3867. ("indent" org-startup-indented t)
  3868. ("noindent" org-startup-indented nil)
  3869. ("hidestars" org-hide-leading-stars t)
  3870. ("showstars" org-hide-leading-stars nil)
  3871. ("odd" org-odd-levels-only t)
  3872. ("oddeven" org-odd-levels-only nil)
  3873. ("align" org-startup-align-all-tables t)
  3874. ("noalign" org-startup-align-all-tables nil)
  3875. ("inlineimages" org-startup-with-inline-images t)
  3876. ("noinlineimages" org-startup-with-inline-images nil)
  3877. ("customtime" org-display-custom-times t)
  3878. ("logdone" org-log-done time)
  3879. ("lognotedone" org-log-done note)
  3880. ("nologdone" org-log-done nil)
  3881. ("lognoteclock-out" org-log-note-clock-out t)
  3882. ("nolognoteclock-out" org-log-note-clock-out nil)
  3883. ("logrepeat" org-log-repeat state)
  3884. ("lognoterepeat" org-log-repeat note)
  3885. ("nologrepeat" org-log-repeat nil)
  3886. ("logreschedule" org-log-reschedule time)
  3887. ("lognotereschedule" org-log-reschedule note)
  3888. ("nologreschedule" org-log-reschedule nil)
  3889. ("logredeadline" org-log-redeadline time)
  3890. ("lognoteredeadline" org-log-redeadline note)
  3891. ("nologredeadline" org-log-redeadline nil)
  3892. ("logrefile" org-log-refile time)
  3893. ("lognoterefile" org-log-refile note)
  3894. ("nologrefile" org-log-refile nil)
  3895. ("fninline" org-footnote-define-inline t)
  3896. ("nofninline" org-footnote-define-inline nil)
  3897. ("fnlocal" org-footnote-section nil)
  3898. ("fnauto" org-footnote-auto-label t)
  3899. ("fnprompt" org-footnote-auto-label nil)
  3900. ("fnconfirm" org-footnote-auto-label confirm)
  3901. ("fnplain" org-footnote-auto-label plain)
  3902. ("fnadjust" org-footnote-auto-adjust t)
  3903. ("nofnadjust" org-footnote-auto-adjust nil)
  3904. ("constcgs" constants-unit-system cgs)
  3905. ("constSI" constants-unit-system SI)
  3906. ("noptag" org-tag-persistent-alist nil)
  3907. ("hideblocks" org-hide-block-startup t)
  3908. ("nohideblocks" org-hide-block-startup nil)
  3909. ("beamer" org-startup-with-beamer-mode t)
  3910. ("entitiespretty" org-pretty-entities t)
  3911. ("entitiesplain" org-pretty-entities nil))
  3912. "Variable associated with STARTUP options for org-mode.
  3913. Each element is a list of three items: The startup options as written
  3914. in the #+STARTUP line, the corresponding variable, and the value to
  3915. set this variable to if the option is found. An optional forth element PUSH
  3916. means to push this value onto the list in the variable.")
  3917. (defun org-update-property-plist (key val props)
  3918. "Update PROPS with KEY and VAL."
  3919. (let* ((appending (string= "+" (substring key (- (length key) 1))))
  3920. (key (if appending (substring key 0 (- (length key) 1)) key))
  3921. (remainder (org-remove-if (lambda (p) (string= (car p) key)) props))
  3922. (previous (cdr (assoc key props))))
  3923. (if appending
  3924. (cons (cons key (if previous (concat previous " " val) val)) remainder)
  3925. (cons (cons key val) remainder))))
  3926. (defconst org-block-regexp
  3927. "^[ \t]*#\\+begin_?\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_?\\1[ \t]*$"
  3928. "Regular expression for hiding blocks.")
  3929. (defconst org-heading-keyword-regexp-format
  3930. "^\\(\\*+\\)\\(?: +%s\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  3931. "Printf format for a regexp matching an headline with some keyword.
  3932. This regexp will match the headline of any node which has the
  3933. exact keyword that is put into the format. The keyword isn't in
  3934. any group by default, but the stars and the body are.")
  3935. (defconst org-heading-keyword-maybe-regexp-format
  3936. "^\\(\\*+\\)\\(?: +%s\\)?\\(?: +\\(.*?\\)\\)?[ \t]*$"
  3937. "Printf format for a regexp matching an headline, possibly with some keyword.
  3938. This regexp can match any headline with the specified keyword, or
  3939. without a keyword. The keyword isn't in any group by default,
  3940. but the stars and the body are.")
  3941. (defun org-set-regexps-and-options ()
  3942. "Precompute regular expressions for current buffer."
  3943. (when (eq major-mode 'org-mode)
  3944. (org-set-local 'org-todo-kwd-alist nil)
  3945. (org-set-local 'org-todo-key-alist nil)
  3946. (org-set-local 'org-todo-key-trigger nil)
  3947. (org-set-local 'org-todo-keywords-1 nil)
  3948. (org-set-local 'org-done-keywords nil)
  3949. (org-set-local 'org-todo-heads nil)
  3950. (org-set-local 'org-todo-sets nil)
  3951. (org-set-local 'org-todo-log-states nil)
  3952. (org-set-local 'org-file-properties nil)
  3953. (org-set-local 'org-file-tags nil)
  3954. (let ((re (org-make-options-regexp
  3955. '("CATEGORY" "TODO" "COLUMNS"
  3956. "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
  3957. "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE" "LATEX_CLASS"
  3958. "OPTIONS")
  3959. "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
  3960. (splitre "[ \t]+")
  3961. (scripts org-use-sub-superscripts)
  3962. kwds kws0 kwsa key log value cat arch tags const links hw dws
  3963. tail sep kws1 prio props ftags drawers beamer-p
  3964. ext-setup-or-nil setup-contents (start 0))
  3965. (save-excursion
  3966. (save-restriction
  3967. (widen)
  3968. (goto-char (point-min))
  3969. (while (or (and ext-setup-or-nil
  3970. (string-match re ext-setup-or-nil start)
  3971. (setq start (match-end 0)))
  3972. (and (setq ext-setup-or-nil nil start 0)
  3973. (re-search-forward re nil t)))
  3974. (setq key (upcase (match-string 1 ext-setup-or-nil))
  3975. value (org-match-string-no-properties 2 ext-setup-or-nil))
  3976. (if (stringp value) (setq value (org-trim value)))
  3977. (cond
  3978. ((equal key "CATEGORY")
  3979. (setq cat value))
  3980. ((member key '("SEQ_TODO" "TODO"))
  3981. (push (cons 'sequence (org-split-string value splitre)) kwds))
  3982. ((equal key "TYP_TODO")
  3983. (push (cons 'type (org-split-string value splitre)) kwds))
  3984. ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key)
  3985. ;; general TODO-like setup
  3986. (push (cons (intern (downcase (match-string 1 key)))
  3987. (org-split-string value splitre)) kwds))
  3988. ((equal key "TAGS")
  3989. (setq tags (append tags (if tags '("\\n") nil)
  3990. (org-split-string value splitre))))
  3991. ((equal key "COLUMNS")
  3992. (org-set-local 'org-columns-default-format value))
  3993. ((equal key "LINK")
  3994. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  3995. (push (cons (match-string 1 value)
  3996. (org-trim (match-string 2 value)))
  3997. links)))
  3998. ((equal key "PRIORITIES")
  3999. (setq prio (org-split-string value " +")))
  4000. ((equal key "PROPERTY")
  4001. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  4002. (setq props (org-update-property-plist (match-string 1 value)
  4003. (match-string 2 value)
  4004. props))))
  4005. ((equal key "FILETAGS")
  4006. (when (string-match "\\S-" value)
  4007. (setq ftags
  4008. (append
  4009. ftags
  4010. (apply 'append
  4011. (mapcar (lambda (x) (org-split-string x ":"))
  4012. (org-split-string value)))))))
  4013. ((equal key "DRAWERS")
  4014. (setq drawers (delete-dups (append org-drawers (org-split-string value splitre)))))
  4015. ((equal key "CONSTANTS")
  4016. (setq const (append const (org-split-string value splitre))))
  4017. ((equal key "STARTUP")
  4018. (let ((opts (org-split-string value splitre))
  4019. l var val)
  4020. (while (setq l (pop opts))
  4021. (when (setq l (assoc l org-startup-options))
  4022. (setq var (nth 1 l) val (nth 2 l))
  4023. (if (not (nth 3 l))
  4024. (set (make-local-variable var) val)
  4025. (if (not (listp (symbol-value var)))
  4026. (set (make-local-variable var) nil))
  4027. (set (make-local-variable var) (symbol-value var))
  4028. (add-to-list var val))))))
  4029. ((equal key "ARCHIVE")
  4030. (setq arch value)
  4031. (remove-text-properties 0 (length arch)
  4032. '(face t fontified t) arch))
  4033. ((equal key "LATEX_CLASS")
  4034. (setq beamer-p (equal value "beamer")))
  4035. ((equal key "OPTIONS")
  4036. (if (string-match "\\([ \t]\\|\\`\\)\\^:\\(t\\|nil\\|{}\\)" value)
  4037. (setq scripts (read (match-string 2 value)))))
  4038. ((equal key "SETUPFILE")
  4039. (setq setup-contents (org-file-contents
  4040. (expand-file-name
  4041. (org-remove-double-quotes value))
  4042. 'noerror))
  4043. (if (not ext-setup-or-nil)
  4044. (setq ext-setup-or-nil setup-contents start 0)
  4045. (setq ext-setup-or-nil
  4046. (concat (substring ext-setup-or-nil 0 start)
  4047. "\n" setup-contents "\n"
  4048. (substring ext-setup-or-nil start)))))))
  4049. ;; search for property blocks
  4050. (goto-char (point-min))
  4051. (while (re-search-forward org-block-regexp nil t)
  4052. (when (equal "PROPERTY" (upcase (match-string 1)))
  4053. (setq value (replace-regexp-in-string
  4054. "[\n\r]" " " (match-string 4)))
  4055. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  4056. (setq props (org-update-property-plist (match-string 1 value)
  4057. (match-string 2 value)
  4058. props)))))))
  4059. (org-set-local 'org-use-sub-superscripts scripts)
  4060. (when cat
  4061. (org-set-local 'org-category (intern cat))
  4062. (push (cons "CATEGORY" cat) props))
  4063. (when prio
  4064. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  4065. (setq prio (mapcar 'string-to-char prio))
  4066. (org-set-local 'org-highest-priority (nth 0 prio))
  4067. (org-set-local 'org-lowest-priority (nth 1 prio))
  4068. (org-set-local 'org-default-priority (nth 2 prio)))
  4069. (and props (org-set-local 'org-file-properties (nreverse props)))
  4070. (and ftags (org-set-local 'org-file-tags
  4071. (mapcar 'org-add-prop-inherited ftags)))
  4072. (and drawers (org-set-local 'org-drawers drawers))
  4073. (and arch (org-set-local 'org-archive-location arch))
  4074. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  4075. ;; Process the TODO keywords
  4076. (unless kwds
  4077. ;; Use the global values as if they had been given locally.
  4078. (setq kwds (default-value 'org-todo-keywords))
  4079. (if (stringp (car kwds))
  4080. (setq kwds (list (cons org-todo-interpretation
  4081. (default-value 'org-todo-keywords)))))
  4082. (setq kwds (reverse kwds)))
  4083. (setq kwds (nreverse kwds))
  4084. (let (inter kws kw)
  4085. (while (setq kws (pop kwds))
  4086. (let ((kws (or
  4087. (run-hook-with-args-until-success
  4088. 'org-todo-setup-filter-hook kws)
  4089. kws)))
  4090. (setq inter (pop kws) sep (member "|" kws)
  4091. kws0 (delete "|" (copy-sequence kws))
  4092. kwsa nil
  4093. kws1 (mapcar
  4094. (lambda (x)
  4095. ;; 1 2
  4096. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  4097. (progn
  4098. (setq kw (match-string 1 x)
  4099. key (and (match-end 2) (match-string 2 x))
  4100. log (org-extract-log-state-settings x))
  4101. (push (cons kw (and key (string-to-char key))) kwsa)
  4102. (and log (push log org-todo-log-states))
  4103. kw)
  4104. (error "Invalid TODO keyword %s" x)))
  4105. kws0)
  4106. kwsa (if kwsa (append '((:startgroup))
  4107. (nreverse kwsa)
  4108. '((:endgroup))))
  4109. hw (car kws1)
  4110. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  4111. tail (list inter hw (car dws) (org-last dws))))
  4112. (add-to-list 'org-todo-heads hw 'append)
  4113. (push kws1 org-todo-sets)
  4114. (setq org-done-keywords (append org-done-keywords dws nil))
  4115. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  4116. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  4117. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  4118. (setq org-todo-sets (nreverse org-todo-sets)
  4119. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  4120. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  4121. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  4122. ;; Process the constants
  4123. (when const
  4124. (let (e cst)
  4125. (while (setq e (pop const))
  4126. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  4127. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  4128. (setq org-table-formula-constants-local cst)))
  4129. ;; Process the tags.
  4130. (when tags
  4131. (let (e tgs)
  4132. (while (setq e (pop tags))
  4133. (cond
  4134. ((equal e "{") (push '(:startgroup) tgs))
  4135. ((equal e "}") (push '(:endgroup) tgs))
  4136. ((equal e "\\n") (push '(:newline) tgs))
  4137. ((string-match (org-re "^\\([[:alnum:]_@#%]+\\)(\\(.\\))$") e)
  4138. (push (cons (match-string 1 e)
  4139. (string-to-char (match-string 2 e)))
  4140. tgs))
  4141. (t (push (list e) tgs))))
  4142. (org-set-local 'org-tag-alist nil)
  4143. (while (setq e (pop tgs))
  4144. (or (and (stringp (car e))
  4145. (assoc (car e) org-tag-alist))
  4146. (push e org-tag-alist)))))
  4147. ;; Compute the regular expressions and other local variables.
  4148. ;; Using `org-outline-regexp-bol' would complicate them much,
  4149. ;; because of the fixed white space at the end of that string.
  4150. (if (not org-done-keywords)
  4151. (setq org-done-keywords (and org-todo-keywords-1
  4152. (list (org-last org-todo-keywords-1)))))
  4153. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  4154. (length org-scheduled-string)
  4155. (length org-clock-string)
  4156. (length org-closed-string)))
  4157. org-drawer-regexp
  4158. (concat "^[ \t]*:\\("
  4159. (mapconcat 'regexp-quote org-drawers "\\|")
  4160. "\\):[ \t]*$")
  4161. org-not-done-keywords
  4162. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  4163. org-todo-regexp
  4164. (concat "\\("
  4165. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  4166. "\\)")
  4167. org-not-done-regexp
  4168. (concat "\\("
  4169. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  4170. "\\)")
  4171. org-not-done-heading-regexp
  4172. (format org-heading-keyword-regexp-format org-not-done-regexp)
  4173. org-todo-line-regexp
  4174. (format org-heading-keyword-maybe-regexp-format org-todo-regexp)
  4175. org-complex-heading-regexp
  4176. (concat "^\\(\\*+\\)"
  4177. "\\(?: +" org-todo-regexp "\\)?"
  4178. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4179. "\\(?: +\\(.*?\\)\\)?"
  4180. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?")
  4181. "[ \t]*$")
  4182. org-complex-heading-regexp-format
  4183. (concat "^\\(\\*+\\)"
  4184. "\\(?: +" org-todo-regexp "\\)?"
  4185. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4186. "\\(?: +"
  4187. ;; Stats cookies can be stuck to body.
  4188. "\\(?:\\[[0-9%%/]+\\] *\\)?"
  4189. "\\(%s\\)"
  4190. "\\(?: *\\[[0-9%%/]+\\]\\)?"
  4191. "\\)"
  4192. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%%:]+:\\)\\)?")
  4193. "[ \t]*$")
  4194. org-todo-line-tags-regexp
  4195. (concat "^\\(\\*+\\)"
  4196. "\\(?: +" org-todo-regexp "\\)?"
  4197. "\\(?: +\\(.*?\\)\\)?"
  4198. (org-re "\\(?:[ \t]+\\(:[[:alnum:]:_@#%]+:\\)\\)?")
  4199. "[ \t]*$")
  4200. org-deadline-regexp (concat "\\<" org-deadline-string)
  4201. org-deadline-time-regexp
  4202. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  4203. org-deadline-line-regexp
  4204. (concat "\\<\\(" org-deadline-string "\\).*")
  4205. org-scheduled-regexp
  4206. (concat "\\<" org-scheduled-string)
  4207. org-scheduled-time-regexp
  4208. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  4209. org-closed-time-regexp
  4210. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  4211. org-keyword-time-regexp
  4212. (concat "\\<\\(" org-scheduled-string
  4213. "\\|" org-deadline-string
  4214. "\\|" org-closed-string
  4215. "\\|" org-clock-string "\\)"
  4216. " *[[<]\\([^]>]+\\)[]>]")
  4217. org-keyword-time-not-clock-regexp
  4218. (concat "\\<\\(" org-scheduled-string
  4219. "\\|" org-deadline-string
  4220. "\\|" org-closed-string
  4221. "\\)"
  4222. " *[[<]\\([^]>]+\\)[]>]")
  4223. org-maybe-keyword-time-regexp
  4224. (concat "\\(\\<\\(" org-scheduled-string
  4225. "\\|" org-deadline-string
  4226. "\\|" org-closed-string
  4227. "\\|" org-clock-string "\\)\\)?"
  4228. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  4229. org-planning-or-clock-line-re
  4230. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  4231. "\\|" org-deadline-string
  4232. "\\|" org-closed-string "\\|" org-clock-string
  4233. "\\)\\>\\)")
  4234. org-all-time-keywords
  4235. (mapcar (lambda (w) (substring w 0 -1))
  4236. (list org-scheduled-string org-deadline-string
  4237. org-clock-string org-closed-string))
  4238. )
  4239. (org-compute-latex-and-specials-regexp)
  4240. (org-set-font-lock-defaults))))
  4241. (defun org-file-contents (file &optional noerror)
  4242. "Return the contents of FILE, as a string."
  4243. (if (or (not file)
  4244. (not (file-readable-p file)))
  4245. (if noerror
  4246. (progn
  4247. (message "Cannot read file \"%s\"" file)
  4248. (ding) (sit-for 2)
  4249. "")
  4250. (error "Cannot read file \"%s\"" file))
  4251. (with-temp-buffer
  4252. (insert-file-contents file)
  4253. (buffer-string))))
  4254. (defun org-extract-log-state-settings (x)
  4255. "Extract the log state setting from a TODO keyword string.
  4256. This will extract info from a string like \"WAIT(w@/!)\"."
  4257. (let (kw key log1 log2)
  4258. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  4259. (setq kw (match-string 1 x)
  4260. key (and (match-end 2) (match-string 2 x))
  4261. log1 (and (match-end 3) (match-string 3 x))
  4262. log2 (and (match-end 4) (match-string 4 x)))
  4263. (and (or log1 log2)
  4264. (list kw
  4265. (and log1 (if (equal log1 "!") 'time 'note))
  4266. (and log2 (if (equal log2 "!") 'time 'note)))))))
  4267. (defun org-remove-keyword-keys (list)
  4268. "Remove a pair of parenthesis at the end of each string in LIST."
  4269. (mapcar (lambda (x)
  4270. (if (string-match "(.*)$" x)
  4271. (substring x 0 (match-beginning 0))
  4272. x))
  4273. list))
  4274. (defun org-assign-fast-keys (alist)
  4275. "Assign fast keys to a keyword-key alist.
  4276. Respect keys that are already there."
  4277. (let (new e (alt ?0))
  4278. (while (setq e (pop alist))
  4279. (if (or (memq (car e) '(:newline :endgroup :startgroup))
  4280. (cdr e)) ;; Key already assigned.
  4281. (push e new)
  4282. (let ((clist (string-to-list (downcase (car e))))
  4283. (used (append new alist)))
  4284. (when (= (car clist) ?@)
  4285. (pop clist))
  4286. (while (and clist (rassoc (car clist) used))
  4287. (pop clist))
  4288. (unless clist
  4289. (while (rassoc alt used)
  4290. (incf alt)))
  4291. (push (cons (car e) (or (car clist) alt)) new))))
  4292. (nreverse new)))
  4293. ;;; Some variables used in various places
  4294. (defvar org-window-configuration nil
  4295. "Used in various places to store a window configuration.")
  4296. (defvar org-selected-window nil
  4297. "Used in various places to store a window configuration.")
  4298. (defvar org-finish-function nil
  4299. "Function to be called when `C-c C-c' is used.
  4300. This is for getting out of special buffers like remember.")
  4301. ;; FIXME: Occasionally check by commenting these, to make sure
  4302. ;; no other functions uses these, forgetting to let-bind them.
  4303. (defvar entry)
  4304. (defvar last-state)
  4305. (defvar date)
  4306. ;; Defined somewhere in this file, but used before definition.
  4307. (defvar org-entities) ;; defined in org-entities.el
  4308. (defvar org-struct-menu)
  4309. (defvar org-org-menu)
  4310. (defvar org-tbl-menu)
  4311. ;;;; Define the Org-mode
  4312. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  4313. (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"))
  4314. ;; We use a before-change function to check if a table might need
  4315. ;; an update.
  4316. (defvar org-table-may-need-update t
  4317. "Indicates that a table might need an update.
  4318. This variable is set by `org-before-change-function'.
  4319. `org-table-align' sets it back to nil.")
  4320. (defun org-before-change-function (beg end)
  4321. "Every change indicates that a table might need an update."
  4322. (setq org-table-may-need-update t))
  4323. (defvar org-mode-map)
  4324. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  4325. (defvar org-inhibit-startup-visibility-stuff nil) ; Dynamically-scoped param.
  4326. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  4327. (defvar org-inhibit-logging nil) ; Dynamically-scoped param.
  4328. (defvar org-inhibit-blocking nil) ; Dynamically-scoped param.
  4329. (defvar org-table-buffer-is-an nil)
  4330. ;; `org-outline-regexp' ought to be a defconst but is let-binding in
  4331. ;; some places -- e.g. see the macro org-with-limited-levels.
  4332. ;;
  4333. ;; In Org buffers, the value of `outline-regexp' is that of
  4334. ;; `org-outline-regexp'. The only function still directly relying on
  4335. ;; `outline-regexp' is `org-overview' so that `org-cycle' can do its
  4336. ;; job when `orgstruct-mode' is active.
  4337. (defvar org-outline-regexp "\\*+ "
  4338. "Regexp to match Org headlines.")
  4339. (defconst org-outline-regexp-bol "^\\*+ "
  4340. "Regexp to match Org headlines.
  4341. This is similar to `org-outline-regexp' but additionally makes
  4342. sure that we are at the beginning of the line.")
  4343. (defconst org-heading-regexp "^\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  4344. "Matches an headline, putting stars and text into groups.
  4345. Stars are put in group 1 and the trimmed body in group 2.")
  4346. ;;;###autoload
  4347. (define-derived-mode org-mode outline-mode "Org"
  4348. "Outline-based notes management and organizer, alias
  4349. \"Carsten's outline-mode for keeping track of everything.\"
  4350. Org-mode develops organizational tasks around a NOTES file which
  4351. contains information about projects as plain text. Org-mode is
  4352. implemented on top of outline-mode, which is ideal to keep the content
  4353. of large files well structured. It supports ToDo items, deadlines and
  4354. time stamps, which magically appear in the diary listing of the Emacs
  4355. calendar. Tables are easily created with a built-in table editor.
  4356. Plain text URL-like links connect to websites, emails (VM), Usenet
  4357. messages (Gnus), BBDB entries, and any files related to the project.
  4358. For printing and sharing of notes, an Org-mode file (or a part of it)
  4359. can be exported as a structured ASCII or HTML file.
  4360. The following commands are available:
  4361. \\{org-mode-map}"
  4362. ;; Get rid of Outline menus, they are not needed
  4363. ;; Need to do this here because define-derived-mode sets up
  4364. ;; the keymap so late. Still, it is a waste to call this each time
  4365. ;; we switch another buffer into org-mode.
  4366. (if (featurep 'xemacs)
  4367. (when (boundp 'outline-mode-menu-heading)
  4368. ;; Assume this is Greg's port, it uses easymenu
  4369. (easy-menu-remove outline-mode-menu-heading)
  4370. (easy-menu-remove outline-mode-menu-show)
  4371. (easy-menu-remove outline-mode-menu-hide))
  4372. (define-key org-mode-map [menu-bar headings] 'undefined)
  4373. (define-key org-mode-map [menu-bar hide] 'undefined)
  4374. (define-key org-mode-map [menu-bar show] 'undefined))
  4375. (org-load-modules-maybe)
  4376. (easy-menu-add org-org-menu)
  4377. (easy-menu-add org-tbl-menu)
  4378. (org-install-agenda-files-menu)
  4379. (if org-descriptive-links (add-to-invisibility-spec '(org-link)))
  4380. (add-to-invisibility-spec '(org-cwidth))
  4381. (add-to-invisibility-spec '(org-hide-block . t))
  4382. (when (featurep 'xemacs)
  4383. (org-set-local 'line-move-ignore-invisible t))
  4384. (org-set-local 'outline-regexp org-outline-regexp)
  4385. (org-set-local 'outline-level 'org-outline-level)
  4386. (setq bidi-paragraph-direction 'left-to-right)
  4387. (when (and org-ellipsis
  4388. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  4389. (fboundp 'make-glyph-code))
  4390. (unless org-display-table
  4391. (setq org-display-table (make-display-table)))
  4392. (set-display-table-slot
  4393. org-display-table 4
  4394. (vconcat (mapcar
  4395. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  4396. org-ellipsis)))
  4397. (if (stringp org-ellipsis) org-ellipsis "..."))))
  4398. (setq buffer-display-table org-display-table))
  4399. (org-set-regexps-and-options)
  4400. (when (and org-tag-faces (not org-tags-special-faces-re))
  4401. ;; tag faces set outside customize.... force initialization.
  4402. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  4403. ;; Calc embedded
  4404. (org-set-local 'calc-embedded-open-mode "# ")
  4405. (modify-syntax-entry ?@ "w")
  4406. (if org-startup-truncated (setq truncate-lines t))
  4407. (org-set-local 'font-lock-unfontify-region-function
  4408. 'org-unfontify-region)
  4409. ;; Activate before-change-function
  4410. (org-set-local 'org-table-may-need-update t)
  4411. (org-add-hook 'before-change-functions 'org-before-change-function nil
  4412. 'local)
  4413. ;; Check for running clock before killing a buffer
  4414. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  4415. ;; Paragraphs and auto-filling
  4416. (org-set-autofill-regexps)
  4417. (setq indent-line-function 'org-indent-line-function)
  4418. (org-update-radio-target-regexp)
  4419. ;; Beginning/end of defun
  4420. (org-set-local 'beginning-of-defun-function 'org-beginning-of-defun)
  4421. (org-set-local 'end-of-defun-function 'org-end-of-defun)
  4422. ;; Next error for sparse trees
  4423. (org-set-local 'next-error-function 'org-occur-next-match)
  4424. ;; Make sure dependence stuff works reliably, even for users who set it
  4425. ;; too late :-(
  4426. (if org-enforce-todo-dependencies
  4427. (add-hook 'org-blocker-hook
  4428. 'org-block-todo-from-children-or-siblings-or-parent)
  4429. (remove-hook 'org-blocker-hook
  4430. 'org-block-todo-from-children-or-siblings-or-parent))
  4431. (if org-enforce-todo-checkbox-dependencies
  4432. (add-hook 'org-blocker-hook
  4433. 'org-block-todo-from-checkboxes)
  4434. (remove-hook 'org-blocker-hook
  4435. 'org-block-todo-from-checkboxes))
  4436. ;; Comment characters
  4437. (org-set-local 'comment-start "#")
  4438. (org-set-local 'comment-padding " ")
  4439. ;; Align options lines
  4440. (org-set-local
  4441. 'align-mode-rules-list
  4442. '((org-in-buffer-settings
  4443. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  4444. (modes . '(org-mode)))))
  4445. ;; Imenu
  4446. (org-set-local 'imenu-create-index-function
  4447. 'org-imenu-get-tree)
  4448. ;; Make isearch reveal context
  4449. (if (or (featurep 'xemacs)
  4450. (not (boundp 'outline-isearch-open-invisible-function)))
  4451. ;; Emacs 21 and XEmacs make use of the hook
  4452. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  4453. ;; Emacs 22 deals with this through a special variable
  4454. (org-set-local 'outline-isearch-open-invisible-function
  4455. (lambda (&rest ignore) (org-show-context 'isearch))))
  4456. ;; Turn on org-beamer-mode?
  4457. (and org-startup-with-beamer-mode (org-beamer-mode 1))
  4458. ;; Setup the pcomplete hooks
  4459. (set (make-local-variable 'pcomplete-command-completion-function)
  4460. 'org-pcomplete-initial)
  4461. (set (make-local-variable 'pcomplete-command-name-function)
  4462. 'org-command-at-point)
  4463. (set (make-local-variable 'pcomplete-default-completion-function)
  4464. 'ignore)
  4465. (set (make-local-variable 'pcomplete-parse-arguments-function)
  4466. 'org-parse-arguments)
  4467. (set (make-local-variable 'pcomplete-termination-string) "")
  4468. (set (make-local-variable 'face-remapping-alist)
  4469. '((default org-default)))
  4470. ;; If empty file that did not turn on org-mode automatically, make it to.
  4471. (if (and org-insert-mode-line-in-empty-file
  4472. (org-called-interactively-p 'any)
  4473. (= (point-min) (point-max)))
  4474. (insert "# -*- mode: org -*-\n\n"))
  4475. (unless org-inhibit-startup
  4476. (when org-startup-align-all-tables
  4477. (let ((bmp (buffer-modified-p)))
  4478. (org-table-map-tables 'org-table-align 'quietly)
  4479. (set-buffer-modified-p bmp)))
  4480. (when org-startup-with-inline-images
  4481. (org-display-inline-images))
  4482. (when org-startup-indented
  4483. (require 'org-indent)
  4484. (org-indent-mode 1))
  4485. (unless org-inhibit-startup-visibility-stuff
  4486. (org-set-startup-visibility))))
  4487. (when (fboundp 'abbrev-table-put)
  4488. (abbrev-table-put org-mode-abbrev-table
  4489. :parents (list text-mode-abbrev-table)))
  4490. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  4491. (defun org-current-time ()
  4492. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  4493. (if (> (car org-time-stamp-rounding-minutes) 1)
  4494. (let ((r (car org-time-stamp-rounding-minutes))
  4495. (time (decode-time)))
  4496. (apply 'encode-time
  4497. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  4498. (nthcdr 2 time))))
  4499. (current-time)))
  4500. (defun org-today ()
  4501. "Return today date, considering `org-extend-today-until'."
  4502. (time-to-days
  4503. (time-subtract (current-time)
  4504. (list 0 (* 3600 org-extend-today-until) 0))))
  4505. ;;;; Font-Lock stuff, including the activators
  4506. (defvar org-mouse-map (make-sparse-keymap))
  4507. (org-defkey org-mouse-map [mouse-2] 'org-open-at-mouse)
  4508. (org-defkey org-mouse-map [mouse-3] 'org-find-file-at-mouse)
  4509. (when org-mouse-1-follows-link
  4510. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  4511. (when org-tab-follows-link
  4512. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  4513. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  4514. (require 'font-lock)
  4515. (defconst org-non-link-chars "]\t\n\r<>")
  4516. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  4517. "shell" "elisp" "doi" "message"))
  4518. (defvar org-link-types-re nil
  4519. "Matches a link that has a url-like prefix like \"http:\"")
  4520. (defvar org-link-re-with-space nil
  4521. "Matches a link with spaces, optional angular brackets around it.")
  4522. (defvar org-link-re-with-space2 nil
  4523. "Matches a link with spaces, optional angular brackets around it.")
  4524. (defvar org-link-re-with-space3 nil
  4525. "Matches a link with spaces, only for internal part in bracket links.")
  4526. (defvar org-angle-link-re nil
  4527. "Matches link with angular brackets, spaces are allowed.")
  4528. (defvar org-plain-link-re nil
  4529. "Matches plain link, without spaces.")
  4530. (defvar org-bracket-link-regexp nil
  4531. "Matches a link in double brackets.")
  4532. (defvar org-bracket-link-analytic-regexp nil
  4533. "Regular expression used to analyze links.
  4534. Here is what the match groups contain after a match:
  4535. 1: http:
  4536. 2: http
  4537. 3: path
  4538. 4: [desc]
  4539. 5: desc")
  4540. (defvar org-bracket-link-analytic-regexp++ nil
  4541. "Like `org-bracket-link-analytic-regexp', but include coderef internal type.")
  4542. (defvar org-any-link-re nil
  4543. "Regular expression matching any link.")
  4544. (defcustom org-match-sexp-depth 3
  4545. "Number of stacked braces for sub/superscript matching.
  4546. This has to be set before loading org.el to be effective."
  4547. :group 'org-export-translation ; ??????????????????????????/
  4548. :type 'integer)
  4549. (defun org-create-multibrace-regexp (left right n)
  4550. "Create a regular expression which will match a balanced sexp.
  4551. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  4552. as single character strings.
  4553. The regexp returned will match the entire expression including the
  4554. delimiters. It will also define a single group which contains the
  4555. match except for the outermost delimiters. The maximum depth of
  4556. stacked delimiters is N. Escaping delimiters is not possible."
  4557. (let* ((nothing (concat "[^" left right "]*?"))
  4558. (or "\\|")
  4559. (re nothing)
  4560. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  4561. (while (> n 1)
  4562. (setq n (1- n)
  4563. re (concat re or next)
  4564. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  4565. (concat left "\\(" re "\\)" right)))
  4566. (defvar org-match-substring-regexp
  4567. (concat
  4568. "\\([^\\]\\|^\\)\\([_^]\\)\\("
  4569. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4570. "\\|"
  4571. "\\(" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  4572. "\\|"
  4573. "\\(\\(?:\\*\\|[-+]?[^-+*!@#$%^_ \t\r\n,:\"?<>~;./{}=()]+\\)\\)\\)")
  4574. "The regular expression matching a sub- or superscript.")
  4575. (defvar org-match-substring-with-braces-regexp
  4576. (concat
  4577. "\\([^\\]\\|^\\)\\([_^]\\)\\("
  4578. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4579. "\\)")
  4580. "The regular expression matching a sub- or superscript, forcing braces.")
  4581. (defun org-make-link-regexps ()
  4582. "Update the link regular expressions.
  4583. This should be called after the variable `org-link-types' has changed."
  4584. (setq org-link-types-re
  4585. (concat
  4586. "\\`\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):")
  4587. org-link-re-with-space
  4588. (concat
  4589. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4590. "\\([^" org-non-link-chars " ]"
  4591. "[^" org-non-link-chars "]*"
  4592. "[^" org-non-link-chars " ]\\)>?")
  4593. org-link-re-with-space2
  4594. (concat
  4595. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4596. "\\([^" org-non-link-chars " ]"
  4597. "[^\t\n\r]*"
  4598. "[^" org-non-link-chars " ]\\)>?")
  4599. org-link-re-with-space3
  4600. (concat
  4601. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4602. "\\([^" org-non-link-chars " ]"
  4603. "[^\t\n\r]*\\)")
  4604. org-angle-link-re
  4605. (concat
  4606. "<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4607. "\\([^" org-non-link-chars " ]"
  4608. "[^" org-non-link-chars "]*"
  4609. "\\)>")
  4610. org-plain-link-re
  4611. (concat
  4612. "\\<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4613. (org-re "\\([^ \t\n()<>]+\\(?:([[:word:]0-9_]+)\\|\\([^[:punct:] \t\n]\\|/\\)\\)\\)"))
  4614. ;; "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  4615. org-bracket-link-regexp
  4616. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  4617. org-bracket-link-analytic-regexp
  4618. (concat
  4619. "\\[\\["
  4620. "\\(\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):\\)?"
  4621. "\\([^]]+\\)"
  4622. "\\]"
  4623. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4624. "\\]")
  4625. org-bracket-link-analytic-regexp++
  4626. (concat
  4627. "\\[\\["
  4628. "\\(\\(" (mapconcat 'regexp-quote (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  4629. "\\([^]]+\\)"
  4630. "\\]"
  4631. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4632. "\\]")
  4633. org-any-link-re
  4634. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  4635. org-angle-link-re "\\)\\|\\("
  4636. org-plain-link-re "\\)")))
  4637. (org-make-link-regexps)
  4638. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^\r\n>]*?\\)>"
  4639. "Regular expression for fast time stamp matching.")
  4640. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?\\)[]>]"
  4641. "Regular expression for fast time stamp matching.")
  4642. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]+0-9>\r\n -]*\\)\\( \\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4643. "Regular expression matching time strings for analysis.
  4644. This one does not require the space after the date, so it can be used
  4645. on a string that terminates immediately after the date.")
  4646. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]+0-9>\r\n -]*\\)\\( \\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4647. "Regular expression matching time strings for analysis.")
  4648. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  4649. "Regular expression matching time stamps, with groups.")
  4650. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  4651. "Regular expression matching time stamps (also [..]), with groups.")
  4652. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  4653. "Regular expression matching a time stamp range.")
  4654. (defconst org-tr-regexp-both
  4655. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  4656. "Regular expression matching a time stamp range.")
  4657. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  4658. org-ts-regexp "\\)?")
  4659. "Regular expression matching a time stamp or time stamp range.")
  4660. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  4661. org-ts-regexp-both "\\)?")
  4662. "Regular expression matching a time stamp or time stamp range.
  4663. The time stamps may be either active or inactive.")
  4664. (defvar org-emph-face nil)
  4665. (defun org-do-emphasis-faces (limit)
  4666. "Run through the buffer and add overlays to emphasized strings."
  4667. (let (rtn a)
  4668. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  4669. (if (not (= (char-after (match-beginning 3))
  4670. (char-after (match-beginning 4))))
  4671. (progn
  4672. (setq rtn t)
  4673. (setq a (assoc (match-string 3) org-emphasis-alist))
  4674. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  4675. 'face
  4676. (nth 1 a))
  4677. (and (nth 4 a)
  4678. (org-remove-flyspell-overlays-in
  4679. (match-beginning 0) (match-end 0)))
  4680. (add-text-properties (match-beginning 2) (match-end 2)
  4681. '(font-lock-multiline t org-emphasis t))
  4682. (when org-hide-emphasis-markers
  4683. (add-text-properties (match-end 4) (match-beginning 5)
  4684. '(invisible org-link))
  4685. (add-text-properties (match-beginning 3) (match-end 3)
  4686. '(invisible org-link)))))
  4687. (backward-char 1))
  4688. rtn))
  4689. (defun org-emphasize (&optional char)
  4690. "Insert or change an emphasis, i.e. a font like bold or italic.
  4691. If there is an active region, change that region to a new emphasis.
  4692. If there is no region, just insert the marker characters and position
  4693. the cursor between them.
  4694. CHAR should be either the marker character, or the first character of the
  4695. HTML tag associated with that emphasis. If CHAR is a space, the means
  4696. to remove the emphasis of the selected region.
  4697. If char is not given (for example in an interactive call) it
  4698. will be prompted for."
  4699. (interactive)
  4700. (let ((eal org-emphasis-alist) e det
  4701. (erc org-emphasis-regexp-components)
  4702. (prompt "")
  4703. (string "") beg end move tag c s)
  4704. (if (org-region-active-p)
  4705. (setq beg (region-beginning) end (region-end)
  4706. string (buffer-substring beg end))
  4707. (setq move t))
  4708. (while (setq e (pop eal))
  4709. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  4710. c (aref tag 0))
  4711. (push (cons c (string-to-char (car e))) det)
  4712. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  4713. (substring tag 1)))))
  4714. (setq det (nreverse det))
  4715. (unless char
  4716. (message "%s" (concat "Emphasis marker or tag:" prompt))
  4717. (setq char (read-char-exclusive)))
  4718. (setq char (or (cdr (assoc char det)) char))
  4719. (if (equal char ?\ )
  4720. (setq s "" move nil)
  4721. (unless (assoc (char-to-string char) org-emphasis-alist)
  4722. (error "No such emphasis marker: \"%c\"" char))
  4723. (setq s (char-to-string char)))
  4724. (while (and (> (length string) 1)
  4725. (equal (substring string 0 1) (substring string -1))
  4726. (assoc (substring string 0 1) org-emphasis-alist))
  4727. (setq string (substring string 1 -1)))
  4728. (setq string (concat s string s))
  4729. (if beg (delete-region beg end))
  4730. (unless (or (bolp)
  4731. (string-match (concat "[" (nth 0 erc) "\n]")
  4732. (char-to-string (char-before (point)))))
  4733. (insert " "))
  4734. (unless (or (eobp)
  4735. (string-match (concat "[" (nth 1 erc) "\n]")
  4736. (char-to-string (char-after (point)))))
  4737. (insert " ") (backward-char 1))
  4738. (insert string)
  4739. (and move (backward-char 1))))
  4740. (defconst org-nonsticky-props
  4741. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  4742. (defsubst org-rear-nonsticky-at (pos)
  4743. (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
  4744. (defun org-activate-plain-links (limit)
  4745. "Run through the buffer and add overlays to links."
  4746. (catch 'exit
  4747. (let (f)
  4748. (if (re-search-forward org-plain-link-re limit t)
  4749. (progn
  4750. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4751. (setq f (get-text-property (match-beginning 0) 'face))
  4752. (if (or (eq f 'org-tag)
  4753. (and (listp f) (memq 'org-tag f)))
  4754. nil
  4755. (add-text-properties (match-beginning 0) (match-end 0)
  4756. (list 'mouse-face 'highlight
  4757. 'face 'org-link
  4758. 'keymap org-mouse-map))
  4759. (org-rear-nonsticky-at (match-end 0)))
  4760. t)))))
  4761. (defun org-activate-code (limit)
  4762. (if (re-search-forward "^[ \t]*\\(:\\(?: .*\\|$\\)\n?\\)" limit t)
  4763. (progn
  4764. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4765. (remove-text-properties (match-beginning 0) (match-end 0)
  4766. '(display t invisible t intangible t))
  4767. t)))
  4768. (defcustom org-src-fontify-natively nil
  4769. "When non-nil, fontify code in code blocks."
  4770. :type 'boolean
  4771. :version "24.1"
  4772. :group 'org-appearance
  4773. :group 'org-babel)
  4774. (defun org-fontify-meta-lines-and-blocks (limit)
  4775. (condition-case nil
  4776. (org-fontify-meta-lines-and-blocks-1 limit)
  4777. (error (message "org-mode fontification error"))))
  4778. (defun org-fontify-meta-lines-and-blocks-1 (limit)
  4779. "Fontify #+ lines and blocks, in the correct ways."
  4780. (let ((case-fold-search t))
  4781. (if (re-search-forward
  4782. "^\\([ \t]*#\\+\\(\\([a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)[ \t]*\\(\\([^ \t\n]*\\)[ \t]*\\(.*\\)\\)\\)"
  4783. limit t)
  4784. (let ((beg (match-beginning 0))
  4785. (block-start (match-end 0))
  4786. (block-end nil)
  4787. (lang (match-string 7))
  4788. (beg1 (line-beginning-position 2))
  4789. (dc1 (downcase (match-string 2)))
  4790. (dc3 (downcase (match-string 3)))
  4791. end end1 quoting block-type ovl)
  4792. (cond
  4793. ((member dc1 '("html:" "ascii:" "latex:" "docbook:"))
  4794. ;; a single line of backend-specific content
  4795. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4796. (remove-text-properties (match-beginning 0) (match-end 0)
  4797. '(display t invisible t intangible t))
  4798. (add-text-properties (match-beginning 1) (match-end 3)
  4799. '(font-lock-fontified t face org-meta-line))
  4800. (add-text-properties (match-beginning 6) (+ (match-end 6) 1)
  4801. '(font-lock-fontified t face org-block))
  4802. ; for backend-specific code
  4803. t)
  4804. ((and (match-end 4) (equal dc3 "begin"))
  4805. ;; Truly a block
  4806. (setq block-type (downcase (match-string 5))
  4807. quoting (member block-type org-protecting-blocks))
  4808. (when (re-search-forward
  4809. (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
  4810. nil t) ;; on purpose, we look further than LIMIT
  4811. (setq end (match-end 0) end1 (1- (match-beginning 0)))
  4812. (setq block-end (match-beginning 0))
  4813. (when quoting
  4814. (remove-text-properties beg end
  4815. '(display t invisible t intangible t)))
  4816. (add-text-properties
  4817. beg end
  4818. '(font-lock-fontified t font-lock-multiline t))
  4819. (add-text-properties beg beg1 '(face org-meta-line))
  4820. (add-text-properties end1 (min (point-max) (1+ end))
  4821. '(face org-meta-line)) ; for end_src
  4822. (cond
  4823. ((and lang (not (string= lang "")) org-src-fontify-natively)
  4824. (org-src-font-lock-fontify-block lang block-start block-end)
  4825. ;; remove old background overlays
  4826. (mapc (lambda (ov)
  4827. (if (eq (overlay-get ov 'face) 'org-block-background)
  4828. (delete-overlay ov)))
  4829. (overlays-at (/ (+ beg1 block-end) 2)))
  4830. ;; add a background overlay
  4831. (setq ovl (make-overlay beg1 block-end))
  4832. (overlay-put ovl 'face 'org-block-background)
  4833. (overlay-put ovl 'evaporate t)) ;; make it go away when empty
  4834. (quoting
  4835. (add-text-properties beg1 (min (point-max) (1+ end1))
  4836. '(face org-block))) ; end of source block
  4837. ((not org-fontify-quote-and-verse-blocks))
  4838. ((string= block-type "quote")
  4839. (add-text-properties beg1 (1+ end1) '(face org-quote)))
  4840. ((string= block-type "verse")
  4841. (add-text-properties beg1 (1+ end1) '(face org-verse))))
  4842. (add-text-properties beg beg1 '(face org-block-begin-line))
  4843. (add-text-properties (1+ end) (1+ end1) '(face org-block-end-line))
  4844. t))
  4845. ((member dc1 '("title:" "author:" "email:" "date:"))
  4846. (add-text-properties
  4847. beg (match-end 3)
  4848. (if (member (intern (substring dc1 0 -1)) org-hidden-keywords)
  4849. '(font-lock-fontified t invisible t)
  4850. '(font-lock-fontified t face org-document-info-keyword)))
  4851. (add-text-properties
  4852. (match-beginning 6) (match-end 6)
  4853. (if (string-equal dc1 "title:")
  4854. '(font-lock-fontified t face org-document-title)
  4855. '(font-lock-fontified t face org-document-info))))
  4856. ((not (member (char-after beg) '(?\ ?\t)))
  4857. ;; just any other in-buffer setting, but not indented
  4858. (add-text-properties
  4859. beg (1+ (match-end 0))
  4860. '(font-lock-fontified t face org-meta-line))
  4861. t)
  4862. ((or (member dc1 '("begin:" "end:" "caption:" "label:"
  4863. "orgtbl:" "tblfm:" "tblname:" "results:"
  4864. "call:" "header:" "headers:" "name:"))
  4865. (and (match-end 4) (equal dc3 "attr")))
  4866. (add-text-properties
  4867. beg (match-end 0)
  4868. '(font-lock-fontified t face org-meta-line))
  4869. t)
  4870. ((member dc3 '(" " ""))
  4871. (add-text-properties
  4872. beg (match-end 0)
  4873. '(font-lock-fontified t face font-lock-comment-face)))
  4874. (t nil))))))
  4875. (defun org-activate-angle-links (limit)
  4876. "Run through the buffer and add overlays to links."
  4877. (if (re-search-forward org-angle-link-re limit t)
  4878. (progn
  4879. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4880. (add-text-properties (match-beginning 0) (match-end 0)
  4881. (list 'mouse-face 'highlight
  4882. 'keymap org-mouse-map))
  4883. (org-rear-nonsticky-at (match-end 0))
  4884. t)))
  4885. (defun org-activate-footnote-links (limit)
  4886. "Run through the buffer and add overlays to footnotes."
  4887. (let ((fn (org-footnote-next-reference-or-definition limit)))
  4888. (when fn
  4889. (let ((beg (nth 1 fn)) (end (nth 2 fn)))
  4890. (org-remove-flyspell-overlays-in beg end)
  4891. (add-text-properties beg end
  4892. (list 'mouse-face 'highlight
  4893. 'keymap org-mouse-map
  4894. 'help-echo
  4895. (if (= (point-at-bol) beg)
  4896. "Footnote definition"
  4897. "Footnote reference")
  4898. 'font-lock-fontified t
  4899. 'font-lock-multiline t
  4900. 'face 'org-footnote))))))
  4901. (defun org-activate-bracket-links (limit)
  4902. "Run through the buffer and add overlays to bracketed links."
  4903. (if (re-search-forward org-bracket-link-regexp limit t)
  4904. (let* ((help (concat "LINK: "
  4905. (org-match-string-no-properties 1)))
  4906. ;; FIXME: above we should remove the escapes.
  4907. ;; but that requires another match, protecting match data,
  4908. ;; a lot of overhead for font-lock.
  4909. (ip (org-maybe-intangible
  4910. (list 'invisible 'org-link
  4911. 'keymap org-mouse-map 'mouse-face 'highlight
  4912. 'font-lock-multiline t 'help-echo help)))
  4913. (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
  4914. 'font-lock-multiline t 'help-echo help)))
  4915. ;; We need to remove the invisible property here. Table narrowing
  4916. ;; may have made some of this invisible.
  4917. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4918. (remove-text-properties (match-beginning 0) (match-end 0)
  4919. '(invisible nil))
  4920. (if (match-end 3)
  4921. (progn
  4922. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  4923. (org-rear-nonsticky-at (match-beginning 3))
  4924. (add-text-properties (match-beginning 3) (match-end 3) vp)
  4925. (org-rear-nonsticky-at (match-end 3))
  4926. (add-text-properties (match-end 3) (match-end 0) ip)
  4927. (org-rear-nonsticky-at (match-end 0)))
  4928. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  4929. (org-rear-nonsticky-at (match-beginning 1))
  4930. (add-text-properties (match-beginning 1) (match-end 1) vp)
  4931. (org-rear-nonsticky-at (match-end 1))
  4932. (add-text-properties (match-end 1) (match-end 0) ip)
  4933. (org-rear-nonsticky-at (match-end 0)))
  4934. t)))
  4935. (defun org-activate-dates (limit)
  4936. "Run through the buffer and add overlays to dates."
  4937. (if (re-search-forward org-tsr-regexp-both limit t)
  4938. (progn
  4939. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4940. (add-text-properties (match-beginning 0) (match-end 0)
  4941. (list 'mouse-face 'highlight
  4942. 'keymap org-mouse-map))
  4943. (org-rear-nonsticky-at (match-end 0))
  4944. (when org-display-custom-times
  4945. (if (match-end 3)
  4946. (org-display-custom-time (match-beginning 3) (match-end 3)))
  4947. (org-display-custom-time (match-beginning 1) (match-end 1)))
  4948. t)))
  4949. (defvar org-target-link-regexp nil
  4950. "Regular expression matching radio targets in plain text.")
  4951. (make-variable-buffer-local 'org-target-link-regexp)
  4952. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  4953. "Regular expression matching a link target.")
  4954. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  4955. "Regular expression matching a radio target.")
  4956. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  4957. "Regular expression matching any target.")
  4958. (defun org-activate-target-links (limit)
  4959. "Run through the buffer and add overlays to target matches."
  4960. (when org-target-link-regexp
  4961. (let ((case-fold-search t))
  4962. (if (re-search-forward org-target-link-regexp limit t)
  4963. (progn
  4964. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4965. (add-text-properties (match-beginning 0) (match-end 0)
  4966. (list 'mouse-face 'highlight
  4967. 'keymap org-mouse-map
  4968. 'help-echo "Radio target link"
  4969. 'org-linked-text t))
  4970. (org-rear-nonsticky-at (match-end 0))
  4971. t)))))
  4972. (defun org-update-radio-target-regexp ()
  4973. "Find all radio targets in this file and update the regular expression."
  4974. (interactive)
  4975. (when (memq 'radio org-activate-links)
  4976. (setq org-target-link-regexp
  4977. (org-make-target-link-regexp (org-all-targets 'radio)))
  4978. (org-restart-font-lock)))
  4979. (defun org-hide-wide-columns (limit)
  4980. (let (s e)
  4981. (setq s (text-property-any (point) (or limit (point-max))
  4982. 'org-cwidth t))
  4983. (when s
  4984. (setq e (next-single-property-change s 'org-cwidth))
  4985. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  4986. (goto-char e)
  4987. t)))
  4988. (defvar org-latex-and-specials-regexp nil
  4989. "Regular expression for highlighting export special stuff.")
  4990. (defvar org-match-substring-regexp)
  4991. (defvar org-match-substring-with-braces-regexp)
  4992. ;; This should be with the exporter code, but we also use if for font-locking
  4993. (defconst org-export-html-special-string-regexps
  4994. '(("\\\\-" . "&shy;")
  4995. ("---\\([^-]\\)" . "&mdash;\\1")
  4996. ("--\\([^-]\\)" . "&ndash;\\1")
  4997. ("\\.\\.\\." . "&hellip;"))
  4998. "Regular expressions for special string conversion.")
  4999. (defun org-compute-latex-and-specials-regexp ()
  5000. "Compute regular expression for stuff treated specially by exporters."
  5001. (if (not org-highlight-latex-fragments-and-specials)
  5002. (org-set-local 'org-latex-and-specials-regexp nil)
  5003. (require 'org-exp)
  5004. (let*
  5005. ((matchers (plist-get org-format-latex-options :matchers))
  5006. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  5007. org-latex-regexps)))
  5008. (org-export-allow-BIND nil)
  5009. (options (org-combine-plists (org-default-export-plist)
  5010. (org-infile-export-plist)))
  5011. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  5012. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  5013. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  5014. (org-export-html-expand (plist-get options :expand-quoted-html))
  5015. (org-export-with-special-strings (plist-get options :special-strings))
  5016. (re-sub
  5017. (cond
  5018. ((equal org-export-with-sub-superscripts '{})
  5019. (list org-match-substring-with-braces-regexp))
  5020. (org-export-with-sub-superscripts
  5021. (list org-match-substring-regexp))
  5022. (t nil)))
  5023. (re-latex
  5024. (if org-export-with-LaTeX-fragments
  5025. (mapcar (lambda (x) (nth 1 x)) latexs)))
  5026. (re-macros
  5027. (if org-export-with-TeX-macros
  5028. (list (concat "\\\\"
  5029. (regexp-opt
  5030. (append
  5031. (delq nil
  5032. (mapcar 'car-safe
  5033. (append org-entities-user
  5034. org-entities)))
  5035. (if (boundp 'org-latex-entities)
  5036. (mapcar (lambda (x)
  5037. (or (car-safe x) x))
  5038. org-latex-entities)
  5039. nil))
  5040. 'words))) ; FIXME
  5041. ))
  5042. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  5043. (re-special (if org-export-with-special-strings
  5044. (mapcar (lambda (x) (car x))
  5045. org-export-html-special-string-regexps)))
  5046. (re-rest
  5047. (delq nil
  5048. (list
  5049. (if org-export-html-expand "@<[^>\n]+>")
  5050. ))))
  5051. (org-set-local
  5052. 'org-latex-and-specials-regexp
  5053. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  5054. re-rest) "\\|")))))
  5055. (defun org-do-latex-and-special-faces (limit)
  5056. "Run through the buffer and add overlays to links."
  5057. (when org-latex-and-specials-regexp
  5058. (let (rtn d)
  5059. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  5060. limit t))
  5061. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  5062. 'face))
  5063. '(org-code org-verbatim underline)))
  5064. (progn
  5065. (setq rtn t
  5066. d (cond ((member (char-after (1+ (match-beginning 0)))
  5067. '(?_ ?^)) 1)
  5068. (t 0)))
  5069. (font-lock-prepend-text-property
  5070. (+ d (match-beginning 0)) (match-end 0)
  5071. 'face 'org-latex-and-export-specials)
  5072. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  5073. '(font-lock-multiline t)))))
  5074. rtn)))
  5075. (defun org-restart-font-lock ()
  5076. "Restart `font-lock-mode', to force refontification."
  5077. (when (and (boundp 'font-lock-mode) font-lock-mode)
  5078. (font-lock-mode -1)
  5079. (font-lock-mode 1)))
  5080. (defun org-all-targets (&optional radio)
  5081. "Return a list of all targets in this file.
  5082. With optional argument RADIO, only find radio targets."
  5083. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  5084. rtn)
  5085. (save-excursion
  5086. (goto-char (point-min))
  5087. (while (re-search-forward re nil t)
  5088. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  5089. rtn)))
  5090. (defun org-make-target-link-regexp (targets)
  5091. "Make regular expression matching all strings in TARGETS.
  5092. The regular expression finds the targets also if there is a line break
  5093. between words."
  5094. (and targets
  5095. (concat
  5096. "\\<\\("
  5097. (mapconcat
  5098. (lambda (x)
  5099. (setq x (regexp-quote x))
  5100. (while (string-match " +" x)
  5101. (setq x (replace-match "\\s-+" t t x)))
  5102. x)
  5103. targets
  5104. "\\|")
  5105. "\\)\\>")))
  5106. (defun org-activate-tags (limit)
  5107. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \r\n]") limit t)
  5108. (progn
  5109. (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
  5110. (add-text-properties (match-beginning 1) (match-end 1)
  5111. (list 'mouse-face 'highlight
  5112. 'keymap org-mouse-map))
  5113. (org-rear-nonsticky-at (match-end 1))
  5114. t)))
  5115. (defun org-outline-level ()
  5116. "Compute the outline level of the heading at point.
  5117. This function assumes that the cursor is at the beginning of a line matched
  5118. by `outline-regexp'. Otherwise it returns garbage.
  5119. If this is called at a normal headline, the level is the number of stars.
  5120. Use `org-reduced-level' to remove the effect of `org-odd-levels'."
  5121. (save-excursion
  5122. (looking-at org-outline-regexp)
  5123. (1- (- (match-end 0) (match-beginning 0)))))
  5124. (defvar org-font-lock-keywords nil)
  5125. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\+?\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  5126. "Regular expression matching a property line.")
  5127. (defvar org-font-lock-hook nil
  5128. "Functions to be called for special font lock stuff.")
  5129. (defvar org-font-lock-set-keywords-hook nil
  5130. "Functions that can manipulate `org-font-lock-extra-keywords'.
  5131. This is called after `org-font-lock-extra-keywords' is defined, but before
  5132. it is installed to be used by font lock. This can be useful if something
  5133. needs to be inserted at a specific position in the font-lock sequence.")
  5134. (defun org-font-lock-hook (limit)
  5135. (run-hook-with-args 'org-font-lock-hook limit))
  5136. (defun org-set-font-lock-defaults ()
  5137. (let* ((em org-fontify-emphasized-text)
  5138. (lk org-activate-links)
  5139. (org-font-lock-extra-keywords
  5140. (list
  5141. ;; Call the hook
  5142. '(org-font-lock-hook)
  5143. ;; Headlines
  5144. `(,(if org-fontify-whole-heading-line
  5145. "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
  5146. "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
  5147. (1 (org-get-level-face 1))
  5148. (2 (org-get-level-face 2))
  5149. (3 (org-get-level-face 3)))
  5150. ;; Table lines
  5151. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  5152. (1 'org-table t))
  5153. ;; Table internals
  5154. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  5155. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  5156. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  5157. '("| *\\(<[lrc]?[0-9]*>\\)" (1 'org-formula t))
  5158. ;; Drawers
  5159. (list org-drawer-regexp '(0 'org-special-keyword t))
  5160. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  5161. ;; Properties
  5162. (list org-property-re
  5163. '(1 'org-special-keyword t)
  5164. '(3 'org-property-value t))
  5165. ;; Links
  5166. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  5167. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  5168. (if (memq 'plain lk) '(org-activate-plain-links))
  5169. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  5170. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  5171. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  5172. (if (memq 'footnote lk) '(org-activate-footnote-links))
  5173. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  5174. '(org-hide-wide-columns (0 nil append))
  5175. ;; TODO keyword
  5176. (list (format org-heading-keyword-regexp-format
  5177. org-todo-regexp)
  5178. '(2 (org-get-todo-face 2) t))
  5179. ;; DONE
  5180. (if org-fontify-done-headline
  5181. (list (format org-heading-keyword-regexp-format
  5182. (concat
  5183. "\\(?:"
  5184. (mapconcat 'regexp-quote org-done-keywords "\\|")
  5185. "\\)"))
  5186. '(2 'org-headline-done t))
  5187. nil)
  5188. ;; Priorities
  5189. '(org-font-lock-add-priority-faces)
  5190. ;; Tags
  5191. '(org-font-lock-add-tag-faces)
  5192. ;; Special keywords
  5193. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  5194. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  5195. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  5196. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  5197. ;; Emphasis
  5198. (if em
  5199. (if (featurep 'xemacs)
  5200. '(org-do-emphasis-faces (0 nil append))
  5201. '(org-do-emphasis-faces)))
  5202. ;; Checkboxes
  5203. '("^[ \t]*\\(?:[-+*]\\|[0-9]+[.)]\\)[ \t]+\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?\\(\\[[- X]\\]\\)"
  5204. 1 'org-checkbox prepend)
  5205. (if (cdr (assq 'checkbox org-list-automatic-rules))
  5206. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  5207. (0 (org-get-checkbox-statistics-face) t)))
  5208. ;; Description list items
  5209. '("^[ \t]*[-+*][ \t]+\\(.*?[ \t]+::\\)\\([ \t]+\\|$\\)"
  5210. 1 'bold prepend)
  5211. ;; ARCHIVEd headings
  5212. (list (concat
  5213. org-outline-regexp-bol
  5214. "\\(.*:" org-archive-tag ":.*\\)")
  5215. '(1 'org-archived prepend))
  5216. ;; Specials
  5217. '(org-do-latex-and-special-faces)
  5218. '(org-fontify-entities)
  5219. '(org-raise-scripts)
  5220. ;; Code
  5221. '(org-activate-code (1 'org-code t))
  5222. ;; COMMENT
  5223. (list (format org-heading-keyword-regexp-format
  5224. (concat "\\("
  5225. org-comment-string "\\|" org-quote-string
  5226. "\\)"))
  5227. '(2 'org-special-keyword t))
  5228. '("^#.*" (0 'font-lock-comment-face t))
  5229. ;; Blocks and meta lines
  5230. '(org-fontify-meta-lines-and-blocks)
  5231. )))
  5232. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  5233. (run-hooks 'org-font-lock-set-keywords-hook)
  5234. ;; Now set the full font-lock-keywords
  5235. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  5236. (org-set-local 'font-lock-defaults
  5237. '(org-font-lock-keywords t nil nil backward-paragraph))
  5238. (kill-local-variable 'font-lock-keywords) nil))
  5239. (defun org-toggle-pretty-entities ()
  5240. "Toggle the composition display of entities as UTF8 characters."
  5241. (interactive)
  5242. (org-set-local 'org-pretty-entities (not org-pretty-entities))
  5243. (org-restart-font-lock)
  5244. (if org-pretty-entities
  5245. (message "Entities are displayed as UTF8 characters")
  5246. (save-restriction
  5247. (widen)
  5248. (org-decompose-region (point-min) (point-max))
  5249. (message "Entities are displayed plain"))))
  5250. (defun org-fontify-entities (limit)
  5251. "Find an entity to fontify."
  5252. (let (ee)
  5253. (when org-pretty-entities
  5254. (catch 'match
  5255. (while (re-search-forward
  5256. "\\\\\\(frac[13][24]\\|[a-zA-Z]+\\)\\($\\|[^[:alpha:]\n]\\)"
  5257. limit t)
  5258. (if (and (not (org-in-indented-comment-line))
  5259. (setq ee (org-entity-get (match-string 1)))
  5260. (= (length (nth 6 ee)) 1))
  5261. (progn
  5262. (add-text-properties
  5263. (match-beginning 0) (match-end 1)
  5264. (list 'font-lock-fontified t))
  5265. (compose-region (match-beginning 0) (match-end 1)
  5266. (nth 6 ee) nil)
  5267. (backward-char 1)
  5268. (throw 'match t))))
  5269. nil))))
  5270. (defun org-fontify-like-in-org-mode (s &optional odd-levels)
  5271. "Fontify string S like in Org-mode."
  5272. (with-temp-buffer
  5273. (insert s)
  5274. (let ((org-odd-levels-only odd-levels))
  5275. (org-mode)
  5276. (font-lock-fontify-buffer)
  5277. (buffer-string))))
  5278. (defvar org-m nil)
  5279. (defvar org-l nil)
  5280. (defvar org-f nil)
  5281. (defun org-get-level-face (n)
  5282. "Get the right face for match N in font-lock matching of headlines."
  5283. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  5284. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  5285. (if org-cycle-level-faces
  5286. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  5287. (setq org-f (nth (1- (min org-l org-n-level-faces)) org-level-faces)))
  5288. (cond
  5289. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  5290. ((eq n 2) org-f)
  5291. (t (if org-level-color-stars-only nil org-f))))
  5292. (defun org-get-todo-face (kwd)
  5293. "Get the right face for a TODO keyword KWD.
  5294. If KWD is a number, get the corresponding match group."
  5295. (if (numberp kwd) (setq kwd (match-string kwd)))
  5296. (or (org-face-from-face-or-color
  5297. 'todo 'org-todo (cdr (assoc kwd org-todo-keyword-faces)))
  5298. (and (member kwd org-done-keywords) 'org-done)
  5299. 'org-todo))
  5300. (defun org-face-from-face-or-color (context inherit face-or-color)
  5301. "Create a face list that inherits INHERIT, but sets the foreground color.
  5302. When FACE-OR-COLOR is not a string, just return it."
  5303. (if (stringp face-or-color)
  5304. (list :inherit inherit
  5305. (cdr (assoc context org-faces-easy-properties))
  5306. face-or-color)
  5307. face-or-color))
  5308. (defun org-font-lock-add-tag-faces (limit)
  5309. "Add the special tag faces."
  5310. (when (and org-tag-faces org-tags-special-faces-re)
  5311. (while (re-search-forward org-tags-special-faces-re limit t)
  5312. (add-text-properties (match-beginning 1) (match-end 1)
  5313. (list 'face (org-get-tag-face 1)
  5314. 'font-lock-fontified t))
  5315. (backward-char 1))))
  5316. (defun org-font-lock-add-priority-faces (limit)
  5317. "Add the special priority faces."
  5318. (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
  5319. (add-text-properties
  5320. (match-beginning 0) (match-end 0)
  5321. (list 'face (or (org-face-from-face-or-color
  5322. 'priority 'org-special-keyword
  5323. (cdr (assoc (char-after (match-beginning 1))
  5324. org-priority-faces)))
  5325. 'org-special-keyword)
  5326. 'font-lock-fontified t))))
  5327. (defun org-get-tag-face (kwd)
  5328. "Get the right face for a TODO keyword KWD.
  5329. If KWD is a number, get the corresponding match group."
  5330. (if (numberp kwd) (setq kwd (match-string kwd)))
  5331. (or (org-face-from-face-or-color
  5332. 'tag 'org-tag (cdr (assoc kwd org-tag-faces)))
  5333. 'org-tag))
  5334. (defun org-unfontify-region (beg end &optional maybe_loudly)
  5335. "Remove fontification and activation overlays from links."
  5336. (font-lock-default-unfontify-region beg end)
  5337. (let* ((buffer-undo-list t)
  5338. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  5339. (inhibit-modification-hooks t)
  5340. deactivate-mark buffer-file-name buffer-file-truename)
  5341. (org-decompose-region beg end)
  5342. (remove-text-properties beg end
  5343. '(mouse-face t keymap t org-linked-text t
  5344. invisible t intangible t
  5345. org-no-flyspell t org-emphasis t))
  5346. (org-remove-font-lock-display-properties beg end)))
  5347. (defconst org-script-display '(((raise -0.3) (height 0.7))
  5348. ((raise 0.3) (height 0.7))
  5349. ((raise -0.5))
  5350. ((raise 0.5)))
  5351. "Display properties for showing superscripts and subscripts.")
  5352. (defun org-remove-font-lock-display-properties (beg end)
  5353. "Remove specific display properties that have been added by font lock.
  5354. The will remove the raise properties that are used to show superscripts
  5355. and subscripts."
  5356. (let (next prop)
  5357. (while (< beg end)
  5358. (setq next (next-single-property-change beg 'display nil end)
  5359. prop (get-text-property beg 'display))
  5360. (if (member prop org-script-display)
  5361. (put-text-property beg next 'display nil))
  5362. (setq beg next))))
  5363. (defun org-raise-scripts (limit)
  5364. "Add raise properties to sub/superscripts."
  5365. (when (and org-pretty-entities org-pretty-entities-include-sub-superscripts)
  5366. (if (re-search-forward
  5367. (if (eq org-use-sub-superscripts t)
  5368. org-match-substring-regexp
  5369. org-match-substring-with-braces-regexp)
  5370. limit t)
  5371. (let* ((pos (point)) table-p comment-p
  5372. (mpos (match-beginning 3))
  5373. (emph-p (get-text-property mpos 'org-emphasis))
  5374. (link-p (get-text-property mpos 'mouse-face))
  5375. (keyw-p (eq 'org-special-keyword (get-text-property mpos 'face))))
  5376. (goto-char (point-at-bol))
  5377. (setq table-p (org-looking-at-p org-table-dataline-regexp)
  5378. comment-p (org-looking-at-p "[ \t]*#"))
  5379. (goto-char pos)
  5380. ;; FIXME: Should we go back one character here, for a_b^c
  5381. ;; (goto-char (1- pos)) ;????????????????????
  5382. (if (or comment-p emph-p link-p keyw-p)
  5383. t
  5384. (put-text-property (match-beginning 3) (match-end 0)
  5385. 'display
  5386. (if (equal (char-after (match-beginning 2)) ?^)
  5387. (nth (if table-p 3 1) org-script-display)
  5388. (nth (if table-p 2 0) org-script-display)))
  5389. (add-text-properties (match-beginning 2) (match-end 2)
  5390. (list 'invisible t
  5391. 'org-dwidth t 'org-dwidth-n 1))
  5392. (if (and (eq (char-after (match-beginning 3)) ?{)
  5393. (eq (char-before (match-end 3)) ?}))
  5394. (progn
  5395. (add-text-properties
  5396. (match-beginning 3) (1+ (match-beginning 3))
  5397. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))
  5398. (add-text-properties
  5399. (1- (match-end 3)) (match-end 3)
  5400. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))))
  5401. t)))))
  5402. ;;;; Visibility cycling, including org-goto and indirect buffer
  5403. ;;; Cycling
  5404. (defvar org-cycle-global-status nil)
  5405. (make-variable-buffer-local 'org-cycle-global-status)
  5406. (defvar org-cycle-subtree-status nil)
  5407. (make-variable-buffer-local 'org-cycle-subtree-status)
  5408. ;;;###autoload
  5409. (defvar org-inlinetask-min-level)
  5410. (defun org-cycle (&optional arg)
  5411. "TAB-action and visibility cycling for Org-mode.
  5412. This is the command invoked in Org-mode by the TAB key. Its main purpose
  5413. is outline visibility cycling, but it also invokes other actions
  5414. in special contexts.
  5415. - When this function is called with a prefix argument, rotate the entire
  5416. buffer through 3 states (global cycling)
  5417. 1. OVERVIEW: Show only top-level headlines.
  5418. 2. CONTENTS: Show all headlines of all levels, but no body text.
  5419. 3. SHOW ALL: Show everything.
  5420. When called with two `C-u C-u' prefixes, switch to the startup visibility,
  5421. determined by the variable `org-startup-folded', and by any VISIBILITY
  5422. properties in the buffer.
  5423. When called with three `C-u C-u C-u' prefixed, show the entire buffer,
  5424. including any drawers.
  5425. - When inside a table, re-align the table and move to the next field.
  5426. - When point is at the beginning of a headline, rotate the subtree started
  5427. by this line through 3 different states (local cycling)
  5428. 1. FOLDED: Only the main headline is shown.
  5429. 2. CHILDREN: The main headline and the direct children are shown.
  5430. From this state, you can move to one of the children
  5431. and zoom in further.
  5432. 3. SUBTREE: Show the entire subtree, including body text.
  5433. If there is no subtree, switch directly from CHILDREN to FOLDED.
  5434. - When point is at the beginning of an empty headline and the variable
  5435. `org-cycle-level-after-item/entry-creation' is set, cycle the level
  5436. of the headline by demoting and promoting it to likely levels. This
  5437. speeds up creation document structure by pressing TAB once or several
  5438. times right after creating a new headline.
  5439. - When there is a numeric prefix, go up to a heading with level ARG, do
  5440. a `show-subtree' and return to the previous cursor position. If ARG
  5441. is negative, go up that many levels.
  5442. - When point is not at the beginning of a headline, execute the global
  5443. binding for TAB, which is re-indenting the line. See the option
  5444. `org-cycle-emulate-tab' for details.
  5445. - Special case: if point is at the beginning of the buffer and there is
  5446. no headline in line 1, this function will act as if called with prefix arg
  5447. (C-u TAB, same as S-TAB) also when called without prefix arg.
  5448. But only if also the variable `org-cycle-global-at-bob' is t."
  5449. (interactive "P")
  5450. (org-load-modules-maybe)
  5451. (unless (or (run-hook-with-args-until-success 'org-tab-first-hook)
  5452. (and org-cycle-level-after-item/entry-creation
  5453. (or (org-cycle-level)
  5454. (org-cycle-item-indentation))))
  5455. (let* ((limit-level
  5456. (or org-cycle-max-level
  5457. (and (boundp 'org-inlinetask-min-level)
  5458. org-inlinetask-min-level
  5459. (1- org-inlinetask-min-level))))
  5460. (nstars (and limit-level
  5461. (if org-odd-levels-only
  5462. (and limit-level (1- (* limit-level 2)))
  5463. limit-level)))
  5464. (org-outline-regexp
  5465. (if (not (eq major-mode 'org-mode))
  5466. outline-regexp
  5467. (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ "))))
  5468. (bob-special (and org-cycle-global-at-bob (not arg) (bobp)
  5469. (not (looking-at org-outline-regexp))))
  5470. (org-cycle-hook
  5471. (if bob-special
  5472. (delq 'org-optimize-window-after-visibility-change
  5473. (copy-sequence org-cycle-hook))
  5474. org-cycle-hook))
  5475. (pos (point)))
  5476. (if (or bob-special (equal arg '(4)))
  5477. ;; special case: use global cycling
  5478. (setq arg t))
  5479. (cond
  5480. ((equal arg '(16))
  5481. (setq last-command 'dummy)
  5482. (org-set-startup-visibility)
  5483. (message "Startup visibility, plus VISIBILITY properties"))
  5484. ((equal arg '(64))
  5485. (show-all)
  5486. (message "Entire buffer visible, including drawers"))
  5487. ;; Table: enter it or move to the next field.
  5488. ((org-at-table-p 'any)
  5489. (if (org-at-table.el-p)
  5490. (message "Use C-c ' to edit table.el tables")
  5491. (if arg (org-table-edit-field t)
  5492. (org-table-justify-field-maybe)
  5493. (call-interactively 'org-table-next-field))))
  5494. ((run-hook-with-args-until-success
  5495. 'org-tab-after-check-for-table-hook))
  5496. ;; Global cycling: delegate to `org-cycle-internal-global'.
  5497. ((eq arg t) (org-cycle-internal-global))
  5498. ;; Drawers: delegate to `org-flag-drawer'.
  5499. ((and org-drawers org-drawer-regexp
  5500. (save-excursion
  5501. (beginning-of-line 1)
  5502. (looking-at org-drawer-regexp)))
  5503. (org-flag-drawer ; toggle block visibility
  5504. (not (get-char-property (match-end 0) 'invisible))))
  5505. ;; Show-subtree, ARG levels up from here.
  5506. ((integerp arg)
  5507. (save-excursion
  5508. (org-back-to-heading)
  5509. (outline-up-heading (if (< arg 0) (- arg)
  5510. (- (funcall outline-level) arg)))
  5511. (org-show-subtree)))
  5512. ;; Inline task: delegate to `org-inlinetask-toggle-visibility'.
  5513. ((and (featurep 'org-inlinetask)
  5514. (org-inlinetask-at-task-p)
  5515. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5516. (org-inlinetask-toggle-visibility))
  5517. ((org-try-cdlatex-tab))
  5518. ;; At an item/headline: delegate to `org-cycle-internal-local'.
  5519. ((and (or (and org-cycle-include-plain-lists (org-at-item-p))
  5520. (save-excursion (beginning-of-line 1)
  5521. (looking-at org-outline-regexp)))
  5522. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5523. (org-cycle-internal-local))
  5524. ;; From there: TAB emulation and template completion.
  5525. (buffer-read-only (org-back-to-heading))
  5526. ((run-hook-with-args-until-success
  5527. 'org-tab-after-check-for-cycling-hook))
  5528. ((org-try-structure-completion))
  5529. ((run-hook-with-args-until-success
  5530. 'org-tab-before-tab-emulation-hook))
  5531. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  5532. (or (not (bolp))
  5533. (not (looking-at org-outline-regexp))))
  5534. (call-interactively (global-key-binding "\t")))
  5535. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  5536. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  5537. (or (and (eq org-cycle-emulate-tab 'white)
  5538. (= (match-end 0) (point-at-eol)))
  5539. (and (eq org-cycle-emulate-tab 'whitestart)
  5540. (>= (match-end 0) pos))))
  5541. t
  5542. (eq org-cycle-emulate-tab t))
  5543. (call-interactively (global-key-binding "\t")))
  5544. (t (save-excursion
  5545. (org-back-to-heading)
  5546. (org-cycle)))))))
  5547. (defun org-cycle-internal-global ()
  5548. "Do the global cycling action."
  5549. ;; Hack to avoid display of messages for .org attachments in Gnus
  5550. (let ((ga (string-match "\\*fontification" (buffer-name))))
  5551. (cond
  5552. ((and (eq last-command this-command)
  5553. (eq org-cycle-global-status 'overview))
  5554. ;; We just created the overview - now do table of contents
  5555. ;; This can be slow in very large buffers, so indicate action
  5556. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  5557. (unless ga (message "CONTENTS..."))
  5558. (org-content)
  5559. (unless ga (message "CONTENTS...done"))
  5560. (setq org-cycle-global-status 'contents)
  5561. (run-hook-with-args 'org-cycle-hook 'contents))
  5562. ((and (eq last-command this-command)
  5563. (eq org-cycle-global-status 'contents))
  5564. ;; We just showed the table of contents - now show everything
  5565. (run-hook-with-args 'org-pre-cycle-hook 'all)
  5566. (show-all)
  5567. (unless ga (message "SHOW ALL"))
  5568. (setq org-cycle-global-status 'all)
  5569. (run-hook-with-args 'org-cycle-hook 'all))
  5570. (t
  5571. ;; Default action: go to overview
  5572. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  5573. (org-overview)
  5574. (unless ga (message "OVERVIEW"))
  5575. (setq org-cycle-global-status 'overview)
  5576. (run-hook-with-args 'org-cycle-hook 'overview)))))
  5577. (defun org-cycle-internal-local ()
  5578. "Do the local cycling action."
  5579. (let ((goal-column 0) eoh eol eos has-children children-skipped struct)
  5580. ;; First, determine end of headline (EOH), end of subtree or item
  5581. ;; (EOS), and if item or heading has children (HAS-CHILDREN).
  5582. (save-excursion
  5583. (if (org-at-item-p)
  5584. (progn
  5585. (beginning-of-line)
  5586. (setq struct (org-list-struct))
  5587. (setq eoh (point-at-eol))
  5588. (setq eos (org-list-get-item-end-before-blank (point) struct))
  5589. (setq has-children (org-list-has-child-p (point) struct)))
  5590. (org-back-to-heading)
  5591. (setq eoh (save-excursion (outline-end-of-heading) (point)))
  5592. (setq eos (save-excursion
  5593. (org-end-of-subtree t)
  5594. (unless (eobp)
  5595. (skip-chars-forward " \t\n"))
  5596. (if (eobp) (point) (1- (point)))))
  5597. (setq has-children
  5598. (or (save-excursion
  5599. (let ((level (funcall outline-level)))
  5600. (outline-next-heading)
  5601. (and (org-at-heading-p t)
  5602. (> (funcall outline-level) level))))
  5603. (save-excursion
  5604. (org-list-search-forward (org-item-beginning-re) eos t)))))
  5605. ;; Determine end invisible part of buffer (EOL)
  5606. (beginning-of-line 2)
  5607. ;; XEmacs doesn't have `next-single-char-property-change'
  5608. (if (featurep 'xemacs)
  5609. (while (and (not (eobp)) ;; this is like `next-line'
  5610. (get-char-property (1- (point)) 'invisible))
  5611. (beginning-of-line 2))
  5612. (while (and (not (eobp)) ;; this is like `next-line'
  5613. (get-char-property (1- (point)) 'invisible))
  5614. (goto-char (next-single-char-property-change (point) 'invisible))
  5615. (and (eolp) (beginning-of-line 2))))
  5616. (setq eol (point)))
  5617. ;; Find out what to do next and set `this-command'
  5618. (cond
  5619. ((= eos eoh)
  5620. ;; Nothing is hidden behind this heading
  5621. (run-hook-with-args 'org-pre-cycle-hook 'empty)
  5622. (message "EMPTY ENTRY")
  5623. (setq org-cycle-subtree-status nil)
  5624. (save-excursion
  5625. (goto-char eos)
  5626. (outline-next-heading)
  5627. (if (outline-invisible-p) (org-flag-heading nil))))
  5628. ((and (or (>= eol eos)
  5629. (not (string-match "\\S-" (buffer-substring eol eos))))
  5630. (or has-children
  5631. (not (setq children-skipped
  5632. org-cycle-skip-children-state-if-no-children))))
  5633. ;; Entire subtree is hidden in one line: children view
  5634. (run-hook-with-args 'org-pre-cycle-hook 'children)
  5635. (if (org-at-item-p)
  5636. (org-list-set-item-visibility (point-at-bol) struct 'children)
  5637. (org-show-entry)
  5638. (show-children)
  5639. (when (memq 'org-cycle-hide-drawers org-cycle-hook)
  5640. (org-cycle-hide-drawers 'subtree))
  5641. ;; Fold every list in subtree to top-level items.
  5642. (when (eq org-cycle-include-plain-lists 'integrate)
  5643. (save-excursion
  5644. (org-back-to-heading)
  5645. (while (org-list-search-forward (org-item-beginning-re) eos t)
  5646. (beginning-of-line 1)
  5647. (let* ((struct (org-list-struct))
  5648. (prevs (org-list-prevs-alist struct))
  5649. (end (org-list-get-bottom-point struct)))
  5650. (mapc (lambda (e) (org-list-set-item-visibility e struct 'folded))
  5651. (org-list-get-all-items (point) struct prevs))
  5652. (goto-char end))))))
  5653. (message "CHILDREN")
  5654. (save-excursion
  5655. (goto-char eos)
  5656. (outline-next-heading)
  5657. (if (outline-invisible-p) (org-flag-heading nil)))
  5658. (setq org-cycle-subtree-status 'children)
  5659. (run-hook-with-args 'org-cycle-hook 'children))
  5660. ((or children-skipped
  5661. (and (eq last-command this-command)
  5662. (eq org-cycle-subtree-status 'children)))
  5663. ;; We just showed the children, or no children are there,
  5664. ;; now show everything.
  5665. (run-hook-with-args 'org-pre-cycle-hook 'subtree)
  5666. (outline-flag-region eoh eos nil)
  5667. (message (if children-skipped "SUBTREE (NO CHILDREN)" "SUBTREE"))
  5668. (setq org-cycle-subtree-status 'subtree)
  5669. (run-hook-with-args 'org-cycle-hook 'subtree))
  5670. (t
  5671. ;; Default action: hide the subtree.
  5672. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  5673. (outline-flag-region eoh eos t)
  5674. (message "FOLDED")
  5675. (setq org-cycle-subtree-status 'folded)
  5676. (run-hook-with-args 'org-cycle-hook 'folded)))))
  5677. ;;;###autoload
  5678. (defun org-global-cycle (&optional arg)
  5679. "Cycle the global visibility. For details see `org-cycle'.
  5680. With \\[universal-argument] prefix arg, switch to startup visibility.
  5681. With a numeric prefix, show all headlines up to that level."
  5682. (interactive "P")
  5683. (let ((org-cycle-include-plain-lists
  5684. (if (eq major-mode 'org-mode) org-cycle-include-plain-lists nil)))
  5685. (cond
  5686. ((integerp arg)
  5687. (show-all)
  5688. (hide-sublevels arg)
  5689. (setq org-cycle-global-status 'contents))
  5690. ((equal arg '(4))
  5691. (org-set-startup-visibility)
  5692. (message "Startup visibility, plus VISIBILITY properties."))
  5693. (t
  5694. (org-cycle '(4))))))
  5695. (defun org-set-startup-visibility ()
  5696. "Set the visibility required by startup options and properties."
  5697. (cond
  5698. ((eq org-startup-folded t)
  5699. (org-cycle '(4)))
  5700. ((eq org-startup-folded 'content)
  5701. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  5702. (org-cycle '(4)) (org-cycle '(4)))))
  5703. (unless (eq org-startup-folded 'showeverything)
  5704. (if org-hide-block-startup (org-hide-block-all))
  5705. (org-set-visibility-according-to-property 'no-cleanup)
  5706. (org-cycle-hide-archived-subtrees 'all)
  5707. (org-cycle-hide-drawers 'all)
  5708. (org-cycle-show-empty-lines t)))
  5709. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  5710. "Switch subtree visibilities according to :VISIBILITY: property."
  5711. (interactive)
  5712. (let (org-show-entry-below state)
  5713. (save-excursion
  5714. (goto-char (point-min))
  5715. (while (re-search-forward
  5716. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  5717. nil t)
  5718. (setq state (match-string 1))
  5719. (save-excursion
  5720. (org-back-to-heading t)
  5721. (hide-subtree)
  5722. (org-reveal)
  5723. (cond
  5724. ((equal state '("fold" "folded"))
  5725. (hide-subtree))
  5726. ((equal state "children")
  5727. (org-show-hidden-entry)
  5728. (show-children))
  5729. ((equal state "content")
  5730. (save-excursion
  5731. (save-restriction
  5732. (org-narrow-to-subtree)
  5733. (org-content))))
  5734. ((member state '("all" "showall"))
  5735. (show-subtree)))))
  5736. (unless no-cleanup
  5737. (org-cycle-hide-archived-subtrees 'all)
  5738. (org-cycle-hide-drawers 'all)
  5739. (org-cycle-show-empty-lines 'all)))))
  5740. ;; This function uses outline-regexp instead of the more fundamental
  5741. ;; org-outline-regexp so that org-cycle-global works outside of Org
  5742. ;; buffers, where outline-regexp is needed.
  5743. (defun org-overview ()
  5744. "Switch to overview mode, showing only top-level headlines.
  5745. Really, this shows all headlines with level equal or greater than the level
  5746. of the first headline in the buffer. This is important, because if the
  5747. first headline is not level one, then (hide-sublevels 1) gives confusing
  5748. results."
  5749. (interactive)
  5750. (let ((level (save-excursion
  5751. (goto-char (point-min))
  5752. (if (re-search-forward (concat "^" outline-regexp) nil t)
  5753. (progn
  5754. (goto-char (match-beginning 0))
  5755. (funcall outline-level))))))
  5756. (and level (hide-sublevels level))))
  5757. (defun org-content (&optional arg)
  5758. "Show all headlines in the buffer, like a table of contents.
  5759. With numerical argument N, show content up to level N."
  5760. (interactive "P")
  5761. (save-excursion
  5762. ;; Visit all headings and show their offspring
  5763. (and (integerp arg) (org-overview))
  5764. (goto-char (point-max))
  5765. (catch 'exit
  5766. (while (and (progn (condition-case nil
  5767. (outline-previous-visible-heading 1)
  5768. (error (goto-char (point-min))))
  5769. t)
  5770. (looking-at org-outline-regexp))
  5771. (if (integerp arg)
  5772. (show-children (1- arg))
  5773. (show-branches))
  5774. (if (bobp) (throw 'exit nil))))))
  5775. (defun org-optimize-window-after-visibility-change (state)
  5776. "Adjust the window after a change in outline visibility.
  5777. This function is the default value of the hook `org-cycle-hook'."
  5778. (when (get-buffer-window (current-buffer))
  5779. (cond
  5780. ((eq state 'content) nil)
  5781. ((eq state 'all) nil)
  5782. ((eq state 'folded) nil)
  5783. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  5784. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  5785. (defun org-remove-empty-overlays-at (pos)
  5786. "Remove outline overlays that do not contain non-white stuff."
  5787. (mapc
  5788. (lambda (o)
  5789. (and (eq 'outline (overlay-get o 'invisible))
  5790. (not (string-match "\\S-" (buffer-substring (overlay-start o)
  5791. (overlay-end o))))
  5792. (delete-overlay o)))
  5793. (overlays-at pos)))
  5794. (defun org-clean-visibility-after-subtree-move ()
  5795. "Fix visibility issues after moving a subtree."
  5796. ;; First, find a reasonable region to look at:
  5797. ;; Start two siblings above, end three below
  5798. (let* ((beg (save-excursion
  5799. (and (org-get-last-sibling)
  5800. (org-get-last-sibling))
  5801. (point)))
  5802. (end (save-excursion
  5803. (and (org-get-next-sibling)
  5804. (org-get-next-sibling)
  5805. (org-get-next-sibling))
  5806. (if (org-at-heading-p)
  5807. (point-at-eol)
  5808. (point))))
  5809. (level (looking-at "\\*+"))
  5810. (re (if level (concat "^" (regexp-quote (match-string 0)) " "))))
  5811. (save-excursion
  5812. (save-restriction
  5813. (narrow-to-region beg end)
  5814. (when re
  5815. ;; Properly fold already folded siblings
  5816. (goto-char (point-min))
  5817. (while (re-search-forward re nil t)
  5818. (if (and (not (outline-invisible-p))
  5819. (save-excursion
  5820. (goto-char (point-at-eol)) (outline-invisible-p)))
  5821. (hide-entry))))
  5822. (org-cycle-show-empty-lines 'overview)
  5823. (org-cycle-hide-drawers 'overview)))))
  5824. (defun org-cycle-show-empty-lines (state)
  5825. "Show empty lines above all visible headlines.
  5826. The region to be covered depends on STATE when called through
  5827. `org-cycle-hook'. Lisp program can use t for STATE to get the
  5828. entire buffer covered. Note that an empty line is only shown if there
  5829. are at least `org-cycle-separator-lines' empty lines before the headline."
  5830. (when (not (= org-cycle-separator-lines 0))
  5831. (save-excursion
  5832. (let* ((n (abs org-cycle-separator-lines))
  5833. (re (cond
  5834. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  5835. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  5836. (t (let ((ns (number-to-string (- n 2))))
  5837. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  5838. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  5839. beg end b e)
  5840. (cond
  5841. ((memq state '(overview contents t))
  5842. (setq beg (point-min) end (point-max)))
  5843. ((memq state '(children folded))
  5844. (setq beg (point) end (progn (org-end-of-subtree t t)
  5845. (beginning-of-line 2)
  5846. (point)))))
  5847. (when beg
  5848. (goto-char beg)
  5849. (while (re-search-forward re end t)
  5850. (unless (get-char-property (match-end 1) 'invisible)
  5851. (setq e (match-end 1))
  5852. (if (< org-cycle-separator-lines 0)
  5853. (setq b (save-excursion
  5854. (goto-char (match-beginning 0))
  5855. (org-back-over-empty-lines)
  5856. (if (save-excursion
  5857. (goto-char (max (point-min) (1- (point))))
  5858. (org-at-heading-p))
  5859. (1- (point))
  5860. (point))))
  5861. (setq b (match-beginning 1)))
  5862. (outline-flag-region b e nil)))))))
  5863. ;; Never hide empty lines at the end of the file.
  5864. (save-excursion
  5865. (goto-char (point-max))
  5866. (outline-previous-heading)
  5867. (outline-end-of-heading)
  5868. (if (and (looking-at "[ \t\n]+")
  5869. (= (match-end 0) (point-max)))
  5870. (outline-flag-region (point) (match-end 0) nil))))
  5871. (defun org-show-empty-lines-in-parent ()
  5872. "Move to the parent and re-show empty lines before visible headlines."
  5873. (save-excursion
  5874. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  5875. (org-cycle-show-empty-lines context))))
  5876. (defun org-files-list ()
  5877. "Return `org-agenda-files' list, plus all open org-mode files.
  5878. This is useful for operations that need to scan all of a user's
  5879. open and agenda-wise Org files."
  5880. (let ((files (mapcar 'expand-file-name (org-agenda-files))))
  5881. (dolist (buf (buffer-list))
  5882. (with-current-buffer buf
  5883. (if (and (eq major-mode 'org-mode) (buffer-file-name))
  5884. (let ((file (expand-file-name (buffer-file-name))))
  5885. (unless (member file files)
  5886. (push file files))))))
  5887. files))
  5888. (defsubst org-entry-beginning-position ()
  5889. "Return the beginning position of the current entry."
  5890. (save-excursion (outline-back-to-heading t) (point)))
  5891. (defsubst org-entry-end-position ()
  5892. "Return the end position of the current entry."
  5893. (save-excursion (outline-next-heading) (point)))
  5894. (defun org-cycle-hide-drawers (state)
  5895. "Re-hide all drawers after a visibility state change."
  5896. (when (and (eq major-mode 'org-mode)
  5897. (not (memq state '(overview folded contents))))
  5898. (save-excursion
  5899. (let* ((globalp (memq state '(contents all)))
  5900. (beg (if globalp (point-min) (point)))
  5901. (end (if globalp (point-max)
  5902. (if (eq state 'children)
  5903. (save-excursion (outline-next-heading) (point))
  5904. (org-end-of-subtree t)))))
  5905. (goto-char beg)
  5906. (while (re-search-forward org-drawer-regexp end t)
  5907. (org-flag-drawer t))))))
  5908. (defun org-flag-drawer (flag)
  5909. (save-excursion
  5910. (beginning-of-line 1)
  5911. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  5912. (let ((b (match-end 0)))
  5913. (if (re-search-forward
  5914. "^[ \t]*:END:"
  5915. (save-excursion (outline-next-heading) (point)) t)
  5916. (outline-flag-region b (point-at-eol) flag)
  5917. (error ":END: line missing at position %s" b))))))
  5918. (defun org-subtree-end-visible-p ()
  5919. "Is the end of the current subtree visible?"
  5920. (pos-visible-in-window-p
  5921. (save-excursion (org-end-of-subtree t) (point))))
  5922. (defun org-first-headline-recenter (&optional N)
  5923. "Move cursor to the first headline and recenter the headline.
  5924. Optional argument N means put the headline into the Nth line of the window."
  5925. (goto-char (point-min))
  5926. (when (re-search-forward (concat "^\\(" org-outline-regexp "\\)") nil t)
  5927. (beginning-of-line)
  5928. (recenter (prefix-numeric-value N))))
  5929. ;;; Saving and restoring visibility
  5930. (defun org-outline-overlay-data (&optional use-markers)
  5931. "Return a list of the locations of all outline overlays.
  5932. These are overlays with the `invisible' property value `outline'.
  5933. The return value is a list of cons cells, with start and stop
  5934. positions for each overlay.
  5935. If USE-MARKERS is set, return the positions as markers."
  5936. (let (beg end)
  5937. (save-excursion
  5938. (save-restriction
  5939. (widen)
  5940. (delq nil
  5941. (mapcar (lambda (o)
  5942. (when (eq (overlay-get o 'invisible) 'outline)
  5943. (setq beg (overlay-start o)
  5944. end (overlay-end o))
  5945. (and beg end (> end beg)
  5946. (if use-markers
  5947. (cons (move-marker (make-marker) beg)
  5948. (move-marker (make-marker) end))
  5949. (cons beg end)))))
  5950. (overlays-in (point-min) (point-max))))))))
  5951. (defun org-set-outline-overlay-data (data)
  5952. "Create visibility overlays for all positions in DATA.
  5953. DATA should have been made by `org-outline-overlay-data'."
  5954. (let (o)
  5955. (save-excursion
  5956. (save-restriction
  5957. (widen)
  5958. (show-all)
  5959. (mapc (lambda (c)
  5960. (outline-flag-region (car c) (cdr c) t))
  5961. data)))))
  5962. ;;; Folding of blocks
  5963. (defvar org-hide-block-overlays nil
  5964. "Overlays hiding blocks.")
  5965. (make-variable-buffer-local 'org-hide-block-overlays)
  5966. (defun org-block-map (function &optional start end)
  5967. "Call FUNCTION at the head of all source blocks in the current buffer.
  5968. Optional arguments START and END can be used to limit the range."
  5969. (let ((start (or start (point-min)))
  5970. (end (or end (point-max))))
  5971. (save-excursion
  5972. (goto-char start)
  5973. (while (and (< (point) end) (re-search-forward org-block-regexp end t))
  5974. (save-excursion
  5975. (save-match-data
  5976. (goto-char (match-beginning 0))
  5977. (funcall function)))))))
  5978. (defun org-hide-block-toggle-all ()
  5979. "Toggle the visibility of all blocks in the current buffer."
  5980. (org-block-map #'org-hide-block-toggle))
  5981. (defun org-hide-block-all ()
  5982. "Fold all blocks in the current buffer."
  5983. (interactive)
  5984. (org-show-block-all)
  5985. (org-block-map #'org-hide-block-toggle-maybe))
  5986. (defun org-show-block-all ()
  5987. "Unfold all blocks in the current buffer."
  5988. (interactive)
  5989. (mapc 'delete-overlay org-hide-block-overlays)
  5990. (setq org-hide-block-overlays nil))
  5991. (defun org-hide-block-toggle-maybe ()
  5992. "Toggle visibility of block at point."
  5993. (interactive)
  5994. (let ((case-fold-search t))
  5995. (if (save-excursion
  5996. (beginning-of-line 1)
  5997. (looking-at org-block-regexp))
  5998. (progn (org-hide-block-toggle)
  5999. t) ;; to signal that we took action
  6000. nil))) ;; to signal that we did not
  6001. (defun org-hide-block-toggle (&optional force)
  6002. "Toggle the visibility of the current block."
  6003. (interactive)
  6004. (save-excursion
  6005. (beginning-of-line)
  6006. (if (re-search-forward org-block-regexp nil t)
  6007. (let ((start (- (match-beginning 4) 1)) ;; beginning of body
  6008. (end (match-end 0)) ;; end of entire body
  6009. ov)
  6010. (if (memq t (mapcar (lambda (overlay)
  6011. (eq (overlay-get overlay 'invisible)
  6012. 'org-hide-block))
  6013. (overlays-at start)))
  6014. (if (or (not force) (eq force 'off))
  6015. (mapc (lambda (ov)
  6016. (when (member ov org-hide-block-overlays)
  6017. (setq org-hide-block-overlays
  6018. (delq ov org-hide-block-overlays)))
  6019. (when (eq (overlay-get ov 'invisible)
  6020. 'org-hide-block)
  6021. (delete-overlay ov)))
  6022. (overlays-at start)))
  6023. (setq ov (make-overlay start end))
  6024. (overlay-put ov 'invisible 'org-hide-block)
  6025. ;; make the block accessible to isearch
  6026. (overlay-put
  6027. ov 'isearch-open-invisible
  6028. (lambda (ov)
  6029. (when (member ov org-hide-block-overlays)
  6030. (setq org-hide-block-overlays
  6031. (delq ov org-hide-block-overlays)))
  6032. (when (eq (overlay-get ov 'invisible)
  6033. 'org-hide-block)
  6034. (delete-overlay ov))))
  6035. (push ov org-hide-block-overlays)))
  6036. (error "Not looking at a source block"))))
  6037. ;; org-tab-after-check-for-cycling-hook
  6038. (add-hook 'org-tab-first-hook 'org-hide-block-toggle-maybe)
  6039. ;; Remove overlays when changing major mode
  6040. (add-hook 'org-mode-hook
  6041. (lambda () (org-add-hook 'change-major-mode-hook
  6042. 'org-show-block-all 'append 'local)))
  6043. ;;; Org-goto
  6044. (defvar org-goto-window-configuration nil)
  6045. (defvar org-goto-marker nil)
  6046. (defvar org-goto-map
  6047. (let ((map (make-sparse-keymap)))
  6048. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  6049. (while (setq cmd (pop cmds))
  6050. (substitute-key-definition cmd cmd map global-map)))
  6051. (suppress-keymap map)
  6052. (org-defkey map "\C-m" 'org-goto-ret)
  6053. (org-defkey map [(return)] 'org-goto-ret)
  6054. (org-defkey map [(left)] 'org-goto-left)
  6055. (org-defkey map [(right)] 'org-goto-right)
  6056. (org-defkey map [(control ?g)] 'org-goto-quit)
  6057. (org-defkey map "\C-i" 'org-cycle)
  6058. (org-defkey map [(tab)] 'org-cycle)
  6059. (org-defkey map [(down)] 'outline-next-visible-heading)
  6060. (org-defkey map [(up)] 'outline-previous-visible-heading)
  6061. (if org-goto-auto-isearch
  6062. (if (fboundp 'define-key-after)
  6063. (define-key-after map [t] 'org-goto-local-auto-isearch)
  6064. nil)
  6065. (org-defkey map "q" 'org-goto-quit)
  6066. (org-defkey map "n" 'outline-next-visible-heading)
  6067. (org-defkey map "p" 'outline-previous-visible-heading)
  6068. (org-defkey map "f" 'outline-forward-same-level)
  6069. (org-defkey map "b" 'outline-backward-same-level)
  6070. (org-defkey map "u" 'outline-up-heading))
  6071. (org-defkey map "/" 'org-occur)
  6072. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  6073. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  6074. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  6075. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  6076. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  6077. map))
  6078. (defconst org-goto-help
  6079. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  6080. RET=jump to location [Q]uit and return to previous location
  6081. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  6082. (defvar org-goto-start-pos) ; dynamically scoped parameter
  6083. ;; FIXME: Docstring does not mention both interfaces
  6084. (defun org-goto (&optional alternative-interface)
  6085. "Look up a different location in the current file, keeping current visibility.
  6086. When you want look-up or go to a different location in a document, the
  6087. fastest way is often to fold the entire buffer and then dive into the tree.
  6088. This method has the disadvantage, that the previous location will be folded,
  6089. which may not be what you want.
  6090. This command works around this by showing a copy of the current buffer
  6091. in an indirect buffer, in overview mode. You can dive into the tree in
  6092. that copy, use org-occur and incremental search to find a location.
  6093. When pressing RET or `Q', the command returns to the original buffer in
  6094. which the visibility is still unchanged. After RET is will also jump to
  6095. the location selected in the indirect buffer and expose the headline
  6096. hierarchy above."
  6097. (interactive "P")
  6098. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  6099. (org-refile-use-outline-path t)
  6100. (org-refile-target-verify-function nil)
  6101. (interface
  6102. (if (not alternative-interface)
  6103. org-goto-interface
  6104. (if (eq org-goto-interface 'outline)
  6105. 'outline-path-completion
  6106. 'outline)))
  6107. (org-goto-start-pos (point))
  6108. (selected-point
  6109. (if (eq interface 'outline)
  6110. (car (org-get-location (current-buffer) org-goto-help))
  6111. (let ((pa (org-refile-get-location "Goto")))
  6112. (org-refile-check-position pa)
  6113. (nth 3 pa)))))
  6114. (if selected-point
  6115. (progn
  6116. (org-mark-ring-push org-goto-start-pos)
  6117. (goto-char selected-point)
  6118. (if (or (outline-invisible-p) (org-invisible-p2))
  6119. (org-show-context 'org-goto)))
  6120. (message "Quit"))))
  6121. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  6122. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  6123. (defvar org-goto-local-auto-isearch-map) ; defined below
  6124. (defun org-get-location (buf help)
  6125. "Let the user select a location in the Org-mode buffer BUF.
  6126. This function uses a recursive edit. It returns the selected position
  6127. or nil."
  6128. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  6129. (isearch-hide-immediately nil)
  6130. (isearch-search-fun-function
  6131. (lambda () 'org-goto-local-search-headings))
  6132. (org-goto-selected-point org-goto-exit-command)
  6133. (pop-up-frames nil)
  6134. (special-display-buffer-names nil)
  6135. (special-display-regexps nil)
  6136. (special-display-function nil))
  6137. (save-excursion
  6138. (save-window-excursion
  6139. (delete-other-windows)
  6140. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  6141. (org-pop-to-buffer-same-window
  6142. (condition-case nil
  6143. (make-indirect-buffer (current-buffer) "*org-goto*")
  6144. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  6145. (with-output-to-temp-buffer "*Help*"
  6146. (princ help))
  6147. (org-fit-window-to-buffer (get-buffer-window "*Help*"))
  6148. (setq buffer-read-only nil)
  6149. (let ((org-startup-truncated t)
  6150. (org-startup-folded nil)
  6151. (org-startup-align-all-tables nil))
  6152. (org-mode)
  6153. (org-overview))
  6154. (setq buffer-read-only t)
  6155. (if (and (boundp 'org-goto-start-pos)
  6156. (integer-or-marker-p org-goto-start-pos))
  6157. (let ((org-show-hierarchy-above t)
  6158. (org-show-siblings t)
  6159. (org-show-following-heading t))
  6160. (goto-char org-goto-start-pos)
  6161. (and (outline-invisible-p) (org-show-context)))
  6162. (goto-char (point-min)))
  6163. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  6164. (message "Select location and press RET")
  6165. (use-local-map org-goto-map)
  6166. (recursive-edit)
  6167. ))
  6168. (kill-buffer "*org-goto*")
  6169. (cons org-goto-selected-point org-goto-exit-command)))
  6170. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  6171. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  6172. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  6173. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  6174. (defun org-goto-local-search-headings (string bound noerror)
  6175. "Search and make sure that any matches are in headlines."
  6176. (catch 'return
  6177. (while (if isearch-forward
  6178. (search-forward string bound noerror)
  6179. (search-backward string bound noerror))
  6180. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  6181. (and (member :headline context)
  6182. (not (member :tags context))))
  6183. (throw 'return (point))))))
  6184. (defun org-goto-local-auto-isearch ()
  6185. "Start isearch."
  6186. (interactive)
  6187. (goto-char (point-min))
  6188. (let ((keys (this-command-keys)))
  6189. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  6190. (isearch-mode t)
  6191. (isearch-process-search-char (string-to-char keys)))))
  6192. (defun org-goto-ret (&optional arg)
  6193. "Finish `org-goto' by going to the new location."
  6194. (interactive "P")
  6195. (setq org-goto-selected-point (point)
  6196. org-goto-exit-command 'return)
  6197. (throw 'exit nil))
  6198. (defun org-goto-left ()
  6199. "Finish `org-goto' by going to the new location."
  6200. (interactive)
  6201. (if (org-at-heading-p)
  6202. (progn
  6203. (beginning-of-line 1)
  6204. (setq org-goto-selected-point (point)
  6205. org-goto-exit-command 'left)
  6206. (throw 'exit nil))
  6207. (error "Not on a heading")))
  6208. (defun org-goto-right ()
  6209. "Finish `org-goto' by going to the new location."
  6210. (interactive)
  6211. (if (org-at-heading-p)
  6212. (progn
  6213. (setq org-goto-selected-point (point)
  6214. org-goto-exit-command 'right)
  6215. (throw 'exit nil))
  6216. (error "Not on a heading")))
  6217. (defun org-goto-quit ()
  6218. "Finish `org-goto' without cursor motion."
  6219. (interactive)
  6220. (setq org-goto-selected-point nil)
  6221. (setq org-goto-exit-command 'quit)
  6222. (throw 'exit nil))
  6223. ;;; Indirect buffer display of subtrees
  6224. (defvar org-indirect-dedicated-frame nil
  6225. "This is the frame being used for indirect tree display.")
  6226. (defvar org-last-indirect-buffer nil)
  6227. (defun org-tree-to-indirect-buffer (&optional arg)
  6228. "Create indirect buffer and narrow it to current subtree.
  6229. With numerical prefix ARG, go up to this level and then take that tree.
  6230. If ARG is negative, go up that many levels.
  6231. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  6232. indirect buffer previously made with this command, to avoid proliferation of
  6233. indirect buffers. However, when you call the command with a \
  6234. \\[universal-argument] prefix, or
  6235. when `org-indirect-buffer-display' is `new-frame', the last buffer
  6236. is kept so that you can work with several indirect buffers at the same time.
  6237. If `org-indirect-buffer-display' is `dedicated-frame', the \
  6238. \\[universal-argument] prefix also
  6239. requests that a new frame be made for the new buffer, so that the dedicated
  6240. frame is not changed."
  6241. (interactive "P")
  6242. (let ((cbuf (current-buffer))
  6243. (cwin (selected-window))
  6244. (pos (point))
  6245. beg end level heading ibuf)
  6246. (save-excursion
  6247. (org-back-to-heading t)
  6248. (when (numberp arg)
  6249. (setq level (org-outline-level))
  6250. (if (< arg 0) (setq arg (+ level arg)))
  6251. (while (> (setq level (org-outline-level)) arg)
  6252. (outline-up-heading 1 t)))
  6253. (setq beg (point)
  6254. heading (org-get-heading))
  6255. (org-end-of-subtree t t)
  6256. (if (org-at-heading-p) (backward-char 1))
  6257. (setq end (point)))
  6258. (if (and (buffer-live-p org-last-indirect-buffer)
  6259. (not (eq org-indirect-buffer-display 'new-frame))
  6260. (not arg))
  6261. (kill-buffer org-last-indirect-buffer))
  6262. (setq ibuf (org-get-indirect-buffer cbuf)
  6263. org-last-indirect-buffer ibuf)
  6264. (cond
  6265. ((or (eq org-indirect-buffer-display 'new-frame)
  6266. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  6267. (select-frame (make-frame))
  6268. (delete-other-windows)
  6269. (org-pop-to-buffer-same-window ibuf)
  6270. (org-set-frame-title heading))
  6271. ((eq org-indirect-buffer-display 'dedicated-frame)
  6272. (raise-frame
  6273. (select-frame (or (and org-indirect-dedicated-frame
  6274. (frame-live-p org-indirect-dedicated-frame)
  6275. org-indirect-dedicated-frame)
  6276. (setq org-indirect-dedicated-frame (make-frame)))))
  6277. (delete-other-windows)
  6278. (org-pop-to-buffer-same-window ibuf)
  6279. (org-set-frame-title (concat "Indirect: " heading)))
  6280. ((eq org-indirect-buffer-display 'current-window)
  6281. (org-pop-to-buffer-same-window ibuf))
  6282. ((eq org-indirect-buffer-display 'other-window)
  6283. (pop-to-buffer ibuf))
  6284. (t (error "Invalid value")))
  6285. (if (featurep 'xemacs)
  6286. (save-excursion (org-mode) (turn-on-font-lock)))
  6287. (narrow-to-region beg end)
  6288. (show-all)
  6289. (goto-char pos)
  6290. (run-hook-with-args 'org-cycle-hook 'all)
  6291. (and (window-live-p cwin) (select-window cwin))))
  6292. (defun org-get-indirect-buffer (&optional buffer)
  6293. (setq buffer (or buffer (current-buffer)))
  6294. (let ((n 1) (base (buffer-name buffer)) bname)
  6295. (while (buffer-live-p
  6296. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  6297. (setq n (1+ n)))
  6298. (condition-case nil
  6299. (make-indirect-buffer buffer bname 'clone)
  6300. (error (make-indirect-buffer buffer bname)))))
  6301. (defun org-set-frame-title (title)
  6302. "Set the title of the current frame to the string TITLE."
  6303. ;; FIXME: how to name a single frame in XEmacs???
  6304. (unless (featurep 'xemacs)
  6305. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  6306. ;;;; Structure editing
  6307. ;;; Inserting headlines
  6308. (defun org-previous-line-empty-p ()
  6309. (save-excursion
  6310. (and (not (bobp))
  6311. (or (beginning-of-line 0) t)
  6312. (save-match-data
  6313. (looking-at "[ \t]*$")))))
  6314. (defun org-insert-heading (&optional force-heading invisible-ok)
  6315. "Insert a new heading or item with same depth at point.
  6316. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  6317. If point is at the beginning of a headline, insert a sibling before the
  6318. current headline. If point is not at the beginning, split the line,
  6319. create the new headline with the text in the current line after point
  6320. \(but see also the variable `org-M-RET-may-split-line').
  6321. When INVISIBLE-OK is set, stop at invisible headlines when going back.
  6322. This is important for non-interactive uses of the command."
  6323. (interactive "P")
  6324. (if (or (= (buffer-size) 0)
  6325. (and (not (save-excursion
  6326. (and (ignore-errors (org-back-to-heading invisible-ok))
  6327. (org-at-heading-p))))
  6328. (or force-heading (not (org-in-item-p)))))
  6329. (progn
  6330. (insert "\n* ")
  6331. (run-hooks 'org-insert-heading-hook))
  6332. (when (or force-heading (not (org-insert-item)))
  6333. (let* ((empty-line-p nil)
  6334. (level nil)
  6335. (on-heading (org-at-heading-p))
  6336. (head (save-excursion
  6337. (condition-case nil
  6338. (progn
  6339. (org-back-to-heading invisible-ok)
  6340. (when (and (not on-heading)
  6341. (featurep 'org-inlinetask)
  6342. (integerp org-inlinetask-min-level)
  6343. (>= (length (match-string 0))
  6344. org-inlinetask-min-level))
  6345. ;; Find a heading level before the inline task
  6346. (while (and (setq level (org-up-heading-safe))
  6347. (>= level org-inlinetask-min-level)))
  6348. (if (org-at-heading-p)
  6349. (org-back-to-heading invisible-ok)
  6350. (error "This should not happen")))
  6351. (setq empty-line-p (org-previous-line-empty-p))
  6352. (match-string 0))
  6353. (error "*"))))
  6354. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  6355. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  6356. pos hide-previous previous-pos)
  6357. (cond
  6358. ((and (org-at-heading-p) (bolp)
  6359. (or (bobp)
  6360. (save-excursion (backward-char 1) (not (outline-invisible-p)))))
  6361. ;; insert before the current line
  6362. (open-line (if blank 2 1)))
  6363. ((and (bolp)
  6364. (not org-insert-heading-respect-content)
  6365. (or (bobp)
  6366. (save-excursion
  6367. (backward-char 1) (not (outline-invisible-p)))))
  6368. ;; insert right here
  6369. nil)
  6370. (t
  6371. ;; somewhere in the line
  6372. (save-excursion
  6373. (setq previous-pos (point-at-bol))
  6374. (end-of-line)
  6375. (setq hide-previous (outline-invisible-p)))
  6376. (and org-insert-heading-respect-content (org-show-subtree))
  6377. (let ((split
  6378. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  6379. (save-excursion
  6380. (let ((p (point)))
  6381. (goto-char (point-at-bol))
  6382. (and (looking-at org-complex-heading-regexp)
  6383. (match-beginning 4)
  6384. (> p (match-beginning 4)))))))
  6385. tags pos)
  6386. (cond
  6387. (org-insert-heading-respect-content
  6388. (org-end-of-subtree nil t)
  6389. (when (featurep 'org-inlinetask)
  6390. (while (and (not (eobp))
  6391. (looking-at "\\(\\*+\\)[ \t]+")
  6392. (>= (length (match-string 1))
  6393. org-inlinetask-min-level))
  6394. (org-end-of-subtree nil t)))
  6395. (or (bolp) (newline))
  6396. (or (org-previous-line-empty-p)
  6397. (and blank (newline)))
  6398. (open-line 1))
  6399. ((org-at-heading-p)
  6400. (when hide-previous
  6401. (show-children)
  6402. (org-show-entry))
  6403. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?[ \t]*$")
  6404. (setq tags (and (match-end 2) (match-string 2)))
  6405. (and (match-end 1)
  6406. (delete-region (match-beginning 1) (match-end 1)))
  6407. (setq pos (point-at-bol))
  6408. (or split (end-of-line 1))
  6409. (delete-horizontal-space)
  6410. (if (string-match "\\`\\*+\\'"
  6411. (buffer-substring (point-at-bol) (point)))
  6412. (insert " "))
  6413. (newline (if blank 2 1))
  6414. (when tags
  6415. (save-excursion
  6416. (goto-char pos)
  6417. (end-of-line 1)
  6418. (insert " " tags)
  6419. (org-set-tags nil 'align))))
  6420. (t
  6421. (or split (end-of-line 1))
  6422. (newline (if blank 2 1)))))))
  6423. (insert head) (just-one-space)
  6424. (setq pos (point))
  6425. (end-of-line 1)
  6426. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  6427. (when (and org-insert-heading-respect-content hide-previous)
  6428. (save-excursion
  6429. (goto-char previous-pos)
  6430. (hide-subtree)))
  6431. (run-hooks 'org-insert-heading-hook)))))
  6432. (defun org-get-heading (&optional no-tags no-todo)
  6433. "Return the heading of the current entry, without the stars.
  6434. When NO-TAGS is non-nil, don't include tags.
  6435. When NO-TODO is non-nil, don't include TODO keywords."
  6436. (save-excursion
  6437. (org-back-to-heading t)
  6438. (cond
  6439. ((and no-tags no-todo)
  6440. (looking-at org-complex-heading-regexp)
  6441. (match-string 4))
  6442. (no-tags
  6443. (looking-at (concat org-outline-regexp
  6444. "\\(.*?\\)"
  6445. "\\(?:[ \t]+:[[:alnum:]:_@#%]+:\\)?[ \t]*$"))
  6446. (match-string 1))
  6447. (no-todo
  6448. (looking-at org-todo-line-regexp)
  6449. (match-string 3))
  6450. (t (looking-at org-heading-regexp)
  6451. (match-string 2)))))
  6452. (defun org-heading-components ()
  6453. "Return the components of the current heading.
  6454. This is a list with the following elements:
  6455. - the level as an integer
  6456. - the reduced level, different if `org-odd-levels-only' is set.
  6457. - the TODO keyword, or nil
  6458. - the priority character, like ?A, or nil if no priority is given
  6459. - the headline text itself, or the tags string if no headline text
  6460. - the tags string, or nil."
  6461. (save-excursion
  6462. (org-back-to-heading t)
  6463. (if (let (case-fold-search) (looking-at org-complex-heading-regexp))
  6464. (list (length (match-string 1))
  6465. (org-reduced-level (length (match-string 1)))
  6466. (org-match-string-no-properties 2)
  6467. (and (match-end 3) (aref (match-string 3) 2))
  6468. (org-match-string-no-properties 4)
  6469. (org-match-string-no-properties 5)))))
  6470. (defun org-get-entry ()
  6471. "Get the entry text, after heading, entire subtree."
  6472. (save-excursion
  6473. (org-back-to-heading t)
  6474. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  6475. (defun org-insert-heading-after-current ()
  6476. "Insert a new heading with same level as current, after current subtree."
  6477. (interactive)
  6478. (org-back-to-heading)
  6479. (org-insert-heading)
  6480. (org-move-subtree-down)
  6481. (end-of-line 1))
  6482. (defun org-insert-heading-respect-content ()
  6483. (interactive)
  6484. (let ((org-insert-heading-respect-content t))
  6485. (org-insert-heading t)))
  6486. (defun org-insert-todo-heading-respect-content (&optional force-state)
  6487. (interactive "P")
  6488. (let ((org-insert-heading-respect-content t))
  6489. (org-insert-todo-heading force-state t)))
  6490. (defun org-insert-todo-heading (arg &optional force-heading)
  6491. "Insert a new heading with the same level and TODO state as current heading.
  6492. If the heading has no TODO state, or if the state is DONE, use the first
  6493. state (TODO by default). Also with prefix arg, force first state."
  6494. (interactive "P")
  6495. (when (or force-heading (not (org-insert-item 'checkbox)))
  6496. (org-insert-heading force-heading)
  6497. (save-excursion
  6498. (org-back-to-heading)
  6499. (outline-previous-heading)
  6500. (looking-at org-todo-line-regexp))
  6501. (let*
  6502. ((new-mark-x
  6503. (if (or arg
  6504. (not (match-beginning 2))
  6505. (member (match-string 2) org-done-keywords))
  6506. (car org-todo-keywords-1)
  6507. (match-string 2)))
  6508. (new-mark
  6509. (or
  6510. (run-hook-with-args-until-success
  6511. 'org-todo-get-default-hook new-mark-x nil)
  6512. new-mark-x)))
  6513. (beginning-of-line 1)
  6514. (and (looking-at org-outline-regexp) (goto-char (match-end 0))
  6515. (if org-treat-insert-todo-heading-as-state-change
  6516. (org-todo new-mark)
  6517. (insert new-mark " "))))
  6518. (when org-provide-todo-statistics
  6519. (org-update-parent-todo-statistics))))
  6520. (defun org-insert-subheading (arg)
  6521. "Insert a new subheading and demote it.
  6522. Works for outline headings and for plain lists alike."
  6523. (interactive "P")
  6524. (org-insert-heading arg)
  6525. (cond
  6526. ((org-at-heading-p) (org-do-demote))
  6527. ((org-at-item-p) (org-indent-item))))
  6528. (defun org-insert-todo-subheading (arg)
  6529. "Insert a new subheading with TODO keyword or checkbox and demote it.
  6530. Works for outline headings and for plain lists alike."
  6531. (interactive "P")
  6532. (org-insert-todo-heading arg)
  6533. (cond
  6534. ((org-at-heading-p) (org-do-demote))
  6535. ((org-at-item-p) (org-indent-item))))
  6536. ;;; Promotion and Demotion
  6537. (defvar org-after-demote-entry-hook nil
  6538. "Hook run after an entry has been demoted.
  6539. The cursor will be at the beginning of the entry.
  6540. When a subtree is being demoted, the hook will be called for each node.")
  6541. (defvar org-after-promote-entry-hook nil
  6542. "Hook run after an entry has been promoted.
  6543. The cursor will be at the beginning of the entry.
  6544. When a subtree is being promoted, the hook will be called for each node.")
  6545. (defun org-promote-subtree ()
  6546. "Promote the entire subtree.
  6547. See also `org-promote'."
  6548. (interactive)
  6549. (save-excursion
  6550. (org-with-limited-levels (org-map-tree 'org-promote)))
  6551. (org-fix-position-after-promote))
  6552. (defun org-demote-subtree ()
  6553. "Demote the entire subtree. See `org-demote'.
  6554. See also `org-promote'."
  6555. (interactive)
  6556. (save-excursion
  6557. (org-with-limited-levels (org-map-tree 'org-demote)))
  6558. (org-fix-position-after-promote))
  6559. (defun org-do-promote ()
  6560. "Promote the current heading higher up the tree.
  6561. If the region is active in `transient-mark-mode', promote all headings
  6562. in the region."
  6563. (interactive)
  6564. (save-excursion
  6565. (if (org-region-active-p)
  6566. (org-map-region 'org-promote (region-beginning) (region-end))
  6567. (org-promote)))
  6568. (org-fix-position-after-promote))
  6569. (defun org-do-demote ()
  6570. "Demote the current heading lower down the tree.
  6571. If the region is active in `transient-mark-mode', demote all headings
  6572. in the region."
  6573. (interactive)
  6574. (save-excursion
  6575. (if (org-region-active-p)
  6576. (org-map-region 'org-demote (region-beginning) (region-end))
  6577. (org-demote)))
  6578. (org-fix-position-after-promote))
  6579. (defun org-fix-position-after-promote ()
  6580. "Make sure that after pro/demotion cursor position is right."
  6581. (let ((pos (point)))
  6582. (when (save-excursion
  6583. (beginning-of-line 1)
  6584. (looking-at org-todo-line-regexp)
  6585. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  6586. (cond ((eobp) (insert " "))
  6587. ((eolp) (insert " "))
  6588. ((equal (char-after) ?\ ) (forward-char 1))))))
  6589. (defun org-current-level ()
  6590. "Return the level of the current entry, or nil if before the first headline.
  6591. The level is the number of stars at the beginning of the headline."
  6592. (save-excursion
  6593. (org-with-limited-levels
  6594. (if (ignore-errors (org-back-to-heading t))
  6595. (funcall outline-level)))))
  6596. (defun org-get-previous-line-level ()
  6597. "Return the outline depth of the last headline before the current line.
  6598. Returns 0 for the first headline in the buffer, and nil if before the
  6599. first headline."
  6600. (let ((current-level (org-current-level))
  6601. (prev-level (when (> (line-number-at-pos) 1)
  6602. (save-excursion
  6603. (beginning-of-line 0)
  6604. (org-current-level)))))
  6605. (cond ((null current-level) nil) ; Before first headline
  6606. ((null prev-level) 0) ; At first headline
  6607. (prev-level))))
  6608. (defun org-reduced-level (l)
  6609. "Compute the effective level of a heading.
  6610. This takes into account the setting of `org-odd-levels-only'."
  6611. (cond
  6612. ((zerop l) 0)
  6613. (org-odd-levels-only (1+ (floor (/ l 2))))
  6614. (t l)))
  6615. (defun org-level-increment ()
  6616. "Return the number of stars that will be added or removed at a
  6617. time to headlines when structure editing, based on the value of
  6618. `org-odd-levels-only'."
  6619. (if org-odd-levels-only 2 1))
  6620. (defun org-get-valid-level (level &optional change)
  6621. "Rectify a level change under the influence of `org-odd-levels-only'
  6622. LEVEL is a current level, CHANGE is by how much the level should be
  6623. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  6624. even level numbers will become the next higher odd number."
  6625. (if org-odd-levels-only
  6626. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  6627. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  6628. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  6629. (max 1 (+ level (or change 0)))))
  6630. (if (boundp 'define-obsolete-function-alias)
  6631. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  6632. (define-obsolete-function-alias 'org-get-legal-level
  6633. 'org-get-valid-level)
  6634. (define-obsolete-function-alias 'org-get-legal-level
  6635. 'org-get-valid-level "23.1")))
  6636. (defun org-promote ()
  6637. "Promote the current heading higher up the tree.
  6638. If the region is active in `transient-mark-mode', promote all headings
  6639. in the region."
  6640. (org-back-to-heading t)
  6641. (let* ((level (save-match-data (funcall outline-level)))
  6642. (after-change-functions (remove 'flyspell-after-change-function
  6643. after-change-functions))
  6644. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  6645. (diff (abs (- level (length up-head) -1))))
  6646. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  6647. (replace-match up-head nil t)
  6648. ;; Fixup tag positioning
  6649. (and org-auto-align-tags (org-set-tags nil t))
  6650. (if org-adapt-indentation (org-fixup-indentation (- diff)))
  6651. (run-hooks 'org-after-promote-entry-hook)))
  6652. (defun org-demote ()
  6653. "Demote the current heading lower down the tree.
  6654. If the region is active in `transient-mark-mode', demote all headings
  6655. in the region."
  6656. (org-back-to-heading t)
  6657. (let* ((level (save-match-data (funcall outline-level)))
  6658. (after-change-functions (remove 'flyspell-after-change-function
  6659. after-change-functions))
  6660. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  6661. (diff (abs (- level (length down-head) -1))))
  6662. (replace-match down-head nil t)
  6663. ;; Fixup tag positioning
  6664. (and org-auto-align-tags (org-set-tags nil t))
  6665. (if org-adapt-indentation (org-fixup-indentation diff))
  6666. (run-hooks 'org-after-demote-entry-hook)))
  6667. (defun org-cycle-level ()
  6668. "Cycle the level of an empty headline through possible states.
  6669. This goes first to child, then to parent, level, then up the hierarchy.
  6670. After top level, it switches back to sibling level."
  6671. (interactive)
  6672. (let ((org-adapt-indentation nil))
  6673. (when (org-point-at-end-of-empty-headline)
  6674. (setq this-command 'org-cycle-level) ; Only needed for caching
  6675. (let ((cur-level (org-current-level))
  6676. (prev-level (org-get-previous-line-level)))
  6677. (cond
  6678. ;; If first headline in file, promote to top-level.
  6679. ((= prev-level 0)
  6680. (loop repeat (/ (- cur-level 1) (org-level-increment))
  6681. do (org-do-promote)))
  6682. ;; If same level as prev, demote one.
  6683. ((= prev-level cur-level)
  6684. (org-do-demote))
  6685. ;; If parent is top-level, promote to top level if not already.
  6686. ((= prev-level 1)
  6687. (loop repeat (/ (- cur-level 1) (org-level-increment))
  6688. do (org-do-promote)))
  6689. ;; If top-level, return to prev-level.
  6690. ((= cur-level 1)
  6691. (loop repeat (/ (- prev-level 1) (org-level-increment))
  6692. do (org-do-demote)))
  6693. ;; If less than prev-level, promote one.
  6694. ((< cur-level prev-level)
  6695. (org-do-promote))
  6696. ;; If deeper than prev-level, promote until higher than
  6697. ;; prev-level.
  6698. ((> cur-level prev-level)
  6699. (loop repeat (+ 1 (/ (- cur-level prev-level) (org-level-increment)))
  6700. do (org-do-promote))))
  6701. t))))
  6702. (defun org-map-tree (fun)
  6703. "Call FUN for every heading underneath the current one."
  6704. (org-back-to-heading)
  6705. (let ((level (funcall outline-level)))
  6706. (save-excursion
  6707. (funcall fun)
  6708. (while (and (progn
  6709. (outline-next-heading)
  6710. (> (funcall outline-level) level))
  6711. (not (eobp)))
  6712. (funcall fun)))))
  6713. (defun org-map-region (fun beg end)
  6714. "Call FUN for every heading between BEG and END."
  6715. (let ((org-ignore-region t))
  6716. (save-excursion
  6717. (setq end (copy-marker end))
  6718. (goto-char beg)
  6719. (if (and (re-search-forward org-outline-regexp-bol nil t)
  6720. (< (point) end))
  6721. (funcall fun))
  6722. (while (and (progn
  6723. (outline-next-heading)
  6724. (< (point) end))
  6725. (not (eobp)))
  6726. (funcall fun)))))
  6727. (defvar org-property-end-re) ; silence byte-compiler
  6728. (defun org-fixup-indentation (diff)
  6729. "Change the indentation in the current entry by DIFF.
  6730. However, if any line in the current entry has no indentation, or if it
  6731. would end up with no indentation after the change, nothing at all is done."
  6732. (save-excursion
  6733. (let ((end (save-excursion (outline-next-heading)
  6734. (point-marker)))
  6735. (prohibit (if (> diff 0)
  6736. "^\\S-"
  6737. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  6738. col)
  6739. (unless (save-excursion (end-of-line 1)
  6740. (re-search-forward prohibit end t))
  6741. (while (and (< (point) end)
  6742. (re-search-forward "^[ \t]+" end t))
  6743. (goto-char (match-end 0))
  6744. (setq col (current-column))
  6745. (if (< diff 0) (replace-match ""))
  6746. (org-indent-to-column (+ diff col))))
  6747. (move-marker end nil))))
  6748. (defun org-convert-to-odd-levels ()
  6749. "Convert an org-mode file with all levels allowed to one with odd levels.
  6750. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  6751. level 5 etc."
  6752. (interactive)
  6753. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  6754. (let ((outline-level 'org-outline-level)
  6755. (org-odd-levels-only nil) n)
  6756. (save-excursion
  6757. (goto-char (point-min))
  6758. (while (re-search-forward "^\\*\\*+ " nil t)
  6759. (setq n (- (length (match-string 0)) 2))
  6760. (while (>= (setq n (1- n)) 0)
  6761. (org-demote))
  6762. (end-of-line 1))))))
  6763. (defun org-convert-to-oddeven-levels ()
  6764. "Convert an org-mode file with only odd levels to one with odd/even levels.
  6765. This promotes level 3 to level 2, level 5 to level 3 etc. If the
  6766. file contains a section with an even level, conversion would
  6767. destroy the structure of the file. An error is signaled in this
  6768. case."
  6769. (interactive)
  6770. (goto-char (point-min))
  6771. ;; First check if there are no even levels
  6772. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  6773. (org-show-context t)
  6774. (error "Not all levels are odd in this file. Conversion not possible"))
  6775. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  6776. (let ((outline-regexp org-outline-regexp)
  6777. (outline-level 'org-outline-level)
  6778. (org-odd-levels-only nil) n)
  6779. (save-excursion
  6780. (goto-char (point-min))
  6781. (while (re-search-forward "^\\*\\*+ " nil t)
  6782. (setq n (/ (1- (length (match-string 0))) 2))
  6783. (while (>= (setq n (1- n)) 0)
  6784. (org-promote))
  6785. (end-of-line 1))))))
  6786. (defun org-tr-level (n)
  6787. "Make N odd if required."
  6788. (if org-odd-levels-only (1+ (/ n 2)) n))
  6789. ;;; Vertical tree motion, cutting and pasting of subtrees
  6790. (defun org-move-subtree-up (&optional arg)
  6791. "Move the current subtree up past ARG headlines of the same level."
  6792. (interactive "p")
  6793. (org-move-subtree-down (- (prefix-numeric-value arg))))
  6794. (defun org-move-subtree-down (&optional arg)
  6795. "Move the current subtree down past ARG headlines of the same level."
  6796. (interactive "p")
  6797. (setq arg (prefix-numeric-value arg))
  6798. (let ((movfunc (if (> arg 0) 'org-get-next-sibling
  6799. 'org-get-last-sibling))
  6800. (ins-point (make-marker))
  6801. (cnt (abs arg))
  6802. (col (current-column))
  6803. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  6804. ;; Select the tree
  6805. (org-back-to-heading)
  6806. (setq beg0 (point))
  6807. (save-excursion
  6808. (setq ne-beg (org-back-over-empty-lines))
  6809. (setq beg (point)))
  6810. (save-match-data
  6811. (save-excursion (outline-end-of-heading)
  6812. (setq folded (outline-invisible-p)))
  6813. (outline-end-of-subtree))
  6814. (outline-next-heading)
  6815. (setq ne-end (org-back-over-empty-lines))
  6816. (setq end (point))
  6817. (goto-char beg0)
  6818. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  6819. ;; include less whitespace
  6820. (save-excursion
  6821. (goto-char beg)
  6822. (forward-line (- ne-beg ne-end))
  6823. (setq beg (point))))
  6824. ;; Find insertion point, with error handling
  6825. (while (> cnt 0)
  6826. (or (and (funcall movfunc) (looking-at org-outline-regexp))
  6827. (progn (goto-char beg0)
  6828. (error "Cannot move past superior level or buffer limit")))
  6829. (setq cnt (1- cnt)))
  6830. (if (> arg 0)
  6831. ;; Moving forward - still need to move over subtree
  6832. (progn (org-end-of-subtree t t)
  6833. (save-excursion
  6834. (org-back-over-empty-lines)
  6835. (or (bolp) (newline)))))
  6836. (setq ne-ins (org-back-over-empty-lines))
  6837. (move-marker ins-point (point))
  6838. (setq txt (buffer-substring beg end))
  6839. (org-save-markers-in-region beg end)
  6840. (delete-region beg end)
  6841. (org-remove-empty-overlays-at beg)
  6842. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  6843. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  6844. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  6845. (let ((bbb (point)))
  6846. (insert-before-markers txt)
  6847. (org-reinstall-markers-in-region bbb)
  6848. (move-marker ins-point bbb))
  6849. (or (bolp) (insert "\n"))
  6850. (setq ins-end (point))
  6851. (goto-char ins-point)
  6852. (org-skip-whitespace)
  6853. (when (and (< arg 0)
  6854. (org-first-sibling-p)
  6855. (> ne-ins ne-beg))
  6856. ;; Move whitespace back to beginning
  6857. (save-excursion
  6858. (goto-char ins-end)
  6859. (let ((kill-whole-line t))
  6860. (kill-line (- ne-ins ne-beg)) (point)))
  6861. (insert (make-string (- ne-ins ne-beg) ?\n)))
  6862. (move-marker ins-point nil)
  6863. (if folded
  6864. (hide-subtree)
  6865. (org-show-entry)
  6866. (show-children)
  6867. (org-cycle-hide-drawers 'children))
  6868. (org-clean-visibility-after-subtree-move)
  6869. ;; move back to the initial column we were at
  6870. (move-to-column col)))
  6871. (defvar org-subtree-clip ""
  6872. "Clipboard for cut and paste of subtrees.
  6873. This is actually only a copy of the kill, because we use the normal kill
  6874. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  6875. (defvar org-subtree-clip-folded nil
  6876. "Was the last copied subtree folded?
  6877. This is used to fold the tree back after pasting.")
  6878. (defun org-cut-subtree (&optional n)
  6879. "Cut the current subtree into the clipboard.
  6880. With prefix arg N, cut this many sequential subtrees.
  6881. This is a short-hand for marking the subtree and then cutting it."
  6882. (interactive "p")
  6883. (org-copy-subtree n 'cut))
  6884. (defun org-copy-subtree (&optional n cut force-store-markers)
  6885. "Cut the current subtree into the clipboard.
  6886. With prefix arg N, cut this many sequential subtrees.
  6887. This is a short-hand for marking the subtree and then copying it.
  6888. If CUT is non-nil, actually cut the subtree.
  6889. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  6890. of some markers in the region, even if CUT is non-nil. This is
  6891. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  6892. (interactive "p")
  6893. (let (beg end folded (beg0 (point)))
  6894. (if (org-called-interactively-p 'any)
  6895. (org-back-to-heading nil) ; take what looks like a subtree
  6896. (org-back-to-heading t)) ; take what is really there
  6897. (org-back-over-empty-lines)
  6898. (setq beg (point))
  6899. (skip-chars-forward " \t\r\n")
  6900. (save-match-data
  6901. (save-excursion (outline-end-of-heading)
  6902. (setq folded (outline-invisible-p)))
  6903. (condition-case nil
  6904. (org-forward-same-level (1- n) t)
  6905. (error nil))
  6906. (org-end-of-subtree t t))
  6907. (org-back-over-empty-lines)
  6908. (setq end (point))
  6909. (goto-char beg0)
  6910. (when (> end beg)
  6911. (setq org-subtree-clip-folded folded)
  6912. (when (or cut force-store-markers)
  6913. (org-save-markers-in-region beg end))
  6914. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  6915. (setq org-subtree-clip (current-kill 0))
  6916. (message "%s: Subtree(s) with %d characters"
  6917. (if cut "Cut" "Copied")
  6918. (length org-subtree-clip)))))
  6919. (defun org-paste-subtree (&optional level tree for-yank)
  6920. "Paste the clipboard as a subtree, with modification of headline level.
  6921. The entire subtree is promoted or demoted in order to match a new headline
  6922. level.
  6923. If the cursor is at the beginning of a headline, the same level as
  6924. that headline is used to paste the tree
  6925. If not, the new level is derived from the *visible* headings
  6926. before and after the insertion point, and taken to be the inferior headline
  6927. level of the two. So if the previous visible heading is level 3 and the
  6928. next is level 4 (or vice versa), level 4 will be used for insertion.
  6929. This makes sure that the subtree remains an independent subtree and does
  6930. not swallow low level entries.
  6931. You can also force a different level, either by using a numeric prefix
  6932. argument, or by inserting the heading marker by hand. For example, if the
  6933. cursor is after \"*****\", then the tree will be shifted to level 5.
  6934. If optional TREE is given, use this text instead of the kill ring.
  6935. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  6936. move back over whitespace before inserting, and move point to the end of
  6937. the inserted text when done."
  6938. (interactive "P")
  6939. (setq tree (or tree (and kill-ring (current-kill 0))))
  6940. (unless (org-kill-is-subtree-p tree)
  6941. (error "%s"
  6942. (substitute-command-keys
  6943. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  6944. (org-with-limited-levels
  6945. (let* ((visp (not (outline-invisible-p)))
  6946. (txt tree)
  6947. (^re_ "\\(\\*+\\)[ \t]*")
  6948. (old-level (if (string-match org-outline-regexp-bol txt)
  6949. (- (match-end 0) (match-beginning 0) 1)
  6950. -1))
  6951. (force-level (cond (level (prefix-numeric-value level))
  6952. ((and (looking-at "[ \t]*$")
  6953. (string-match
  6954. "^\\*+$" (buffer-substring
  6955. (point-at-bol) (point))))
  6956. (- (match-end 1) (match-beginning 1)))
  6957. ((and (bolp)
  6958. (looking-at org-outline-regexp))
  6959. (- (match-end 0) (point) 1))
  6960. (t nil)))
  6961. (previous-level (save-excursion
  6962. (condition-case nil
  6963. (progn
  6964. (outline-previous-visible-heading 1)
  6965. (if (looking-at ^re_)
  6966. (- (match-end 0) (match-beginning 0) 1)
  6967. 1))
  6968. (error 1))))
  6969. (next-level (save-excursion
  6970. (condition-case nil
  6971. (progn
  6972. (or (looking-at org-outline-regexp)
  6973. (outline-next-visible-heading 1))
  6974. (if (looking-at ^re_)
  6975. (- (match-end 0) (match-beginning 0) 1)
  6976. 1))
  6977. (error 1))))
  6978. (new-level (or force-level (max previous-level next-level)))
  6979. (shift (if (or (= old-level -1)
  6980. (= new-level -1)
  6981. (= old-level new-level))
  6982. 0
  6983. (- new-level old-level)))
  6984. (delta (if (> shift 0) -1 1))
  6985. (func (if (> shift 0) 'org-demote 'org-promote))
  6986. (org-odd-levels-only nil)
  6987. beg end newend)
  6988. ;; Remove the forced level indicator
  6989. (if force-level
  6990. (delete-region (point-at-bol) (point)))
  6991. ;; Paste
  6992. (beginning-of-line (if (bolp) 1 2))
  6993. (unless for-yank (org-back-over-empty-lines))
  6994. (setq beg (point))
  6995. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  6996. (insert-before-markers txt)
  6997. (unless (string-match "\n\\'" txt) (insert "\n"))
  6998. (setq newend (point))
  6999. (org-reinstall-markers-in-region beg)
  7000. (setq end (point))
  7001. (goto-char beg)
  7002. (skip-chars-forward " \t\n\r")
  7003. (setq beg (point))
  7004. (if (and (outline-invisible-p) visp)
  7005. (save-excursion (outline-show-heading)))
  7006. ;; Shift if necessary
  7007. (unless (= shift 0)
  7008. (save-restriction
  7009. (narrow-to-region beg end)
  7010. (while (not (= shift 0))
  7011. (org-map-region func (point-min) (point-max))
  7012. (setq shift (+ delta shift)))
  7013. (goto-char (point-min))
  7014. (setq newend (point-max))))
  7015. (when (or (org-called-interactively-p 'interactive) for-yank)
  7016. (message "Clipboard pasted as level %d subtree" new-level))
  7017. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  7018. kill-ring
  7019. (eq org-subtree-clip (current-kill 0))
  7020. org-subtree-clip-folded)
  7021. ;; The tree was folded before it was killed/copied
  7022. (hide-subtree))
  7023. (and for-yank (goto-char newend)))))
  7024. (defun org-kill-is-subtree-p (&optional txt)
  7025. "Check if the current kill is an outline subtree, or a set of trees.
  7026. Returns nil if kill does not start with a headline, or if the first
  7027. headline level is not the largest headline level in the tree.
  7028. So this will actually accept several entries of equal levels as well,
  7029. which is OK for `org-paste-subtree'.
  7030. If optional TXT is given, check this string instead of the current kill."
  7031. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  7032. (re (org-get-limited-outline-regexp))
  7033. (^re (concat "^" re))
  7034. (start-level (and kill
  7035. (string-match
  7036. (concat "\\`\\([ \t\n\r]*?\n\\)?\\(" re "\\)")
  7037. kill)
  7038. (- (match-end 2) (match-beginning 2) 1)))
  7039. (start (1+ (or (match-beginning 2) -1))))
  7040. (if (not start-level)
  7041. (progn
  7042. nil) ;; does not even start with a heading
  7043. (catch 'exit
  7044. (while (setq start (string-match ^re kill (1+ start)))
  7045. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  7046. (throw 'exit nil)))
  7047. t))))
  7048. (defvar org-markers-to-move nil
  7049. "Markers that should be moved with a cut-and-paste operation.
  7050. Those markers are stored together with their positions relative to
  7051. the start of the region.")
  7052. (defun org-save-markers-in-region (beg end)
  7053. "Check markers in region.
  7054. If these markers are between BEG and END, record their position relative
  7055. to BEG, so that after moving the block of text, we can put the markers back
  7056. into place.
  7057. This function gets called just before an entry or tree gets cut from the
  7058. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  7059. called immediately, to move the markers with the entries."
  7060. (setq org-markers-to-move nil)
  7061. (when (featurep 'org-clock)
  7062. (org-clock-save-markers-for-cut-and-paste beg end))
  7063. (when (featurep 'org-agenda)
  7064. (org-agenda-save-markers-for-cut-and-paste beg end)))
  7065. (defun org-check-and-save-marker (marker beg end)
  7066. "Check if MARKER is between BEG and END.
  7067. If yes, remember the marker and the distance to BEG."
  7068. (when (and (marker-buffer marker)
  7069. (equal (marker-buffer marker) (current-buffer)))
  7070. (if (and (>= marker beg) (< marker end))
  7071. (push (cons marker (- marker beg)) org-markers-to-move))))
  7072. (defun org-reinstall-markers-in-region (beg)
  7073. "Move all remembered markers to their position relative to BEG."
  7074. (mapc (lambda (x)
  7075. (move-marker (car x) (+ beg (cdr x))))
  7076. org-markers-to-move)
  7077. (setq org-markers-to-move nil))
  7078. (defun org-narrow-to-subtree ()
  7079. "Narrow buffer to the current subtree."
  7080. (interactive)
  7081. (save-excursion
  7082. (save-match-data
  7083. (org-with-limited-levels
  7084. (narrow-to-region
  7085. (progn (org-back-to-heading t) (point))
  7086. (progn (org-end-of-subtree t t)
  7087. (if (and (org-at-heading-p) (not (eobp))) (backward-char 1))
  7088. (point)))))))
  7089. (defun org-narrow-to-block ()
  7090. "Narrow buffer to the current block."
  7091. (interactive)
  7092. (let* ((case-fold-search t)
  7093. (blockp (org-between-regexps-p "^[ \t]*#\\+begin_.*"
  7094. "^[ \t]*#\\+end_.*")))
  7095. (if blockp
  7096. (narrow-to-region (car blockp) (cdr blockp))
  7097. (error "Not in a block"))))
  7098. (eval-when-compile
  7099. (defvar org-property-drawer-re))
  7100. (defvar org-property-start-re) ;; defined below
  7101. (defun org-clone-subtree-with-time-shift (n &optional shift)
  7102. "Clone the task (subtree) at point N times.
  7103. The clones will be inserted as siblings.
  7104. In interactive use, the user will be prompted for the number of
  7105. clones to be produced, and for a time SHIFT, which may be a
  7106. repeater as used in time stamps, for example `+3d'.
  7107. When a valid repeater is given and the entry contains any time
  7108. stamps, the clones will become a sequence in time, with time
  7109. stamps in the subtree shifted for each clone produced. If SHIFT
  7110. is nil or the empty string, time stamps will be left alone. The
  7111. ID property of the original subtree is removed.
  7112. If the original subtree did contain time stamps with a repeater,
  7113. the following will happen:
  7114. - the repeater will be removed in each clone
  7115. - an additional clone will be produced, with the current, unshifted
  7116. date(s) in the entry.
  7117. - the original entry will be placed *after* all the clones, with
  7118. repeater intact.
  7119. - the start days in the repeater in the original entry will be shifted
  7120. to past the last clone.
  7121. I this way you can spell out a number of instances of a repeating task,
  7122. and still retain the repeater to cover future instances of the task."
  7123. (interactive "nNumber of clones to produce: \nsDate shift per clone (e.g. +1w, empty to copy unchanged): ")
  7124. (let (beg end template task idprop
  7125. shift-n shift-what doshift nmin nmax (n-no-remove -1)
  7126. (drawer-re org-drawer-regexp))
  7127. (if (not (and (integerp n) (> n 0)))
  7128. (error "Invalid number of replications %s" n))
  7129. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  7130. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([dwmy]\\)[ \t]*\\'"
  7131. shift)))
  7132. (error "Invalid shift specification %s" shift))
  7133. (when doshift
  7134. (setq shift-n (string-to-number (match-string 1 shift))
  7135. shift-what (cdr (assoc (match-string 2 shift)
  7136. '(("d" . day) ("w" . week)
  7137. ("m" . month) ("y" . year))))))
  7138. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  7139. (setq nmin 1 nmax n)
  7140. (org-back-to-heading t)
  7141. (setq beg (point))
  7142. (setq idprop (org-entry-get nil "ID"))
  7143. (org-end-of-subtree t t)
  7144. (or (bolp) (insert "\n"))
  7145. (setq end (point))
  7146. (setq template (buffer-substring beg end))
  7147. (when (and doshift
  7148. (string-match "<[^<>\n]+ \\+[0-9]+[dwmy][^<>\n]*>" template))
  7149. (delete-region beg end)
  7150. (setq end beg)
  7151. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  7152. (goto-char end)
  7153. (loop for n from nmin to nmax do
  7154. ;; prepare clone
  7155. (with-temp-buffer
  7156. (insert template)
  7157. (org-mode)
  7158. (goto-char (point-min))
  7159. (org-show-subtree)
  7160. (and idprop (if org-clone-delete-id
  7161. (org-entry-delete nil "ID")
  7162. (org-id-get-create t)))
  7163. (unless (= n 0)
  7164. (while (re-search-forward "^[ \t]*CLOCK:.*$" nil t)
  7165. (kill-whole-line))
  7166. (goto-char (point-min))
  7167. (while (re-search-forward drawer-re nil t)
  7168. (mapc (lambda (d)
  7169. (org-remove-empty-drawer-at d (point))) org-drawers)))
  7170. (goto-char (point-min))
  7171. (when doshift
  7172. (while (re-search-forward org-ts-regexp-both nil t)
  7173. (org-timestamp-change (* n shift-n) shift-what))
  7174. (unless (= n n-no-remove)
  7175. (goto-char (point-min))
  7176. (while (re-search-forward org-ts-regexp nil t)
  7177. (save-excursion
  7178. (goto-char (match-beginning 0))
  7179. (if (looking-at "<[^<>\n]+\\( +\\+[0-9]+[dwmy]\\)")
  7180. (delete-region (match-beginning 1) (match-end 1)))))))
  7181. (setq task (buffer-string)))
  7182. (insert task))
  7183. (goto-char beg)))
  7184. ;;; Outline Sorting
  7185. (defun org-sort (with-case)
  7186. "Call `org-sort-entries', `org-table-sort-lines' or `org-sort-list'.
  7187. Optional argument WITH-CASE means sort case-sensitively."
  7188. (interactive "P")
  7189. (cond
  7190. ((org-at-table-p) (org-call-with-arg 'org-table-sort-lines with-case))
  7191. ((org-at-item-p) (org-call-with-arg 'org-sort-list with-case))
  7192. (t
  7193. (org-call-with-arg 'org-sort-entries with-case))))
  7194. (defun org-sort-remove-invisible (s)
  7195. (remove-text-properties 0 (length s) org-rm-props s)
  7196. (while (string-match org-bracket-link-regexp s)
  7197. (setq s (replace-match (if (match-end 2)
  7198. (match-string 3 s)
  7199. (match-string 1 s)) t t s)))
  7200. s)
  7201. (defvar org-priority-regexp) ; defined later in the file
  7202. (defvar org-after-sorting-entries-or-items-hook nil
  7203. "Hook that is run after a bunch of entries or items have been sorted.
  7204. When children are sorted, the cursor is in the parent line when this
  7205. hook gets called. When a region or a plain list is sorted, the cursor
  7206. will be in the first entry of the sorted region/list.")
  7207. (defun org-sort-entries
  7208. (&optional with-case sorting-type getkey-func compare-func property)
  7209. "Sort entries on a certain level of an outline tree.
  7210. If there is an active region, the entries in the region are sorted.
  7211. Else, if the cursor is before the first entry, sort the top-level items.
  7212. Else, the children of the entry at point are sorted.
  7213. Sorting can be alphabetically, numerically, by date/time as given by
  7214. a time stamp, by a property or by priority.
  7215. The command prompts for the sorting type unless it has been given to the
  7216. function through the SORTING-TYPE argument, which needs to be a character,
  7217. \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?r ?R ?f ?F). Here is the
  7218. precise meaning of each character:
  7219. n Numerically, by converting the beginning of the entry/item to a number.
  7220. a Alphabetically, ignoring the TODO keyword and the priority, if any.
  7221. t By date/time, either the first active time stamp in the entry, or, if
  7222. none exist, by the first inactive one.
  7223. s By the scheduled date/time.
  7224. d By deadline date/time.
  7225. c By creation time, which is assumed to be the first inactive time stamp
  7226. at the beginning of a line.
  7227. p By priority according to the cookie.
  7228. r By the value of a property.
  7229. Capital letters will reverse the sort order.
  7230. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  7231. called with point at the beginning of the record. It must return either
  7232. a string or a number that should serve as the sorting key for that record.
  7233. Comparing entries ignores case by default. However, with an optional argument
  7234. WITH-CASE, the sorting considers case as well."
  7235. (interactive "P")
  7236. (let ((case-func (if with-case 'identity 'downcase))
  7237. start beg end stars re re2
  7238. txt what tmp)
  7239. ;; Find beginning and end of region to sort
  7240. (cond
  7241. ((org-region-active-p)
  7242. ;; we will sort the region
  7243. (setq end (region-end)
  7244. what "region")
  7245. (goto-char (region-beginning))
  7246. (if (not (org-at-heading-p)) (outline-next-heading))
  7247. (setq start (point)))
  7248. ((or (org-at-heading-p)
  7249. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  7250. ;; we will sort the children of the current headline
  7251. (org-back-to-heading)
  7252. (setq start (point)
  7253. end (progn (org-end-of-subtree t t)
  7254. (or (bolp) (insert "\n"))
  7255. (org-back-over-empty-lines)
  7256. (point))
  7257. what "children")
  7258. (goto-char start)
  7259. (show-subtree)
  7260. (outline-next-heading))
  7261. (t
  7262. ;; we will sort the top-level entries in this file
  7263. (goto-char (point-min))
  7264. (or (org-at-heading-p) (outline-next-heading))
  7265. (setq start (point))
  7266. (goto-char (point-max))
  7267. (beginning-of-line 1)
  7268. (when (looking-at ".*?\\S-")
  7269. ;; File ends in a non-white line
  7270. (end-of-line 1)
  7271. (insert "\n"))
  7272. (setq end (point-max))
  7273. (setq what "top-level")
  7274. (goto-char start)
  7275. (show-all)))
  7276. (setq beg (point))
  7277. (if (>= beg end) (error "Nothing to sort"))
  7278. (looking-at "\\(\\*+\\)")
  7279. (setq stars (match-string 1)
  7280. re (concat "^" (regexp-quote stars) " +")
  7281. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[ \t\n]")
  7282. txt (buffer-substring beg end))
  7283. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  7284. (if (and (not (equal stars "*")) (string-match re2 txt))
  7285. (error "Region to sort contains a level above the first entry"))
  7286. (unless sorting-type
  7287. (message
  7288. "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
  7289. [t]ime [s]cheduled [d]eadline [c]reated
  7290. A/N/T/S/D/C/P/O/F means reversed:"
  7291. what)
  7292. (setq sorting-type (read-char-exclusive))
  7293. (and (= (downcase sorting-type) ?f)
  7294. (setq getkey-func
  7295. (org-icompleting-read "Sort using function: "
  7296. obarray 'fboundp t nil nil))
  7297. (setq getkey-func (intern getkey-func)))
  7298. (and (= (downcase sorting-type) ?r)
  7299. (setq property
  7300. (org-icompleting-read "Property: "
  7301. (mapcar 'list (org-buffer-property-keys t))
  7302. nil t))))
  7303. (message "Sorting entries...")
  7304. (save-restriction
  7305. (narrow-to-region start end)
  7306. (let ((dcst (downcase sorting-type))
  7307. (case-fold-search nil)
  7308. (now (current-time)))
  7309. (sort-subr
  7310. (/= dcst sorting-type)
  7311. ;; This function moves to the beginning character of the "record" to
  7312. ;; be sorted.
  7313. (lambda nil
  7314. (if (re-search-forward re nil t)
  7315. (goto-char (match-beginning 0))
  7316. (goto-char (point-max))))
  7317. ;; This function moves to the last character of the "record" being
  7318. ;; sorted.
  7319. (lambda nil
  7320. (save-match-data
  7321. (condition-case nil
  7322. (outline-forward-same-level 1)
  7323. (error
  7324. (goto-char (point-max))))))
  7325. ;; This function returns the value that gets sorted against.
  7326. (lambda nil
  7327. (cond
  7328. ((= dcst ?n)
  7329. (if (looking-at org-complex-heading-regexp)
  7330. (string-to-number (match-string 4))
  7331. nil))
  7332. ((= dcst ?a)
  7333. (if (looking-at org-complex-heading-regexp)
  7334. (funcall case-func (match-string 4))
  7335. nil))
  7336. ((= dcst ?t)
  7337. (let ((end (save-excursion (outline-next-heading) (point))))
  7338. (if (or (re-search-forward org-ts-regexp end t)
  7339. (re-search-forward org-ts-regexp-both end t))
  7340. (org-time-string-to-seconds (match-string 0))
  7341. (org-float-time now))))
  7342. ((= dcst ?c)
  7343. (let ((end (save-excursion (outline-next-heading) (point))))
  7344. (if (re-search-forward
  7345. (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
  7346. end t)
  7347. (org-time-string-to-seconds (match-string 0))
  7348. (org-float-time now))))
  7349. ((= dcst ?s)
  7350. (let ((end (save-excursion (outline-next-heading) (point))))
  7351. (if (re-search-forward org-scheduled-time-regexp end t)
  7352. (org-time-string-to-seconds (match-string 1))
  7353. (org-float-time now))))
  7354. ((= dcst ?d)
  7355. (let ((end (save-excursion (outline-next-heading) (point))))
  7356. (if (re-search-forward org-deadline-time-regexp end t)
  7357. (org-time-string-to-seconds (match-string 1))
  7358. (org-float-time now))))
  7359. ((= dcst ?p)
  7360. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  7361. (string-to-char (match-string 2))
  7362. org-default-priority))
  7363. ((= dcst ?r)
  7364. (or (org-entry-get nil property) ""))
  7365. ((= dcst ?o)
  7366. (if (looking-at org-complex-heading-regexp)
  7367. (- 9999 (length (member (match-string 2)
  7368. org-todo-keywords-1)))))
  7369. ((= dcst ?f)
  7370. (if getkey-func
  7371. (progn
  7372. (setq tmp (funcall getkey-func))
  7373. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  7374. tmp)
  7375. (error "Invalid key function `%s'" getkey-func)))
  7376. (t (error "Invalid sorting type `%c'" sorting-type))))
  7377. nil
  7378. (cond
  7379. ((= dcst ?a) 'string<)
  7380. ((= dcst ?f) compare-func)
  7381. ((member dcst '(?p ?t ?s ?d ?c)) '<)
  7382. (t nil)))))
  7383. (run-hooks 'org-after-sorting-entries-or-items-hook)
  7384. (message "Sorting entries...done")))
  7385. (defun org-do-sort (table what &optional with-case sorting-type)
  7386. "Sort TABLE of WHAT according to SORTING-TYPE.
  7387. The user will be prompted for the SORTING-TYPE if the call to this
  7388. function does not specify it. WHAT is only for the prompt, to indicate
  7389. what is being sorted. The sorting key will be extracted from
  7390. the car of the elements of the table.
  7391. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  7392. (unless sorting-type
  7393. (message
  7394. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  7395. what)
  7396. (setq sorting-type (read-char-exclusive)))
  7397. (let ((dcst (downcase sorting-type))
  7398. extractfun comparefun)
  7399. ;; Define the appropriate functions
  7400. (cond
  7401. ((= dcst ?n)
  7402. (setq extractfun 'string-to-number
  7403. comparefun (if (= dcst sorting-type) '< '>)))
  7404. ((= dcst ?a)
  7405. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  7406. (lambda(x) (downcase (org-sort-remove-invisible x))))
  7407. comparefun (if (= dcst sorting-type)
  7408. 'string<
  7409. (lambda (a b) (and (not (string< a b))
  7410. (not (string= a b)))))))
  7411. ((= dcst ?t)
  7412. (setq extractfun
  7413. (lambda (x)
  7414. (if (or (string-match org-ts-regexp x)
  7415. (string-match org-ts-regexp-both x))
  7416. (org-float-time
  7417. (org-time-string-to-time (match-string 0 x)))
  7418. 0))
  7419. comparefun (if (= dcst sorting-type) '< '>)))
  7420. (t (error "Invalid sorting type `%c'" sorting-type)))
  7421. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  7422. table)
  7423. (lambda (a b) (funcall comparefun (car a) (car b))))))
  7424. ;;; The orgstruct minor mode
  7425. ;; Define a minor mode which can be used in other modes in order to
  7426. ;; integrate the org-mode structure editing commands.
  7427. ;; This is really a hack, because the org-mode structure commands use
  7428. ;; keys which normally belong to the major mode. Here is how it
  7429. ;; works: The minor mode defines all the keys necessary to operate the
  7430. ;; structure commands, but wraps the commands into a function which
  7431. ;; tests if the cursor is currently at a headline or a plain list
  7432. ;; item. If that is the case, the structure command is used,
  7433. ;; temporarily setting many Org-mode variables like regular
  7434. ;; expressions for filling etc. However, when any of those keys is
  7435. ;; used at a different location, function uses `key-binding' to look
  7436. ;; up if the key has an associated command in another currently active
  7437. ;; keymap (minor modes, major mode, global), and executes that
  7438. ;; command. There might be problems if any of the keys is otherwise
  7439. ;; used as a prefix key.
  7440. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  7441. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  7442. ;; addresses this by checking explicitly for both bindings.
  7443. (defvar orgstruct-mode-map (make-sparse-keymap)
  7444. "Keymap for the minor `orgstruct-mode'.")
  7445. (defvar org-local-vars nil
  7446. "List of local variables, for use by `orgstruct-mode'.")
  7447. ;;;###autoload
  7448. (define-minor-mode orgstruct-mode
  7449. "Toggle the minor mode `orgstruct-mode'.
  7450. This mode is for using Org-mode structure commands in other
  7451. modes. The following keys behave as if Org-mode were active, if
  7452. the cursor is on a headline, or on a plain list item (both as
  7453. defined by Org-mode).
  7454. M-up Move entry/item up
  7455. M-down Move entry/item down
  7456. M-left Promote
  7457. M-right Demote
  7458. M-S-up Move entry/item up
  7459. M-S-down Move entry/item down
  7460. M-S-left Promote subtree
  7461. M-S-right Demote subtree
  7462. M-q Fill paragraph and items like in Org-mode
  7463. C-c ^ Sort entries
  7464. C-c - Cycle list bullet
  7465. TAB Cycle item visibility
  7466. M-RET Insert new heading/item
  7467. S-M-RET Insert new TODO heading / Checkbox item
  7468. C-c C-c Set tags / toggle checkbox"
  7469. nil " OrgStruct" nil
  7470. (org-load-modules-maybe)
  7471. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  7472. ;;;###autoload
  7473. (defun turn-on-orgstruct ()
  7474. "Unconditionally turn on `orgstruct-mode'."
  7475. (orgstruct-mode 1))
  7476. (defun orgstruct++-mode (&optional arg)
  7477. "Toggle `orgstruct-mode', the enhanced version of it.
  7478. In addition to setting orgstruct-mode, this also exports all indentation
  7479. and autofilling variables from org-mode into the buffer. It will also
  7480. recognize item context in multiline items.
  7481. Note that turning off orgstruct-mode will *not* remove the
  7482. indentation/paragraph settings. This can only be done by refreshing the
  7483. major mode, for example with \\[normal-mode]."
  7484. (interactive "P")
  7485. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  7486. (if (< arg 1)
  7487. (orgstruct-mode -1)
  7488. (orgstruct-mode 1)
  7489. (let (var val)
  7490. (mapc
  7491. (lambda (x)
  7492. (when (string-match
  7493. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  7494. (symbol-name (car x)))
  7495. (setq var (car x) val (nth 1 x))
  7496. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  7497. org-local-vars)
  7498. (org-set-local 'orgstruct-is-++ t))))
  7499. (defvar orgstruct-is-++ nil
  7500. "Is `orgstruct-mode' in ++ version in the current-buffer?")
  7501. (make-variable-buffer-local 'orgstruct-is-++)
  7502. ;;;###autoload
  7503. (defun turn-on-orgstruct++ ()
  7504. "Unconditionally turn on `orgstruct++-mode'."
  7505. (orgstruct++-mode 1))
  7506. (defun orgstruct-error ()
  7507. "Error when there is no default binding for a structure key."
  7508. (interactive)
  7509. (error "This key has no function outside structure elements"))
  7510. (defun orgstruct-setup ()
  7511. "Setup orgstruct keymaps."
  7512. (let ((nfunc 0)
  7513. (bindings
  7514. (list
  7515. '([(meta up)] org-metaup)
  7516. '([(meta down)] org-metadown)
  7517. '([(meta left)] org-metaleft)
  7518. '([(meta right)] org-metaright)
  7519. '([(meta shift up)] org-shiftmetaup)
  7520. '([(meta shift down)] org-shiftmetadown)
  7521. '([(meta shift left)] org-shiftmetaleft)
  7522. '([(meta shift right)] org-shiftmetaright)
  7523. '([?\e (up)] org-metaup)
  7524. '([?\e (down)] org-metadown)
  7525. '([?\e (left)] org-metaleft)
  7526. '([?\e (right)] org-metaright)
  7527. '([?\e (shift up)] org-shiftmetaup)
  7528. '([?\e (shift down)] org-shiftmetadown)
  7529. '([?\e (shift left)] org-shiftmetaleft)
  7530. '([?\e (shift right)] org-shiftmetaright)
  7531. '([(shift up)] org-shiftup)
  7532. '([(shift down)] org-shiftdown)
  7533. '([(shift left)] org-shiftleft)
  7534. '([(shift right)] org-shiftright)
  7535. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  7536. '("\M-q" fill-paragraph)
  7537. '("\C-c^" org-sort)
  7538. '("\C-c-" org-cycle-list-bullet)))
  7539. elt key fun cmd)
  7540. (while (setq elt (pop bindings))
  7541. (setq nfunc (1+ nfunc))
  7542. (setq key (org-key (car elt))
  7543. fun (nth 1 elt)
  7544. cmd (orgstruct-make-binding fun nfunc key))
  7545. (org-defkey orgstruct-mode-map key cmd))
  7546. ;; Special treatment needed for TAB and RET
  7547. (org-defkey orgstruct-mode-map [(tab)]
  7548. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  7549. (org-defkey orgstruct-mode-map "\C-i"
  7550. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  7551. (org-defkey orgstruct-mode-map "\M-\C-m"
  7552. (orgstruct-make-binding 'org-insert-heading 105
  7553. "\M-\C-m" [(meta return)]))
  7554. (org-defkey orgstruct-mode-map [(meta return)]
  7555. (orgstruct-make-binding 'org-insert-heading 106
  7556. [(meta return)] "\M-\C-m"))
  7557. (org-defkey orgstruct-mode-map [(shift meta return)]
  7558. (orgstruct-make-binding 'org-insert-todo-heading 107
  7559. [(meta return)] "\M-\C-m"))
  7560. (org-defkey orgstruct-mode-map "\e\C-m"
  7561. (orgstruct-make-binding 'org-insert-heading 108
  7562. "\e\C-m" [?\e (return)]))
  7563. (org-defkey orgstruct-mode-map [?\e (return)]
  7564. (orgstruct-make-binding 'org-insert-heading 109
  7565. [?\e (return)] "\e\C-m"))
  7566. (org-defkey orgstruct-mode-map [?\e (shift return)]
  7567. (orgstruct-make-binding 'org-insert-todo-heading 110
  7568. [?\e (return)] "\e\C-m"))
  7569. (unless org-local-vars
  7570. (setq org-local-vars (org-get-local-variables)))
  7571. t))
  7572. (defun orgstruct-make-binding (fun n &rest keys)
  7573. "Create a function for binding in the structure minor mode.
  7574. FUN is the command to call inside a table. N is used to create a unique
  7575. command name. KEYS are keys that should be checked in for a command
  7576. to execute outside of tables."
  7577. (eval
  7578. (list 'defun
  7579. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  7580. '(arg)
  7581. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  7582. "Outside of structure, run the binding of `"
  7583. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  7584. "'.")
  7585. '(interactive "p")
  7586. (list 'if
  7587. `(org-context-p 'headline 'item
  7588. (and orgstruct-is-++
  7589. ,(and (memq fun '(org-insert-heading org-insert-todo-heading)) t)
  7590. 'item-body))
  7591. (list 'org-run-like-in-org-mode (list 'quote fun))
  7592. (list 'let '(orgstruct-mode)
  7593. (list 'call-interactively
  7594. (append '(or)
  7595. (mapcar (lambda (k)
  7596. (list 'key-binding k))
  7597. keys)
  7598. '('orgstruct-error))))))))
  7599. (defun org-context-p (&rest contexts)
  7600. "Check if local context is any of CONTEXTS.
  7601. Possible values in the list of contexts are `table', `headline', and `item'."
  7602. (let ((pos (point)))
  7603. (goto-char (point-at-bol))
  7604. (prog1 (or (and (memq 'table contexts)
  7605. (looking-at "[ \t]*|"))
  7606. (and (memq 'headline contexts)
  7607. (looking-at org-outline-regexp))
  7608. (and (memq 'item contexts)
  7609. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  7610. (and (memq 'item-body contexts)
  7611. (org-in-item-p)))
  7612. (goto-char pos))))
  7613. (defun org-get-local-variables ()
  7614. "Return a list of all local variables in an org-mode buffer."
  7615. (let (varlist)
  7616. (with-current-buffer (get-buffer-create "*Org tmp*")
  7617. (erase-buffer)
  7618. (org-mode)
  7619. (setq varlist (buffer-local-variables)))
  7620. (kill-buffer "*Org tmp*")
  7621. (delq nil
  7622. (mapcar
  7623. (lambda (x)
  7624. (setq x
  7625. (if (symbolp x)
  7626. (list x)
  7627. (list (car x) (list 'quote (cdr x)))))
  7628. (if (string-match
  7629. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  7630. (symbol-name (car x)))
  7631. x nil))
  7632. varlist))))
  7633. (defun org-clone-local-variables (from-buffer &optional regexp)
  7634. "Clone local variables from FROM-BUFFER.
  7635. Optional argument REGEXP selects variables to clone."
  7636. (mapc
  7637. (lambda (pair)
  7638. (and (symbolp (car pair))
  7639. (or (null regexp)
  7640. (string-match regexp (symbol-name (car pair))))
  7641. (set (make-local-variable (car pair))
  7642. (cdr pair))))
  7643. (buffer-local-variables from-buffer)))
  7644. ;;;###autoload
  7645. (defun org-run-like-in-org-mode (cmd)
  7646. "Run a command, pretending that the current buffer is in Org-mode.
  7647. This will temporarily bind local variables that are typically bound in
  7648. Org-mode to the values they have in Org-mode, and then interactively
  7649. call CMD."
  7650. (org-load-modules-maybe)
  7651. (unless org-local-vars
  7652. (setq org-local-vars (org-get-local-variables)))
  7653. (eval (list 'let org-local-vars
  7654. (list 'call-interactively (list 'quote cmd)))))
  7655. ;;;; Archiving
  7656. (defun org-get-category (&optional pos force-refresh)
  7657. "Get the category applying to position POS."
  7658. (if force-refresh (org-refresh-category-properties))
  7659. (let ((pos (or pos (point))))
  7660. (or (get-text-property pos 'org-category)
  7661. (progn (org-refresh-category-properties)
  7662. (get-text-property pos 'org-category)))))
  7663. (defun org-refresh-category-properties ()
  7664. "Refresh category text properties in the buffer."
  7665. (let ((def-cat (cond
  7666. ((null org-category)
  7667. (if buffer-file-name
  7668. (file-name-sans-extension
  7669. (file-name-nondirectory buffer-file-name))
  7670. "???"))
  7671. ((symbolp org-category) (symbol-name org-category))
  7672. (t org-category)))
  7673. beg end cat pos optionp)
  7674. (org-unmodified
  7675. (save-excursion
  7676. (save-restriction
  7677. (widen)
  7678. (goto-char (point-min))
  7679. (put-text-property (point) (point-max) 'org-category def-cat)
  7680. (while (re-search-forward
  7681. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  7682. (setq pos (match-end 0)
  7683. optionp (equal (char-after (match-beginning 0)) ?#)
  7684. cat (org-trim (match-string 2)))
  7685. (if optionp
  7686. (setq beg (point-at-bol) end (point-max))
  7687. (org-back-to-heading t)
  7688. (setq beg (point) end (org-end-of-subtree t t)))
  7689. (put-text-property beg end 'org-category cat)
  7690. (put-text-property beg end 'org-category-position beg)
  7691. (goto-char pos)))))))
  7692. ;;;; Link Stuff
  7693. ;;; Link abbreviations
  7694. (defun org-link-expand-abbrev (link)
  7695. "Apply replacements as defined in `org-link-abbrev-alist."
  7696. (if (string-match "^\\([^:]*\\)\\(::?\\(.*\\)\\)?$" link)
  7697. (let* ((key (match-string 1 link))
  7698. (as (or (assoc key org-link-abbrev-alist-local)
  7699. (assoc key org-link-abbrev-alist)))
  7700. (tag (and (match-end 2) (match-string 3 link)))
  7701. rpl)
  7702. (if (not as)
  7703. link
  7704. (setq rpl (cdr as))
  7705. (cond
  7706. ((symbolp rpl) (funcall rpl tag))
  7707. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  7708. ((string-match "%h" rpl)
  7709. (replace-match (url-hexify-string (or tag "")) t t rpl))
  7710. (t (concat rpl tag)))))
  7711. link))
  7712. ;;; Storing and inserting links
  7713. (defvar org-insert-link-history nil
  7714. "Minibuffer history for links inserted with `org-insert-link'.")
  7715. (defvar org-stored-links nil
  7716. "Contains the links stored with `org-store-link'.")
  7717. (defvar org-store-link-plist nil
  7718. "Plist with info about the most recently link created with `org-store-link'.")
  7719. (defvar org-link-protocols nil
  7720. "Link protocols added to Org-mode using `org-add-link-type'.")
  7721. (defvar org-store-link-functions nil
  7722. "List of functions that are called to create and store a link.
  7723. Each function will be called in turn until one returns a non-nil
  7724. value. Each function should check if it is responsible for creating
  7725. this link (for example by looking at the major mode).
  7726. If not, it must exit and return nil.
  7727. If yes, it should return a non-nil value after a calling
  7728. `org-store-link-props' with a list of properties and values.
  7729. Special properties are:
  7730. :type The link prefix, like \"http\". This must be given.
  7731. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  7732. This is obligatory as well.
  7733. :description Optional default description for the second pair
  7734. of brackets in an Org-mode link. The user can still change
  7735. this when inserting this link into an Org-mode buffer.
  7736. In addition to these, any additional properties can be specified
  7737. and then used in remember templates.")
  7738. (defun org-add-link-type (type &optional follow export)
  7739. "Add TYPE to the list of `org-link-types'.
  7740. Re-compute all regular expressions depending on `org-link-types'
  7741. FOLLOW and EXPORT are two functions.
  7742. FOLLOW should take the link path as the single argument and do whatever
  7743. is necessary to follow the link, for example find a file or display
  7744. a mail message.
  7745. EXPORT should format the link path for export to one of the export formats.
  7746. It should be a function accepting three arguments:
  7747. path the path of the link, the text after the prefix (like \"http:\")
  7748. desc the description of the link, if any, or a description added by
  7749. org-export-normalize-links if there is none
  7750. format the export format, a symbol like `html' or `latex' or `ascii'..
  7751. The function may use the FORMAT information to return different values
  7752. depending on the format. The return value will be put literally into
  7753. the exported file. If the return value is nil, this means Org should
  7754. do what it normally does with links which do not have EXPORT defined.
  7755. Org-mode has a built-in default for exporting links. If you are happy with
  7756. this default, there is no need to define an export function for the link
  7757. type. For a simple example of an export function, see `org-bbdb.el'."
  7758. (add-to-list 'org-link-types type t)
  7759. (org-make-link-regexps)
  7760. (if (assoc type org-link-protocols)
  7761. (setcdr (assoc type org-link-protocols) (list follow export))
  7762. (push (list type follow export) org-link-protocols)))
  7763. (defvar org-agenda-buffer-name)
  7764. ;;;###autoload
  7765. (defun org-store-link (arg)
  7766. "\\<org-mode-map>Store an org-link to the current location.
  7767. This link is added to `org-stored-links' and can later be inserted
  7768. into an org-buffer with \\[org-insert-link].
  7769. For some link types, a prefix arg is interpreted:
  7770. For links to usenet articles, arg negates `org-gnus-prefer-web-links'.
  7771. For file links, arg negates `org-context-in-file-links'."
  7772. (interactive "P")
  7773. (org-load-modules-maybe)
  7774. (setq org-store-link-plist nil) ; reset
  7775. (org-with-limited-levels
  7776. (let (link cpltxt desc description search txt custom-id agenda-link)
  7777. (cond
  7778. ((run-hook-with-args-until-success 'org-store-link-functions)
  7779. (setq link (plist-get org-store-link-plist :link)
  7780. desc (or (plist-get org-store-link-plist :description) link)))
  7781. ((equal (buffer-name) "*Org Edit Src Example*")
  7782. (let (label gc)
  7783. (while (or (not label)
  7784. (save-excursion
  7785. (save-restriction
  7786. (widen)
  7787. (goto-char (point-min))
  7788. (re-search-forward
  7789. (regexp-quote (format org-coderef-label-format label))
  7790. nil t))))
  7791. (when label (message "Label exists already") (sit-for 2))
  7792. (setq label (read-string "Code line label: " label)))
  7793. (end-of-line 1)
  7794. (setq link (format org-coderef-label-format label))
  7795. (setq gc (- 79 (length link)))
  7796. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  7797. (insert link)
  7798. (setq link (concat "(" label ")") desc nil)))
  7799. ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
  7800. ;; We are in the agenda, link to referenced location
  7801. (let ((m (or (get-text-property (point) 'org-hd-marker)
  7802. (get-text-property (point) 'org-marker))))
  7803. (when m
  7804. (org-with-point-at m
  7805. (setq agenda-link
  7806. (if (org-called-interactively-p 'any)
  7807. (call-interactively 'org-store-link)
  7808. (org-store-link nil)))))))
  7809. ((eq major-mode 'calendar-mode)
  7810. (let ((cd (calendar-cursor-to-date)))
  7811. (setq link
  7812. (format-time-string
  7813. (car org-time-stamp-formats)
  7814. (apply 'encode-time
  7815. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  7816. nil nil nil))))
  7817. (org-store-link-props :type "calendar" :date cd)))
  7818. ((eq major-mode 'w3-mode)
  7819. (setq cpltxt (if (and (buffer-name)
  7820. (not (string-match "Untitled" (buffer-name))))
  7821. (buffer-name)
  7822. (url-view-url t))
  7823. link (org-make-link (url-view-url t)))
  7824. (org-store-link-props :type "w3" :url (url-view-url t)))
  7825. ((eq major-mode 'w3m-mode)
  7826. (setq cpltxt (or w3m-current-title w3m-current-url)
  7827. link (org-make-link w3m-current-url))
  7828. (org-store-link-props :type "w3m" :url (url-view-url t)))
  7829. ((setq search (run-hook-with-args-until-success
  7830. 'org-create-file-search-functions))
  7831. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  7832. "::" search))
  7833. (setq cpltxt (or description link)))
  7834. ((eq major-mode 'image-mode)
  7835. (setq cpltxt (concat "file:"
  7836. (abbreviate-file-name buffer-file-name))
  7837. link (org-make-link cpltxt))
  7838. (org-store-link-props :type "image" :file buffer-file-name))
  7839. ((eq major-mode 'dired-mode)
  7840. ;; link to the file in the current line
  7841. (let ((file (dired-get-filename nil t)))
  7842. (setq file (if file
  7843. (abbreviate-file-name
  7844. (expand-file-name (dired-get-filename nil t)))
  7845. ;; otherwise, no file so use current directory.
  7846. default-directory))
  7847. (setq cpltxt (concat "file:" file)
  7848. link (org-make-link cpltxt))))
  7849. ((and (buffer-file-name (buffer-base-buffer)) (eq major-mode 'org-mode))
  7850. (setq custom-id (org-entry-get nil "CUSTOM_ID"))
  7851. (cond
  7852. ((org-in-regexp "<<\\(.*?\\)>>")
  7853. (setq cpltxt
  7854. (concat "file:"
  7855. (abbreviate-file-name
  7856. (buffer-file-name (buffer-base-buffer)))
  7857. "::" (match-string 1))
  7858. link (org-make-link cpltxt)))
  7859. ((and (featurep 'org-id)
  7860. (or (eq org-link-to-org-use-id t)
  7861. (and (eq org-link-to-org-use-id 'create-if-interactive)
  7862. (org-called-interactively-p 'any))
  7863. (and (eq org-link-to-org-use-id
  7864. 'create-if-interactive-and-no-custom-id)
  7865. (org-called-interactively-p 'any)
  7866. (not custom-id))
  7867. (and org-link-to-org-use-id
  7868. (org-entry-get nil "ID"))))
  7869. ;; We can make a link using the ID.
  7870. (setq link (condition-case nil
  7871. (prog1 (org-id-store-link)
  7872. (setq desc (plist-get org-store-link-plist
  7873. :description)))
  7874. (error
  7875. ;; probably before first headline, link to file only
  7876. (concat "file:"
  7877. (abbreviate-file-name
  7878. (buffer-file-name (buffer-base-buffer))))))))
  7879. (t
  7880. ;; Just link to current headline
  7881. (setq cpltxt (concat "file:"
  7882. (abbreviate-file-name
  7883. (buffer-file-name (buffer-base-buffer)))))
  7884. ;; Add a context search string
  7885. (when (org-xor org-context-in-file-links arg)
  7886. (setq txt (cond
  7887. ((org-at-heading-p) nil)
  7888. ((org-region-active-p)
  7889. (buffer-substring (region-beginning) (region-end)))
  7890. (t nil)))
  7891. (when (or (null txt) (string-match "\\S-" txt))
  7892. (setq cpltxt
  7893. (concat cpltxt "::"
  7894. (condition-case nil
  7895. (org-make-org-heading-search-string txt)
  7896. (error "")))
  7897. desc (or (nth 4 (ignore-errors
  7898. (org-heading-components))) "NONE"))))
  7899. (if (string-match "::\\'" cpltxt)
  7900. (setq cpltxt (substring cpltxt 0 -2)))
  7901. (setq link (org-make-link cpltxt)))))
  7902. ((buffer-file-name (buffer-base-buffer))
  7903. ;; Just link to this file here.
  7904. (setq cpltxt (concat "file:"
  7905. (abbreviate-file-name
  7906. (buffer-file-name (buffer-base-buffer)))))
  7907. ;; Add a context string
  7908. (when (org-xor org-context-in-file-links arg)
  7909. (setq txt (if (org-region-active-p)
  7910. (buffer-substring (region-beginning) (region-end))
  7911. (buffer-substring (point-at-bol) (point-at-eol))))
  7912. ;; Only use search option if there is some text.
  7913. (when (string-match "\\S-" txt)
  7914. (setq cpltxt
  7915. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  7916. desc "NONE")))
  7917. (setq link (org-make-link cpltxt)))
  7918. ((org-called-interactively-p 'interactive)
  7919. (error "Cannot link to a buffer which is not visiting a file"))
  7920. (t (setq link nil)))
  7921. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  7922. (setq link (or link cpltxt)
  7923. desc (or desc cpltxt))
  7924. (if (equal desc "NONE") (setq desc nil))
  7925. (if (and (or (org-called-interactively-p 'any) executing-kbd-macro) link)
  7926. (progn
  7927. (setq org-stored-links
  7928. (cons (list link desc) org-stored-links))
  7929. (message "Stored: %s" (or desc link))
  7930. (when custom-id
  7931. (setq link (concat "file:" (abbreviate-file-name (buffer-file-name))
  7932. "::#" custom-id))
  7933. (setq org-stored-links
  7934. (cons (list link desc) org-stored-links))))
  7935. (or agenda-link (and link (org-make-link-string link desc)))))))
  7936. (defun org-store-link-props (&rest plist)
  7937. "Store link properties, extract names and addresses."
  7938. (let (x adr)
  7939. (when (setq x (plist-get plist :from))
  7940. (setq adr (mail-extract-address-components x))
  7941. (setq plist (plist-put plist :fromname (car adr)))
  7942. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  7943. (when (setq x (plist-get plist :to))
  7944. (setq adr (mail-extract-address-components x))
  7945. (setq plist (plist-put plist :toname (car adr)))
  7946. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  7947. (let ((from (plist-get plist :from))
  7948. (to (plist-get plist :to)))
  7949. (when (and from to org-from-is-user-regexp)
  7950. (setq plist
  7951. (plist-put plist :fromto
  7952. (if (string-match org-from-is-user-regexp from)
  7953. (concat "to %t")
  7954. (concat "from %f"))))))
  7955. (setq org-store-link-plist plist))
  7956. (defun org-add-link-props (&rest plist)
  7957. "Add these properties to the link property list."
  7958. (let (key value)
  7959. (while plist
  7960. (setq key (pop plist) value (pop plist))
  7961. (setq org-store-link-plist
  7962. (plist-put org-store-link-plist key value)))))
  7963. (defun org-email-link-description (&optional fmt)
  7964. "Return the description part of an email link.
  7965. This takes information from `org-store-link-plist' and formats it
  7966. according to FMT (default from `org-email-link-description-format')."
  7967. (setq fmt (or fmt org-email-link-description-format))
  7968. (let* ((p org-store-link-plist)
  7969. (to (plist-get p :toaddress))
  7970. (from (plist-get p :fromaddress))
  7971. (table
  7972. (list
  7973. (cons "%c" (plist-get p :fromto))
  7974. (cons "%F" (plist-get p :from))
  7975. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  7976. (cons "%T" (plist-get p :to))
  7977. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  7978. (cons "%s" (plist-get p :subject))
  7979. (cons "%d" (plist-get p :date))
  7980. (cons "%m" (plist-get p :message-id)))))
  7981. (when (string-match "%c" fmt)
  7982. ;; Check if the user wrote this message
  7983. (if (and org-from-is-user-regexp from to
  7984. (save-match-data (string-match org-from-is-user-regexp from)))
  7985. (setq fmt (replace-match "to %t" t t fmt))
  7986. (setq fmt (replace-match "from %f" t t fmt))))
  7987. (org-replace-escapes fmt table)))
  7988. (defun org-make-org-heading-search-string (&optional string heading)
  7989. "Make search string for STRING or current headline."
  7990. (interactive)
  7991. (let ((s (or string (org-get-heading)))
  7992. (lines org-context-in-file-links))
  7993. (unless (and string (not heading))
  7994. ;; We are using a headline, clean up garbage in there.
  7995. (if (string-match org-todo-regexp s)
  7996. (setq s (replace-match "" t t s)))
  7997. (if (string-match (org-re ":[[:alnum:]_@#%:]+:[ \t]*$") s)
  7998. (setq s (replace-match "" t t s)))
  7999. (setq s (org-trim s))
  8000. (if (string-match (concat "^\\(" org-quote-string "\\|"
  8001. org-comment-string "\\)") s)
  8002. (setq s (replace-match "" t t s)))
  8003. (while (string-match org-ts-regexp s)
  8004. (setq s (replace-match "" t t s))))
  8005. (or string (setq s (concat "*" s))) ; Add * for headlines
  8006. (when (and string (integerp lines) (> lines 0))
  8007. (let ((slines (org-split-string s "\n")))
  8008. (when (< lines (length slines))
  8009. (setq s (mapconcat
  8010. 'identity
  8011. (reverse (nthcdr (- (length slines) lines)
  8012. (reverse slines))) "\n")))))
  8013. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  8014. (defun org-make-link (&rest strings)
  8015. "Concatenate STRINGS."
  8016. (apply 'concat strings))
  8017. (defun org-make-link-string (link &optional description)
  8018. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  8019. (unless (string-match "\\S-" link)
  8020. (error "Empty link"))
  8021. (when (and description
  8022. (stringp description)
  8023. (not (string-match "\\S-" description)))
  8024. (setq description nil))
  8025. (when (stringp description)
  8026. ;; Remove brackets from the description, they are fatal.
  8027. (while (string-match "\\[" description)
  8028. (setq description (replace-match "{" t t description)))
  8029. (while (string-match "\\]" description)
  8030. (setq description (replace-match "}" t t description))))
  8031. (when (equal link description)
  8032. ;; No description needed, it is identical
  8033. (setq description nil))
  8034. (when (and (not description)
  8035. (not (string-match (org-image-file-name-regexp) link))
  8036. (not (equal link (org-link-escape link))))
  8037. (setq description (org-extract-attributes link)))
  8038. (setq link
  8039. (cond ((string-match (org-image-file-name-regexp) link) link)
  8040. ((string-match org-link-types-re link)
  8041. (concat (match-string 1 link)
  8042. (org-link-escape (substring link (match-end 1)))))
  8043. (t (org-link-escape link))))
  8044. (concat "[[" link "]"
  8045. (if description (concat "[" description "]") "")
  8046. "]"))
  8047. (defconst org-link-escape-chars
  8048. '(?\ ?\[ ?\] ?\; ?\= ?\+)
  8049. "List of characters that should be escaped in link.
  8050. This is the list that is used for internal purposes.")
  8051. (defvar org-url-encoding-use-url-hexify nil)
  8052. (defconst org-link-escape-chars-browser
  8053. '(?\ )
  8054. "List of escapes for characters that are problematic in links.
  8055. This is the list that is used before handing over to the browser.")
  8056. (defun org-link-escape (text &optional table merge)
  8057. "Return percent escaped representation of TEXT.
  8058. TEXT is a string with the text to escape.
  8059. Optional argument TABLE is a list with characters that should be
  8060. escaped. When nil, `org-link-escape-chars' is used.
  8061. If optional argument MERGE is set, merge TABLE into
  8062. `org-link-escape-chars'."
  8063. (if (and org-url-encoding-use-url-hexify (not table))
  8064. (url-hexify-string text)
  8065. (cond
  8066. ((and table merge)
  8067. (mapc (lambda (defchr)
  8068. (unless (member defchr table)
  8069. (setq table (cons defchr table)))) org-link-escape-chars))
  8070. ((null table)
  8071. (setq table org-link-escape-chars)))
  8072. (mapconcat
  8073. (lambda (char)
  8074. (if (or (member char table)
  8075. (< char 32) (= char 37) (> char 126))
  8076. (mapconcat (lambda (sequence-element)
  8077. (format "%%%.2X" sequence-element))
  8078. (or (encode-coding-char char 'utf-8)
  8079. (error "Unable to percent escape character: %s"
  8080. (char-to-string char))) "")
  8081. (char-to-string char))) text "")))
  8082. (defun org-link-unescape (str)
  8083. "Unhex hexified Unicode strings as returned from the JavaScript function
  8084. encodeURIComponent. E.g. `%C3%B6' is the german Umlaut `ö'."
  8085. (unless (and (null str) (string= "" str))
  8086. (let ((pos 0) (case-fold-search t) unhexed)
  8087. (while (setq pos (string-match "\\(%[0-9a-f][0-9a-f]\\)+" str pos))
  8088. (setq unhexed (org-link-unescape-compound (match-string 0 str)))
  8089. (setq str (replace-match unhexed t t str))
  8090. (setq pos (+ pos (length unhexed))))))
  8091. str)
  8092. (defun org-link-unescape-compound (hex)
  8093. "Unhexify Unicode hex-chars. E.g. `%C3%B6' is the German Umlaut `ö'.
  8094. Note: this function also decodes single byte encodings like
  8095. `%E1' (\"á\") if not followed by another `%[A-F0-9]{2}' group."
  8096. (save-match-data
  8097. (let* ((bytes (cdr (split-string hex "%")))
  8098. (ret "")
  8099. (eat 0)
  8100. (sum 0))
  8101. (while bytes
  8102. (let* ((val (string-to-number (pop bytes) 16))
  8103. (shift-xor
  8104. (if (= 0 eat)
  8105. (cond
  8106. ((>= val 252) (cons 6 252))
  8107. ((>= val 248) (cons 5 248))
  8108. ((>= val 240) (cons 4 240))
  8109. ((>= val 224) (cons 3 224))
  8110. ((>= val 192) (cons 2 192))
  8111. (t (cons 0 0)))
  8112. (cons 6 128))))
  8113. (if (>= val 192) (setq eat (car shift-xor)))
  8114. (setq val (logxor val (cdr shift-xor)))
  8115. (setq sum (+ (lsh sum (car shift-xor)) val))
  8116. (if (> eat 0) (setq eat (- eat 1)))
  8117. (cond
  8118. ((= 0 eat) ;multi byte
  8119. (setq ret (concat ret (org-char-to-string sum)))
  8120. (setq sum 0))
  8121. ((not bytes) ; single byte(s)
  8122. (setq ret (org-link-unescape-single-byte-sequence hex))))
  8123. )) ;; end (while bytes
  8124. ret )))
  8125. (defun org-link-unescape-single-byte-sequence (hex)
  8126. "Unhexify hex-encoded single byte character sequences."
  8127. (mapconcat (lambda (byte)
  8128. (char-to-string (string-to-number byte 16)))
  8129. (cdr (split-string hex "%")) ""))
  8130. (defun org-xor (a b)
  8131. "Exclusive or."
  8132. (if a (not b) b))
  8133. (defun org-fixup-message-id-for-http (s)
  8134. "Replace special characters in a message id, so it can be used in an http query."
  8135. (when (string-match "%" s)
  8136. (setq s (mapconcat (lambda (c)
  8137. (if (eq c ?%)
  8138. "%25"
  8139. (char-to-string c)))
  8140. s "")))
  8141. (while (string-match "<" s)
  8142. (setq s (replace-match "%3C" t t s)))
  8143. (while (string-match ">" s)
  8144. (setq s (replace-match "%3E" t t s)))
  8145. (while (string-match "@" s)
  8146. (setq s (replace-match "%40" t t s)))
  8147. s)
  8148. ;;;###autoload
  8149. (defun org-insert-link-global ()
  8150. "Insert a link like Org-mode does.
  8151. This command can be called in any mode to insert a link in Org-mode syntax."
  8152. (interactive)
  8153. (org-load-modules-maybe)
  8154. (org-run-like-in-org-mode 'org-insert-link))
  8155. (defun org-insert-link (&optional complete-file link-location default-description)
  8156. "Insert a link. At the prompt, enter the link.
  8157. Completion can be used to insert any of the link protocol prefixes like
  8158. http or ftp in use.
  8159. The history can be used to select a link previously stored with
  8160. `org-store-link'. When the empty string is entered (i.e. if you just
  8161. press RET at the prompt), the link defaults to the most recently
  8162. stored link. As SPC triggers completion in the minibuffer, you need to
  8163. use M-SPC or C-q SPC to force the insertion of a space character.
  8164. You will also be prompted for a description, and if one is given, it will
  8165. be displayed in the buffer instead of the link.
  8166. If there is already a link at point, this command will allow you to edit link
  8167. and description parts.
  8168. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  8169. be selected using completion. The path to the file will be relative to the
  8170. current directory if the file is in the current directory or a subdirectory.
  8171. Otherwise, the link will be the absolute path as completed in the minibuffer
  8172. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  8173. option `org-link-file-path-type'.
  8174. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  8175. the current directory or below.
  8176. With three \\[universal-argument] prefixes, negate the meaning of
  8177. `org-keep-stored-link-after-insertion'.
  8178. If `org-make-link-description-function' is non-nil, this function will be
  8179. called with the link target, and the result will be the default
  8180. link description.
  8181. If the LINK-LOCATION parameter is non-nil, this value will be
  8182. used as the link location instead of reading one interactively.
  8183. If the DEFAULT-DESCRIPTION parameter is non-nil, this value will
  8184. be used as the default description."
  8185. (interactive "P")
  8186. (let* ((wcf (current-window-configuration))
  8187. (region (if (org-region-active-p)
  8188. (buffer-substring (region-beginning) (region-end))))
  8189. (remove (and region (list (region-beginning) (region-end))))
  8190. (desc region)
  8191. tmphist ; byte-compile incorrectly complains about this
  8192. (link link-location)
  8193. entry file all-prefixes)
  8194. (cond
  8195. (link-location) ; specified by arg, just use it.
  8196. ((org-in-regexp org-bracket-link-regexp 1)
  8197. ;; We do have a link at point, and we are going to edit it.
  8198. (setq remove (list (match-beginning 0) (match-end 0)))
  8199. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  8200. (setq link (read-string "Link: "
  8201. (org-link-unescape
  8202. (org-match-string-no-properties 1)))))
  8203. ((or (org-in-regexp org-angle-link-re)
  8204. (org-in-regexp org-plain-link-re))
  8205. ;; Convert to bracket link
  8206. (setq remove (list (match-beginning 0) (match-end 0))
  8207. link (read-string "Link: "
  8208. (org-remove-angle-brackets (match-string 0)))))
  8209. ((member complete-file '((4) (16)))
  8210. ;; Completing read for file names.
  8211. (setq link (org-file-complete-link complete-file)))
  8212. (t
  8213. ;; Read link, with completion for stored links.
  8214. (with-output-to-temp-buffer "*Org Links*"
  8215. (princ "Insert a link.
  8216. Use TAB to complete link prefixes, then RET for type-specific completion support\n")
  8217. (when org-stored-links
  8218. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  8219. (princ (mapconcat
  8220. (lambda (x)
  8221. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  8222. (reverse org-stored-links) "\n"))))
  8223. (let ((cw (selected-window)))
  8224. (select-window (get-buffer-window "*Org Links*" 'visible))
  8225. (with-current-buffer "*Org Links*" (setq truncate-lines t))
  8226. (unless (pos-visible-in-window-p (point-max))
  8227. (org-fit-window-to-buffer))
  8228. (and (window-live-p cw) (select-window cw)))
  8229. ;; Fake a link history, containing the stored links.
  8230. (setq tmphist (append (mapcar 'car org-stored-links)
  8231. org-insert-link-history))
  8232. (setq all-prefixes (append (mapcar 'car org-link-abbrev-alist-local)
  8233. (mapcar 'car org-link-abbrev-alist)
  8234. org-link-types))
  8235. (unwind-protect
  8236. (progn
  8237. (setq link
  8238. (let ((org-completion-use-ido nil)
  8239. (org-completion-use-iswitchb nil))
  8240. (org-completing-read
  8241. "Link: "
  8242. (append
  8243. (mapcar (lambda (x) (list (concat x ":")))
  8244. all-prefixes)
  8245. (mapcar 'car org-stored-links))
  8246. nil nil nil
  8247. 'tmphist
  8248. (car (car org-stored-links)))))
  8249. (if (not (string-match "\\S-" link))
  8250. (error "No link selected"))
  8251. (if (or (member link all-prefixes)
  8252. (and (equal ":" (substring link -1))
  8253. (member (substring link 0 -1) all-prefixes)
  8254. (setq link (substring link 0 -1))))
  8255. (setq link (org-link-try-special-completion link))))
  8256. (set-window-configuration wcf)
  8257. (kill-buffer "*Org Links*"))
  8258. (setq entry (assoc link org-stored-links))
  8259. (or entry (push link org-insert-link-history))
  8260. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  8261. (not org-keep-stored-link-after-insertion))
  8262. (setq org-stored-links (delq (assoc link org-stored-links)
  8263. org-stored-links)))
  8264. (setq desc (or desc (nth 1 entry)))))
  8265. (if (string-match org-plain-link-re link)
  8266. ;; URL-like link, normalize the use of angular brackets.
  8267. (setq link (org-make-link (org-remove-angle-brackets link))))
  8268. ;; Check if we are linking to the current file with a search option
  8269. ;; If yes, simplify the link by using only the search option.
  8270. (when (and buffer-file-name
  8271. (string-match "^file:\\(.+?\\)::\\([^>]+\\)" link))
  8272. (let* ((path (match-string 1 link))
  8273. (case-fold-search nil)
  8274. (search (match-string 2 link)))
  8275. (save-match-data
  8276. (if (equal (file-truename buffer-file-name) (file-truename path))
  8277. ;; We are linking to this same file, with a search option
  8278. (setq link search)))))
  8279. ;; Check if we can/should use a relative path. If yes, simplify the link
  8280. (when (string-match "^\\(file:\\|docview:\\)\\(.*\\)" link)
  8281. (let* ((type (match-string 1 link))
  8282. (path (match-string 2 link))
  8283. (origpath path)
  8284. (case-fold-search nil))
  8285. (cond
  8286. ((or (eq org-link-file-path-type 'absolute)
  8287. (equal complete-file '(16)))
  8288. (setq path (abbreviate-file-name (expand-file-name path))))
  8289. ((eq org-link-file-path-type 'noabbrev)
  8290. (setq path (expand-file-name path)))
  8291. ((eq org-link-file-path-type 'relative)
  8292. (setq path (file-relative-name path)))
  8293. (t
  8294. (save-match-data
  8295. (if (string-match (concat "^" (regexp-quote
  8296. (expand-file-name
  8297. (file-name-as-directory
  8298. default-directory))))
  8299. (expand-file-name path))
  8300. ;; We are linking a file with relative path name.
  8301. (setq path (substring (expand-file-name path)
  8302. (match-end 0)))
  8303. (setq path (abbreviate-file-name (expand-file-name path)))))))
  8304. (setq link (concat type path))
  8305. (if (equal desc origpath)
  8306. (setq desc path))))
  8307. (if org-make-link-description-function
  8308. (setq desc (funcall org-make-link-description-function link desc))
  8309. (if default-description (setq desc default-description)))
  8310. (setq desc (read-string "Description: " desc))
  8311. (unless (string-match "\\S-" desc) (setq desc nil))
  8312. (if remove (apply 'delete-region remove))
  8313. (insert (org-make-link-string link desc))))
  8314. (defun org-link-try-special-completion (type)
  8315. "If there is completion support for link type TYPE, offer it."
  8316. (let ((fun (intern (concat "org-" type "-complete-link"))))
  8317. (if (functionp fun)
  8318. (funcall fun)
  8319. (read-string "Link (no completion support): " (concat type ":")))))
  8320. (defun org-file-complete-link (&optional arg)
  8321. "Create a file link using completion."
  8322. (let (file link)
  8323. (setq file (read-file-name "File: "))
  8324. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  8325. (pwd1 (file-name-as-directory (abbreviate-file-name
  8326. (expand-file-name ".")))))
  8327. (cond
  8328. ((equal arg '(16))
  8329. (setq link (org-make-link
  8330. "file:"
  8331. (abbreviate-file-name (expand-file-name file)))))
  8332. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  8333. (setq link (org-make-link "file:" (match-string 1 file))))
  8334. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  8335. (expand-file-name file))
  8336. (setq link (org-make-link
  8337. "file:" (match-string 1 (expand-file-name file)))))
  8338. (t (setq link (org-make-link "file:" file)))))
  8339. link))
  8340. (defun org-completing-read (&rest args)
  8341. "Completing-read with SPACE being a normal character."
  8342. (let ((enable-recursive-minibuffers t)
  8343. (minibuffer-local-completion-map
  8344. (copy-keymap minibuffer-local-completion-map)))
  8345. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  8346. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  8347. (org-defkey minibuffer-local-completion-map (kbd "C-c !") 'org-time-stamp-inactive)
  8348. (apply 'org-icompleting-read args)))
  8349. (defun org-completing-read-no-i (&rest args)
  8350. (let (org-completion-use-ido org-completion-use-iswitchb)
  8351. (apply 'org-completing-read args)))
  8352. (defun org-iswitchb-completing-read (prompt choices &rest args)
  8353. "Use iswitch as a completing-read replacement to choose from choices.
  8354. PROMPT is a string to prompt with. CHOICES is a list of strings to choose
  8355. from."
  8356. (let* ((iswitchb-use-virtual-buffers nil)
  8357. (iswitchb-make-buflist-hook
  8358. (lambda ()
  8359. (setq iswitchb-temp-buflist choices))))
  8360. (iswitchb-read-buffer prompt)))
  8361. (defun org-icompleting-read (&rest args)
  8362. "Completing-read using `ido-mode' or `iswitchb' speedups if available."
  8363. (org-without-partial-completion
  8364. (if (and org-completion-use-ido
  8365. (fboundp 'ido-completing-read)
  8366. (boundp 'ido-mode) ido-mode
  8367. (listp (second args)))
  8368. (let ((ido-enter-matching-directory nil))
  8369. (apply 'ido-completing-read (concat (car args))
  8370. (if (consp (car (nth 1 args)))
  8371. (mapcar 'car (nth 1 args))
  8372. (nth 1 args))
  8373. (cddr args)))
  8374. (if (and org-completion-use-iswitchb
  8375. (boundp 'iswitchb-mode) iswitchb-mode
  8376. (listp (second args)))
  8377. (apply 'org-iswitchb-completing-read (concat (car args))
  8378. (if (consp (car (nth 1 args)))
  8379. (mapcar 'car (nth 1 args))
  8380. (nth 1 args))
  8381. (cddr args))
  8382. (apply 'completing-read args)))))
  8383. (defun org-extract-attributes (s)
  8384. "Extract the attributes cookie from a string and set as text property."
  8385. (let (a attr (start 0) key value)
  8386. (save-match-data
  8387. (when (string-match "{{\\([^}]+\\)}}$" s)
  8388. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  8389. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  8390. (setq key (match-string 1 a) value (match-string 2 a)
  8391. start (match-end 0)
  8392. attr (plist-put attr (intern key) value))))
  8393. (org-add-props s nil 'org-attr attr))
  8394. s))
  8395. (defun org-extract-attributes-from-string (tag)
  8396. (let (key value attr)
  8397. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"\\s-?" tag)
  8398. (setq key (match-string 1 tag) value (match-string 2 tag)
  8399. tag (replace-match "" t t tag)
  8400. attr (plist-put attr (intern key) value)))
  8401. (cons tag attr)))
  8402. (defun org-attributes-to-string (plist)
  8403. "Format a property list into an HTML attribute list."
  8404. (let ((s "") key value)
  8405. (while plist
  8406. (setq key (pop plist) value (pop plist))
  8407. (and value
  8408. (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
  8409. s))
  8410. ;;; Opening/following a link
  8411. (defvar org-link-search-failed nil)
  8412. (defvar org-open-link-functions nil
  8413. "Hook for functions finding a plain text link.
  8414. These functions must take a single argument, the link content.
  8415. They will be called for links that look like [[link text][description]]
  8416. when LINK TEXT does not have a protocol like \"http:\" and does not look
  8417. like a filename (e.g. \"./blue.png\").
  8418. These functions will be called *before* Org attempts to resolve the
  8419. link by doing text searches in the current buffer - so if you want a
  8420. link \"[[target]]\" to still find \"<<target>>\", your function should
  8421. handle this as a special case.
  8422. When the function does handle the link, it must return a non-nil value.
  8423. If it decides that it is not responsible for this link, it must return
  8424. nil to indicate that that Org-mode can continue with other options
  8425. like exact and fuzzy text search.")
  8426. (defun org-next-link ()
  8427. "Move forward to the next link.
  8428. If the link is in hidden text, expose it."
  8429. (interactive)
  8430. (when (and org-link-search-failed (eq this-command last-command))
  8431. (goto-char (point-min))
  8432. (message "Link search wrapped back to beginning of buffer"))
  8433. (setq org-link-search-failed nil)
  8434. (let* ((pos (point))
  8435. (ct (org-context))
  8436. (a (assoc :link ct)))
  8437. (if a (goto-char (nth 2 a)))
  8438. (if (re-search-forward org-any-link-re nil t)
  8439. (progn
  8440. (goto-char (match-beginning 0))
  8441. (if (outline-invisible-p) (org-show-context)))
  8442. (goto-char pos)
  8443. (setq org-link-search-failed t)
  8444. (error "No further link found"))))
  8445. (defun org-previous-link ()
  8446. "Move backward to the previous link.
  8447. If the link is in hidden text, expose it."
  8448. (interactive)
  8449. (when (and org-link-search-failed (eq this-command last-command))
  8450. (goto-char (point-max))
  8451. (message "Link search wrapped back to end of buffer"))
  8452. (setq org-link-search-failed nil)
  8453. (let* ((pos (point))
  8454. (ct (org-context))
  8455. (a (assoc :link ct)))
  8456. (if a (goto-char (nth 1 a)))
  8457. (if (re-search-backward org-any-link-re nil t)
  8458. (progn
  8459. (goto-char (match-beginning 0))
  8460. (if (outline-invisible-p) (org-show-context)))
  8461. (goto-char pos)
  8462. (setq org-link-search-failed t)
  8463. (error "No further link found"))))
  8464. (defun org-translate-link (s)
  8465. "Translate a link string if a translation function has been defined."
  8466. (if (and org-link-translation-function
  8467. (fboundp org-link-translation-function)
  8468. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  8469. (progn
  8470. (setq s (funcall org-link-translation-function
  8471. (match-string 1) (match-string 2)))
  8472. (concat (car s) ":" (cdr s)))
  8473. s))
  8474. (defun org-translate-link-from-planner (type path)
  8475. "Translate a link from Emacs Planner syntax so that Org can follow it.
  8476. This is still an experimental function, your mileage may vary."
  8477. (cond
  8478. ((member type '("http" "https" "news" "ftp"))
  8479. ;; standard Internet links are the same.
  8480. nil)
  8481. ((and (equal type "irc") (string-match "^//" path))
  8482. ;; Planner has two / at the beginning of an irc link, we have 1.
  8483. ;; We should have zero, actually....
  8484. (setq path (substring path 1)))
  8485. ((and (equal type "lisp") (string-match "^/" path))
  8486. ;; Planner has a slash, we do not.
  8487. (setq type "elisp" path (substring path 1)))
  8488. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  8489. ;; A typical message link. Planner has the id after the final slash,
  8490. ;; we separate it with a hash mark
  8491. (setq path (concat (match-string 1 path) "#"
  8492. (org-remove-angle-brackets (match-string 2 path)))))
  8493. )
  8494. (cons type path))
  8495. (defun org-find-file-at-mouse (ev)
  8496. "Open file link or URL at mouse."
  8497. (interactive "e")
  8498. (mouse-set-point ev)
  8499. (org-open-at-point 'in-emacs))
  8500. (defun org-open-at-mouse (ev)
  8501. "Open file link or URL at mouse.
  8502. See the docstring of `org-open-file' for details."
  8503. (interactive "e")
  8504. (mouse-set-point ev)
  8505. (if (eq major-mode 'org-agenda-mode)
  8506. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  8507. (org-open-at-point))
  8508. (defvar org-window-config-before-follow-link nil
  8509. "The window configuration before following a link.
  8510. This is saved in case the need arises to restore it.")
  8511. (defvar org-open-link-marker (make-marker)
  8512. "Marker pointing to the location where `org-open-at-point; was called.")
  8513. ;;;###autoload
  8514. (defun org-open-at-point-global ()
  8515. "Follow a link like Org-mode does.
  8516. This command can be called in any mode to follow a link that has
  8517. Org-mode syntax."
  8518. (interactive)
  8519. (org-run-like-in-org-mode 'org-open-at-point))
  8520. ;;;###autoload
  8521. (defun org-open-link-from-string (s &optional arg reference-buffer)
  8522. "Open a link in the string S, as if it was in Org-mode."
  8523. (interactive "sLink: \nP")
  8524. (let ((reference-buffer (or reference-buffer (current-buffer))))
  8525. (with-temp-buffer
  8526. (let ((org-inhibit-startup (not reference-buffer)))
  8527. (org-mode)
  8528. (insert s)
  8529. (goto-char (point-min))
  8530. (when reference-buffer
  8531. (setq org-link-abbrev-alist-local
  8532. (with-current-buffer reference-buffer
  8533. org-link-abbrev-alist-local)))
  8534. (org-open-at-point arg reference-buffer)))))
  8535. (defvar org-open-at-point-functions nil
  8536. "Hook that is run when following a link at point.
  8537. Functions in this hook must return t if they identify and follow
  8538. a link at point. If they don't find anything interesting at point,
  8539. they must return nil.")
  8540. (defun org-open-at-point (&optional arg reference-buffer)
  8541. "Open link at or after point.
  8542. If there is no link at point, this function will search forward up to
  8543. the end of the current line.
  8544. Normally, files will be opened by an appropriate application. If the
  8545. optional prefix argument ARG is non-nil, Emacs will visit the file.
  8546. With a double prefix argument, try to open outside of Emacs, in the
  8547. application the system uses for this file type."
  8548. (interactive "P")
  8549. ;; if in a code block, then open the block's results
  8550. (unless (call-interactively #'org-babel-open-src-block-result)
  8551. (org-load-modules-maybe)
  8552. (move-marker org-open-link-marker (point))
  8553. (setq org-window-config-before-follow-link (current-window-configuration))
  8554. (org-remove-occur-highlights nil nil t)
  8555. (cond
  8556. ((and (org-at-heading-p)
  8557. (not (org-in-regexp
  8558. (concat org-plain-link-re "\\|"
  8559. org-bracket-link-regexp "\\|"
  8560. org-angle-link-re "\\|"
  8561. "[ \t]:[^ \t\n]+:[ \t]*$")))
  8562. (not (get-text-property (point) 'org-linked-text)))
  8563. (or (org-offer-links-in-entry arg)
  8564. (progn (require 'org-attach) (org-attach-reveal 'if-exists))))
  8565. ((run-hook-with-args-until-success 'org-open-at-point-functions))
  8566. ((org-at-timestamp-p t) (org-follow-timestamp-link))
  8567. ((and (or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  8568. (not (org-in-regexp org-bracket-link-regexp)))
  8569. (org-footnote-action))
  8570. (t
  8571. (let (type path link line search (pos (point)))
  8572. (catch 'match
  8573. (save-excursion
  8574. (skip-chars-forward "^]\n\r")
  8575. (when (org-in-regexp org-bracket-link-regexp 1)
  8576. (setq link (org-extract-attributes
  8577. (org-link-unescape (org-match-string-no-properties 1))))
  8578. (while (string-match " *\n *" link)
  8579. (setq link (replace-match " " t t link)))
  8580. (setq link (org-link-expand-abbrev link))
  8581. (cond
  8582. ((or (file-name-absolute-p link)
  8583. (string-match "^\\.\\.?/" link))
  8584. (setq type "file" path link))
  8585. ((string-match org-link-re-with-space3 link)
  8586. (setq type (match-string 1 link) path (match-string 2 link)))
  8587. (t (setq type "thisfile" path link)))
  8588. (throw 'match t)))
  8589. (when (get-text-property (point) 'org-linked-text)
  8590. (setq type "thisfile"
  8591. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  8592. (1+ (point)) (point))
  8593. path (buffer-substring
  8594. (or (previous-single-property-change pos 'org-linked-text)
  8595. (point-min))
  8596. (or (next-single-property-change pos 'org-linked-text)
  8597. (point-max))))
  8598. (throw 'match t))
  8599. (save-excursion
  8600. (when (or (org-in-regexp org-angle-link-re)
  8601. (org-in-regexp org-plain-link-re))
  8602. (setq type (match-string 1)
  8603. path (org-link-unescape (match-string 2)))
  8604. (throw 'match t)))
  8605. (save-excursion
  8606. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@#%:]+\\):[ \t]*$"))
  8607. (setq type "tags"
  8608. path (match-string 1))
  8609. (while (string-match ":" path)
  8610. (setq path (replace-match "+" t t path)))
  8611. (throw 'match t)))
  8612. (when (org-in-regexp "<\\([^><\n]+\\)>")
  8613. (setq type "tree-match"
  8614. path (match-string 1))
  8615. (throw 'match t)))
  8616. (unless path
  8617. (error "No link found"))
  8618. ;; switch back to reference buffer
  8619. ;; needed when if called in a temporary buffer through
  8620. ;; org-open-link-from-string
  8621. (with-current-buffer (or reference-buffer (current-buffer))
  8622. ;; Remove any trailing spaces in path
  8623. (if (string-match " +\\'" path)
  8624. (setq path (replace-match "" t t path)))
  8625. (if (and org-link-translation-function
  8626. (fboundp org-link-translation-function))
  8627. ;; Check if we need to translate the link
  8628. (let ((tmp (funcall org-link-translation-function type path)))
  8629. (setq type (car tmp) path (cdr tmp))))
  8630. (cond
  8631. ((assoc type org-link-protocols)
  8632. (funcall (nth 1 (assoc type org-link-protocols)) path))
  8633. ((equal type "mailto")
  8634. (let ((cmd (car org-link-mailto-program))
  8635. (args (cdr org-link-mailto-program)) args1
  8636. (address path) (subject "") a)
  8637. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  8638. (setq address (match-string 1 path)
  8639. subject (org-link-escape (match-string 2 path))))
  8640. (while args
  8641. (cond
  8642. ((not (stringp (car args))) (push (pop args) args1))
  8643. (t (setq a (pop args))
  8644. (if (string-match "%a" a)
  8645. (setq a (replace-match address t t a)))
  8646. (if (string-match "%s" a)
  8647. (setq a (replace-match subject t t a)))
  8648. (push a args1))))
  8649. (apply cmd (nreverse args1))))
  8650. ((member type '("http" "https" "ftp" "news"))
  8651. (browse-url (concat type ":" (if (org-string-match-p "[[:nonascii:] ]" path)
  8652. (org-link-escape
  8653. path org-link-escape-chars-browser)
  8654. path))))
  8655. ((string= type "doi")
  8656. (browse-url (concat "http://dx.doi.org/" (if (org-string-match-p "[[:nonascii:] ]" path)
  8657. (org-link-escape
  8658. path org-link-escape-chars-browser)
  8659. path))))
  8660. ((member type '("message"))
  8661. (browse-url (concat type ":" path)))
  8662. ((string= type "tags")
  8663. (org-tags-view arg path))
  8664. ((string= type "tree-match")
  8665. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  8666. ((string= type "file")
  8667. (if (string-match "::\\([0-9]+\\)\\'" path)
  8668. (setq line (string-to-number (match-string 1 path))
  8669. path (substring path 0 (match-beginning 0)))
  8670. (if (string-match "::\\(.+\\)\\'" path)
  8671. (setq search (match-string 1 path)
  8672. path (substring path 0 (match-beginning 0)))))
  8673. (if (string-match "[*?{]" (file-name-nondirectory path))
  8674. (dired path)
  8675. (org-open-file path arg line search)))
  8676. ((string= type "shell")
  8677. (let ((cmd path))
  8678. (if (or (and (not (string= org-confirm-shell-link-not-regexp ""))
  8679. (string-match org-confirm-shell-link-not-regexp cmd))
  8680. (not org-confirm-shell-link-function)
  8681. (funcall org-confirm-shell-link-function
  8682. (format "Execute \"%s\" in shell? "
  8683. (org-add-props cmd nil
  8684. 'face 'org-warning))))
  8685. (progn
  8686. (message "Executing %s" cmd)
  8687. (shell-command cmd))
  8688. (error "Abort"))))
  8689. ((string= type "elisp")
  8690. (let ((cmd path))
  8691. (if (or (and (not (string= org-confirm-elisp-link-not-regexp ""))
  8692. (string-match org-confirm-elisp-link-not-regexp cmd))
  8693. (not org-confirm-elisp-link-function)
  8694. (funcall org-confirm-elisp-link-function
  8695. (format "Execute \"%s\" as elisp? "
  8696. (org-add-props cmd nil
  8697. 'face 'org-warning))))
  8698. (message "%s => %s" cmd
  8699. (if (equal (string-to-char cmd) ?\()
  8700. (eval (read cmd))
  8701. (call-interactively (read cmd))))
  8702. (error "Abort"))))
  8703. ((and (string= type "thisfile")
  8704. (run-hook-with-args-until-success
  8705. 'org-open-link-functions path)))
  8706. ((string= type "thisfile")
  8707. (if arg
  8708. (switch-to-buffer-other-window
  8709. (org-get-buffer-for-internal-link (current-buffer)))
  8710. (org-mark-ring-push))
  8711. (let ((cmd `(org-link-search
  8712. ,path
  8713. ,(cond ((equal arg '(4)) ''occur)
  8714. ((equal arg '(16)) ''org-occur)
  8715. (t nil))
  8716. ,pos)))
  8717. (condition-case nil (let ((org-link-search-inhibit-query t))
  8718. (eval cmd))
  8719. (error (progn (widen) (eval cmd))))))
  8720. (t
  8721. (browse-url-at-point)))))))
  8722. (move-marker org-open-link-marker nil)
  8723. (run-hook-with-args 'org-follow-link-hook)))
  8724. (defun org-offer-links-in-entry (&optional nth zero)
  8725. "Offer links in the current entry and follow the selected link.
  8726. If there is only one link, follow it immediately as well.
  8727. If NTH is an integer, immediately pick the NTH link found.
  8728. If ZERO is a string, check also this string for a link, and if
  8729. there is one, offer it as link number zero."
  8730. (let ((re (concat "\\(" org-bracket-link-regexp "\\)\\|"
  8731. "\\(" org-angle-link-re "\\)\\|"
  8732. "\\(" org-plain-link-re "\\)"))
  8733. (cnt ?0)
  8734. (in-emacs (if (integerp nth) nil nth))
  8735. have-zero end links link c)
  8736. (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
  8737. (push (match-string 0 zero) links)
  8738. (setq cnt (1- cnt) have-zero t))
  8739. (save-excursion
  8740. (org-back-to-heading t)
  8741. (setq end (save-excursion (outline-next-heading) (point)))
  8742. (while (re-search-forward re end t)
  8743. (push (match-string 0) links))
  8744. (setq links (org-uniquify (reverse links))))
  8745. (cond
  8746. ((null links)
  8747. (message "No links"))
  8748. ((equal (length links) 1)
  8749. (setq link (list (car links))))
  8750. ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
  8751. (setq link (list (nth (if have-zero nth (1- nth)) links))))
  8752. (t ; we have to select a link
  8753. (save-excursion
  8754. (save-window-excursion
  8755. (delete-other-windows)
  8756. (with-output-to-temp-buffer "*Select Link*"
  8757. (mapc (lambda (l)
  8758. (if (not (string-match org-bracket-link-regexp l))
  8759. (princ (format "[%c] %s\n" (incf cnt)
  8760. (org-remove-angle-brackets l)))
  8761. (if (match-end 3)
  8762. (princ (format "[%c] %s (%s)\n" (incf cnt)
  8763. (match-string 3 l) (match-string 1 l)))
  8764. (princ (format "[%c] %s\n" (incf cnt)
  8765. (match-string 1 l))))))
  8766. links))
  8767. (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
  8768. (message "Select link to open, RET to open all:")
  8769. (setq c (read-char-exclusive))
  8770. (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
  8771. (when (equal c ?q) (error "Abort"))
  8772. (if (equal c ?\C-m)
  8773. (setq link links)
  8774. (setq nth (- c ?0))
  8775. (if have-zero (setq nth (1+ nth)))
  8776. (unless (and (integerp nth) (>= (length links) nth))
  8777. (error "Invalid link selection"))
  8778. (setq link (list (nth (1- nth) links))))))
  8779. (if link
  8780. (let ((buf (current-buffer)))
  8781. (dolist (l link)
  8782. (org-open-link-from-string l in-emacs buf))
  8783. t)
  8784. nil)))
  8785. ;; Add special file links that specify the way of opening
  8786. (org-add-link-type "file+sys" 'org-open-file-with-system)
  8787. (org-add-link-type "file+emacs" 'org-open-file-with-emacs)
  8788. (defun org-open-file-with-system (path)
  8789. "Open file at PATH using the system way of opening it."
  8790. (org-open-file path 'system))
  8791. (defun org-open-file-with-emacs (path)
  8792. "Open file at PATH in Emacs."
  8793. (org-open-file path 'emacs))
  8794. (defun org-remove-file-link-modifiers ()
  8795. "Remove the file link modifiers in `file+sys:' and `file+emacs:' links."
  8796. (goto-char (point-min))
  8797. (while (re-search-forward "\\<file\\+\\(sys\\|emacs\\):" nil t)
  8798. (org-if-unprotected
  8799. (replace-match "file:" t t))))
  8800. (eval-after-load "org-exp"
  8801. '(add-hook 'org-export-preprocess-before-normalizing-links-hook
  8802. 'org-remove-file-link-modifiers))
  8803. ;;;; Time estimates
  8804. (defun org-get-effort (&optional pom)
  8805. "Get the effort estimate for the current entry."
  8806. (org-entry-get pom org-effort-property))
  8807. ;;; File search
  8808. (defvar org-create-file-search-functions nil
  8809. "List of functions to construct the right search string for a file link.
  8810. These functions are called in turn with point at the location to
  8811. which the link should point.
  8812. A function in the hook should first test if it would like to
  8813. handle this file type, for example by checking the `major-mode'
  8814. or the file extension. If it decides not to handle this file, it
  8815. should just return nil to give other functions a chance. If it
  8816. does handle the file, it must return the search string to be used
  8817. when following the link. The search string will be part of the
  8818. file link, given after a double colon, and `org-open-at-point'
  8819. will automatically search for it. If special measures must be
  8820. taken to make the search successful, another function should be
  8821. added to the companion hook `org-execute-file-search-functions',
  8822. which see.
  8823. A function in this hook may also use `setq' to set the variable
  8824. `description' to provide a suggestion for the descriptive text to
  8825. be used for this link when it gets inserted into an Org-mode
  8826. buffer with \\[org-insert-link].")
  8827. (defvar org-execute-file-search-functions nil
  8828. "List of functions to execute a file search triggered by a link.
  8829. Functions added to this hook must accept a single argument, the
  8830. search string that was part of the file link, the part after the
  8831. double colon. The function must first check if it would like to
  8832. handle this search, for example by checking the `major-mode' or
  8833. the file extension. If it decides not to handle this search, it
  8834. should just return nil to give other functions a chance. If it
  8835. does handle the search, it must return a non-nil value to keep
  8836. other functions from trying.
  8837. Each function can access the current prefix argument through the
  8838. variable `current-prefix-argument'. Note that a single prefix is
  8839. used to force opening a link in Emacs, so it may be good to only
  8840. use a numeric or double prefix to guide the search function.
  8841. In case this is needed, a function in this hook can also restore
  8842. the window configuration before `org-open-at-point' was called using:
  8843. (set-window-configuration org-window-config-before-follow-link)")
  8844. (defvar org-link-search-inhibit-query nil) ;; dynamically scoped
  8845. (defun org-link-search (s &optional type avoid-pos stealth)
  8846. "Search for a link search option.
  8847. If S is surrounded by forward slashes, it is interpreted as a
  8848. regular expression. In org-mode files, this will create an `org-occur'
  8849. sparse tree. In ordinary files, `occur' will be used to list matches.
  8850. If the current buffer is in `dired-mode', grep will be used to search
  8851. in all files. If AVOID-POS is given, ignore matches near that position.
  8852. When optional argument STEALTH is non-nil, do not modify
  8853. visibility around point, thus ignoring
  8854. `org-show-hierarchy-above', `org-show-following-heading' and
  8855. `org-show-siblings' variables."
  8856. (let ((case-fold-search t)
  8857. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  8858. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  8859. (append '(("") (" ") ("\t") ("\n"))
  8860. org-emphasis-alist)
  8861. "\\|") "\\)"))
  8862. (pos (point))
  8863. (pre nil) (post nil)
  8864. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  8865. (cond
  8866. ;; First check if there are any special search functions
  8867. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  8868. ;; Now try the builtin stuff
  8869. ((and (equal (string-to-char s0) ?#)
  8870. (> (length s0) 1)
  8871. (save-excursion
  8872. (goto-char (point-min))
  8873. (and
  8874. (re-search-forward
  8875. (concat "^[ \t]*:CUSTOM_ID:[ \t]+" (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
  8876. (setq type 'dedicated
  8877. pos (match-beginning 0))))
  8878. ;; There is an exact target for this
  8879. (goto-char pos)
  8880. (org-back-to-heading t)))
  8881. ((save-excursion
  8882. (goto-char (point-min))
  8883. (and
  8884. (re-search-forward
  8885. (concat "<<" (regexp-quote s0) ">>") nil t)
  8886. (setq type 'dedicated
  8887. pos (match-beginning 0))))
  8888. ;; There is an exact target for this
  8889. (goto-char pos))
  8890. ((and (string-match "^(\\(.*\\))$" s0)
  8891. (save-excursion
  8892. (goto-char (point-min))
  8893. (and
  8894. (re-search-forward
  8895. (concat "[^[]" (regexp-quote
  8896. (format org-coderef-label-format
  8897. (match-string 1 s0))))
  8898. nil t)
  8899. (setq type 'dedicated
  8900. pos (1+ (match-beginning 0))))))
  8901. ;; There is a coderef target for this
  8902. (goto-char pos))
  8903. ((string-match "^/\\(.*\\)/$" s)
  8904. ;; A regular expression
  8905. (cond
  8906. ((eq major-mode 'org-mode)
  8907. (org-occur (match-string 1 s)))
  8908. ;;((eq major-mode 'dired-mode)
  8909. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  8910. (t (org-do-occur (match-string 1 s)))))
  8911. ((and (eq major-mode 'org-mode) org-link-search-must-match-exact-headline)
  8912. (and (equal (string-to-char s) ?*) (setq s (substring s 1)))
  8913. (goto-char (point-min))
  8914. (cond
  8915. ((let (case-fold-search)
  8916. (re-search-forward (format org-complex-heading-regexp-format
  8917. (regexp-quote s))
  8918. nil t))
  8919. ;; OK, found a match
  8920. (setq type 'dedicated)
  8921. (goto-char (match-beginning 0)))
  8922. ((and (not org-link-search-inhibit-query)
  8923. (eq org-link-search-must-match-exact-headline 'query-to-create)
  8924. (y-or-n-p "No match - create this as a new heading? "))
  8925. (goto-char (point-max))
  8926. (or (bolp) (newline))
  8927. (insert "* " s "\n")
  8928. (beginning-of-line 0))
  8929. (t
  8930. (goto-char pos)
  8931. (error "No match"))))
  8932. (t
  8933. ;; A normal search string
  8934. (when (equal (string-to-char s) ?*)
  8935. ;; Anchor on headlines, post may include tags.
  8936. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  8937. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@#%:+]:[ \t]*\\)?$")
  8938. s (substring s 1)))
  8939. (remove-text-properties
  8940. 0 (length s)
  8941. '(face nil mouse-face nil keymap nil fontified nil) s)
  8942. ;; Make a series of regular expressions to find a match
  8943. (setq words (org-split-string s "[ \n\r\t]+")
  8944. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  8945. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  8946. "\\)" markers)
  8947. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  8948. re2a (concat "[ \t\r\n]" re2a_)
  8949. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  8950. re4 (concat "[^a-zA-Z_]" re4_)
  8951. re1 (concat pre re2 post)
  8952. re3 (concat pre (if pre re4_ re4) post)
  8953. re5 (concat pre ".*" re4)
  8954. re2 (concat pre re2)
  8955. re2a (concat pre (if pre re2a_ re2a))
  8956. re4 (concat pre (if pre re4_ re4))
  8957. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  8958. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  8959. re5 "\\)"
  8960. ))
  8961. (cond
  8962. ((eq type 'org-occur) (org-occur reall))
  8963. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  8964. (t (goto-char (point-min))
  8965. (setq type 'fuzzy)
  8966. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  8967. (org-search-not-self 1 re1 nil t)
  8968. (org-search-not-self 1 re2 nil t)
  8969. (org-search-not-self 1 re2a nil t)
  8970. (org-search-not-self 1 re3 nil t)
  8971. (org-search-not-self 1 re4 nil t)
  8972. (org-search-not-self 1 re5 nil t)
  8973. )
  8974. (goto-char (match-beginning 1))
  8975. (goto-char pos)
  8976. (error "No match"))))))
  8977. (and (eq major-mode 'org-mode)
  8978. (not stealth)
  8979. (org-show-context 'link-search))
  8980. type))
  8981. (defun org-search-not-self (group &rest args)
  8982. "Execute `re-search-forward', but only accept matches that do not
  8983. enclose the position of `org-open-link-marker'."
  8984. (let ((m org-open-link-marker))
  8985. (catch 'exit
  8986. (while (apply 're-search-forward args)
  8987. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  8988. (goto-char (match-end group))
  8989. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  8990. (> (match-beginning 0) (marker-position m))
  8991. (< (match-end 0) (marker-position m)))
  8992. (save-match-data
  8993. (or (not (org-in-regexp
  8994. org-bracket-link-analytic-regexp 1))
  8995. (not (match-end 4)) ; no description
  8996. (and (<= (match-beginning 4) (point))
  8997. (>= (match-end 4) (point))))))
  8998. (throw 'exit (point))))))))
  8999. (defun org-get-buffer-for-internal-link (buffer)
  9000. "Return a buffer to be used for displaying the link target of internal links."
  9001. (cond
  9002. ((not org-display-internal-link-with-indirect-buffer)
  9003. buffer)
  9004. ((string-match "(Clone)$" (buffer-name buffer))
  9005. (message "Buffer is already a clone, not making another one")
  9006. ;; we also do not modify visibility in this case
  9007. buffer)
  9008. (t ; make a new indirect buffer for displaying the link
  9009. (let* ((bn (buffer-name buffer))
  9010. (ibn (concat bn "(Clone)"))
  9011. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  9012. (with-current-buffer ib (org-overview))
  9013. ib))))
  9014. (defun org-do-occur (regexp &optional cleanup)
  9015. "Call the Emacs command `occur'.
  9016. If CLEANUP is non-nil, remove the printout of the regular expression
  9017. in the *Occur* buffer. This is useful if the regex is long and not useful
  9018. to read."
  9019. (occur regexp)
  9020. (when cleanup
  9021. (let ((cwin (selected-window)) win beg end)
  9022. (when (setq win (get-buffer-window "*Occur*"))
  9023. (select-window win))
  9024. (goto-char (point-min))
  9025. (when (re-search-forward "match[a-z]+" nil t)
  9026. (setq beg (match-end 0))
  9027. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  9028. (setq end (1- (match-beginning 0)))))
  9029. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  9030. (goto-char (point-min))
  9031. (select-window cwin))))
  9032. ;;; The mark ring for links jumps
  9033. (defvar org-mark-ring nil
  9034. "Mark ring for positions before jumps in Org-mode.")
  9035. (defvar org-mark-ring-last-goto nil
  9036. "Last position in the mark ring used to go back.")
  9037. ;; Fill and close the ring
  9038. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  9039. (loop for i from 1 to org-mark-ring-length do
  9040. (push (make-marker) org-mark-ring))
  9041. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  9042. org-mark-ring)
  9043. (defun org-mark-ring-push (&optional pos buffer)
  9044. "Put the current position or POS into the mark ring and rotate it."
  9045. (interactive)
  9046. (setq pos (or pos (point)))
  9047. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  9048. (move-marker (car org-mark-ring)
  9049. (or pos (point))
  9050. (or buffer (current-buffer)))
  9051. (message "%s"
  9052. (substitute-command-keys
  9053. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  9054. (defun org-mark-ring-goto (&optional n)
  9055. "Jump to the previous position in the mark ring.
  9056. With prefix arg N, jump back that many stored positions. When
  9057. called several times in succession, walk through the entire ring.
  9058. Org-mode commands jumping to a different position in the current file,
  9059. or to another Org-mode file, automatically push the old position
  9060. onto the ring."
  9061. (interactive "p")
  9062. (let (p m)
  9063. (if (eq last-command this-command)
  9064. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  9065. (setq p org-mark-ring))
  9066. (setq org-mark-ring-last-goto p)
  9067. (setq m (car p))
  9068. (org-pop-to-buffer-same-window (marker-buffer m))
  9069. (goto-char m)
  9070. (if (or (outline-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  9071. (defun org-remove-angle-brackets (s)
  9072. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  9073. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  9074. s)
  9075. (defun org-add-angle-brackets (s)
  9076. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  9077. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  9078. s)
  9079. (defun org-remove-double-quotes (s)
  9080. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  9081. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  9082. s)
  9083. ;;; Following specific links
  9084. (defun org-follow-timestamp-link ()
  9085. (cond
  9086. ((org-at-date-range-p t)
  9087. (let ((org-agenda-start-on-weekday)
  9088. (t1 (match-string 1))
  9089. (t2 (match-string 2)))
  9090. (setq t1 (time-to-days (org-time-string-to-time t1))
  9091. t2 (time-to-days (org-time-string-to-time t2)))
  9092. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  9093. ((org-at-timestamp-p t)
  9094. (org-agenda-list nil (time-to-days (org-time-string-to-time
  9095. (substring (match-string 1) 0 10)))
  9096. 1))
  9097. (t (error "This should not happen"))))
  9098. ;;; Following file links
  9099. (declare-function mailcap-parse-mailcaps "mailcap" (&optional path force))
  9100. (declare-function mailcap-extension-to-mime "mailcap" (extn))
  9101. (declare-function mailcap-mime-info
  9102. "mailcap" (string &optional request no-decode))
  9103. (defvar org-wait nil)
  9104. (defun org-open-file (path &optional in-emacs line search)
  9105. "Open the file at PATH.
  9106. First, this expands any special file name abbreviations. Then the
  9107. configuration variable `org-file-apps' is checked if it contains an
  9108. entry for this file type, and if yes, the corresponding command is launched.
  9109. If no application is found, Emacs simply visits the file.
  9110. With optional prefix argument IN-EMACS, Emacs will visit the file.
  9111. With a double \\[universal-argument] \\[universal-argument] \
  9112. prefix arg, Org tries to avoid opening in Emacs
  9113. and to use an external application to visit the file.
  9114. Optional LINE specifies a line to go to, optional SEARCH a string
  9115. to search for. If LINE or SEARCH is given, the file will be
  9116. opened in Emacs, unless an entry from org-file-apps that makes
  9117. use of groups in a regexp matches.
  9118. If you want to change the way frames are used when following a
  9119. link, please customize `org-link-frame-setup'.
  9120. If the file does not exist, an error is thrown."
  9121. (let* ((file (if (equal path "")
  9122. buffer-file-name
  9123. (substitute-in-file-name (expand-file-name path))))
  9124. (file-apps (append org-file-apps (org-default-apps)))
  9125. (apps (org-remove-if
  9126. 'org-file-apps-entry-match-against-dlink-p file-apps))
  9127. (apps-dlink (org-remove-if-not
  9128. 'org-file-apps-entry-match-against-dlink-p file-apps))
  9129. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  9130. (dirp (if remp nil (file-directory-p file)))
  9131. (file (if (and dirp org-open-directory-means-index-dot-org)
  9132. (concat (file-name-as-directory file) "index.org")
  9133. file))
  9134. (a-m-a-p (assq 'auto-mode apps))
  9135. (dfile (downcase file))
  9136. ;; reconstruct the original file: link from the PATH, LINE and SEARCH args
  9137. (link (cond ((and (eq line nil)
  9138. (eq search nil))
  9139. file)
  9140. (line
  9141. (concat file "::" (number-to-string line)))
  9142. (search
  9143. (concat file "::" search))))
  9144. (dlink (downcase link))
  9145. (old-buffer (current-buffer))
  9146. (old-pos (point))
  9147. (old-mode major-mode)
  9148. ext cmd link-match-data)
  9149. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  9150. (setq ext (match-string 1 dfile))
  9151. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  9152. (setq ext (match-string 1 dfile))))
  9153. (cond
  9154. ((member in-emacs '((16) system))
  9155. (setq cmd (cdr (assoc 'system apps))))
  9156. (in-emacs (setq cmd 'emacs))
  9157. (t
  9158. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  9159. (and dirp (cdr (assoc 'directory apps)))
  9160. ; first, try matching against apps-dlink
  9161. ; if we get a match here, store the match data for later
  9162. (let ((match (assoc-default dlink apps-dlink
  9163. 'string-match)))
  9164. (if match
  9165. (progn (setq link-match-data (match-data))
  9166. match)
  9167. (progn (setq in-emacs (or in-emacs line search))
  9168. nil))) ; if we have no match in apps-dlink,
  9169. ; always open the file in emacs if line or search
  9170. ; is given (for backwards compatibility)
  9171. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  9172. 'string-match)
  9173. (cdr (assoc ext apps))
  9174. (cdr (assoc t apps))))))
  9175. (when (eq cmd 'system)
  9176. (setq cmd (cdr (assoc 'system apps))))
  9177. (when (eq cmd 'default)
  9178. (setq cmd (cdr (assoc t apps))))
  9179. (when (eq cmd 'mailcap)
  9180. (require 'mailcap)
  9181. (mailcap-parse-mailcaps)
  9182. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  9183. (command (mailcap-mime-info mime-type)))
  9184. (if (stringp command)
  9185. (setq cmd command)
  9186. (setq cmd 'emacs))))
  9187. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  9188. (not (file-exists-p file))
  9189. (not org-open-non-existing-files))
  9190. (error "No such file: %s" file))
  9191. (cond
  9192. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  9193. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  9194. (while (string-match "['\"]%s['\"]" cmd)
  9195. (setq cmd (replace-match "%s" t t cmd)))
  9196. (while (string-match "%s" cmd)
  9197. (setq cmd (replace-match
  9198. (save-match-data
  9199. (shell-quote-argument
  9200. (convert-standard-filename file)))
  9201. t t cmd)))
  9202. ;; Replace "%1", "%2" etc. in command with group matches from regex
  9203. (save-match-data
  9204. (let ((match-index 1)
  9205. (number-of-groups (- (/ (length link-match-data) 2) 1)))
  9206. (set-match-data link-match-data)
  9207. (while (<= match-index number-of-groups)
  9208. (let ((regex (concat "%" (number-to-string match-index)))
  9209. (replace-with (match-string match-index dlink)))
  9210. (while (string-match regex cmd)
  9211. (setq cmd (replace-match replace-with t t cmd))))
  9212. (setq match-index (+ match-index 1)))))
  9213. (save-window-excursion
  9214. (start-process-shell-command cmd nil cmd)
  9215. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
  9216. ))
  9217. ((or (stringp cmd)
  9218. (eq cmd 'emacs))
  9219. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  9220. (widen)
  9221. (if line (org-goto-line line)
  9222. (if search (org-link-search search))))
  9223. ((consp cmd)
  9224. (let ((file (convert-standard-filename file)))
  9225. (save-match-data
  9226. (set-match-data link-match-data)
  9227. (eval cmd))))
  9228. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  9229. (and (eq major-mode 'org-mode) (eq old-mode 'org-mode)
  9230. (or (not (equal old-buffer (current-buffer)))
  9231. (not (equal old-pos (point))))
  9232. (org-mark-ring-push old-pos old-buffer))))
  9233. (defun org-file-apps-entry-match-against-dlink-p (entry)
  9234. "This function returns non-nil if `entry' uses a regular
  9235. expression which should be matched against the whole link by
  9236. org-open-file.
  9237. It assumes that is the case when the entry uses a regular
  9238. expression which has at least one grouping construct and the
  9239. action is either a lisp form or a command string containing
  9240. '%1', i.e. using at least one subexpression match as a
  9241. parameter."
  9242. (let ((selector (car entry))
  9243. (action (cdr entry)))
  9244. (if (stringp selector)
  9245. (and (> (regexp-opt-depth selector) 0)
  9246. (or (and (stringp action)
  9247. (string-match "%[0-9]" action))
  9248. (consp action)))
  9249. nil)))
  9250. (defun org-default-apps ()
  9251. "Return the default applications for this operating system."
  9252. (cond
  9253. ((eq system-type 'darwin)
  9254. org-file-apps-defaults-macosx)
  9255. ((eq system-type 'windows-nt)
  9256. org-file-apps-defaults-windowsnt)
  9257. (t org-file-apps-defaults-gnu)))
  9258. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  9259. "Convert extensions to regular expressions in the cars of LIST.
  9260. Also, weed out any non-string entries, because the return value is used
  9261. only for regexp matching.
  9262. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  9263. point to the symbol `emacs', indicating that the file should
  9264. be opened in Emacs."
  9265. (append
  9266. (delq nil
  9267. (mapcar (lambda (x)
  9268. (if (not (stringp (car x)))
  9269. nil
  9270. (if (string-match "\\W" (car x))
  9271. x
  9272. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  9273. list))
  9274. (if add-auto-mode
  9275. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  9276. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  9277. (defun org-file-remote-p (file)
  9278. "Test whether FILE specifies a location on a remote system.
  9279. Return non-nil if the location is indeed remote.
  9280. For example, the filename \"/user@host:/foo\" specifies a location
  9281. on the system \"/user@host:\"."
  9282. (cond ((fboundp 'file-remote-p)
  9283. (file-remote-p file))
  9284. ((fboundp 'tramp-handle-file-remote-p)
  9285. (tramp-handle-file-remote-p file))
  9286. ((and (boundp 'ange-ftp-name-format)
  9287. (string-match (car ange-ftp-name-format) file))
  9288. t)
  9289. (t nil)))
  9290. ;;;; Refiling
  9291. (defun org-get-org-file ()
  9292. "Read a filename, with default directory `org-directory'."
  9293. (let ((default (or org-default-notes-file remember-data-file)))
  9294. (read-file-name (format "File name [%s]: " default)
  9295. (file-name-as-directory org-directory)
  9296. default)))
  9297. (defun org-notes-order-reversed-p ()
  9298. "Check if the current file should receive notes in reversed order."
  9299. (cond
  9300. ((not org-reverse-note-order) nil)
  9301. ((eq t org-reverse-note-order) t)
  9302. ((not (listp org-reverse-note-order)) nil)
  9303. (t (catch 'exit
  9304. (let ((all org-reverse-note-order)
  9305. entry)
  9306. (while (setq entry (pop all))
  9307. (if (string-match (car entry) buffer-file-name)
  9308. (throw 'exit (cdr entry))))
  9309. nil)))))
  9310. (defvar org-refile-target-table nil
  9311. "The list of refile targets, created by `org-refile'.")
  9312. (defvar org-agenda-new-buffers nil
  9313. "Buffers created to visit agenda files.")
  9314. (defvar org-refile-cache nil
  9315. "Cache for refile targets.")
  9316. (defvar org-refile-markers nil
  9317. "All the markers used for caching refile locations.")
  9318. (defun org-refile-marker (pos)
  9319. "Get a new refile marker, but only if caching is in use."
  9320. (if (not org-refile-use-cache)
  9321. pos
  9322. (let ((m (make-marker)))
  9323. (move-marker m pos)
  9324. (push m org-refile-markers)
  9325. m)))
  9326. (defun org-refile-cache-clear ()
  9327. "Clear the refile cache and disable all the markers."
  9328. (mapc (lambda (m) (move-marker m nil)) org-refile-markers)
  9329. (setq org-refile-markers nil)
  9330. (setq org-refile-cache nil)
  9331. (message "Refile cache has been cleared"))
  9332. (defun org-refile-cache-check-set (set)
  9333. "Check if all the markers in the cache still have live buffers."
  9334. (let (marker)
  9335. (catch 'exit
  9336. (while (and set (setq marker (nth 3 (pop set))))
  9337. ;; if org-refile-use-outline-path is 'file, marker may be nil
  9338. (when (and marker (null (marker-buffer marker)))
  9339. (message "not found") (sit-for 3)
  9340. (throw 'exit nil)))
  9341. t)))
  9342. (defun org-refile-cache-put (set &rest identifiers)
  9343. "Push the refile targets SET into the cache, under IDENTIFIERS."
  9344. (let* ((key (sha1 (prin1-to-string identifiers)))
  9345. (entry (assoc key org-refile-cache)))
  9346. (if entry
  9347. (setcdr entry set)
  9348. (push (cons key set) org-refile-cache))))
  9349. (defun org-refile-cache-get (&rest identifiers)
  9350. "Retrieve the cached value for refile targets given by IDENTIFIERS."
  9351. (cond
  9352. ((not org-refile-cache) nil)
  9353. ((not org-refile-use-cache) (org-refile-cache-clear) nil)
  9354. (t
  9355. (let ((set (cdr (assoc (sha1 (prin1-to-string identifiers))
  9356. org-refile-cache))))
  9357. (and set (org-refile-cache-check-set set) set)))))
  9358. (defun org-refile-get-targets (&optional default-buffer excluded-entries)
  9359. "Produce a table with refile targets."
  9360. (let ((case-fold-search nil)
  9361. ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
  9362. (entries (or org-refile-targets '((nil . (:level . 1)))))
  9363. targets tgs txt re files f desc descre fast-path-p level pos0)
  9364. (message "Getting targets...")
  9365. (with-current-buffer (or default-buffer (current-buffer))
  9366. (while (setq entry (pop entries))
  9367. (setq files (car entry) desc (cdr entry))
  9368. (setq fast-path-p nil)
  9369. (cond
  9370. ((null files) (setq files (list (current-buffer))))
  9371. ((eq files 'org-agenda-files)
  9372. (setq files (org-agenda-files 'unrestricted)))
  9373. ((and (symbolp files) (fboundp files))
  9374. (setq files (funcall files)))
  9375. ((and (symbolp files) (boundp files))
  9376. (setq files (symbol-value files))))
  9377. (if (stringp files) (setq files (list files)))
  9378. (cond
  9379. ((eq (car desc) :tag)
  9380. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  9381. ((eq (car desc) :todo)
  9382. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  9383. ((eq (car desc) :regexp)
  9384. (setq descre (cdr desc)))
  9385. ((eq (car desc) :level)
  9386. (setq descre (concat "^\\*\\{" (number-to-string
  9387. (if org-odd-levels-only
  9388. (1- (* 2 (cdr desc)))
  9389. (cdr desc)))
  9390. "\\}[ \t]")))
  9391. ((eq (car desc) :maxlevel)
  9392. (setq fast-path-p t)
  9393. (setq descre (concat "^\\*\\{1," (number-to-string
  9394. (if org-odd-levels-only
  9395. (1- (* 2 (cdr desc)))
  9396. (cdr desc)))
  9397. "\\}[ \t]")))
  9398. (t (error "Bad refiling target description %s" desc)))
  9399. (while (setq f (pop files))
  9400. (with-current-buffer
  9401. (if (bufferp f) f (org-get-agenda-file-buffer f))
  9402. (or
  9403. (setq tgs (org-refile-cache-get (buffer-file-name) descre))
  9404. (progn
  9405. (if (bufferp f) (setq f (buffer-file-name
  9406. (buffer-base-buffer f))))
  9407. (setq f (and f (expand-file-name f)))
  9408. (if (eq org-refile-use-outline-path 'file)
  9409. (push (list (file-name-nondirectory f) f nil nil) tgs))
  9410. (save-excursion
  9411. (save-restriction
  9412. (widen)
  9413. (goto-char (point-min))
  9414. (while (re-search-forward descre nil t)
  9415. (goto-char (setq pos0 (point-at-bol)))
  9416. (catch 'next
  9417. (when org-refile-target-verify-function
  9418. (save-match-data
  9419. (or (funcall org-refile-target-verify-function)
  9420. (throw 'next t))))
  9421. (when (and (looking-at org-complex-heading-regexp)
  9422. (not (member (match-string 4) excluded-entries))
  9423. (match-string 4))
  9424. (setq level (org-reduced-level
  9425. (- (match-end 1) (match-beginning 1)))
  9426. txt (org-link-display-format (match-string 4))
  9427. txt (replace-regexp-in-string "\\( *\[[0-9]+/?[0-9]*%?\]\\)+$" "" txt)
  9428. re (format org-complex-heading-regexp-format
  9429. (regexp-quote (match-string 4))))
  9430. (when org-refile-use-outline-path
  9431. (setq txt (mapconcat
  9432. 'org-protect-slash
  9433. (append
  9434. (if (eq org-refile-use-outline-path
  9435. 'file)
  9436. (list (file-name-nondirectory
  9437. (buffer-file-name
  9438. (buffer-base-buffer))))
  9439. (if (eq org-refile-use-outline-path
  9440. 'full-file-path)
  9441. (list (buffer-file-name
  9442. (buffer-base-buffer)))))
  9443. (org-get-outline-path fast-path-p
  9444. level txt)
  9445. (list txt))
  9446. "/")))
  9447. (push (list txt f re (org-refile-marker (point)))
  9448. tgs)))
  9449. (when (= (point) pos0)
  9450. ;; verification function has not moved point
  9451. (goto-char (point-at-eol))))))))
  9452. (when org-refile-use-cache
  9453. (org-refile-cache-put tgs (buffer-file-name) descre))
  9454. (setq targets (append tgs targets))
  9455. ))))
  9456. (message "Getting targets...done")
  9457. (nreverse targets)))
  9458. (defun org-protect-slash (s)
  9459. (while (string-match "/" s)
  9460. (setq s (replace-match "\\" t t s)))
  9461. s)
  9462. (defvar org-olpa (make-vector 20 nil))
  9463. (defun org-get-outline-path (&optional fastp level heading)
  9464. "Return the outline path to the current entry, as a list.
  9465. The parameters FASTP, LEVEL, and HEADING are for use by a scanner
  9466. routine which makes outline path derivations for an entire file,
  9467. avoiding backtracing. Refile target collection makes use of that."
  9468. (if fastp
  9469. (progn
  9470. (if (> level 19)
  9471. (error "Outline path failure, more than 19 levels"))
  9472. (loop for i from level upto 19 do
  9473. (aset org-olpa i nil))
  9474. (prog1
  9475. (delq nil (append org-olpa nil))
  9476. (aset org-olpa level heading)))
  9477. (let (rtn case-fold-search)
  9478. (save-excursion
  9479. (save-restriction
  9480. (widen)
  9481. (while (org-up-heading-safe)
  9482. (when (looking-at org-complex-heading-regexp)
  9483. (push (org-match-string-no-properties 4) rtn)))
  9484. rtn)))))
  9485. (defun org-format-outline-path (path &optional width prefix)
  9486. "Format the outline path PATH for display.
  9487. Width is the maximum number of characters that is available.
  9488. Prefix is a prefix to be included in the returned string,
  9489. such as the file name."
  9490. (setq width (or width 79))
  9491. (if prefix (setq width (- width (length prefix))))
  9492. (if (not path)
  9493. (or prefix "")
  9494. (let* ((nsteps (length path))
  9495. (total-width (+ nsteps (apply '+ (mapcar 'length path))))
  9496. (maxwidth (if (<= total-width width)
  9497. 10000 ;; everything fits
  9498. ;; we need to shorten the level headings
  9499. (/ (- width nsteps) nsteps)))
  9500. (org-odd-levels-only nil)
  9501. (n 0)
  9502. (total (1+ (length prefix))))
  9503. (setq maxwidth (max maxwidth 10))
  9504. (concat prefix
  9505. (mapconcat
  9506. (lambda (h)
  9507. (setq n (1+ n))
  9508. (if (and (= n nsteps) (< maxwidth 10000))
  9509. (setq maxwidth (- total-width total)))
  9510. (if (< (length h) maxwidth)
  9511. (progn (setq total (+ total (length h) 1)) h)
  9512. (setq h (substring h 0 (- maxwidth 2))
  9513. total (+ total maxwidth 1))
  9514. (if (string-match "[ \t]+\\'" h)
  9515. (setq h (substring h 0 (match-beginning 0))))
  9516. (setq h (concat h "..")))
  9517. (org-add-props h nil 'face
  9518. (nth (% (1- n) org-n-level-faces)
  9519. org-level-faces))
  9520. h)
  9521. path "/")))))
  9522. (defun org-display-outline-path (&optional file current)
  9523. "Display the current outline path in the echo area."
  9524. (interactive "P")
  9525. (let* ((bfn (buffer-file-name (buffer-base-buffer)))
  9526. (case-fold-search nil)
  9527. (path (and (eq major-mode 'org-mode) (org-get-outline-path))))
  9528. (if current (setq path (append path
  9529. (save-excursion
  9530. (org-back-to-heading t)
  9531. (if (looking-at org-complex-heading-regexp)
  9532. (list (match-string 4)))))))
  9533. (message "%s"
  9534. (org-format-outline-path
  9535. path
  9536. (1- (frame-width))
  9537. (and file bfn (concat (file-name-nondirectory bfn) "/"))))))
  9538. (defvar org-refile-history nil
  9539. "History for refiling operations.")
  9540. (defvar org-after-refile-insert-hook nil
  9541. "Hook run after `org-refile' has inserted its stuff at the new location.
  9542. Note that this is still *before* the stuff will be removed from
  9543. the *old* location.")
  9544. (defvar org-capture-last-stored-marker)
  9545. (defun org-refile (&optional goto default-buffer rfloc)
  9546. "Move the entry or entries at point to another heading.
  9547. The list of target headings is compiled using the information in
  9548. `org-refile-targets', which see.
  9549. At the target location, the entry is filed as a subitem of the target
  9550. heading. Depending on `org-reverse-note-order', the new subitem will
  9551. either be the first or the last subitem.
  9552. If there is an active region, all entries in that region will be moved.
  9553. However, the region must fulfill the requirement that the first heading
  9554. is the first one sets the top-level of the moved text - at most siblings
  9555. below it are allowed.
  9556. With prefix arg GOTO, the command will only visit the target location
  9557. and not actually move anything.
  9558. With a double prefix arg \\[universal-argument] \\[universal-argument], \
  9559. go to the location where the last refiling operation has put the subtree.
  9560. With a prefix argument of `2', refile to the running clock.
  9561. RFLOC can be a refile location obtained in a different way.
  9562. See also `org-refile-use-outline-path' and `org-completion-use-ido'.
  9563. If you are using target caching (see `org-refile-use-cache'),
  9564. You have to clear the target cache in order to find new targets.
  9565. This can be done with a 0 prefix (`C-0 C-c C-w') or a triple
  9566. prefix argument (`C-u C-u C-u C-c C-w')."
  9567. (interactive "P")
  9568. (if (member goto '(0 (64)))
  9569. (org-refile-cache-clear)
  9570. (let* ((cbuf (current-buffer))
  9571. (regionp (org-region-active-p))
  9572. (region-start (and regionp (region-beginning)))
  9573. (region-end (and regionp (region-end)))
  9574. (region-length (and regionp (- region-end region-start)))
  9575. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  9576. pos it nbuf file re level reversed)
  9577. (setq last-command nil)
  9578. (when regionp
  9579. (goto-char region-start)
  9580. (or (bolp) (goto-char (point-at-bol)))
  9581. (setq region-start (point))
  9582. (unless (or (org-kill-is-subtree-p
  9583. (buffer-substring region-start region-end))
  9584. (prog1 org-refile-active-region-within-subtree
  9585. (org-toggle-heading)))
  9586. (error "The region is not a (sequence of) subtree(s)")))
  9587. (if (equal goto '(16))
  9588. (org-refile-goto-last-stored)
  9589. (when (or
  9590. (and (equal goto 2)
  9591. org-clock-hd-marker (marker-buffer org-clock-hd-marker)
  9592. (prog1
  9593. (setq it (list (or org-clock-heading "running clock")
  9594. (buffer-file-name
  9595. (marker-buffer org-clock-hd-marker))
  9596. ""
  9597. (marker-position org-clock-hd-marker)))
  9598. (setq goto nil)))
  9599. (setq it (or rfloc
  9600. (let (heading-text)
  9601. (save-excursion
  9602. (unless goto
  9603. (org-back-to-heading t)
  9604. (setq heading-text
  9605. (nth 4 (org-heading-components))))
  9606. (org-refile-get-location
  9607. (cond (goto "Goto")
  9608. (regionp "Refile region to")
  9609. (t (concat "Refile subtree \""
  9610. heading-text "\" to")))
  9611. default-buffer
  9612. org-refile-allow-creating-parent-nodes
  9613. goto))))))
  9614. (setq file (nth 1 it)
  9615. re (nth 2 it)
  9616. pos (nth 3 it))
  9617. (if (and (not goto)
  9618. pos
  9619. (equal (buffer-file-name) file)
  9620. (if regionp
  9621. (and (>= pos region-start)
  9622. (<= pos region-end))
  9623. (and (>= pos (point))
  9624. (< pos (save-excursion
  9625. (org-end-of-subtree t t))))))
  9626. (error "Cannot refile to position inside the tree or region"))
  9627. (setq nbuf (or (find-buffer-visiting file)
  9628. (find-file-noselect file)))
  9629. (if goto
  9630. (progn
  9631. (org-pop-to-buffer-same-window nbuf)
  9632. (goto-char pos)
  9633. (org-show-context 'org-goto))
  9634. (if regionp
  9635. (progn
  9636. (org-kill-new (buffer-substring region-start region-end))
  9637. (org-save-markers-in-region region-start region-end))
  9638. (org-copy-subtree 1 nil t))
  9639. (with-current-buffer (setq nbuf (or (find-buffer-visiting file)
  9640. (find-file-noselect file)))
  9641. (setq reversed (org-notes-order-reversed-p))
  9642. (save-excursion
  9643. (save-restriction
  9644. (widen)
  9645. (if pos
  9646. (progn
  9647. (goto-char pos)
  9648. (looking-at org-outline-regexp)
  9649. (setq level (org-get-valid-level (funcall outline-level) 1))
  9650. (goto-char
  9651. (if reversed
  9652. (or (outline-next-heading) (point-max))
  9653. (or (save-excursion (org-get-next-sibling))
  9654. (org-end-of-subtree t t)
  9655. (point-max)))))
  9656. (setq level 1)
  9657. (if (not reversed)
  9658. (goto-char (point-max))
  9659. (goto-char (point-min))
  9660. (or (outline-next-heading) (goto-char (point-max)))))
  9661. (if (not (bolp)) (newline))
  9662. (org-paste-subtree level)
  9663. (when org-log-refile
  9664. (org-add-log-setup 'refile nil nil 'findpos
  9665. org-log-refile)
  9666. (unless (eq org-log-refile 'note)
  9667. (save-excursion (org-add-log-note))))
  9668. (and org-auto-align-tags (org-set-tags nil t))
  9669. (bookmark-set "org-refile-last-stored")
  9670. ;; If we are refiling for capture, make sure that the
  9671. ;; last-capture pointers point here
  9672. (when (org-bound-and-true-p org-refile-for-capture)
  9673. (bookmark-set "org-capture-last-stored-marker")
  9674. (move-marker org-capture-last-stored-marker (point)))
  9675. (if (fboundp 'deactivate-mark) (deactivate-mark))
  9676. (run-hooks 'org-after-refile-insert-hook))))
  9677. (if regionp
  9678. (delete-region (point) (+ (point) region-length))
  9679. (org-cut-subtree))
  9680. (when (featurep 'org-inlinetask)
  9681. (org-inlinetask-remove-END-maybe))
  9682. (setq org-markers-to-move nil)
  9683. (message "Refiled to \"%s\" in file %s" (car it) file)))))))
  9684. (defun org-refile-goto-last-stored ()
  9685. "Go to the location where the last refile was stored."
  9686. (interactive)
  9687. (bookmark-jump "org-refile-last-stored")
  9688. (message "This is the location of the last refile"))
  9689. (defun org-refile-get-location (&optional prompt default-buffer new-nodes
  9690. no-exclude)
  9691. "Prompt the user for a refile location, using PROMPT.
  9692. PROMPT should not be suffixed with a colon and a space, because
  9693. this function appends the default value from
  9694. `org-refile-history' automatically, if that is not empty.
  9695. When NO-EXCLUDE is set, do not exclude headlines in the current subtree,
  9696. this is used for the GOTO interface."
  9697. (let ((org-refile-targets org-refile-targets)
  9698. (org-refile-use-outline-path org-refile-use-outline-path)
  9699. excluded-entries)
  9700. (when (and (eq major-mode 'org-mode)
  9701. (not org-refile-use-cache)
  9702. (not no-exclude))
  9703. (org-map-tree
  9704. (lambda()
  9705. (setq excluded-entries
  9706. (append excluded-entries (list (org-get-heading t t)))))))
  9707. (setq org-refile-target-table
  9708. (org-refile-get-targets default-buffer excluded-entries)))
  9709. (unless org-refile-target-table
  9710. (error "No refile targets"))
  9711. (let* ((prompt (concat prompt
  9712. (and (car org-refile-history)
  9713. (concat " (default " (car org-refile-history) ")"))
  9714. ": "))
  9715. (cbuf (current-buffer))
  9716. (partial-completion-mode nil)
  9717. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  9718. (cfunc (if (and org-refile-use-outline-path
  9719. org-outline-path-complete-in-steps)
  9720. 'org-olpath-completing-read
  9721. 'org-icompleting-read))
  9722. (extra (if org-refile-use-outline-path "/" ""))
  9723. (filename (and cfn (expand-file-name cfn)))
  9724. (tbl (mapcar
  9725. (lambda (x)
  9726. (if (and (not (member org-refile-use-outline-path
  9727. '(file full-file-path)))
  9728. (not (equal filename (nth 1 x))))
  9729. (cons (concat (car x) extra " ("
  9730. (file-name-nondirectory (nth 1 x)) ")")
  9731. (cdr x))
  9732. (cons (concat (car x) extra) (cdr x))))
  9733. org-refile-target-table))
  9734. (completion-ignore-case t)
  9735. pa answ parent-target child parent old-hist)
  9736. (setq old-hist org-refile-history)
  9737. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  9738. nil 'org-refile-history (car org-refile-history)))
  9739. (setq pa (or (assoc answ tbl) (assoc (concat answ "/") tbl)))
  9740. (org-refile-check-position pa)
  9741. (if pa
  9742. (progn
  9743. (when (or (not org-refile-history)
  9744. (not (eq old-hist org-refile-history))
  9745. (not (equal (car pa) (car org-refile-history))))
  9746. (setq org-refile-history
  9747. (cons (car pa) (if (assoc (car org-refile-history) tbl)
  9748. org-refile-history
  9749. (cdr org-refile-history))))
  9750. (if (equal (car org-refile-history) (nth 1 org-refile-history))
  9751. (pop org-refile-history)))
  9752. pa)
  9753. (if (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  9754. (progn
  9755. (setq parent (match-string 1 answ)
  9756. child (match-string 2 answ))
  9757. (setq parent-target (or (assoc parent tbl)
  9758. (assoc (concat parent "/") tbl)))
  9759. (when (and parent-target
  9760. (or (eq new-nodes t)
  9761. (and (eq new-nodes 'confirm)
  9762. (y-or-n-p (format "Create new node \"%s\"? "
  9763. child)))))
  9764. (org-refile-new-child parent-target child)))
  9765. (error "Invalid target location")))))
  9766. (defun org-refile-check-position (refile-pointer)
  9767. "Check if the refile pointer matches the readline to which it points."
  9768. (let* ((file (nth 1 refile-pointer))
  9769. (re (nth 2 refile-pointer))
  9770. (pos (nth 3 refile-pointer))
  9771. buffer)
  9772. (when (org-string-nw-p re)
  9773. (setq buffer (if (markerp pos)
  9774. (marker-buffer pos)
  9775. (or (find-buffer-visiting file)
  9776. (find-file-noselect file))))
  9777. (with-current-buffer buffer
  9778. (save-excursion
  9779. (save-restriction
  9780. (widen)
  9781. (goto-char pos)
  9782. (beginning-of-line 1)
  9783. (unless (org-looking-at-p re)
  9784. (error "Invalid refile position, please clear the cache with `C-0 C-c C-w' before refiling"))))))))
  9785. (defun org-refile-new-child (parent-target child)
  9786. "Use refile target PARENT-TARGET to add new CHILD below it."
  9787. (unless parent-target
  9788. (error "Cannot find parent for new node"))
  9789. (let ((file (nth 1 parent-target))
  9790. (pos (nth 3 parent-target))
  9791. level)
  9792. (with-current-buffer (or (find-buffer-visiting file)
  9793. (find-file-noselect file))
  9794. (save-excursion
  9795. (save-restriction
  9796. (widen)
  9797. (if pos
  9798. (goto-char pos)
  9799. (goto-char (point-max))
  9800. (if (not (bolp)) (newline)))
  9801. (when (looking-at org-outline-regexp)
  9802. (setq level (funcall outline-level))
  9803. (org-end-of-subtree t t))
  9804. (org-back-over-empty-lines)
  9805. (insert "\n" (make-string
  9806. (if pos (org-get-valid-level level 1) 1) ?*)
  9807. " " child "\n")
  9808. (beginning-of-line 0)
  9809. (list (concat (car parent-target) "/" child) file "" (point)))))))
  9810. (defun org-olpath-completing-read (prompt collection &rest args)
  9811. "Read an outline path like a file name."
  9812. (let ((thetable collection)
  9813. (org-completion-use-ido nil) ; does not work with ido.
  9814. (org-completion-use-iswitchb nil)) ; or iswitchb
  9815. (apply
  9816. 'org-icompleting-read prompt
  9817. (lambda (string predicate &optional flag)
  9818. (let (rtn r f (l (length string)))
  9819. (cond
  9820. ((eq flag nil)
  9821. ;; try completion
  9822. (try-completion string thetable))
  9823. ((eq flag t)
  9824. ;; all-completions
  9825. (setq rtn (all-completions string thetable predicate))
  9826. (mapcar
  9827. (lambda (x)
  9828. (setq r (substring x l))
  9829. (if (string-match " ([^)]*)$" x)
  9830. (setq f (match-string 0 x))
  9831. (setq f ""))
  9832. (if (string-match "/" r)
  9833. (concat string (substring r 0 (match-end 0)) f)
  9834. x))
  9835. rtn))
  9836. ((eq flag 'lambda)
  9837. ;; exact match?
  9838. (assoc string thetable)))
  9839. ))
  9840. args)))
  9841. ;;;; Dynamic blocks
  9842. (defun org-find-dblock (name)
  9843. "Find the first dynamic block with name NAME in the buffer.
  9844. If not found, stay at current position and return nil."
  9845. (let (pos)
  9846. (save-excursion
  9847. (goto-char (point-min))
  9848. (setq pos (and (re-search-forward (concat "^[ \t]*#\\+BEGIN:[ \t]+" name "\\>")
  9849. nil t)
  9850. (match-beginning 0))))
  9851. (if pos (goto-char pos))
  9852. pos))
  9853. (defconst org-dblock-start-re
  9854. "^[ \t]*#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  9855. "Matches the start line of a dynamic block, with parameters.")
  9856. (defconst org-dblock-end-re "^[ \t]*#\\+END\\([: \t\r\n]\\|$\\)"
  9857. "Matches the end of a dynamic block.")
  9858. (defun org-create-dblock (plist)
  9859. "Create a dynamic block section, with parameters taken from PLIST.
  9860. PLIST must contain a :name entry which is used as name of the block."
  9861. (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
  9862. (end-of-line 1)
  9863. (newline))
  9864. (let ((col (current-column))
  9865. (name (plist-get plist :name)))
  9866. (insert "#+BEGIN: " name)
  9867. (while plist
  9868. (if (eq (car plist) :name)
  9869. (setq plist (cddr plist))
  9870. (insert " " (prin1-to-string (pop plist)))))
  9871. (insert "\n\n" (make-string col ?\ ) "#+END:\n")
  9872. (beginning-of-line -2)))
  9873. (defun org-prepare-dblock ()
  9874. "Prepare dynamic block for refresh.
  9875. This empties the block, puts the cursor at the insert position and returns
  9876. the property list including an extra property :name with the block name."
  9877. (unless (looking-at org-dblock-start-re)
  9878. (error "Not at a dynamic block"))
  9879. (let* ((begdel (1+ (match-end 0)))
  9880. (name (org-no-properties (match-string 1)))
  9881. (params (append (list :name name)
  9882. (read (concat "(" (match-string 3) ")")))))
  9883. (save-excursion
  9884. (beginning-of-line 1)
  9885. (skip-chars-forward " \t")
  9886. (setq params (plist-put params :indentation-column (current-column))))
  9887. (unless (re-search-forward org-dblock-end-re nil t)
  9888. (error "Dynamic block not terminated"))
  9889. (setq params
  9890. (append params
  9891. (list :content (buffer-substring
  9892. begdel (match-beginning 0)))))
  9893. (delete-region begdel (match-beginning 0))
  9894. (goto-char begdel)
  9895. (open-line 1)
  9896. params))
  9897. (defun org-map-dblocks (&optional command)
  9898. "Apply COMMAND to all dynamic blocks in the current buffer.
  9899. If COMMAND is not given, use `org-update-dblock'."
  9900. (let ((cmd (or command 'org-update-dblock)))
  9901. (save-excursion
  9902. (goto-char (point-min))
  9903. (while (re-search-forward org-dblock-start-re nil t)
  9904. (goto-char (match-beginning 0))
  9905. (save-excursion
  9906. (condition-case nil
  9907. (funcall cmd)
  9908. (error (message "Error during update of dynamic block"))))
  9909. (unless (re-search-forward org-dblock-end-re nil t)
  9910. (error "Dynamic block not terminated"))))))
  9911. (defun org-dblock-update (&optional arg)
  9912. "User command for updating dynamic blocks.
  9913. Update the dynamic block at point. With prefix ARG, update all dynamic
  9914. blocks in the buffer."
  9915. (interactive "P")
  9916. (if arg
  9917. (org-update-all-dblocks)
  9918. (or (looking-at org-dblock-start-re)
  9919. (org-beginning-of-dblock))
  9920. (org-update-dblock)))
  9921. (defun org-update-dblock ()
  9922. "Update the dynamic block at point.
  9923. This means to empty the block, parse for parameters and then call
  9924. the correct writing function."
  9925. (interactive)
  9926. (save-window-excursion
  9927. (let* ((pos (point))
  9928. (line (org-current-line))
  9929. (params (org-prepare-dblock))
  9930. (name (plist-get params :name))
  9931. (indent (plist-get params :indentation-column))
  9932. (cmd (intern (concat "org-dblock-write:" name))))
  9933. (message "Updating dynamic block `%s' at line %d..." name line)
  9934. (funcall cmd params)
  9935. (message "Updating dynamic block `%s' at line %d...done" name line)
  9936. (goto-char pos)
  9937. (when (and indent (> indent 0))
  9938. (setq indent (make-string indent ?\ ))
  9939. (save-excursion
  9940. (org-beginning-of-dblock)
  9941. (forward-line 1)
  9942. (while (not (looking-at org-dblock-end-re))
  9943. (insert indent)
  9944. (beginning-of-line 2))
  9945. (when (looking-at org-dblock-end-re)
  9946. (and (looking-at "[ \t]+")
  9947. (replace-match ""))
  9948. (insert indent)))))))
  9949. (defun org-beginning-of-dblock ()
  9950. "Find the beginning of the dynamic block at point.
  9951. Error if there is no such block at point."
  9952. (let ((pos (point))
  9953. beg)
  9954. (end-of-line 1)
  9955. (if (and (re-search-backward org-dblock-start-re nil t)
  9956. (setq beg (match-beginning 0))
  9957. (re-search-forward org-dblock-end-re nil t)
  9958. (> (match-end 0) pos))
  9959. (goto-char beg)
  9960. (goto-char pos)
  9961. (error "Not in a dynamic block"))))
  9962. (defun org-update-all-dblocks ()
  9963. "Update all dynamic blocks in the buffer.
  9964. This function can be used in a hook."
  9965. (interactive)
  9966. (when (eq major-mode 'org-mode)
  9967. (org-map-dblocks 'org-update-dblock)))
  9968. ;;;; Completion
  9969. (defconst org-additional-option-like-keywords
  9970. '("BEGIN_HTML" "END_HTML" "HTML:" "ATTR_HTML:"
  9971. "BEGIN_DocBook" "END_DocBook" "DocBook:" "ATTR_DocBook:"
  9972. "BEGIN_LaTeX" "END_LaTeX" "LaTeX:" "LATEX_HEADER:"
  9973. "LATEX_CLASS:" "LATEX_CLASS_OPTIONS:" "ATTR_LaTeX:"
  9974. "BEGIN:" "END:"
  9975. "ORGTBL" "TBLFM:" "TBLNAME:"
  9976. "BEGIN_EXAMPLE" "END_EXAMPLE"
  9977. "BEGIN_QUOTE" "END_QUOTE"
  9978. "BEGIN_VERSE" "END_VERSE"
  9979. "BEGIN_CENTER" "END_CENTER"
  9980. "BEGIN_SRC" "END_SRC"
  9981. "BEGIN_RESULT" "END_RESULT"
  9982. "NAME:" "RESULTS:"
  9983. "HEADER:" "HEADERS:"
  9984. "CATEGORY:" "COLUMNS:" "PROPERTY:"
  9985. "CAPTION:" "LABEL:"
  9986. "SETUPFILE:"
  9987. "INCLUDE:"
  9988. "BIND:"
  9989. "MACRO:"))
  9990. (defcustom org-structure-template-alist
  9991. '(
  9992. ("s" "#+BEGIN_SRC ?\n\n#+END_SRC"
  9993. "<src lang=\"?\">\n\n</src>")
  9994. ("e" "#+BEGIN_EXAMPLE\n?\n#+END_EXAMPLE"
  9995. "<example>\n?\n</example>")
  9996. ("q" "#+BEGIN_QUOTE\n?\n#+END_QUOTE"
  9997. "<quote>\n?\n</quote>")
  9998. ("v" "#+BEGIN_VERSE\n?\n#+END_VERSE"
  9999. "<verse>\n?\n</verse>")
  10000. ("c" "#+BEGIN_CENTER\n?\n#+END_CENTER"
  10001. "<center>\n?\n</center>")
  10002. ("l" "#+BEGIN_LaTeX\n?\n#+END_LaTeX"
  10003. "<literal style=\"latex\">\n?\n</literal>")
  10004. ("L" "#+LaTeX: "
  10005. "<literal style=\"latex\">?</literal>")
  10006. ("h" "#+BEGIN_HTML\n?\n#+END_HTML"
  10007. "<literal style=\"html\">\n?\n</literal>")
  10008. ("H" "#+HTML: "
  10009. "<literal style=\"html\">?</literal>")
  10010. ("a" "#+BEGIN_ASCII\n?\n#+END_ASCII")
  10011. ("A" "#+ASCII: ")
  10012. ("i" "#+INDEX: ?"
  10013. "#+INDEX: ?")
  10014. ("I" "#+INCLUDE %file ?"
  10015. "<include file=%file markup=\"?\">")
  10016. )
  10017. "Structure completion elements.
  10018. This is a list of abbreviation keys and values. The value gets inserted
  10019. if you type `<' followed by the key and then press the completion key,
  10020. usually `M-TAB'. %file will be replaced by a file name after prompting
  10021. for the file using completion. The cursor will be placed at the position
  10022. of the `?` in the template.
  10023. There are two templates for each key, the first uses the original Org syntax,
  10024. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  10025. the default when the /org-mtags.el/ module has been loaded. See also the
  10026. variable `org-mtags-prefer-muse-templates'."
  10027. :group 'org-completion
  10028. :type '(repeat
  10029. (string :tag "Key")
  10030. (string :tag "Template")
  10031. (string :tag "Muse Template")))
  10032. (defun org-try-structure-completion ()
  10033. "Try to complete a structure template before point.
  10034. This looks for strings like \"<e\" on an otherwise empty line and
  10035. expands them."
  10036. (let ((l (buffer-substring (point-at-bol) (point)))
  10037. a)
  10038. (when (and (looking-at "[ \t]*$")
  10039. (string-match "^[ \t]*<\\([a-zA-Z]+\\)$" l)
  10040. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  10041. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  10042. (match-beginning 1)) a)
  10043. t)))
  10044. (defun org-complete-expand-structure-template (start cell)
  10045. "Expand a structure template."
  10046. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  10047. (rpl (nth (if musep 2 1) cell))
  10048. (ind ""))
  10049. (delete-region start (point))
  10050. (when (string-match "\\`#\\+" rpl)
  10051. (cond
  10052. ((bolp))
  10053. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  10054. (setq ind (buffer-substring (point-at-bol) (point))))
  10055. (t (newline))))
  10056. (setq start (point))
  10057. (if (string-match "%file" rpl)
  10058. (setq rpl (replace-match
  10059. (concat
  10060. "\""
  10061. (save-match-data
  10062. (abbreviate-file-name (read-file-name "Include file: ")))
  10063. "\"")
  10064. t t rpl)))
  10065. (setq rpl (mapconcat 'identity (split-string rpl "\n")
  10066. (concat "\n" ind)))
  10067. (insert rpl)
  10068. (if (re-search-backward "\\?" start t) (delete-char 1))))
  10069. ;;;; TODO, DEADLINE, Comments
  10070. (defun org-toggle-comment ()
  10071. "Change the COMMENT state of an entry."
  10072. (interactive)
  10073. (save-excursion
  10074. (org-back-to-heading)
  10075. (let (case-fold-search)
  10076. (cond
  10077. ((looking-at (format org-heading-keyword-regexp-format
  10078. org-comment-string))
  10079. (goto-char (match-end 1))
  10080. (looking-at (concat " +" org-comment-string))
  10081. (replace-match "" t t)
  10082. (when (eolp) (insert " ")))
  10083. ((looking-at org-outline-regexp)
  10084. (goto-char (match-end 0))
  10085. (insert org-comment-string " "))))))
  10086. (defvar org-last-todo-state-is-todo nil
  10087. "This is non-nil when the last TODO state change led to a TODO state.
  10088. If the last change removed the TODO tag or switched to DONE, then
  10089. this is nil.")
  10090. (defvar org-setting-tags nil) ; dynamically skipped
  10091. (defvar org-todo-setup-filter-hook nil
  10092. "Hook for functions that pre-filter todo specs.
  10093. Each function takes a todo spec and returns either nil or the spec
  10094. transformed into canonical form." )
  10095. (defvar org-todo-get-default-hook nil
  10096. "Hook for functions that get a default item for todo.
  10097. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  10098. nil or a string to be used for the todo mark." )
  10099. (defvar org-agenda-headline-snapshot-before-repeat)
  10100. (defun org-current-effective-time ()
  10101. "Return current time adjusted for `org-extend-today-until' variable"
  10102. (let* ((ct (org-current-time))
  10103. (dct (decode-time ct))
  10104. (ct1
  10105. (if (and org-use-effective-time
  10106. (< (nth 2 dct) org-extend-today-until))
  10107. (encode-time 0 59 23 (1- (nth 3 dct)) (nth 4 dct) (nth 5 dct))
  10108. ct)))
  10109. ct1))
  10110. (defun org-todo-yesterday (&optional arg)
  10111. "Like `org-todo' but the time of change will be 23:59 of yesterday."
  10112. (interactive "P")
  10113. (if (eq major-mode 'org-agenda-mode)
  10114. (apply 'org-agenda-todo-yesterday arg)
  10115. (let* ((hour (third (decode-time
  10116. (org-current-time))))
  10117. (org-extend-today-until (1+ hour)))
  10118. (org-todo arg))))
  10119. (defun org-todo (&optional arg)
  10120. "Change the TODO state of an item.
  10121. The state of an item is given by a keyword at the start of the heading,
  10122. like
  10123. *** TODO Write paper
  10124. *** DONE Call mom
  10125. The different keywords are specified in the variable `org-todo-keywords'.
  10126. By default the available states are \"TODO\" and \"DONE\".
  10127. So for this example: when the item starts with TODO, it is changed to DONE.
  10128. When it starts with DONE, the DONE is removed. And when neither TODO nor
  10129. DONE are present, add TODO at the beginning of the heading.
  10130. With \\[universal-argument] prefix arg, use completion to determine the new \
  10131. state.
  10132. With numeric prefix arg, switch to that state.
  10133. With a double \\[universal-argument] prefix, switch to the next set of TODO \
  10134. keywords (nextset).
  10135. With a triple \\[universal-argument] prefix, circumvent any state blocking.
  10136. With a numeric prefix arg of 0, inhibit note taking for the change.
  10137. For calling through lisp, arg is also interpreted in the following way:
  10138. 'none -> empty state
  10139. \"\"(empty string) -> switch to empty state
  10140. 'done -> switch to DONE
  10141. 'nextset -> switch to the next set of keywords
  10142. 'previousset -> switch to the previous set of keywords
  10143. \"WAITING\" -> switch to the specified keyword, but only if it
  10144. really is a member of `org-todo-keywords'."
  10145. (interactive "P")
  10146. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  10147. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  10148. 'region-start-level 'region))
  10149. org-loop-over-headlines-in-active-region)
  10150. (org-map-entries
  10151. `(org-todo ,arg)
  10152. org-loop-over-headlines-in-active-region
  10153. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  10154. (if (equal arg '(16)) (setq arg 'nextset))
  10155. (let ((org-blocker-hook org-blocker-hook)
  10156. (case-fold-search nil))
  10157. (when (equal arg '(64))
  10158. (setq arg nil org-blocker-hook nil))
  10159. (when (and org-blocker-hook
  10160. (or org-inhibit-blocking
  10161. (org-entry-get nil "NOBLOCKING")))
  10162. (setq org-blocker-hook nil))
  10163. (save-excursion
  10164. (catch 'exit
  10165. (org-back-to-heading t)
  10166. (if (looking-at org-outline-regexp) (goto-char (1- (match-end 0))))
  10167. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|[ \t]*$\\)"))
  10168. (looking-at "\\(?: *\\|[ \t]*$\\)"))
  10169. (let* ((match-data (match-data))
  10170. (startpos (point-at-bol))
  10171. (logging (save-match-data (org-entry-get nil "LOGGING" t t)))
  10172. (org-log-done org-log-done)
  10173. (org-log-repeat org-log-repeat)
  10174. (org-todo-log-states org-todo-log-states)
  10175. (org-inhibit-logging
  10176. (if (equal arg 0)
  10177. (progn (setq arg nil) 'note) org-inhibit-logging))
  10178. (this (match-string 1))
  10179. (hl-pos (match-beginning 0))
  10180. (head (org-get-todo-sequence-head this))
  10181. (ass (assoc head org-todo-kwd-alist))
  10182. (interpret (nth 1 ass))
  10183. (done-word (nth 3 ass))
  10184. (final-done-word (nth 4 ass))
  10185. (last-state (or this ""))
  10186. (completion-ignore-case t)
  10187. (member (member this org-todo-keywords-1))
  10188. (tail (cdr member))
  10189. (state (cond
  10190. ((and org-todo-key-trigger
  10191. (or (and (equal arg '(4))
  10192. (eq org-use-fast-todo-selection 'prefix))
  10193. (and (not arg) org-use-fast-todo-selection
  10194. (not (eq org-use-fast-todo-selection
  10195. 'prefix)))))
  10196. ;; Use fast selection
  10197. (org-fast-todo-selection))
  10198. ((and (equal arg '(4))
  10199. (or (not org-use-fast-todo-selection)
  10200. (not org-todo-key-trigger)))
  10201. ;; Read a state with completion
  10202. (org-icompleting-read
  10203. "State: " (mapcar (lambda(x) (list x))
  10204. org-todo-keywords-1)
  10205. nil t))
  10206. ((eq arg 'right)
  10207. (if this
  10208. (if tail (car tail) nil)
  10209. (car org-todo-keywords-1)))
  10210. ((eq arg 'left)
  10211. (if (equal member org-todo-keywords-1)
  10212. nil
  10213. (if this
  10214. (nth (- (length org-todo-keywords-1)
  10215. (length tail) 2)
  10216. org-todo-keywords-1)
  10217. (org-last org-todo-keywords-1))))
  10218. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  10219. (setq arg nil))) ; hack to fall back to cycling
  10220. (arg
  10221. ;; user or caller requests a specific state
  10222. (cond
  10223. ((equal arg "") nil)
  10224. ((eq arg 'none) nil)
  10225. ((eq arg 'done) (or done-word (car org-done-keywords)))
  10226. ((eq arg 'nextset)
  10227. (or (car (cdr (member head org-todo-heads)))
  10228. (car org-todo-heads)))
  10229. ((eq arg 'previousset)
  10230. (let ((org-todo-heads (reverse org-todo-heads)))
  10231. (or (car (cdr (member head org-todo-heads)))
  10232. (car org-todo-heads))))
  10233. ((car (member arg org-todo-keywords-1)))
  10234. ((stringp arg)
  10235. (error "State `%s' not valid in this file" arg))
  10236. ((nth (1- (prefix-numeric-value arg))
  10237. org-todo-keywords-1))))
  10238. ((null member) (or head (car org-todo-keywords-1)))
  10239. ((equal this final-done-word) nil) ;; -> make empty
  10240. ((null tail) nil) ;; -> first entry
  10241. ((memq interpret '(type priority))
  10242. (if (eq this-command last-command)
  10243. (car tail)
  10244. (if (> (length tail) 0)
  10245. (or done-word (car org-done-keywords))
  10246. nil)))
  10247. (t
  10248. (car tail))))
  10249. (state (or
  10250. (run-hook-with-args-until-success
  10251. 'org-todo-get-default-hook state last-state)
  10252. state))
  10253. (next (if state (concat " " state " ") " "))
  10254. (change-plist (list :type 'todo-state-change :from this :to state
  10255. :position startpos))
  10256. dolog now-done-p)
  10257. (when org-blocker-hook
  10258. (setq org-last-todo-state-is-todo
  10259. (not (member this org-done-keywords)))
  10260. (unless (save-excursion
  10261. (save-match-data
  10262. (org-with-wide-buffer
  10263. (run-hook-with-args-until-failure
  10264. 'org-blocker-hook change-plist))))
  10265. (if (org-called-interactively-p 'interactive)
  10266. (error "TODO state change from %s to %s blocked" this state)
  10267. ;; fail silently
  10268. (message "TODO state change from %s to %s blocked" this state)
  10269. (throw 'exit nil))))
  10270. (store-match-data match-data)
  10271. (replace-match next t t)
  10272. (unless (pos-visible-in-window-p hl-pos)
  10273. (message "TODO state changed to %s" (org-trim next)))
  10274. (unless head
  10275. (setq head (org-get-todo-sequence-head state)
  10276. ass (assoc head org-todo-kwd-alist)
  10277. interpret (nth 1 ass)
  10278. done-word (nth 3 ass)
  10279. final-done-word (nth 4 ass)))
  10280. (when (memq arg '(nextset previousset))
  10281. (message "Keyword-Set %d/%d: %s"
  10282. (- (length org-todo-sets) -1
  10283. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  10284. (length org-todo-sets)
  10285. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  10286. (setq org-last-todo-state-is-todo
  10287. (not (member state org-done-keywords)))
  10288. (setq now-done-p (and (member state org-done-keywords)
  10289. (not (member this org-done-keywords))))
  10290. (and logging (org-local-logging logging))
  10291. (when (and (or org-todo-log-states org-log-done)
  10292. (not (eq org-inhibit-logging t))
  10293. (not (memq arg '(nextset previousset))))
  10294. ;; we need to look at recording a time and note
  10295. (setq dolog (or (nth 1 (assoc state org-todo-log-states))
  10296. (nth 2 (assoc this org-todo-log-states))))
  10297. (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
  10298. (setq dolog 'time))
  10299. (when (and state
  10300. (member state org-not-done-keywords)
  10301. (not (member this org-not-done-keywords)))
  10302. ;; This is now a todo state and was not one before
  10303. ;; If there was a CLOSED time stamp, get rid of it.
  10304. (org-add-planning-info nil nil 'closed))
  10305. (when (and now-done-p org-log-done)
  10306. ;; It is now done, and it was not done before
  10307. (org-add-planning-info 'closed (org-current-effective-time))
  10308. (if (and (not dolog) (eq 'note org-log-done))
  10309. (org-add-log-setup 'done state this 'findpos 'note)))
  10310. (when (and state dolog)
  10311. ;; This is a non-nil state, and we need to log it
  10312. (org-add-log-setup 'state state this 'findpos dolog)))
  10313. ;; Fixup tag positioning
  10314. (org-todo-trigger-tag-changes state)
  10315. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  10316. (when org-provide-todo-statistics
  10317. (org-update-parent-todo-statistics))
  10318. (run-hooks 'org-after-todo-state-change-hook)
  10319. (if (and arg (not (member state org-done-keywords)))
  10320. (setq head (org-get-todo-sequence-head state)))
  10321. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  10322. ;; Do we need to trigger a repeat?
  10323. (when now-done-p
  10324. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  10325. ;; This is for the agenda, take a snapshot of the headline.
  10326. (save-match-data
  10327. (setq org-agenda-headline-snapshot-before-repeat
  10328. (org-get-heading))))
  10329. (org-auto-repeat-maybe state))
  10330. ;; Fixup cursor location if close to the keyword
  10331. (if (and (outline-on-heading-p)
  10332. (not (bolp))
  10333. (save-excursion (beginning-of-line 1)
  10334. (looking-at org-todo-line-regexp))
  10335. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  10336. (progn
  10337. (goto-char (or (match-end 2) (match-end 1)))
  10338. (and (looking-at " ") (just-one-space))))
  10339. (when org-trigger-hook
  10340. (save-excursion
  10341. (run-hook-with-args 'org-trigger-hook change-plist)))))))))
  10342. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  10343. "Block turning an entry into a TODO, using the hierarchy.
  10344. This checks whether the current task should be blocked from state
  10345. changes. Such blocking occurs when:
  10346. 1. The task has children which are not all in a completed state.
  10347. 2. A task has a parent with the property :ORDERED:, and there
  10348. are siblings prior to the current task with incomplete
  10349. status.
  10350. 3. The parent of the task is blocked because it has siblings that should
  10351. be done first, or is child of a block grandparent TODO entry."
  10352. (if (not org-enforce-todo-dependencies)
  10353. t ; if locally turned off don't block
  10354. (catch 'dont-block
  10355. ;; If this is not a todo state change, or if this entry is already DONE,
  10356. ;; do not block
  10357. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  10358. (member (plist-get change-plist :from)
  10359. (cons 'done org-done-keywords))
  10360. (member (plist-get change-plist :to)
  10361. (cons 'todo org-not-done-keywords))
  10362. (not (plist-get change-plist :to)))
  10363. (throw 'dont-block t))
  10364. ;; If this task has children, and any are undone, it's blocked
  10365. (save-excursion
  10366. (org-back-to-heading t)
  10367. (let ((this-level (funcall outline-level)))
  10368. (outline-next-heading)
  10369. (let ((child-level (funcall outline-level)))
  10370. (while (and (not (eobp))
  10371. (> child-level this-level))
  10372. ;; this todo has children, check whether they are all
  10373. ;; completed
  10374. (if (and (not (org-entry-is-done-p))
  10375. (org-entry-is-todo-p))
  10376. (throw 'dont-block nil))
  10377. (outline-next-heading)
  10378. (setq child-level (funcall outline-level))))))
  10379. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  10380. ;; any previous siblings are undone, it's blocked
  10381. (save-excursion
  10382. (org-back-to-heading t)
  10383. (let* ((pos (point))
  10384. (parent-pos (and (org-up-heading-safe) (point))))
  10385. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  10386. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  10387. (forward-line 1)
  10388. (re-search-forward org-not-done-heading-regexp pos t))
  10389. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  10390. ;; Search further up the hierarchy, to see if an ancestor is blocked
  10391. (while t
  10392. (goto-char parent-pos)
  10393. (if (not (looking-at org-not-done-heading-regexp))
  10394. (throw 'dont-block t)) ; do not block, parent is not a TODO
  10395. (setq pos (point))
  10396. (setq parent-pos (and (org-up-heading-safe) (point)))
  10397. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  10398. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  10399. (forward-line 1)
  10400. (re-search-forward org-not-done-heading-regexp pos t))
  10401. (throw 'dont-block nil)))))))) ; block, older sibling not done.
  10402. (defcustom org-track-ordered-property-with-tag nil
  10403. "Should the ORDERED property also be shown as a tag?
  10404. The ORDERED property decides if an entry should require subtasks to be
  10405. completed in sequence. Since a property is not very visible, setting
  10406. this option means that toggling the ORDERED property with the command
  10407. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  10408. not relevant for the behavior, but it makes things more visible.
  10409. Note that toggling the tag with tags commands will not change the property
  10410. and therefore not influence behavior!
  10411. This can be t, meaning the tag ORDERED should be used, It can also be a
  10412. string to select a different tag for this task."
  10413. :group 'org-todo
  10414. :type '(choice
  10415. (const :tag "No tracking" nil)
  10416. (const :tag "Track with ORDERED tag" t)
  10417. (string :tag "Use other tag")))
  10418. (defun org-toggle-ordered-property ()
  10419. "Toggle the ORDERED property of the current entry.
  10420. For better visibility, you can track the value of this property with a tag.
  10421. See variable `org-track-ordered-property-with-tag'."
  10422. (interactive)
  10423. (let* ((t1 org-track-ordered-property-with-tag)
  10424. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  10425. (save-excursion
  10426. (org-back-to-heading)
  10427. (if (org-entry-get nil "ORDERED")
  10428. (progn
  10429. (org-delete-property "ORDERED")
  10430. (and tag (org-toggle-tag tag 'off))
  10431. (message "Subtasks can be completed in arbitrary order"))
  10432. (org-entry-put nil "ORDERED" "t")
  10433. (and tag (org-toggle-tag tag 'on))
  10434. (message "Subtasks must be completed in sequence")))))
  10435. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  10436. (defun org-block-todo-from-checkboxes (change-plist)
  10437. "Block turning an entry into a TODO, using checkboxes.
  10438. This checks whether the current task should be blocked from state
  10439. changes because there are unchecked boxes in this entry."
  10440. (if (not org-enforce-todo-checkbox-dependencies)
  10441. t ; if locally turned off don't block
  10442. (catch 'dont-block
  10443. ;; If this is not a todo state change, or if this entry is already DONE,
  10444. ;; do not block
  10445. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  10446. (member (plist-get change-plist :from)
  10447. (cons 'done org-done-keywords))
  10448. (member (plist-get change-plist :to)
  10449. (cons 'todo org-not-done-keywords))
  10450. (not (plist-get change-plist :to)))
  10451. (throw 'dont-block t))
  10452. ;; If this task has checkboxes that are not checked, it's blocked
  10453. (save-excursion
  10454. (org-back-to-heading t)
  10455. (let ((beg (point)) end)
  10456. (outline-next-heading)
  10457. (setq end (point))
  10458. (goto-char beg)
  10459. (if (org-list-search-forward
  10460. (concat (org-item-beginning-re)
  10461. "\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?"
  10462. "\\[[- ]\\]")
  10463. end t)
  10464. (progn
  10465. (if (boundp 'org-blocked-by-checkboxes)
  10466. (setq org-blocked-by-checkboxes t))
  10467. (throw 'dont-block nil)))))
  10468. t))) ; do not block
  10469. (defun org-entry-blocked-p ()
  10470. "Is the current entry blocked?"
  10471. (if (org-entry-get nil "NOBLOCKING")
  10472. nil ;; Never block this entry
  10473. (not
  10474. (run-hook-with-args-until-failure
  10475. 'org-blocker-hook
  10476. (list :type 'todo-state-change
  10477. :position (point)
  10478. :from 'todo
  10479. :to 'done)))))
  10480. (defun org-update-statistics-cookies (all)
  10481. "Update the statistics cookie, either from TODO or from checkboxes.
  10482. This should be called with the cursor in a line with a statistics cookie."
  10483. (interactive "P")
  10484. (if all
  10485. (progn
  10486. (org-update-checkbox-count 'all)
  10487. (org-map-entries 'org-update-parent-todo-statistics))
  10488. (if (not (org-at-heading-p))
  10489. (org-update-checkbox-count)
  10490. (let ((pos (move-marker (make-marker) (point)))
  10491. end l1 l2)
  10492. (ignore-errors (org-back-to-heading t))
  10493. (if (not (org-at-heading-p))
  10494. (org-update-checkbox-count)
  10495. (setq l1 (org-outline-level))
  10496. (setq end (save-excursion
  10497. (outline-next-heading)
  10498. (if (org-at-heading-p) (setq l2 (org-outline-level)))
  10499. (point)))
  10500. (if (and (save-excursion
  10501. (re-search-forward
  10502. "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) \\[[- X]\\]" end t))
  10503. (not (save-excursion (re-search-forward
  10504. ":COOKIE_DATA:.*\\<todo\\>" end t))))
  10505. (org-update-checkbox-count)
  10506. (if (and l2 (> l2 l1))
  10507. (progn
  10508. (goto-char end)
  10509. (org-update-parent-todo-statistics))
  10510. (goto-char pos)
  10511. (beginning-of-line 1)
  10512. (while (re-search-forward
  10513. "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)"
  10514. (point-at-eol) t)
  10515. (replace-match (if (match-end 2) "[100%]" "[0/0]") t t)))))
  10516. (goto-char pos)
  10517. (move-marker pos nil)))))
  10518. (defvar org-entry-property-inherited-from) ;; defined below
  10519. (defun org-update-parent-todo-statistics ()
  10520. "Update any statistics cookie in the parent of the current headline.
  10521. When `org-hierarchical-todo-statistics' is nil, statistics will cover
  10522. the entire subtree and this will travel up the hierarchy and update
  10523. statistics everywhere."
  10524. (let* ((prop (save-excursion (org-up-heading-safe)
  10525. (org-entry-get nil "COOKIE_DATA" 'inherit)))
  10526. (recursive (or (not org-hierarchical-todo-statistics)
  10527. (and prop (string-match "\\<recursive\\>" prop))))
  10528. (lim (or (and prop (marker-position org-entry-property-inherited-from))
  10529. 0))
  10530. (first t)
  10531. (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  10532. level ltoggle l1 new ndel
  10533. (cnt-all 0) (cnt-done 0) is-percent kwd
  10534. checkbox-beg ov ovs ove cookie-present)
  10535. (catch 'exit
  10536. (save-excursion
  10537. (beginning-of-line 1)
  10538. (setq ltoggle (funcall outline-level))
  10539. ;; Three situations are to consider:
  10540. ;; 1. if `org-hierarchical-todo-statistics' is nil, repeat up
  10541. ;; to the top-level ancestor on the headline;
  10542. ;; 2. If parent has "recursive" property, repeat up to the
  10543. ;; headline setting that property, taking inheritance into
  10544. ;; account;
  10545. ;; 3. Else, move up to direct parent and proceed only once.
  10546. (while (and (setq level (org-up-heading-safe))
  10547. (or recursive first)
  10548. (>= (point) lim))
  10549. (setq first nil cookie-present nil)
  10550. (unless (and level
  10551. (not (string-match
  10552. "\\<checkbox\\>"
  10553. (downcase (or (org-entry-get nil "COOKIE_DATA")
  10554. "")))))
  10555. (throw 'exit nil))
  10556. (while (re-search-forward box-re (point-at-eol) t)
  10557. (setq cnt-all 0 cnt-done 0 cookie-present t)
  10558. (setq is-percent (match-end 2) checkbox-beg (match-beginning 0))
  10559. (save-match-data
  10560. (unless (outline-next-heading) (throw 'exit nil))
  10561. (while (and (looking-at org-complex-heading-regexp)
  10562. (> (setq l1 (length (match-string 1))) level))
  10563. (setq kwd (and (or recursive (= l1 ltoggle))
  10564. (match-string 2)))
  10565. (if (or (eq org-provide-todo-statistics 'all-headlines)
  10566. (and (listp org-provide-todo-statistics)
  10567. (or (member kwd org-provide-todo-statistics)
  10568. (member kwd org-done-keywords))))
  10569. (setq cnt-all (1+ cnt-all))
  10570. (if (eq org-provide-todo-statistics t)
  10571. (and kwd (setq cnt-all (1+ cnt-all)))))
  10572. (and (member kwd org-done-keywords)
  10573. (setq cnt-done (1+ cnt-done)))
  10574. (outline-next-heading)))
  10575. (setq new
  10576. (if is-percent
  10577. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  10578. (format "[%d/%d]" cnt-done cnt-all))
  10579. ndel (- (match-end 0) checkbox-beg))
  10580. ;; handle overlays when updating cookie from column view
  10581. (when (setq ov (car (overlays-at checkbox-beg)))
  10582. (setq ovs (overlay-start ov) ove (overlay-end ov))
  10583. (delete-overlay ov))
  10584. (goto-char checkbox-beg)
  10585. (insert new)
  10586. (delete-region (point) (+ (point) ndel))
  10587. (when org-auto-align-tags (org-fix-tags-on-the-fly))
  10588. (when ov (move-overlay ov ovs ove)))
  10589. (when cookie-present
  10590. (run-hook-with-args 'org-after-todo-statistics-hook
  10591. cnt-done (- cnt-all cnt-done))))))
  10592. (run-hooks 'org-todo-statistics-hook)))
  10593. (defvar org-after-todo-statistics-hook nil
  10594. "Hook that is called after a TODO statistics cookie has been updated.
  10595. Each function is called with two arguments: the number of not-done entries
  10596. and the number of done entries.
  10597. For example, the following function, when added to this hook, will switch
  10598. an entry to DONE when all children are done, and back to TODO when new
  10599. entries are set to a TODO status. Note that this hook is only called
  10600. when there is a statistics cookie in the headline!
  10601. (defun org-summary-todo (n-done n-not-done)
  10602. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  10603. (let (org-log-done org-log-states) ; turn off logging
  10604. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  10605. ")
  10606. (defvar org-todo-statistics-hook nil
  10607. "Hook that is run whenever Org thinks TODO statistics should be updated.
  10608. This hook runs even if there is no statistics cookie present, in which case
  10609. `org-after-todo-statistics-hook' would not run.")
  10610. (defun org-todo-trigger-tag-changes (state)
  10611. "Apply the changes defined in `org-todo-state-tags-triggers'."
  10612. (let ((l org-todo-state-tags-triggers)
  10613. changes)
  10614. (when (or (not state) (equal state ""))
  10615. (setq changes (append changes (cdr (assoc "" l)))))
  10616. (when (and (stringp state) (> (length state) 0))
  10617. (setq changes (append changes (cdr (assoc state l)))))
  10618. (when (member state org-not-done-keywords)
  10619. (setq changes (append changes (cdr (assoc 'todo l)))))
  10620. (when (member state org-done-keywords)
  10621. (setq changes (append changes (cdr (assoc 'done l)))))
  10622. (dolist (c changes)
  10623. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  10624. (defun org-local-logging (value)
  10625. "Get logging settings from a property VALUE."
  10626. (let* (words w a)
  10627. ;; directly set the variables, they are already local.
  10628. (setq org-log-done nil
  10629. org-log-repeat nil
  10630. org-todo-log-states nil)
  10631. (setq words (org-split-string value))
  10632. (while (setq w (pop words))
  10633. (cond
  10634. ((setq a (assoc w org-startup-options))
  10635. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  10636. (set (nth 1 a) (nth 2 a))))
  10637. ((setq a (org-extract-log-state-settings w))
  10638. (and (member (car a) org-todo-keywords-1)
  10639. (push a org-todo-log-states)))))))
  10640. (defun org-get-todo-sequence-head (kwd)
  10641. "Return the head of the TODO sequence to which KWD belongs.
  10642. If KWD is not set, check if there is a text property remembering the
  10643. right sequence."
  10644. (let (p)
  10645. (cond
  10646. ((not kwd)
  10647. (or (get-text-property (point-at-bol) 'org-todo-head)
  10648. (progn
  10649. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  10650. nil (point-at-eol)))
  10651. (get-text-property p 'org-todo-head))))
  10652. ((not (member kwd org-todo-keywords-1))
  10653. (car org-todo-keywords-1))
  10654. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  10655. (defun org-fast-todo-selection ()
  10656. "Fast TODO keyword selection with single keys.
  10657. Returns the new TODO keyword, or nil if no state change should occur."
  10658. (let* ((fulltable org-todo-key-alist)
  10659. (done-keywords org-done-keywords) ;; needed for the faces.
  10660. (maxlen (apply 'max (mapcar
  10661. (lambda (x)
  10662. (if (stringp (car x)) (string-width (car x)) 0))
  10663. fulltable)))
  10664. (expert nil)
  10665. (fwidth (+ maxlen 3 1 3))
  10666. (ncol (/ (- (window-width) 4) fwidth))
  10667. tg cnt e c tbl
  10668. groups ingroup)
  10669. (save-excursion
  10670. (save-window-excursion
  10671. (if expert
  10672. (set-buffer (get-buffer-create " *Org todo*"))
  10673. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  10674. (erase-buffer)
  10675. (org-set-local 'org-done-keywords done-keywords)
  10676. (setq tbl fulltable cnt 0)
  10677. (while (setq e (pop tbl))
  10678. (cond
  10679. ((equal e '(:startgroup))
  10680. (push '() groups) (setq ingroup t)
  10681. (when (not (= cnt 0))
  10682. (setq cnt 0)
  10683. (insert "\n"))
  10684. (insert "{ "))
  10685. ((equal e '(:endgroup))
  10686. (setq ingroup nil cnt 0)
  10687. (insert "}\n"))
  10688. ((equal e '(:newline))
  10689. (when (not (= cnt 0))
  10690. (setq cnt 0)
  10691. (insert "\n")
  10692. (setq e (car tbl))
  10693. (while (equal (car tbl) '(:newline))
  10694. (insert "\n")
  10695. (setq tbl (cdr tbl)))))
  10696. (t
  10697. (setq tg (car e) c (cdr e))
  10698. (if ingroup (push tg (car groups)))
  10699. (setq tg (org-add-props tg nil 'face
  10700. (org-get-todo-face tg)))
  10701. (if (and (= cnt 0) (not ingroup)) (insert " "))
  10702. (insert "[" c "] " tg (make-string
  10703. (- fwidth 4 (length tg)) ?\ ))
  10704. (when (= (setq cnt (1+ cnt)) ncol)
  10705. (insert "\n")
  10706. (if ingroup (insert " "))
  10707. (setq cnt 0)))))
  10708. (insert "\n")
  10709. (goto-char (point-min))
  10710. (if (not expert) (org-fit-window-to-buffer))
  10711. (message "[a-z..]:Set [SPC]:clear")
  10712. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  10713. (cond
  10714. ((or (= c ?\C-g)
  10715. (and (= c ?q) (not (rassoc c fulltable))))
  10716. (setq quit-flag t))
  10717. ((= c ?\ ) nil)
  10718. ((setq e (rassoc c fulltable) tg (car e))
  10719. tg)
  10720. (t (setq quit-flag t)))))))
  10721. (defun org-entry-is-todo-p ()
  10722. (member (org-get-todo-state) org-not-done-keywords))
  10723. (defun org-entry-is-done-p ()
  10724. (member (org-get-todo-state) org-done-keywords))
  10725. (defun org-get-todo-state ()
  10726. (save-excursion
  10727. (org-back-to-heading t)
  10728. (and (looking-at org-todo-line-regexp)
  10729. (match-end 2)
  10730. (match-string 2))))
  10731. (defun org-at-date-range-p (&optional inactive-ok)
  10732. "Is the cursor inside a date range?"
  10733. (interactive)
  10734. (save-excursion
  10735. (catch 'exit
  10736. (let ((pos (point)))
  10737. (skip-chars-backward "^[<\r\n")
  10738. (skip-chars-backward "<[")
  10739. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  10740. (>= (match-end 0) pos)
  10741. (throw 'exit t))
  10742. (skip-chars-backward "^<[\r\n")
  10743. (skip-chars-backward "<[")
  10744. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  10745. (>= (match-end 0) pos)
  10746. (throw 'exit t)))
  10747. nil)))
  10748. (defun org-get-repeat (&optional tagline)
  10749. "Check if there is a deadline/schedule with repeater in this entry."
  10750. (save-match-data
  10751. (save-excursion
  10752. (org-back-to-heading t)
  10753. (and (re-search-forward (if tagline
  10754. (concat tagline "\\s-*" org-repeat-re)
  10755. org-repeat-re)
  10756. (org-entry-end-position) t)
  10757. (match-string-no-properties 1)))))
  10758. (defvar org-last-changed-timestamp)
  10759. (defvar org-last-inserted-timestamp)
  10760. (defvar org-log-post-message)
  10761. (defvar org-log-note-purpose)
  10762. (defvar org-log-note-how)
  10763. (defvar org-log-note-extra)
  10764. (defun org-auto-repeat-maybe (done-word)
  10765. "Check if the current headline contains a repeated deadline/schedule.
  10766. If yes, set TODO state back to what it was and change the base date
  10767. of repeating deadline/scheduled time stamps to new date.
  10768. This function is run automatically after each state change to a DONE state."
  10769. ;; last-state is dynamically scoped into this function
  10770. (let* ((repeat (org-get-repeat))
  10771. (aa (assoc last-state org-todo-kwd-alist))
  10772. (interpret (nth 1 aa))
  10773. (head (nth 2 aa))
  10774. (whata '(("d" . day) ("m" . month) ("y" . year)))
  10775. (msg "Entry repeats: ")
  10776. (org-log-done nil)
  10777. (org-todo-log-states nil)
  10778. re type n what ts time to-state)
  10779. (when repeat
  10780. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  10781. (setq to-state (or (org-entry-get nil "REPEAT_TO_STATE")
  10782. org-todo-repeat-to-state))
  10783. (unless (and to-state (member to-state org-todo-keywords-1))
  10784. (setq to-state (if (eq interpret 'type) last-state head)))
  10785. (org-todo to-state)
  10786. (when (or org-log-repeat (org-entry-get nil "CLOCK"))
  10787. (org-entry-put nil "LAST_REPEAT" (format-time-string
  10788. (org-time-stamp-format t t))))
  10789. (when org-log-repeat
  10790. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  10791. (memq 'org-add-log-note post-command-hook))
  10792. ;; OK, we are already setup for some record
  10793. (if (eq org-log-repeat 'note)
  10794. ;; make sure we take a note, not only a time stamp
  10795. (setq org-log-note-how 'note))
  10796. ;; Set up for taking a record
  10797. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  10798. last-state
  10799. 'findpos org-log-repeat)))
  10800. (org-back-to-heading t)
  10801. (org-add-planning-info nil nil 'closed)
  10802. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  10803. org-deadline-time-regexp "\\)\\|\\("
  10804. org-ts-regexp "\\)"))
  10805. (while (re-search-forward
  10806. re (save-excursion (outline-next-heading) (point)) t)
  10807. (setq type (if (match-end 1) org-scheduled-string
  10808. (if (match-end 3) org-deadline-string "Plain:"))
  10809. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  10810. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
  10811. (setq n (string-to-number (match-string 2 ts))
  10812. what (match-string 3 ts))
  10813. (if (equal what "w") (setq n (* n 7) what "d"))
  10814. ;; Preparation, see if we need to modify the start date for the change
  10815. (when (match-end 1)
  10816. (setq time (save-match-data (org-time-string-to-time ts)))
  10817. (cond
  10818. ((equal (match-string 1 ts) ".")
  10819. ;; Shift starting date to today
  10820. (org-timestamp-change
  10821. (- (org-today) (time-to-days time))
  10822. 'day))
  10823. ((equal (match-string 1 ts) "+")
  10824. (let ((nshiftmax 10) (nshift 0))
  10825. (while (or (= nshift 0)
  10826. (<= (time-to-days time)
  10827. (time-to-days (current-time))))
  10828. (when (= (incf nshift) nshiftmax)
  10829. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  10830. (error "Abort")))
  10831. (org-timestamp-change n (cdr (assoc what whata)))
  10832. (org-at-timestamp-p t)
  10833. (setq ts (match-string 1))
  10834. (setq time (save-match-data (org-time-string-to-time ts)))))
  10835. (org-timestamp-change (- n) (cdr (assoc what whata)))
  10836. ;; rematch, so that we have everything in place for the real shift
  10837. (org-at-timestamp-p t)
  10838. (setq ts (match-string 1))
  10839. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
  10840. (org-timestamp-change n (cdr (assoc what whata)))
  10841. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  10842. (setq org-log-post-message msg)
  10843. (message "%s" msg))))
  10844. (defun org-show-todo-tree (arg)
  10845. "Make a compact tree which shows all headlines marked with TODO.
  10846. The tree will show the lines where the regexp matches, and all higher
  10847. headlines above the match.
  10848. With a \\[universal-argument] prefix, prompt for a regexp to match.
  10849. With a numeric prefix N, construct a sparse tree for the Nth element
  10850. of `org-todo-keywords-1'."
  10851. (interactive "P")
  10852. (let ((case-fold-search nil)
  10853. (kwd-re
  10854. (cond ((null arg) org-not-done-regexp)
  10855. ((equal arg '(4))
  10856. (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
  10857. (mapcar 'list org-todo-keywords-1))))
  10858. (concat "\\("
  10859. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  10860. "\\)\\>")))
  10861. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  10862. (regexp-quote (nth (1- (prefix-numeric-value arg))
  10863. org-todo-keywords-1)))
  10864. (t (error "Invalid prefix argument: %s" arg)))))
  10865. (message "%d TODO entries found"
  10866. (org-occur (concat "^" org-outline-regexp " *" kwd-re )))))
  10867. (defun org-deadline (&optional remove time)
  10868. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  10869. With argument REMOVE, remove any deadline from the item.
  10870. With argument TIME, set the deadline at the corresponding date. TIME
  10871. can either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  10872. (interactive "P")
  10873. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  10874. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  10875. 'region-start-level 'region))
  10876. org-loop-over-headlines-in-active-region)
  10877. (org-map-entries
  10878. `(org-deadline ',remove ,time)
  10879. org-loop-over-headlines-in-active-region
  10880. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  10881. (let* ((old-date (org-entry-get nil "DEADLINE"))
  10882. (repeater (and old-date
  10883. (string-match
  10884. "\\([.+-]+[0-9]+[dwmy]\\(?:[/ ][-+]?[0-9]+[dwmy]\\)?\\) ?"
  10885. old-date)
  10886. (match-string 1 old-date))))
  10887. (if remove
  10888. (progn
  10889. (when (and old-date org-log-redeadline)
  10890. (org-add-log-setup 'deldeadline nil old-date 'findpos
  10891. org-log-redeadline))
  10892. (org-remove-timestamp-with-keyword org-deadline-string)
  10893. (message "Item no longer has a deadline."))
  10894. (org-add-planning-info 'deadline time 'closed)
  10895. (when (and old-date org-log-redeadline
  10896. (not (equal old-date
  10897. (substring org-last-inserted-timestamp 1 -1))))
  10898. (org-add-log-setup 'redeadline nil old-date 'findpos
  10899. org-log-redeadline))
  10900. (when repeater
  10901. (save-excursion
  10902. (org-back-to-heading t)
  10903. (when (re-search-forward (concat org-deadline-string " "
  10904. org-last-inserted-timestamp)
  10905. (save-excursion
  10906. (outline-next-heading) (point)) t)
  10907. (goto-char (1- (match-end 0)))
  10908. (insert " " repeater)
  10909. (setq org-last-inserted-timestamp
  10910. (concat (substring org-last-inserted-timestamp 0 -1)
  10911. " " repeater
  10912. (substring org-last-inserted-timestamp -1))))))
  10913. (message "Deadline on %s" org-last-inserted-timestamp)))))
  10914. (defun org-schedule (&optional remove time)
  10915. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  10916. With argument REMOVE, remove any scheduling date from the item.
  10917. With argument TIME, scheduled at the corresponding date. TIME can
  10918. either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  10919. (interactive "P")
  10920. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  10921. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  10922. 'region-start-level 'region))
  10923. org-loop-over-headlines-in-active-region)
  10924. (org-map-entries
  10925. `(org-schedule ',remove ,time)
  10926. org-loop-over-headlines-in-active-region
  10927. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  10928. (let* ((old-date (org-entry-get nil "SCHEDULED"))
  10929. (repeater (and old-date
  10930. (string-match
  10931. "\\([.+-]+[0-9]+[dwmy]\\(?:[/ ][-+]?[0-9]+[dwmy]\\)?\\) ?"
  10932. old-date)
  10933. (match-string 1 old-date))))
  10934. (if remove
  10935. (progn
  10936. (when (and old-date org-log-reschedule)
  10937. (org-add-log-setup 'delschedule nil old-date 'findpos
  10938. org-log-reschedule))
  10939. (org-remove-timestamp-with-keyword org-scheduled-string)
  10940. (message "Item is no longer scheduled."))
  10941. (org-add-planning-info 'scheduled time 'closed)
  10942. (when (and old-date org-log-reschedule
  10943. (not (equal old-date
  10944. (substring org-last-inserted-timestamp 1 -1))))
  10945. (org-add-log-setup 'reschedule nil old-date 'findpos
  10946. org-log-reschedule))
  10947. (when repeater
  10948. (save-excursion
  10949. (org-back-to-heading t)
  10950. (when (re-search-forward (concat org-scheduled-string " "
  10951. org-last-inserted-timestamp)
  10952. (save-excursion
  10953. (outline-next-heading) (point)) t)
  10954. (goto-char (1- (match-end 0)))
  10955. (insert " " repeater)
  10956. (setq org-last-inserted-timestamp
  10957. (concat (substring org-last-inserted-timestamp 0 -1)
  10958. " " repeater
  10959. (substring org-last-inserted-timestamp -1))))))
  10960. (message "Scheduled to %s" org-last-inserted-timestamp)))))
  10961. (defun org-get-scheduled-time (pom &optional inherit)
  10962. "Get the scheduled time as a time tuple, of a format suitable
  10963. for calling org-schedule with, or if there is no scheduling,
  10964. returns nil."
  10965. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  10966. (when time
  10967. (apply 'encode-time (org-parse-time-string time)))))
  10968. (defun org-get-deadline-time (pom &optional inherit)
  10969. "Get the deadline as a time tuple, of a format suitable for
  10970. calling org-deadline with, or if there is no scheduling, returns
  10971. nil."
  10972. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  10973. (when time
  10974. (apply 'encode-time (org-parse-time-string time)))))
  10975. (defun org-remove-timestamp-with-keyword (keyword)
  10976. "Remove all time stamps with KEYWORD in the current entry."
  10977. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  10978. beg)
  10979. (save-excursion
  10980. (org-back-to-heading t)
  10981. (setq beg (point))
  10982. (outline-next-heading)
  10983. (while (re-search-backward re beg t)
  10984. (replace-match "")
  10985. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  10986. (equal (char-before) ?\ ))
  10987. (backward-delete-char 1)
  10988. (if (string-match "^[ \t]*$" (buffer-substring
  10989. (point-at-bol) (point-at-eol)))
  10990. (delete-region (point-at-bol)
  10991. (min (point-max) (1+ (point-at-eol))))))))))
  10992. (defun org-add-planning-info (what &optional time &rest remove)
  10993. "Insert new timestamp with keyword in the line directly after the headline.
  10994. WHAT indicates what kind of time stamp to add. TIME indicates the time to use.
  10995. If non is given, the user is prompted for a date.
  10996. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  10997. be removed."
  10998. (interactive)
  10999. (let (org-time-was-given org-end-time-was-given ts
  11000. end default-time default-input)
  11001. (catch 'exit
  11002. (when (and (memq what '(scheduled deadline))
  11003. (or (not time)
  11004. (and (stringp time)
  11005. (string-match "^[-+]+[0-9]" time))))
  11006. ;; Try to get a default date/time from existing timestamp
  11007. (save-excursion
  11008. (org-back-to-heading t)
  11009. (setq end (save-excursion (outline-next-heading) (point)))
  11010. (when (re-search-forward (if (eq what 'scheduled)
  11011. org-scheduled-time-regexp
  11012. org-deadline-time-regexp)
  11013. end t)
  11014. (setq ts (match-string 1)
  11015. default-time
  11016. (apply 'encode-time (org-parse-time-string ts))
  11017. default-input (and ts (org-get-compact-tod ts))))))
  11018. (when what
  11019. (setq time
  11020. (if (stringp time)
  11021. ;; This is a string (relative or absolute), set proper date
  11022. (apply 'encode-time
  11023. (org-read-date-analyze
  11024. time default-time (decode-time default-time)))
  11025. ;; If necessary, get the time from the user
  11026. (or time (org-read-date nil 'to-time nil nil
  11027. default-time default-input)))))
  11028. (when (and org-insert-labeled-timestamps-at-point
  11029. (member what '(scheduled deadline)))
  11030. (insert
  11031. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  11032. (org-insert-time-stamp time org-time-was-given
  11033. nil nil nil (list org-end-time-was-given))
  11034. (setq what nil))
  11035. (save-excursion
  11036. (save-restriction
  11037. (let (col list elt ts buffer-invisibility-spec)
  11038. (org-back-to-heading t)
  11039. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"))
  11040. (goto-char (match-end 1))
  11041. (setq col (current-column))
  11042. (goto-char (match-end 0))
  11043. (if (eobp) (insert "\n") (forward-char 1))
  11044. (when (and (not what)
  11045. (not (looking-at
  11046. (concat "[ \t]*"
  11047. org-keyword-time-not-clock-regexp))))
  11048. ;; Nothing to add, nothing to remove...... :-)
  11049. (throw 'exit nil))
  11050. (if (and (not (looking-at org-outline-regexp))
  11051. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  11052. "[^\r\n]*"))
  11053. (not (equal (match-string 1) org-clock-string)))
  11054. (narrow-to-region (match-beginning 0) (match-end 0))
  11055. (insert-before-markers "\n")
  11056. (backward-char 1)
  11057. (narrow-to-region (point) (point))
  11058. (and org-adapt-indentation (org-indent-to-column col)))
  11059. ;; Check if we have to remove something.
  11060. (setq list (cons what remove))
  11061. (while list
  11062. (setq elt (pop list))
  11063. (when (or (and (eq elt 'scheduled)
  11064. (re-search-forward org-scheduled-time-regexp nil t))
  11065. (and (eq elt 'deadline)
  11066. (re-search-forward org-deadline-time-regexp nil t))
  11067. (and (eq elt 'closed)
  11068. (re-search-forward org-closed-time-regexp nil t)))
  11069. (replace-match "")
  11070. (if (looking-at "--+<[^>]+>") (replace-match ""))))
  11071. (and (looking-at "[ \t]+") (replace-match ""))
  11072. (and org-adapt-indentation (bolp) (org-indent-to-column col))
  11073. (when what
  11074. (insert
  11075. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  11076. (cond ((eq what 'scheduled) org-scheduled-string)
  11077. ((eq what 'deadline) org-deadline-string)
  11078. ((eq what 'closed) org-closed-string))
  11079. " ")
  11080. (setq ts (org-insert-time-stamp
  11081. time
  11082. (or org-time-was-given
  11083. (and (eq what 'closed) org-log-done-with-time))
  11084. (eq what 'closed)
  11085. nil nil (list org-end-time-was-given)))
  11086. (insert
  11087. (if (not (or (bolp) (eq (char-before) ?\ )
  11088. (memq (char-after) '(32 10))
  11089. (eobp))) " " ""))
  11090. (end-of-line 1))
  11091. (goto-char (point-min))
  11092. (widen)
  11093. (if (and (looking-at "[ \t]*\n")
  11094. (equal (char-before) ?\n))
  11095. (delete-region (1- (point)) (point-at-eol)))
  11096. ts))))))
  11097. (defvar org-log-note-marker (make-marker))
  11098. (defvar org-log-note-purpose nil)
  11099. (defvar org-log-note-state nil)
  11100. (defvar org-log-note-previous-state nil)
  11101. (defvar org-log-note-how nil)
  11102. (defvar org-log-note-extra nil)
  11103. (defvar org-log-note-window-configuration nil)
  11104. (defvar org-log-note-return-to (make-marker))
  11105. (defvar org-log-note-effective-time nil
  11106. "Remembered current time so that dynamically scoped
  11107. `org-extend-today-until' affects tha timestamps in state change
  11108. log")
  11109. (defvar org-log-post-message nil
  11110. "Message to be displayed after a log note has been stored.
  11111. The auto-repeater uses this.")
  11112. (defun org-add-note ()
  11113. "Add a note to the current entry.
  11114. This is done in the same way as adding a state change note."
  11115. (interactive)
  11116. (org-add-log-setup 'note nil nil 'findpos nil))
  11117. (defvar org-property-end-re)
  11118. (defun org-add-log-setup (&optional purpose state prev-state
  11119. findpos how extra)
  11120. "Set up the post command hook to take a note.
  11121. If this is about to TODO state change, the new state is expected in STATE.
  11122. When FINDPOS is non-nil, find the correct position for the note in
  11123. the current entry. If not, assume that it can be inserted at point.
  11124. HOW is an indicator what kind of note should be created.
  11125. EXTRA is additional text that will be inserted into the notes buffer."
  11126. (let* ((org-log-into-drawer (org-log-into-drawer))
  11127. (drawer (cond ((stringp org-log-into-drawer)
  11128. org-log-into-drawer)
  11129. (org-log-into-drawer "LOGBOOK")
  11130. (t nil))))
  11131. (save-restriction
  11132. (save-excursion
  11133. (when findpos
  11134. (org-back-to-heading t)
  11135. (narrow-to-region (point) (save-excursion
  11136. (outline-next-heading) (point)))
  11137. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"
  11138. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  11139. "[^\r\n]*\\)?"))
  11140. (goto-char (match-end 0))
  11141. (cond
  11142. (drawer
  11143. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  11144. nil t)
  11145. (progn
  11146. (goto-char (match-end 0))
  11147. (or org-log-states-order-reversed
  11148. (and (re-search-forward org-property-end-re nil t)
  11149. (goto-char (1- (match-beginning 0))))))
  11150. (insert "\n:" drawer ":\n:END:")
  11151. (beginning-of-line 0)
  11152. (org-indent-line-function)
  11153. (beginning-of-line 2)
  11154. (org-indent-line-function)
  11155. (end-of-line 0)))
  11156. ((and org-log-state-notes-insert-after-drawers
  11157. (save-excursion
  11158. (forward-line) (looking-at org-drawer-regexp)))
  11159. (forward-line)
  11160. (while (looking-at org-drawer-regexp)
  11161. (goto-char (match-end 0))
  11162. (re-search-forward org-property-end-re (point-max) t)
  11163. (forward-line))
  11164. (forward-line -1)))
  11165. (unless org-log-states-order-reversed
  11166. (and (= (char-after) ?\n) (forward-char 1))
  11167. (org-skip-over-state-notes)
  11168. (skip-chars-backward " \t\n\r")))
  11169. (move-marker org-log-note-marker (point))
  11170. (setq org-log-note-purpose purpose
  11171. org-log-note-state state
  11172. org-log-note-previous-state prev-state
  11173. org-log-note-how how
  11174. org-log-note-extra extra
  11175. org-log-note-effective-time (org-current-effective-time))
  11176. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  11177. (defun org-skip-over-state-notes ()
  11178. "Skip past the list of State notes in an entry."
  11179. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  11180. (when (ignore-errors (goto-char (org-in-item-p)))
  11181. (let* ((struct (org-list-struct))
  11182. (prevs (org-list-prevs-alist struct)))
  11183. (while (looking-at "[ \t]*- State")
  11184. (goto-char (or (org-list-get-next-item (point) struct prevs)
  11185. (org-list-get-item-end (point) struct)))))))
  11186. (defun org-add-log-note (&optional purpose)
  11187. "Pop up a window for taking a note, and add this note later at point."
  11188. (remove-hook 'post-command-hook 'org-add-log-note)
  11189. (setq org-log-note-window-configuration (current-window-configuration))
  11190. (delete-other-windows)
  11191. (move-marker org-log-note-return-to (point))
  11192. (org-pop-to-buffer-same-window (marker-buffer org-log-note-marker))
  11193. (goto-char org-log-note-marker)
  11194. (org-switch-to-buffer-other-window "*Org Note*")
  11195. (erase-buffer)
  11196. (if (memq org-log-note-how '(time state))
  11197. (let (current-prefix-arg) (org-store-log-note))
  11198. (let ((org-inhibit-startup t)) (org-mode))
  11199. (insert (format "# Insert note for %s.
  11200. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  11201. (cond
  11202. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  11203. ((eq org-log-note-purpose 'done) "closed todo item")
  11204. ((eq org-log-note-purpose 'state)
  11205. (format "state change from \"%s\" to \"%s\""
  11206. (or org-log-note-previous-state "")
  11207. (or org-log-note-state "")))
  11208. ((eq org-log-note-purpose 'reschedule)
  11209. "rescheduling")
  11210. ((eq org-log-note-purpose 'delschedule)
  11211. "no longer scheduled")
  11212. ((eq org-log-note-purpose 'redeadline)
  11213. "changing deadline")
  11214. ((eq org-log-note-purpose 'deldeadline)
  11215. "removing deadline")
  11216. ((eq org-log-note-purpose 'refile)
  11217. "refiling")
  11218. ((eq org-log-note-purpose 'note)
  11219. "this entry")
  11220. (t (error "This should not happen")))))
  11221. (if org-log-note-extra (insert org-log-note-extra))
  11222. (org-set-local 'org-finish-function 'org-store-log-note)
  11223. (run-hooks 'org-log-buffer-setup-hook)))
  11224. (defvar org-note-abort nil) ; dynamically scoped
  11225. (defun org-store-log-note ()
  11226. "Finish taking a log note, and insert it to where it belongs."
  11227. (let ((txt (buffer-string))
  11228. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  11229. lines ind bul)
  11230. (kill-buffer (current-buffer))
  11231. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  11232. (setq txt (replace-match "" t t txt)))
  11233. (if (string-match "\\s-+\\'" txt)
  11234. (setq txt (replace-match "" t t txt)))
  11235. (setq lines (org-split-string txt "\n"))
  11236. (when (and note (string-match "\\S-" note))
  11237. (setq note
  11238. (org-replace-escapes
  11239. note
  11240. (list (cons "%u" (user-login-name))
  11241. (cons "%U" user-full-name)
  11242. (cons "%t" (format-time-string
  11243. (org-time-stamp-format 'long 'inactive)
  11244. org-log-note-effective-time))
  11245. (cons "%T" (format-time-string
  11246. (org-time-stamp-format 'long nil)
  11247. org-log-note-effective-time))
  11248. (cons "%d" (format-time-string
  11249. (org-time-stamp-format nil 'inactive)
  11250. org-log-note-effective-time))
  11251. (cons "%D" (format-time-string
  11252. (org-time-stamp-format nil nil)
  11253. org-log-note-effective-time))
  11254. (cons "%s" (if org-log-note-state
  11255. (concat "\"" org-log-note-state "\"")
  11256. ""))
  11257. (cons "%S" (if org-log-note-previous-state
  11258. (concat "\"" org-log-note-previous-state "\"")
  11259. "\"\"")))))
  11260. (if lines (setq note (concat note " \\\\")))
  11261. (push note lines))
  11262. (when (or current-prefix-arg org-note-abort)
  11263. (when org-log-into-drawer
  11264. (org-remove-empty-drawer-at
  11265. (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
  11266. org-log-note-marker))
  11267. (setq lines nil))
  11268. (when lines
  11269. (with-current-buffer (marker-buffer org-log-note-marker)
  11270. (save-excursion
  11271. (goto-char org-log-note-marker)
  11272. (move-marker org-log-note-marker nil)
  11273. (end-of-line 1)
  11274. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  11275. (setq ind (save-excursion
  11276. (if (ignore-errors (goto-char (org-in-item-p)))
  11277. (let ((struct (org-list-struct)))
  11278. (org-list-get-ind
  11279. (org-list-get-top-point struct) struct))
  11280. (skip-chars-backward " \r\t\n")
  11281. (cond
  11282. ((and (org-at-heading-p)
  11283. org-adapt-indentation)
  11284. (1+ (org-current-level)))
  11285. ((org-at-heading-p) 0)
  11286. (t (org-get-indentation))))))
  11287. (setq bul (org-list-bullet-string "-"))
  11288. (org-indent-line-to ind)
  11289. (insert bul (pop lines))
  11290. (let ((ind-body (+ (length bul) ind)))
  11291. (while lines
  11292. (insert "\n")
  11293. (org-indent-line-to ind-body)
  11294. (insert (pop lines))))
  11295. (message "Note stored")
  11296. (org-back-to-heading t)
  11297. (org-cycle-hide-drawers 'children)))))
  11298. (set-window-configuration org-log-note-window-configuration)
  11299. (with-current-buffer (marker-buffer org-log-note-return-to)
  11300. (goto-char org-log-note-return-to))
  11301. (move-marker org-log-note-return-to nil)
  11302. (and org-log-post-message (message "%s" org-log-post-message)))
  11303. (defun org-remove-empty-drawer-at (drawer pos)
  11304. "Remove an empty drawer DRAWER at position POS.
  11305. POS may also be a marker."
  11306. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  11307. (save-excursion
  11308. (save-restriction
  11309. (widen)
  11310. (goto-char pos)
  11311. (if (org-in-regexp
  11312. (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
  11313. (replace-match ""))))))
  11314. (defun org-sparse-tree (&optional arg)
  11315. "Create a sparse tree, prompt for the details.
  11316. This command can create sparse trees. You first need to select the type
  11317. of match used to create the tree:
  11318. t Show all TODO entries.
  11319. T Show entries with a specific TODO keyword.
  11320. m Show entries selected by a tags/property match.
  11321. p Enter a property name and its value (both with completion on existing
  11322. names/values) and show entries with that property.
  11323. r Show entries matching a regular expression (`/' can be used as well)
  11324. d Show deadlines due within `org-deadline-warning-days'.
  11325. b Show deadlines and scheduled items before a date.
  11326. a Show deadlines and scheduled items after a date."
  11327. (interactive "P")
  11328. (let (ans kwd value)
  11329. (message "Sparse tree: [r]egexp [/]regexp [t]odo [T]odo-kwd [m]atch [p]roperty\n [d]eadlines [b]efore-date [a]fter-date [D]ates range")
  11330. (setq ans (read-char-exclusive))
  11331. (cond
  11332. ((equal ans ?d)
  11333. (call-interactively 'org-check-deadlines))
  11334. ((equal ans ?b)
  11335. (call-interactively 'org-check-before-date))
  11336. ((equal ans ?a)
  11337. (call-interactively 'org-check-after-date))
  11338. ((equal ans ?D)
  11339. (call-interactively 'org-check-dates-range))
  11340. ((equal ans ?t)
  11341. (org-show-todo-tree nil))
  11342. ((equal ans ?T)
  11343. (org-show-todo-tree '(4)))
  11344. ((member ans '(?T ?m))
  11345. (call-interactively 'org-match-sparse-tree))
  11346. ((member ans '(?p ?P))
  11347. (setq kwd (org-icompleting-read "Property: "
  11348. (mapcar 'list (org-buffer-property-keys))))
  11349. (setq value (org-icompleting-read "Value: "
  11350. (mapcar 'list (org-property-values kwd))))
  11351. (unless (string-match "\\`{.*}\\'" value)
  11352. (setq value (concat "\"" value "\"")))
  11353. (org-match-sparse-tree arg (concat kwd "=" value)))
  11354. ((member ans '(?r ?R ?/))
  11355. (call-interactively 'org-occur))
  11356. (t (error "No such sparse tree command \"%c\"" ans)))))
  11357. (defvar org-occur-highlights nil
  11358. "List of overlays used for occur matches.")
  11359. (make-variable-buffer-local 'org-occur-highlights)
  11360. (defvar org-occur-parameters nil
  11361. "Parameters of the active org-occur calls.
  11362. This is a list, each call to org-occur pushes as cons cell,
  11363. containing the regular expression and the callback, onto the list.
  11364. The list can contain several entries if `org-occur' has been called
  11365. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  11366. will only contain one set of parameters. When the highlights are
  11367. removed (for example with `C-c C-c', or with the next edit (depending
  11368. on `org-remove-highlights-with-change'), this variable is emptied
  11369. as well.")
  11370. (make-variable-buffer-local 'org-occur-parameters)
  11371. (defun org-occur (regexp &optional keep-previous callback)
  11372. "Make a compact tree which shows all matches of REGEXP.
  11373. The tree will show the lines where the regexp matches, and all higher
  11374. headlines above the match. It will also show the heading after the match,
  11375. to make sure editing the matching entry is easy.
  11376. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  11377. call to `org-occur' will be kept, to allow stacking of calls to this
  11378. command.
  11379. If CALLBACK is non-nil, it is a function which is called to confirm
  11380. that the match should indeed be shown."
  11381. (interactive "sRegexp: \nP")
  11382. (when (equal regexp "")
  11383. (error "Regexp cannot be empty"))
  11384. (unless keep-previous
  11385. (org-remove-occur-highlights nil nil t))
  11386. (push (cons regexp callback) org-occur-parameters)
  11387. (let ((cnt 0))
  11388. (save-excursion
  11389. (goto-char (point-min))
  11390. (if (or (not keep-previous) ; do not want to keep
  11391. (not org-occur-highlights)) ; no previous matches
  11392. ;; hide everything
  11393. (org-overview))
  11394. (while (re-search-forward regexp nil t)
  11395. (when (or (not callback)
  11396. (save-match-data (funcall callback)))
  11397. (setq cnt (1+ cnt))
  11398. (when org-highlight-sparse-tree-matches
  11399. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  11400. (org-show-context 'occur-tree))))
  11401. (when org-remove-highlights-with-change
  11402. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  11403. nil 'local))
  11404. (unless org-sparse-tree-open-archived-trees
  11405. (org-hide-archived-subtrees (point-min) (point-max)))
  11406. (run-hooks 'org-occur-hook)
  11407. (if (org-called-interactively-p 'interactive)
  11408. (message "%d match(es) for regexp %s" cnt regexp))
  11409. cnt))
  11410. (defun org-occur-next-match (&optional n reset)
  11411. "Function for `next-error-function' to find sparse tree matches.
  11412. N is the number of matches to move, when negative move backwards.
  11413. RESET is entirely ignored - this function always goes back to the
  11414. starting point when no match is found."
  11415. (let* ((limit (if (< n 0) (point-min) (point-max)))
  11416. (search-func (if (< n 0)
  11417. 'previous-single-char-property-change
  11418. 'next-single-char-property-change))
  11419. (n (abs n))
  11420. (pos (point))
  11421. p1)
  11422. (catch 'exit
  11423. (while (setq p1 (funcall search-func (point) 'org-type))
  11424. (when (equal p1 limit)
  11425. (goto-char pos)
  11426. (error "No more matches"))
  11427. (when (equal (get-char-property p1 'org-type) 'org-occur)
  11428. (setq n (1- n))
  11429. (when (= n 0)
  11430. (goto-char p1)
  11431. (throw 'exit (point))))
  11432. (goto-char p1))
  11433. (goto-char p1)
  11434. (error "No more matches"))))
  11435. (defun org-show-context (&optional key)
  11436. "Make sure point and context are visible.
  11437. How much context is shown depends upon the variables
  11438. `org-show-hierarchy-above', `org-show-following-heading',
  11439. `org-show-entry-below' and `org-show-siblings'."
  11440. (let ((heading-p (org-at-heading-p t))
  11441. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  11442. (following-p (org-get-alist-option org-show-following-heading key))
  11443. (entry-p (org-get-alist-option org-show-entry-below key))
  11444. (siblings-p (org-get-alist-option org-show-siblings key)))
  11445. (catch 'exit
  11446. ;; Show heading or entry text
  11447. (if (and heading-p (not entry-p))
  11448. (org-flag-heading nil) ; only show the heading
  11449. (and (or entry-p (outline-invisible-p) (org-invisible-p2))
  11450. (org-show-hidden-entry))) ; show entire entry
  11451. (when following-p
  11452. ;; Show next sibling, or heading below text
  11453. (save-excursion
  11454. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  11455. (org-flag-heading nil))))
  11456. (when siblings-p (org-show-siblings))
  11457. (when hierarchy-p
  11458. ;; show all higher headings, possibly with siblings
  11459. (save-excursion
  11460. (while (and (condition-case nil
  11461. (progn (org-up-heading-all 1) t)
  11462. (error nil))
  11463. (not (bobp)))
  11464. (org-flag-heading nil)
  11465. (when siblings-p (org-show-siblings))))))))
  11466. (defvar org-reveal-start-hook nil
  11467. "Hook run before revealing a location.")
  11468. (defun org-reveal (&optional siblings)
  11469. "Show current entry, hierarchy above it, and the following headline.
  11470. This can be used to show a consistent set of context around locations
  11471. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  11472. not t for the search context.
  11473. With optional argument SIBLINGS, on each level of the hierarchy all
  11474. siblings are shown. This repairs the tree structure to what it would
  11475. look like when opened with hierarchical calls to `org-cycle'.
  11476. With double optional argument \\[universal-argument] \\[universal-argument], \
  11477. go to the parent and show the
  11478. entire tree."
  11479. (interactive "P")
  11480. (run-hooks 'org-reveal-start-hook)
  11481. (let ((org-show-hierarchy-above t)
  11482. (org-show-following-heading t)
  11483. (org-show-siblings (if siblings t org-show-siblings)))
  11484. (org-show-context nil))
  11485. (when (equal siblings '(16))
  11486. (save-excursion
  11487. (when (org-up-heading-safe)
  11488. (org-show-subtree)
  11489. (run-hook-with-args 'org-cycle-hook 'subtree)))))
  11490. (defun org-highlight-new-match (beg end)
  11491. "Highlight from BEG to END and mark the highlight is an occur headline."
  11492. (let ((ov (make-overlay beg end)))
  11493. (overlay-put ov 'face 'secondary-selection)
  11494. (overlay-put ov 'org-type 'org-occur)
  11495. (push ov org-occur-highlights)))
  11496. (defun org-remove-occur-highlights (&optional beg end noremove)
  11497. "Remove the occur highlights from the buffer.
  11498. BEG and END are ignored. If NOREMOVE is nil, remove this function
  11499. from the `before-change-functions' in the current buffer."
  11500. (interactive)
  11501. (unless org-inhibit-highlight-removal
  11502. (mapc 'delete-overlay org-occur-highlights)
  11503. (setq org-occur-highlights nil)
  11504. (setq org-occur-parameters nil)
  11505. (unless noremove
  11506. (remove-hook 'before-change-functions
  11507. 'org-remove-occur-highlights 'local))))
  11508. ;;;; Priorities
  11509. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  11510. "Regular expression matching the priority indicator.")
  11511. (defvar org-remove-priority-next-time nil)
  11512. (defun org-priority-up ()
  11513. "Increase the priority of the current item."
  11514. (interactive)
  11515. (org-priority 'up))
  11516. (defun org-priority-down ()
  11517. "Decrease the priority of the current item."
  11518. (interactive)
  11519. (org-priority 'down))
  11520. (defun org-priority (&optional action)
  11521. "Change the priority of an item by ARG.
  11522. ACTION can be `set', `up', `down', or a character."
  11523. (interactive)
  11524. (unless org-enable-priority-commands
  11525. (error "Priority commands are disabled"))
  11526. (setq action (or action 'set))
  11527. (let (current new news have remove)
  11528. (save-excursion
  11529. (org-back-to-heading t)
  11530. (if (looking-at org-priority-regexp)
  11531. (setq current (string-to-char (match-string 2))
  11532. have t))
  11533. (cond
  11534. ((eq action 'remove)
  11535. (setq remove t new ?\ ))
  11536. ((or (eq action 'set)
  11537. (if (featurep 'xemacs) (characterp action) (integerp action)))
  11538. (if (not (eq action 'set))
  11539. (setq new action)
  11540. (message "Priority %c-%c, SPC to remove: "
  11541. org-highest-priority org-lowest-priority)
  11542. (save-match-data
  11543. (setq new (read-char-exclusive))))
  11544. (if (and (= (upcase org-highest-priority) org-highest-priority)
  11545. (= (upcase org-lowest-priority) org-lowest-priority))
  11546. (setq new (upcase new)))
  11547. (cond ((equal new ?\ ) (setq remove t))
  11548. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  11549. (error "Priority must be between `%c' and `%c'"
  11550. org-highest-priority org-lowest-priority))))
  11551. ((eq action 'up)
  11552. (setq new (if have
  11553. (1- current) ; normal cycling
  11554. ;; last priority was empty
  11555. (if (eq last-command this-command)
  11556. org-lowest-priority ; wrap around empty to lowest
  11557. ;; default
  11558. (if org-priority-start-cycle-with-default
  11559. org-default-priority
  11560. (1- org-default-priority))))))
  11561. ((eq action 'down)
  11562. (setq new (if have
  11563. (1+ current) ; normal cycling
  11564. ;; last priority was empty
  11565. (if (eq last-command this-command)
  11566. org-highest-priority ; wrap around empty to highest
  11567. ;; default
  11568. (if org-priority-start-cycle-with-default
  11569. org-default-priority
  11570. (1+ org-default-priority))))))
  11571. (t (error "Invalid action")))
  11572. (if (or (< (upcase new) org-highest-priority)
  11573. (> (upcase new) org-lowest-priority))
  11574. (if (and (memq action '(up down))
  11575. (not have) (not (eq last-command this-command)))
  11576. ;; `new' is from default priority
  11577. (error
  11578. "The default can not be set, see `org-default-priority' why")
  11579. ;; normal cycling: `new' is beyond highest/lowest priority
  11580. ;; and is wrapped around to the empty priority
  11581. (setq remove t)))
  11582. (setq news (format "%c" new))
  11583. (if have
  11584. (if remove
  11585. (replace-match "" t t nil 1)
  11586. (replace-match news t t nil 2))
  11587. (if remove
  11588. (error "No priority cookie found in line")
  11589. (let ((case-fold-search nil))
  11590. (looking-at org-todo-line-regexp))
  11591. (if (match-end 2)
  11592. (progn
  11593. (goto-char (match-end 2))
  11594. (insert " [#" news "]"))
  11595. (goto-char (match-beginning 3))
  11596. (insert "[#" news "] "))))
  11597. (org-preserve-lc (org-set-tags nil 'align)))
  11598. (if remove
  11599. (message "Priority removed")
  11600. (message "Priority of current item set to %s" news))))
  11601. (defun org-get-priority (s)
  11602. "Find priority cookie and return priority."
  11603. (if (functionp org-get-priority-function)
  11604. (funcall org-get-priority-function)
  11605. (save-match-data
  11606. (if (not (string-match org-priority-regexp s))
  11607. (* 1000 (- org-lowest-priority org-default-priority))
  11608. (* 1000 (- org-lowest-priority
  11609. (string-to-char (match-string 2 s))))))))
  11610. ;;;; Tags
  11611. (defvar org-agenda-archives-mode)
  11612. (defvar org-map-continue-from nil
  11613. "Position from where mapping should continue.
  11614. Can be set by the action argument to `org-scan-tags' and `org-map-entries'.")
  11615. (defvar org-scanner-tags nil
  11616. "The current tag list while the tags scanner is running.")
  11617. (defvar org-trust-scanner-tags nil
  11618. "Should `org-get-tags-at' use the tags for the scanner.
  11619. This is for internal dynamical scoping only.
  11620. When this is non-nil, the function `org-get-tags-at' will return the value
  11621. of `org-scanner-tags' instead of building the list by itself. This
  11622. can lead to large speed-ups when the tags scanner is used in a file with
  11623. many entries, and when the list of tags is retrieved, for example to
  11624. obtain a list of properties. Building the tags list for each entry in such
  11625. a file becomes an N^2 operation - but with this variable set, it scales
  11626. as N.")
  11627. (defun org-scan-tags (action matcher &optional todo-only start-level)
  11628. "Scan headline tags with inheritance and produce output ACTION.
  11629. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  11630. or `agenda' to produce an entry list for an agenda view. It can also be
  11631. a Lisp form or a function that should be called at each matched headline, in
  11632. this case the return value is a list of all return values from these calls.
  11633. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  11634. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  11635. only lines with a TODO keyword are included in the output.
  11636. START-LEVEL can be a string with asterisks, reducing the scope to
  11637. headlines matching this string."
  11638. (require 'org-agenda)
  11639. (let* ((re (concat "^"
  11640. (if start-level
  11641. ;; Get the correct level to match
  11642. (concat "\\*\\{" (number-to-string start-level) "\\} ")
  11643. org-outline-regexp)
  11644. " *\\(\\<\\("
  11645. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  11646. (org-re
  11647. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*$")))
  11648. (props (list 'face 'default
  11649. 'done-face 'org-agenda-done
  11650. 'undone-face 'default
  11651. 'mouse-face 'highlight
  11652. 'org-not-done-regexp org-not-done-regexp
  11653. 'org-todo-regexp org-todo-regexp
  11654. 'org-complex-heading-regexp org-complex-heading-regexp
  11655. 'help-echo
  11656. (format "mouse-2 or RET jump to org file %s"
  11657. (abbreviate-file-name
  11658. (or (buffer-file-name (buffer-base-buffer))
  11659. (buffer-name (buffer-base-buffer)))))))
  11660. (case-fold-search nil)
  11661. (org-map-continue-from nil)
  11662. lspos tags
  11663. (tags-alist (list (cons 0 org-file-tags)))
  11664. (llast 0) rtn rtn1 level category i txt
  11665. todo marker entry priority)
  11666. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  11667. (setq action (list 'lambda nil action)))
  11668. (save-excursion
  11669. (goto-char (point-min))
  11670. (when (eq action 'sparse-tree)
  11671. (org-overview)
  11672. (org-remove-occur-highlights))
  11673. (while (re-search-forward re nil t)
  11674. (setq org-map-continue-from nil)
  11675. (catch :skip
  11676. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  11677. tags (if (match-end 4) (org-match-string-no-properties 4)))
  11678. (goto-char (setq lspos (match-beginning 0)))
  11679. (setq level (org-reduced-level (funcall outline-level))
  11680. category (org-get-category))
  11681. (setq i llast llast level)
  11682. ;; remove tag lists from same and sublevels
  11683. (while (>= i level)
  11684. (when (setq entry (assoc i tags-alist))
  11685. (setq tags-alist (delete entry tags-alist)))
  11686. (setq i (1- i)))
  11687. ;; add the next tags
  11688. (when tags
  11689. (setq tags (org-split-string tags ":")
  11690. tags-alist
  11691. (cons (cons level tags) tags-alist)))
  11692. ;; compile tags for current headline
  11693. (setq tags-list
  11694. (if org-use-tag-inheritance
  11695. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  11696. tags)
  11697. org-scanner-tags tags-list)
  11698. (when org-use-tag-inheritance
  11699. (setcdr (car tags-alist)
  11700. (mapcar (lambda (x)
  11701. (setq x (copy-sequence x))
  11702. (org-add-prop-inherited x))
  11703. (cdar tags-alist))))
  11704. (when (and tags org-use-tag-inheritance
  11705. (or (not (eq t org-use-tag-inheritance))
  11706. org-tags-exclude-from-inheritance))
  11707. ;; selective inheritance, remove uninherited ones
  11708. (setcdr (car tags-alist)
  11709. (org-remove-uninherited-tags (cdar tags-alist))))
  11710. (when (and
  11711. ;; eval matcher only when the todo condition is OK
  11712. (and (or (not todo-only) (member todo org-not-done-keywords))
  11713. (let ((case-fold-search t)) (eval matcher)))
  11714. ;; Call the skipper, but return t if it does not skip,
  11715. ;; so that the `and' form continues evaluating
  11716. (progn
  11717. (unless (eq action 'sparse-tree) (org-agenda-skip))
  11718. t)
  11719. ;; Check if timestamps are deselecting this entry
  11720. (or (not todo-only)
  11721. (and (member todo org-not-done-keywords)
  11722. (or (not org-agenda-tags-todo-honor-ignore-options)
  11723. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))))
  11724. ;; Extra check for the archive tag
  11725. ;; FIXME: Does the skipper already do this????
  11726. (or
  11727. (not (member org-archive-tag tags-list))
  11728. ;; we have an archive tag, should we use this anyway?
  11729. (or (not org-agenda-skip-archived-trees)
  11730. (and (eq action 'agenda) org-agenda-archives-mode))))
  11731. ;; select this headline
  11732. (cond
  11733. ((eq action 'sparse-tree)
  11734. (and org-highlight-sparse-tree-matches
  11735. (org-get-heading) (match-end 0)
  11736. (org-highlight-new-match
  11737. (match-beginning 1) (match-end 1)))
  11738. (org-show-context 'tags-tree))
  11739. ((eq action 'agenda)
  11740. (setq txt (org-agenda-format-item
  11741. ""
  11742. (concat
  11743. (if (eq org-tags-match-list-sublevels 'indented)
  11744. (make-string (1- level) ?.) "")
  11745. (org-get-heading))
  11746. category
  11747. tags-list)
  11748. priority (org-get-priority txt))
  11749. (goto-char lspos)
  11750. (setq marker (org-agenda-new-marker))
  11751. (org-add-props txt props
  11752. 'org-marker marker 'org-hd-marker marker 'org-category category
  11753. 'todo-state todo
  11754. 'priority priority 'type "tagsmatch")
  11755. (push txt rtn))
  11756. ((functionp action)
  11757. (setq org-map-continue-from nil)
  11758. (save-excursion
  11759. (setq rtn1 (funcall action))
  11760. (push rtn1 rtn)))
  11761. (t (error "Invalid action")))
  11762. ;; if we are to skip sublevels, jump to end of subtree
  11763. (unless org-tags-match-list-sublevels
  11764. (org-end-of-subtree t)
  11765. (backward-char 1))))
  11766. ;; Get the correct position from where to continue
  11767. (if org-map-continue-from
  11768. (goto-char org-map-continue-from)
  11769. (and (= (point) lspos) (end-of-line 1)))))
  11770. (when (and (eq action 'sparse-tree)
  11771. (not org-sparse-tree-open-archived-trees))
  11772. (org-hide-archived-subtrees (point-min) (point-max)))
  11773. (nreverse rtn)))
  11774. (defun org-remove-uninherited-tags (tags)
  11775. "Remove all tags that are not inherited from the list TAGS."
  11776. (cond
  11777. ((eq org-use-tag-inheritance t)
  11778. (if org-tags-exclude-from-inheritance
  11779. (org-delete-all org-tags-exclude-from-inheritance tags)
  11780. tags))
  11781. ((not org-use-tag-inheritance) nil)
  11782. ((stringp org-use-tag-inheritance)
  11783. (delq nil (mapcar
  11784. (lambda (x)
  11785. (if (and (string-match org-use-tag-inheritance x)
  11786. (not (member x org-tags-exclude-from-inheritance)))
  11787. x nil))
  11788. tags)))
  11789. ((listp org-use-tag-inheritance)
  11790. (delq nil (mapcar
  11791. (lambda (x)
  11792. (if (member x org-use-tag-inheritance) x nil))
  11793. tags)))))
  11794. (defvar todo-only) ;; dynamically scoped
  11795. (defun org-match-sparse-tree (&optional todo-only match)
  11796. "Create a sparse tree according to tags string MATCH.
  11797. MATCH can contain positive and negative selection of tags, like
  11798. \"+WORK+URGENT-WITHBOSS\".
  11799. If optional argument TODO-ONLY is non-nil, only select lines that are
  11800. also TODO lines."
  11801. (interactive "P")
  11802. (org-prepare-agenda-buffers (list (current-buffer)))
  11803. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  11804. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  11805. (defvar org-cached-props nil)
  11806. (defun org-cached-entry-get (pom property)
  11807. (if (or (eq t org-use-property-inheritance)
  11808. (and (stringp org-use-property-inheritance)
  11809. (string-match org-use-property-inheritance property))
  11810. (and (listp org-use-property-inheritance)
  11811. (member property org-use-property-inheritance)))
  11812. ;; Caching is not possible, check it directly
  11813. (org-entry-get pom property 'inherit)
  11814. ;; Get all properties, so that we can do complicated checks easily
  11815. (cdr (assoc property (or org-cached-props
  11816. (setq org-cached-props
  11817. (org-entry-properties pom)))))))
  11818. (defun org-global-tags-completion-table (&optional files)
  11819. "Return the list of all tags in all agenda buffer/files.
  11820. Optional FILES argument is a list of files to which can be used
  11821. instead of the agenda files."
  11822. (save-excursion
  11823. (org-uniquify
  11824. (delq nil
  11825. (apply 'append
  11826. (mapcar
  11827. (lambda (file)
  11828. (set-buffer (find-file-noselect file))
  11829. (append (org-get-buffer-tags)
  11830. (mapcar (lambda (x) (if (stringp (car-safe x))
  11831. (list (car-safe x)) nil))
  11832. org-tag-alist)))
  11833. (if (and files (car files))
  11834. files
  11835. (org-agenda-files))))))))
  11836. (defun org-make-tags-matcher (match)
  11837. "Create the TAGS/TODO matcher form for the selection string MATCH."
  11838. ;; todo-only is scoped dynamically into this function, and the function
  11839. ;; may change it if the matcher asks for it.
  11840. (unless match
  11841. ;; Get a new match request, with completion
  11842. (let ((org-last-tags-completion-table
  11843. (org-global-tags-completion-table)))
  11844. (setq match (org-completing-read-no-i
  11845. "Match: " 'org-tags-completion-function nil nil nil
  11846. 'org-tags-history))))
  11847. ;; Parse the string and create a lisp form
  11848. (let ((match0 match)
  11849. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\(\\(?:[[:alnum:]_]+\\(?:\\\\-\\)*\\)+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@#%]+\\)"))
  11850. minus tag mm
  11851. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  11852. orterms term orlist re-p str-p level-p level-op time-p
  11853. prop-p pn pv po gv rest)
  11854. (if (string-match "/+" match)
  11855. ;; match contains also a todo-matching request
  11856. (progn
  11857. (setq tagsmatch (substring match 0 (match-beginning 0))
  11858. todomatch (substring match (match-end 0)))
  11859. (if (string-match "^!" todomatch)
  11860. (setq todo-only t todomatch (substring todomatch 1)))
  11861. (if (string-match "^\\s-*$" todomatch)
  11862. (setq todomatch nil)))
  11863. ;; only matching tags
  11864. (setq tagsmatch match todomatch nil))
  11865. ;; Make the tags matcher
  11866. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  11867. (setq tagsmatcher t)
  11868. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  11869. (while (setq term (pop orterms))
  11870. (while (and (equal (substring term -1) "\\") orterms)
  11871. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  11872. (while (string-match re term)
  11873. (setq rest (substring term (match-end 0))
  11874. minus (and (match-end 1)
  11875. (equal (match-string 1 term) "-"))
  11876. tag (save-match-data (replace-regexp-in-string
  11877. "\\\\-" "-"
  11878. (match-string 2 term)))
  11879. re-p (equal (string-to-char tag) ?{)
  11880. level-p (match-end 4)
  11881. prop-p (match-end 5)
  11882. mm (cond
  11883. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  11884. (level-p
  11885. (setq level-op (org-op-to-function (match-string 3 term)))
  11886. `(,level-op level ,(string-to-number
  11887. (match-string 4 term))))
  11888. (prop-p
  11889. (setq pn (match-string 5 term)
  11890. po (match-string 6 term)
  11891. pv (match-string 7 term)
  11892. re-p (equal (string-to-char pv) ?{)
  11893. str-p (equal (string-to-char pv) ?\")
  11894. time-p (save-match-data
  11895. (string-match "^\"[[<].*[]>]\"$" pv))
  11896. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  11897. (if time-p (setq pv (org-matcher-time pv)))
  11898. (setq po (org-op-to-function po (if time-p 'time str-p)))
  11899. (cond
  11900. ((equal pn "CATEGORY")
  11901. (setq gv '(get-text-property (point) 'org-category)))
  11902. ((equal pn "TODO")
  11903. (setq gv 'todo))
  11904. (t
  11905. (setq gv `(org-cached-entry-get nil ,pn))))
  11906. (if re-p
  11907. (if (eq po 'org<>)
  11908. `(not (string-match ,pv (or ,gv "")))
  11909. `(string-match ,pv (or ,gv "")))
  11910. (if str-p
  11911. `(,po (or ,gv "") ,pv)
  11912. `(,po (string-to-number (or ,gv ""))
  11913. ,(string-to-number pv) ))))
  11914. (t `(member ,tag tags-list)))
  11915. mm (if minus (list 'not mm) mm)
  11916. term rest)
  11917. (push mm tagsmatcher))
  11918. (push (if (> (length tagsmatcher) 1)
  11919. (cons 'and tagsmatcher)
  11920. (car tagsmatcher))
  11921. orlist)
  11922. (setq tagsmatcher nil))
  11923. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  11924. (setq tagsmatcher
  11925. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  11926. ;; Make the todo matcher
  11927. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  11928. (setq todomatcher t)
  11929. (setq orterms (org-split-string todomatch "|") orlist nil)
  11930. (while (setq term (pop orterms))
  11931. (while (string-match re term)
  11932. (setq minus (and (match-end 1)
  11933. (equal (match-string 1 term) "-"))
  11934. kwd (match-string 2 term)
  11935. re-p (equal (string-to-char kwd) ?{)
  11936. term (substring term (match-end 0))
  11937. mm (if re-p
  11938. `(string-match ,(substring kwd 1 -1) todo)
  11939. (list 'equal 'todo kwd))
  11940. mm (if minus (list 'not mm) mm))
  11941. (push mm todomatcher))
  11942. (push (if (> (length todomatcher) 1)
  11943. (cons 'and todomatcher)
  11944. (car todomatcher))
  11945. orlist)
  11946. (setq todomatcher nil))
  11947. (setq todomatcher (if (> (length orlist) 1)
  11948. (cons 'or orlist) (car orlist))))
  11949. ;; Return the string and lisp forms of the matcher
  11950. (setq matcher (if todomatcher
  11951. (list 'and tagsmatcher todomatcher)
  11952. tagsmatcher))
  11953. (cons match0 matcher)))
  11954. (defun org-op-to-function (op &optional stringp)
  11955. "Turn an operator into the appropriate function."
  11956. (setq op
  11957. (cond
  11958. ((equal op "<" ) '(< string< org-time<))
  11959. ((equal op ">" ) '(> org-string> org-time>))
  11960. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  11961. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  11962. ((member op '("=" "==")) '(= string= org-time=))
  11963. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  11964. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  11965. (defun org<> (a b) (not (= a b)))
  11966. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  11967. (defun org-string>= (a b) (not (string< a b)))
  11968. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  11969. (defun org-string<> (a b) (not (string= a b)))
  11970. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  11971. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  11972. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  11973. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  11974. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  11975. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  11976. (defun org-2ft (s)
  11977. "Convert S to a floating point time.
  11978. If S is already a number, just return it. If it is a string, parse
  11979. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  11980. (cond
  11981. ((numberp s) s)
  11982. ((stringp s)
  11983. (condition-case nil
  11984. (float-time (apply 'encode-time (org-parse-time-string s)))
  11985. (error 0.)))
  11986. (t 0.)))
  11987. (defun org-time-today ()
  11988. "Time in seconds today at 0:00.
  11989. Returns the float number of seconds since the beginning of the
  11990. epoch to the beginning of today (00:00)."
  11991. (float-time (apply 'encode-time
  11992. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  11993. (defun org-matcher-time (s)
  11994. "Interpret a time comparison value."
  11995. (save-match-data
  11996. (cond
  11997. ((string= s "<now>") (float-time))
  11998. ((string= s "<today>") (org-time-today))
  11999. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  12000. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  12001. ((string-match "^<\\([-+][0-9]+\\)\\([dwmy]\\)>$" s)
  12002. (+ (org-time-today)
  12003. (* (string-to-number (match-string 1 s))
  12004. (cdr (assoc (match-string 2 s)
  12005. '(("d" . 86400.0) ("w" . 604800.0)
  12006. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  12007. (t (org-2ft s)))))
  12008. (defun org-match-any-p (re list)
  12009. "Does re match any element of list?"
  12010. (setq list (mapcar (lambda (x) (string-match re x)) list))
  12011. (delq nil list))
  12012. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  12013. (defvar org-tags-overlay (make-overlay 1 1))
  12014. (org-detach-overlay org-tags-overlay)
  12015. (defun org-get-local-tags-at (&optional pos)
  12016. "Get a list of tags defined in the current headline."
  12017. (org-get-tags-at pos 'local))
  12018. (defun org-get-local-tags ()
  12019. "Get a list of tags defined in the current headline."
  12020. (org-get-tags-at nil 'local))
  12021. (defun org-get-tags-at (&optional pos local)
  12022. "Get a list of all headline tags applicable at POS.
  12023. POS defaults to point. If tags are inherited, the list contains
  12024. the targets in the same sequence as the headlines appear, i.e.
  12025. the tags of the current headline come last.
  12026. When LOCAL is non-nil, only return tags from the current headline,
  12027. ignore inherited ones."
  12028. (interactive)
  12029. (if (and org-trust-scanner-tags
  12030. (or (not pos) (equal pos (point)))
  12031. (not local))
  12032. org-scanner-tags
  12033. (let (tags ltags lastpos parent)
  12034. (save-excursion
  12035. (save-restriction
  12036. (widen)
  12037. (goto-char (or pos (point)))
  12038. (save-match-data
  12039. (catch 'done
  12040. (condition-case nil
  12041. (progn
  12042. (org-back-to-heading t)
  12043. (while (not (equal lastpos (point)))
  12044. (setq lastpos (point))
  12045. (when (looking-at
  12046. (org-re "[^\r\n]+?:\\([[:alnum:]_@#%:]+\\):[ \t]*$"))
  12047. (setq ltags (org-split-string
  12048. (org-match-string-no-properties 1) ":"))
  12049. (when parent
  12050. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  12051. (setq tags (append
  12052. (if parent
  12053. (org-remove-uninherited-tags ltags)
  12054. ltags)
  12055. tags)))
  12056. (or org-use-tag-inheritance (throw 'done t))
  12057. (if local (throw 'done t))
  12058. (or (org-up-heading-safe) (error nil))
  12059. (setq parent t)))
  12060. (error nil)))))
  12061. (if local
  12062. tags
  12063. (append (org-remove-uninherited-tags org-file-tags) tags))))))
  12064. (defun org-add-prop-inherited (s)
  12065. (add-text-properties 0 (length s) '(inherited t) s)
  12066. s)
  12067. (defun org-toggle-tag (tag &optional onoff)
  12068. "Toggle the tag TAG for the current line.
  12069. If ONOFF is `on' or `off', don't toggle but set to this state."
  12070. (let (res current)
  12071. (save-excursion
  12072. (org-back-to-heading t)
  12073. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t]*$")
  12074. (point-at-eol) t)
  12075. (progn
  12076. (setq current (match-string 1))
  12077. (replace-match ""))
  12078. (setq current ""))
  12079. (setq current (nreverse (org-split-string current ":")))
  12080. (cond
  12081. ((eq onoff 'on)
  12082. (setq res t)
  12083. (or (member tag current) (push tag current)))
  12084. ((eq onoff 'off)
  12085. (or (not (member tag current)) (setq current (delete tag current))))
  12086. (t (if (member tag current)
  12087. (setq current (delete tag current))
  12088. (setq res t)
  12089. (push tag current))))
  12090. (end-of-line 1)
  12091. (if current
  12092. (progn
  12093. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  12094. (org-set-tags nil t))
  12095. (delete-horizontal-space))
  12096. (run-hooks 'org-after-tags-change-hook))
  12097. res))
  12098. (defun org-align-tags-here (to-col)
  12099. ;; Assumes that this is a headline
  12100. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  12101. (beginning-of-line 1)
  12102. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  12103. (< pos (match-beginning 2)))
  12104. (progn
  12105. (setq tags-l (- (match-end 2) (match-beginning 2)))
  12106. (goto-char (match-beginning 1))
  12107. (insert " ")
  12108. (delete-region (point) (1+ (match-beginning 2)))
  12109. (setq ncol (max (current-column)
  12110. (1+ col)
  12111. (if (> to-col 0)
  12112. to-col
  12113. (- (abs to-col) tags-l))))
  12114. (setq p (point))
  12115. (insert (make-string (- ncol (current-column)) ?\ ))
  12116. (setq ncol (current-column))
  12117. (when indent-tabs-mode (tabify p (point-at-eol)))
  12118. (org-move-to-column (min ncol col) t))
  12119. (goto-char pos))))
  12120. (defun org-set-tags-command (&optional arg just-align)
  12121. "Call the set-tags command for the current entry."
  12122. (interactive "P")
  12123. (if (or (org-at-heading-p) (and arg (org-before-first-heading-p)))
  12124. (org-set-tags arg just-align)
  12125. (save-excursion
  12126. (org-back-to-heading t)
  12127. (org-set-tags arg just-align))))
  12128. (defun org-set-tags-to (data)
  12129. "Set the tags of the current entry to DATA, replacing the current tags.
  12130. DATA may be a tags string like :aa:bb:cc:, or a list of tags.
  12131. If DATA is nil or the empty string, any tags will be removed."
  12132. (interactive "sTags: ")
  12133. (setq data
  12134. (cond
  12135. ((eq data nil) "")
  12136. ((equal data "") "")
  12137. ((stringp data)
  12138. (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
  12139. ":"))
  12140. ((listp data)
  12141. (concat ":" (mapconcat 'identity data ":") ":"))
  12142. (t nil)))
  12143. (when data
  12144. (save-excursion
  12145. (org-back-to-heading t)
  12146. (when (looking-at org-complex-heading-regexp)
  12147. (if (match-end 5)
  12148. (progn
  12149. (goto-char (match-beginning 5))
  12150. (insert data)
  12151. (delete-region (point) (point-at-eol))
  12152. (org-set-tags nil 'align))
  12153. (goto-char (point-at-eol))
  12154. (insert " " data)
  12155. (org-set-tags nil 'align)))
  12156. (beginning-of-line 1)
  12157. (if (looking-at ".*?\\([ \t]+\\)$")
  12158. (delete-region (match-beginning 1) (match-end 1))))))
  12159. (defun org-align-all-tags ()
  12160. "Align the tags i all headings."
  12161. (interactive)
  12162. (save-excursion
  12163. (or (ignore-errors (org-back-to-heading t))
  12164. (outline-next-heading))
  12165. (if (org-at-heading-p)
  12166. (org-set-tags t)
  12167. (message "No headings"))))
  12168. (defvar org-indent-indentation-per-level)
  12169. (defun org-set-tags (&optional arg just-align)
  12170. "Set the tags for the current headline.
  12171. With prefix ARG, realign all tags in headings in the current buffer."
  12172. (interactive "P")
  12173. (let* ((re org-outline-regexp-bol)
  12174. (current (unless arg (org-get-tags-string)))
  12175. (col (current-column))
  12176. (org-setting-tags t)
  12177. table current-tags inherited-tags ; computed below when needed
  12178. tags p0 c0 c1 rpl di tc level)
  12179. (if arg
  12180. (save-excursion
  12181. (goto-char (point-min))
  12182. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  12183. (while (re-search-forward re nil t)
  12184. (org-set-tags nil t)
  12185. (end-of-line 1)))
  12186. (message "All tags realigned to column %d" org-tags-column))
  12187. (if just-align
  12188. (setq tags current)
  12189. ;; Get a new set of tags from the user
  12190. (save-excursion
  12191. (setq table (append org-tag-persistent-alist
  12192. (or org-tag-alist (org-get-buffer-tags))
  12193. (and
  12194. org-complete-tags-always-offer-all-agenda-tags
  12195. (org-global-tags-completion-table
  12196. (org-agenda-files))))
  12197. org-last-tags-completion-table table
  12198. current-tags (org-split-string current ":")
  12199. inherited-tags (nreverse
  12200. (nthcdr (length current-tags)
  12201. (nreverse (org-get-tags-at))))
  12202. tags
  12203. (if (or (eq t org-use-fast-tag-selection)
  12204. (and org-use-fast-tag-selection
  12205. (delq nil (mapcar 'cdr table))))
  12206. (org-fast-tag-selection
  12207. current-tags inherited-tags table
  12208. (if org-fast-tag-selection-include-todo
  12209. org-todo-key-alist))
  12210. (let ((org-add-colon-after-tag-completion t))
  12211. (org-trim
  12212. (org-icompleting-read "Tags: "
  12213. 'org-tags-completion-function
  12214. nil nil current 'org-tags-history))))))
  12215. (while (string-match "[-+&]+" tags)
  12216. ;; No boolean logic, just a list
  12217. (setq tags (replace-match ":" t t tags))))
  12218. (setq tags (replace-regexp-in-string "[,]" ":" tags))
  12219. (if org-tags-sort-function
  12220. (setq tags (mapconcat 'identity
  12221. (sort (org-split-string
  12222. tags (org-re "[^[:alnum:]_@#%]+"))
  12223. org-tags-sort-function) ":")))
  12224. (if (string-match "\\`[\t ]*\\'" tags)
  12225. (setq tags "")
  12226. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  12227. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  12228. ;; Insert new tags at the correct column
  12229. (beginning-of-line 1)
  12230. (setq level (or (and (looking-at org-outline-regexp)
  12231. (- (match-end 0) (point) 1))
  12232. 1))
  12233. (cond
  12234. ((and (equal current "") (equal tags "")))
  12235. ((re-search-forward
  12236. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  12237. (point-at-eol) t)
  12238. (if (equal tags "")
  12239. (setq rpl "")
  12240. (goto-char (match-beginning 0))
  12241. (setq c0 (current-column)
  12242. ;; compute offset for the case of org-indent-mode active
  12243. di (if org-indent-mode
  12244. (* (1- org-indent-indentation-per-level) (1- level))
  12245. 0)
  12246. p0 (if (equal (char-before) ?*) (1+ (point)) (point))
  12247. tc (+ org-tags-column (if (> org-tags-column 0) (- di) di))
  12248. c1 (max (1+ c0) (if (> tc 0) tc (- (- tc) (length tags))))
  12249. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  12250. (replace-match rpl t t)
  12251. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  12252. tags)
  12253. (t (error "Tags alignment failed")))
  12254. (org-move-to-column col)
  12255. (unless just-align
  12256. (run-hooks 'org-after-tags-change-hook)))))
  12257. (defun org-change-tag-in-region (beg end tag off)
  12258. "Add or remove TAG for each entry in the region.
  12259. This works in the agenda, and also in an org-mode buffer."
  12260. (interactive
  12261. (list (region-beginning) (region-end)
  12262. (let ((org-last-tags-completion-table
  12263. (if (eq major-mode 'org-mode)
  12264. (org-get-buffer-tags)
  12265. (org-global-tags-completion-table))))
  12266. (org-icompleting-read
  12267. "Tag: " 'org-tags-completion-function nil nil nil
  12268. 'org-tags-history))
  12269. (progn
  12270. (message "[s]et or [r]emove? ")
  12271. (equal (read-char-exclusive) ?r))))
  12272. (if (fboundp 'deactivate-mark) (deactivate-mark))
  12273. (let ((agendap (equal major-mode 'org-agenda-mode))
  12274. l1 l2 m buf pos newhead (cnt 0))
  12275. (goto-char end)
  12276. (setq l2 (1- (org-current-line)))
  12277. (goto-char beg)
  12278. (setq l1 (org-current-line))
  12279. (loop for l from l1 to l2 do
  12280. (org-goto-line l)
  12281. (setq m (get-text-property (point) 'org-hd-marker))
  12282. (when (or (and (eq major-mode 'org-mode) (org-at-heading-p))
  12283. (and agendap m))
  12284. (setq buf (if agendap (marker-buffer m) (current-buffer))
  12285. pos (if agendap m (point)))
  12286. (with-current-buffer buf
  12287. (save-excursion
  12288. (save-restriction
  12289. (goto-char pos)
  12290. (setq cnt (1+ cnt))
  12291. (org-toggle-tag tag (if off 'off 'on))
  12292. (setq newhead (org-get-heading)))))
  12293. (and agendap (org-agenda-change-all-lines newhead m))))
  12294. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  12295. (defun org-tags-completion-function (string predicate &optional flag)
  12296. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  12297. (confirm (lambda (x) (stringp (car x)))))
  12298. (if (string-match "^\\(.*[-+:&,|]\\)\\([^-+:&,|]*\\)$" string)
  12299. (setq s1 (match-string 1 string)
  12300. s2 (match-string 2 string))
  12301. (setq s1 "" s2 string))
  12302. (cond
  12303. ((eq flag nil)
  12304. ;; try completion
  12305. (setq rtn (try-completion s2 ctable confirm))
  12306. (if (stringp rtn)
  12307. (setq rtn
  12308. (concat s1 s2 (substring rtn (length s2))
  12309. (if (and org-add-colon-after-tag-completion
  12310. (assoc rtn ctable))
  12311. ":" ""))))
  12312. rtn)
  12313. ((eq flag t)
  12314. ;; all-completions
  12315. (all-completions s2 ctable confirm)
  12316. )
  12317. ((eq flag 'lambda)
  12318. ;; exact match?
  12319. (assoc s2 ctable)))
  12320. ))
  12321. (defun org-fast-tag-insert (kwd tags face &optional end)
  12322. "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
  12323. (insert (format "%-12s" (concat kwd ":"))
  12324. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  12325. (or end "")))
  12326. (defun org-fast-tag-show-exit (flag)
  12327. (save-excursion
  12328. (org-goto-line 3)
  12329. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  12330. (replace-match ""))
  12331. (when flag
  12332. (end-of-line 1)
  12333. (org-move-to-column (- (window-width) 19) t)
  12334. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  12335. (defun org-set-current-tags-overlay (current prefix)
  12336. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  12337. (if (featurep 'xemacs)
  12338. (org-overlay-display org-tags-overlay (concat prefix s)
  12339. 'secondary-selection)
  12340. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  12341. (org-overlay-display org-tags-overlay (concat prefix s)))))
  12342. (defvar org-last-tag-selection-key nil)
  12343. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  12344. "Fast tag selection with single keys.
  12345. CURRENT is the current list of tags in the headline, INHERITED is the
  12346. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  12347. possibly with grouping information. TODO-TABLE is a similar table with
  12348. TODO keywords, should these have keys assigned to them.
  12349. If the keys are nil, a-z are automatically assigned.
  12350. Returns the new tags string, or nil to not change the current settings."
  12351. (let* ((fulltable (append table todo-table))
  12352. (maxlen (apply 'max (mapcar
  12353. (lambda (x)
  12354. (if (stringp (car x)) (string-width (car x)) 0))
  12355. fulltable)))
  12356. (buf (current-buffer))
  12357. (expert (eq org-fast-tag-selection-single-key 'expert))
  12358. (buffer-tags nil)
  12359. (fwidth (+ maxlen 3 1 3))
  12360. (ncol (/ (- (window-width) 4) fwidth))
  12361. (i-face 'org-done)
  12362. (c-face 'org-todo)
  12363. tg cnt e c char c1 c2 ntable tbl rtn
  12364. ov-start ov-end ov-prefix
  12365. (exit-after-next org-fast-tag-selection-single-key)
  12366. (done-keywords org-done-keywords)
  12367. groups ingroup)
  12368. (save-excursion
  12369. (beginning-of-line 1)
  12370. (if (looking-at
  12371. (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  12372. (setq ov-start (match-beginning 1)
  12373. ov-end (match-end 1)
  12374. ov-prefix "")
  12375. (setq ov-start (1- (point-at-eol))
  12376. ov-end (1+ ov-start))
  12377. (skip-chars-forward "^\n\r")
  12378. (setq ov-prefix
  12379. (concat
  12380. (buffer-substring (1- (point)) (point))
  12381. (if (> (current-column) org-tags-column)
  12382. " "
  12383. (make-string (- org-tags-column (current-column)) ?\ ))))))
  12384. (move-overlay org-tags-overlay ov-start ov-end)
  12385. (save-window-excursion
  12386. (if expert
  12387. (set-buffer (get-buffer-create " *Org tags*"))
  12388. (delete-other-windows)
  12389. (split-window-vertically)
  12390. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  12391. (erase-buffer)
  12392. (org-set-local 'org-done-keywords done-keywords)
  12393. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  12394. (org-fast-tag-insert "Current" current c-face "\n\n")
  12395. (org-fast-tag-show-exit exit-after-next)
  12396. (org-set-current-tags-overlay current ov-prefix)
  12397. (setq tbl fulltable char ?a cnt 0)
  12398. (while (setq e (pop tbl))
  12399. (cond
  12400. ((equal (car e) :startgroup)
  12401. (push '() groups) (setq ingroup t)
  12402. (when (not (= cnt 0))
  12403. (setq cnt 0)
  12404. (insert "\n"))
  12405. (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
  12406. ((equal (car e) :endgroup)
  12407. (setq ingroup nil cnt 0)
  12408. (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
  12409. ((equal e '(:newline))
  12410. (when (not (= cnt 0))
  12411. (setq cnt 0)
  12412. (insert "\n")
  12413. (setq e (car tbl))
  12414. (while (equal (car tbl) '(:newline))
  12415. (insert "\n")
  12416. (setq tbl (cdr tbl)))))
  12417. (t
  12418. (setq tg (copy-sequence (car e)) c2 nil)
  12419. (if (cdr e)
  12420. (setq c (cdr e))
  12421. ;; automatically assign a character.
  12422. (setq c1 (string-to-char
  12423. (downcase (substring
  12424. tg (if (= (string-to-char tg) ?@) 1 0)))))
  12425. (if (or (rassoc c1 ntable) (rassoc c1 table))
  12426. (while (or (rassoc char ntable) (rassoc char table))
  12427. (setq char (1+ char)))
  12428. (setq c2 c1))
  12429. (setq c (or c2 char)))
  12430. (if ingroup (push tg (car groups)))
  12431. (setq tg (org-add-props tg nil 'face
  12432. (cond
  12433. ((not (assoc tg table))
  12434. (org-get-todo-face tg))
  12435. ((member tg current) c-face)
  12436. ((member tg inherited) i-face)
  12437. (t nil))))
  12438. (if (and (= cnt 0) (not ingroup)) (insert " "))
  12439. (insert "[" c "] " tg (make-string
  12440. (- fwidth 4 (length tg)) ?\ ))
  12441. (push (cons tg c) ntable)
  12442. (when (= (setq cnt (1+ cnt)) ncol)
  12443. (insert "\n")
  12444. (if ingroup (insert " "))
  12445. (setq cnt 0)))))
  12446. (setq ntable (nreverse ntable))
  12447. (insert "\n")
  12448. (goto-char (point-min))
  12449. (if (not expert) (org-fit-window-to-buffer))
  12450. (setq rtn
  12451. (catch 'exit
  12452. (while t
  12453. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
  12454. (if (not groups) "no " "")
  12455. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  12456. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  12457. (setq org-last-tag-selection-key c)
  12458. (cond
  12459. ((= c ?\r) (throw 'exit t))
  12460. ((= c ?!)
  12461. (setq groups (not groups))
  12462. (goto-char (point-min))
  12463. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  12464. ((= c ?\C-c)
  12465. (if (not expert)
  12466. (org-fast-tag-show-exit
  12467. (setq exit-after-next (not exit-after-next)))
  12468. (setq expert nil)
  12469. (delete-other-windows)
  12470. (set-window-buffer (split-window-vertically) " *Org tags*")
  12471. (org-switch-to-buffer-other-window " *Org tags*")
  12472. (org-fit-window-to-buffer)))
  12473. ((or (= c ?\C-g)
  12474. (and (= c ?q) (not (rassoc c ntable))))
  12475. (org-detach-overlay org-tags-overlay)
  12476. (setq quit-flag t))
  12477. ((= c ?\ )
  12478. (setq current nil)
  12479. (if exit-after-next (setq exit-after-next 'now)))
  12480. ((= c ?\t)
  12481. (condition-case nil
  12482. (setq tg (org-icompleting-read
  12483. "Tag: "
  12484. (or buffer-tags
  12485. (with-current-buffer buf
  12486. (org-get-buffer-tags)))))
  12487. (quit (setq tg "")))
  12488. (when (string-match "\\S-" tg)
  12489. (add-to-list 'buffer-tags (list tg))
  12490. (if (member tg current)
  12491. (setq current (delete tg current))
  12492. (push tg current)))
  12493. (if exit-after-next (setq exit-after-next 'now)))
  12494. ((setq e (rassoc c todo-table) tg (car e))
  12495. (with-current-buffer buf
  12496. (save-excursion (org-todo tg)))
  12497. (if exit-after-next (setq exit-after-next 'now)))
  12498. ((setq e (rassoc c ntable) tg (car e))
  12499. (if (member tg current)
  12500. (setq current (delete tg current))
  12501. (loop for g in groups do
  12502. (if (member tg g)
  12503. (mapc (lambda (x)
  12504. (setq current (delete x current)))
  12505. g)))
  12506. (push tg current))
  12507. (if exit-after-next (setq exit-after-next 'now))))
  12508. ;; Create a sorted list
  12509. (setq current
  12510. (sort current
  12511. (lambda (a b)
  12512. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  12513. (if (eq exit-after-next 'now) (throw 'exit t))
  12514. (goto-char (point-min))
  12515. (beginning-of-line 2)
  12516. (delete-region (point) (point-at-eol))
  12517. (org-fast-tag-insert "Current" current c-face)
  12518. (org-set-current-tags-overlay current ov-prefix)
  12519. (while (re-search-forward
  12520. (org-re "\\[.\\] \\([[:alnum:]_@#%]+\\)") nil t)
  12521. (setq tg (match-string 1))
  12522. (add-text-properties
  12523. (match-beginning 1) (match-end 1)
  12524. (list 'face
  12525. (cond
  12526. ((member tg current) c-face)
  12527. ((member tg inherited) i-face)
  12528. (t (get-text-property (match-beginning 1) 'face))))))
  12529. (goto-char (point-min)))))
  12530. (org-detach-overlay org-tags-overlay)
  12531. (if rtn
  12532. (mapconcat 'identity current ":")
  12533. nil))))
  12534. (defun org-get-tags-string ()
  12535. "Get the TAGS string in the current headline."
  12536. (unless (org-at-heading-p t)
  12537. (error "Not on a heading"))
  12538. (save-excursion
  12539. (beginning-of-line 1)
  12540. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  12541. (org-match-string-no-properties 1)
  12542. "")))
  12543. (defun org-get-tags ()
  12544. "Get the list of tags specified in the current headline."
  12545. (org-split-string (org-get-tags-string) ":"))
  12546. (defun org-get-buffer-tags ()
  12547. "Get a table of all tags used in the buffer, for completion."
  12548. (let (tags)
  12549. (save-excursion
  12550. (goto-char (point-min))
  12551. (while (re-search-forward
  12552. (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t\r\n]") nil t)
  12553. (when (equal (char-after (point-at-bol 0)) ?*)
  12554. (mapc (lambda (x) (add-to-list 'tags x))
  12555. (org-split-string (org-match-string-no-properties 1) ":")))))
  12556. (mapc (lambda (s) (add-to-list 'tags s)) org-file-tags)
  12557. (mapcar 'list tags)))
  12558. ;;;; The mapping API
  12559. ;;;###autoload
  12560. (defun org-map-entries (func &optional match scope &rest skip)
  12561. "Call FUNC at each headline selected by MATCH in SCOPE.
  12562. FUNC is a function or a lisp form. The function will be called without
  12563. arguments, with the cursor positioned at the beginning of the headline.
  12564. The return values of all calls to the function will be collected and
  12565. returned as a list.
  12566. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  12567. does not need to preserve point. After evaluation, the cursor will be
  12568. moved to the end of the line (presumably of the headline of the
  12569. processed entry) and search continues from there. Under some
  12570. circumstances, this may not produce the wanted results. For example,
  12571. if you have removed (e.g. archived) the current (sub)tree it could
  12572. mean that the next entry will be skipped entirely. In such cases, you
  12573. can specify the position from where search should continue by making
  12574. FUNC set the variable `org-map-continue-from' to the desired buffer
  12575. position.
  12576. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  12577. Only headlines that are matched by this query will be considered during
  12578. the iteration. When MATCH is nil or t, all headlines will be
  12579. visited by the iteration.
  12580. SCOPE determines the scope of this command. It can be any of:
  12581. nil The current buffer, respecting the restriction if any
  12582. tree The subtree started with the entry at point
  12583. region The entries within the active region, if any
  12584. region-start-level
  12585. The entries within the active region, but only those at
  12586. the same level than the first one.
  12587. file The current buffer, without restriction
  12588. file-with-archives
  12589. The current buffer, and any archives associated with it
  12590. agenda All agenda files
  12591. agenda-with-archives
  12592. All agenda files with any archive files associated with them
  12593. \(file1 file2 ...)
  12594. If this is a list, all files in the list will be scanned
  12595. The remaining args are treated as settings for the skipping facilities of
  12596. the scanner. The following items can be given here:
  12597. archive skip trees with the archive tag.
  12598. comment skip trees with the COMMENT keyword
  12599. function or Emacs Lisp form:
  12600. will be used as value for `org-agenda-skip-function', so whenever
  12601. the function returns t, FUNC will not be called for that
  12602. entry and search will continue from the point where the
  12603. function leaves it.
  12604. If your function needs to retrieve the tags including inherited tags
  12605. at the *current* entry, you can use the value of the variable
  12606. `org-scanner-tags' which will be much faster than getting the value
  12607. with `org-get-tags-at'. If your function gets properties with
  12608. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  12609. to t around the call to `org-entry-properties' to get the same speedup.
  12610. Note that if your function moves around to retrieve tags and properties at
  12611. a *different* entry, you cannot use these techniques."
  12612. (unless (and (or (eq scope 'region) (eq scope 'region-start-level))
  12613. (not (org-region-active-p)))
  12614. (let* ((org-agenda-archives-mode nil) ; just to make sure
  12615. (org-agenda-skip-archived-trees (memq 'archive skip))
  12616. (org-agenda-skip-comment-trees (memq 'comment skip))
  12617. (org-agenda-skip-function
  12618. (car (org-delete-all '(comment archive) skip)))
  12619. (org-tags-match-list-sublevels t)
  12620. (start-level (eq scope 'region-start-level))
  12621. matcher file res
  12622. org-todo-keywords-for-agenda
  12623. org-done-keywords-for-agenda
  12624. org-todo-keyword-alist-for-agenda
  12625. org-drawers-for-agenda
  12626. org-tag-alist-for-agenda)
  12627. (cond
  12628. ((eq match t) (setq matcher t))
  12629. ((eq match nil) (setq matcher t))
  12630. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  12631. (save-excursion
  12632. (save-restriction
  12633. (cond ((eq scope 'tree)
  12634. (org-back-to-heading t)
  12635. (org-narrow-to-subtree)
  12636. (setq scope nil))
  12637. ((and (or (eq scope 'region) (eq scope 'region-start-level))
  12638. (org-region-active-p))
  12639. ;; If needed, set start-level to a string like "2"
  12640. (when start-level
  12641. (save-excursion
  12642. (goto-char (region-beginning))
  12643. (unless (org-at-heading-p) (outline-next-heading))
  12644. (setq start-level (org-current-level))))
  12645. (narrow-to-region (region-beginning)
  12646. (save-excursion
  12647. (goto-char (region-end))
  12648. (unless (and (bolp) (org-at-heading-p))
  12649. (outline-next-heading))
  12650. (point)))
  12651. (setq scope nil)))
  12652. (if (not scope)
  12653. (progn
  12654. (org-prepare-agenda-buffers
  12655. (list (buffer-file-name (current-buffer))))
  12656. (setq res (org-scan-tags func matcher nil start-level)))
  12657. ;; Get the right scope
  12658. (cond
  12659. ((and scope (listp scope) (symbolp (car scope)))
  12660. (setq scope (eval scope)))
  12661. ((eq scope 'agenda)
  12662. (setq scope (org-agenda-files t)))
  12663. ((eq scope 'agenda-with-archives)
  12664. (setq scope (org-agenda-files t))
  12665. (setq scope (org-add-archive-files scope)))
  12666. ((eq scope 'file)
  12667. (setq scope (list (buffer-file-name))))
  12668. ((eq scope 'file-with-archives)
  12669. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  12670. (org-prepare-agenda-buffers scope)
  12671. (while (setq file (pop scope))
  12672. (with-current-buffer (org-find-base-buffer-visiting file)
  12673. (save-excursion
  12674. (save-restriction
  12675. (widen)
  12676. (goto-char (point-min))
  12677. (setq res (append res (org-scan-tags func matcher))))))))))
  12678. res)))
  12679. ;;;; Properties
  12680. ;;; Setting and retrieving properties
  12681. (defconst org-special-properties
  12682. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  12683. "TIMESTAMP" "TIMESTAMP_IA" "BLOCKED" "FILE" "CLOCKSUM")
  12684. "The special properties valid in Org-mode.
  12685. These are properties that are not defined in the property drawer,
  12686. but in some other way.")
  12687. (defconst org-default-properties
  12688. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  12689. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  12690. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  12691. "EXPORT_OPTIONS" "EXPORT_TEXT" "EXPORT_FILE_NAME"
  12692. "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
  12693. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER" "REPEAT_TO_STATE"
  12694. "CLOCK_MODELINE_TOTAL" "STYLE" "HTML_CONTAINER_CLASS")
  12695. "Some properties that are used by Org-mode for various purposes.
  12696. Being in this list makes sure that they are offered for completion.")
  12697. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  12698. "Regular expression matching the first line of a property drawer.")
  12699. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  12700. "Regular expression matching the last line of a property drawer.")
  12701. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  12702. "Regular expression matching the first line of a property drawer.")
  12703. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  12704. "Regular expression matching the first line of a property drawer.")
  12705. (defconst org-property-drawer-re
  12706. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  12707. org-property-end-re "\\)\n?")
  12708. "Matches an entire property drawer.")
  12709. (defconst org-clock-drawer-re
  12710. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  12711. org-property-end-re "\\)\n?")
  12712. "Matches an entire clock drawer.")
  12713. (defsubst org-re-property (property)
  12714. "Return a regexp matching PROPERTY.
  12715. Match group 1 will be set to the value "
  12716. (concat "^[ \t]*:" (regexp-quote property) ":[ \t]*\\(\\S-.*\\)"))
  12717. (defun org-property-action ()
  12718. "Do an action on properties."
  12719. (interactive)
  12720. (let (c)
  12721. (org-at-property-p)
  12722. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  12723. (setq c (read-char-exclusive))
  12724. (cond
  12725. ((equal c ?s)
  12726. (call-interactively 'org-set-property))
  12727. ((equal c ?d)
  12728. (call-interactively 'org-delete-property))
  12729. ((equal c ?D)
  12730. (call-interactively 'org-delete-property-globally))
  12731. ((equal c ?c)
  12732. (call-interactively 'org-compute-property-at-point))
  12733. (t (error "No such property action %c" c)))))
  12734. (defun org-set-effort (&optional value)
  12735. "Set the effort property of the current entry.
  12736. With numerical prefix arg, use the nth allowed value, 0 stands for the 10th
  12737. allowed value."
  12738. (interactive "P")
  12739. (if (equal value 0) (setq value 10))
  12740. (let* ((completion-ignore-case t)
  12741. (prop org-effort-property)
  12742. (cur (org-entry-get nil prop))
  12743. (allowed (org-property-get-allowed-values nil prop 'table))
  12744. (existing (mapcar 'list (org-property-values prop)))
  12745. rpl
  12746. (val (cond
  12747. ((stringp value) value)
  12748. ((and allowed (integerp value))
  12749. (or (car (nth (1- value) allowed))
  12750. (car (org-last allowed))))
  12751. (allowed
  12752. (message "Select 1-9,0, [RET%s]: %s"
  12753. (if cur (concat "=" cur) "")
  12754. (mapconcat 'car allowed " "))
  12755. (setq rpl (read-char-exclusive))
  12756. (if (equal rpl ?\r)
  12757. cur
  12758. (setq rpl (- rpl ?0))
  12759. (if (equal rpl 0) (setq rpl 10))
  12760. (if (and (> rpl 0) (<= rpl (length allowed)))
  12761. (car (nth (1- rpl) allowed))
  12762. (org-completing-read "Effort: " allowed nil))))
  12763. (t
  12764. (let (org-completion-use-ido org-completion-use-iswitchb)
  12765. (org-completing-read
  12766. (concat "Effort " (if (and cur (string-match "\\S-" cur))
  12767. (concat "[" cur "]") "")
  12768. ": ")
  12769. existing nil nil "" nil cur))))))
  12770. (unless (equal (org-entry-get nil prop) val)
  12771. (org-entry-put nil prop val))
  12772. (message "%s is now %s" prop val)))
  12773. (defun org-at-property-p ()
  12774. "Is cursor inside a property drawer?"
  12775. (save-excursion
  12776. (beginning-of-line 1)
  12777. (when (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))
  12778. (save-match-data ;; Used by calling procedures
  12779. (let ((p (point))
  12780. (range (unless (org-before-first-heading-p)
  12781. (org-get-property-block))))
  12782. (and range (<= (car range) p) (< p (cdr range))))))))
  12783. (defun org-get-property-block (&optional beg end force)
  12784. "Return the (beg . end) range of the body of the property drawer.
  12785. BEG and END can be beginning and end of subtree, if not given
  12786. they will be found.
  12787. If the drawer does not exist and FORCE is non-nil, create the drawer."
  12788. (catch 'exit
  12789. (save-excursion
  12790. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  12791. (end (or end (progn (outline-next-heading) (point)))))
  12792. (goto-char beg)
  12793. (if (re-search-forward org-property-start-re end t)
  12794. (setq beg (1+ (match-end 0)))
  12795. (if force
  12796. (save-excursion
  12797. (org-insert-property-drawer)
  12798. (setq end (progn (outline-next-heading) (point))))
  12799. (throw 'exit nil))
  12800. (goto-char beg)
  12801. (if (re-search-forward org-property-start-re end t)
  12802. (setq beg (1+ (match-end 0)))))
  12803. (if (re-search-forward org-property-end-re end t)
  12804. (setq end (match-beginning 0))
  12805. (or force (throw 'exit nil))
  12806. (goto-char beg)
  12807. (setq end beg)
  12808. (org-indent-line-function)
  12809. (insert ":END:\n"))
  12810. (cons beg end)))))
  12811. (defun org-entry-properties (&optional pom which specific)
  12812. "Get all properties of the entry at point-or-marker POM.
  12813. This includes the TODO keyword, the tags, time strings for deadline,
  12814. scheduled, and clocking, and any additional properties defined in the
  12815. entry. The return value is an alist, keys may occur multiple times
  12816. if the property key was used several times.
  12817. POM may also be nil, in which case the current entry is used.
  12818. If WHICH is nil or `all', get all properties. If WHICH is
  12819. `special' or `standard', only get that subclass. If WHICH
  12820. is a string only get exactly this property. SPECIFIC can be a string, the
  12821. specific property we are interested in. Specifying it can speed
  12822. things up because then unnecessary parsing is avoided."
  12823. (setq which (or which 'all))
  12824. (org-with-point-at pom
  12825. (let ((clockstr (substring org-clock-string 0 -1))
  12826. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY" "BLOCKED"))
  12827. (case-fold-search nil)
  12828. beg end range props sum-props key key1 value string clocksum)
  12829. (save-excursion
  12830. (when (condition-case nil
  12831. (and (eq major-mode 'org-mode) (org-back-to-heading t))
  12832. (error nil))
  12833. (setq beg (point))
  12834. (setq sum-props (get-text-property (point) 'org-summaries))
  12835. (setq clocksum (get-text-property (point) :org-clock-minutes))
  12836. (outline-next-heading)
  12837. (setq end (point))
  12838. (when (memq which '(all special))
  12839. ;; Get the special properties, like TODO and tags
  12840. (goto-char beg)
  12841. (when (and (or (not specific) (string= specific "TODO"))
  12842. (looking-at org-todo-line-regexp) (match-end 2))
  12843. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  12844. (when (and (or (not specific) (string= specific "PRIORITY"))
  12845. (looking-at org-priority-regexp))
  12846. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  12847. (when (or (not specific) (string= specific "FILE"))
  12848. (push (cons "FILE" buffer-file-name) props))
  12849. (when (and (or (not specific) (string= specific "TAGS"))
  12850. (setq value (org-get-tags-string))
  12851. (string-match "\\S-" value))
  12852. (push (cons "TAGS" value) props))
  12853. (when (and (or (not specific) (string= specific "ALLTAGS"))
  12854. (setq value (org-get-tags-at)))
  12855. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":")
  12856. ":"))
  12857. props))
  12858. (when (or (not specific) (string= specific "BLOCKED"))
  12859. (push (cons "BLOCKED" (if (org-entry-blocked-p) "t" "")) props))
  12860. (when (or (not specific)
  12861. (member specific
  12862. '("SCHEDULED" "DEADLINE" "CLOCK" "CLOSED"
  12863. "TIMESTAMP" "TIMESTAMP_IA")))
  12864. (catch 'match
  12865. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  12866. (setq key (if (match-end 1)
  12867. (substring (org-match-string-no-properties 1)
  12868. 0 -1))
  12869. string (if (equal key clockstr)
  12870. (org-no-properties
  12871. (org-trim
  12872. (buffer-substring
  12873. (match-beginning 3) (goto-char
  12874. (point-at-eol)))))
  12875. (substring (org-match-string-no-properties 3)
  12876. 1 -1)))
  12877. ;; Get the correct property name from the key. This is
  12878. ;; necessary if the user has configured time keywords.
  12879. (setq key1 (concat key ":"))
  12880. (cond
  12881. ((not key)
  12882. (setq key
  12883. (if (= (char-after (match-beginning 3)) ?\[)
  12884. "TIMESTAMP_IA" "TIMESTAMP")))
  12885. ((equal key1 org-scheduled-string) (setq key "SCHEDULED"))
  12886. ((equal key1 org-deadline-string) (setq key "DEADLINE"))
  12887. ((equal key1 org-closed-string) (setq key "CLOSED"))
  12888. ((equal key1 org-clock-string) (setq key "CLOCK")))
  12889. (if (and specific (equal key specific) (not (equal key "CLOCK")))
  12890. (progn
  12891. (push (cons key string) props)
  12892. ;; no need to search further if match is found
  12893. (throw 'match t))
  12894. (when (or (equal key "CLOCK") (not (assoc key props)))
  12895. (push (cons key string) props))))))
  12896. )
  12897. (when (memq which '(all standard))
  12898. ;; Get the standard properties, like :PROP: ...
  12899. (setq range (org-get-property-block beg end))
  12900. (when range
  12901. (goto-char (car range))
  12902. (while (re-search-forward
  12903. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  12904. (cdr range) t)
  12905. (setq key (org-match-string-no-properties 1)
  12906. value (org-trim (or (org-match-string-no-properties 2) "")))
  12907. (unless (member key excluded)
  12908. (push (cons key (or value "")) props)))))
  12909. (if clocksum
  12910. (push (cons "CLOCKSUM"
  12911. (org-columns-number-to-string (/ (float clocksum) 60.)
  12912. 'add_times))
  12913. props))
  12914. (unless (assoc "CATEGORY" props)
  12915. (push (cons "CATEGORY" (org-get-category)) props))
  12916. (append sum-props (nreverse props)))))))
  12917. (defun org-entry-get (pom property &optional inherit literal-nil)
  12918. "Get value of PROPERTY for entry at point-or-marker POM.
  12919. If INHERIT is non-nil and the entry does not have the property,
  12920. then also check higher levels of the hierarchy.
  12921. If INHERIT is the symbol `selective', use inheritance only if the setting
  12922. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  12923. If the property is present but empty, the return value is the empty string.
  12924. If the property is not present at all, nil is returned.
  12925. If LITERAL-NIL is set, return the string value \"nil\" as a string,
  12926. do not interpret it as the list atom nil. This is used for inheritance
  12927. when a \"nil\" value can supersede a non-nil value higher up the hierarchy."
  12928. (org-with-point-at pom
  12929. (if (and inherit (if (eq inherit 'selective)
  12930. (org-property-inherit-p property)
  12931. t))
  12932. (org-entry-get-with-inheritance property literal-nil)
  12933. (if (member property org-special-properties)
  12934. ;; We need a special property. Use `org-entry-properties' to
  12935. ;; retrieve it, but specify the wanted property
  12936. (cdr (assoc property (org-entry-properties nil 'special property)))
  12937. (let ((range (unless (org-before-first-heading-p)
  12938. (org-get-property-block))))
  12939. (when (and range (goto-char (car range)))
  12940. ((lambda (val) (when val (if literal-nil val (org-not-nil val))))
  12941. (cond
  12942. ((re-search-forward
  12943. (org-re-property property) (cdr range) t)
  12944. (if (match-end 1) (org-match-string-no-properties 1) ""))
  12945. ((re-search-forward
  12946. (org-re-property (concat property "+")) (cdr range) t)
  12947. (cdr (assoc
  12948. property
  12949. (org-update-property-plist
  12950. (concat property "+")
  12951. (if (match-end 1) (org-match-string-no-properties 1) "")
  12952. (list (or (assoc property org-file-properties)
  12953. (assoc property org-global-properties)
  12954. (assoc property org-global-properties-fixed)
  12955. ))))))))))))))
  12956. (defun org-property-or-variable-value (var &optional inherit)
  12957. "Check if there is a property fixing the value of VAR.
  12958. If yes, return this value. If not, return the current value of the variable."
  12959. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  12960. (if (and prop (stringp prop) (string-match "\\S-" prop))
  12961. (read prop)
  12962. (symbol-value var))))
  12963. (defun org-entry-delete (pom property)
  12964. "Delete the property PROPERTY from entry at point-or-marker POM."
  12965. (org-with-point-at pom
  12966. (if (member property org-special-properties)
  12967. nil ; cannot delete these properties.
  12968. (let ((range (org-get-property-block)))
  12969. (if (and range
  12970. (goto-char (car range))
  12971. (re-search-forward
  12972. (org-re-property property)
  12973. (cdr range) t))
  12974. (progn
  12975. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  12976. t)
  12977. nil)))))
  12978. ;; Multi-values properties are properties that contain multiple values
  12979. ;; These values are assumed to be single words, separated by whitespace.
  12980. (defun org-entry-add-to-multivalued-property (pom property value)
  12981. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  12982. (let* ((old (org-entry-get pom property))
  12983. (values (and old (org-split-string old "[ \t]"))))
  12984. (setq value (org-entry-protect-space value))
  12985. (unless (member value values)
  12986. (setq values (cons value values))
  12987. (org-entry-put pom property
  12988. (mapconcat 'identity values " ")))))
  12989. (defun org-entry-remove-from-multivalued-property (pom property value)
  12990. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  12991. (let* ((old (org-entry-get pom property))
  12992. (values (and old (org-split-string old "[ \t]"))))
  12993. (setq value (org-entry-protect-space value))
  12994. (when (member value values)
  12995. (setq values (delete value values))
  12996. (org-entry-put pom property
  12997. (mapconcat 'identity values " ")))))
  12998. (defun org-entry-member-in-multivalued-property (pom property value)
  12999. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  13000. (let* ((old (org-entry-get pom property))
  13001. (values (and old (org-split-string old "[ \t]"))))
  13002. (setq value (org-entry-protect-space value))
  13003. (member value values)))
  13004. (defun org-entry-get-multivalued-property (pom property)
  13005. "Return a list of values in a multivalued property."
  13006. (let* ((value (org-entry-get pom property))
  13007. (values (and value (org-split-string value "[ \t]"))))
  13008. (mapcar 'org-entry-restore-space values)))
  13009. (defun org-entry-put-multivalued-property (pom property &rest values)
  13010. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  13011. VALUES should be a list of strings. Spaces will be protected."
  13012. (org-entry-put pom property
  13013. (mapconcat 'org-entry-protect-space values " "))
  13014. (let* ((value (org-entry-get pom property))
  13015. (values (and value (org-split-string value "[ \t]"))))
  13016. (mapcar 'org-entry-restore-space values)))
  13017. (defun org-entry-protect-space (s)
  13018. "Protect spaces and newline in string S."
  13019. (while (string-match " " s)
  13020. (setq s (replace-match "%20" t t s)))
  13021. (while (string-match "\n" s)
  13022. (setq s (replace-match "%0A" t t s)))
  13023. s)
  13024. (defun org-entry-restore-space (s)
  13025. "Restore spaces and newline in string S."
  13026. (while (string-match "%20" s)
  13027. (setq s (replace-match " " t t s)))
  13028. (while (string-match "%0A" s)
  13029. (setq s (replace-match "\n" t t s)))
  13030. s)
  13031. (defvar org-entry-property-inherited-from (make-marker)
  13032. "Marker pointing to the entry from where a property was inherited.
  13033. Each call to `org-entry-get-with-inheritance' will set this marker to the
  13034. location of the entry where the inheritance search matched. If there was
  13035. no match, the marker will point nowhere.
  13036. Note that also `org-entry-get' calls this function, if the INHERIT flag
  13037. is set.")
  13038. (defun org-entry-get-with-inheritance (property &optional literal-nil)
  13039. "Get entry property, and search higher levels if not present.
  13040. The search will stop at the first ancestor which has the property defined.
  13041. If the value found is \"nil\", return nil to show that the property
  13042. should be considered as undefined (this is the meaning of nil here).
  13043. However, if LITERAL-NIL is set, return the string value \"nil\" instead."
  13044. (move-marker org-entry-property-inherited-from nil)
  13045. (let (tmp)
  13046. (unless (org-before-first-heading-p)
  13047. (save-excursion
  13048. (save-restriction
  13049. (widen)
  13050. (catch 'ex
  13051. (while t
  13052. (when (setq tmp (org-entry-get nil property nil 'literal-nil))
  13053. (org-back-to-heading t)
  13054. (move-marker org-entry-property-inherited-from (point))
  13055. (throw 'ex tmp))
  13056. (or (org-up-heading-safe) (throw 'ex nil)))))))
  13057. (setq tmp (or tmp
  13058. (cdr (assoc property org-file-properties))
  13059. (cdr (assoc property org-global-properties))
  13060. (cdr (assoc property org-global-properties-fixed))))
  13061. (if literal-nil tmp (org-not-nil tmp))))
  13062. (defvar org-property-changed-functions nil
  13063. "Hook called when the value of a property has changed.
  13064. Each hook function should accept two arguments, the name of the property
  13065. and the new value.")
  13066. (defun org-entry-put (pom property value)
  13067. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  13068. (org-with-point-at pom
  13069. (org-back-to-heading t)
  13070. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  13071. range)
  13072. (cond
  13073. ((equal property "TODO")
  13074. (when (and (stringp value) (string-match "\\S-" value)
  13075. (not (member value org-todo-keywords-1)))
  13076. (error "\"%s\" is not a valid TODO state" value))
  13077. (if (or (not value)
  13078. (not (string-match "\\S-" value)))
  13079. (setq value 'none))
  13080. (org-todo value)
  13081. (org-set-tags nil 'align))
  13082. ((equal property "PRIORITY")
  13083. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  13084. (string-to-char value) ?\ ))
  13085. (org-set-tags nil 'align))
  13086. ((equal property "SCHEDULED")
  13087. (if (re-search-forward org-scheduled-time-regexp end t)
  13088. (cond
  13089. ((eq value 'earlier) (org-timestamp-change -1 'day))
  13090. ((eq value 'later) (org-timestamp-change 1 'day))
  13091. (t (call-interactively 'org-schedule)))
  13092. (call-interactively 'org-schedule)))
  13093. ((equal property "DEADLINE")
  13094. (if (re-search-forward org-deadline-time-regexp end t)
  13095. (cond
  13096. ((eq value 'earlier) (org-timestamp-change -1 'day))
  13097. ((eq value 'later) (org-timestamp-change 1 'day))
  13098. (t (call-interactively 'org-deadline)))
  13099. (call-interactively 'org-deadline)))
  13100. ((member property org-special-properties)
  13101. (error "The %s property can not yet be set with `org-entry-put'"
  13102. property))
  13103. (t ; a non-special property
  13104. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  13105. (setq range (org-get-property-block beg end 'force))
  13106. (goto-char (car range))
  13107. (if (re-search-forward
  13108. (org-re-property property) (cdr range) t)
  13109. (progn
  13110. (delete-region (match-beginning 0) (match-end 0))
  13111. (goto-char (match-beginning 0)))
  13112. (goto-char (cdr range))
  13113. (insert "\n")
  13114. (backward-char 1)
  13115. (org-indent-line-function))
  13116. (insert ":" property ":")
  13117. (and value (insert " " value))
  13118. (org-indent-line-function)))))
  13119. (run-hook-with-args 'org-property-changed-functions property value)))
  13120. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  13121. "Get all property keys in the current buffer.
  13122. With INCLUDE-SPECIALS, also list the special properties that reflect things
  13123. like tags and TODO state.
  13124. With INCLUDE-DEFAULTS, also include properties that has special meaning
  13125. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING
  13126. and others.
  13127. With INCLUDE-COLUMNS, also include property names given in COLUMN
  13128. formats in the current buffer."
  13129. (let (rtn range cfmt s p)
  13130. (save-excursion
  13131. (save-restriction
  13132. (widen)
  13133. (goto-char (point-min))
  13134. (while (re-search-forward org-property-start-re nil t)
  13135. (setq range (org-get-property-block))
  13136. (goto-char (car range))
  13137. (while (re-search-forward
  13138. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  13139. (cdr range) t)
  13140. (add-to-list 'rtn (org-match-string-no-properties 1)))
  13141. (outline-next-heading))))
  13142. (when include-specials
  13143. (setq rtn (append org-special-properties rtn)))
  13144. (when include-defaults
  13145. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties)
  13146. (add-to-list 'rtn org-effort-property))
  13147. (when include-columns
  13148. (save-excursion
  13149. (save-restriction
  13150. (widen)
  13151. (goto-char (point-min))
  13152. (while (re-search-forward
  13153. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  13154. nil t)
  13155. (setq cfmt (match-string 2) s 0)
  13156. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  13157. cfmt s)
  13158. (setq s (match-end 0)
  13159. p (match-string 1 cfmt))
  13160. (unless (or (equal p "ITEM")
  13161. (member p org-special-properties))
  13162. (add-to-list 'rtn (match-string 1 cfmt))))))))
  13163. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  13164. (defun org-property-values (key)
  13165. "Return a list of all values of property KEY in the current buffer."
  13166. (save-excursion
  13167. (save-restriction
  13168. (widen)
  13169. (goto-char (point-min))
  13170. (let ((re (org-re-property key))
  13171. values)
  13172. (while (re-search-forward re nil t)
  13173. (add-to-list 'values (org-trim (match-string 1))))
  13174. (delete "" values)))))
  13175. (defun org-insert-property-drawer ()
  13176. "Insert a property drawer into the current entry."
  13177. (org-back-to-heading t)
  13178. (looking-at org-outline-regexp)
  13179. (let ((indent (if org-adapt-indentation
  13180. (- (match-end 0) (match-beginning 0))
  13181. 0))
  13182. (beg (point))
  13183. (re (concat "^[ \t]*" org-keyword-time-regexp))
  13184. end hiddenp)
  13185. (outline-next-heading)
  13186. (setq end (point))
  13187. (goto-char beg)
  13188. (while (re-search-forward re end t))
  13189. (setq hiddenp (outline-invisible-p))
  13190. (end-of-line 1)
  13191. (and (equal (char-after) ?\n) (forward-char 1))
  13192. (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
  13193. (if (member (match-string 1) '("CLOCK:" ":END:"))
  13194. ;; just skip this line
  13195. (beginning-of-line 2)
  13196. ;; Drawer start, find the end
  13197. (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
  13198. (beginning-of-line 1)))
  13199. (org-skip-over-state-notes)
  13200. (skip-chars-backward " \t\n\r")
  13201. (if (eq (char-before) ?*) (forward-char 1))
  13202. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  13203. (beginning-of-line 0)
  13204. (org-indent-to-column indent)
  13205. (beginning-of-line 2)
  13206. (org-indent-to-column indent)
  13207. (beginning-of-line 0)
  13208. (if hiddenp
  13209. (save-excursion
  13210. (org-back-to-heading t)
  13211. (hide-entry))
  13212. (org-flag-drawer t))))
  13213. (defun org-insert-drawer (&optional arg drawer)
  13214. "Insert a drawer at point.
  13215. Optional argument DRAWER, when non-nil, is a string representing
  13216. drawer's name. Otherwise, the user is prompted for a name.
  13217. If a region is active, insert the drawer around that region
  13218. instead.
  13219. Point is left between drawer's boundaries."
  13220. (interactive "P")
  13221. (let* ((logbook (if (stringp org-log-into-drawer) org-log-into-drawer
  13222. "LOGBOOK"))
  13223. ;; SYSTEM-DRAWERS is a list of drawer names that are used
  13224. ;; internally by Org. They are meant to be inserted
  13225. ;; automatically.
  13226. (system-drawers `("CLOCK" ,logbook "PROPERTIES"))
  13227. ;; Remove system drawers from list. Note: For some reason,
  13228. ;; `org-completing-read' ignores the predicate while
  13229. ;; `completing-read' handles it fine.
  13230. (drawer (if arg "PROPERTIES"
  13231. (or drawer
  13232. (completing-read
  13233. "Drawer: " org-drawers
  13234. (lambda (d) (not (member d system-drawers))))))))
  13235. (cond
  13236. ;; With C-u, fall back on `org-insert-property-drawer'
  13237. (arg (org-insert-property-drawer))
  13238. ;; With an active region, insert a drawer at point.
  13239. ((not (org-region-active-p))
  13240. (progn
  13241. (unless (bolp) (insert "\n"))
  13242. (insert (format ":%s:\n\n:END:\n" drawer))
  13243. (forward-line -2)))
  13244. ;; Otherwise, insert the drawer at point
  13245. (t
  13246. (let ((rbeg (region-beginning))
  13247. (rend (copy-marker (region-end))))
  13248. (unwind-protect
  13249. (progn
  13250. (goto-char rbeg)
  13251. (beginning-of-line)
  13252. (when (save-excursion
  13253. (re-search-forward org-outline-regexp-bol rend t))
  13254. (error "Drawers cannot contain headlines"))
  13255. ;; Position point at the beginning of the first
  13256. ;; non-blank line in region. Insert drawer's opening
  13257. ;; there, then indent it.
  13258. (org-skip-whitespace)
  13259. (beginning-of-line)
  13260. (insert ":" drawer ":\n")
  13261. (forward-line -1)
  13262. (indent-for-tab-command)
  13263. ;; Move point to the beginning of the first blank line
  13264. ;; after the last non-blank line in region. Insert
  13265. ;; drawer's closing, then indent it.
  13266. (goto-char rend)
  13267. (skip-chars-backward " \r\t\n")
  13268. (insert "\n:END:")
  13269. (indent-for-tab-command)
  13270. (unless (eolp) (insert "\n")))
  13271. ;; Clear marker, whatever the outcome of insertion is.
  13272. (set-marker rend nil)))))))
  13273. (defvar org-property-set-functions-alist nil
  13274. "Property set function alist.
  13275. Each entry should have the following format:
  13276. (PROPERTY . READ-FUNCTION)
  13277. The read function will be called with the same argument as
  13278. `org-completing-read'.")
  13279. (defun org-set-property-function (property)
  13280. "Get the function that should be used to set PROPERTY.
  13281. This is computed according to `org-property-set-functions-alist'."
  13282. (or (cdr (assoc property org-property-set-functions-alist))
  13283. 'org-completing-read))
  13284. (defun org-read-property-value (property)
  13285. "Read PROPERTY value from user."
  13286. (let* ((completion-ignore-case t)
  13287. (allowed (org-property-get-allowed-values nil property 'table))
  13288. (cur (org-entry-get nil property))
  13289. (prompt (concat property " value"
  13290. (if (and cur (string-match "\\S-" cur))
  13291. (concat " [" cur "]") "") ": "))
  13292. (set-function (org-set-property-function property))
  13293. (val (if allowed
  13294. (funcall set-function prompt allowed nil
  13295. (not (get-text-property 0 'org-unrestricted
  13296. (caar allowed))))
  13297. (let (org-completion-use-ido org-completion-use-iswitchb)
  13298. (funcall set-function prompt
  13299. (mapcar 'list (org-property-values property))
  13300. nil nil "" nil cur)))))
  13301. (if (equal val "")
  13302. cur
  13303. val)))
  13304. (defvar org-last-set-property nil)
  13305. (defun org-read-property-name ()
  13306. "Read a property name."
  13307. (let* ((completion-ignore-case t)
  13308. (keys (org-buffer-property-keys nil t t))
  13309. (default-prop (or (save-excursion
  13310. (save-match-data
  13311. (beginning-of-line)
  13312. (and (looking-at "^\\s-*:\\([^:\n]+\\):")
  13313. (null (string= (match-string 1) "END"))
  13314. (match-string 1))))
  13315. org-last-set-property))
  13316. (property (org-icompleting-read
  13317. (concat "Property"
  13318. (if default-prop (concat " [" default-prop "]") "")
  13319. ": ")
  13320. (mapcar 'list keys)
  13321. nil nil nil nil
  13322. default-prop
  13323. )))
  13324. (if (member property keys)
  13325. property
  13326. (or (cdr (assoc (downcase property)
  13327. (mapcar (lambda (x) (cons (downcase x) x))
  13328. keys)))
  13329. property))))
  13330. (defun org-set-property (property value)
  13331. "In the current entry, set PROPERTY to VALUE.
  13332. When called interactively, this will prompt for a property name, offering
  13333. completion on existing and default properties. And then it will prompt
  13334. for a value, offering completion either on allowed values (via an inherited
  13335. xxx_ALL property) or on existing values in other instances of this property
  13336. in the current file."
  13337. (interactive (list nil nil))
  13338. (let* ((property (or property (org-read-property-name)))
  13339. (value (or value (org-read-property-value property)))
  13340. (fn (assoc property org-properties-postprocess-alist)))
  13341. (setq org-last-set-property property)
  13342. ;; Possibly postprocess the inserted value:
  13343. (when fn (setq value (funcall (cadr fn) value)))
  13344. (unless (equal (org-entry-get nil property) value)
  13345. (org-entry-put nil property value))))
  13346. (defun org-delete-property (property)
  13347. "In the current entry, delete PROPERTY."
  13348. (interactive
  13349. (let* ((completion-ignore-case t)
  13350. (prop (org-icompleting-read "Property: "
  13351. (org-entry-properties nil 'standard))))
  13352. (list prop)))
  13353. (message "Property %s %s" property
  13354. (if (org-entry-delete nil property)
  13355. "deleted"
  13356. "was not present in the entry")))
  13357. (defun org-delete-property-globally (property)
  13358. "Remove PROPERTY globally, from all entries."
  13359. (interactive
  13360. (let* ((completion-ignore-case t)
  13361. (prop (org-icompleting-read
  13362. "Globally remove property: "
  13363. (mapcar 'list (org-buffer-property-keys)))))
  13364. (list prop)))
  13365. (save-excursion
  13366. (save-restriction
  13367. (widen)
  13368. (goto-char (point-min))
  13369. (let ((cnt 0))
  13370. (while (re-search-forward
  13371. (org-re-property property)
  13372. nil t)
  13373. (setq cnt (1+ cnt))
  13374. (replace-match ""))
  13375. (message "Property \"%s\" removed from %d entries" property cnt)))))
  13376. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  13377. (defun org-compute-property-at-point ()
  13378. "Compute the property at point.
  13379. This looks for an enclosing column format, extracts the operator and
  13380. then applies it to the property in the column format's scope."
  13381. (interactive)
  13382. (unless (org-at-property-p)
  13383. (error "Not at a property"))
  13384. (let ((prop (org-match-string-no-properties 2)))
  13385. (org-columns-get-format-and-top-level)
  13386. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  13387. (error "No operator defined for property %s" prop))
  13388. (org-columns-compute prop)))
  13389. (defvar org-property-allowed-value-functions nil
  13390. "Hook for functions supplying allowed values for a specific property.
  13391. The functions must take a single argument, the name of the property, and
  13392. return a flat list of allowed values. If \":ETC\" is one of
  13393. the values, this means that these values are intended as defaults for
  13394. completion, but that other values should be allowed too.
  13395. The functions must return nil if they are not responsible for this
  13396. property.")
  13397. (defun org-property-get-allowed-values (pom property &optional table)
  13398. "Get allowed values for the property PROPERTY.
  13399. When TABLE is non-nil, return an alist that can directly be used for
  13400. completion."
  13401. (let (vals)
  13402. (cond
  13403. ((equal property "TODO")
  13404. (setq vals (org-with-point-at pom
  13405. (append org-todo-keywords-1 '("")))))
  13406. ((equal property "PRIORITY")
  13407. (let ((n org-lowest-priority))
  13408. (while (>= n org-highest-priority)
  13409. (push (char-to-string n) vals)
  13410. (setq n (1- n)))))
  13411. ((member property org-special-properties))
  13412. ((setq vals (run-hook-with-args-until-success
  13413. 'org-property-allowed-value-functions property)))
  13414. (t
  13415. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  13416. (when (and vals (string-match "\\S-" vals))
  13417. (setq vals (car (read-from-string (concat "(" vals ")"))))
  13418. (setq vals (mapcar (lambda (x)
  13419. (cond ((stringp x) x)
  13420. ((numberp x) (number-to-string x))
  13421. ((symbolp x) (symbol-name x))
  13422. (t "???")))
  13423. vals)))))
  13424. (when (member ":ETC" vals)
  13425. (setq vals (remove ":ETC" vals))
  13426. (org-add-props (car vals) '(org-unrestricted t)))
  13427. (if table (mapcar 'list vals) vals)))
  13428. (defun org-property-previous-allowed-value (&optional previous)
  13429. "Switch to the next allowed value for this property."
  13430. (interactive)
  13431. (org-property-next-allowed-value t))
  13432. (defun org-property-next-allowed-value (&optional previous)
  13433. "Switch to the next allowed value for this property."
  13434. (interactive)
  13435. (unless (org-at-property-p)
  13436. (error "Not at a property"))
  13437. (let* ((key (match-string 2))
  13438. (value (match-string 3))
  13439. (allowed (or (org-property-get-allowed-values (point) key)
  13440. (and (member value '("[ ]" "[-]" "[X]"))
  13441. '("[ ]" "[X]"))))
  13442. nval)
  13443. (unless allowed
  13444. (error "Allowed values for this property have not been defined"))
  13445. (if previous (setq allowed (reverse allowed)))
  13446. (if (member value allowed)
  13447. (setq nval (car (cdr (member value allowed)))))
  13448. (setq nval (or nval (car allowed)))
  13449. (if (equal nval value)
  13450. (error "Only one allowed value for this property"))
  13451. (org-at-property-p)
  13452. (replace-match (concat " :" key ": " nval) t t)
  13453. (org-indent-line-function)
  13454. (beginning-of-line 1)
  13455. (skip-chars-forward " \t")
  13456. (run-hook-with-args 'org-property-changed-functions key nval)))
  13457. (defun org-find-olp (path &optional this-buffer)
  13458. "Return a marker pointing to the entry at outline path OLP.
  13459. If anything goes wrong, throw an error.
  13460. You can wrap this call to catch the error like this:
  13461. (condition-case msg
  13462. (org-mobile-locate-entry (match-string 4))
  13463. (error (nth 1 msg)))
  13464. The return value will then be either a string with the error message,
  13465. or a marker if everything is OK.
  13466. If THIS-BUFFER is set, the outline path does not contain a file,
  13467. only headings."
  13468. (let* ((file (if this-buffer buffer-file-name (pop path)))
  13469. (buffer (if this-buffer (current-buffer) (find-file-noselect file)))
  13470. (level 1)
  13471. (lmin 1)
  13472. (lmax 1)
  13473. limit re end found pos heading cnt flevel)
  13474. (unless buffer (error "File not found :%s" file))
  13475. (with-current-buffer buffer
  13476. (save-excursion
  13477. (save-restriction
  13478. (widen)
  13479. (setq limit (point-max))
  13480. (goto-char (point-min))
  13481. (while (setq heading (pop path))
  13482. (setq re (format org-complex-heading-regexp-format
  13483. (regexp-quote heading)))
  13484. (setq cnt 0 pos (point))
  13485. (while (re-search-forward re end t)
  13486. (setq level (- (match-end 1) (match-beginning 1)))
  13487. (if (and (>= level lmin) (<= level lmax))
  13488. (setq found (match-beginning 0) flevel level cnt (1+ cnt))))
  13489. (when (= cnt 0) (error "Heading not found on level %d: %s"
  13490. lmax heading))
  13491. (when (> cnt 1) (error "Heading not unique on level %d: %s"
  13492. lmax heading))
  13493. (goto-char found)
  13494. (setq lmin (1+ flevel) lmax (+ lmin (if org-odd-levels-only 1 0)))
  13495. (setq end (save-excursion (org-end-of-subtree t t))))
  13496. (when (org-at-heading-p)
  13497. (move-marker (make-marker) (point))))))))
  13498. (defun org-find-exact-headline-in-buffer (heading &optional buffer pos-only)
  13499. "Find node HEADING in BUFFER.
  13500. Return a marker to the heading if it was found, or nil if not.
  13501. If POS-ONLY is set, return just the position instead of a marker.
  13502. The heading text must match exact, but it may have a TODO keyword,
  13503. a priority cookie and tags in the standard locations."
  13504. (with-current-buffer (or buffer (current-buffer))
  13505. (save-excursion
  13506. (save-restriction
  13507. (widen)
  13508. (goto-char (point-min))
  13509. (let (case-fold-search)
  13510. (if (re-search-forward
  13511. (format org-complex-heading-regexp-format
  13512. (regexp-quote heading)) nil t)
  13513. (if pos-only
  13514. (match-beginning 0)
  13515. (move-marker (make-marker) (match-beginning 0)))))))))
  13516. (defun org-find-exact-heading-in-directory (heading &optional dir)
  13517. "Find Org node headline HEADING in all .org files in directory DIR.
  13518. When the target headline is found, return a marker to this location."
  13519. (let ((files (directory-files (or dir default-directory)
  13520. nil "\\`[^.#].*\\.org\\'"))
  13521. file visiting m buffer)
  13522. (catch 'found
  13523. (while (setq file (pop files))
  13524. (message "trying %s" file)
  13525. (setq visiting (org-find-base-buffer-visiting file))
  13526. (setq buffer (or visiting (find-file-noselect file)))
  13527. (setq m (org-find-exact-headline-in-buffer
  13528. heading buffer))
  13529. (when (and (not m) (not visiting)) (kill-buffer buffer))
  13530. (and m (throw 'found m))))))
  13531. (defun org-find-entry-with-id (ident)
  13532. "Locate the entry that contains the ID property with exact value IDENT.
  13533. IDENT can be a string, a symbol or a number, this function will search for
  13534. the string representation of it.
  13535. Return the position where this entry starts, or nil if there is no such entry."
  13536. (interactive "sID: ")
  13537. (let ((id (cond
  13538. ((stringp ident) ident)
  13539. ((symbol-name ident) (symbol-name ident))
  13540. ((numberp ident) (number-to-string ident))
  13541. (t (error "IDENT %s must be a string, symbol or number" ident))))
  13542. (case-fold-search nil))
  13543. (save-excursion
  13544. (save-restriction
  13545. (widen)
  13546. (goto-char (point-min))
  13547. (when (re-search-forward
  13548. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  13549. nil t)
  13550. (org-back-to-heading t)
  13551. (point))))))
  13552. ;;;; Timestamps
  13553. (defvar org-last-changed-timestamp nil)
  13554. (defvar org-last-inserted-timestamp nil
  13555. "The last time stamp inserted with `org-insert-time-stamp'.")
  13556. (defvar org-time-was-given) ; dynamically scoped parameter
  13557. (defvar org-end-time-was-given) ; dynamically scoped parameter
  13558. (defvar org-ts-what) ; dynamically scoped parameter
  13559. (defun org-time-stamp (arg &optional inactive)
  13560. "Prompt for a date/time and insert a time stamp.
  13561. If the user specifies a time like HH:MM, or if this command is called
  13562. with a prefix argument, the time stamp will contain date and time.
  13563. Otherwise, only the date will be included. All parts of a date not
  13564. specified by the user will be filled in from the current date/time.
  13565. So if you press just return without typing anything, the time stamp
  13566. will represent the current date/time. If there is already a timestamp
  13567. at the cursor, it will be modified."
  13568. (interactive "P")
  13569. (let* ((ts nil)
  13570. (default-time
  13571. ;; Default time is either today, or, when entering a range,
  13572. ;; the range start.
  13573. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  13574. (save-excursion
  13575. (re-search-backward
  13576. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  13577. (- (point) 20) t)))
  13578. (apply 'encode-time (org-parse-time-string (match-string 1)))
  13579. (current-time)))
  13580. (default-input (and ts (org-get-compact-tod ts)))
  13581. (repeater (save-excursion
  13582. (save-match-data
  13583. (beginning-of-line)
  13584. (when (re-search-forward
  13585. "\\([.+-]+[0-9]+[dwmy] ?\\)+" ;;\\(?:[/ ][-+]?[0-9]+[dwmy]\\)?\\) ?"
  13586. (save-excursion (progn (end-of-line) (point))) t)
  13587. (match-string 0)))))
  13588. org-time-was-given org-end-time-was-given time)
  13589. (cond
  13590. ((and (org-at-timestamp-p t)
  13591. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  13592. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  13593. (insert "--")
  13594. (setq time (let ((this-command this-command))
  13595. (org-read-date arg 'totime nil nil
  13596. default-time default-input inactive)))
  13597. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  13598. ((org-at-timestamp-p t)
  13599. (setq time (let ((this-command this-command))
  13600. (org-read-date arg 'totime nil nil default-time default-input inactive)))
  13601. (when (org-at-timestamp-p t) ; just to get the match data
  13602. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  13603. (replace-match "")
  13604. (setq org-last-changed-timestamp
  13605. (org-insert-time-stamp
  13606. time (or org-time-was-given arg)
  13607. inactive nil nil (list org-end-time-was-given)))
  13608. (when repeater (goto-char (1- (point))) (insert " " repeater)
  13609. (setq org-last-changed-timestamp
  13610. (concat (substring org-last-inserted-timestamp 0 -1)
  13611. " " repeater ">"))))
  13612. (message "Timestamp updated"))
  13613. (t
  13614. (setq time (let ((this-command this-command))
  13615. (org-read-date arg 'totime nil nil default-time default-input inactive)))
  13616. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  13617. nil nil (list org-end-time-was-given))))))
  13618. ;; FIXME: can we use this for something else, like computing time differences?
  13619. (defun org-get-compact-tod (s)
  13620. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  13621. (let* ((t1 (match-string 1 s))
  13622. (h1 (string-to-number (match-string 2 s)))
  13623. (m1 (string-to-number (match-string 3 s)))
  13624. (t2 (and (match-end 4) (match-string 5 s)))
  13625. (h2 (and t2 (string-to-number (match-string 6 s))))
  13626. (m2 (and t2 (string-to-number (match-string 7 s))))
  13627. dh dm)
  13628. (if (not t2)
  13629. t1
  13630. (setq dh (- h2 h1) dm (- m2 m1))
  13631. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  13632. (concat t1 "+" (number-to-string dh)
  13633. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  13634. (defun org-time-stamp-inactive (&optional arg)
  13635. "Insert an inactive time stamp.
  13636. An inactive time stamp is enclosed in square brackets instead of angle
  13637. brackets. It is inactive in the sense that it does not trigger agenda entries,
  13638. does not link to the calendar and cannot be changed with the S-cursor keys.
  13639. So these are more for recording a certain time/date."
  13640. (interactive "P")
  13641. (org-time-stamp arg 'inactive))
  13642. (defvar org-date-ovl (make-overlay 1 1))
  13643. (overlay-put org-date-ovl 'face 'org-warning)
  13644. (org-detach-overlay org-date-ovl)
  13645. (defvar org-ans1) ; dynamically scoped parameter
  13646. (defvar org-ans2) ; dynamically scoped parameter
  13647. (defvar org-plain-time-of-day-regexp) ; defined below
  13648. (defvar org-overriding-default-time nil) ; dynamically scoped
  13649. (defvar org-read-date-overlay nil)
  13650. (defvar org-dcst nil) ; dynamically scoped
  13651. (defvar org-read-date-history nil)
  13652. (defvar org-read-date-final-answer nil)
  13653. (defvar org-read-date-analyze-futurep nil)
  13654. (defvar org-read-date-analyze-forced-year nil)
  13655. (defun org-read-date (&optional with-time to-time from-string prompt
  13656. default-time default-input inactive)
  13657. "Read a date, possibly a time, and make things smooth for the user.
  13658. The prompt will suggest to enter an ISO date, but you can also enter anything
  13659. which will at least partially be understood by `parse-time-string'.
  13660. Unrecognized parts of the date will default to the current day, month, year,
  13661. hour and minute. If this command is called to replace a timestamp at point,
  13662. or to enter the second timestamp of a range, the default time is taken
  13663. from the existing stamp. Furthermore, the command prefers the future,
  13664. so if you are giving a date where the year is not given, and the day-month
  13665. combination is already past in the current year, it will assume you
  13666. mean next year. For details, see the manual. A few examples:
  13667. 3-2-5 --> 2003-02-05
  13668. feb 15 --> currentyear-02-15
  13669. 2/15 --> currentyear-02-15
  13670. sep 12 9 --> 2009-09-12
  13671. 12:45 --> today 12:45
  13672. 22 sept 0:34 --> currentyear-09-22 0:34
  13673. 12 --> currentyear-currentmonth-12
  13674. Fri --> nearest Friday (today or later)
  13675. etc.
  13676. Furthermore you can specify a relative date by giving, as the *first* thing
  13677. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  13678. change in days weeks, months, years.
  13679. With a single plus or minus, the date is relative to today. With a double
  13680. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  13681. +4d --> four days from today
  13682. +4 --> same as above
  13683. +2w --> two weeks from today
  13684. ++5 --> five days from default date
  13685. The function understands only English month and weekday abbreviations,
  13686. but this can be configured with the variables `parse-time-months' and
  13687. `parse-time-weekdays'.
  13688. While prompting, a calendar is popped up - you can also select the
  13689. date with the mouse (button 1). The calendar shows a period of three
  13690. months. To scroll it to other months, use the keys `>' and `<'.
  13691. If you don't like the calendar, turn it off with
  13692. \(setq org-read-date-popup-calendar nil)
  13693. With optional argument TO-TIME, the date will immediately be converted
  13694. to an internal time.
  13695. With an optional argument WITH-TIME, the prompt will suggest to also
  13696. insert a time. Note that when WITH-TIME is not set, you can still
  13697. enter a time, and this function will inform the calling routine about
  13698. this change. The calling routine may then choose to change the format
  13699. used to insert the time stamp into the buffer to include the time.
  13700. With optional argument FROM-STRING, read from this string instead from
  13701. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  13702. the time/date that is used for everything that is not specified by the
  13703. user."
  13704. (require 'parse-time)
  13705. (let* ((org-time-stamp-rounding-minutes
  13706. (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  13707. (org-dcst org-display-custom-times)
  13708. (ct (org-current-time))
  13709. (def (or org-overriding-default-time default-time ct))
  13710. (defdecode (decode-time def))
  13711. (dummy (progn
  13712. (when (< (nth 2 defdecode) org-extend-today-until)
  13713. (setcar (nthcdr 2 defdecode) -1)
  13714. (setcar (nthcdr 1 defdecode) 59)
  13715. (setq def (apply 'encode-time defdecode)
  13716. defdecode (decode-time def)))))
  13717. (calendar-frame-setup nil)
  13718. (calendar-setup nil)
  13719. (calendar-move-hook nil)
  13720. (calendar-view-diary-initially-flag nil)
  13721. (calendar-view-holidays-initially-flag nil)
  13722. (timestr (format-time-string
  13723. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  13724. (prompt (concat (if prompt (concat prompt " ") "")
  13725. (format "Date+time [%s]: " timestr)))
  13726. ans (org-ans0 "") org-ans1 org-ans2 final)
  13727. (cond
  13728. (from-string (setq ans from-string))
  13729. (org-read-date-popup-calendar
  13730. (save-excursion
  13731. (save-window-excursion
  13732. (calendar)
  13733. (unwind-protect
  13734. (progn
  13735. (calendar-forward-day (- (time-to-days def)
  13736. (calendar-absolute-from-gregorian
  13737. (calendar-current-date))))
  13738. (org-eval-in-calendar nil t)
  13739. (let* ((old-map (current-local-map))
  13740. (map (copy-keymap calendar-mode-map))
  13741. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  13742. (org-defkey map (kbd "RET") 'org-calendar-select)
  13743. (org-defkey map [mouse-1] 'org-calendar-select-mouse)
  13744. (org-defkey map [mouse-2] 'org-calendar-select-mouse)
  13745. (org-defkey minibuffer-local-map [(meta shift left)]
  13746. (lambda () (interactive)
  13747. (org-eval-in-calendar '(calendar-backward-month 1))))
  13748. (org-defkey minibuffer-local-map [(meta shift right)]
  13749. (lambda () (interactive)
  13750. (org-eval-in-calendar '(calendar-forward-month 1))))
  13751. (org-defkey minibuffer-local-map [(meta shift up)]
  13752. (lambda () (interactive)
  13753. (org-eval-in-calendar '(calendar-backward-year 1))))
  13754. (org-defkey minibuffer-local-map [(meta shift down)]
  13755. (lambda () (interactive)
  13756. (org-eval-in-calendar '(calendar-forward-year 1))))
  13757. (org-defkey minibuffer-local-map [?\e (shift left)]
  13758. (lambda () (interactive)
  13759. (org-eval-in-calendar '(calendar-backward-month 1))))
  13760. (org-defkey minibuffer-local-map [?\e (shift right)]
  13761. (lambda () (interactive)
  13762. (org-eval-in-calendar '(calendar-forward-month 1))))
  13763. (org-defkey minibuffer-local-map [?\e (shift up)]
  13764. (lambda () (interactive)
  13765. (org-eval-in-calendar '(calendar-backward-year 1))))
  13766. (org-defkey minibuffer-local-map [?\e (shift down)]
  13767. (lambda () (interactive)
  13768. (org-eval-in-calendar '(calendar-forward-year 1))))
  13769. (org-defkey minibuffer-local-map [(shift up)]
  13770. (lambda () (interactive)
  13771. (org-eval-in-calendar '(calendar-backward-week 1))))
  13772. (org-defkey minibuffer-local-map [(shift down)]
  13773. (lambda () (interactive)
  13774. (org-eval-in-calendar '(calendar-forward-week 1))))
  13775. (org-defkey minibuffer-local-map [(shift left)]
  13776. (lambda () (interactive)
  13777. (org-eval-in-calendar '(calendar-backward-day 1))))
  13778. (org-defkey minibuffer-local-map [(shift right)]
  13779. (lambda () (interactive)
  13780. (org-eval-in-calendar '(calendar-forward-day 1))))
  13781. (org-defkey minibuffer-local-map ">"
  13782. (lambda () (interactive)
  13783. (org-eval-in-calendar '(scroll-calendar-left 1))))
  13784. (org-defkey minibuffer-local-map "<"
  13785. (lambda () (interactive)
  13786. (org-eval-in-calendar '(scroll-calendar-right 1))))
  13787. (org-defkey minibuffer-local-map "\C-v"
  13788. (lambda () (interactive)
  13789. (org-eval-in-calendar
  13790. '(calendar-scroll-left-three-months 1))))
  13791. (org-defkey minibuffer-local-map "\M-v"
  13792. (lambda () (interactive)
  13793. (org-eval-in-calendar
  13794. '(calendar-scroll-right-three-months 1))))
  13795. (run-hooks 'org-read-date-minibuffer-setup-hook)
  13796. (unwind-protect
  13797. (progn
  13798. (use-local-map map)
  13799. (setq org-read-date-inactive inactive)
  13800. (add-hook 'post-command-hook 'org-read-date-display)
  13801. (setq org-ans0 (read-string prompt default-input
  13802. 'org-read-date-history nil))
  13803. ;; org-ans0: from prompt
  13804. ;; org-ans1: from mouse click
  13805. ;; org-ans2: from calendar motion
  13806. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  13807. (remove-hook 'post-command-hook 'org-read-date-display)
  13808. (use-local-map old-map)
  13809. (when org-read-date-overlay
  13810. (delete-overlay org-read-date-overlay)
  13811. (setq org-read-date-overlay nil)))))
  13812. (bury-buffer "*Calendar*")))))
  13813. (t ; Naked prompt only
  13814. (unwind-protect
  13815. (setq ans (read-string prompt default-input
  13816. 'org-read-date-history timestr))
  13817. (when org-read-date-overlay
  13818. (delete-overlay org-read-date-overlay)
  13819. (setq org-read-date-overlay nil)))))
  13820. (setq final (org-read-date-analyze ans def defdecode))
  13821. (when org-read-date-analyze-forced-year
  13822. (message "Year was forced into %s"
  13823. (if org-read-date-force-compatible-dates
  13824. "compatible range (1970-2037)"
  13825. "range representable on this machine"))
  13826. (ding))
  13827. ;; One round trip to get rid of 34th of August and stuff like that....
  13828. (setq final (decode-time (apply 'encode-time final)))
  13829. (setq org-read-date-final-answer ans)
  13830. (if to-time
  13831. (apply 'encode-time final)
  13832. (if (and (boundp 'org-time-was-given) org-time-was-given)
  13833. (format "%04d-%02d-%02d %02d:%02d"
  13834. (nth 5 final) (nth 4 final) (nth 3 final)
  13835. (nth 2 final) (nth 1 final))
  13836. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  13837. (defvar def)
  13838. (defvar defdecode)
  13839. (defvar with-time)
  13840. (defvar org-read-date-inactive)
  13841. (defun org-read-date-display ()
  13842. "Display the current date prompt interpretation in the minibuffer."
  13843. (when org-read-date-display-live
  13844. (when org-read-date-overlay
  13845. (delete-overlay org-read-date-overlay))
  13846. (let ((p (point)))
  13847. (end-of-line 1)
  13848. (while (not (equal (buffer-substring
  13849. (max (point-min) (- (point) 4)) (point))
  13850. " "))
  13851. (insert " "))
  13852. (goto-char p))
  13853. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  13854. " " (or org-ans1 org-ans2)))
  13855. (org-end-time-was-given nil)
  13856. (f (org-read-date-analyze ans def defdecode))
  13857. (fmts (if org-dcst
  13858. org-time-stamp-custom-formats
  13859. org-time-stamp-formats))
  13860. (fmt (if (or with-time
  13861. (and (boundp 'org-time-was-given) org-time-was-given))
  13862. (cdr fmts)
  13863. (car fmts)))
  13864. (txt (format-time-string fmt (apply 'encode-time f)))
  13865. (txt (if org-read-date-inactive (concat "[" (substring txt 1 -1) "]") txt))
  13866. (txt (concat "=> " txt)))
  13867. (when (and org-end-time-was-given
  13868. (string-match org-plain-time-of-day-regexp txt))
  13869. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  13870. org-end-time-was-given
  13871. (substring txt (match-end 0)))))
  13872. (when org-read-date-analyze-futurep
  13873. (setq txt (concat txt " (=>F)")))
  13874. (setq org-read-date-overlay
  13875. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  13876. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  13877. (defun org-read-date-analyze (ans def defdecode)
  13878. "Analyze the combined answer of the date prompt."
  13879. ;; FIXME: cleanup and comment
  13880. (let ((nowdecode (decode-time (current-time)))
  13881. delta deltan deltaw deltadef year month day
  13882. hour minute second wday pm h2 m2 tl wday1
  13883. iso-year iso-weekday iso-week iso-year iso-date futurep kill-year)
  13884. (setq org-read-date-analyze-futurep nil
  13885. org-read-date-analyze-forced-year nil)
  13886. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  13887. (setq ans "+0"))
  13888. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  13889. (setq ans (replace-match "" t t ans)
  13890. deltan (car delta)
  13891. deltaw (nth 1 delta)
  13892. deltadef (nth 2 delta)))
  13893. ;; Check if there is an iso week date in there
  13894. ;; If yes, store the info and postpone interpreting it until the rest
  13895. ;; of the parsing is done
  13896. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  13897. (setq iso-year (if (match-end 1)
  13898. (org-small-year-to-year
  13899. (string-to-number (match-string 1 ans))))
  13900. iso-weekday (if (match-end 3)
  13901. (string-to-number (match-string 3 ans)))
  13902. iso-week (string-to-number (match-string 2 ans)))
  13903. (setq ans (replace-match "" t t ans)))
  13904. ;; Help matching ISO dates with single digit month or day, like 2006-8-11.
  13905. (when (string-match
  13906. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  13907. (setq year (if (match-end 2)
  13908. (string-to-number (match-string 2 ans))
  13909. (progn (setq kill-year t)
  13910. (string-to-number (format-time-string "%Y"))))
  13911. month (string-to-number (match-string 3 ans))
  13912. day (string-to-number (match-string 4 ans)))
  13913. (if (< year 100) (setq year (+ 2000 year)))
  13914. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  13915. t nil ans)))
  13916. ;; Help matching dotted european dates
  13917. (when (string-match
  13918. "^ *\\(3[01]\\|0?[1-9]\\|[12][0-9]\\)\\. ?\\(0?[1-9]\\|1[012]\\)\\. ?\\([1-9][0-9][0-9][0-9]\\)?" ans)
  13919. (setq year (if (match-end 3)
  13920. (string-to-number (match-string 3 ans))
  13921. (progn (setq kill-year t)
  13922. (string-to-number (format-time-string "%Y"))))
  13923. day (string-to-number (match-string 1 ans))
  13924. month (string-to-number (match-string 2 ans))
  13925. ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  13926. t nil ans)))
  13927. ;; Help matching american dates, like 5/30 or 5/30/7
  13928. (when (string-match
  13929. "^ *\\(0?[1-9]\\|1[012]\\)/\\(0?[1-9]\\|[12][0-9]\\|3[01]\\)\\(/\\([0-9]+\\)\\)?\\([^/0-9]\\|$\\)" ans)
  13930. (setq year (if (match-end 4)
  13931. (string-to-number (match-string 4 ans))
  13932. (progn (setq kill-year t)
  13933. (string-to-number (format-time-string "%Y"))))
  13934. month (string-to-number (match-string 1 ans))
  13935. day (string-to-number (match-string 2 ans)))
  13936. (if (< year 100) (setq year (+ 2000 year)))
  13937. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  13938. t nil ans)))
  13939. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  13940. ;; If there is a time with am/pm, and *no* time without it, we convert
  13941. ;; so that matching will be successful.
  13942. (loop for i from 1 to 2 do ; twice, for end time as well
  13943. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  13944. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  13945. (setq hour (string-to-number (match-string 1 ans))
  13946. minute (if (match-end 3)
  13947. (string-to-number (match-string 3 ans))
  13948. 0)
  13949. pm (equal ?p
  13950. (string-to-char (downcase (match-string 4 ans)))))
  13951. (if (and (= hour 12) (not pm))
  13952. (setq hour 0)
  13953. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  13954. (setq ans (replace-match (format "%02d:%02d" hour minute)
  13955. t t ans))))
  13956. ;; Check if a time range is given as a duration
  13957. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  13958. (setq hour (string-to-number (match-string 1 ans))
  13959. h2 (+ hour (string-to-number (match-string 3 ans)))
  13960. minute (string-to-number (match-string 2 ans))
  13961. m2 (+ minute (if (match-end 5) (string-to-number
  13962. (match-string 5 ans))0)))
  13963. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  13964. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  13965. t t ans)))
  13966. ;; Check if there is a time range
  13967. (when (boundp 'org-end-time-was-given)
  13968. (setq org-time-was-given nil)
  13969. (when (and (string-match org-plain-time-of-day-regexp ans)
  13970. (match-end 8))
  13971. (setq org-end-time-was-given (match-string 8 ans))
  13972. (setq ans (concat (substring ans 0 (match-beginning 7))
  13973. (substring ans (match-end 7))))))
  13974. (setq tl (parse-time-string ans)
  13975. day (or (nth 3 tl) (nth 3 defdecode))
  13976. month (or (nth 4 tl)
  13977. (if (and org-read-date-prefer-future
  13978. (nth 3 tl) (< (nth 3 tl) (nth 3 nowdecode)))
  13979. (prog1 (1+ (nth 4 nowdecode)) (setq futurep t))
  13980. (nth 4 defdecode)))
  13981. year (or (and (not kill-year) (nth 5 tl))
  13982. (if (and org-read-date-prefer-future
  13983. (nth 4 tl) (< (nth 4 tl) (nth 4 nowdecode)))
  13984. (prog1 (1+ (nth 5 nowdecode)) (setq futurep t))
  13985. (nth 5 defdecode)))
  13986. hour (or (nth 2 tl) (nth 2 defdecode))
  13987. minute (or (nth 1 tl) (nth 1 defdecode))
  13988. second (or (nth 0 tl) 0)
  13989. wday (nth 6 tl))
  13990. (when (and (eq org-read-date-prefer-future 'time)
  13991. (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
  13992. (equal day (nth 3 nowdecode))
  13993. (equal month (nth 4 nowdecode))
  13994. (equal year (nth 5 nowdecode))
  13995. (nth 2 tl)
  13996. (or (< (nth 2 tl) (nth 2 nowdecode))
  13997. (and (= (nth 2 tl) (nth 2 nowdecode))
  13998. (nth 1 tl)
  13999. (< (nth 1 tl) (nth 1 nowdecode)))))
  14000. (setq day (1+ day)
  14001. futurep t))
  14002. ;; Special date definitions below
  14003. (cond
  14004. (iso-week
  14005. ;; There was an iso week
  14006. (require 'cal-iso)
  14007. (setq futurep nil)
  14008. (setq year (or iso-year year)
  14009. day (or iso-weekday wday 1)
  14010. wday nil ; to make sure that the trigger below does not match
  14011. iso-date (calendar-gregorian-from-absolute
  14012. (calendar-absolute-from-iso
  14013. (list iso-week day year))))
  14014. ; FIXME: Should we also push ISO weeks into the future?
  14015. ; (when (and org-read-date-prefer-future
  14016. ; (not iso-year)
  14017. ; (< (calendar-absolute-from-gregorian iso-date)
  14018. ; (time-to-days (current-time))))
  14019. ; (setq year (1+ year)
  14020. ; iso-date (calendar-gregorian-from-absolute
  14021. ; (calendar-absolute-from-iso
  14022. ; (list iso-week day year)))))
  14023. (setq month (car iso-date)
  14024. year (nth 2 iso-date)
  14025. day (nth 1 iso-date)))
  14026. (deltan
  14027. (setq futurep nil)
  14028. (unless deltadef
  14029. (let ((now (decode-time (current-time))))
  14030. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  14031. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  14032. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  14033. ((equal deltaw "m") (setq month (+ month deltan)))
  14034. ((equal deltaw "y") (setq year (+ year deltan)))))
  14035. ((and wday (not (nth 3 tl)))
  14036. (setq futurep nil)
  14037. ;; Weekday was given, but no day, so pick that day in the week
  14038. ;; on or after the derived date.
  14039. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  14040. (unless (equal wday wday1)
  14041. (setq day (+ day (% (- wday wday1 -7) 7))))))
  14042. (if (and (boundp 'org-time-was-given)
  14043. (nth 2 tl))
  14044. (setq org-time-was-given t))
  14045. (if (< year 100) (setq year (+ 2000 year)))
  14046. ;; Check of the date is representable
  14047. (if org-read-date-force-compatible-dates
  14048. (progn
  14049. (if (< year 1970)
  14050. (setq year 1970 org-read-date-analyze-forced-year t))
  14051. (if (> year 2037)
  14052. (setq year 2037 org-read-date-analyze-forced-year t)))
  14053. (condition-case nil
  14054. (ignore (encode-time second minute hour day month year))
  14055. (error
  14056. (setq year (nth 5 defdecode))
  14057. (setq org-read-date-analyze-forced-year t))))
  14058. (setq org-read-date-analyze-futurep futurep)
  14059. (list second minute hour day month year)))
  14060. (defvar parse-time-weekdays)
  14061. (defun org-read-date-get-relative (s today default)
  14062. "Check string S for special relative date string.
  14063. TODAY and DEFAULT are internal times, for today and for a default.
  14064. Return shift list (N what def-flag)
  14065. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  14066. N is the number of WHATs to shift.
  14067. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  14068. the DEFAULT date rather than TODAY."
  14069. (require 'parse-time)
  14070. (when (and
  14071. (string-match
  14072. (concat
  14073. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  14074. "\\([0-9]+\\)?"
  14075. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  14076. "\\([ \t]\\|$\\)") s)
  14077. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  14078. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  14079. (string-to-char (substring (match-string 1 s) -1))
  14080. ?+))
  14081. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  14082. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  14083. (what (if (match-end 3) (match-string 3 s) "d"))
  14084. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  14085. (date (if rel default today))
  14086. (wday (nth 6 (decode-time date)))
  14087. delta)
  14088. (if wday1
  14089. (progn
  14090. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  14091. (if (= dir ?-) (setq delta (- delta 7)))
  14092. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  14093. (list delta "d" rel))
  14094. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  14095. (defun org-order-calendar-date-args (arg1 arg2 arg3)
  14096. "Turn a user-specified date into the internal representation.
  14097. The internal representation needed by the calendar is (month day year).
  14098. This is a wrapper to handle the brain-dead convention in calendar that
  14099. user function argument order change dependent on argument order."
  14100. (if (boundp 'calendar-date-style)
  14101. (cond
  14102. ((eq calendar-date-style 'american)
  14103. (list arg1 arg2 arg3))
  14104. ((eq calendar-date-style 'european)
  14105. (list arg2 arg1 arg3))
  14106. ((eq calendar-date-style 'iso)
  14107. (list arg2 arg3 arg1)))
  14108. (with-no-warnings ;; european-calendar-style is obsolete as of version 23.1
  14109. (if (org-bound-and-true-p european-calendar-style)
  14110. (list arg2 arg1 arg3)
  14111. (list arg1 arg2 arg3)))))
  14112. (defun org-eval-in-calendar (form &optional keepdate)
  14113. "Eval FORM in the calendar window and return to current window.
  14114. Also, store the cursor date in variable org-ans2."
  14115. (let ((sf (selected-frame))
  14116. (sw (selected-window)))
  14117. (select-window (get-buffer-window "*Calendar*" t))
  14118. (eval form)
  14119. (when (and (not keepdate) (calendar-cursor-to-date))
  14120. (let* ((date (calendar-cursor-to-date))
  14121. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14122. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  14123. (move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  14124. (select-window sw)
  14125. (org-select-frame-set-input-focus sf)))
  14126. (defun org-calendar-select ()
  14127. "Return to `org-read-date' with the date currently selected.
  14128. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  14129. (interactive)
  14130. (when (calendar-cursor-to-date)
  14131. (let* ((date (calendar-cursor-to-date))
  14132. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14133. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  14134. (if (active-minibuffer-window) (exit-minibuffer))))
  14135. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  14136. "Insert a date stamp for the date given by the internal TIME.
  14137. WITH-HM means use the stamp format that includes the time of the day.
  14138. INACTIVE means use square brackets instead of angular ones, so that the
  14139. stamp will not contribute to the agenda.
  14140. PRE and POST are optional strings to be inserted before and after the
  14141. stamp.
  14142. The command returns the inserted time stamp."
  14143. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  14144. stamp)
  14145. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  14146. (insert-before-markers (or pre ""))
  14147. (when (listp extra)
  14148. (setq extra (car extra))
  14149. (if (and (stringp extra)
  14150. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  14151. (setq extra (format "-%02d:%02d"
  14152. (string-to-number (match-string 1 extra))
  14153. (string-to-number (match-string 2 extra))))
  14154. (setq extra nil)))
  14155. (when extra
  14156. (setq fmt (concat (substring fmt 0 -1) extra (substring fmt -1))))
  14157. (insert-before-markers (setq stamp (format-time-string fmt time)))
  14158. (insert-before-markers (or post ""))
  14159. (setq org-last-inserted-timestamp stamp)))
  14160. (defun org-toggle-time-stamp-overlays ()
  14161. "Toggle the use of custom time stamp formats."
  14162. (interactive)
  14163. (setq org-display-custom-times (not org-display-custom-times))
  14164. (unless org-display-custom-times
  14165. (let ((p (point-min)) (bmp (buffer-modified-p)))
  14166. (while (setq p (next-single-property-change p 'display))
  14167. (if (and (get-text-property p 'display)
  14168. (eq (get-text-property p 'face) 'org-date))
  14169. (remove-text-properties
  14170. p (setq p (next-single-property-change p 'display))
  14171. '(display t))))
  14172. (set-buffer-modified-p bmp)))
  14173. (if (featurep 'xemacs)
  14174. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  14175. (org-restart-font-lock)
  14176. (setq org-table-may-need-update t)
  14177. (if org-display-custom-times
  14178. (message "Time stamps are overlaid with custom format")
  14179. (message "Time stamp overlays removed")))
  14180. (defun org-display-custom-time (beg end)
  14181. "Overlay modified time stamp format over timestamp between BEG and END."
  14182. (let* ((ts (buffer-substring beg end))
  14183. t1 w1 with-hm tf time str w2 (off 0))
  14184. (save-match-data
  14185. (setq t1 (org-parse-time-string ts t))
  14186. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)?\\'" ts)
  14187. (setq off (- (match-end 0) (match-beginning 0)))))
  14188. (setq end (- end off))
  14189. (setq w1 (- end beg)
  14190. with-hm (and (nth 1 t1) (nth 2 t1))
  14191. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  14192. time (org-fix-decoded-time t1)
  14193. str (org-add-props
  14194. (format-time-string
  14195. (substring tf 1 -1) (apply 'encode-time time))
  14196. nil 'mouse-face 'highlight)
  14197. w2 (length str))
  14198. (if (not (= w2 w1))
  14199. (add-text-properties (1+ beg) (+ 2 beg)
  14200. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  14201. (if (featurep 'xemacs)
  14202. (progn
  14203. (put-text-property beg end 'invisible t)
  14204. (put-text-property beg end 'end-glyph (make-glyph str)))
  14205. (put-text-property beg end 'display str))))
  14206. (defun org-translate-time (string)
  14207. "Translate all timestamps in STRING to custom format.
  14208. But do this only if the variable `org-display-custom-times' is set."
  14209. (when org-display-custom-times
  14210. (save-match-data
  14211. (let* ((start 0)
  14212. (re org-ts-regexp-both)
  14213. t1 with-hm inactive tf time str beg end)
  14214. (while (setq start (string-match re string start))
  14215. (setq beg (match-beginning 0)
  14216. end (match-end 0)
  14217. t1 (save-match-data
  14218. (org-parse-time-string (substring string beg end) t))
  14219. with-hm (and (nth 1 t1) (nth 2 t1))
  14220. inactive (equal (substring string beg (1+ beg)) "[")
  14221. tf (funcall (if with-hm 'cdr 'car)
  14222. org-time-stamp-custom-formats)
  14223. time (org-fix-decoded-time t1)
  14224. str (format-time-string
  14225. (concat
  14226. (if inactive "[" "<") (substring tf 1 -1)
  14227. (if inactive "]" ">"))
  14228. (apply 'encode-time time))
  14229. string (replace-match str t t string)
  14230. start (+ start (length str)))))))
  14231. string)
  14232. (defun org-fix-decoded-time (time)
  14233. "Set 0 instead of nil for the first 6 elements of time.
  14234. Don't touch the rest."
  14235. (let ((n 0))
  14236. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  14237. (defun org-days-to-time (timestamp-string)
  14238. "Difference between TIMESTAMP-STRING and now in days."
  14239. (- (time-to-days (org-time-string-to-time timestamp-string))
  14240. (time-to-days (current-time))))
  14241. (defun org-deadline-close (timestamp-string &optional ndays)
  14242. "Is the time in TIMESTAMP-STRING close to the current date?"
  14243. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  14244. (and (< (org-days-to-time timestamp-string) ndays)
  14245. (not (org-entry-is-done-p))))
  14246. (defun org-get-wdays (ts)
  14247. "Get the deadline lead time appropriate for timestring TS."
  14248. (cond
  14249. ((<= org-deadline-warning-days 0)
  14250. ;; 0 or negative, enforce this value no matter what
  14251. (- org-deadline-warning-days))
  14252. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\| \\)" ts)
  14253. ;; lead time is specified.
  14254. (floor (* (string-to-number (match-string 1 ts))
  14255. (cdr (assoc (match-string 2 ts)
  14256. '(("d" . 1) ("w" . 7)
  14257. ("m" . 30.4) ("y" . 365.25)))))))
  14258. ;; go for the default.
  14259. (t org-deadline-warning-days)))
  14260. (defun org-calendar-select-mouse (ev)
  14261. "Return to `org-read-date' with the date currently selected.
  14262. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  14263. (interactive "e")
  14264. (mouse-set-point ev)
  14265. (when (calendar-cursor-to-date)
  14266. (let* ((date (calendar-cursor-to-date))
  14267. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14268. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  14269. (if (active-minibuffer-window) (exit-minibuffer))))
  14270. (defun org-check-deadlines (ndays)
  14271. "Check if there are any deadlines due or past due.
  14272. A deadline is considered due if it happens within `org-deadline-warning-days'
  14273. days from today's date. If the deadline appears in an entry marked DONE,
  14274. it is not shown. The prefix arg NDAYS can be used to test that many
  14275. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  14276. (interactive "P")
  14277. (let* ((org-warn-days
  14278. (cond
  14279. ((equal ndays '(4)) 100000)
  14280. (ndays (prefix-numeric-value ndays))
  14281. (t (abs org-deadline-warning-days))))
  14282. (case-fold-search nil)
  14283. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  14284. (callback
  14285. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  14286. (message "%d deadlines past-due or due within %d days"
  14287. (org-occur regexp nil callback)
  14288. org-warn-days)))
  14289. (defun org-check-before-date (date)
  14290. "Check if there are deadlines or scheduled entries before DATE."
  14291. (interactive (list (org-read-date)))
  14292. (let ((case-fold-search nil)
  14293. (regexp (concat "\\<\\(" org-deadline-string
  14294. "\\|" org-scheduled-string
  14295. "\\) *<\\([^>]+\\)>"))
  14296. (callback
  14297. (lambda () (time-less-p
  14298. (org-time-string-to-time (match-string 2))
  14299. (org-time-string-to-time date)))))
  14300. (message "%d entries before %s"
  14301. (org-occur regexp nil callback) date)))
  14302. (defun org-check-after-date (date)
  14303. "Check if there are deadlines or scheduled entries after DATE."
  14304. (interactive (list (org-read-date)))
  14305. (let ((case-fold-search nil)
  14306. (regexp (concat "\\<\\(" org-deadline-string
  14307. "\\|" org-scheduled-string
  14308. "\\) *<\\([^>]+\\)>"))
  14309. (callback
  14310. (lambda () (not
  14311. (time-less-p
  14312. (org-time-string-to-time (match-string 2))
  14313. (org-time-string-to-time date))))))
  14314. (message "%d entries after %s"
  14315. (org-occur regexp nil callback) date)))
  14316. (defun org-check-dates-range (start-date end-date)
  14317. "Check for deadlines/scheduled entries between START-DATE and END-DATE."
  14318. (interactive (list (org-read-date nil nil nil "Range starts")
  14319. (org-read-date nil nil nil "Range end")))
  14320. (let ((case-fold-search nil)
  14321. (regexp (concat "\\<\\(" org-deadline-string
  14322. "\\|" org-scheduled-string
  14323. "\\) *<\\([^>]+\\)>"))
  14324. (callback
  14325. (lambda ()
  14326. (let ((match (match-string 2)))
  14327. (and
  14328. (not (time-less-p
  14329. (org-time-string-to-time match)
  14330. (org-time-string-to-time start-date)))
  14331. (time-less-p
  14332. (org-time-string-to-time match)
  14333. (org-time-string-to-time end-date)))))))
  14334. (message "%d entries between %s and %s"
  14335. (org-occur regexp nil callback) start-date end-date)))
  14336. (defun org-evaluate-time-range (&optional to-buffer)
  14337. "Evaluate a time range by computing the difference between start and end.
  14338. Normally the result is just printed in the echo area, but with prefix arg
  14339. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  14340. If the time range is actually in a table, the result is inserted into the
  14341. next column.
  14342. For time difference computation, a year is assumed to be exactly 365
  14343. days in order to avoid rounding problems."
  14344. (interactive "P")
  14345. (or
  14346. (org-clock-update-time-maybe)
  14347. (save-excursion
  14348. (unless (org-at-date-range-p t)
  14349. (goto-char (point-at-bol))
  14350. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  14351. (if (not (org-at-date-range-p t))
  14352. (error "Not at a time-stamp range, and none found in current line")))
  14353. (let* ((ts1 (match-string 1))
  14354. (ts2 (match-string 2))
  14355. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  14356. (match-end (match-end 0))
  14357. (time1 (org-time-string-to-time ts1))
  14358. (time2 (org-time-string-to-time ts2))
  14359. (t1 (org-float-time time1))
  14360. (t2 (org-float-time time2))
  14361. (diff (abs (- t2 t1)))
  14362. (negative (< (- t2 t1) 0))
  14363. ;; (ys (floor (* 365 24 60 60)))
  14364. (ds (* 24 60 60))
  14365. (hs (* 60 60))
  14366. (fy "%dy %dd %02d:%02d")
  14367. (fy1 "%dy %dd")
  14368. (fd "%dd %02d:%02d")
  14369. (fd1 "%dd")
  14370. (fh "%02d:%02d")
  14371. y d h m align)
  14372. (if havetime
  14373. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  14374. y 0
  14375. d (floor (/ diff ds)) diff (mod diff ds)
  14376. h (floor (/ diff hs)) diff (mod diff hs)
  14377. m (floor (/ diff 60)))
  14378. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  14379. y 0
  14380. d (floor (+ (/ diff ds) 0.5))
  14381. h 0 m 0))
  14382. (if (not to-buffer)
  14383. (message "%s" (org-make-tdiff-string y d h m))
  14384. (if (org-at-table-p)
  14385. (progn
  14386. (goto-char match-end)
  14387. (setq align t)
  14388. (and (looking-at " *|") (goto-char (match-end 0))))
  14389. (goto-char match-end))
  14390. (if (looking-at
  14391. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  14392. (replace-match ""))
  14393. (if negative (insert " -"))
  14394. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  14395. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  14396. (insert " " (format fh h m))))
  14397. (if align (org-table-align))
  14398. (message "Time difference inserted")))))
  14399. (defun org-make-tdiff-string (y d h m)
  14400. (let ((fmt "")
  14401. (l nil))
  14402. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  14403. l (push y l)))
  14404. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  14405. l (push d l)))
  14406. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  14407. l (push h l)))
  14408. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  14409. l (push m l)))
  14410. (apply 'format fmt (nreverse l))))
  14411. (defun org-time-string-to-time (s &optional buffer pos)
  14412. (condition-case errdata
  14413. (apply 'encode-time (org-parse-time-string s))
  14414. (error (error "Bad timestamp `%s'%s\nError was: %s"
  14415. s (if (not (and buffer pos))
  14416. ""
  14417. (format " at %d in buffer `%s'" pos buffer))
  14418. (cdr errdata)))))
  14419. (defun org-time-string-to-seconds (s)
  14420. (org-float-time (org-time-string-to-time s)))
  14421. (defun org-time-string-to-absolute (s &optional daynr prefer show-all buffer pos)
  14422. "Convert a time stamp to an absolute day number.
  14423. If there is a specifier for a cyclic time stamp, get the closest date to
  14424. DAYNR.
  14425. PREFER and SHOW-ALL are passed through to `org-closest-date'.
  14426. The variable date is bound by the calendar when this is called."
  14427. (cond
  14428. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  14429. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  14430. daynr
  14431. (+ daynr 1000)))
  14432. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  14433. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  14434. (time-to-days (current-time))) (match-string 0 s)
  14435. prefer show-all))
  14436. (t (time-to-days
  14437. (condition-case errdata
  14438. (apply 'encode-time (org-parse-time-string s))
  14439. (error (error "Bad timestamp `%s'%s\nError was: %s"
  14440. s (if (not (and buffer pos))
  14441. ""
  14442. (format " at %d in buffer `%s'" pos buffer))
  14443. (cdr errdata))))))))
  14444. (defun org-days-to-iso-week (days)
  14445. "Return the iso week number."
  14446. (require 'cal-iso)
  14447. (car (calendar-iso-from-absolute days)))
  14448. (defun org-small-year-to-year (year)
  14449. "Convert 2-digit years into 4-digit years.
  14450. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  14451. The year 2000 cannot be abbreviated. Any year larger than 99
  14452. is returned unchanged."
  14453. (if (< year 38)
  14454. (setq year (+ 2000 year))
  14455. (if (< year 100)
  14456. (setq year (+ 1900 year))))
  14457. year)
  14458. (defun org-time-from-absolute (d)
  14459. "Return the time corresponding to date D.
  14460. D may be an absolute day number, or a calendar-type list (month day year)."
  14461. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  14462. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  14463. (defun org-calendar-holiday ()
  14464. "List of holidays, for Diary display in Org-mode."
  14465. (require 'holidays)
  14466. (let ((hl (funcall
  14467. (if (fboundp 'calendar-check-holidays)
  14468. 'calendar-check-holidays 'check-calendar-holidays) date)))
  14469. (if hl (mapconcat 'identity hl "; "))))
  14470. (defun org-diary-sexp-entry (sexp entry date)
  14471. "Process a SEXP diary ENTRY for DATE."
  14472. (require 'diary-lib)
  14473. (let ((result (if calendar-debug-sexp
  14474. (let ((stack-trace-on-error t))
  14475. (eval (car (read-from-string sexp))))
  14476. (condition-case nil
  14477. (eval (car (read-from-string sexp)))
  14478. (error
  14479. (beep)
  14480. (message "Bad sexp at line %d in %s: %s"
  14481. (org-current-line)
  14482. (buffer-file-name) sexp)
  14483. (sleep-for 2))))))
  14484. (cond ((stringp result) (split-string result "; "))
  14485. ((and (consp result)
  14486. (not (consp (cdr result)))
  14487. (stringp (cdr result))) (cdr result))
  14488. ((and (consp result)
  14489. (stringp (car result))) result)
  14490. (result entry)
  14491. (t nil))))
  14492. (defun org-diary-to-ical-string (frombuf)
  14493. "Get iCalendar entries from diary entries in buffer FROMBUF.
  14494. This uses the icalendar.el library."
  14495. (let* ((tmpdir (if (featurep 'xemacs)
  14496. (temp-directory)
  14497. temporary-file-directory))
  14498. (tmpfile (make-temp-name
  14499. (expand-file-name "orgics" tmpdir)))
  14500. buf rtn b e)
  14501. (with-current-buffer frombuf
  14502. (icalendar-export-region (point-min) (point-max) tmpfile)
  14503. (setq buf (find-buffer-visiting tmpfile))
  14504. (set-buffer buf)
  14505. (goto-char (point-min))
  14506. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  14507. (setq b (match-beginning 0)))
  14508. (goto-char (point-max))
  14509. (if (re-search-backward "^END:VEVENT" nil t)
  14510. (setq e (match-end 0)))
  14511. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  14512. (kill-buffer buf)
  14513. (delete-file tmpfile)
  14514. rtn))
  14515. (defun org-closest-date (start current change prefer show-all)
  14516. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  14517. When PREFER is `past', return a date that is either CURRENT or past.
  14518. When PREFER is `future', return a date that is either CURRENT or future.
  14519. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  14520. ;; Make the proper lists from the dates
  14521. (catch 'exit
  14522. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  14523. dn dw sday cday n1 n2 n0
  14524. d m y y1 y2 date1 date2 nmonths nm ny m2)
  14525. (setq start (org-date-to-gregorian start)
  14526. current (org-date-to-gregorian
  14527. (if show-all
  14528. current
  14529. (time-to-days (current-time))))
  14530. sday (calendar-absolute-from-gregorian start)
  14531. cday (calendar-absolute-from-gregorian current))
  14532. (if (<= cday sday) (throw 'exit sday))
  14533. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  14534. (setq dn (string-to-number (match-string 1 change))
  14535. dw (cdr (assoc (match-string 2 change) a1)))
  14536. (error "Invalid change specifier: %s" change))
  14537. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  14538. (cond
  14539. ((eq dw 'day)
  14540. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  14541. n2 (+ n1 dn)))
  14542. ((eq dw 'year)
  14543. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  14544. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  14545. (setq date1 (list m d y1)
  14546. n1 (calendar-absolute-from-gregorian date1)
  14547. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  14548. n2 (calendar-absolute-from-gregorian date2)))
  14549. ((eq dw 'month)
  14550. ;; approx number of month between the two dates
  14551. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  14552. ;; How often does dn fit in there?
  14553. (setq d (nth 1 start) m (car start) y (nth 2 start)
  14554. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  14555. m (+ m nm)
  14556. ny (floor (/ m 12))
  14557. y (+ y ny)
  14558. m (- m (* ny 12)))
  14559. (while (> m 12) (setq m (- m 12) y (1+ y)))
  14560. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  14561. (setq m2 (+ m dn) y2 y)
  14562. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  14563. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  14564. (while (<= n2 cday)
  14565. (setq n1 n2 m m2 y y2)
  14566. (setq m2 (+ m dn) y2 y)
  14567. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  14568. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  14569. ;; Make sure n1 is the earlier date
  14570. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  14571. (if show-all
  14572. (cond
  14573. ((eq prefer 'past) (if (= cday n2) n2 n1))
  14574. ((eq prefer 'future) (if (= cday n1) n1 n2))
  14575. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  14576. (cond
  14577. ((eq prefer 'past) (if (= cday n2) n2 n1))
  14578. ((eq prefer 'future) (if (= cday n1) n1 n2))
  14579. (t (if (= cday n1) n1 n2)))))))
  14580. (defun org-date-to-gregorian (date)
  14581. "Turn any specification of DATE into a Gregorian date for the calendar."
  14582. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  14583. ((and (listp date) (= (length date) 3)) date)
  14584. ((stringp date)
  14585. (setq date (org-parse-time-string date))
  14586. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  14587. ((listp date)
  14588. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  14589. (defun org-parse-time-string (s &optional nodefault)
  14590. "Parse the standard Org-mode time string.
  14591. This should be a lot faster than the normal `parse-time-string'.
  14592. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  14593. hour and minute fields will be nil if not given."
  14594. (if (string-match org-ts-regexp0 s)
  14595. (list 0
  14596. (if (or (match-beginning 8) (not nodefault))
  14597. (string-to-number (or (match-string 8 s) "0")))
  14598. (if (or (match-beginning 7) (not nodefault))
  14599. (string-to-number (or (match-string 7 s) "0")))
  14600. (string-to-number (match-string 4 s))
  14601. (string-to-number (match-string 3 s))
  14602. (string-to-number (match-string 2 s))
  14603. nil nil nil)
  14604. (error "Not a standard Org-mode time string: %s" s)))
  14605. (defun org-timestamp-up (&optional arg)
  14606. "Increase the date item at the cursor by one.
  14607. If the cursor is on the year, change the year. If it is on the month,
  14608. the day or the time, change that.
  14609. With prefix ARG, change by that many units."
  14610. (interactive "p")
  14611. (org-timestamp-change (prefix-numeric-value arg) nil 'updown))
  14612. (defun org-timestamp-down (&optional arg)
  14613. "Decrease the date item at the cursor by one.
  14614. If the cursor is on the year, change the year. If it is on the month,
  14615. the day or the time, change that.
  14616. With prefix ARG, change by that many units."
  14617. (interactive "p")
  14618. (org-timestamp-change (- (prefix-numeric-value arg)) nil 'updown))
  14619. (defun org-timestamp-up-day (&optional arg)
  14620. "Increase the date in the time stamp by one day.
  14621. With prefix ARG, change that many days."
  14622. (interactive "p")
  14623. (if (and (not (org-at-timestamp-p t))
  14624. (org-at-heading-p))
  14625. (org-todo 'up)
  14626. (org-timestamp-change (prefix-numeric-value arg) 'day 'updown)))
  14627. (defun org-timestamp-down-day (&optional arg)
  14628. "Decrease the date in the time stamp by one day.
  14629. With prefix ARG, change that many days."
  14630. (interactive "p")
  14631. (if (and (not (org-at-timestamp-p t))
  14632. (org-at-heading-p))
  14633. (org-todo 'down)
  14634. (org-timestamp-change (- (prefix-numeric-value arg)) 'day) 'updown))
  14635. (defun org-at-timestamp-p (&optional inactive-ok)
  14636. "Determine if the cursor is in or at a timestamp."
  14637. (interactive)
  14638. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  14639. (pos (point))
  14640. (ans (or (looking-at tsr)
  14641. (save-excursion
  14642. (skip-chars-backward "^[<\n\r\t")
  14643. (if (> (point) (point-min)) (backward-char 1))
  14644. (and (looking-at tsr)
  14645. (> (- (match-end 0) pos) -1))))))
  14646. (and ans
  14647. (boundp 'org-ts-what)
  14648. (setq org-ts-what
  14649. (cond
  14650. ((= pos (match-beginning 0)) 'bracket)
  14651. ;; Point is considered to be "on the bracket" whether
  14652. ;; it's really on it or right after it.
  14653. ((or (= pos (1- (match-end 0)))
  14654. (= pos (match-end 0))) 'bracket)
  14655. ((org-pos-in-match-range pos 2) 'year)
  14656. ((org-pos-in-match-range pos 3) 'month)
  14657. ((org-pos-in-match-range pos 7) 'hour)
  14658. ((org-pos-in-match-range pos 8) 'minute)
  14659. ((or (org-pos-in-match-range pos 4)
  14660. (org-pos-in-match-range pos 5)) 'day)
  14661. ((and (> pos (or (match-end 8) (match-end 5)))
  14662. (< pos (match-end 0)))
  14663. (- pos (or (match-end 8) (match-end 5))))
  14664. (t 'day))))
  14665. ans))
  14666. (defun org-toggle-timestamp-type ()
  14667. "Toggle the type (<active> or [inactive]) of a time stamp."
  14668. (interactive)
  14669. (when (org-at-timestamp-p t)
  14670. (let ((beg (match-beginning 0)) (end (match-end 0))
  14671. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  14672. (save-excursion
  14673. (goto-char beg)
  14674. (while (re-search-forward "[][<>]" end t)
  14675. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  14676. t t)))
  14677. (message "Timestamp is now %sactive"
  14678. (if (equal (char-after beg) ?<) "" "in")))))
  14679. (defun org-timestamp-change (n &optional what updown)
  14680. "Change the date in the time stamp at point.
  14681. The date will be changed by N times WHAT. WHAT can be `day', `month',
  14682. `year', `minute', `second'. If WHAT is not given, the cursor position
  14683. in the timestamp determines what will be changed."
  14684. (let ((origin (point)) origin-cat
  14685. with-hm inactive
  14686. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  14687. org-ts-what
  14688. extra rem
  14689. ts time time0)
  14690. (if (not (org-at-timestamp-p t))
  14691. (error "Not at a timestamp"))
  14692. (if (and (not what) (eq org-ts-what 'bracket))
  14693. (org-toggle-timestamp-type)
  14694. ;; Point isn't on brackets. Remember the part of the time-stamp
  14695. ;; the point was in. Indeed, size of time-stamps may change,
  14696. ;; but point must be kept in the same category nonetheless.
  14697. (setq origin-cat org-ts-what)
  14698. (if (and (not what) (not (eq org-ts-what 'day))
  14699. org-display-custom-times
  14700. (get-text-property (point) 'display)
  14701. (not (get-text-property (1- (point)) 'display)))
  14702. (setq org-ts-what 'day))
  14703. (setq org-ts-what (or what org-ts-what)
  14704. inactive (= (char-after (match-beginning 0)) ?\[)
  14705. ts (match-string 0))
  14706. (replace-match "")
  14707. (if (string-match
  14708. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)*\\)[]>]"
  14709. ts)
  14710. (setq extra (match-string 1 ts)))
  14711. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  14712. (setq with-hm t))
  14713. (setq time0 (org-parse-time-string ts))
  14714. (when (and updown
  14715. (eq org-ts-what 'minute)
  14716. (not current-prefix-arg))
  14717. ;; This looks like s-up and s-down. Change by one rounding step.
  14718. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  14719. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  14720. (setcar (cdr time0) (+ (nth 1 time0)
  14721. (if (> n 0) (- rem) (- dm rem))))))
  14722. (setq time
  14723. (encode-time (or (car time0) 0)
  14724. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  14725. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  14726. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  14727. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  14728. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  14729. (nthcdr 6 time0)))
  14730. (when (and (member org-ts-what '(hour minute))
  14731. extra
  14732. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  14733. (setq extra (org-modify-ts-extra
  14734. extra
  14735. (if (eq org-ts-what 'hour) 2 5)
  14736. n dm)))
  14737. (when (integerp org-ts-what)
  14738. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  14739. (if (eq what 'calendar)
  14740. (let ((cal-date (org-get-date-from-calendar)))
  14741. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  14742. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  14743. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  14744. (setcar time0 (or (car time0) 0))
  14745. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  14746. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  14747. (setq time (apply 'encode-time time0))))
  14748. ;; Insert the new time-stamp, and ensure point stays in the same
  14749. ;; category as before (i.e. not after the last position in that
  14750. ;; category).
  14751. (let ((pos (point)))
  14752. ;; Stay before inserted string. `save-excursion' is of no use.
  14753. (setq org-last-changed-timestamp
  14754. (org-insert-time-stamp time with-hm inactive nil nil extra))
  14755. (goto-char pos))
  14756. (save-match-data
  14757. (looking-at org-ts-regexp3)
  14758. (goto-char (cond
  14759. ;; `day' category ends before `hour' if any, or at
  14760. ;; the end of the day name.
  14761. ((eq origin-cat 'day)
  14762. (min (or (match-beginning 7) (1- (match-end 5))) origin))
  14763. ((eq origin-cat 'hour) (min (match-end 7) origin))
  14764. ((eq origin-cat 'minute) (min (1- (match-end 8)) origin))
  14765. ((integerp origin-cat) (min (1- (match-end 0)) origin))
  14766. ;; `year' and `month' have both fixed size: point
  14767. ;; couldn't have moved into another part.
  14768. (t origin))))
  14769. ;; Update clock if on a CLOCK line.
  14770. (org-clock-update-time-maybe)
  14771. ;; Try to recenter the calendar window, if any.
  14772. (if (and org-calendar-follow-timestamp-change
  14773. (get-buffer-window "*Calendar*" t)
  14774. (memq org-ts-what '(day month year)))
  14775. (org-recenter-calendar (time-to-days time))))))
  14776. (defun org-modify-ts-extra (s pos n dm)
  14777. "Change the different parts of the lead-time and repeat fields in timestamp."
  14778. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  14779. ng h m new rem)
  14780. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  14781. (cond
  14782. ((or (org-pos-in-match-range pos 2)
  14783. (org-pos-in-match-range pos 3))
  14784. (setq m (string-to-number (match-string 3 s))
  14785. h (string-to-number (match-string 2 s)))
  14786. (if (org-pos-in-match-range pos 2)
  14787. (setq h (+ h n))
  14788. (setq n (* dm (org-no-warnings (signum n))))
  14789. (when (not (= 0 (setq rem (% m dm))))
  14790. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  14791. (setq m (+ m n)))
  14792. (if (< m 0) (setq m (+ m 60) h (1- h)))
  14793. (if (> m 59) (setq m (- m 60) h (1+ h)))
  14794. (setq h (min 24 (max 0 h)))
  14795. (setq ng 1 new (format "-%02d:%02d" h m)))
  14796. ((org-pos-in-match-range pos 6)
  14797. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  14798. ((org-pos-in-match-range pos 5)
  14799. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  14800. ((org-pos-in-match-range pos 9)
  14801. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  14802. ((org-pos-in-match-range pos 8)
  14803. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  14804. (when ng
  14805. (setq s (concat
  14806. (substring s 0 (match-beginning ng))
  14807. new
  14808. (substring s (match-end ng))))))
  14809. s))
  14810. (defun org-recenter-calendar (date)
  14811. "If the calendar is visible, recenter it to DATE."
  14812. (let* ((win (selected-window))
  14813. (cwin (get-buffer-window "*Calendar*" t))
  14814. (calendar-move-hook nil))
  14815. (when cwin
  14816. (select-window cwin)
  14817. (calendar-goto-date (if (listp date) date
  14818. (calendar-gregorian-from-absolute date)))
  14819. (select-window win))))
  14820. (defun org-goto-calendar (&optional arg)
  14821. "Go to the Emacs calendar at the current date.
  14822. If there is a time stamp in the current line, go to that date.
  14823. A prefix ARG can be used to force the current date."
  14824. (interactive "P")
  14825. (let ((tsr org-ts-regexp) diff
  14826. (calendar-move-hook nil)
  14827. (calendar-view-holidays-initially-flag nil)
  14828. (calendar-view-diary-initially-flag nil))
  14829. (if (or (org-at-timestamp-p)
  14830. (save-excursion
  14831. (beginning-of-line 1)
  14832. (looking-at (concat ".*" tsr))))
  14833. (let ((d1 (time-to-days (current-time)))
  14834. (d2 (time-to-days
  14835. (org-time-string-to-time (match-string 1)))))
  14836. (setq diff (- d2 d1))))
  14837. (calendar)
  14838. (calendar-goto-today)
  14839. (if (and diff (not arg)) (calendar-forward-day diff))))
  14840. (defun org-get-date-from-calendar ()
  14841. "Return a list (month day year) of date at point in calendar."
  14842. (with-current-buffer "*Calendar*"
  14843. (save-match-data
  14844. (calendar-cursor-to-date))))
  14845. (defun org-date-from-calendar ()
  14846. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  14847. If there is already a time stamp at the cursor position, update it."
  14848. (interactive)
  14849. (if (org-at-timestamp-p t)
  14850. (org-timestamp-change 0 'calendar)
  14851. (let ((cal-date (org-get-date-from-calendar)))
  14852. (org-insert-time-stamp
  14853. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  14854. (defun org-minutes-to-hh:mm-string (m)
  14855. "Compute H:MM from a number of minutes."
  14856. (let ((h (/ m 60)))
  14857. (setq m (- m (* 60 h)))
  14858. (format org-time-clocksum-format h m)))
  14859. (defun org-hh:mm-string-to-minutes (s)
  14860. "Convert a string H:MM to a number of minutes.
  14861. If the string is just a number, interpret it as minutes.
  14862. In fact, the first hh:mm or number in the string will be taken,
  14863. there can be extra stuff in the string.
  14864. If no number is found, the return value is 0."
  14865. (cond
  14866. ((integerp s) s)
  14867. ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  14868. (+ (* (string-to-number (match-string 1 s)) 60)
  14869. (string-to-number (match-string 2 s))))
  14870. ((string-match "\\([0-9]+\\)" s)
  14871. (string-to-number (match-string 1 s)))
  14872. (t 0)))
  14873. (defcustom org-effort-durations
  14874. `(("h" . 60)
  14875. ("d" . ,(* 60 8))
  14876. ("w" . ,(* 60 8 5))
  14877. ("m" . ,(* 60 8 5 4))
  14878. ("y" . ,(* 60 8 5 40)))
  14879. "Conversion factor to minutes for an effort modifier.
  14880. Each entry has the form (MODIFIER . MINUTES).
  14881. In an effort string, a number followed by MODIFIER is multiplied
  14882. by the specified number of MINUTES to obtain an effort in
  14883. minutes.
  14884. For example, if the value of this variable is ((\"hours\" . 60)), then an
  14885. effort string \"2hours\" is equivalent to 120 minutes."
  14886. :group 'org-agenda
  14887. :version "24.1"
  14888. :type '(alist :key-type (string :tag "Modifier")
  14889. :value-type (number :tag "Minutes")))
  14890. (defun org-duration-string-to-minutes (s)
  14891. "Convert a duration string S to minutes.
  14892. A bare number is interpreted as minutes, modifiers can be set by
  14893. customizing `org-effort-durations' (which see).
  14894. Entries containing a colon are interpreted as H:MM by
  14895. `org-hh:mm-string-to-minutes'."
  14896. (let ((result 0)
  14897. (re (concat "\\([0-9]+\\) *\\("
  14898. (regexp-opt (mapcar 'car org-effort-durations))
  14899. "\\)")))
  14900. (while (string-match re s)
  14901. (incf result (* (cdr (assoc (match-string 2 s) org-effort-durations))
  14902. (string-to-number (match-string 1 s))))
  14903. (setq s (replace-match "" nil t s)))
  14904. (incf result (org-hh:mm-string-to-minutes s))
  14905. result))
  14906. ;;;; Files
  14907. (defun org-save-all-org-buffers ()
  14908. "Save all Org-mode buffers without user confirmation."
  14909. (interactive)
  14910. (message "Saving all Org-mode buffers...")
  14911. (save-some-buffers t (lambda () (eq major-mode 'org-mode)))
  14912. (when (featurep 'org-id) (org-id-locations-save))
  14913. (message "Saving all Org-mode buffers... done"))
  14914. (defun org-revert-all-org-buffers ()
  14915. "Revert all Org-mode buffers.
  14916. Prompt for confirmation when there are unsaved changes.
  14917. Be sure you know what you are doing before letting this function
  14918. overwrite your changes.
  14919. This function is useful in a setup where one tracks org files
  14920. with a version control system, to revert on one machine after pulling
  14921. changes from another. I believe the procedure must be like this:
  14922. 1. M-x org-save-all-org-buffers
  14923. 2. Pull changes from the other machine, resolve conflicts
  14924. 3. M-x org-revert-all-org-buffers"
  14925. (interactive)
  14926. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  14927. (error "Abort"))
  14928. (save-excursion
  14929. (save-window-excursion
  14930. (mapc
  14931. (lambda (b)
  14932. (when (and (with-current-buffer b (eq major-mode 'org-mode))
  14933. (with-current-buffer b buffer-file-name))
  14934. (org-pop-to-buffer-same-window b)
  14935. (revert-buffer t 'no-confirm)))
  14936. (buffer-list))
  14937. (when (and (featurep 'org-id) org-id-track-globally)
  14938. (org-id-locations-load)))))
  14939. ;;;; Agenda files
  14940. ;;;###autoload
  14941. (defun org-switchb (&optional arg)
  14942. "Switch between Org buffers.
  14943. With one prefix argument, restrict available buffers to files.
  14944. With two prefix arguments, restrict available buffers to agenda files.
  14945. Defaults to `iswitchb' for buffer name completion.
  14946. Set `org-completion-use-ido' to make it use ido instead."
  14947. (interactive "P")
  14948. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  14949. ((equal arg '(16)) (org-buffer-list 'agenda))
  14950. (t (org-buffer-list))))
  14951. (org-completion-use-iswitchb org-completion-use-iswitchb)
  14952. (org-completion-use-ido org-completion-use-ido))
  14953. (unless (or org-completion-use-ido org-completion-use-iswitchb)
  14954. (setq org-completion-use-iswitchb t))
  14955. (org-pop-to-buffer-same-window
  14956. (org-icompleting-read "Org buffer: "
  14957. (mapcar 'list (mapcar 'buffer-name blist))
  14958. nil t))))
  14959. ;;; Define some older names previously used for this functionality
  14960. ;;;###autoload
  14961. (defalias 'org-ido-switchb 'org-switchb)
  14962. ;;;###autoload
  14963. (defalias 'org-iswitchb 'org-switchb)
  14964. (defun org-buffer-list (&optional predicate exclude-tmp)
  14965. "Return a list of Org buffers.
  14966. PREDICATE can be `export', `files' or `agenda'.
  14967. export restrict the list to Export buffers.
  14968. files restrict the list to buffers visiting Org files.
  14969. agenda restrict the list to buffers visiting agenda files.
  14970. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  14971. (let* ((bfn nil)
  14972. (agenda-files (and (eq predicate 'agenda)
  14973. (mapcar 'file-truename (org-agenda-files t))))
  14974. (filter
  14975. (cond
  14976. ((eq predicate 'files)
  14977. (lambda (b) (with-current-buffer b (eq major-mode 'org-mode))))
  14978. ((eq predicate 'export)
  14979. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  14980. ((eq predicate 'agenda)
  14981. (lambda (b)
  14982. (with-current-buffer b
  14983. (and (eq major-mode 'org-mode)
  14984. (setq bfn (buffer-file-name b))
  14985. (member (file-truename bfn) agenda-files)))))
  14986. (t (lambda (b) (with-current-buffer b
  14987. (or (eq major-mode 'org-mode)
  14988. (string-match "\*Org .*Export"
  14989. (buffer-name b)))))))))
  14990. (delq nil
  14991. (mapcar
  14992. (lambda(b)
  14993. (if (and (funcall filter b)
  14994. (or (not exclude-tmp)
  14995. (not (string-match "tmp" (buffer-name b)))))
  14996. b
  14997. nil))
  14998. (buffer-list)))))
  14999. (defun org-agenda-files (&optional unrestricted archives)
  15000. "Get the list of agenda files.
  15001. Optional UNRESTRICTED means return the full list even if a restriction
  15002. is currently in place.
  15003. When ARCHIVES is t, include all archive files that are really being
  15004. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  15005. `org-agenda-archives-mode' is t."
  15006. (let ((files
  15007. (cond
  15008. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  15009. ((stringp org-agenda-files) (org-read-agenda-file-list))
  15010. ((listp org-agenda-files) org-agenda-files)
  15011. (t (error "Invalid value of `org-agenda-files'")))))
  15012. (setq files (apply 'append
  15013. (mapcar (lambda (f)
  15014. (if (file-directory-p f)
  15015. (directory-files
  15016. f t org-agenda-file-regexp)
  15017. (list f)))
  15018. files)))
  15019. (when org-agenda-skip-unavailable-files
  15020. (setq files (delq nil
  15021. (mapcar (function
  15022. (lambda (file)
  15023. (and (file-readable-p file) file)))
  15024. files))))
  15025. (when (or (eq archives t)
  15026. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  15027. (setq files (org-add-archive-files files)))
  15028. files))
  15029. (defun org-agenda-file-p (&optional file)
  15030. "Return non-nil, if FILE is an agenda file.
  15031. If FILE is omitted, use the file associated with the current
  15032. buffer."
  15033. (member (or file (buffer-file-name))
  15034. (org-agenda-files t)))
  15035. (defun org-edit-agenda-file-list ()
  15036. "Edit the list of agenda files.
  15037. Depending on setup, this either uses customize to edit the variable
  15038. `org-agenda-files', or it visits the file that is holding the list. In the
  15039. latter case, the buffer is set up in a way that saving it automatically kills
  15040. the buffer and restores the previous window configuration."
  15041. (interactive)
  15042. (if (stringp org-agenda-files)
  15043. (let ((cw (current-window-configuration)))
  15044. (find-file org-agenda-files)
  15045. (org-set-local 'org-window-configuration cw)
  15046. (org-add-hook 'after-save-hook
  15047. (lambda ()
  15048. (set-window-configuration
  15049. (prog1 org-window-configuration
  15050. (kill-buffer (current-buffer))))
  15051. (org-install-agenda-files-menu)
  15052. (message "New agenda file list installed"))
  15053. nil 'local)
  15054. (message "%s" (substitute-command-keys
  15055. "Edit list and finish with \\[save-buffer]")))
  15056. (customize-variable 'org-agenda-files)))
  15057. (defun org-store-new-agenda-file-list (list)
  15058. "Set new value for the agenda file list and save it correctly."
  15059. (if (stringp org-agenda-files)
  15060. (let ((fe (org-read-agenda-file-list t)) b u)
  15061. (while (setq b (find-buffer-visiting org-agenda-files))
  15062. (kill-buffer b))
  15063. (with-temp-file org-agenda-files
  15064. (insert
  15065. (mapconcat
  15066. (lambda (f) ;; Keep un-expanded entries.
  15067. (if (setq u (assoc f fe))
  15068. (cdr u)
  15069. f))
  15070. list "\n")
  15071. "\n")))
  15072. (let ((org-mode-hook nil) (org-inhibit-startup t)
  15073. (org-insert-mode-line-in-empty-file nil))
  15074. (setq org-agenda-files list)
  15075. (customize-save-variable 'org-agenda-files org-agenda-files))))
  15076. (defun org-read-agenda-file-list (&optional pair-with-expansion)
  15077. "Read the list of agenda files from a file.
  15078. If PAIR-WITH-EXPANSION is t return pairs with un-expanded
  15079. filenames, used by `org-store-new-agenda-file-list' to write back
  15080. un-expanded file names."
  15081. (when (file-directory-p org-agenda-files)
  15082. (error "`org-agenda-files' cannot be a single directory"))
  15083. (when (stringp org-agenda-files)
  15084. (with-temp-buffer
  15085. (insert-file-contents org-agenda-files)
  15086. (mapcar
  15087. (lambda (f)
  15088. (let ((e (expand-file-name (substitute-in-file-name f)
  15089. org-directory)))
  15090. (if pair-with-expansion
  15091. (cons e f)
  15092. e)))
  15093. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*")))))
  15094. ;;;###autoload
  15095. (defun org-cycle-agenda-files ()
  15096. "Cycle through the files in `org-agenda-files'.
  15097. If the current buffer visits an agenda file, find the next one in the list.
  15098. If the current buffer does not, find the first agenda file."
  15099. (interactive)
  15100. (let* ((fs (org-agenda-files t))
  15101. (files (append fs (list (car fs))))
  15102. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  15103. file)
  15104. (unless files (error "No agenda files"))
  15105. (catch 'exit
  15106. (while (setq file (pop files))
  15107. (if (equal (file-truename file) tcf)
  15108. (when (car files)
  15109. (find-file (car files))
  15110. (throw 'exit t))))
  15111. (find-file (car fs)))
  15112. (if (buffer-base-buffer) (org-pop-to-buffer-same-window (buffer-base-buffer)))))
  15113. (defun org-agenda-file-to-front (&optional to-end)
  15114. "Move/add the current file to the top of the agenda file list.
  15115. If the file is not present in the list, it is added to the front. If it is
  15116. present, it is moved there. With optional argument TO-END, add/move to the
  15117. end of the list."
  15118. (interactive "P")
  15119. (let ((org-agenda-skip-unavailable-files nil)
  15120. (file-alist (mapcar (lambda (x)
  15121. (cons (file-truename x) x))
  15122. (org-agenda-files t)))
  15123. (ctf (file-truename buffer-file-name))
  15124. x had)
  15125. (setq x (assoc ctf file-alist) had x)
  15126. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  15127. (if to-end
  15128. (setq file-alist (append (delq x file-alist) (list x)))
  15129. (setq file-alist (cons x (delq x file-alist))))
  15130. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  15131. (org-install-agenda-files-menu)
  15132. (message "File %s to %s of agenda file list"
  15133. (if had "moved" "added") (if to-end "end" "front"))))
  15134. (defun org-remove-file (&optional file)
  15135. "Remove current file from the list of files in variable `org-agenda-files'.
  15136. These are the files which are being checked for agenda entries.
  15137. Optional argument FILE means use this file instead of the current."
  15138. (interactive)
  15139. (let* ((org-agenda-skip-unavailable-files nil)
  15140. (file (or file buffer-file-name))
  15141. (true-file (file-truename file))
  15142. (afile (abbreviate-file-name file))
  15143. (files (delq nil (mapcar
  15144. (lambda (x)
  15145. (if (equal true-file
  15146. (file-truename x))
  15147. nil x))
  15148. (org-agenda-files t)))))
  15149. (if (not (= (length files) (length (org-agenda-files t))))
  15150. (progn
  15151. (org-store-new-agenda-file-list files)
  15152. (org-install-agenda-files-menu)
  15153. (message "Removed file: %s" afile))
  15154. (message "File was not in list: %s (not removed)" afile))))
  15155. (defun org-file-menu-entry (file)
  15156. (vector file (list 'find-file file) t))
  15157. (defun org-check-agenda-file (file)
  15158. "Make sure FILE exists. If not, ask user what to do."
  15159. (when (not (file-exists-p file))
  15160. (message "non-existent agenda file %s. [R]emove from list or [A]bort?"
  15161. (abbreviate-file-name file))
  15162. (let ((r (downcase (read-char-exclusive))))
  15163. (cond
  15164. ((equal r ?r)
  15165. (org-remove-file file)
  15166. (throw 'nextfile t))
  15167. (t (error "Abort"))))))
  15168. (defun org-get-agenda-file-buffer (file)
  15169. "Get a buffer visiting FILE. If the buffer needs to be created, add
  15170. it to the list of buffers which might be released later."
  15171. (let ((buf (org-find-base-buffer-visiting file)))
  15172. (if buf
  15173. buf ; just return it
  15174. ;; Make a new buffer and remember it
  15175. (setq buf (find-file-noselect file))
  15176. (if buf (push buf org-agenda-new-buffers))
  15177. buf)))
  15178. (defun org-release-buffers (blist)
  15179. "Release all buffers in list, asking the user for confirmation when needed.
  15180. When a buffer is unmodified, it is just killed. When modified, it is saved
  15181. \(if the user agrees) and then killed."
  15182. (let (buf file)
  15183. (while (setq buf (pop blist))
  15184. (setq file (buffer-file-name buf))
  15185. (when (and (buffer-modified-p buf)
  15186. file
  15187. (y-or-n-p (format "Save file %s? " file)))
  15188. (with-current-buffer buf (save-buffer)))
  15189. (kill-buffer buf))))
  15190. (defun org-prepare-agenda-buffers (files)
  15191. "Create buffers for all agenda files, protect archived trees and comments."
  15192. (interactive)
  15193. (let ((pa '(:org-archived t))
  15194. (pc '(:org-comment t))
  15195. (pall '(:org-archived t :org-comment t))
  15196. (inhibit-read-only t)
  15197. (rea (concat ":" org-archive-tag ":"))
  15198. bmp file re)
  15199. (save-excursion
  15200. (save-restriction
  15201. (while (setq file (pop files))
  15202. (catch 'nextfile
  15203. (if (bufferp file)
  15204. (set-buffer file)
  15205. (org-check-agenda-file file)
  15206. (set-buffer (org-get-agenda-file-buffer file)))
  15207. (widen)
  15208. (setq bmp (buffer-modified-p))
  15209. (org-refresh-category-properties)
  15210. (setq org-todo-keywords-for-agenda
  15211. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  15212. (setq org-done-keywords-for-agenda
  15213. (append org-done-keywords-for-agenda org-done-keywords))
  15214. (setq org-todo-keyword-alist-for-agenda
  15215. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  15216. (setq org-drawers-for-agenda
  15217. (append org-drawers-for-agenda org-drawers))
  15218. (setq org-tag-alist-for-agenda
  15219. (append org-tag-alist-for-agenda org-tag-alist))
  15220. (save-excursion
  15221. (remove-text-properties (point-min) (point-max) pall)
  15222. (when org-agenda-skip-archived-trees
  15223. (goto-char (point-min))
  15224. (while (re-search-forward rea nil t)
  15225. (if (org-at-heading-p t)
  15226. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  15227. (goto-char (point-min))
  15228. (setq re (format org-heading-keyword-regexp-format
  15229. org-comment-string))
  15230. (while (re-search-forward re nil t)
  15231. (add-text-properties
  15232. (match-beginning 0) (org-end-of-subtree t) pc)))
  15233. (set-buffer-modified-p bmp)))))
  15234. (setq org-todo-keywords-for-agenda
  15235. (org-uniquify org-todo-keywords-for-agenda))
  15236. (setq org-todo-keyword-alist-for-agenda
  15237. (org-uniquify org-todo-keyword-alist-for-agenda)
  15238. org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
  15239. ;;;; Embedded LaTeX
  15240. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  15241. "Keymap for the minor `org-cdlatex-mode'.")
  15242. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  15243. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  15244. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  15245. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  15246. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  15247. (defvar org-cdlatex-texmathp-advice-is-done nil
  15248. "Flag remembering if we have applied the advice to texmathp already.")
  15249. (define-minor-mode org-cdlatex-mode
  15250. "Toggle the minor `org-cdlatex-mode'.
  15251. This mode supports entering LaTeX environment and math in LaTeX fragments
  15252. in Org-mode.
  15253. \\{org-cdlatex-mode-map}"
  15254. nil " OCDL" nil
  15255. (when org-cdlatex-mode
  15256. (require 'cdlatex)
  15257. (run-hooks 'cdlatex-mode-hook)
  15258. (cdlatex-compute-tables))
  15259. (unless org-cdlatex-texmathp-advice-is-done
  15260. (setq org-cdlatex-texmathp-advice-is-done t)
  15261. (defadvice texmathp (around org-math-always-on activate)
  15262. "Always return t in org-mode buffers.
  15263. This is because we want to insert math symbols without dollars even outside
  15264. the LaTeX math segments. If Orgmode thinks that point is actually inside
  15265. an embedded LaTeX fragment, let texmathp do its job.
  15266. \\[org-cdlatex-mode-map]"
  15267. (interactive)
  15268. (let (p)
  15269. (cond
  15270. ((not (eq major-mode 'org-mode)) ad-do-it)
  15271. ((eq this-command 'cdlatex-math-symbol)
  15272. (setq ad-return-value t
  15273. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  15274. (t
  15275. (let ((p (org-inside-LaTeX-fragment-p)))
  15276. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  15277. (setq ad-return-value t
  15278. texmathp-why '("Org-mode embedded math" . 0))
  15279. (if p ad-do-it)))))))))
  15280. (defun turn-on-org-cdlatex ()
  15281. "Unconditionally turn on `org-cdlatex-mode'."
  15282. (org-cdlatex-mode 1))
  15283. (defun org-inside-LaTeX-fragment-p ()
  15284. "Test if point is inside a LaTeX fragment.
  15285. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  15286. sequence appearing also before point.
  15287. Even though the matchers for math are configurable, this function assumes
  15288. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  15289. delimiters are skipped when they have been removed by customization.
  15290. The return value is nil, or a cons cell with the delimiter and the
  15291. position of this delimiter.
  15292. This function does a reasonably good job, but can locally be fooled by
  15293. for example currency specifications. For example it will assume being in
  15294. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  15295. fragments that are properly closed, but during editing, we have to live
  15296. with the uncertainty caused by missing closing delimiters. This function
  15297. looks only before point, not after."
  15298. (catch 'exit
  15299. (let ((pos (point))
  15300. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  15301. (lim (progn
  15302. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  15303. (point)))
  15304. dd-on str (start 0) m re)
  15305. (goto-char pos)
  15306. (when dodollar
  15307. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  15308. re (nth 1 (assoc "$" org-latex-regexps)))
  15309. (while (string-match re str start)
  15310. (cond
  15311. ((= (match-end 0) (length str))
  15312. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  15313. ((= (match-end 0) (- (length str) 5))
  15314. (throw 'exit nil))
  15315. (t (setq start (match-end 0))))))
  15316. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  15317. (goto-char pos)
  15318. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  15319. (and (match-beginning 2) (throw 'exit nil))
  15320. ;; count $$
  15321. (while (re-search-backward "\\$\\$" lim t)
  15322. (setq dd-on (not dd-on)))
  15323. (goto-char pos)
  15324. (if dd-on (cons "$$" m))))))
  15325. (defun org-inside-latex-macro-p ()
  15326. "Is point inside a LaTeX macro or its arguments?"
  15327. (save-match-data
  15328. (org-in-regexp
  15329. "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")))
  15330. (defun org-try-cdlatex-tab ()
  15331. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  15332. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  15333. - inside a LaTeX fragment, or
  15334. - after the first word in a line, where an abbreviation expansion could
  15335. insert a LaTeX environment."
  15336. (when org-cdlatex-mode
  15337. (cond
  15338. ;; Before any word on the line: No expansion possible.
  15339. ((save-excursion (skip-chars-backward " \t") (bolp)) nil)
  15340. ;; Just after first word on the line: Expand it. Make sure it
  15341. ;; cannot happen on headlines, though.
  15342. ((save-excursion
  15343. (skip-chars-backward "a-zA-Z0-9*")
  15344. (skip-chars-backward " \t")
  15345. (and (bolp) (not (org-at-heading-p))))
  15346. (cdlatex-tab) t)
  15347. ((org-inside-LaTeX-fragment-p) (cdlatex-tab) t))))
  15348. (defun org-cdlatex-underscore-caret (&optional arg)
  15349. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  15350. Revert to the normal definition outside of these fragments."
  15351. (interactive "P")
  15352. (if (org-inside-LaTeX-fragment-p)
  15353. (call-interactively 'cdlatex-sub-superscript)
  15354. (let (org-cdlatex-mode)
  15355. (call-interactively (key-binding (vector last-input-event))))))
  15356. (defun org-cdlatex-math-modify (&optional arg)
  15357. "Execute `cdlatex-math-modify' in LaTeX fragments.
  15358. Revert to the normal definition outside of these fragments."
  15359. (interactive "P")
  15360. (if (org-inside-LaTeX-fragment-p)
  15361. (call-interactively 'cdlatex-math-modify)
  15362. (let (org-cdlatex-mode)
  15363. (call-interactively (key-binding (vector last-input-event))))))
  15364. (defvar org-latex-fragment-image-overlays nil
  15365. "List of overlays carrying the images of latex fragments.")
  15366. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  15367. (defun org-remove-latex-fragment-image-overlays ()
  15368. "Remove all overlays with LaTeX fragment images in current buffer."
  15369. (mapc 'delete-overlay org-latex-fragment-image-overlays)
  15370. (setq org-latex-fragment-image-overlays nil))
  15371. (defun org-preview-latex-fragment (&optional subtree)
  15372. "Preview the LaTeX fragment at point, or all locally or globally.
  15373. If the cursor is in a LaTeX fragment, create the image and overlay
  15374. it over the source code. If there is no fragment at point, display
  15375. all fragments in the current text, from one headline to the next. With
  15376. prefix SUBTREE, display all fragments in the current subtree. With a
  15377. double prefix arg \\[universal-argument] \\[universal-argument], or when \
  15378. the cursor is before the first headline,
  15379. display all fragments in the buffer.
  15380. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  15381. (interactive "P")
  15382. (unless buffer-file-name
  15383. (error "Can't preview LaTeX fragment in a non-file buffer"))
  15384. (org-remove-latex-fragment-image-overlays)
  15385. (save-excursion
  15386. (save-restriction
  15387. (let (beg end at msg)
  15388. (cond
  15389. ((or (equal subtree '(16))
  15390. (not (save-excursion
  15391. (re-search-backward org-outline-regexp-bol nil t))))
  15392. (setq beg (point-min) end (point-max)
  15393. msg "Creating images for buffer...%s"))
  15394. ((equal subtree '(4))
  15395. (org-back-to-heading)
  15396. (setq beg (point) end (org-end-of-subtree t)
  15397. msg "Creating images for subtree...%s"))
  15398. (t
  15399. (if (setq at (org-inside-LaTeX-fragment-p))
  15400. (goto-char (max (point-min) (- (cdr at) 2)))
  15401. (org-back-to-heading))
  15402. (setq beg (point) end (progn (outline-next-heading) (point))
  15403. msg (if at "Creating image...%s"
  15404. "Creating images for entry...%s"))))
  15405. (message msg "")
  15406. (narrow-to-region beg end)
  15407. (goto-char beg)
  15408. (org-format-latex
  15409. (concat "ltxpng/" (file-name-sans-extension
  15410. (file-name-nondirectory
  15411. buffer-file-name)))
  15412. default-directory 'overlays msg at 'forbuffer 'dvipng)
  15413. (message msg "done. Use `C-c C-c' to remove images.")))))
  15414. (defvar org-latex-regexps
  15415. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  15416. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  15417. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  15418. ("$1" "\\([^$]\\|^\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  15419. ("$" "\\([^$]\\|^\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  15420. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  15421. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
  15422. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
  15423. "Regular expressions for matching embedded LaTeX.")
  15424. (defvar org-export-have-math nil) ;; dynamic scoping
  15425. (defun org-format-latex (prefix &optional dir overlays msg at
  15426. forbuffer processing-type)
  15427. "Replace LaTeX fragments with links to an image, and produce images.
  15428. Some of the options can be changed using the variable
  15429. `org-format-latex-options'."
  15430. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  15431. (let* ((prefixnodir (file-name-nondirectory prefix))
  15432. (absprefix (expand-file-name prefix dir))
  15433. (todir (file-name-directory absprefix))
  15434. (opt org-format-latex-options)
  15435. (matchers (plist-get opt :matchers))
  15436. (re-list org-latex-regexps)
  15437. (org-format-latex-header-extra
  15438. (plist-get (org-infile-export-plist) :latex-header-extra))
  15439. (cnt 0) txt hash link beg end re e checkdir
  15440. executables-checked string
  15441. m n block-type block linkfile movefile ov)
  15442. ;; Check the different regular expressions
  15443. (while (setq e (pop re-list))
  15444. (setq m (car e) re (nth 1 e) n (nth 2 e) block-type (nth 3 e)
  15445. block (if block-type "\n\n" ""))
  15446. (when (member m matchers)
  15447. (goto-char (point-min))
  15448. (while (re-search-forward re nil t)
  15449. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  15450. (not (get-text-property (match-beginning n)
  15451. 'org-protected))
  15452. (or (not overlays)
  15453. (not (eq (get-char-property (match-beginning n)
  15454. 'org-overlay-type)
  15455. 'org-latex-overlay))))
  15456. (setq org-export-have-math t)
  15457. (cond
  15458. ((eq processing-type 'verbatim)
  15459. ;; Leave the text verbatim, just protect it
  15460. (add-text-properties (match-beginning n) (match-end n)
  15461. '(org-protected t)))
  15462. ((eq processing-type 'mathjax)
  15463. ;; Prepare for MathJax processing
  15464. (setq string (match-string n))
  15465. (if (member m '("$" "$1"))
  15466. (save-excursion
  15467. (delete-region (match-beginning n) (match-end n))
  15468. (goto-char (match-beginning n))
  15469. (insert (org-add-props (concat "\\(" (substring string 1 -1)
  15470. "\\)")
  15471. '(org-protected t))))
  15472. (add-text-properties (match-beginning n) (match-end n)
  15473. '(org-protected t))))
  15474. ((eq processing-type 'dvipng)
  15475. ;; Process to an image
  15476. (setq txt (match-string n)
  15477. beg (match-beginning n) end (match-end n)
  15478. cnt (1+ cnt))
  15479. (let (print-length print-level) ; make sure full list is printed
  15480. (setq hash (sha1 (prin1-to-string
  15481. (list org-format-latex-header
  15482. org-format-latex-header-extra
  15483. org-export-latex-default-packages-alist
  15484. org-export-latex-packages-alist
  15485. org-format-latex-options
  15486. forbuffer txt)))
  15487. linkfile (format "%s_%s.png" prefix hash)
  15488. movefile (format "%s_%s.png" absprefix hash)))
  15489. (setq link (concat block "[[file:" linkfile "]]" block))
  15490. (if msg (message msg cnt))
  15491. (goto-char beg)
  15492. (unless checkdir ; make sure the directory exists
  15493. (setq checkdir t)
  15494. (or (file-directory-p todir) (make-directory todir t)))
  15495. (unless executables-checked
  15496. (org-check-external-command
  15497. "latex" "needed to convert LaTeX fragments to images")
  15498. (org-check-external-command
  15499. "dvipng" "needed to convert LaTeX fragments to images")
  15500. (setq executables-checked t))
  15501. (unless (file-exists-p movefile)
  15502. (org-create-formula-image
  15503. txt movefile opt forbuffer))
  15504. (if overlays
  15505. (progn
  15506. (mapc (lambda (o)
  15507. (if (eq (overlay-get o 'org-overlay-type)
  15508. 'org-latex-overlay)
  15509. (delete-overlay o)))
  15510. (overlays-in beg end))
  15511. (setq ov (make-overlay beg end))
  15512. (overlay-put ov 'org-overlay-type 'org-latex-overlay)
  15513. (if (featurep 'xemacs)
  15514. (progn
  15515. (overlay-put ov 'invisible t)
  15516. (overlay-put
  15517. ov 'end-glyph
  15518. (make-glyph (vector 'png :file movefile))))
  15519. (overlay-put
  15520. ov 'display
  15521. (list 'image :type 'png :file movefile :ascent 'center)))
  15522. (push ov org-latex-fragment-image-overlays)
  15523. (goto-char end))
  15524. (delete-region beg end)
  15525. (insert (org-add-props link
  15526. (list 'org-latex-src
  15527. (replace-regexp-in-string
  15528. "\"" "" txt)
  15529. 'org-latex-src-embed-type
  15530. (if block-type 'paragraph 'character))))))
  15531. ((eq processing-type 'mathml)
  15532. ;; Process to MathML
  15533. (unless executables-checked
  15534. (unless (save-match-data (org-format-latex-mathml-available-p))
  15535. (error "LaTeX to MathML converter not configured"))
  15536. (setq executables-checked t))
  15537. (setq txt (match-string n)
  15538. beg (match-beginning n) end (match-end n)
  15539. cnt (1+ cnt))
  15540. (if msg (message msg cnt))
  15541. (goto-char beg)
  15542. (delete-region beg end)
  15543. (insert (org-format-latex-as-mathml
  15544. txt block-type prefix dir)))
  15545. (t
  15546. (error "Unknown conversion type %s for latex fragments"
  15547. processing-type)))))))))
  15548. (defun org-create-math-formula (latex-frag &optional mathml-file)
  15549. "Convert LATEX-FRAG to MathML and store it in MATHML-FILE.
  15550. Use `org-latex-to-mathml-convert-command'. If the conversion is
  15551. sucessful, return the portion between \"<math...> </math>\"
  15552. elements otherwise return nil. When MATHML-FILE is specified,
  15553. write the results in to that file. When invoked as an
  15554. interactive command, prompt for LATEX-FRAG, with initial value
  15555. set to the current active region and echo the results for user
  15556. inspection."
  15557. (interactive (list (let ((frag (when (region-active-p)
  15558. (buffer-substring-no-properties
  15559. (region-beginning) (region-end)))))
  15560. (read-string "LaTeX Fragment: " frag nil frag))))
  15561. (unless latex-frag (error "Invalid latex-frag"))
  15562. (let* ((tmp-in-file (file-relative-name
  15563. (make-temp-name (expand-file-name "ltxmathml-in"))))
  15564. (ignore (write-region latex-frag nil tmp-in-file))
  15565. (tmp-out-file (file-relative-name
  15566. (make-temp-name (expand-file-name "ltxmathml-out"))))
  15567. (cmd (format-spec
  15568. org-latex-to-mathml-convert-command
  15569. `((?j . ,(shell-quote-argument
  15570. (expand-file-name org-latex-to-mathml-jar-file)))
  15571. (?I . ,(shell-quote-argument tmp-in-file))
  15572. (?o . ,(shell-quote-argument tmp-out-file)))))
  15573. mathml shell-command-output)
  15574. (when (org-called-interactively-p 'any)
  15575. (unless (org-format-latex-mathml-available-p)
  15576. (error "LaTeX to MathML converter not configured")))
  15577. (message "Running %s" cmd)
  15578. (setq shell-command-output (shell-command-to-string cmd))
  15579. (setq mathml
  15580. (when (file-readable-p tmp-out-file)
  15581. (with-current-buffer (find-file-noselect tmp-out-file t)
  15582. (goto-char (point-min))
  15583. (when (re-search-forward
  15584. (concat
  15585. (regexp-quote
  15586. "<math xmlns=\"http://www.w3.org/1998/Math/MathML\">")
  15587. "\\(.\\|\n\\)*"
  15588. (regexp-quote "</math>")) nil t)
  15589. (prog1 (match-string 0) (kill-buffer))))))
  15590. (cond
  15591. (mathml
  15592. (setq mathml
  15593. (concat "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" mathml))
  15594. (when mathml-file
  15595. (write-region mathml nil mathml-file))
  15596. (when (org-called-interactively-p 'any)
  15597. (message mathml)))
  15598. ((message "LaTeX to MathML conversion failed")
  15599. (message shell-command-output)))
  15600. (delete-file tmp-in-file)
  15601. (when (file-exists-p tmp-out-file)
  15602. (delete-file tmp-out-file))
  15603. mathml))
  15604. (defun org-format-latex-as-mathml (latex-frag latex-frag-type
  15605. prefix &optional dir)
  15606. "Use `org-create-math-formula' but check local cache first."
  15607. (let* ((absprefix (expand-file-name prefix dir))
  15608. (print-length nil) (print-level nil)
  15609. (formula-id (concat
  15610. "formula-"
  15611. (sha1
  15612. (prin1-to-string
  15613. (list latex-frag
  15614. org-latex-to-mathml-convert-command)))))
  15615. (formula-cache (format "%s-%s.mathml" absprefix formula-id))
  15616. (formula-cache-dir (file-name-directory formula-cache)))
  15617. (unless (file-directory-p formula-cache-dir)
  15618. (make-directory formula-cache-dir t))
  15619. (unless (file-exists-p formula-cache)
  15620. (org-create-math-formula latex-frag formula-cache))
  15621. (if (file-exists-p formula-cache)
  15622. ;; Successful conversion. Return the link to MathML file.
  15623. (org-add-props
  15624. (format "[[file:%s]]" (file-relative-name formula-cache dir))
  15625. (list 'org-latex-src (replace-regexp-in-string "\"" "" latex-frag)
  15626. 'org-latex-src-embed-type (if latex-frag-type
  15627. 'paragraph 'character)))
  15628. ;; Failed conversion. Return the LaTeX fragment verbatim
  15629. (add-text-properties
  15630. 0 (1- (length latex-frag)) '(org-protected t) latex-frag)
  15631. latex-frag)))
  15632. ;; This function borrows from Ganesh Swami's latex2png.el
  15633. (defun org-create-formula-image (string tofile options buffer)
  15634. "This calls dvipng."
  15635. (require 'org-latex)
  15636. (let* ((tmpdir (if (featurep 'xemacs)
  15637. (temp-directory)
  15638. temporary-file-directory))
  15639. (texfilebase (make-temp-name
  15640. (expand-file-name "orgtex" tmpdir)))
  15641. (texfile (concat texfilebase ".tex"))
  15642. (dvifile (concat texfilebase ".dvi"))
  15643. (pngfile (concat texfilebase ".png"))
  15644. (fnh (if (featurep 'xemacs)
  15645. (font-height (get-face-font 'default))
  15646. (face-attribute 'default :height nil)))
  15647. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  15648. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  15649. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  15650. "Black"))
  15651. (bg (or (plist-get options (if buffer :background :html-background))
  15652. "Transparent")))
  15653. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  15654. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  15655. (with-temp-file texfile
  15656. (insert (org-splice-latex-header
  15657. org-format-latex-header
  15658. org-export-latex-default-packages-alist
  15659. org-export-latex-packages-alist t
  15660. org-format-latex-header-extra))
  15661. (insert "\n\\begin{document}\n" string "\n\\end{document}\n")
  15662. (require 'org-latex)
  15663. (org-export-latex-fix-inputenc))
  15664. (let ((dir default-directory))
  15665. (condition-case nil
  15666. (progn
  15667. (cd tmpdir)
  15668. (call-process "latex" nil nil nil texfile))
  15669. (error nil))
  15670. (cd dir))
  15671. (if (not (file-exists-p dvifile))
  15672. (progn (message "Failed to create dvi file from %s" texfile) nil)
  15673. (condition-case nil
  15674. (call-process "dvipng" nil nil nil
  15675. "-fg" fg "-bg" bg
  15676. "-D" dpi
  15677. ;;"-x" scale "-y" scale
  15678. "-T" "tight"
  15679. "-o" pngfile
  15680. dvifile)
  15681. (error nil))
  15682. (if (not (file-exists-p pngfile))
  15683. (if org-format-latex-signal-error
  15684. (error "Failed to create png file from %s" texfile)
  15685. (message "Failed to create png file from %s" texfile)
  15686. nil)
  15687. ;; Use the requested file name and clean up
  15688. (copy-file pngfile tofile 'replace)
  15689. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  15690. (delete-file (concat texfilebase e)))
  15691. pngfile))))
  15692. (defun org-splice-latex-header (tpl def-pkg pkg snippets-p &optional extra)
  15693. "Fill a LaTeX header template TPL.
  15694. In the template, the following place holders will be recognized:
  15695. [DEFAULT-PACKAGES] \\usepackage statements for DEF-PKG
  15696. [NO-DEFAULT-PACKAGES] do not include DEF-PKG
  15697. [PACKAGES] \\usepackage statements for PKG
  15698. [NO-PACKAGES] do not include PKG
  15699. [EXTRA] the string EXTRA
  15700. [NO-EXTRA] do not include EXTRA
  15701. For backward compatibility, if both the positive and the negative place
  15702. holder is missing, the positive one (without the \"NO-\") will be
  15703. assumed to be present at the end of the template.
  15704. DEF-PKG and PKG are assumed to be alists of options/packagename lists.
  15705. EXTRA is a string.
  15706. SNIPPETS-P indicates if this is run to create snippet images for HTML."
  15707. (let (rpl (end ""))
  15708. (if (string-match "^[ \t]*\\[\\(NO-\\)?DEFAULT-PACKAGES\\][ \t]*\n?" tpl)
  15709. (setq rpl (if (or (match-end 1) (not def-pkg))
  15710. "" (org-latex-packages-to-string def-pkg snippets-p t))
  15711. tpl (replace-match rpl t t tpl))
  15712. (if def-pkg (setq end (org-latex-packages-to-string def-pkg snippets-p))))
  15713. (if (string-match "\\[\\(NO-\\)?PACKAGES\\][ \t]*\n?" tpl)
  15714. (setq rpl (if (or (match-end 1) (not pkg))
  15715. "" (org-latex-packages-to-string pkg snippets-p t))
  15716. tpl (replace-match rpl t t tpl))
  15717. (if pkg (setq end
  15718. (concat end "\n"
  15719. (org-latex-packages-to-string pkg snippets-p)))))
  15720. (if (string-match "\\[\\(NO-\\)?EXTRA\\][ \t]*\n?" tpl)
  15721. (setq rpl (if (or (match-end 1) (not extra))
  15722. "" (concat extra "\n"))
  15723. tpl (replace-match rpl t t tpl))
  15724. (if (and extra (string-match "\\S-" extra))
  15725. (setq end (concat end "\n" extra))))
  15726. (if (string-match "\\S-" end)
  15727. (concat tpl "\n" end)
  15728. tpl)))
  15729. (defun org-latex-packages-to-string (pkg &optional snippets-p newline)
  15730. "Turn an alist of packages into a string with the \\usepackage macros."
  15731. (setq pkg (mapconcat (lambda(p)
  15732. (cond
  15733. ((stringp p) p)
  15734. ((and snippets-p (>= (length p) 3) (not (nth 2 p)))
  15735. (format "%% Package %s omitted" (cadr p)))
  15736. ((equal "" (car p))
  15737. (format "\\usepackage{%s}" (cadr p)))
  15738. (t
  15739. (format "\\usepackage[%s]{%s}"
  15740. (car p) (cadr p)))))
  15741. pkg
  15742. "\n"))
  15743. (if newline (concat pkg "\n") pkg))
  15744. (defun org-dvipng-color (attr)
  15745. "Return an rgb color specification for dvipng."
  15746. (apply 'format "rgb %s %s %s"
  15747. (mapcar 'org-normalize-color
  15748. (color-values (face-attribute 'default attr nil)))))
  15749. (defun org-normalize-color (value)
  15750. "Return string to be used as color value for an RGB component."
  15751. (format "%g" (/ value 65535.0)))
  15752. ;; Image display
  15753. (defvar org-inline-image-overlays nil)
  15754. (make-variable-buffer-local 'org-inline-image-overlays)
  15755. (defun org-toggle-inline-images (&optional include-linked)
  15756. "Toggle the display of inline images.
  15757. INCLUDE-LINKED is passed to `org-display-inline-images'."
  15758. (interactive "P")
  15759. (if org-inline-image-overlays
  15760. (progn
  15761. (org-remove-inline-images)
  15762. (message "Inline image display turned off"))
  15763. (org-display-inline-images include-linked)
  15764. (if org-inline-image-overlays
  15765. (message "%d images displayed inline"
  15766. (length org-inline-image-overlays))
  15767. (message "No images to display inline"))))
  15768. (defun org-display-inline-images (&optional include-linked refresh beg end)
  15769. "Display inline images.
  15770. Normally only links without a description part are inlined, because this
  15771. is how it will work for export. When INCLUDE-LINKED is set, also links
  15772. with a description part will be inlined. This can be nice for a quick
  15773. look at those images, but it does not reflect what exported files will look
  15774. like.
  15775. When REFRESH is set, refresh existing images between BEG and END.
  15776. This will create new image displays only if necessary.
  15777. BEG and END default to the buffer boundaries."
  15778. (interactive "P")
  15779. (unless refresh
  15780. (org-remove-inline-images)
  15781. (if (fboundp 'clear-image-cache) (clear-image-cache)))
  15782. (save-excursion
  15783. (save-restriction
  15784. (widen)
  15785. (setq beg (or beg (point-min)) end (or end (point-max)))
  15786. (goto-char (point-min))
  15787. (let ((re (concat "\\[\\[\\(\\(file:\\)\\|\\([./~]\\)\\)\\([^]\n]+?"
  15788. (substring (org-image-file-name-regexp) 0 -2)
  15789. "\\)\\]" (if include-linked "" "\\]")))
  15790. old file ov img)
  15791. (while (re-search-forward re end t)
  15792. (setq old (get-char-property-and-overlay (match-beginning 1)
  15793. 'org-image-overlay))
  15794. (setq file (expand-file-name
  15795. (concat (or (match-string 3) "") (match-string 4))))
  15796. (when (file-exists-p file)
  15797. (if (and (car-safe old) refresh)
  15798. (image-refresh (overlay-get (cdr old) 'display))
  15799. (setq img (save-match-data (create-image file)))
  15800. (when img
  15801. (setq ov (make-overlay (match-beginning 0) (match-end 0)))
  15802. (overlay-put ov 'display img)
  15803. (overlay-put ov 'face 'default)
  15804. (overlay-put ov 'org-image-overlay t)
  15805. (overlay-put ov 'modification-hooks
  15806. (list 'org-display-inline-modification-hook))
  15807. (push ov org-inline-image-overlays)))))))))
  15808. (defun org-display-inline-modification-hook (ov after beg end &optional len)
  15809. "Remove inline-display overlay if a corresponding region is modified."
  15810. (let ((inhibit-modification-hooks t))
  15811. (when (and ov after)
  15812. (delete ov org-inline-image-overlays)
  15813. (delete-overlay ov))))
  15814. (defun org-remove-inline-images ()
  15815. "Remove inline display of images."
  15816. (interactive)
  15817. (mapc 'delete-overlay org-inline-image-overlays)
  15818. (setq org-inline-image-overlays nil))
  15819. ;;;; Key bindings
  15820. ;; Outline functions from `outline-mode-prefix-map'
  15821. ;; that can be remapped in Org:
  15822. (define-key org-mode-map [remap outline-mark-subtree] 'org-mark-subtree)
  15823. (define-key org-mode-map [remap show-subtree] 'org-show-subtree)
  15824. (define-key org-mode-map [remap outline-forward-same-level]
  15825. 'org-forward-same-level)
  15826. (define-key org-mode-map [remap outline-backward-same-level]
  15827. 'org-backward-same-level)
  15828. (define-key org-mode-map [remap show-branches]
  15829. 'org-kill-note-or-show-branches)
  15830. (define-key org-mode-map [remap outline-promote] 'org-promote-subtree)
  15831. (define-key org-mode-map [remap outline-demote] 'org-demote-subtree)
  15832. (define-key org-mode-map [remap outline-insert-heading] 'org-ctrl-c-ret)
  15833. ;; Outline functions from `outline-mode-prefix-map'
  15834. ;; that can not be remapped in Org:
  15835. ;; - the column "key binding" shows whether the Outline function is still
  15836. ;; available in Org mode on the same key that it has been bound to in
  15837. ;; Outline mode:
  15838. ;; - "overridden": key used for a different functionality in Org mode
  15839. ;; - else: key still bound to the same Outline function in Org mode
  15840. ;; | Outline function | key binding | Org replacement |
  15841. ;; |------------------------------------+-------------+-----------------------|
  15842. ;; | `outline-next-visible-heading' | `C-c C-n' | still same function |
  15843. ;; | `outline-previous-visible-heading' | `C-c C-p' | still same function |
  15844. ;; | `show-children' | `C-c C-i' | visibility cycling |
  15845. ;; | `hide-subtree' | overridden | visibility cycling |
  15846. ;; | `outline-up-heading' | `C-c C-u' | still same function |
  15847. ;; | `hide-body' | overridden | no replacement |
  15848. ;; | `show-all' | overridden | no replacement |
  15849. ;; | `hide-entry' | overridden | visibility cycling |
  15850. ;; | `show-entry' | overridden | no replacement |
  15851. ;; | `hide-leaves' | overridden | no replacement |
  15852. ;; | `hide-sublevels' | overridden | no replacement |
  15853. ;; | `hide-other' | overridden | no replacement |
  15854. ;; | `outline-move-subtree-up' | `C-c C-^' | better: org-shiftup |
  15855. ;; | `outline-move-subtree-down' | overridden | better: org-shiftdown |
  15856. ;; Make `C-c C-x' a prefix key
  15857. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  15858. ;; TAB key with modifiers
  15859. (org-defkey org-mode-map "\C-i" 'org-cycle)
  15860. (org-defkey org-mode-map [(tab)] 'org-cycle)
  15861. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  15862. (org-defkey org-mode-map [(meta tab)] 'pcomplete)
  15863. (org-defkey org-mode-map "\M-\t" 'pcomplete)
  15864. (org-defkey org-mode-map "\M-\C-i" 'pcomplete)
  15865. ;; The following line is necessary under Suse GNU/Linux
  15866. (unless (featurep 'xemacs)
  15867. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  15868. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  15869. (define-key org-mode-map [backtab] 'org-shifttab)
  15870. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  15871. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  15872. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  15873. ;; Cursor keys with modifiers
  15874. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  15875. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  15876. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  15877. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  15878. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  15879. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  15880. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  15881. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  15882. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  15883. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  15884. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  15885. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  15886. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  15887. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  15888. (org-defkey org-mode-map [(control shift up)] 'org-shiftcontrolup)
  15889. (org-defkey org-mode-map [(control shift down)] 'org-shiftcontroldown)
  15890. ;; Babel keys
  15891. (define-key org-mode-map org-babel-key-prefix org-babel-map)
  15892. (mapc (lambda (pair)
  15893. (define-key org-babel-map (car pair) (cdr pair)))
  15894. org-babel-key-bindings)
  15895. ;;; Extra keys for tty access.
  15896. ;; We only set them when really needed because otherwise the
  15897. ;; menus don't show the simple keys
  15898. (when (or org-use-extra-keys
  15899. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  15900. (not window-system))
  15901. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  15902. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  15903. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  15904. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  15905. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  15906. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  15907. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  15908. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  15909. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  15910. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  15911. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  15912. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  15913. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  15914. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  15915. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  15916. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  15917. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  15918. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  15919. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  15920. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  15921. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  15922. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
  15923. (org-defkey org-mode-map [?\e (tab)] 'pcomplete)
  15924. (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
  15925. (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
  15926. (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
  15927. (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
  15928. (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
  15929. ;; All the other keys
  15930. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  15931. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  15932. (if (boundp 'narrow-map)
  15933. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  15934. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  15935. (if (boundp 'narrow-map)
  15936. (org-defkey narrow-map "b" 'org-narrow-to-block)
  15937. (org-defkey org-mode-map "\C-xnb" 'org-narrow-to-block))
  15938. (org-defkey org-mode-map "\C-c\C-f" 'org-forward-same-level)
  15939. (org-defkey org-mode-map "\C-c\C-b" 'org-backward-same-level)
  15940. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  15941. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  15942. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
  15943. (org-defkey org-mode-map "\C-c\C-xd" 'org-insert-drawer)
  15944. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  15945. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  15946. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  15947. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  15948. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  15949. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  15950. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  15951. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  15952. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  15953. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  15954. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  15955. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  15956. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  15957. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  15958. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  15959. (org-defkey org-mode-map "\C-c\C-xv" 'org-copy-visible)
  15960. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  15961. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  15962. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  15963. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  15964. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  15965. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  15966. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  15967. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  15968. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  15969. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  15970. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  15971. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  15972. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  15973. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  15974. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  15975. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  15976. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  15977. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  15978. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  15979. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  15980. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  15981. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  15982. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  15983. (org-defkey org-mode-map "\C-c^" 'org-sort)
  15984. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  15985. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  15986. (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
  15987. (org-defkey org-mode-map "\C-m" 'org-return)
  15988. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  15989. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  15990. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  15991. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  15992. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  15993. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  15994. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  15995. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  15996. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  15997. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  15998. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  15999. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  16000. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  16001. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  16002. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  16003. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  16004. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  16005. (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
  16006. (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
  16007. (org-defkey org-mode-map "\C-c@" 'org-mark-subtree)
  16008. (org-defkey org-mode-map "\C-c\C-@" 'org-mark-list)
  16009. (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
  16010. ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
  16011. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-mark-entry-for-agenda-action)
  16012. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  16013. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  16014. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  16015. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  16016. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  16017. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  16018. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  16019. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  16020. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  16021. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  16022. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  16023. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  16024. (org-defkey org-mode-map "\C-c\C-x\C-v" 'org-toggle-inline-images)
  16025. (org-defkey org-mode-map "\C-c\C-x\\" 'org-toggle-pretty-entities)
  16026. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  16027. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  16028. (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
  16029. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  16030. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  16031. (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
  16032. (org-defkey org-mode-map [(control ?c) (control ?x) ?\:] 'org-timer-cancel-timer)
  16033. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  16034. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  16035. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  16036. (org-defkey org-mode-map "\C-c\C-x_" 'org-timer-stop)
  16037. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  16038. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  16039. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  16040. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  16041. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  16042. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  16043. (when (featurep 'xemacs)
  16044. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  16045. (defconst org-speed-commands-default
  16046. '(
  16047. ("Outline Navigation")
  16048. ("n" . (org-speed-move-safe 'outline-next-visible-heading))
  16049. ("p" . (org-speed-move-safe 'outline-previous-visible-heading))
  16050. ("f" . (org-speed-move-safe 'org-forward-same-level))
  16051. ("b" . (org-speed-move-safe 'org-backward-same-level))
  16052. ("u" . (org-speed-move-safe 'outline-up-heading))
  16053. ("j" . org-goto)
  16054. ("g" . (org-refile t))
  16055. ("Outline Visibility")
  16056. ("c" . org-cycle)
  16057. ("C" . org-shifttab)
  16058. (" " . org-display-outline-path)
  16059. ("Outline Structure Editing")
  16060. ("U" . org-shiftmetaup)
  16061. ("D" . org-shiftmetadown)
  16062. ("r" . org-metaright)
  16063. ("l" . org-metaleft)
  16064. ("R" . org-shiftmetaright)
  16065. ("L" . org-shiftmetaleft)
  16066. ("i" . (progn (forward-char 1) (call-interactively
  16067. 'org-insert-heading-respect-content)))
  16068. ("^" . org-sort)
  16069. ("w" . org-refile)
  16070. ("a" . org-archive-subtree-default-with-confirmation)
  16071. ("." . org-mark-subtree)
  16072. ("Clock Commands")
  16073. ("I" . org-clock-in)
  16074. ("O" . org-clock-out)
  16075. ("Meta Data Editing")
  16076. ("t" . org-todo)
  16077. ("0" . (org-priority ?\ ))
  16078. ("1" . (org-priority ?A))
  16079. ("2" . (org-priority ?B))
  16080. ("3" . (org-priority ?C))
  16081. (";" . org-set-tags-command)
  16082. ("e" . org-set-effort)
  16083. ("Agenda Views etc")
  16084. ("v" . org-agenda)
  16085. ("/" . org-sparse-tree)
  16086. ("Misc")
  16087. ("o" . org-open-at-point)
  16088. ("?" . org-speed-command-help)
  16089. ("<" . (org-agenda-set-restriction-lock 'subtree))
  16090. (">" . (org-agenda-remove-restriction-lock))
  16091. )
  16092. "The default speed commands.")
  16093. (defun org-print-speed-command (e)
  16094. (if (> (length (car e)) 1)
  16095. (progn
  16096. (princ "\n")
  16097. (princ (car e))
  16098. (princ "\n")
  16099. (princ (make-string (length (car e)) ?-))
  16100. (princ "\n"))
  16101. (princ (car e))
  16102. (princ " ")
  16103. (if (symbolp (cdr e))
  16104. (princ (symbol-name (cdr e)))
  16105. (prin1 (cdr e)))
  16106. (princ "\n")))
  16107. (defun org-speed-command-help ()
  16108. "Show the available speed commands."
  16109. (interactive)
  16110. (if (not org-use-speed-commands)
  16111. (error "Speed commands are not activated, customize `org-use-speed-commands'")
  16112. (with-output-to-temp-buffer "*Help*"
  16113. (princ "User-defined Speed commands\n===========================\n")
  16114. (mapc 'org-print-speed-command org-speed-commands-user)
  16115. (princ "\n")
  16116. (princ "Built-in Speed commands\n=======================\n")
  16117. (mapc 'org-print-speed-command org-speed-commands-default))
  16118. (with-current-buffer "*Help*"
  16119. (setq truncate-lines t))))
  16120. (defun org-speed-move-safe (cmd)
  16121. "Execute CMD, but make sure that the cursor always ends up in a headline.
  16122. If not, return to the original position and throw an error."
  16123. (interactive)
  16124. (let ((pos (point)))
  16125. (call-interactively cmd)
  16126. (unless (and (bolp) (org-at-heading-p))
  16127. (goto-char pos)
  16128. (error "Boundary reached while executing %s" cmd))))
  16129. (defvar org-self-insert-command-undo-counter 0)
  16130. (defvar org-table-auto-blank-field) ; defined in org-table.el
  16131. (defvar org-speed-command nil)
  16132. (defun org-speed-command-default-hook (keys)
  16133. "Hook for activating single-letter speed commands.
  16134. `org-speed-commands-default' specifies a minimal command set.
  16135. Use `org-speed-commands-user' for further customization."
  16136. (when (or (and (bolp) (looking-at org-outline-regexp))
  16137. (and (functionp org-use-speed-commands)
  16138. (funcall org-use-speed-commands)))
  16139. (cdr (assoc keys (append org-speed-commands-user
  16140. org-speed-commands-default)))))
  16141. (defun org-babel-speed-command-hook (keys)
  16142. "Hook for activating single-letter code block commands."
  16143. (when (and (bolp) (looking-at org-babel-src-block-regexp))
  16144. (cdr (assoc keys org-babel-key-bindings))))
  16145. (defcustom org-speed-command-hook
  16146. '(org-speed-command-default-hook org-babel-speed-command-hook)
  16147. "Hook for activating speed commands at strategic locations.
  16148. Hook functions are called in sequence until a valid handler is
  16149. found.
  16150. Each hook takes a single argument, a user-pressed command key
  16151. which is also a `self-insert-command' from the global map.
  16152. Within the hook, examine the cursor position and the command key
  16153. and return nil or a valid handler as appropriate. Handler could
  16154. be one of an interactive command, a function, or a form.
  16155. Set `org-use-speed-commands' to non-nil value to enable this
  16156. hook. The default setting is `org-speed-command-default-hook'."
  16157. :group 'org-structure
  16158. :version "24.1"
  16159. :type 'hook)
  16160. (defun org-self-insert-command (N)
  16161. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  16162. If the cursor is in a table looking at whitespace, the whitespace is
  16163. overwritten, and the table is not marked as requiring realignment."
  16164. (interactive "p")
  16165. (org-check-before-invisible-edit 'insert)
  16166. (cond
  16167. ((and org-use-speed-commands
  16168. (setq org-speed-command
  16169. (run-hook-with-args-until-success
  16170. 'org-speed-command-hook (this-command-keys))))
  16171. (cond
  16172. ((commandp org-speed-command)
  16173. (setq this-command org-speed-command)
  16174. (call-interactively org-speed-command))
  16175. ((functionp org-speed-command)
  16176. (funcall org-speed-command))
  16177. ((and org-speed-command (listp org-speed-command))
  16178. (eval org-speed-command))
  16179. (t (let (org-use-speed-commands)
  16180. (call-interactively 'org-self-insert-command)))))
  16181. ((and
  16182. (org-table-p)
  16183. (progn
  16184. ;; check if we blank the field, and if that triggers align
  16185. (and (featurep 'org-table) org-table-auto-blank-field
  16186. (member last-command
  16187. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c yas/expand))
  16188. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  16189. ;; got extra space, this field does not determine column width
  16190. (let (org-table-may-need-update) (org-table-blank-field))
  16191. ;; no extra space, this field may determine column width
  16192. (org-table-blank-field)))
  16193. t)
  16194. (eq N 1)
  16195. (looking-at "[^|\n]* |"))
  16196. (let (org-table-may-need-update)
  16197. (goto-char (1- (match-end 0)))
  16198. (delete-char -1)
  16199. (goto-char (match-beginning 0))
  16200. (self-insert-command N)))
  16201. (t
  16202. (setq org-table-may-need-update t)
  16203. (self-insert-command N)
  16204. (org-fix-tags-on-the-fly)
  16205. (if org-self-insert-cluster-for-undo
  16206. (if (not (eq last-command 'org-self-insert-command))
  16207. (setq org-self-insert-command-undo-counter 1)
  16208. (if (>= org-self-insert-command-undo-counter 20)
  16209. (setq org-self-insert-command-undo-counter 1)
  16210. (and (> org-self-insert-command-undo-counter 0)
  16211. buffer-undo-list (listp buffer-undo-list)
  16212. (not (cadr buffer-undo-list)) ; remove nil entry
  16213. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  16214. (setq org-self-insert-command-undo-counter
  16215. (1+ org-self-insert-command-undo-counter))))))))
  16216. (defun org-check-before-invisible-edit (kind)
  16217. "Check is editing if kind KIND would be dangerous with invisible text around.
  16218. The detailed reaction depends on the user option `org-catch-invisible-edits'."
  16219. ;; First, try to get out of here as quickly as possible, to reduce overhead
  16220. (if (and org-catch-invisible-edits
  16221. (or (not (boundp 'visible-mode)) (not visible-mode))
  16222. (or (get-char-property (point) 'invisible)
  16223. (get-char-property (max (point-min) (1- (point))) 'invisible)))
  16224. ;; OK, we need to take a closer look
  16225. (let* ((invisible-at-point (get-char-property (point) 'invisible))
  16226. (invisible-before-point (if (bobp) nil (get-char-property
  16227. (1- (point)) 'invisible)))
  16228. (border-and-ok-direction
  16229. (or
  16230. ;; Check if we are acting predictably before invisible text
  16231. (and invisible-at-point (not invisible-before-point)
  16232. (memq kind '(insert delete-backward)))
  16233. ;; Check if we are acting predictably after invisible text
  16234. ;; This works not well, and I have turned it off. It seems
  16235. ;; better to always show and stop after invisible text.
  16236. ;; (and (not invisible-at-point) invisible-before-point
  16237. ;; (memq kind '(insert delete)))
  16238. )))
  16239. (when (or (memq invisible-at-point '(outline org-hide-block))
  16240. (memq invisible-before-point '(outline org-hide-block)))
  16241. (if (eq org-catch-invisible-edits 'error)
  16242. (error "Editing in invisible areas is prohibited - make visible first"))
  16243. ;; Make the area visible
  16244. (save-excursion
  16245. (if invisible-before-point
  16246. (goto-char (previous-single-char-property-change
  16247. (point) 'invisible)))
  16248. (org-cycle))
  16249. (cond
  16250. ((eq org-catch-invisible-edits 'show)
  16251. ;; That's it, we do the edit after showing
  16252. (message
  16253. "Unfolding invisible region around point before editing")
  16254. (sit-for 1))
  16255. ((and (eq org-catch-invisible-edits 'smart)
  16256. border-and-ok-direction)
  16257. (message "Unfolding invisible region around point before editing"))
  16258. (t
  16259. ;; Don't do the edit, make the user repeat it in full visibility
  16260. (error "Edit in invisible region aborted, repeat to confirm with text visible")))))))
  16261. (defun org-fix-tags-on-the-fly ()
  16262. (when (and (equal (char-after (point-at-bol)) ?*)
  16263. (org-at-heading-p))
  16264. (org-align-tags-here org-tags-column)))
  16265. (defun org-delete-backward-char (N)
  16266. "Like `delete-backward-char', insert whitespace at field end in tables.
  16267. When deleting backwards, in tables this function will insert whitespace in
  16268. front of the next \"|\" separator, to keep the table aligned. The table will
  16269. still be marked for re-alignment if the field did fill the entire column,
  16270. because, in this case the deletion might narrow the column."
  16271. (interactive "p")
  16272. (org-check-before-invisible-edit 'delete-backward)
  16273. (if (and (org-table-p)
  16274. (eq N 1)
  16275. (string-match "|" (buffer-substring (point-at-bol) (point)))
  16276. (looking-at ".*?|"))
  16277. (let ((pos (point))
  16278. (noalign (looking-at "[^|\n\r]* |"))
  16279. (c org-table-may-need-update))
  16280. (backward-delete-char N)
  16281. (if (not overwrite-mode)
  16282. (progn
  16283. (skip-chars-forward "^|")
  16284. (insert " ")
  16285. (goto-char (1- pos))))
  16286. ;; noalign: if there were two spaces at the end, this field
  16287. ;; does not determine the width of the column.
  16288. (if noalign (setq org-table-may-need-update c)))
  16289. (backward-delete-char N)
  16290. (org-fix-tags-on-the-fly)))
  16291. (defun org-delete-char (N)
  16292. "Like `delete-char', but insert whitespace at field end in tables.
  16293. When deleting characters, in tables this function will insert whitespace in
  16294. front of the next \"|\" separator, to keep the table aligned. The table will
  16295. still be marked for re-alignment if the field did fill the entire column,
  16296. because, in this case the deletion might narrow the column."
  16297. (interactive "p")
  16298. (org-check-before-invisible-edit 'delete)
  16299. (if (and (org-table-p)
  16300. (not (bolp))
  16301. (not (= (char-after) ?|))
  16302. (eq N 1))
  16303. (if (looking-at ".*?|")
  16304. (let ((pos (point))
  16305. (noalign (looking-at "[^|\n\r]* |"))
  16306. (c org-table-may-need-update))
  16307. (replace-match (concat
  16308. (substring (match-string 0) 1 -1)
  16309. " |"))
  16310. (goto-char pos)
  16311. ;; noalign: if there were two spaces at the end, this field
  16312. ;; does not determine the width of the column.
  16313. (if noalign (setq org-table-may-need-update c)))
  16314. (delete-char N))
  16315. (delete-char N)
  16316. (org-fix-tags-on-the-fly)))
  16317. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  16318. (put 'org-self-insert-command 'delete-selection t)
  16319. (put 'orgtbl-self-insert-command 'delete-selection t)
  16320. (put 'org-delete-char 'delete-selection 'supersede)
  16321. (put 'org-delete-backward-char 'delete-selection 'supersede)
  16322. (put 'org-yank 'delete-selection 'yank)
  16323. ;; Make `flyspell-mode' delay after some commands
  16324. (put 'org-self-insert-command 'flyspell-delayed t)
  16325. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  16326. (put 'org-delete-char 'flyspell-delayed t)
  16327. (put 'org-delete-backward-char 'flyspell-delayed t)
  16328. ;; Make pabbrev-mode expand after org-mode commands
  16329. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  16330. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  16331. ;; How to do this: Measure non-white length of current string
  16332. ;; If equal to column width, we should realign.
  16333. (defun org-remap (map &rest commands)
  16334. "In MAP, remap the functions given in COMMANDS.
  16335. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  16336. (let (new old)
  16337. (while commands
  16338. (setq old (pop commands) new (pop commands))
  16339. (if (fboundp 'command-remapping)
  16340. (org-defkey map (vector 'remap old) new)
  16341. (substitute-key-definition old new map global-map)))))
  16342. (when (eq org-enable-table-editor 'optimized)
  16343. ;; If the user wants maximum table support, we need to hijack
  16344. ;; some standard editing functions
  16345. (org-remap org-mode-map
  16346. 'self-insert-command 'org-self-insert-command
  16347. 'delete-char 'org-delete-char
  16348. 'delete-backward-char 'org-delete-backward-char)
  16349. (org-defkey org-mode-map "|" 'org-force-self-insert))
  16350. (defvar org-ctrl-c-ctrl-c-hook nil
  16351. "Hook for functions attaching themselves to `C-c C-c'.
  16352. This can be used to add additional functionality to the C-c C-c
  16353. key which executes context-dependent commands. This hook is run
  16354. before any other test, while `org-ctrl-c-ctrl-c-final-hook' is
  16355. run after the last test.
  16356. Each function will be called with no arguments. The function
  16357. must check if the context is appropriate for it to act. If yes,
  16358. it should do its thing and then return a non-nil value. If the
  16359. context is wrong, just do nothing and return nil.")
  16360. (defvar org-ctrl-c-ctrl-c-final-hook nil
  16361. "Hook for functions attaching themselves to `C-c C-c'.
  16362. This can be used to add additional functionality to the C-c C-c
  16363. key which executes context-dependent commands. This hook is run
  16364. after any other test, while `org-ctrl-c-ctrl-c-hook' is run
  16365. before the first test.
  16366. Each function will be called with no arguments. The function
  16367. must check if the context is appropriate for it to act. If yes,
  16368. it should do its thing and then return a non-nil value. If the
  16369. context is wrong, just do nothing and return nil.")
  16370. (defvar org-tab-first-hook nil
  16371. "Hook for functions to attach themselves to TAB.
  16372. See `org-ctrl-c-ctrl-c-hook' for more information.
  16373. This hook runs as the first action when TAB is pressed, even before
  16374. `org-cycle' messes around with the `outline-regexp' to cater for
  16375. inline tasks and plain list item folding.
  16376. If any function in this hook returns t, any other actions that
  16377. would have been caused by TAB (such as table field motion or visibility
  16378. cycling) will not occur.")
  16379. (defvar org-tab-after-check-for-table-hook nil
  16380. "Hook for functions to attach themselves to TAB.
  16381. See `org-ctrl-c-ctrl-c-hook' for more information.
  16382. This hook runs after it has been established that the cursor is not in a
  16383. table, but before checking if the cursor is in a headline or if global cycling
  16384. should be done.
  16385. If any function in this hook returns t, not other actions like visibility
  16386. cycling will be done.")
  16387. (defvar org-tab-after-check-for-cycling-hook nil
  16388. "Hook for functions to attach themselves to TAB.
  16389. See `org-ctrl-c-ctrl-c-hook' for more information.
  16390. This hook runs after it has been established that not table field motion and
  16391. not visibility should be done because of current context. This is probably
  16392. the place where a package like yasnippets can hook in.")
  16393. (defvar org-tab-before-tab-emulation-hook nil
  16394. "Hook for functions to attach themselves to TAB.
  16395. See `org-ctrl-c-ctrl-c-hook' for more information.
  16396. This hook runs after every other options for TAB have been exhausted, but
  16397. before indentation and \t insertion takes place.")
  16398. (defvar org-metaleft-hook nil
  16399. "Hook for functions attaching themselves to `M-left'.
  16400. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16401. (defvar org-metaright-hook nil
  16402. "Hook for functions attaching themselves to `M-right'.
  16403. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16404. (defvar org-metaup-hook nil
  16405. "Hook for functions attaching themselves to `M-up'.
  16406. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16407. (defvar org-metadown-hook nil
  16408. "Hook for functions attaching themselves to `M-down'.
  16409. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16410. (defvar org-shiftmetaleft-hook nil
  16411. "Hook for functions attaching themselves to `M-S-left'.
  16412. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16413. (defvar org-shiftmetaright-hook nil
  16414. "Hook for functions attaching themselves to `M-S-right'.
  16415. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16416. (defvar org-shiftmetaup-hook nil
  16417. "Hook for functions attaching themselves to `M-S-up'.
  16418. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16419. (defvar org-shiftmetadown-hook nil
  16420. "Hook for functions attaching themselves to `M-S-down'.
  16421. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16422. (defvar org-metareturn-hook nil
  16423. "Hook for functions attaching themselves to `M-RET'.
  16424. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16425. (defvar org-shiftup-hook nil
  16426. "Hook for functions attaching themselves to `S-up'.
  16427. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16428. (defvar org-shiftup-final-hook nil
  16429. "Hook for functions attaching themselves to `S-up'.
  16430. This one runs after all other options except shift-select have been excluded.
  16431. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16432. (defvar org-shiftdown-hook nil
  16433. "Hook for functions attaching themselves to `S-down'.
  16434. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16435. (defvar org-shiftdown-final-hook nil
  16436. "Hook for functions attaching themselves to `S-down'.
  16437. This one runs after all other options except shift-select have been excluded.
  16438. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16439. (defvar org-shiftleft-hook nil
  16440. "Hook for functions attaching themselves to `S-left'.
  16441. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16442. (defvar org-shiftleft-final-hook nil
  16443. "Hook for functions attaching themselves to `S-left'.
  16444. This one runs after all other options except shift-select have been excluded.
  16445. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16446. (defvar org-shiftright-hook nil
  16447. "Hook for functions attaching themselves to `S-right'.
  16448. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16449. (defvar org-shiftright-final-hook nil
  16450. "Hook for functions attaching themselves to `S-right'.
  16451. This one runs after all other options except shift-select have been excluded.
  16452. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16453. (defun org-modifier-cursor-error ()
  16454. "Throw an error, a modified cursor command was applied in wrong context."
  16455. (error "This command is active in special context like tables, headlines or items"))
  16456. (defun org-shiftselect-error ()
  16457. "Throw an error because Shift-Cursor command was applied in wrong context."
  16458. (if (and (boundp 'shift-select-mode) shift-select-mode)
  16459. (error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
  16460. (error "This command works only in special context like headlines or timestamps")))
  16461. (defun org-call-for-shift-select (cmd)
  16462. (let ((this-command-keys-shift-translated t))
  16463. (call-interactively cmd)))
  16464. (defun org-shifttab (&optional arg)
  16465. "Global visibility cycling or move to previous table field.
  16466. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  16467. on context.
  16468. See the individual commands for more information."
  16469. (interactive "P")
  16470. (cond
  16471. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  16472. ((integerp arg)
  16473. (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
  16474. (message "Content view to level: %d" arg)
  16475. (org-content (prefix-numeric-value arg2))
  16476. (setq org-cycle-global-status 'overview)))
  16477. (t (call-interactively 'org-global-cycle))))
  16478. (defun org-shiftmetaleft ()
  16479. "Promote subtree or delete table column.
  16480. Calls `org-promote-subtree', `org-outdent-item',
  16481. or `org-table-delete-column', depending on context.
  16482. See the individual commands for more information."
  16483. (interactive)
  16484. (cond
  16485. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  16486. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  16487. ((org-at-heading-p) (call-interactively 'org-promote-subtree))
  16488. ((org-at-item-p) (call-interactively 'org-outdent-item-tree))
  16489. (t (org-modifier-cursor-error))))
  16490. (defun org-shiftmetaright ()
  16491. "Demote subtree or insert table column.
  16492. Calls `org-demote-subtree', `org-indent-item',
  16493. or `org-table-insert-column', depending on context.
  16494. See the individual commands for more information."
  16495. (interactive)
  16496. (cond
  16497. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  16498. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  16499. ((org-at-heading-p) (call-interactively 'org-demote-subtree))
  16500. ((org-at-item-p) (call-interactively 'org-indent-item-tree))
  16501. (t (org-modifier-cursor-error))))
  16502. (defun org-shiftmetaup (&optional arg)
  16503. "Move subtree up or kill table row.
  16504. Calls `org-move-subtree-up' or `org-table-kill-row' or
  16505. `org-move-item-up' depending on context. See the individual commands
  16506. for more information."
  16507. (interactive "P")
  16508. (cond
  16509. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  16510. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  16511. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  16512. ((org-at-item-p) (call-interactively 'org-move-item-up))
  16513. (t (org-modifier-cursor-error))))
  16514. (defun org-shiftmetadown (&optional arg)
  16515. "Move subtree down or insert table row.
  16516. Calls `org-move-subtree-down' or `org-table-insert-row' or
  16517. `org-move-item-down', depending on context. See the individual
  16518. commands for more information."
  16519. (interactive "P")
  16520. (cond
  16521. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  16522. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  16523. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  16524. ((org-at-item-p) (call-interactively 'org-move-item-down))
  16525. (t (org-modifier-cursor-error))))
  16526. (defsubst org-hidden-tree-error ()
  16527. (error
  16528. "Hidden subtree, open with TAB or use subtree command M-S-<left>/<right>"))
  16529. (defun org-metaleft (&optional arg)
  16530. "Promote heading or move table column to left.
  16531. Calls `org-do-promote' or `org-table-move-column', depending on context.
  16532. With no specific context, calls the Emacs default `backward-word'.
  16533. See the individual commands for more information."
  16534. (interactive "P")
  16535. (cond
  16536. ((run-hook-with-args-until-success 'org-metaleft-hook))
  16537. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  16538. ((org-with-limited-levels
  16539. (or (org-at-heading-p)
  16540. (and (org-region-active-p)
  16541. (save-excursion
  16542. (goto-char (region-beginning))
  16543. (org-at-heading-p)))))
  16544. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  16545. (call-interactively 'org-do-promote))
  16546. ;; At an inline task.
  16547. ((org-at-heading-p)
  16548. (call-interactively 'org-inlinetask-promote))
  16549. ((or (org-at-item-p)
  16550. (and (org-region-active-p)
  16551. (save-excursion
  16552. (goto-char (region-beginning))
  16553. (org-at-item-p))))
  16554. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  16555. (call-interactively 'org-outdent-item))
  16556. (t (call-interactively 'backward-word))))
  16557. (defun org-metaright (&optional arg)
  16558. "Demote a subtree, a list item or move table column to right.
  16559. In front of a drawer or a block keyword, indent it correctly.
  16560. With no specific context, calls the Emacs default `forward-word'.
  16561. See the individual commands for more information."
  16562. (interactive "P")
  16563. (cond
  16564. ((run-hook-with-args-until-success 'org-metaright-hook))
  16565. ((org-at-table-p) (call-interactively 'org-table-move-column))
  16566. ((org-at-drawer-p) (call-interactively 'org-indent-drawer))
  16567. ((org-at-block-p) (call-interactively 'org-indent-block))
  16568. ((org-with-limited-levels
  16569. (or (org-at-heading-p)
  16570. (and (org-region-active-p)
  16571. (save-excursion
  16572. (goto-char (region-beginning))
  16573. (org-at-heading-p)))))
  16574. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  16575. (call-interactively 'org-do-demote))
  16576. ;; At an inline task.
  16577. ((org-at-heading-p)
  16578. (call-interactively 'org-inlinetask-demote))
  16579. ((or (org-at-item-p)
  16580. (and (org-region-active-p)
  16581. (save-excursion
  16582. (goto-char (region-beginning))
  16583. (org-at-item-p))))
  16584. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  16585. (call-interactively 'org-indent-item))
  16586. (t (call-interactively 'forward-word))))
  16587. (defun org-check-for-hidden (what)
  16588. "Check if there are hidden headlines/items in the current visual line.
  16589. WHAT can be either `headlines' or `items'. If the current line is
  16590. an outline or item heading and it has a folded subtree below it,
  16591. this function returns t, nil otherwise."
  16592. (let ((re (cond
  16593. ((eq what 'headlines) org-outline-regexp-bol)
  16594. ((eq what 'items) (org-item-beginning-re))
  16595. (t (error "This should not happen"))))
  16596. beg end)
  16597. (save-excursion
  16598. (catch 'exit
  16599. (unless (org-region-active-p)
  16600. (setq beg (point-at-bol))
  16601. (beginning-of-line 2)
  16602. (while (and (not (eobp)) ;; this is like `next-line'
  16603. (get-char-property (1- (point)) 'invisible))
  16604. (beginning-of-line 2))
  16605. (setq end (point))
  16606. (goto-char beg)
  16607. (goto-char (point-at-eol))
  16608. (setq end (max end (point)))
  16609. (while (re-search-forward re end t)
  16610. (if (get-char-property (match-beginning 0) 'invisible)
  16611. (throw 'exit t))))
  16612. nil))))
  16613. (defun org-metaup (&optional arg)
  16614. "Move subtree up or move table row up.
  16615. Calls `org-move-subtree-up' or `org-table-move-row' or
  16616. `org-move-item-up', depending on context. See the individual commands
  16617. for more information."
  16618. (interactive "P")
  16619. (cond
  16620. ((run-hook-with-args-until-success 'org-metaup-hook))
  16621. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  16622. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  16623. ((org-at-item-p) (call-interactively 'org-move-item-up))
  16624. (t (transpose-lines 1) (beginning-of-line -1))))
  16625. (defun org-metadown (&optional arg)
  16626. "Move subtree down or move table row down.
  16627. Calls `org-move-subtree-down' or `org-table-move-row' or
  16628. `org-move-item-down', depending on context. See the individual
  16629. commands for more information."
  16630. (interactive "P")
  16631. (cond
  16632. ((run-hook-with-args-until-success 'org-metadown-hook))
  16633. ((org-at-table-p) (call-interactively 'org-table-move-row))
  16634. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  16635. ((org-at-item-p) (call-interactively 'org-move-item-down))
  16636. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  16637. (defun org-shiftup (&optional arg)
  16638. "Increase item in timestamp or increase priority of current headline.
  16639. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  16640. depending on context. See the individual commands for more information."
  16641. (interactive "P")
  16642. (cond
  16643. ((run-hook-with-args-until-success 'org-shiftup-hook))
  16644. ((and org-support-shift-select (org-region-active-p))
  16645. (org-call-for-shift-select 'previous-line))
  16646. ((org-at-timestamp-p t)
  16647. (call-interactively (if org-edit-timestamp-down-means-later
  16648. 'org-timestamp-down 'org-timestamp-up)))
  16649. ((and (not (eq org-support-shift-select 'always))
  16650. org-enable-priority-commands
  16651. (org-at-heading-p))
  16652. (call-interactively 'org-priority-up))
  16653. ((and (not org-support-shift-select) (org-at-item-p))
  16654. (call-interactively 'org-previous-item))
  16655. ((org-clocktable-try-shift 'up arg))
  16656. ((run-hook-with-args-until-success 'org-shiftup-final-hook))
  16657. (org-support-shift-select
  16658. (org-call-for-shift-select 'previous-line))
  16659. (t (org-shiftselect-error))))
  16660. (defun org-shiftdown (&optional arg)
  16661. "Decrease item in timestamp or decrease priority of current headline.
  16662. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  16663. depending on context. See the individual commands for more information."
  16664. (interactive "P")
  16665. (cond
  16666. ((run-hook-with-args-until-success 'org-shiftdown-hook))
  16667. ((and org-support-shift-select (org-region-active-p))
  16668. (org-call-for-shift-select 'next-line))
  16669. ((org-at-timestamp-p t)
  16670. (call-interactively (if org-edit-timestamp-down-means-later
  16671. 'org-timestamp-up 'org-timestamp-down)))
  16672. ((and (not (eq org-support-shift-select 'always))
  16673. org-enable-priority-commands
  16674. (org-at-heading-p))
  16675. (call-interactively 'org-priority-down))
  16676. ((and (not org-support-shift-select) (org-at-item-p))
  16677. (call-interactively 'org-next-item))
  16678. ((org-clocktable-try-shift 'down arg))
  16679. ((run-hook-with-args-until-success 'org-shiftdown-final-hook))
  16680. (org-support-shift-select
  16681. (org-call-for-shift-select 'next-line))
  16682. (t (org-shiftselect-error))))
  16683. (defun org-shiftright (&optional arg)
  16684. "Cycle the thing at point or in the current line, depending on context.
  16685. Depending on context, this does one of the following:
  16686. - switch a timestamp at point one day into the future
  16687. - on a headline, switch to the next TODO keyword.
  16688. - on an item, switch entire list to the next bullet type
  16689. - on a property line, switch to the next allowed value
  16690. - on a clocktable definition line, move time block into the future"
  16691. (interactive "P")
  16692. (cond
  16693. ((run-hook-with-args-until-success 'org-shiftright-hook))
  16694. ((and org-support-shift-select (org-region-active-p))
  16695. (org-call-for-shift-select 'forward-char))
  16696. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  16697. ((and (not (eq org-support-shift-select 'always))
  16698. (org-at-heading-p))
  16699. (let ((org-inhibit-logging
  16700. (not org-treat-S-cursor-todo-selection-as-state-change))
  16701. (org-inhibit-blocking
  16702. (not org-treat-S-cursor-todo-selection-as-state-change)))
  16703. (org-call-with-arg 'org-todo 'right)))
  16704. ((or (and org-support-shift-select
  16705. (not (eq org-support-shift-select 'always))
  16706. (org-at-item-bullet-p))
  16707. (and (not org-support-shift-select) (org-at-item-p)))
  16708. (org-call-with-arg 'org-cycle-list-bullet nil))
  16709. ((and (not (eq org-support-shift-select 'always))
  16710. (org-at-property-p))
  16711. (call-interactively 'org-property-next-allowed-value))
  16712. ((org-clocktable-try-shift 'right arg))
  16713. ((run-hook-with-args-until-success 'org-shiftright-final-hook))
  16714. (org-support-shift-select
  16715. (org-call-for-shift-select 'forward-char))
  16716. (t (org-shiftselect-error))))
  16717. (defun org-shiftleft (&optional arg)
  16718. "Cycle the thing at point or in the current line, depending on context.
  16719. Depending on context, this does one of the following:
  16720. - switch a timestamp at point one day into the past
  16721. - on a headline, switch to the previous TODO keyword.
  16722. - on an item, switch entire list to the previous bullet type
  16723. - on a property line, switch to the previous allowed value
  16724. - on a clocktable definition line, move time block into the past"
  16725. (interactive "P")
  16726. (cond
  16727. ((run-hook-with-args-until-success 'org-shiftleft-hook))
  16728. ((and org-support-shift-select (org-region-active-p))
  16729. (org-call-for-shift-select 'backward-char))
  16730. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  16731. ((and (not (eq org-support-shift-select 'always))
  16732. (org-at-heading-p))
  16733. (let ((org-inhibit-logging
  16734. (not org-treat-S-cursor-todo-selection-as-state-change))
  16735. (org-inhibit-blocking
  16736. (not org-treat-S-cursor-todo-selection-as-state-change)))
  16737. (org-call-with-arg 'org-todo 'left)))
  16738. ((or (and org-support-shift-select
  16739. (not (eq org-support-shift-select 'always))
  16740. (org-at-item-bullet-p))
  16741. (and (not org-support-shift-select) (org-at-item-p)))
  16742. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  16743. ((and (not (eq org-support-shift-select 'always))
  16744. (org-at-property-p))
  16745. (call-interactively 'org-property-previous-allowed-value))
  16746. ((org-clocktable-try-shift 'left arg))
  16747. ((run-hook-with-args-until-success 'org-shiftleft-final-hook))
  16748. (org-support-shift-select
  16749. (org-call-for-shift-select 'backward-char))
  16750. (t (org-shiftselect-error))))
  16751. (defun org-shiftcontrolright ()
  16752. "Switch to next TODO set."
  16753. (interactive)
  16754. (cond
  16755. ((and org-support-shift-select (org-region-active-p))
  16756. (org-call-for-shift-select 'forward-word))
  16757. ((and (not (eq org-support-shift-select 'always))
  16758. (org-at-heading-p))
  16759. (org-call-with-arg 'org-todo 'nextset))
  16760. (org-support-shift-select
  16761. (org-call-for-shift-select 'forward-word))
  16762. (t (org-shiftselect-error))))
  16763. (defun org-shiftcontrolleft ()
  16764. "Switch to previous TODO set."
  16765. (interactive)
  16766. (cond
  16767. ((and org-support-shift-select (org-region-active-p))
  16768. (org-call-for-shift-select 'backward-word))
  16769. ((and (not (eq org-support-shift-select 'always))
  16770. (org-at-heading-p))
  16771. (org-call-with-arg 'org-todo 'previousset))
  16772. (org-support-shift-select
  16773. (org-call-for-shift-select 'backward-word))
  16774. (t (org-shiftselect-error))))
  16775. (defun org-shiftcontrolup ()
  16776. "Change timestamps synchronously up in CLOCK log lines."
  16777. (interactive)
  16778. (cond ((and (not org-support-shift-select)
  16779. (org-at-clock-log-p)
  16780. (org-at-timestamp-p t))
  16781. (org-clock-timestamps-up))
  16782. (t (org-shiftselect-error))))
  16783. (defun org-shiftcontroldown ()
  16784. "Change timestamps synchronously down in CLOCK log lines."
  16785. (interactive)
  16786. (cond ((and (not org-support-shift-select)
  16787. (org-at-clock-log-p)
  16788. (org-at-timestamp-p t))
  16789. (org-clock-timestamps-down))
  16790. (t (org-shiftselect-error))))
  16791. (defun org-ctrl-c-ret ()
  16792. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  16793. (interactive)
  16794. (cond
  16795. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  16796. (t (call-interactively 'org-insert-heading))))
  16797. (defun org-find-visible ()
  16798. (let ((s (point)))
  16799. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  16800. (get-char-property s 'invisible)))
  16801. s))
  16802. (defun org-find-invisible ()
  16803. (let ((s (point)))
  16804. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  16805. (not (get-char-property s 'invisible))))
  16806. s))
  16807. (defun org-copy-visible (beg end)
  16808. "Copy the visible parts of the region."
  16809. (interactive "r")
  16810. (let (snippets s)
  16811. (save-excursion
  16812. (save-restriction
  16813. (narrow-to-region beg end)
  16814. (setq s (goto-char (point-min)))
  16815. (while (not (= (point) (point-max)))
  16816. (goto-char (org-find-invisible))
  16817. (push (buffer-substring s (point)) snippets)
  16818. (setq s (goto-char (org-find-visible))))))
  16819. (kill-new (apply 'concat (nreverse snippets)))))
  16820. (defun org-copy-special ()
  16821. "Copy region in table or copy current subtree.
  16822. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  16823. See the individual commands for more information."
  16824. (interactive)
  16825. (call-interactively
  16826. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  16827. (defun org-cut-special ()
  16828. "Cut region in table or cut current subtree.
  16829. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  16830. See the individual commands for more information."
  16831. (interactive)
  16832. (call-interactively
  16833. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  16834. (defun org-paste-special (arg)
  16835. "Paste rectangular region into table, or past subtree relative to level.
  16836. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  16837. See the individual commands for more information."
  16838. (interactive "P")
  16839. (if (org-at-table-p)
  16840. (org-table-paste-rectangle)
  16841. (org-paste-subtree arg)))
  16842. (defun org-edit-special (&optional arg)
  16843. "Call a special editor for the stuff at point.
  16844. When at a table, call the formula editor with `org-table-edit-formulas'.
  16845. When at the first line of an src example, call `org-edit-src-code'.
  16846. When in an #+include line, visit the include file. Otherwise call
  16847. `ffap' to visit the file at point."
  16848. (interactive)
  16849. ;; possibly prep session before editing source
  16850. (when arg
  16851. (let* ((info (org-babel-get-src-block-info))
  16852. (lang (nth 0 info))
  16853. (params (nth 2 info))
  16854. (session (cdr (assoc :session params))))
  16855. (when (and info session) ;; we are in a source-code block with a session
  16856. (funcall
  16857. (intern (concat "org-babel-prep-session:" lang)) session params))))
  16858. (cond ;; proceed with `org-edit-special'
  16859. ((save-excursion
  16860. (beginning-of-line 1)
  16861. (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
  16862. (find-file (org-trim (match-string 1))))
  16863. ((org-edit-src-code))
  16864. ((org-edit-fixed-width-region))
  16865. ((org-at-table.el-p)
  16866. (org-edit-src-code))
  16867. ((or (org-at-table-p)
  16868. (save-excursion
  16869. (beginning-of-line 1)
  16870. (looking-at "[ \t]*#\\+TBLFM:")))
  16871. (call-interactively 'org-table-edit-formulas))
  16872. (t (call-interactively 'ffap))))
  16873. (defvar org-table-coordinate-overlays) ; defined in org-table.el
  16874. (defun org-ctrl-c-ctrl-c (&optional arg)
  16875. "Set tags in headline, or update according to changed information at point.
  16876. This command does many different things, depending on context:
  16877. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  16878. this is what we do.
  16879. - If the cursor is on a statistics cookie, update it.
  16880. - If the cursor is in a headline, prompt for tags and insert them
  16881. into the current line, aligned to `org-tags-column'. When called
  16882. with prefix arg, realign all tags in the current buffer.
  16883. - If the cursor is in one of the special #+KEYWORD lines, this
  16884. triggers scanning the buffer for these lines and updating the
  16885. information.
  16886. - If the cursor is inside a table, realign the table. This command
  16887. works even if the automatic table editor has been turned off.
  16888. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  16889. the entire table.
  16890. - If the cursor is at a footnote reference or definition, jump to
  16891. the corresponding definition or references, respectively.
  16892. - If the cursor is a the beginning of a dynamic block, update it.
  16893. - If the current buffer is a capture buffer, close note and file it.
  16894. - If the cursor is on a <<<target>>>, update radio targets and
  16895. corresponding links in this buffer.
  16896. - If the cursor is on a numbered item in a plain list, renumber the
  16897. ordered list.
  16898. - If the cursor is on a checkbox, toggle it.
  16899. - If the cursor is on a code block, evaluate it. The variable
  16900. `org-confirm-babel-evaluate' can be used to control prompting
  16901. before code block evaluation, by default every code block
  16902. evaluation requires confirmation. Code block evaluation can be
  16903. inhibited by setting `org-babel-no-eval-on-ctrl-c-ctrl-c'."
  16904. (interactive "P")
  16905. (let ((org-enable-table-editor t))
  16906. (cond
  16907. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  16908. org-occur-highlights
  16909. org-latex-fragment-image-overlays)
  16910. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  16911. (org-remove-occur-highlights)
  16912. (org-remove-latex-fragment-image-overlays)
  16913. (message "Temporary highlights/overlays removed from current buffer"))
  16914. ((and (local-variable-p 'org-finish-function (current-buffer))
  16915. (fboundp org-finish-function))
  16916. (funcall org-finish-function))
  16917. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  16918. ((org-in-regexp org-ts-regexp-both)
  16919. (org-timestamp-change 0 'day))
  16920. ((or (looking-at org-property-start-re)
  16921. (org-at-property-p))
  16922. (call-interactively 'org-property-action))
  16923. ((org-at-target-p) (call-interactively 'org-update-radio-target-regexp))
  16924. ((and (org-in-regexp "\\[\\([0-9]*%\\|[0-9]*/[0-9]*\\)\\]")
  16925. (or (org-at-heading-p) (org-at-item-p)))
  16926. (call-interactively 'org-update-statistics-cookies))
  16927. ((org-at-heading-p) (call-interactively 'org-set-tags))
  16928. ((org-at-table.el-p)
  16929. (message "Use C-c ' to edit table.el tables"))
  16930. ((org-at-table-p)
  16931. (org-table-maybe-eval-formula)
  16932. (if arg
  16933. (call-interactively 'org-table-recalculate)
  16934. (org-table-maybe-recalculate-line))
  16935. (call-interactively 'org-table-align)
  16936. (orgtbl-send-table 'maybe))
  16937. ((or (org-footnote-at-reference-p)
  16938. (org-footnote-at-definition-p))
  16939. (call-interactively 'org-footnote-action))
  16940. ((org-at-item-checkbox-p)
  16941. ;; Cursor at a checkbox: repair list and update checkboxes. Send
  16942. ;; list only if at top item.
  16943. (let* ((cbox (match-string 1))
  16944. (struct (org-list-struct))
  16945. (old-struct (copy-tree struct))
  16946. (parents (org-list-parents-alist struct))
  16947. (orderedp (org-entry-get nil "ORDERED"))
  16948. (firstp (= (org-list-get-top-point struct) (point-at-bol)))
  16949. block-item)
  16950. ;; Use a light version of `org-toggle-checkbox' to avoid
  16951. ;; computing list structure twice.
  16952. (let ((new-box (cond
  16953. ((equal arg '(16)) "[-]")
  16954. ((equal arg '(4)) nil)
  16955. ((equal "[X]" cbox) "[ ]")
  16956. (t "[X]"))))
  16957. (if (and firstp arg)
  16958. ;; If at first item of sub-list, remove check-box from
  16959. ;; every item at the same level.
  16960. (mapc
  16961. (lambda (pos) (org-list-set-checkbox pos struct new-box))
  16962. (org-list-get-all-items
  16963. (point-at-bol) struct (org-list-prevs-alist struct)))
  16964. (org-list-set-checkbox (point-at-bol) struct new-box)))
  16965. ;; Replicate `org-list-write-struct', while grabbing a return
  16966. ;; value from `org-list-struct-fix-box'.
  16967. (org-list-struct-fix-ind struct parents 2)
  16968. (org-list-struct-fix-item-end struct)
  16969. (let ((prevs (org-list-prevs-alist struct)))
  16970. (org-list-struct-fix-bul struct prevs)
  16971. (org-list-struct-fix-ind struct parents)
  16972. (setq block-item
  16973. (org-list-struct-fix-box struct parents prevs orderedp)))
  16974. (org-list-struct-apply-struct struct old-struct)
  16975. (org-update-checkbox-count-maybe)
  16976. (when block-item
  16977. (message
  16978. "Checkboxes were removed due to unchecked box at line %d"
  16979. (org-current-line block-item)))
  16980. (when firstp (org-list-send-list 'maybe))))
  16981. ((org-at-item-p)
  16982. ;; Cursor at an item: repair list. Do checkbox related actions
  16983. ;; only if function was called with an argument. Send list only
  16984. ;; if at top item.
  16985. (let* ((struct (org-list-struct))
  16986. (firstp (= (org-list-get-top-point struct) (point-at-bol)))
  16987. old-struct)
  16988. (when arg
  16989. (setq old-struct (copy-tree struct))
  16990. (if firstp
  16991. ;; If at first item of sub-list, add check-box to every
  16992. ;; item at the same level.
  16993. (mapc
  16994. (lambda (pos)
  16995. (unless (org-list-get-checkbox pos struct)
  16996. (org-list-set-checkbox pos struct "[ ]")))
  16997. (org-list-get-all-items
  16998. (point-at-bol) struct (org-list-prevs-alist struct)))
  16999. (org-list-set-checkbox (point-at-bol) struct "[ ]")))
  17000. (org-list-write-struct
  17001. struct (org-list-parents-alist struct) old-struct)
  17002. (when arg (org-update-checkbox-count-maybe))
  17003. (when firstp (org-list-send-list 'maybe))))
  17004. ((save-excursion (beginning-of-line 1) (looking-at org-dblock-start-re))
  17005. ;; Dynamic block
  17006. (beginning-of-line 1)
  17007. (save-excursion (org-update-dblock)))
  17008. ((save-excursion
  17009. (beginning-of-line 1)
  17010. (looking-at "[ \t]*#\\+\\([A-Z]+\\)"))
  17011. (cond
  17012. ((equal (match-string 1) "TBLFM")
  17013. ;; Recalculate the table before this line
  17014. (save-excursion
  17015. (beginning-of-line 1)
  17016. (skip-chars-backward " \r\n\t")
  17017. (if (org-at-table-p)
  17018. (org-call-with-arg 'org-table-recalculate (or arg t)))))
  17019. (t
  17020. (let ((org-inhibit-startup-visibility-stuff t)
  17021. (org-startup-align-all-tables nil))
  17022. (when (boundp 'org-table-coordinate-overlays)
  17023. (mapc 'delete-overlay org-table-coordinate-overlays)
  17024. (setq org-table-coordinate-overlays nil))
  17025. (org-save-outline-visibility 'use-markers (org-mode-restart)))
  17026. (message "Local setup has been refreshed"))))
  17027. ((org-clock-update-time-maybe))
  17028. (t
  17029. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  17030. (error "C-c C-c can do nothing useful at this location"))))))
  17031. (defun org-mode-restart ()
  17032. "Restart Org-mode, to scan again for special lines.
  17033. Also updates the keyword regular expressions."
  17034. (interactive)
  17035. (org-mode)
  17036. (message "Org-mode restarted"))
  17037. (defun org-kill-note-or-show-branches ()
  17038. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  17039. (interactive)
  17040. (if (not org-finish-function)
  17041. (progn
  17042. (hide-subtree)
  17043. (call-interactively 'show-branches))
  17044. (let ((org-note-abort t))
  17045. (funcall org-finish-function))))
  17046. (defun org-return (&optional indent)
  17047. "Goto next table row or insert a newline.
  17048. Calls `org-table-next-row' or `newline', depending on context.
  17049. See the individual commands for more information."
  17050. (interactive)
  17051. (cond
  17052. ((or (bobp) (org-in-src-block-p))
  17053. (if indent (newline-and-indent) (newline)))
  17054. ((org-at-table-p)
  17055. (org-table-justify-field-maybe)
  17056. (call-interactively 'org-table-next-row))
  17057. ;; when `newline-and-indent' is called within a list, make sure
  17058. ;; text moved stays inside the item.
  17059. ((and (org-in-item-p) indent)
  17060. (if (and (org-at-item-p) (>= (point) (match-end 0)))
  17061. (progn
  17062. (save-match-data (newline))
  17063. (org-indent-line-to (length (match-string 0))))
  17064. (let ((ind (org-get-indentation)))
  17065. (newline)
  17066. (if (org-looking-back org-list-end-re)
  17067. (org-indent-line-function)
  17068. (org-indent-line-to ind)))))
  17069. ((and org-return-follows-link
  17070. (let ((tprop (get-text-property (point) 'face)))
  17071. (or (eq tprop 'org-link)
  17072. (and (listp tprop) (memq 'org-link tprop)))))
  17073. (call-interactively 'org-open-at-point))
  17074. ((and (org-at-heading-p)
  17075. (looking-at
  17076. (org-re "\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$")))
  17077. (org-show-entry)
  17078. (end-of-line 1)
  17079. (newline))
  17080. (t (if indent (newline-and-indent) (newline)))))
  17081. (defun org-return-indent ()
  17082. "Goto next table row or insert a newline and indent.
  17083. Calls `org-table-next-row' or `newline-and-indent', depending on
  17084. context. See the individual commands for more information."
  17085. (interactive)
  17086. (org-return t))
  17087. (defun org-ctrl-c-star ()
  17088. "Compute table, or change heading status of lines.
  17089. Calls `org-table-recalculate' or `org-toggle-heading',
  17090. depending on context."
  17091. (interactive)
  17092. (cond
  17093. ((org-at-table-p)
  17094. (call-interactively 'org-table-recalculate))
  17095. (t
  17096. ;; Convert all lines in region to list items
  17097. (call-interactively 'org-toggle-heading))))
  17098. (defun org-ctrl-c-minus ()
  17099. "Insert separator line in table or modify bullet status of line.
  17100. Also turns a plain line or a region of lines into list items.
  17101. Calls `org-table-insert-hline', `org-toggle-item', or
  17102. `org-cycle-list-bullet', depending on context."
  17103. (interactive)
  17104. (cond
  17105. ((org-at-table-p)
  17106. (call-interactively 'org-table-insert-hline))
  17107. ((org-region-active-p)
  17108. (call-interactively 'org-toggle-item))
  17109. ((org-in-item-p)
  17110. (call-interactively 'org-cycle-list-bullet))
  17111. (t
  17112. (call-interactively 'org-toggle-item))))
  17113. (defun org-toggle-item (arg)
  17114. "Convert headings or normal lines to items, items to normal lines.
  17115. If there is no active region, only the current line is considered.
  17116. If the first non blank line in the region is an headline, convert
  17117. all headlines to items, shifting text accordingly.
  17118. If it is an item, convert all items to normal lines.
  17119. If it is normal text, change region into an item. With a prefix
  17120. argument ARG, change each line in region into an item."
  17121. (interactive "P")
  17122. (let ((shift-text
  17123. (function
  17124. ;; Shift text in current section to IND, from point to END.
  17125. ;; The function leaves point to END line.
  17126. (lambda (ind end)
  17127. (let ((min-i 1000) (end (copy-marker end)))
  17128. ;; First determine the minimum indentation (MIN-I) of
  17129. ;; the text.
  17130. (save-excursion
  17131. (catch 'exit
  17132. (while (< (point) end)
  17133. (let ((i (org-get-indentation)))
  17134. (cond
  17135. ;; Skip blank lines and inline tasks.
  17136. ((looking-at "^[ \t]*$"))
  17137. ((looking-at org-outline-regexp-bol))
  17138. ;; We can't find less than 0 indentation.
  17139. ((zerop i) (throw 'exit (setq min-i 0)))
  17140. ((< i min-i) (setq min-i i))))
  17141. (forward-line))))
  17142. ;; Then indent each line so that a line indented to
  17143. ;; MIN-I becomes indented to IND. Ignore blank lines
  17144. ;; and inline tasks in the process.
  17145. (let ((delta (- ind min-i)))
  17146. (while (< (point) end)
  17147. (unless (or (looking-at "^[ \t]*$")
  17148. (looking-at org-outline-regexp-bol))
  17149. (org-indent-line-to (+ (org-get-indentation) delta)))
  17150. (forward-line)))))))
  17151. (skip-blanks
  17152. (function
  17153. ;; Return beginning of first non-blank line, starting from
  17154. ;; line at POS.
  17155. (lambda (pos)
  17156. (save-excursion
  17157. (goto-char pos)
  17158. (skip-chars-forward " \r\t\n")
  17159. (point-at-bol)))))
  17160. beg end)
  17161. ;; Determine boundaries of changes.
  17162. (if (org-region-active-p)
  17163. (setq beg (funcall skip-blanks (region-beginning))
  17164. end (copy-marker (region-end)))
  17165. (setq beg (funcall skip-blanks (point-at-bol))
  17166. end (copy-marker (point-at-eol))))
  17167. ;; Depending on the starting line, choose an action on the text
  17168. ;; between BEG and END.
  17169. (org-with-limited-levels
  17170. (save-excursion
  17171. (goto-char beg)
  17172. (cond
  17173. ;; Case 1. Start at an item: de-itemize. Note that it only
  17174. ;; happens when a region is active: `org-ctrl-c-minus'
  17175. ;; would call `org-cycle-list-bullet' otherwise.
  17176. ((org-at-item-p)
  17177. (while (< (point) end)
  17178. (when (org-at-item-p)
  17179. (skip-chars-forward " \t")
  17180. (delete-region (point) (match-end 0)))
  17181. (forward-line)))
  17182. ;; Case 2. Start at an heading: convert to items.
  17183. ((org-at-heading-p)
  17184. (let* ((bul (org-list-bullet-string "-"))
  17185. (bul-len (length bul))
  17186. ;; Indentation of the first heading. It should be
  17187. ;; relative to the indentation of its parent, if any.
  17188. (start-ind (save-excursion
  17189. (cond
  17190. ((not org-adapt-indentation) 0)
  17191. ((not (outline-previous-heading)) 0)
  17192. (t (length (match-string 0))))))
  17193. ;; Level of first heading. Further headings will be
  17194. ;; compared to it to determine hierarchy in the list.
  17195. (ref-level (org-reduced-level (org-outline-level))))
  17196. (while (< (point) end)
  17197. (let* ((level (org-reduced-level (org-outline-level)))
  17198. (delta (max 0 (- level ref-level))))
  17199. ;; If current headline is less indented than the first
  17200. ;; one, set it as reference, in order to preserve
  17201. ;; subtrees.
  17202. (when (< level ref-level) (setq ref-level level))
  17203. (replace-match bul t t)
  17204. (org-indent-line-to (+ start-ind (* delta bul-len)))
  17205. ;; Ensure all text down to END (or SECTION-END) belongs
  17206. ;; to the newly created item.
  17207. (let ((section-end (save-excursion
  17208. (or (outline-next-heading) (point)))))
  17209. (forward-line)
  17210. (funcall shift-text
  17211. (+ start-ind (* (1+ delta) bul-len))
  17212. (min end section-end)))))))
  17213. ;; Case 3. Normal line with ARG: turn each non-item line into
  17214. ;; an item.
  17215. (arg
  17216. (while (< (point) end)
  17217. (unless (or (org-at-heading-p) (org-at-item-p))
  17218. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  17219. (replace-match
  17220. (concat "\\1" (org-list-bullet-string "-") "\\2"))))
  17221. (forward-line)))
  17222. ;; Case 4. Normal line without ARG: make the first line of
  17223. ;; region an item, and shift indentation of others
  17224. ;; lines to set them as item's body.
  17225. (t (let* ((bul (org-list-bullet-string "-"))
  17226. (bul-len (length bul))
  17227. (ref-ind (org-get-indentation)))
  17228. (skip-chars-forward " \t")
  17229. (insert bul)
  17230. (forward-line)
  17231. (while (< (point) end)
  17232. ;; Ensure that lines less indented than first one
  17233. ;; still get included in item body.
  17234. (funcall shift-text
  17235. (+ ref-ind bul-len)
  17236. (min end (save-excursion (or (outline-next-heading)
  17237. (point)))))
  17238. (forward-line)))))))))
  17239. (defun org-toggle-heading (&optional nstars)
  17240. "Convert headings to normal text, or items or text to headings.
  17241. If there is no active region, only the current line is considered.
  17242. If the first non blank line is an headline, remove the stars from
  17243. all headlines in the region.
  17244. If it is a plain list item, turn all plain list items into headings.
  17245. If it is a normal line, turn each and every normal line (i.e. not
  17246. an heading or an item) in the region into a heading.
  17247. When converting a line into a heading, the number of stars is chosen
  17248. such that the lines become children of the current entry. However,
  17249. when a prefix argument is given, its value determines the number of
  17250. stars to add."
  17251. (interactive "P")
  17252. (let ((skip-blanks
  17253. (function
  17254. ;; Return beginning of first non-blank line, starting from
  17255. ;; line at POS.
  17256. (lambda (pos)
  17257. (save-excursion
  17258. (goto-char pos)
  17259. (skip-chars-forward " \r\t\n")
  17260. (point-at-bol)))))
  17261. beg end)
  17262. ;; Determine boundaries of changes. If region ends at a bol, do
  17263. ;; not consider the last line to be in the region.
  17264. (if (org-region-active-p)
  17265. (setq beg (funcall skip-blanks (region-beginning))
  17266. end (copy-marker (save-excursion
  17267. (goto-char (region-end))
  17268. (if (bolp) (point) (point-at-eol)))))
  17269. (setq beg (funcall skip-blanks (point-at-bol))
  17270. end (copy-marker (point-at-eol))))
  17271. ;; Ensure inline tasks don't count as headings.
  17272. (org-with-limited-levels
  17273. (save-excursion
  17274. (goto-char beg)
  17275. (cond
  17276. ;; Case 1. Started at an heading: de-star headings.
  17277. ((org-at-heading-p)
  17278. (while (< (point) end)
  17279. (when (org-at-heading-p t)
  17280. (looking-at org-outline-regexp) (replace-match ""))
  17281. (forward-line)))
  17282. ;; Case 2. Started at an item: change items into headlines.
  17283. ;; One star will be added by `org-list-to-subtree'.
  17284. ((org-at-item-p)
  17285. (let* ((stars (make-string
  17286. (if nstars
  17287. ;; subtract the star that will be added again by
  17288. ;; `org-list-to-subtree'
  17289. (1- (prefix-numeric-value current-prefix-arg))
  17290. (or (org-current-level) 0))
  17291. ?*))
  17292. (add-stars
  17293. (cond (nstars "") ; stars from prefix only
  17294. ((equal stars "") "") ; before first heading
  17295. (org-odd-levels-only "*") ; inside heading, odd
  17296. (t "")))) ; inside heading, oddeven
  17297. (while (< (point) end)
  17298. (when (org-at-item-p)
  17299. ;; Pay attention to cases when region ends before list.
  17300. (let* ((struct (org-list-struct))
  17301. (list-end (min (org-list-get-bottom-point struct) (1+ end))))
  17302. (save-restriction
  17303. (narrow-to-region (point) list-end)
  17304. (insert
  17305. (org-list-to-subtree
  17306. (org-list-parse-list t)
  17307. '(:istart (concat stars add-stars (funcall get-stars depth))
  17308. :icount (concat stars add-stars (funcall get-stars depth))))))))
  17309. (forward-line))))
  17310. ;; Case 3. Started at normal text: make every line an heading,
  17311. ;; skipping headlines and items.
  17312. (t (let* ((stars (make-string
  17313. (if nstars
  17314. (prefix-numeric-value current-prefix-arg)
  17315. (or (org-current-level) 0))
  17316. ?*))
  17317. (add-stars
  17318. (cond (nstars "") ; stars from prefix only
  17319. ((equal stars "") "*") ; before first heading
  17320. (org-odd-levels-only "**") ; inside heading, odd
  17321. (t "*"))) ; inside heading, oddeven
  17322. (rpl (concat stars add-stars " ")))
  17323. (while (< (point) end)
  17324. (when (and (not (org-at-heading-p)) (not (org-at-item-p))
  17325. (looking-at "\\([ \t]*\\)\\(\\S-\\)"))
  17326. (replace-match (concat rpl (match-string 2))))
  17327. (forward-line)))))))))
  17328. (defun org-meta-return (&optional arg)
  17329. "Insert a new heading or wrap a region in a table.
  17330. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  17331. See the individual commands for more information."
  17332. (interactive "P")
  17333. (cond
  17334. ((run-hook-with-args-until-success 'org-metareturn-hook))
  17335. ((or (org-at-drawer-p) (org-at-property-p))
  17336. (newline-and-indent))
  17337. ((org-at-table-p)
  17338. (call-interactively 'org-table-wrap-region))
  17339. (t (call-interactively 'org-insert-heading))))
  17340. ;;; Menu entries
  17341. ;; Define the Org-mode menus
  17342. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  17343. '("Tbl"
  17344. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  17345. ["Next Field" org-cycle (org-at-table-p)]
  17346. ["Previous Field" org-shifttab (org-at-table-p)]
  17347. ["Next Row" org-return (org-at-table-p)]
  17348. "--"
  17349. ["Blank Field" org-table-blank-field (org-at-table-p)]
  17350. ["Edit Field" org-table-edit-field (org-at-table-p)]
  17351. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  17352. "--"
  17353. ("Column"
  17354. ["Move Column Left" org-metaleft (org-at-table-p)]
  17355. ["Move Column Right" org-metaright (org-at-table-p)]
  17356. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  17357. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  17358. ("Row"
  17359. ["Move Row Up" org-metaup (org-at-table-p)]
  17360. ["Move Row Down" org-metadown (org-at-table-p)]
  17361. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  17362. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  17363. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  17364. "--"
  17365. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  17366. ("Rectangle"
  17367. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  17368. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  17369. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  17370. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  17371. "--"
  17372. ("Calculate"
  17373. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  17374. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  17375. ["Edit Formulas" org-edit-special (org-at-table-p)]
  17376. "--"
  17377. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  17378. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  17379. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  17380. "--"
  17381. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  17382. "--"
  17383. ["Sum Column/Rectangle" org-table-sum
  17384. (or (org-at-table-p) (org-region-active-p))]
  17385. ["Which Column?" org-table-current-column (org-at-table-p)])
  17386. ["Debug Formulas"
  17387. org-table-toggle-formula-debugger
  17388. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  17389. ["Show Col/Row Numbers"
  17390. org-table-toggle-coordinate-overlays
  17391. :style toggle
  17392. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  17393. "--"
  17394. ["Create" org-table-create (and (not (org-at-table-p))
  17395. org-enable-table-editor)]
  17396. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  17397. ["Import from File" org-table-import (not (org-at-table-p))]
  17398. ["Export to File" org-table-export (org-at-table-p)]
  17399. "--"
  17400. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  17401. (easy-menu-define org-org-menu org-mode-map "Org menu"
  17402. '("Org"
  17403. ("Show/Hide"
  17404. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  17405. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  17406. ["Sparse Tree..." org-sparse-tree t]
  17407. ["Reveal Context" org-reveal t]
  17408. ["Show All" show-all t]
  17409. "--"
  17410. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  17411. "--"
  17412. ["New Heading" org-insert-heading t]
  17413. ("Navigate Headings"
  17414. ["Up" outline-up-heading t]
  17415. ["Next" outline-next-visible-heading t]
  17416. ["Previous" outline-previous-visible-heading t]
  17417. ["Next Same Level" outline-forward-same-level t]
  17418. ["Previous Same Level" outline-backward-same-level t]
  17419. "--"
  17420. ["Jump" org-goto t])
  17421. ("Edit Structure"
  17422. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  17423. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  17424. "--"
  17425. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  17426. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  17427. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  17428. "--"
  17429. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  17430. "--"
  17431. ["Copy visible text" org-copy-visible t]
  17432. "--"
  17433. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  17434. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  17435. ["Demote Heading" org-metaright (not (org-at-table-p))]
  17436. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  17437. "--"
  17438. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  17439. "--"
  17440. ["Convert to odd levels" org-convert-to-odd-levels t]
  17441. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  17442. ("Editing"
  17443. ["Emphasis..." org-emphasize t]
  17444. ["Edit Source Example" org-edit-special t]
  17445. "--"
  17446. ["Footnote new/jump" org-footnote-action t]
  17447. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  17448. ("Archive"
  17449. ["Archive (default method)" org-archive-subtree-default t]
  17450. "--"
  17451. ["Move Subtree to Archive file" org-advertized-archive-subtree t]
  17452. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  17453. ["Move subtree to Archive sibling" org-archive-to-archive-sibling t]
  17454. )
  17455. "--"
  17456. ("Hyperlinks"
  17457. ["Store Link (Global)" org-store-link t]
  17458. ["Find existing link to here" org-occur-link-in-agenda-files t]
  17459. ["Insert Link" org-insert-link t]
  17460. ["Follow Link" org-open-at-point t]
  17461. "--"
  17462. ["Next link" org-next-link t]
  17463. ["Previous link" org-previous-link t]
  17464. "--"
  17465. ["Descriptive Links"
  17466. org-toggle-link-display
  17467. :style radio
  17468. :selected org-descriptive-links
  17469. ]
  17470. ["Literal Links"
  17471. org-toggle-link-display
  17472. :style radio
  17473. :selected (not org-descriptive-links)])
  17474. "--"
  17475. ("TODO Lists"
  17476. ["TODO/DONE/-" org-todo t]
  17477. ("Select keyword"
  17478. ["Next keyword" org-shiftright (org-at-heading-p)]
  17479. ["Previous keyword" org-shiftleft (org-at-heading-p)]
  17480. ["Complete Keyword" pcomplete (assq :todo-keyword (org-context))]
  17481. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))]
  17482. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))])
  17483. ["Show TODO Tree" org-show-todo-tree :active t :keys "C-c / t"]
  17484. ["Global TODO list" org-todo-list :active t :keys "C-c a t"]
  17485. "--"
  17486. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  17487. :selected org-enforce-todo-dependencies :style toggle :active t]
  17488. "Settings for tree at point"
  17489. ["Do Children sequentially" org-toggle-ordered-property :style radio
  17490. :selected (org-entry-get nil "ORDERED")
  17491. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  17492. ["Do Children parallel" org-toggle-ordered-property :style radio
  17493. :selected (not (org-entry-get nil "ORDERED"))
  17494. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  17495. "--"
  17496. ["Set Priority" org-priority t]
  17497. ["Priority Up" org-shiftup t]
  17498. ["Priority Down" org-shiftdown t]
  17499. "--"
  17500. ["Get news from all feeds" org-feed-update-all t]
  17501. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  17502. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  17503. ("TAGS and Properties"
  17504. ["Set Tags" org-set-tags-command t]
  17505. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  17506. "--"
  17507. ["Set property" org-set-property t]
  17508. ["Column view of properties" org-columns t]
  17509. ["Insert Column View DBlock" org-insert-columns-dblock t])
  17510. ("Dates and Scheduling"
  17511. ["Timestamp" org-time-stamp t]
  17512. ["Timestamp (inactive)" org-time-stamp-inactive t]
  17513. ("Change Date"
  17514. ["1 Day Later" org-shiftright t]
  17515. ["1 Day Earlier" org-shiftleft t]
  17516. ["1 ... Later" org-shiftup t]
  17517. ["1 ... Earlier" org-shiftdown t])
  17518. ["Compute Time Range" org-evaluate-time-range t]
  17519. ["Schedule Item" org-schedule t]
  17520. ["Deadline" org-deadline t]
  17521. "--"
  17522. ["Custom time format" org-toggle-time-stamp-overlays
  17523. :style radio :selected org-display-custom-times]
  17524. "--"
  17525. ["Goto Calendar" org-goto-calendar t]
  17526. ["Date from Calendar" org-date-from-calendar t]
  17527. "--"
  17528. ["Start/Restart Timer" org-timer-start t]
  17529. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  17530. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  17531. ["Insert Timer String" org-timer t]
  17532. ["Insert Timer Item" org-timer-item t])
  17533. ("Logging work"
  17534. ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
  17535. ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
  17536. ["Clock out" org-clock-out t]
  17537. ["Clock cancel" org-clock-cancel t]
  17538. "--"
  17539. ["Mark as default task" org-clock-mark-default-task t]
  17540. ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
  17541. ["Goto running clock" org-clock-goto t]
  17542. "--"
  17543. ["Display times" org-clock-display t]
  17544. ["Create clock table" org-clock-report t]
  17545. "--"
  17546. ["Record DONE time"
  17547. (progn (setq org-log-done (not org-log-done))
  17548. (message "Switching to %s will %s record a timestamp"
  17549. (car org-done-keywords)
  17550. (if org-log-done "automatically" "not")))
  17551. :style toggle :selected org-log-done])
  17552. "--"
  17553. ["Agenda Command..." org-agenda t]
  17554. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  17555. ("File List for Agenda")
  17556. ("Special views current file"
  17557. ["TODO Tree" org-show-todo-tree t]
  17558. ["Check Deadlines" org-check-deadlines t]
  17559. ["Timeline" org-timeline t]
  17560. ["Tags/Property tree" org-match-sparse-tree t])
  17561. "--"
  17562. ["Export/Publish..." org-export t]
  17563. ("LaTeX"
  17564. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  17565. :selected org-cdlatex-mode]
  17566. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  17567. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  17568. ["Modify math symbol" org-cdlatex-math-modify
  17569. (org-inside-LaTeX-fragment-p)]
  17570. ["Insert citation" org-reftex-citation t]
  17571. "--"
  17572. ["Template for BEAMER" org-insert-beamer-options-template t])
  17573. "--"
  17574. ("MobileOrg"
  17575. ["Push Files and Views" org-mobile-push t]
  17576. ["Get Captured and Flagged" org-mobile-pull t]
  17577. ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
  17578. "--"
  17579. ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
  17580. "--"
  17581. ("Documentation"
  17582. ["Show Version" org-version t]
  17583. ["Info Documentation" org-info t])
  17584. ("Customize"
  17585. ["Browse Org Group" org-customize t]
  17586. "--"
  17587. ["Expand This Menu" org-create-customize-menu
  17588. (fboundp 'customize-menu-create)])
  17589. ["Send bug report" org-submit-bug-report t]
  17590. "--"
  17591. ("Refresh/Reload"
  17592. ["Refresh setup current buffer" org-mode-restart t]
  17593. ["Reload Org (after update)" org-reload t]
  17594. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x r"])
  17595. ))
  17596. (defun org-info (&optional node)
  17597. "Read documentation for Org-mode in the info system.
  17598. With optional NODE, go directly to that node."
  17599. (interactive)
  17600. (info (format "(org)%s" (or node ""))))
  17601. ;;;###autoload
  17602. (defun org-submit-bug-report ()
  17603. "Submit a bug report on Org-mode via mail.
  17604. Don't hesitate to report any problems or inaccurate documentation.
  17605. If you don't have setup sending mail from (X)Emacs, please copy the
  17606. output buffer into your mail program, as it gives us important
  17607. information about your Org-mode version and configuration."
  17608. (interactive)
  17609. (require 'reporter)
  17610. (org-load-modules-maybe)
  17611. (org-require-autoloaded-modules)
  17612. (let ((reporter-prompt-for-summary-p "Bug report subject: "))
  17613. (reporter-submit-bug-report
  17614. "emacs-orgmode@gnu.org"
  17615. (org-version)
  17616. (let (list)
  17617. (save-window-excursion
  17618. (org-pop-to-buffer-same-window (get-buffer-create "*Warn about privacy*"))
  17619. (delete-other-windows)
  17620. (erase-buffer)
  17621. (insert "You are about to submit a bug report to the Org-mode mailing list.
  17622. We would like to add your full Org-mode and Outline configuration to the
  17623. bug report. This greatly simplifies the work of the maintainer and
  17624. other experts on the mailing list.
  17625. HOWEVER, some variables you have customized may contain private
  17626. information. The names of customers, colleagues, or friends, might
  17627. appear in the form of file names, tags, todo states, or search strings.
  17628. If you answer yes to the prompt, you might want to check and remove
  17629. such private information before sending the email.")
  17630. (add-text-properties (point-min) (point-max) '(face org-warning))
  17631. (when (yes-or-no-p "Include your Org-mode configuration ")
  17632. (mapatoms
  17633. (lambda (v)
  17634. (and (boundp v)
  17635. (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
  17636. (or (and (symbol-value v)
  17637. (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
  17638. (and
  17639. (get v 'custom-type) (get v 'standard-value)
  17640. (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
  17641. (push v list)))))
  17642. (kill-buffer (get-buffer "*Warn about privacy*"))
  17643. list))
  17644. nil nil
  17645. "Remember to cover the basics, that is, what you expected to happen and
  17646. what in fact did happen. You don't know how to make a good report? See
  17647. http://orgmode.org/manual/Feedback.html#Feedback
  17648. Your bug report will be posted to the Org-mode mailing list.
  17649. ------------------------------------------------------------------------")
  17650. (save-excursion
  17651. (if (re-search-backward "^\\(Subject: \\)Org-mode version \\(.*?\\);[ \t]*\\(.*\\)" nil t)
  17652. (replace-match "\\1Bug: \\3 [\\2]")))))
  17653. (defun org-install-agenda-files-menu ()
  17654. (let ((bl (buffer-list)))
  17655. (save-excursion
  17656. (while bl
  17657. (set-buffer (pop bl))
  17658. (if (eq major-mode 'org-mode) (setq bl nil)))
  17659. (when (eq major-mode 'org-mode)
  17660. (easy-menu-change
  17661. '("Org") "File List for Agenda"
  17662. (append
  17663. (list
  17664. ["Edit File List" (org-edit-agenda-file-list) t]
  17665. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  17666. ["Remove Current File from List" org-remove-file t]
  17667. ["Cycle through agenda files" org-cycle-agenda-files t]
  17668. ["Occur in all agenda files" org-occur-in-agenda-files t]
  17669. "--")
  17670. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  17671. ;;;; Documentation
  17672. ;;;###autoload
  17673. (defun org-require-autoloaded-modules ()
  17674. (interactive)
  17675. (mapc 'require
  17676. '(org-agenda org-archive org-ascii org-attach org-clock org-colview
  17677. org-docbook org-exp org-html org-icalendar
  17678. org-id org-latex
  17679. org-publish org-remember org-table
  17680. org-timer org-xoxo)))
  17681. ;;;###autoload
  17682. (defun org-reload (&optional uncompiled)
  17683. "Reload all org lisp files.
  17684. With prefix arg UNCOMPILED, load the uncompiled versions."
  17685. (interactive "P")
  17686. (require 'find-func)
  17687. (let* ((file-re "^\\(org\\|orgtbl\\)\\(\\.el\\|-.*\\.el\\)")
  17688. (dir-org (file-name-directory (org-find-library-name "org")))
  17689. (dir-org-contrib (ignore-errors
  17690. (file-name-directory
  17691. (org-find-library-name "org-contribdir"))))
  17692. (babel-files
  17693. (mapcar (lambda (el) (concat "ob" (when el (format "-%s" el)) ".el"))
  17694. (append (list nil "comint" "eval" "exp" "keys"
  17695. "lob" "ref" "table" "tangle")
  17696. (delq nil
  17697. (mapcar
  17698. (lambda (lang)
  17699. (when (cdr lang) (symbol-name (car lang))))
  17700. org-babel-load-languages)))))
  17701. (files
  17702. (append (directory-files dir-org t file-re)
  17703. babel-files
  17704. (and dir-org-contrib
  17705. (directory-files dir-org-contrib t file-re))))
  17706. (remove-re (concat (if (featurep 'xemacs)
  17707. "org-colview" "org-colview-xemacs")
  17708. "\\'")))
  17709. (setq files (mapcar 'file-name-sans-extension files))
  17710. (setq files (mapcar
  17711. (lambda (x) (if (string-match remove-re x) nil x))
  17712. files))
  17713. (setq files (delq nil files))
  17714. (mapc
  17715. (lambda (f)
  17716. (when (featurep (intern (file-name-nondirectory f)))
  17717. (if (and (not uncompiled)
  17718. (file-exists-p (concat f ".elc")))
  17719. (load (concat f ".elc") nil nil t)
  17720. (load (concat f ".el") nil nil t))))
  17721. files))
  17722. (org-version))
  17723. ;;;###autoload
  17724. (defun org-customize ()
  17725. "Call the customize function with org as argument."
  17726. (interactive)
  17727. (org-load-modules-maybe)
  17728. (org-require-autoloaded-modules)
  17729. (customize-browse 'org))
  17730. (defun org-create-customize-menu ()
  17731. "Create a full customization menu for Org-mode, insert it into the menu."
  17732. (interactive)
  17733. (org-load-modules-maybe)
  17734. (org-require-autoloaded-modules)
  17735. (if (fboundp 'customize-menu-create)
  17736. (progn
  17737. (easy-menu-change
  17738. '("Org") "Customize"
  17739. `(["Browse Org group" org-customize t]
  17740. "--"
  17741. ,(customize-menu-create 'org)
  17742. ["Set" Custom-set t]
  17743. ["Save" Custom-save t]
  17744. ["Reset to Current" Custom-reset-current t]
  17745. ["Reset to Saved" Custom-reset-saved t]
  17746. ["Reset to Standard Settings" Custom-reset-standard t]))
  17747. (message "\"Org\"-menu now contains full customization menu"))
  17748. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  17749. ;;;; Miscellaneous stuff
  17750. ;;; Generally useful functions
  17751. (defun org-get-at-bol (property)
  17752. "Get text property PROPERTY at beginning of line."
  17753. (get-text-property (point-at-bol) property))
  17754. (defun org-find-text-property-in-string (prop s)
  17755. "Return the first non-nil value of property PROP in string S."
  17756. (or (get-text-property 0 prop s)
  17757. (get-text-property (or (next-single-property-change 0 prop s) 0)
  17758. prop s)))
  17759. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  17760. "Display the given MESSAGE as a warning."
  17761. (if (fboundp 'display-warning)
  17762. (display-warning 'org message
  17763. (if (featurep 'xemacs) 'warning :warning))
  17764. (let ((buf (get-buffer-create "*Org warnings*")))
  17765. (with-current-buffer buf
  17766. (goto-char (point-max))
  17767. (insert "Warning (Org): " message)
  17768. (unless (bolp)
  17769. (newline)))
  17770. (display-buffer buf)
  17771. (sit-for 0))))
  17772. (defun org-eval (form)
  17773. "Eval FORM and return result."
  17774. (condition-case error
  17775. (eval form)
  17776. (error (format "%%![Error: %s]" error))))
  17777. (defun org-in-clocktable-p ()
  17778. "Check if the cursor is in a clocktable."
  17779. (let ((pos (point)) start)
  17780. (save-excursion
  17781. (end-of-line 1)
  17782. (and (re-search-backward "^[ \t]*#\\+BEGIN:[ \t]+clocktable" nil t)
  17783. (setq start (match-beginning 0))
  17784. (re-search-forward "^[ \t]*#\\+END:.*" nil t)
  17785. (>= (match-end 0) pos)
  17786. start))))
  17787. (defun org-in-commented-line ()
  17788. "Is point in a line starting with `#'?"
  17789. (equal (char-after (point-at-bol)) ?#))
  17790. (defun org-in-indented-comment-line ()
  17791. "Is point in a line starting with `#' after some white space?"
  17792. (save-excursion
  17793. (save-match-data
  17794. (goto-char (point-at-bol))
  17795. (looking-at "[ \t]*#"))))
  17796. (defun org-in-verbatim-emphasis ()
  17797. (save-match-data
  17798. (and (org-in-regexp org-emph-re 2) (member (match-string 3) '("=" "~")))))
  17799. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  17800. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  17801. (if (and marker (marker-buffer marker)
  17802. (buffer-live-p (marker-buffer marker)))
  17803. (progn
  17804. (org-pop-to-buffer-same-window (marker-buffer marker))
  17805. (if (or (> marker (point-max)) (< marker (point-min)))
  17806. (widen))
  17807. (goto-char marker)
  17808. (org-show-context 'org-goto))
  17809. (if bookmark
  17810. (bookmark-jump bookmark)
  17811. (error "Cannot find location"))))
  17812. (defun org-quote-csv-field (s)
  17813. "Quote field for inclusion in CSV material."
  17814. (if (string-match "[\",]" s)
  17815. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  17816. s))
  17817. (defun org-force-self-insert (N)
  17818. "Needed to enforce self-insert under remapping."
  17819. (interactive "p")
  17820. (self-insert-command N))
  17821. (defun org-string-width (s)
  17822. "Compute width of string, ignoring invisible characters.
  17823. This ignores character with invisibility property `org-link', and also
  17824. characters with property `org-cwidth', because these will become invisible
  17825. upon the next fontification round."
  17826. (let (b l)
  17827. (when (or (eq t buffer-invisibility-spec)
  17828. (assq 'org-link buffer-invisibility-spec))
  17829. (while (setq b (text-property-any 0 (length s)
  17830. 'invisible 'org-link s))
  17831. (setq s (concat (substring s 0 b)
  17832. (substring s (or (next-single-property-change
  17833. b 'invisible s) (length s)))))))
  17834. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  17835. (setq s (concat (substring s 0 b)
  17836. (substring s (or (next-single-property-change
  17837. b 'org-cwidth s) (length s))))))
  17838. (setq l (string-width s) b -1)
  17839. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  17840. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  17841. l))
  17842. (defun org-shorten-string (s maxlength)
  17843. "Shorten string S so tht it is no longer than MAXLENGTH characters.
  17844. If the string is shorter or has length MAXLENGTH, just return the
  17845. original string. If it is longer, the functions finds a space in the
  17846. string, breaks this string off at that locations and adds three dots
  17847. as ellipsis. Including the ellipsis, the string will not be longer
  17848. than MAXLENGTH. If finding a good breaking point in the string does
  17849. not work, the string is just chopped off in the middle of a word
  17850. if necessary."
  17851. (if (<= (length s) maxlength)
  17852. s
  17853. (let* ((n (max (- maxlength 4) 1))
  17854. (re (concat "\\`\\(.\\{1," (int-to-string n) "\\}[^ ]\\)\\([ ]\\|\\'\\)")))
  17855. (if (string-match re s)
  17856. (concat (match-string 1 s) "...")
  17857. (concat (substring s 0 (max (- maxlength 3) 0)) "...")))))
  17858. (defun org-get-indentation (&optional line)
  17859. "Get the indentation of the current line, interpreting tabs.
  17860. When LINE is given, assume it represents a line and compute its indentation."
  17861. (if line
  17862. (if (string-match "^ *" (org-remove-tabs line))
  17863. (match-end 0))
  17864. (save-excursion
  17865. (beginning-of-line 1)
  17866. (skip-chars-forward " \t")
  17867. (current-column))))
  17868. (defun org-get-string-indentation (s)
  17869. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  17870. (let ((n -1) (i 0) (w tab-width) c)
  17871. (catch 'exit
  17872. (while (< (setq n (1+ n)) (length s))
  17873. (setq c (aref s n))
  17874. (cond ((= c ?\ ) (setq i (1+ i)))
  17875. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  17876. (t (throw 'exit t)))))
  17877. i))
  17878. (defun org-remove-tabs (s &optional width)
  17879. "Replace tabulators in S with spaces.
  17880. Assumes that s is a single line, starting in column 0."
  17881. (setq width (or width tab-width))
  17882. (while (string-match "\t" s)
  17883. (setq s (replace-match
  17884. (make-string
  17885. (- (* width (/ (+ (match-beginning 0) width) width))
  17886. (match-beginning 0)) ?\ )
  17887. t t s)))
  17888. s)
  17889. (defun org-fix-indentation (line ind)
  17890. "Fix indentation in LINE.
  17891. IND is a cons cell with target and minimum indentation.
  17892. If the current indentation in LINE is smaller than the minimum,
  17893. leave it alone. If it is larger than ind, set it to the target."
  17894. (let* ((l (org-remove-tabs line))
  17895. (i (org-get-indentation l))
  17896. (i1 (car ind)) (i2 (cdr ind)))
  17897. (if (>= i i2) (setq l (substring line i2)))
  17898. (if (> i1 0)
  17899. (concat (make-string i1 ?\ ) l)
  17900. l)))
  17901. (defun org-remove-indentation (code &optional n)
  17902. "Remove the maximum common indentation from the lines in CODE.
  17903. N may optionally be the number of spaces to remove."
  17904. (with-temp-buffer
  17905. (insert code)
  17906. (org-do-remove-indentation n)
  17907. (buffer-string)))
  17908. (defun org-do-remove-indentation (&optional n)
  17909. "Remove the maximum common indentation from the buffer."
  17910. (untabify (point-min) (point-max))
  17911. (let ((min 10000) re)
  17912. (if n
  17913. (setq min n)
  17914. (goto-char (point-min))
  17915. (while (re-search-forward "^ *[^ \n]" nil t)
  17916. (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
  17917. (unless (or (= min 0) (= min 10000))
  17918. (setq re (format "^ \\{%d\\}" min))
  17919. (goto-char (point-min))
  17920. (while (re-search-forward re nil t)
  17921. (replace-match "")
  17922. (end-of-line 1))
  17923. min)))
  17924. (defun org-fill-template (template alist)
  17925. "Find each %key of ALIST in TEMPLATE and replace it."
  17926. (let ((case-fold-search nil)
  17927. entry key value)
  17928. (setq alist (sort (copy-sequence alist)
  17929. (lambda (a b) (< (length (car a)) (length (car b))))))
  17930. (while (setq entry (pop alist))
  17931. (setq template
  17932. (replace-regexp-in-string
  17933. (concat "%" (regexp-quote (car entry)))
  17934. (cdr entry) template t t)))
  17935. template))
  17936. (defun org-base-buffer (buffer)
  17937. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  17938. (if (not buffer)
  17939. buffer
  17940. (or (buffer-base-buffer buffer)
  17941. buffer)))
  17942. (defun org-trim (s)
  17943. "Remove whitespace at beginning and end of string."
  17944. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  17945. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  17946. s)
  17947. (defun org-wrap (string &optional width lines)
  17948. "Wrap string to either a number of lines, or a width in characters.
  17949. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  17950. that costs. If there is a word longer than WIDTH, the text is actually
  17951. wrapped to the length of that word.
  17952. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  17953. many lines, whatever width that takes.
  17954. The return value is a list of lines, without newlines at the end."
  17955. (let* ((words (org-split-string string "[ \t\n]+"))
  17956. (maxword (apply 'max (mapcar 'org-string-width words)))
  17957. w ll)
  17958. (cond (width
  17959. (org-do-wrap words (max maxword width)))
  17960. (lines
  17961. (setq w maxword)
  17962. (setq ll (org-do-wrap words maxword))
  17963. (if (<= (length ll) lines)
  17964. ll
  17965. (setq ll words)
  17966. (while (> (length ll) lines)
  17967. (setq w (1+ w))
  17968. (setq ll (org-do-wrap words w)))
  17969. ll))
  17970. (t (error "Cannot wrap this")))))
  17971. (defun org-do-wrap (words width)
  17972. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  17973. (let (lines line)
  17974. (while words
  17975. (setq line (pop words))
  17976. (while (and words (< (+ (length line) (length (car words))) width))
  17977. (setq line (concat line " " (pop words))))
  17978. (setq lines (push line lines)))
  17979. (nreverse lines)))
  17980. (defun org-split-string (string &optional separators)
  17981. "Splits STRING into substrings at SEPARATORS.
  17982. No empty strings are returned if there are matches at the beginning
  17983. and end of string."
  17984. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  17985. (start 0)
  17986. notfirst
  17987. (list nil))
  17988. (while (and (string-match rexp string
  17989. (if (and notfirst
  17990. (= start (match-beginning 0))
  17991. (< start (length string)))
  17992. (1+ start) start))
  17993. (< (match-beginning 0) (length string)))
  17994. (setq notfirst t)
  17995. (or (eq (match-beginning 0) 0)
  17996. (and (eq (match-beginning 0) (match-end 0))
  17997. (eq (match-beginning 0) start))
  17998. (setq list
  17999. (cons (substring string start (match-beginning 0))
  18000. list)))
  18001. (setq start (match-end 0)))
  18002. (or (eq start (length string))
  18003. (setq list
  18004. (cons (substring string start)
  18005. list)))
  18006. (nreverse list)))
  18007. (defun org-quote-vert (s)
  18008. "Replace \"|\" with \"\\vert\"."
  18009. (while (string-match "|" s)
  18010. (setq s (replace-match "\\vert" t t s)))
  18011. s)
  18012. (defun org-uuidgen-p (s)
  18013. "Is S an ID created by UUIDGEN?"
  18014. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  18015. (defun org-in-src-block-p nil
  18016. "Whether point is in a code source block."
  18017. (let (ov)
  18018. (when (setq ov (overlays-at (point)))
  18019. (memq 'org-block-background
  18020. (overlay-properties
  18021. (car ov))))))
  18022. (defun org-context ()
  18023. "Return a list of contexts of the current cursor position.
  18024. If several contexts apply, all are returned.
  18025. Each context entry is a list with a symbol naming the context, and
  18026. two positions indicating start and end of the context. Possible
  18027. contexts are:
  18028. :headline anywhere in a headline
  18029. :headline-stars on the leading stars in a headline
  18030. :todo-keyword on a TODO keyword (including DONE) in a headline
  18031. :tags on the TAGS in a headline
  18032. :priority on the priority cookie in a headline
  18033. :item on the first line of a plain list item
  18034. :item-bullet on the bullet/number of a plain list item
  18035. :checkbox on the checkbox in a plain list item
  18036. :table in an org-mode table
  18037. :table-special on a special filed in a table
  18038. :table-table in a table.el table
  18039. :clocktable in a clocktable
  18040. :src-block in a source block
  18041. :link on a hyperlink
  18042. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE, COMMENT, QUOTE.
  18043. :target on a <<target>>
  18044. :radio-target on a <<<radio-target>>>
  18045. :latex-fragment on a LaTeX fragment
  18046. :latex-preview on a LaTeX fragment with overlaid preview image
  18047. This function expects the position to be visible because it uses font-lock
  18048. faces as a help to recognize the following contexts: :table-special, :link,
  18049. and :keyword."
  18050. (let* ((f (get-text-property (point) 'face))
  18051. (faces (if (listp f) f (list f)))
  18052. (case-fold-search t)
  18053. (p (point)) clist o)
  18054. ;; First the large context
  18055. (cond
  18056. ((org-at-heading-p t)
  18057. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  18058. (when (progn
  18059. (beginning-of-line 1)
  18060. (looking-at org-todo-line-tags-regexp))
  18061. (push (org-point-in-group p 1 :headline-stars) clist)
  18062. (push (org-point-in-group p 2 :todo-keyword) clist)
  18063. (push (org-point-in-group p 4 :tags) clist))
  18064. (goto-char p)
  18065. (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
  18066. (if (looking-at "\\[#[A-Z0-9]\\]")
  18067. (push (org-point-in-group p 0 :priority) clist)))
  18068. ((org-at-item-p)
  18069. (push (org-point-in-group p 2 :item-bullet) clist)
  18070. (push (list :item (point-at-bol)
  18071. (save-excursion (org-end-of-item) (point)))
  18072. clist)
  18073. (and (org-at-item-checkbox-p)
  18074. (push (org-point-in-group p 0 :checkbox) clist)))
  18075. ((org-at-table-p)
  18076. (push (list :table (org-table-begin) (org-table-end)) clist)
  18077. (if (memq 'org-formula faces)
  18078. (push (list :table-special
  18079. (previous-single-property-change p 'face)
  18080. (next-single-property-change p 'face)) clist)))
  18081. ((org-at-table-p 'any)
  18082. (push (list :table-table) clist)))
  18083. (goto-char p)
  18084. ;; New the "medium" contexts: clocktables, source blocks
  18085. (cond ((org-in-clocktable-p)
  18086. (push (list :clocktable
  18087. (and (or (looking-at "#\\+BEGIN: clocktable")
  18088. (search-backward "#+BEGIN: clocktable" nil t))
  18089. (match-beginning 0))
  18090. (and (re-search-forward "#\\+END:?" nil t)
  18091. (match-end 0))) clist))
  18092. ((org-in-src-block-p)
  18093. (push (list :src-block
  18094. (and (or (looking-at "#\\+BEGIN_SRC")
  18095. (search-backward "#+BEGIN_SRC" nil t))
  18096. (match-beginning 0))
  18097. (and (search-forward "#+END_SRC" nil t)
  18098. (match-beginning 0))) clist)))
  18099. (goto-char p)
  18100. ;; Now the small context
  18101. (cond
  18102. ((org-at-timestamp-p)
  18103. (push (org-point-in-group p 0 :timestamp) clist))
  18104. ((memq 'org-link faces)
  18105. (push (list :link
  18106. (previous-single-property-change p 'face)
  18107. (next-single-property-change p 'face)) clist))
  18108. ((memq 'org-special-keyword faces)
  18109. (push (list :keyword
  18110. (previous-single-property-change p 'face)
  18111. (next-single-property-change p 'face)) clist))
  18112. ((org-at-target-p)
  18113. (push (org-point-in-group p 0 :target) clist)
  18114. (goto-char (1- (match-beginning 0)))
  18115. (if (looking-at org-radio-target-regexp)
  18116. (push (org-point-in-group p 0 :radio-target) clist))
  18117. (goto-char p))
  18118. ((setq o (car (delq nil
  18119. (mapcar
  18120. (lambda (x)
  18121. (if (memq x org-latex-fragment-image-overlays) x))
  18122. (overlays-at (point))))))
  18123. (push (list :latex-fragment
  18124. (overlay-start o) (overlay-end o)) clist)
  18125. (push (list :latex-preview
  18126. (overlay-start o) (overlay-end o)) clist))
  18127. ((org-inside-LaTeX-fragment-p)
  18128. ;; FIXME: positions wrong.
  18129. (push (list :latex-fragment (point) (point)) clist)))
  18130. (setq clist (nreverse (delq nil clist)))
  18131. clist))
  18132. ;; FIXME: Compare with at-regexp-p Do we need both?
  18133. (defun org-in-regexp (re &optional nlines visually)
  18134. "Check if point is inside a match of regexp.
  18135. Normally only the current line is checked, but you can include NLINES extra
  18136. lines both before and after point into the search.
  18137. If VISUALLY is set, require that the cursor is not after the match but
  18138. really on, so that the block visually is on the match."
  18139. (catch 'exit
  18140. (let ((pos (point))
  18141. (eol (point-at-eol (+ 1 (or nlines 0))))
  18142. (inc (if visually 1 0)))
  18143. (save-excursion
  18144. (beginning-of-line (- 1 (or nlines 0)))
  18145. (while (re-search-forward re eol t)
  18146. (if (and (<= (match-beginning 0) pos)
  18147. (>= (+ inc (match-end 0)) pos))
  18148. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  18149. (defun org-at-regexp-p (regexp)
  18150. "Is point inside a match of REGEXP in the current line?"
  18151. (catch 'exit
  18152. (save-excursion
  18153. (let ((pos (point)) (end (point-at-eol)))
  18154. (beginning-of-line 1)
  18155. (while (re-search-forward regexp end t)
  18156. (if (and (<= (match-beginning 0) pos)
  18157. (>= (match-end 0) pos))
  18158. (throw 'exit t)))
  18159. nil))))
  18160. (defun org-between-regexps-p (start-re end-re &optional lim-up lim-down)
  18161. "Non-nil when point is between matches of START-RE and END-RE.
  18162. Also return a non-nil value when point is on one of the matches.
  18163. Optional arguments LIM-UP and LIM-DOWN bound the search; they are
  18164. buffer positions. Default values are the positions of headlines
  18165. surrounding the point.
  18166. The functions returns a cons cell whose car (resp. cdr) is the
  18167. position before START-RE (resp. after END-RE)."
  18168. (save-match-data
  18169. (let ((pos (point))
  18170. (limit-up (or lim-up (save-excursion (outline-previous-heading))))
  18171. (limit-down (or lim-down (save-excursion (outline-next-heading))))
  18172. beg end)
  18173. (save-excursion
  18174. ;; Point is on a block when on START-RE or if START-RE can be
  18175. ;; found before it...
  18176. (and (or (org-at-regexp-p start-re)
  18177. (re-search-backward start-re limit-up t))
  18178. (setq beg (match-beginning 0))
  18179. ;; ... and END-RE after it...
  18180. (goto-char (match-end 0))
  18181. (re-search-forward end-re limit-down t)
  18182. (> (setq end (match-end 0)) pos)
  18183. ;; ... without another START-RE in-between.
  18184. (goto-char (match-beginning 0))
  18185. (not (re-search-backward start-re (1+ beg) t))
  18186. ;; Return value.
  18187. (cons beg end))))))
  18188. (defun org-in-block-p (names)
  18189. "Non-nil when point belongs to a block whose name belongs to NAMES.
  18190. NAMES is a list of strings containing names of blocks.
  18191. Return first block name matched, or nil. Beware that in case of
  18192. nested blocks, the returned name may not belong to the closest
  18193. block from point."
  18194. (save-match-data
  18195. (catch 'exit
  18196. (let ((case-fold-search t)
  18197. (lim-up (save-excursion (outline-previous-heading)))
  18198. (lim-down (save-excursion (outline-next-heading))))
  18199. (mapc (lambda (name)
  18200. (let ((n (regexp-quote name)))
  18201. (when (org-between-regexps-p
  18202. (concat "^[ \t]*#\\+begin_" n)
  18203. (concat "^[ \t]*#\\+end_" n)
  18204. lim-up lim-down)
  18205. (throw 'exit n))))
  18206. names))
  18207. nil)))
  18208. (defun org-occur-in-agenda-files (regexp &optional nlines)
  18209. "Call `multi-occur' with buffers for all agenda files."
  18210. (interactive "sOrg-files matching: \np")
  18211. (let* ((files (org-agenda-files))
  18212. (tnames (mapcar 'file-truename files))
  18213. (extra org-agenda-text-search-extra-files)
  18214. f)
  18215. (when (eq (car extra) 'agenda-archives)
  18216. (setq extra (cdr extra))
  18217. (setq files (org-add-archive-files files)))
  18218. (while (setq f (pop extra))
  18219. (unless (member (file-truename f) tnames)
  18220. (add-to-list 'files f 'append)
  18221. (add-to-list 'tnames (file-truename f) 'append)))
  18222. (multi-occur
  18223. (mapcar (lambda (x)
  18224. (with-current-buffer
  18225. (or (get-file-buffer x) (find-file-noselect x))
  18226. (widen)
  18227. (current-buffer)))
  18228. files)
  18229. regexp)))
  18230. (if (boundp 'occur-mode-find-occurrence-hook)
  18231. ;; Emacs 23
  18232. (add-hook 'occur-mode-find-occurrence-hook
  18233. (lambda ()
  18234. (when (eq major-mode 'org-mode)
  18235. (org-reveal))))
  18236. ;; Emacs 22
  18237. (defadvice occur-mode-goto-occurrence
  18238. (after org-occur-reveal activate)
  18239. (and (eq major-mode 'org-mode) (org-reveal)))
  18240. (defadvice occur-mode-goto-occurrence-other-window
  18241. (after org-occur-reveal activate)
  18242. (and (eq major-mode 'org-mode) (org-reveal)))
  18243. (defadvice occur-mode-display-occurrence
  18244. (after org-occur-reveal activate)
  18245. (when (eq major-mode 'org-mode)
  18246. (let ((pos (occur-mode-find-occurrence)))
  18247. (with-current-buffer (marker-buffer pos)
  18248. (save-excursion
  18249. (goto-char pos)
  18250. (org-reveal)))))))
  18251. (defun org-occur-link-in-agenda-files ()
  18252. "Create a link and search for it in the agendas.
  18253. The link is not stored in `org-stored-links', it is just created
  18254. for the search purpose."
  18255. (interactive)
  18256. (let ((link (condition-case nil
  18257. (org-store-link nil)
  18258. (error "Unable to create a link to here"))))
  18259. (org-occur-in-agenda-files (regexp-quote link))))
  18260. (defun org-uniquify (list)
  18261. "Remove duplicate elements from LIST."
  18262. (let (res)
  18263. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  18264. res))
  18265. (defun org-delete-all (elts list)
  18266. "Remove all elements in ELTS from LIST."
  18267. (while elts
  18268. (setq list (delete (pop elts) list)))
  18269. list)
  18270. (defun org-count (cl-item cl-seq)
  18271. "Count the number of occurrences of ITEM in SEQ.
  18272. Taken from `count' in cl-seq.el with all keyword arguments removed."
  18273. (let ((cl-end (length cl-seq)) (cl-start 0) (cl-count 0) cl-x)
  18274. (when (consp cl-seq) (setq cl-seq (nthcdr cl-start cl-seq)))
  18275. (while (< cl-start cl-end)
  18276. (setq cl-x (if (consp cl-seq) (pop cl-seq) (aref cl-seq cl-start)))
  18277. (if (equal cl-item cl-x) (setq cl-count (1+ cl-count)))
  18278. (setq cl-start (1+ cl-start)))
  18279. cl-count))
  18280. (defun org-remove-if (predicate seq)
  18281. "Remove everything from SEQ that fulfills PREDICATE."
  18282. (let (res e)
  18283. (while seq
  18284. (setq e (pop seq))
  18285. (if (not (funcall predicate e)) (push e res)))
  18286. (nreverse res)))
  18287. (defun org-remove-if-not (predicate seq)
  18288. "Remove everything from SEQ that does not fulfill PREDICATE."
  18289. (let (res e)
  18290. (while seq
  18291. (setq e (pop seq))
  18292. (if (funcall predicate e) (push e res)))
  18293. (nreverse res)))
  18294. (defun org-reduce (cl-func cl-seq &rest cl-keys)
  18295. "Reduce two-argument FUNCTION across SEQ.
  18296. Taken from `reduce' in cl-seq.el with all keyword arguments but
  18297. \":initial-value\" removed."
  18298. (let ((cl-accum (cond ((memq :initial-value cl-keys)
  18299. (cadr (memq :initial-value cl-keys)))
  18300. (cl-seq (pop cl-seq))
  18301. (t (funcall cl-func)))))
  18302. (while cl-seq
  18303. (setq cl-accum (funcall cl-func cl-accum (pop cl-seq))))
  18304. cl-accum))
  18305. (defun org-back-over-empty-lines ()
  18306. "Move backwards over whitespace, to the beginning of the first empty line.
  18307. Returns the number of empty lines passed."
  18308. (let ((pos (point)))
  18309. (if (cdr (assoc 'heading org-blank-before-new-entry))
  18310. (skip-chars-backward " \t\n\r")
  18311. (unless (eobp)
  18312. (forward-line -1)))
  18313. (beginning-of-line 2)
  18314. (goto-char (min (point) pos))
  18315. (count-lines (point) pos)))
  18316. (defun org-skip-whitespace ()
  18317. (skip-chars-forward " \t\n\r"))
  18318. (defun org-point-in-group (point group &optional context)
  18319. "Check if POINT is in match-group GROUP.
  18320. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  18321. match. If the match group does not exist or point is not inside it,
  18322. return nil."
  18323. (and (match-beginning group)
  18324. (>= point (match-beginning group))
  18325. (<= point (match-end group))
  18326. (if context
  18327. (list context (match-beginning group) (match-end group))
  18328. t)))
  18329. (defun org-switch-to-buffer-other-window (&rest args)
  18330. "Switch to buffer in a second window on the current frame.
  18331. In particular, do not allow pop-up frames.
  18332. Returns the newly created buffer."
  18333. (let (pop-up-frames special-display-buffer-names special-display-regexps
  18334. special-display-function)
  18335. (apply 'switch-to-buffer-other-window args)))
  18336. (defun org-combine-plists (&rest plists)
  18337. "Create a single property list from all plists in PLISTS.
  18338. The process starts by copying the first list, and then setting properties
  18339. from the other lists. Settings in the last list are the most significant
  18340. ones and overrule settings in the other lists."
  18341. (let ((rtn (copy-sequence (pop plists)))
  18342. p v ls)
  18343. (while plists
  18344. (setq ls (pop plists))
  18345. (while ls
  18346. (setq p (pop ls) v (pop ls))
  18347. (setq rtn (plist-put rtn p v))))
  18348. rtn))
  18349. (defun org-move-line-down (arg)
  18350. "Move the current line down. With prefix argument, move it past ARG lines."
  18351. (interactive "p")
  18352. (let ((col (current-column))
  18353. beg end pos)
  18354. (beginning-of-line 1) (setq beg (point))
  18355. (beginning-of-line 2) (setq end (point))
  18356. (beginning-of-line (+ 1 arg))
  18357. (setq pos (move-marker (make-marker) (point)))
  18358. (insert (delete-and-extract-region beg end))
  18359. (goto-char pos)
  18360. (org-move-to-column col)))
  18361. (defun org-move-line-up (arg)
  18362. "Move the current line up. With prefix argument, move it past ARG lines."
  18363. (interactive "p")
  18364. (let ((col (current-column))
  18365. beg end pos)
  18366. (beginning-of-line 1) (setq beg (point))
  18367. (beginning-of-line 2) (setq end (point))
  18368. (beginning-of-line (- arg))
  18369. (setq pos (move-marker (make-marker) (point)))
  18370. (insert (delete-and-extract-region beg end))
  18371. (goto-char pos)
  18372. (org-move-to-column col)))
  18373. (defun org-replace-escapes (string table)
  18374. "Replace %-escapes in STRING with values in TABLE.
  18375. TABLE is an association list with keys like \"%a\" and string values.
  18376. The sequences in STRING may contain normal field width and padding information,
  18377. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  18378. so values can contain further %-escapes if they are define later in TABLE."
  18379. (let ((tbl (copy-alist table))
  18380. (case-fold-search nil)
  18381. (pchg 0)
  18382. e re rpl)
  18383. (while (setq e (pop tbl))
  18384. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  18385. (when (and (cdr e) (string-match re (cdr e)))
  18386. (let ((sref (substring (cdr e) (match-beginning 0) (match-end 0)))
  18387. (safe "SREF"))
  18388. (add-text-properties 0 3 (list 'sref sref) safe)
  18389. (setcdr e (replace-match safe t t (cdr e)))))
  18390. (while (string-match re string)
  18391. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  18392. (cdr e)))
  18393. (setq string (replace-match rpl t t string))))
  18394. (while (setq pchg (next-property-change pchg string))
  18395. (let ((sref (get-text-property pchg 'sref string)))
  18396. (when (and sref (string-match "SREF" string pchg))
  18397. (setq string (replace-match sref t t string)))))
  18398. string))
  18399. (defun org-sublist (list start end)
  18400. "Return a section of LIST, from START to END.
  18401. Counting starts at 1."
  18402. (let (rtn (c start))
  18403. (setq list (nthcdr (1- start) list))
  18404. (while (and list (<= c end))
  18405. (push (pop list) rtn)
  18406. (setq c (1+ c)))
  18407. (nreverse rtn)))
  18408. (defun org-find-base-buffer-visiting (file)
  18409. "Like `find-buffer-visiting' but always return the base buffer and
  18410. not an indirect buffer."
  18411. (let ((buf (or (get-file-buffer file)
  18412. (find-buffer-visiting file))))
  18413. (if buf
  18414. (or (buffer-base-buffer buf) buf)
  18415. nil)))
  18416. (defun org-image-file-name-regexp (&optional extensions)
  18417. "Return regexp matching the file names of images.
  18418. If EXTENSIONS is given, only match these."
  18419. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  18420. (image-file-name-regexp)
  18421. (let ((image-file-name-extensions
  18422. (or extensions
  18423. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  18424. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  18425. (concat "\\."
  18426. (regexp-opt (nconc (mapcar 'upcase
  18427. image-file-name-extensions)
  18428. image-file-name-extensions)
  18429. t)
  18430. "\\'"))))
  18431. (defun org-file-image-p (file &optional extensions)
  18432. "Return non-nil if FILE is an image."
  18433. (save-match-data
  18434. (string-match (org-image-file-name-regexp extensions) file)))
  18435. (defun org-get-cursor-date ()
  18436. "Return the date at cursor in as a time.
  18437. This works in the calendar and in the agenda, anywhere else it just
  18438. returns the current time."
  18439. (let (date day defd)
  18440. (cond
  18441. ((eq major-mode 'calendar-mode)
  18442. (setq date (calendar-cursor-to-date)
  18443. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  18444. ((eq major-mode 'org-agenda-mode)
  18445. (setq day (get-text-property (point) 'day))
  18446. (if day
  18447. (setq date (calendar-gregorian-from-absolute day)
  18448. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
  18449. (nth 2 date))))))
  18450. (or defd (current-time))))
  18451. (defvar org-agenda-action-marker (make-marker)
  18452. "Marker pointing to the entry for the next agenda action.")
  18453. (defun org-mark-entry-for-agenda-action ()
  18454. "Mark the current entry as target of an agenda action.
  18455. Agenda actions are actions executed from the agenda with the key `k',
  18456. which make use of the date at the cursor."
  18457. (interactive)
  18458. (move-marker org-agenda-action-marker
  18459. (save-excursion (org-back-to-heading t) (point))
  18460. (current-buffer))
  18461. (message
  18462. "Entry marked for action; press `k' at desired date in agenda or calendar"))
  18463. (defun org-mark-subtree ()
  18464. "Mark the current subtree.
  18465. This puts point at the start of the current subtree, and mark at the end.
  18466. If point is in an inline task, mark that task instead."
  18467. (interactive)
  18468. (let ((inline-task-p
  18469. (and (featurep 'org-inlinetask)
  18470. (org-inlinetask-in-task-p)))
  18471. (beg))
  18472. ;; Get beginning of subtree
  18473. (cond
  18474. (inline-task-p (org-inlinetask-goto-beginning))
  18475. ((org-at-heading-p) (beginning-of-line))
  18476. (t (org-with-limited-levels (outline-previous-visible-heading 1))))
  18477. (setq beg (point))
  18478. ;; Get end of it
  18479. (if inline-task-p
  18480. (org-inlinetask-goto-end)
  18481. (org-end-of-subtree))
  18482. ;; Mark zone
  18483. (push-mark (point) nil t)
  18484. (goto-char beg)))
  18485. ;;; Paragraph filling stuff.
  18486. ;; We want this to be just right, so use the full arsenal.
  18487. (defun org-indent-line-function ()
  18488. "Indent line depending on context."
  18489. (interactive)
  18490. (let* ((pos (point))
  18491. (itemp (org-at-item-p))
  18492. (case-fold-search t)
  18493. (org-drawer-regexp (or org-drawer-regexp "\000"))
  18494. (inline-task-p (and (featurep 'org-inlinetask)
  18495. (org-inlinetask-in-task-p)))
  18496. (inline-re (and inline-task-p
  18497. (org-inlinetask-outline-regexp)))
  18498. column)
  18499. (beginning-of-line 1)
  18500. (cond
  18501. ;; Comments
  18502. ((looking-at "# ") (setq column 0))
  18503. ;; Headings
  18504. ((looking-at org-outline-regexp) (setq column 0))
  18505. ;; Included files
  18506. ((looking-at "#\\+include:") (setq column 0))
  18507. ;; Footnote definition
  18508. ((looking-at org-footnote-definition-re) (setq column 0))
  18509. ;; Literal examples
  18510. ((looking-at "[ \t]*:\\( \\|$\\)")
  18511. (setq column (org-get-indentation))) ; do nothing
  18512. ;; Lists
  18513. ((ignore-errors (goto-char (org-in-item-p)))
  18514. (setq column (if itemp
  18515. (org-get-indentation)
  18516. (org-list-item-body-column (point))))
  18517. (goto-char pos))
  18518. ;; Drawers
  18519. ((and (looking-at "[ \t]*:END:")
  18520. (save-excursion (re-search-backward org-drawer-regexp nil t)))
  18521. (save-excursion
  18522. (goto-char (1- (match-beginning 1)))
  18523. (setq column (current-column))))
  18524. ;; Special blocks
  18525. ((and (looking-at "[ \t]*#\\+end_\\([a-z]+\\)")
  18526. (save-excursion
  18527. (re-search-backward
  18528. (concat "^[ \t]*#\\+begin_" (downcase (match-string 1))) nil t)))
  18529. (setq column (org-get-indentation (match-string 0))))
  18530. ((and (not (looking-at "[ \t]*#\\+begin_"))
  18531. (org-between-regexps-p "^[ \t]*#\\+begin_" "[ \t]*#\\+end_"))
  18532. (save-excursion
  18533. (re-search-backward "^[ \t]*#\\+begin_\\([a-z]+\\)" nil t))
  18534. (setq column
  18535. (cond ((equal (downcase (match-string 1)) "src")
  18536. ;; src blocks: let `org-edit-src-exit' handle them
  18537. (org-get-indentation))
  18538. ((equal (downcase (match-string 1)) "example")
  18539. (max (org-get-indentation)
  18540. (org-get-indentation (match-string 0))))
  18541. (t
  18542. (org-get-indentation (match-string 0))))))
  18543. ;; This line has nothing special, look at the previous relevant
  18544. ;; line to compute indentation
  18545. (t
  18546. (beginning-of-line 0)
  18547. (while (and (not (bobp))
  18548. (not (looking-at org-drawer-regexp))
  18549. ;; When point started in an inline task, do not move
  18550. ;; above task starting line.
  18551. (not (and inline-task-p (looking-at inline-re)))
  18552. ;; Skip drawers, blocks, empty lines, verbatim,
  18553. ;; comments, tables, footnotes definitions, lists,
  18554. ;; inline tasks.
  18555. (or (and (looking-at "[ \t]*:END:")
  18556. (re-search-backward org-drawer-regexp nil t))
  18557. (and (looking-at "[ \t]*#\\+end_")
  18558. (re-search-backward "[ \t]*#\\+begin_"nil t))
  18559. (looking-at "[ \t]*[\n:#|]")
  18560. (looking-at org-footnote-definition-re)
  18561. (and (ignore-errors (goto-char (org-in-item-p)))
  18562. (goto-char
  18563. (org-list-get-top-point (org-list-struct))))
  18564. (and (not inline-task-p)
  18565. (featurep 'org-inlinetask)
  18566. (org-inlinetask-in-task-p)
  18567. (or (org-inlinetask-goto-beginning) t))))
  18568. (beginning-of-line 0))
  18569. (cond
  18570. ;; There was an heading above.
  18571. ((looking-at "\\*+[ \t]+")
  18572. (if (not org-adapt-indentation)
  18573. (setq column 0)
  18574. (goto-char (match-end 0))
  18575. (setq column (current-column))))
  18576. ;; A drawer had started and is unfinished
  18577. ((looking-at org-drawer-regexp)
  18578. (goto-char (1- (match-beginning 1)))
  18579. (setq column (current-column)))
  18580. ;; Else, nothing noticeable found: get indentation and go on.
  18581. (t (setq column (org-get-indentation))))))
  18582. ;; Now apply indentation and move cursor accordingly
  18583. (goto-char pos)
  18584. (if (<= (current-column) (current-indentation))
  18585. (org-indent-line-to column)
  18586. (save-excursion (org-indent-line-to column)))
  18587. ;; Special polishing for properties, see `org-property-format'
  18588. (setq column (current-column))
  18589. (beginning-of-line 1)
  18590. (if (looking-at
  18591. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  18592. (replace-match (concat (match-string 1)
  18593. (format org-property-format
  18594. (match-string 2) (match-string 3)))
  18595. t t))
  18596. (org-move-to-column column)))
  18597. (defun org-indent-drawer ()
  18598. "Indent the drawer at point."
  18599. (interactive)
  18600. (let ((p (point))
  18601. (e (and (save-excursion (re-search-forward ":END:" nil t))
  18602. (match-end 0)))
  18603. (folded
  18604. (save-excursion
  18605. (end-of-line)
  18606. (when (overlays-at (point))
  18607. (member 'invisible (overlay-properties
  18608. (car (overlays-at (point)))))))))
  18609. (when folded (org-cycle))
  18610. (indent-for-tab-command)
  18611. (while (and (move-beginning-of-line 2) (< (point) e))
  18612. (indent-for-tab-command))
  18613. (goto-char p)
  18614. (when folded (org-cycle)))
  18615. (message "Drawer at point indented"))
  18616. (defun org-indent-block ()
  18617. "Indent the block at point."
  18618. (interactive)
  18619. (let ((p (point))
  18620. (case-fold-search t)
  18621. (e (and (save-excursion (re-search-forward "#\\+end_?\\(?:[a-z]+\\)?" nil t))
  18622. (match-end 0)))
  18623. (folded
  18624. (save-excursion
  18625. (end-of-line)
  18626. (when (overlays-at (point))
  18627. (member 'invisible (overlay-properties
  18628. (car (overlays-at (point)))))))))
  18629. (when folded (org-cycle))
  18630. (indent-for-tab-command)
  18631. (while (and (move-beginning-of-line 2) (< (point) e))
  18632. (indent-for-tab-command))
  18633. (goto-char p)
  18634. (when folded (org-cycle)))
  18635. (message "Block at point indented"))
  18636. (defvar org-adaptive-fill-regexp-backup adaptive-fill-regexp
  18637. "Variable to store copy of `adaptive-fill-regexp'.
  18638. Since `adaptive-fill-regexp' is set to never match, we need to
  18639. store a backup of its value before entering `org-mode' so that
  18640. the functionality can be provided as a fall-back.")
  18641. (defun org-set-autofill-regexps ()
  18642. (interactive)
  18643. ;; In the paragraph separator we include headlines, because filling
  18644. ;; text in a line directly attached to a headline would otherwise
  18645. ;; fill the headline as well.
  18646. (org-set-local 'comment-start-skip "^#+[ \t]*")
  18647. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|#]")
  18648. ;; The paragraph starter includes hand-formatted lists.
  18649. (org-set-local
  18650. 'paragraph-start
  18651. (concat
  18652. "\f" "\\|"
  18653. "[ ]*$" "\\|"
  18654. org-outline-regexp "\\|"
  18655. "[ \t]*#" "\\|"
  18656. (org-item-re) "\\|"
  18657. "[ \t]*[:|]" "\\|"
  18658. "\\$\\$" "\\|"
  18659. "\\\\\\(begin\\|end\\|[][]\\)"))
  18660. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  18661. ;; But only if the user has not turned off tables or fixed-width regions
  18662. (org-set-local
  18663. 'auto-fill-inhibit-regexp
  18664. (concat org-outline-regexp
  18665. "\\|#\\+"
  18666. "\\|[ \t]*" org-keyword-time-regexp
  18667. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  18668. (concat
  18669. "\\|[ \t]*["
  18670. (if org-enable-table-editor "|" "")
  18671. (if org-enable-fixed-width-editor ":" "")
  18672. "]"))))
  18673. ;; We use our own fill-paragraph function, to make sure that tables
  18674. ;; and fixed-width regions are not wrapped. That function will pass
  18675. ;; through to `fill-paragraph' when appropriate.
  18676. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  18677. ;; Prevent auto-fill from inserting unwanted new items.
  18678. (if (boundp 'fill-nobreak-predicate)
  18679. (org-set-local
  18680. 'fill-nobreak-predicate
  18681. (org-uniquify
  18682. (append fill-nobreak-predicate
  18683. '(org-fill-item-nobreak-p org-fill-line-break-nobreak-p)))))
  18684. ;; Adaptive filling: To get full control, first make sure that
  18685. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  18686. (unless (local-variable-p 'adaptive-fill-regexp (current-buffer))
  18687. (org-set-local 'org-adaptive-fill-regexp-backup
  18688. adaptive-fill-regexp))
  18689. (org-set-local 'adaptive-fill-regexp "\000")
  18690. (org-set-local 'normal-auto-fill-function 'org-auto-fill-function)
  18691. (org-set-local 'adaptive-fill-function
  18692. 'org-adaptive-fill-function)
  18693. (org-set-local
  18694. 'align-mode-rules-list
  18695. '((org-in-buffer-settings
  18696. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  18697. (modes . '(org-mode))))))
  18698. (defun org-fill-item-nobreak-p ()
  18699. "Non-nil when a line break at point would insert a new item."
  18700. (and (looking-at (org-item-re)) (org-list-in-valid-context-p)))
  18701. (defun org-fill-line-break-nobreak-p ()
  18702. "Non-nil when a line break at point would create an Org line break."
  18703. (save-excursion
  18704. (skip-chars-backward "[ \t]")
  18705. (skip-chars-backward "\\\\")
  18706. (looking-at "\\\\\\\\\\($\\|[^\\\\]\\)")))
  18707. (defun org-fill-paragraph (&optional justify)
  18708. "Re-align a table, pass through to fill-paragraph if no table."
  18709. (let ((table-p (org-at-table-p))
  18710. (table.el-p (org-at-table.el-p))
  18711. (itemp (org-in-item-p)))
  18712. (cond ((and (equal (char-after (point-at-bol)) ?*)
  18713. (save-excursion (goto-char (point-at-bol))
  18714. (looking-at org-outline-regexp)))
  18715. t) ; skip headlines
  18716. (table.el-p t) ; skip table.el tables
  18717. (table-p (org-table-align) t) ; align Org tables
  18718. (itemp ; align text in items
  18719. (let* ((struct (save-excursion (goto-char itemp)
  18720. (org-list-struct)))
  18721. (parents (org-list-parents-alist struct))
  18722. (children (org-list-get-children itemp struct parents))
  18723. beg end prev next prefix)
  18724. ;; Determine in which part of item point is: before
  18725. ;; first child, after last child, between two
  18726. ;; sub-lists, or simply in item if there's no child.
  18727. (cond
  18728. ((not children)
  18729. (setq prefix (make-string (org-list-item-body-column itemp) ?\ )
  18730. beg itemp
  18731. end (org-list-get-item-end itemp struct)))
  18732. ((< (point) (setq next (car children)))
  18733. (setq prefix (make-string (org-list-item-body-column itemp) ?\ )
  18734. beg itemp
  18735. end next))
  18736. ((> (point) (setq prev (car (last children))))
  18737. (setq beg (org-list-get-item-end prev struct)
  18738. end (org-list-get-item-end itemp struct)
  18739. prefix (save-excursion
  18740. (goto-char beg)
  18741. (skip-chars-forward " \t")
  18742. (make-string (current-column) ?\ ))))
  18743. (t (catch 'exit
  18744. (while (setq next (pop children))
  18745. (if (> (point) next)
  18746. (setq prev next)
  18747. (setq beg (org-list-get-item-end prev struct)
  18748. end next
  18749. prefix (save-excursion
  18750. (goto-char beg)
  18751. (skip-chars-forward " \t")
  18752. (make-string (current-column) ?\ )))
  18753. (throw 'exit nil))))))
  18754. ;; Use `fill-paragraph' with buffer narrowed to item
  18755. ;; without any child, and with our computed PREFIX.
  18756. (flet ((fill-context-prefix (from to &optional flr) prefix))
  18757. (save-restriction
  18758. (narrow-to-region beg end)
  18759. (save-excursion (fill-paragraph justify)))) t))
  18760. ;; Special case where point is not in a list but is on
  18761. ;; a paragraph adjacent to a list: make sure this paragraph
  18762. ;; doesn't get merged with the end of the list by narrowing
  18763. ;; buffer first.
  18764. ((save-excursion (forward-paragraph -1)
  18765. (setq itemp (org-in-item-p)))
  18766. (let ((struct (save-excursion (goto-char itemp)
  18767. (org-list-struct))))
  18768. (save-restriction
  18769. (narrow-to-region (org-list-get-bottom-point struct)
  18770. (save-excursion (forward-paragraph 1)
  18771. (point)))
  18772. (fill-paragraph justify) t)))
  18773. ;; Else simply call `fill-paragraph'.
  18774. (t nil))))
  18775. ;; For reference, this is the default value of adaptive-fill-regexp
  18776. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  18777. (defun org-adaptive-fill-function ()
  18778. "Return a fill prefix for org-mode files."
  18779. (let (itemp)
  18780. (save-excursion
  18781. (cond
  18782. ;; Comment line
  18783. ((looking-at "#[ \t]+")
  18784. (match-string-no-properties 0))
  18785. ;; Plain list item
  18786. ((org-at-item-p)
  18787. (make-string (org-list-item-body-column (point-at-bol)) ?\ ))
  18788. ;; Point is in a list after `backward-paragraph': original
  18789. ;; point wasn't in the list, or filling would have been taken
  18790. ;; care of by `org-auto-fill-function', but the list and the
  18791. ;; real paragraph are not separated by a blank line. Thus, move
  18792. ;; point after the list to go back to real paragraph and
  18793. ;; determine fill-prefix.
  18794. ((setq itemp (org-in-item-p))
  18795. (goto-char itemp)
  18796. (let* ((struct (org-list-struct))
  18797. (bottom (org-list-get-bottom-point struct)))
  18798. (goto-char bottom)
  18799. (make-string (org-get-indentation) ?\ )))
  18800. ;; Other text
  18801. ((looking-at org-adaptive-fill-regexp-backup)
  18802. (match-string-no-properties 0))))))
  18803. (defun org-auto-fill-function ()
  18804. "Auto-fill function."
  18805. (let (itemp prefix)
  18806. ;; When in a list, compute an appropriate fill-prefix and make
  18807. ;; sure it will be used by `do-auto-fill'.
  18808. (if (setq itemp (org-in-item-p))
  18809. (progn
  18810. (setq prefix (make-string (org-list-item-body-column itemp) ?\ ))
  18811. (flet ((fill-context-prefix (from to &optional flr) prefix))
  18812. (do-auto-fill)))
  18813. ;; Else just use `do-auto-fill'.
  18814. (do-auto-fill))))
  18815. ;;; Other stuff.
  18816. (defun org-toggle-fixed-width-section (arg)
  18817. "Toggle the fixed-width export.
  18818. If there is no active region, the QUOTE keyword at the current headline is
  18819. inserted or removed. When present, it causes the text between this headline
  18820. and the next to be exported as fixed-width text, and unmodified.
  18821. If there is an active region, this command adds or removes a colon as the
  18822. first character of this line. If the first character of a line is a colon,
  18823. this line is also exported in fixed-width font."
  18824. (interactive "P")
  18825. (let* ((cc 0)
  18826. (regionp (org-region-active-p))
  18827. (beg (if regionp (region-beginning) (point)))
  18828. (end (if regionp (region-end)))
  18829. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  18830. (case-fold-search nil)
  18831. (re "[ \t]*\\(:\\(?: \\|$\\)\\)")
  18832. off)
  18833. (if regionp
  18834. (save-excursion
  18835. (goto-char beg)
  18836. (setq cc (current-column))
  18837. (beginning-of-line 1)
  18838. (setq off (looking-at re))
  18839. (while (> nlines 0)
  18840. (setq nlines (1- nlines))
  18841. (beginning-of-line 1)
  18842. (cond
  18843. (arg
  18844. (org-move-to-column cc t)
  18845. (insert ": \n")
  18846. (forward-line -1))
  18847. ((and off (looking-at re))
  18848. (replace-match "" t t nil 1))
  18849. ((not off) (org-move-to-column cc t) (insert ": ")))
  18850. (forward-line 1)))
  18851. (save-excursion
  18852. (org-back-to-heading)
  18853. (cond
  18854. ((looking-at (format org-heading-keyword-regexp-format
  18855. org-quote-string))
  18856. (goto-char (match-end 1))
  18857. (looking-at (concat " +" org-quote-string))
  18858. (replace-match "" t t)
  18859. (when (eolp) (insert " ")))
  18860. ((looking-at org-outline-regexp)
  18861. (goto-char (match-end 0))
  18862. (insert org-quote-string " ")))))))
  18863. (defun org-reftex-citation ()
  18864. "Use reftex-citation to insert a citation into the buffer.
  18865. This looks for a line like
  18866. #+BIBLIOGRAPHY: foo plain option:-d
  18867. and derives from it that foo.bib is the bibliography file relevant
  18868. for this document. It then installs the necessary environment for RefTeX
  18869. to work in this buffer and calls `reftex-citation' to insert a citation
  18870. into the buffer.
  18871. Export of such citations to both LaTeX and HTML is handled by the contributed
  18872. package org-exp-bibtex by Taru Karttunen."
  18873. (interactive)
  18874. (let ((reftex-docstruct-symbol 'rds)
  18875. (reftex-cite-format "\\cite{%l}")
  18876. rds bib)
  18877. (save-excursion
  18878. (save-restriction
  18879. (widen)
  18880. (let ((case-fold-search t)
  18881. (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
  18882. (if (not (save-excursion
  18883. (or (re-search-forward re nil t)
  18884. (re-search-backward re nil t))))
  18885. (error "No bibliography defined in file")
  18886. (setq bib (concat (match-string 1) ".bib")
  18887. rds (list (list 'bib bib)))))))
  18888. (call-interactively 'reftex-citation)))
  18889. ;;;; Functions extending outline functionality
  18890. (defun org-beginning-of-line (&optional arg)
  18891. "Go to the beginning of the current line. If that is invisible, continue
  18892. to a visible line beginning. This makes the function of C-a more intuitive.
  18893. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  18894. first attempt, and only move to after the tags when the cursor is already
  18895. beyond the end of the headline."
  18896. (interactive "P")
  18897. (let ((pos (point))
  18898. (special (if (consp org-special-ctrl-a/e)
  18899. (car org-special-ctrl-a/e)
  18900. org-special-ctrl-a/e))
  18901. refpos)
  18902. (if (org-bound-and-true-p line-move-visual)
  18903. (beginning-of-visual-line 1)
  18904. (beginning-of-line 1))
  18905. (if (and arg (fboundp 'move-beginning-of-line))
  18906. (call-interactively 'move-beginning-of-line)
  18907. (if (bobp)
  18908. nil
  18909. (backward-char 1)
  18910. (if (org-truely-invisible-p)
  18911. (while (and (not (bobp)) (org-truely-invisible-p))
  18912. (backward-char 1)
  18913. (beginning-of-line 1))
  18914. (forward-char 1))))
  18915. (when special
  18916. (cond
  18917. ((and (looking-at org-complex-heading-regexp)
  18918. (= (char-after (match-end 1)) ?\ ))
  18919. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  18920. (point-at-eol)))
  18921. (goto-char
  18922. (if (eq special t)
  18923. (cond ((> pos refpos) refpos)
  18924. ((= pos (point)) refpos)
  18925. (t (point)))
  18926. (cond ((> pos (point)) (point))
  18927. ((not (eq last-command this-command)) (point))
  18928. (t refpos)))))
  18929. ((org-at-item-p)
  18930. ;; Being at an item and not looking at an the item means point
  18931. ;; was previously moved to beginning of a visual line, whiche
  18932. ;; doesn't contain the item. Therefore, do nothing special,
  18933. ;; just stay here.
  18934. (when (looking-at org-list-full-item-re)
  18935. ;; Set special position at first white space character after
  18936. ;; bullet, and check-box, if any.
  18937. (let ((after-bullet
  18938. (let ((box (match-end 3)))
  18939. (if (not box) (match-end 1)
  18940. (let ((after (char-after box)))
  18941. (if (and after (= after ? )) (1+ box) box))))))
  18942. ;; Special case: Move point to special position when
  18943. ;; currently after it or at beginning of line.
  18944. (if (eq special t)
  18945. (when (or (> pos after-bullet) (= (point) pos))
  18946. (goto-char after-bullet))
  18947. ;; Reversed case: Move point to special position when
  18948. ;; point was already at beginning of line and command is
  18949. ;; repeated.
  18950. (when (and (= (point) pos) (eq last-command this-command))
  18951. (goto-char after-bullet))))))))
  18952. (org-no-warnings
  18953. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  18954. (defun org-end-of-line (&optional arg)
  18955. "Go to the end of the line.
  18956. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  18957. first attempt, and only move to after the tags when the cursor is already
  18958. beyond the end of the headline."
  18959. (interactive "P")
  18960. (let ((special (if (consp org-special-ctrl-a/e)
  18961. (cdr org-special-ctrl-a/e)
  18962. org-special-ctrl-a/e)))
  18963. (cond
  18964. ((or (not special) arg
  18965. (not (or (org-at-heading-p) (org-at-item-p) (org-at-drawer-p))))
  18966. (call-interactively
  18967. (cond ((org-bound-and-true-p line-move-visual) 'end-of-visual-line)
  18968. ((fboundp 'move-end-of-line) 'move-end-of-line)
  18969. (t 'end-of-line))))
  18970. ((org-at-heading-p)
  18971. (let ((pos (point)))
  18972. (beginning-of-line 1)
  18973. (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*\\)?$"))
  18974. (if (eq special t)
  18975. (if (or (< pos (match-beginning 1))
  18976. (= pos (match-end 0)))
  18977. (goto-char (match-beginning 1))
  18978. (goto-char (match-end 0)))
  18979. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  18980. (goto-char (match-end 0))
  18981. (goto-char (match-beginning 1))))
  18982. (call-interactively (if (fboundp 'move-end-of-line)
  18983. 'move-end-of-line
  18984. 'end-of-line)))))
  18985. ((org-at-drawer-p)
  18986. (move-end-of-line 1)
  18987. (when (overlays-at (1- (point))) (backward-char 1)))
  18988. ;; At an item: Move before any hidden text.
  18989. (t (call-interactively
  18990. (cond ((org-bound-and-true-p line-move-visual) 'end-of-visual-line)
  18991. ((fboundp 'move-end-of-line) 'move-end-of-line)
  18992. (t 'end-of-line)))))
  18993. (org-no-warnings
  18994. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  18995. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  18996. (define-key org-mode-map "\C-e" 'org-end-of-line)
  18997. (defun org-backward-sentence (&optional arg)
  18998. "Go to beginning of sentence, or beginning of table field.
  18999. This will call `backward-sentence' or `org-table-beginning-of-field',
  19000. depending on context."
  19001. (interactive "P")
  19002. (cond
  19003. ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
  19004. (t (call-interactively 'backward-sentence))))
  19005. (defun org-forward-sentence (&optional arg)
  19006. "Go to end of sentence, or end of table field.
  19007. This will call `forward-sentence' or `org-table-end-of-field',
  19008. depending on context."
  19009. (interactive "P")
  19010. (cond
  19011. ((org-at-table-p) (call-interactively 'org-table-end-of-field))
  19012. (t (call-interactively 'forward-sentence))))
  19013. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  19014. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  19015. (defun org-kill-line (&optional arg)
  19016. "Kill line, to tags or end of line."
  19017. (interactive "P")
  19018. (cond
  19019. ((or (not org-special-ctrl-k)
  19020. (bolp)
  19021. (not (org-at-heading-p)))
  19022. (if (and (get-char-property (min (point-max) (point-at-eol)) 'invisible)
  19023. org-ctrl-k-protect-subtree)
  19024. (if (or (eq org-ctrl-k-protect-subtree 'error)
  19025. (not (y-or-n-p "Kill hidden subtree along with headline? ")))
  19026. (error "C-k aborted - would kill hidden subtree")))
  19027. (call-interactively
  19028. (if visual-line-mode 'kill-visual-line 'kill-line)))
  19029. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$"))
  19030. (kill-region (point) (match-beginning 1))
  19031. (org-set-tags nil t))
  19032. (t (kill-region (point) (point-at-eol)))))
  19033. (define-key org-mode-map "\C-k" 'org-kill-line)
  19034. (defun org-yank (&optional arg)
  19035. "Yank. If the kill is a subtree, treat it specially.
  19036. This command will look at the current kill and check if is a single
  19037. subtree, or a series of subtrees[1]. If it passes the test, and if the
  19038. cursor is at the beginning of a line or after the stars of a currently
  19039. empty headline, then the yank is handled specially. How exactly depends
  19040. on the value of the following variables, both set by default.
  19041. org-yank-folded-subtrees
  19042. When set, the subtree(s) will be folded after insertion, but only
  19043. if doing so would now swallow text after the yanked text.
  19044. org-yank-adjusted-subtrees
  19045. When set, the subtree will be promoted or demoted in order to
  19046. fit into the local outline tree structure, which means that the level
  19047. will be adjusted so that it becomes the smaller one of the two
  19048. *visible* surrounding headings.
  19049. Any prefix to this command will cause `yank' to be called directly with
  19050. no special treatment. In particular, a simple \\[universal-argument] prefix \
  19051. will just
  19052. plainly yank the text as it is.
  19053. \[1] The test checks if the first non-white line is a heading
  19054. and if there are no other headings with fewer stars."
  19055. (interactive "P")
  19056. (org-yank-generic 'yank arg))
  19057. (defun org-yank-generic (command arg)
  19058. "Perform some yank-like command.
  19059. This function implements the behavior described in the `org-yank'
  19060. documentation. However, it has been generalized to work for any
  19061. interactive command with similar behavior."
  19062. ;; pretend to be command COMMAND
  19063. (setq this-command command)
  19064. (if arg
  19065. (call-interactively command)
  19066. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  19067. (and (org-kill-is-subtree-p)
  19068. (or (bolp)
  19069. (and (looking-at "[ \t]*$")
  19070. (string-match
  19071. "\\`\\*+\\'"
  19072. (buffer-substring (point-at-bol) (point)))))))
  19073. swallowp)
  19074. (cond
  19075. ((and subtreep org-yank-folded-subtrees)
  19076. (let ((beg (point))
  19077. end)
  19078. (if (and subtreep org-yank-adjusted-subtrees)
  19079. (org-paste-subtree nil nil 'for-yank)
  19080. (call-interactively command))
  19081. (setq end (point))
  19082. (goto-char beg)
  19083. (when (and (bolp) subtreep
  19084. (not (setq swallowp
  19085. (org-yank-folding-would-swallow-text beg end))))
  19086. (org-with-limited-levels
  19087. (or (looking-at org-outline-regexp)
  19088. (re-search-forward org-outline-regexp-bol end t))
  19089. (while (and (< (point) end) (looking-at org-outline-regexp))
  19090. (hide-subtree)
  19091. (org-cycle-show-empty-lines 'folded)
  19092. (condition-case nil
  19093. (outline-forward-same-level 1)
  19094. (error (goto-char end))))))
  19095. (when swallowp
  19096. (message
  19097. "Inserted text not folded because that would swallow text"))
  19098. (goto-char end)
  19099. (skip-chars-forward " \t\n\r")
  19100. (beginning-of-line 1)
  19101. (push-mark beg 'nomsg)))
  19102. ((and subtreep org-yank-adjusted-subtrees)
  19103. (let ((beg (point-at-bol)))
  19104. (org-paste-subtree nil nil 'for-yank)
  19105. (push-mark beg 'nomsg)))
  19106. (t
  19107. (call-interactively command))))))
  19108. (defun org-yank-folding-would-swallow-text (beg end)
  19109. "Would hide-subtree at BEG swallow any text after END?"
  19110. (let (level)
  19111. (org-with-limited-levels
  19112. (save-excursion
  19113. (goto-char beg)
  19114. (when (or (looking-at org-outline-regexp)
  19115. (re-search-forward org-outline-regexp-bol end t))
  19116. (setq level (org-outline-level)))
  19117. (goto-char end)
  19118. (skip-chars-forward " \t\r\n\v\f")
  19119. (if (or (eobp)
  19120. (and (bolp) (looking-at org-outline-regexp)
  19121. (<= (org-outline-level) level)))
  19122. nil ; Nothing would be swallowed
  19123. t))))) ; something would swallow
  19124. (define-key org-mode-map "\C-y" 'org-yank)
  19125. (defun org-truely-invisible-p ()
  19126. "Check if point is at a character currently not visible.
  19127. This version does not only check the character property, but also
  19128. `visible-mode'."
  19129. ;; Early versions of noutline don't have `outline-invisible-p'.
  19130. (if (org-bound-and-true-p visible-mode)
  19131. nil
  19132. (outline-invisible-p)))
  19133. (defun org-invisible-p2 ()
  19134. "Check if point is at a character currently not visible."
  19135. (save-excursion
  19136. (if (and (eolp) (not (bobp))) (backward-char 1))
  19137. ;; Early versions of noutline don't have `outline-invisible-p'.
  19138. (outline-invisible-p)))
  19139. (defun org-back-to-heading (&optional invisible-ok)
  19140. "Call `outline-back-to-heading', but provide a better error message."
  19141. (condition-case nil
  19142. (outline-back-to-heading invisible-ok)
  19143. (error (error "Before first headline at position %d in buffer %s"
  19144. (point) (current-buffer)))))
  19145. (defun org-beginning-of-defun ()
  19146. "Go to the beginning of the subtree, i.e. back to the heading."
  19147. (org-back-to-heading))
  19148. (defun org-end-of-defun ()
  19149. "Go to the end of the subtree."
  19150. (org-end-of-subtree nil t))
  19151. (defun org-before-first-heading-p ()
  19152. "Before first heading?"
  19153. (save-excursion
  19154. (end-of-line)
  19155. (null (re-search-backward org-outline-regexp-bol nil t))))
  19156. (defun org-at-heading-p (&optional ignored)
  19157. (outline-on-heading-p t))
  19158. ;; Compatibility alias with Org versions < 7.8.03
  19159. (defalias 'org-on-heading-p 'org-at-heading-p)
  19160. (defun org-at-drawer-p nil
  19161. "Is cursor at a drawer keyword?"
  19162. (save-excursion
  19163. (move-beginning-of-line 1)
  19164. (looking-at org-drawer-regexp)))
  19165. (defun org-at-block-p nil
  19166. "Is cursor at a block keyword?"
  19167. (save-excursion
  19168. (move-beginning-of-line 1)
  19169. (looking-at org-block-regexp)))
  19170. (defun org-point-at-end-of-empty-headline ()
  19171. "If point is at the end of an empty headline, return t, else nil.
  19172. If the heading only contains a TODO keyword, it is still still considered
  19173. empty."
  19174. (and (looking-at "[ \t]*$")
  19175. (save-excursion
  19176. (beginning-of-line 1)
  19177. (let ((case-fold-search nil))
  19178. (looking-at org-todo-line-regexp)))
  19179. (string= (match-string 3) "")))
  19180. (defun org-at-heading-or-item-p ()
  19181. (or (org-at-heading-p) (org-at-item-p)))
  19182. (defun org-at-target-p ()
  19183. (or (org-in-regexp org-radio-target-regexp)
  19184. (org-in-regexp org-target-regexp)))
  19185. ;; Compatibility alias with Org versions < 7.8.03
  19186. (defalias 'org-on-target-p 'org-at-target-p)
  19187. (defun org-up-heading-all (arg)
  19188. "Move to the heading line of which the present line is a subheading.
  19189. This function considers both visible and invisible heading lines.
  19190. With argument, move up ARG levels."
  19191. (if (fboundp 'outline-up-heading-all)
  19192. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  19193. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  19194. (defun org-up-heading-safe ()
  19195. "Move to the heading line of which the present line is a subheading.
  19196. This version will not throw an error. It will return the level of the
  19197. headline found, or nil if no higher level is found.
  19198. Also, this function will be a lot faster than `outline-up-heading',
  19199. because it relies on stars being the outline starters. This can really
  19200. make a significant difference in outlines with very many siblings."
  19201. (let (start-level re)
  19202. (org-back-to-heading t)
  19203. (setq start-level (funcall outline-level))
  19204. (if (equal start-level 1)
  19205. nil
  19206. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  19207. (if (re-search-backward re nil t)
  19208. (funcall outline-level)))))
  19209. (defun org-first-sibling-p ()
  19210. "Is this heading the first child of its parents?"
  19211. (interactive)
  19212. (let ((re org-outline-regexp-bol)
  19213. level l)
  19214. (unless (org-at-heading-p t)
  19215. (error "Not at a heading"))
  19216. (setq level (funcall outline-level))
  19217. (save-excursion
  19218. (if (not (re-search-backward re nil t))
  19219. t
  19220. (setq l (funcall outline-level))
  19221. (< l level)))))
  19222. (defun org-goto-sibling (&optional previous)
  19223. "Goto the next sibling, even if it is invisible.
  19224. When PREVIOUS is set, go to the previous sibling instead. Returns t
  19225. when a sibling was found. When none is found, return nil and don't
  19226. move point."
  19227. (let ((fun (if previous 're-search-backward 're-search-forward))
  19228. (pos (point))
  19229. (re org-outline-regexp-bol)
  19230. level l)
  19231. (when (condition-case nil (org-back-to-heading t) (error nil))
  19232. (setq level (funcall outline-level))
  19233. (catch 'exit
  19234. (or previous (forward-char 1))
  19235. (while (funcall fun re nil t)
  19236. (setq l (funcall outline-level))
  19237. (when (< l level) (goto-char pos) (throw 'exit nil))
  19238. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  19239. (goto-char pos)
  19240. nil))))
  19241. (defun org-show-siblings ()
  19242. "Show all siblings of the current headline."
  19243. (save-excursion
  19244. (while (org-goto-sibling) (org-flag-heading nil)))
  19245. (save-excursion
  19246. (while (org-goto-sibling 'previous)
  19247. (org-flag-heading nil))))
  19248. (defun org-goto-first-child ()
  19249. "Goto the first child, even if it is invisible.
  19250. Return t when a child was found. Otherwise don't move point and
  19251. return nil."
  19252. (let (level (pos (point)) (re org-outline-regexp-bol))
  19253. (when (condition-case nil (org-back-to-heading t) (error nil))
  19254. (setq level (outline-level))
  19255. (forward-char 1)
  19256. (if (and (re-search-forward re nil t) (> (outline-level) level))
  19257. (progn (goto-char (match-beginning 0)) t)
  19258. (goto-char pos) nil))))
  19259. (defun org-show-hidden-entry ()
  19260. "Show an entry where even the heading is hidden."
  19261. (save-excursion
  19262. (org-show-entry)))
  19263. (defun org-flag-heading (flag &optional entry)
  19264. "Flag the current heading. FLAG non-nil means make invisible.
  19265. When ENTRY is non-nil, show the entire entry."
  19266. (save-excursion
  19267. (org-back-to-heading t)
  19268. ;; Check if we should show the entire entry
  19269. (if entry
  19270. (progn
  19271. (org-show-entry)
  19272. (save-excursion
  19273. (and (outline-next-heading)
  19274. (org-flag-heading nil))))
  19275. (outline-flag-region (max (point-min) (1- (point)))
  19276. (save-excursion (outline-end-of-heading) (point))
  19277. flag))))
  19278. (defun org-get-next-sibling ()
  19279. "Move to next heading of the same level, and return point.
  19280. If there is no such heading, return nil.
  19281. This is like outline-next-sibling, but invisible headings are ok."
  19282. (let ((level (funcall outline-level)))
  19283. (outline-next-heading)
  19284. (while (and (not (eobp)) (> (funcall outline-level) level))
  19285. (outline-next-heading))
  19286. (if (or (eobp) (< (funcall outline-level) level))
  19287. nil
  19288. (point))))
  19289. (defun org-get-last-sibling ()
  19290. "Move to previous heading of the same level, and return point.
  19291. If there is no such heading, return nil."
  19292. (let ((opoint (point))
  19293. (level (funcall outline-level)))
  19294. (outline-previous-heading)
  19295. (when (and (/= (point) opoint) (outline-on-heading-p t))
  19296. (while (and (> (funcall outline-level) level)
  19297. (not (bobp)))
  19298. (outline-previous-heading))
  19299. (if (< (funcall outline-level) level)
  19300. nil
  19301. (point)))))
  19302. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  19303. ;; This contains an exact copy of the original function, but it uses
  19304. ;; `org-back-to-heading', to make it work also in invisible
  19305. ;; trees. And is uses an invisible-OK argument.
  19306. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  19307. ;; Furthermore, when used inside Org, finding the end of a large subtree
  19308. ;; with many children and grandchildren etc, this can be much faster
  19309. ;; than the outline version.
  19310. (org-back-to-heading invisible-OK)
  19311. (let ((first t)
  19312. (level (funcall outline-level)))
  19313. (if (and (eq major-mode 'org-mode) (< level 1000))
  19314. ;; A true heading (not a plain list item), in Org-mode
  19315. ;; This means we can easily find the end by looking
  19316. ;; only for the right number of stars. Using a regexp to do
  19317. ;; this is so much faster than using a Lisp loop.
  19318. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  19319. (forward-char 1)
  19320. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  19321. ;; something else, do it the slow way
  19322. (while (and (not (eobp))
  19323. (or first (> (funcall outline-level) level)))
  19324. (setq first nil)
  19325. (outline-next-heading)))
  19326. (unless to-heading
  19327. (if (memq (preceding-char) '(?\n ?\^M))
  19328. (progn
  19329. ;; Go to end of line before heading
  19330. (forward-char -1)
  19331. (if (memq (preceding-char) '(?\n ?\^M))
  19332. ;; leave blank line before heading
  19333. (forward-char -1))))))
  19334. (point))
  19335. (defadvice outline-end-of-subtree (around prefer-org-version activate compile)
  19336. "Use Org version in org-mode, for dramatic speed-up."
  19337. (if (eq major-mode 'org-mode)
  19338. (progn
  19339. (org-end-of-subtree nil t)
  19340. (unless (eobp) (backward-char 1)))
  19341. ad-do-it))
  19342. (defun org-end-of-meta-data-and-drawers ()
  19343. "Jump to the first text after meta data and drawers in the current entry.
  19344. This will move over empty lines, lines with planning time stamps,
  19345. clocking lines, and drawers."
  19346. (org-back-to-heading t)
  19347. (let ((end (save-excursion (outline-next-heading) (point)))
  19348. (re (concat "\\(" org-drawer-regexp "\\)"
  19349. "\\|" "[ \t]*" org-keyword-time-regexp)))
  19350. (forward-line 1)
  19351. (while (re-search-forward re end t)
  19352. (if (not (match-end 1))
  19353. ;; empty or planning line
  19354. (forward-line 1)
  19355. ;; a drawer, find the end
  19356. (re-search-forward "^[ \t]*:END:" end 'move)
  19357. (forward-line 1)))
  19358. (and (re-search-forward "[^\n]" nil t) (backward-char 1))
  19359. (point)))
  19360. (defun org-forward-same-level (arg &optional invisible-ok)
  19361. "Move forward to the arg'th subheading at same level as this one.
  19362. Stop at the first and last subheadings of a superior heading.
  19363. Normally this only looks at visible headings, but when INVISIBLE-OK is non-nil
  19364. it wil also look at invisible ones."
  19365. (interactive "p")
  19366. (org-back-to-heading invisible-ok)
  19367. (org-at-heading-p)
  19368. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  19369. (re (format "^\\*\\{1,%d\\} " level))
  19370. l)
  19371. (forward-char 1)
  19372. (while (> arg 0)
  19373. (while (and (re-search-forward re nil 'move)
  19374. (setq l (- (match-end 0) (match-beginning 0) 1))
  19375. (= l level)
  19376. (not invisible-ok)
  19377. (progn (backward-char 1) (outline-invisible-p)))
  19378. (if (< l level) (setq arg 1)))
  19379. (setq arg (1- arg)))
  19380. (beginning-of-line 1)))
  19381. (defun org-backward-same-level (arg &optional invisible-ok)
  19382. "Move backward to the arg'th subheading at same level as this one.
  19383. Stop at the first and last subheadings of a superior heading."
  19384. (interactive "p")
  19385. (org-back-to-heading)
  19386. (org-at-heading-p)
  19387. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  19388. (re (format "^\\*\\{1,%d\\} " level))
  19389. l)
  19390. (while (> arg 0)
  19391. (while (and (re-search-backward re nil 'move)
  19392. (setq l (- (match-end 0) (match-beginning 0) 1))
  19393. (= l level)
  19394. (not invisible-ok)
  19395. (outline-invisible-p))
  19396. (if (< l level) (setq arg 1)))
  19397. (setq arg (1- arg)))))
  19398. (defun org-show-subtree ()
  19399. "Show everything after this heading at deeper levels."
  19400. (outline-flag-region
  19401. (point)
  19402. (save-excursion
  19403. (org-end-of-subtree t t))
  19404. nil))
  19405. (defun org-show-entry ()
  19406. "Show the body directly following this heading.
  19407. Show the heading too, if it is currently invisible."
  19408. (interactive)
  19409. (save-excursion
  19410. (condition-case nil
  19411. (progn
  19412. (org-back-to-heading t)
  19413. (outline-flag-region
  19414. (max (point-min) (1- (point)))
  19415. (save-excursion
  19416. (if (re-search-forward
  19417. (concat "[\r\n]\\(" org-outline-regexp "\\)") nil t)
  19418. (match-beginning 1)
  19419. (point-max)))
  19420. nil)
  19421. (org-cycle-hide-drawers 'children))
  19422. (error nil))))
  19423. (defun org-make-options-regexp (kwds &optional extra)
  19424. "Make a regular expression for keyword lines."
  19425. (concat
  19426. "^"
  19427. "#?[ \t]*\\+\\("
  19428. (mapconcat 'regexp-quote kwds "\\|")
  19429. (if extra (concat "\\|" extra))
  19430. "\\):[ \t]*"
  19431. "\\(.*\\)"))
  19432. ;; Make isearch reveal the necessary context
  19433. (defun org-isearch-end ()
  19434. "Reveal context after isearch exits."
  19435. (when isearch-success ; only if search was successful
  19436. (if (featurep 'xemacs)
  19437. ;; Under XEmacs, the hook is run in the correct place,
  19438. ;; we directly show the context.
  19439. (org-show-context 'isearch)
  19440. ;; In Emacs the hook runs *before* restoring the overlays.
  19441. ;; So we have to use a one-time post-command-hook to do this.
  19442. ;; (Emacs 22 has a special variable, see function `org-mode')
  19443. (unless (and (boundp 'isearch-mode-end-hook-quit)
  19444. isearch-mode-end-hook-quit)
  19445. ;; Only when the isearch was not quitted.
  19446. (org-add-hook 'post-command-hook 'org-isearch-post-command
  19447. 'append 'local)))))
  19448. (defun org-isearch-post-command ()
  19449. "Remove self from hook, and show context."
  19450. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  19451. (org-show-context 'isearch))
  19452. ;;;; Integration with and fixes for other packages
  19453. ;;; Imenu support
  19454. (defvar org-imenu-markers nil
  19455. "All markers currently used by Imenu.")
  19456. (make-variable-buffer-local 'org-imenu-markers)
  19457. (defun org-imenu-new-marker (&optional pos)
  19458. "Return a new marker for use by Imenu, and remember the marker."
  19459. (let ((m (make-marker)))
  19460. (move-marker m (or pos (point)))
  19461. (push m org-imenu-markers)
  19462. m))
  19463. (defun org-imenu-get-tree ()
  19464. "Produce the index for Imenu."
  19465. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  19466. (setq org-imenu-markers nil)
  19467. (let* ((n org-imenu-depth)
  19468. (re (concat "^" (org-get-limited-outline-regexp)))
  19469. (subs (make-vector (1+ n) nil))
  19470. (last-level 0)
  19471. m level head)
  19472. (save-excursion
  19473. (save-restriction
  19474. (widen)
  19475. (goto-char (point-max))
  19476. (while (re-search-backward re nil t)
  19477. (setq level (org-reduced-level (funcall outline-level)))
  19478. (when (<= level n)
  19479. (looking-at org-complex-heading-regexp)
  19480. (setq head (org-link-display-format
  19481. (org-match-string-no-properties 4))
  19482. m (org-imenu-new-marker))
  19483. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  19484. (if (>= level last-level)
  19485. (push (cons head m) (aref subs level))
  19486. (push (cons head (aref subs (1+ level))) (aref subs level))
  19487. (loop for i from (1+ level) to n do (aset subs i nil)))
  19488. (setq last-level level)))))
  19489. (aref subs 1)))
  19490. (eval-after-load "imenu"
  19491. '(progn
  19492. (add-hook 'imenu-after-jump-hook
  19493. (lambda ()
  19494. (if (eq major-mode 'org-mode)
  19495. (org-show-context 'org-goto))))))
  19496. (defun org-link-display-format (link)
  19497. "Replace a link with either the description, or the link target
  19498. if no description is present"
  19499. (save-match-data
  19500. (if (string-match org-bracket-link-analytic-regexp link)
  19501. (replace-match (if (match-end 5)
  19502. (match-string 5 link)
  19503. (concat (match-string 1 link)
  19504. (match-string 3 link)))
  19505. nil t link)
  19506. link)))
  19507. (defun org-toggle-link-display ()
  19508. "Toggle the literal or descriptive display of links."
  19509. (interactive)
  19510. (if org-descriptive-links
  19511. (progn (org-remove-from-invisibility-spec '(org-link))
  19512. (org-restart-font-lock)
  19513. (setq org-descriptive-links nil))
  19514. (progn (add-to-invisibility-spec '(org-link))
  19515. (org-restart-font-lock)
  19516. (setq org-descriptive-links t))))
  19517. ;; Speedbar support
  19518. (defvar org-speedbar-restriction-lock-overlay (make-overlay 1 1)
  19519. "Overlay marking the agenda restriction line in speedbar.")
  19520. (overlay-put org-speedbar-restriction-lock-overlay
  19521. 'face 'org-agenda-restriction-lock)
  19522. (overlay-put org-speedbar-restriction-lock-overlay
  19523. 'help-echo "Agendas are currently limited to this item.")
  19524. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  19525. (defun org-speedbar-set-agenda-restriction ()
  19526. "Restrict future agenda commands to the location at point in speedbar.
  19527. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  19528. (interactive)
  19529. (require 'org-agenda)
  19530. (let (p m tp np dir txt)
  19531. (cond
  19532. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  19533. 'org-imenu t))
  19534. (setq m (get-text-property p 'org-imenu-marker))
  19535. (with-current-buffer (marker-buffer m)
  19536. (goto-char m)
  19537. (org-agenda-set-restriction-lock 'subtree)))
  19538. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  19539. 'speedbar-function 'speedbar-find-file))
  19540. (setq tp (previous-single-property-change
  19541. (1+ p) 'speedbar-function)
  19542. np (next-single-property-change
  19543. tp 'speedbar-function)
  19544. dir (speedbar-line-directory)
  19545. txt (buffer-substring-no-properties (or tp (point-min))
  19546. (or np (point-max))))
  19547. (with-current-buffer (find-file-noselect
  19548. (let ((default-directory dir))
  19549. (expand-file-name txt)))
  19550. (unless (eq major-mode 'org-mode)
  19551. (error "Cannot restrict to non-Org-mode file"))
  19552. (org-agenda-set-restriction-lock 'file)))
  19553. (t (error "Don't know how to restrict Org-mode's agenda")))
  19554. (move-overlay org-speedbar-restriction-lock-overlay
  19555. (point-at-bol) (point-at-eol))
  19556. (setq current-prefix-arg nil)
  19557. (org-agenda-maybe-redo)))
  19558. (eval-after-load "speedbar"
  19559. '(progn
  19560. (speedbar-add-supported-extension ".org")
  19561. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  19562. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  19563. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  19564. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  19565. (add-hook 'speedbar-visiting-tag-hook
  19566. (lambda () (and (eq major-mode 'org-mode) (org-show-context 'org-goto))))))
  19567. ;;; Fixes and Hacks for problems with other packages
  19568. ;; Make flyspell not check words in links, to not mess up our keymap
  19569. (defun org-mode-flyspell-verify ()
  19570. "Don't let flyspell put overlays at active buttons, or on
  19571. {todo,all-time,additional-option-like}-keywords."
  19572. (let ((pos (max (1- (point)) (point-min)))
  19573. (word (thing-at-point 'word)))
  19574. (and (not (get-text-property pos 'keymap))
  19575. (not (get-text-property pos 'org-no-flyspell))
  19576. (not (member word org-todo-keywords-1))
  19577. (not (member word org-all-time-keywords))
  19578. (not (member word org-additional-option-like-keywords)))))
  19579. (defun org-remove-flyspell-overlays-in (beg end)
  19580. "Remove flyspell overlays in region."
  19581. (and (org-bound-and-true-p flyspell-mode)
  19582. (fboundp 'flyspell-delete-region-overlays)
  19583. (flyspell-delete-region-overlays beg end))
  19584. (add-text-properties beg end '(org-no-flyspell t)))
  19585. ;; Make `bookmark-jump' shows the jump location if it was hidden.
  19586. (eval-after-load "bookmark"
  19587. '(if (boundp 'bookmark-after-jump-hook)
  19588. ;; We can use the hook
  19589. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  19590. ;; Hook not available, use advice
  19591. (defadvice bookmark-jump (after org-make-visible activate)
  19592. "Make the position visible."
  19593. (org-bookmark-jump-unhide))))
  19594. ;; Make sure saveplace shows the location if it was hidden
  19595. (eval-after-load "saveplace"
  19596. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  19597. "Make the position visible."
  19598. (org-bookmark-jump-unhide)))
  19599. ;; Make sure ecb shows the location if it was hidden
  19600. (eval-after-load "ecb"
  19601. '(defadvice ecb-method-clicked (after esf/org-show-context activate)
  19602. "Make hierarchy visible when jumping into location from ECB tree buffer."
  19603. (if (eq major-mode 'org-mode)
  19604. (org-show-context))))
  19605. (defun org-bookmark-jump-unhide ()
  19606. "Unhide the current position, to show the bookmark location."
  19607. (and (eq major-mode 'org-mode)
  19608. (or (outline-invisible-p)
  19609. (save-excursion (goto-char (max (point-min) (1- (point))))
  19610. (outline-invisible-p)))
  19611. (org-show-context 'bookmark-jump)))
  19612. ;; Make session.el ignore our circular variable
  19613. (eval-after-load "session"
  19614. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  19615. ;;;; Experimental code
  19616. (defun org-closed-in-range ()
  19617. "Sparse tree of items closed in a certain time range.
  19618. Still experimental, may disappear in the future."
  19619. (interactive)
  19620. ;; Get the time interval from the user.
  19621. (let* ((time1 (org-float-time
  19622. (org-read-date nil 'to-time nil "Starting date: ")))
  19623. (time2 (org-float-time
  19624. (org-read-date nil 'to-time nil "End date:")))
  19625. ;; callback function
  19626. (callback (lambda ()
  19627. (let ((time
  19628. (org-float-time
  19629. (apply 'encode-time
  19630. (org-parse-time-string
  19631. (match-string 1))))))
  19632. ;; check if time in interval
  19633. (and (>= time time1) (<= time time2))))))
  19634. ;; make tree, check each match with the callback
  19635. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  19636. ;;;; Finish up
  19637. (provide 'org)
  19638. (run-hooks 'org-load-hook)
  19639. ;;; org.el ends here